8000 working DMA circular audio buffer · nippoo/lawrencebutton@6648964 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6648964

Browse files
committed
working DMA circular audio buffer
1 parent 352abdc commit 6648964

10 files changed

+1705
-1790
lines changed
-56 Bytes
Binary file not shown.
-3.13 KB
Binary file not shown.

CubeMX/LawrenceButton/Debug/LawrenceButton.list

+1,368-1,410
Large diffs are not rendered by default.

CubeMX/LawrenceButton/Debug/LawrenceButton.map

+282-293
Large diffs are not rendered by default.
-4.83 KB
Binary file not shown.
+12-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
main.c:98:5:main 8 static
2-
main.c:182:6:SystemClock_Config 104 static
3-
main.c:233:13:MX_I2C1_Init 8 static
4-
main.c:267:13:MX_I2S2_Init 8 static
5-
main.c:301:13:MX_SDIO_SD_Init 4 static
6-
main.c:327:13:MX_DMA_Init 16 static
7-
main.c:345:13:MX_GPIO_Init 56 static
8-
main.c:435:6:DMA1_Stream5_IRQHandler 8 static
9-
main.c:460:6:StartAudioBuffers 16 static
10-
main.c:470:6:HAL_I2S_TxCpltCallback 16 static
11-
main.c:492:6:HAL_I2S_TxHalfCpltCallback 16 static
12-
main.c:520:6:Error_Handler 4 static
1+
main.c:93:5:main 8 static
2+
main.c:168:6:SystemClock_Config 104 static
3+
main.c:219:13:MX_I2C1_Init 8 static
4+
main.c:253:13:MX_I2S2_Init 8 static
5+
main.c:287:13:MX_SDIO_SD_Init 4 static
6+
main.c:313:13:MX_DMA_Init 16 static
7+
main.c:331:13:MX_GPIO_Init 56 static
8+
main.c:421:6:DMA1_Stream5_IRQHandler 8 static
9+
main.c:426:6:StartAudioBuffers 16 static
10+
main.c:438:6:HAL_I2S_TxCpltCallback 16 static
11+
main.c:460:6:HAL_I2S_TxHalfCpltCallback 16 static
12+
main.c:488:6:Error_Handler 4 static
Binary file not shown.

CubeMX/LawrenceButton/LawrenceButton.ioc

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ PB14.GPIO_Label=GPIO1
2222
RCC.PLLQCLKFreq_Value=47981714.28571428
2323
PC5.Locked=true
2424
PA12.GPIOParameters=GPIO_PuPd,GPIO_Label
25-
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-SystemClock_Config-RCC-false-HAL-false,3-MX_I2C1_Init-I2C1-false-HAL-true,4-MX_I2S2_Init-I2S2-false-HAL-true,5-MX_SDIO_SD_Init-SDIO-false-HAL-true,6-MX_FATFS_Init-FATFS-false-HAL-false
25+
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-MX_DMA_Init-DMA-false-HAL-true,3-SystemClock_Config-RCC-false-HAL-false,4-MX_I2C1_Init-I2C1-false-HAL-true,5-MX_I2S2_Init-I2S2-false-HAL-true,6-MX_SDIO_SD_Init-SDIO-false-HAL-true,7-MX_FATFS_Init-FATFS-false-HAL-false
2626
PA9.GPIOParameters=GPIO_Label
2727
PD8.Locked=true
2828
RCC.RTCFreq_Value=32000
@@ -271,7 +271,7 @@ Mcu.IP7=SYS
271271
ProjectManager.CoupleFile=false
272272
RCC.48MHZClocksFreq_Value=47981714.28571428
273273
PB3.Signal=SYS_JTDO-SWO
274-
Dma.SPI2_TX.0.Priority=DMA_PRIORITY_HIGH
274+
Dma.SPI2_TX.0.Priority=DMA_PRIORITY_LOW
275275
RCC.SYSCLKFreq_VALUE=167936000
276276
Mcu.Pin22=PD9
277277
Mcu.Pin23=PD10

CubeMX/LawrenceButton/Src/main.c

+40-72
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
/* Includes ------------------------------------------------------------------*/
2222
#include "main.h"
2323
#include "fatfs.h"
24-
#include "string.h"
25-
#include "math.h"
2624

2725
/* Private includes ----------------------------------------------------------*/
2826
/* USER CODE BEGIN Includes */
@@ -47,8 +45,8 @@
4745
/* USER CODE END PM */
4846

4947
/* Private variables ---------------------------------------------------------*/
50-
5148
I2C_HandleTypeDef hi2c1;
49+
5250
I2S_HandleTypeDef hi2s2;
5351
DMA_HandleTypeDef hdma_spi2_tx;
5452

@@ -57,7 +55,7 @@ SD_HandleTypeDef hsd;
5755
/* USER CODE BEGIN PV */
5856

5957
/* Ping-Pong buffer used for audio play */
60-
uint16_t dma_buffer [AUDIO_BUFFER_SIZE];
58+
uint16_t dma_buffer [AUDIO_BUFFER_SIZE];
6159

6260
uint32_t AudioPlayStart = 0;
6361

@@ -70,9 +68,6 @@ FRESULT fr;
7068
FATFS myFAT;
7169
UINT br = 0;
7270

73-
float osc_phi = 0;
74-
float osc_phi_inc = 440.0f / 44100.0f; // generating 440HZ
75-
7671
/* USER CODE END PV */
7772

7873
/* Private function prototypes -----------------------------------------------*/
@@ -99,7 +94,7 @@ int main(void)
9994
{
10095
/* USER CODE BEGIN 1 */
10196

102-
/* USER CODE END 1 */
97+
/* USER CODE END 1 */
10398

10499
/* MCU Configuration--------------------------------------------------------*/
105100

@@ -128,41 +123,32 @@ int main(void)
128123

129124
HAL_Delay(300);
130125

131-
// char line[100]; /* Line buffer */
132-
133126
fr = f_mount(&myFAT, (TCHAR const*)SDPath, 1);
134127

135128
/*## Open and create a text file #################################*/
136129
HAL_Delay(300);
137130

138-
fr = f_open(&fil, "001.wav", FA_READ);
131+
fr = f_open(&fil, "003.wav", FA_READ);
139132
if (fr) return (int)fr;
140-
WaveDataLength = f_size(&fil);
141-
//
142-
// /* Read every line and display it */
143-
// while (f_gets(line, sizeof line, &fil)) {
144-
// HAL_Delay(10);
145-
// }
146-
//
147-
// /* Close the file */
148-
// f_close(&fil);
133+
// WaveDataLength = f_size(&fil);
134+
135+
// start circular dma
136+
f_rewind(&fil);
137+
f_read(&fil, &dma_buffer[0], AUDIO_BUFFER_SIZE*2, &br);
149138

150139
/* USER CODE END 2 */
151140

152141
/* Infinite loop */
153142
/* USER CODE BEGIN WHILE */
154143
while (1)
155144
{
156-
// HAL_Delay(1000);
157145
if (HAL_GPIO_ReadPin(GPIOA, SENSOR_Pin) == GPIO_PIN_SET)
158146
{
159147
HAL_GPIO_WritePin(GPIOA, LED_STATUS_Pin, GPIO_PIN_SET);
160-
f_lseek(&fil, 0);
161-
f_read(&fil, &dma_buffer[0], AUDIO_BUFFER_SIZE, &br);
162-
AudioRemSize = WaveDataLength - AUDIO_BUFFER_SIZE;
163-
// HAL_I2S_Transmit_DMA(&hi2s2, (uint16_t*)&Audio_Buffer[0], AUDIO_BUFFER_SIZE);
148+
// f_lseek(&fil, 0);
149+
// f_read(&fil, &dma_buffer[0], AUDIO_BUFFER_SIZE, &br);
150+
// AudioRemSize = WaveDataLength - br;
164151
StartAudioBuffers(&hi2s2);
165-
HAL_Delay(500);
166152
}
167153
else
168154
{
@@ -437,34 +423,16 @@ void DMA1_Stream5_IRQHandler(void) // this function must be included to avoid DM
437423
HAL_DMA_IRQHandler(&hdma_spi2_tx);
438424
}
439425

440-
//void FillAudioBuffer (uint32_t *buffer, uint16_t len)
441-
//{
442-
// float a;
443-
// int16_t y;
444-
// uint16_t c;
445-
// for (c = 0; c < len; c++)
446-
// {
447-
// // calculate sin
448-
// a = (float) sin (osc_phi * 6.2832f) * 0.20f;
449-
// osc_phi += osc_phi_inc;
450-
// osc_phi -= (float) ((uint16_t) osc_phi);
451-
// // float to integer
452-
// y = (int16_t) (a * 32767.0f);
453-
// // auf beide kanäle
454-
// buffer [c] = ((uint32_t) (uint16_t) y) << 0 |
455-
// ((uint32_t) (uint16_t) y) << 16;
456-
//
457-
// }
458-
//}
459-
460426
void StartAudioBuffers (I2S_HandleTypeDef *hi2s)
461427
{
462428
// clear buffer
463429
// memset (dma_buffer,0, sizeof (dma_buffer ));
464430
HAL_GPIO_WritePin(GPIOE, LCD_E_Pin, GPIO_PIN_SET);
465431
HAL_GPIO_WritePin(GPIOE, LCD_DIG1CC_Pin, GPIO_PIN_SET);
466-
// start circular dma
467-
HAL_I2S_Transmit_DMA (hi2s, (uint32_t *) dma_buffer, AUDIO_BUFFER_SIZE << 1);
432+
433+
434+
// AudioRemSize = WaveDataLength - br;
435+
HAL_I2S_Transmit_DMA (hi2s, dma_buffer, AUDIO_BUFFER_SIZE);
468436
}
469437

470438
void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)
@@ -473,41 +441,41 @@ void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)
473441
// FillBuffer (&(dma_buffer [AUDIO_BUFFER_SIZE >> 1]), AUDIO_BUFFER_SIZE >> 1);
474442
f_read(&fil,
475443
&dma_buffer[AUDIO_BUFFER_SIZE/2],
476-
AUDIO_BUFFER_SIZE/2,
444+
AUDIO_BUFFER_SIZE,
477445
(void *)&br);
478446

479-
if(AudioRemSize > (AUDIO_BUFFER_SIZE / 2))
480-
{
481-
AudioRemSize -= br;
482-
}
483-
else
484-
{
485-
AudioRemSize = 0;
486-
}
487-
488-
// HAL_GPIO_WritePin(GPIOE, LCD_G_Pin, GPIO_PIN_SET);
489-
// HAL_GPIO_WritePin(GPIOE, LCD_F_Pin, GPIO_PIN_RESET);
447+
// if(AudioRemSize > (AUDIO_BUFFER_SIZE / 2))
448+
// {
449+
// AudioRemSize -= br;
450+
// }
451+
// else
452+
// {
453+
// AudioRemSize = 0;
454+
// }
455+
456+
HAL_GPIO_WritePin(GPIOE, LCD_G_Pin, GPIO_PIN_SET);
457+
HAL_GPIO_WritePin(GPIOE, LCD_F_Pin, GPIO_PIN_RESET);
490458
}
491459

492460
void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
493461
{
494462
// first half finished, filling it up again while second half is playing
495463
f_read(&fil,
496464
&dma_buffer[0],
497-
AUDIO_BUFFER_SIZE/2,
465+
AUDIO_BUFFER_SIZE,
498466
(void *)&br);
499467

500-
if(AudioRemSize > (AUDIO_BUFFER_SIZE / 2))
501-
{
502-
AudioRemSize -= br;
503-
}
504-
else
505-
{
506-
AudioRemSize = 0;
507-
}
508-
509-
// HAL_GPIO_WritePin(GPIOE, LCD_F_Pin, GPIO_PIN_SET);
510-
// HAL_GPIO_WritePin(GPIOE, LCD_G_Pin, GPIO_PIN_RESET);
468+
// if(AudioRemSize > (AUDIO_BUFFER_SIZE / 2))
469+
// {
470+
// AudioRemSize -= br;
471+
// }
472+
// else
473+
// {
474+
// AudioRemSize = 0;
475+
// }
476+
477+
HAL_GPIO_WritePin(GPIOE, LCD_F_Pin, GPIO_PIN_SET);
478+
HAL_GPIO_WritePin(GPIOE, LCD_G_Pin, GPIO_PIN_RESET);
511479
// FillBuffer (&(dma_buffer [0]), AUDIO_BUFFER_SIZE >> 1);
512480
}
513481

CubeMX/LawrenceButton/Src/stm32f4xx_hal_msp.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ void HAL_I2S_MspInit(I2S_HandleTypeDef* hi2s)
192192
hdma_spi2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
193193
hdma_spi2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
194194
hdma_spi2_tx.Init.Mode = DMA_CIRCULAR;
195-
hdma_spi2_tx.Init.Priority = DMA_PRIORITY_HIGH;
195+
hdma_spi2_tx.Init.Priority = DMA_PRIORITY_LOW;
196196
hdma_spi2_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
197197
if (HAL_DMA_Init(&hdma_spi2_tx) != HAL_OK)
198198
{

0 commit comments

Comments
 (0)
0