BSP
Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "main.h"
#include <stdio.h>
Private define
#define AUDIO_DEFAULT_VOLUME
#define AUDIO_START_OFFSET_ADDRESS
#define AUDIO_BUFFER_SIZE
Private typedef
AUDIO_PLAYBACK_StateTypeDef
BUFFER_StateTypeDef
AUDIO_BufferTypeDef
Private variables
buffer_ctl
audio_state
uwVolume
uwPauseEnabledStatus
AudioFreq
JoyState
Private function prototypes
AudioPlay_demo()
Audio_SetHint()
AUDIO_Start(uint32_t *, uint32_t)
AUDIO_Process()
GetData(void *, uint32_t, uint8_t *, uint32_t)
BSP_AUDIO_OUT_TransferComplete_CallBack()
BSP_AUDIO_OUT_HalfTransfer_CallBack()
BSP_AUDIO_OUT_Error_CallBack()
Files
loading...
CodeScopeSTM32 Libraries and SamplesBSPSrc/audio_play.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file BSP/Src/audio.c_play * @author MCD Application Team * @brief This example code shows how to use the audio feature in the * stm32469i_eval driver ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* ... */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include <stdio.h> /** @addtogroup STM32F4xx_HAL_Examples * @{ *//* ... */ /** @addtogroup BSP * @{ *//* ... */ Includes /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /*Since SysTick is set to 1ms (unless to set it quicker) */ /* to run up to 48khz, a buffer around 1000 (or more) is requested*/ /* to run up to 96khz, a buffer around 2000 (or more) is requested*/ #define AUDIO_DEFAULT_VOLUME 70 /* Audio file size and start address are defined here since the audio file is stored in Flash memory as a constant table of 16-bit data *//* ... */ #define AUDIO_START_OFFSET_ADDRESS 0 /* Offset relative to audio file header size */ #define AUDIO_BUFFER_SIZE 2048 Private define /* Private typedef -----------------------------------------------------------*/ typedef enum { AUDIO_STATE_IDLE = 0, AUDIO_STATE_INIT, AUDIO_STATE_PLAYING, ...}AUDIO_PLAYBACK_StateTypeDef; typedef enum { BUFFER_OFFSET_NONE = 0, BUFFER_OFFSET_HALF, BUFFER_OFFSET_FULL, ...}BUFFER_StateTypeDef; typedef struct { uint8_t buff[AUDIO_BUFFER_SIZE]; uint32_t fptr; BUFFER_StateTypeDef state; uint32_t AudioFileSize; uint32_t *SrcAddress; ...}AUDIO_BufferTypeDef; Private typedef /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ static AUDIO_BufferTypeDef buffer_ctl; static AUDIO_PLAYBACK_StateTypeDef audio_state; __IO uint32_t uwVolume = 20; __IO uint32_t uwPauseEnabledStatus = 0; static uint32_t AudioFreq[9] = { 8000 , 11025, 16000, 22050, 32000, 44100, 48000, 96000, 192000 ...}; static JOYState_TypeDef JoyState = JOY_NONE; Private variables /* Private function prototypes -----------------------------------------------*/ static void Audio_SetHint(void); static uint32_t GetData(void *pdata, uint32_t offset, uint8_t *pbuf, uint32_t NbrOfData); AUDIO_ErrorTypeDef AUDIO_Start(uint32_t *psrc_address, uint32_t file_size); AUDIO_ErrorTypeDef AUDIO_Stop(void); Private function prototypes /* Private functions ---------------------------------------------------------*/ /** * @brief Audio Play demo * @param None * @retval None *//* ... */ void AudioPlay_demo (void) { uint8_t status = 0; uint32_t *AudioFreq_ptr; AudioFreq_ptr = AudioFreq+6; /*AF_48K*/ uint8_t FreqStr[256] = {0}; uwPauseEnabledStatus = 1; /* 0 when audio is running, 1 when Pause is on */ uwVolume = 40; Audio_SetHint(); /* Pin overlay between SAI1_SDA and PAR_D7 of the camera */ /* Need to make sure the camera is power down */ BSP_CAMERA_PwrDown(); status = BSP_JOY_Init(JOY_MODE_GPIO); if (status != IO_OK) { BSP_LCD_SetBackColor(LCD_COLOR_WHITE); BSP_LCD_SetTextColor(LCD_COLOR_RED); BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 95, (uint8_t *)"ERROR", CENTER_MODE); BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 80, (uint8_t *)"Joystick cannot be initialized", CENTER_MODE); }if (status != IO_OK) { ... } /* if(BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_HEADPHONE1, uwVolume, *AudioFreq_ptr) == 0) if(BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_HEADPHONE2, uwVolume, *AudioFreq_ptr) == 0)*//* ... */ if(BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_BOTH, uwVolume, *AudioFreq_ptr) == 0) { BSP_LCD_SetBackColor(LCD_COLOR_WHITE); BSP_LCD_SetTextColor(LCD_COLOR_GREEN); BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 95, (uint8_t *)" AUDIO CODEC OK ", CENTER_MODE); }if (BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_BOTH, uwVolume, *AudioFreq_ptr) == 0) { ... } else { BSP_LCD_SetBackColor(LCD_COLOR_WHITE); BSP_LCD_SetTextColor(LCD_COLOR_RED); BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 95, (uint8_t *)" AUDIO CODEC FAIL ", CENTER_MODE); BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 80, (uint8_t *)" Try to reset board ", CENTER_MODE); }else { ... } /* Start playing the file from a circular buffer, once the DMA is enabled, it is always in running state. Application has to fill the buffer with the audio data using Transfer complete and/or half transfer complete interrupts callbacks (EVAL_AUDIO_TransferComplete_CallBack() or EVAL_AUDIO_HalfTransfer_CallBack()... *//* ... */ AUDIO_Start((uint32_t *)AUDIO_SRC_FILE_ADDRESS, (uint32_t)AUDIO_FILE_SIZE); /* Display the state on the screen */ BSP_LCD_SetBackColor(LCD_COLOR_WHITE); BSP_LCD_SetTextColor(LCD_COLOR_BLUE); BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 80, (uint8_t *)" PLAYING... ", CENTER_MODE); sprintf((char*)FreqStr, " VOL: %3lu ", uwVolume); BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 65, (uint8_t *)FreqStr, CENTER_MODE); sprintf((char*)FreqStr, " FREQ: %6lu ", *AudioFreq_ptr); BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 50, (uint8_t *)FreqStr, CENTER_MODE); BSP_LCD_DisplayStringAt(0, LINE(15), (uint8_t *)"Hear nothing ? Have you copied the audio file with STM-LINK UTILITY ?", CENTER_MODE); /* IMPORTANT: AUDIO_Process() is called by the SysTick Handler, as it should be called within a periodic process *//* ... */ /* Infinite loop */ while (1) { BSP_LCD_SetBackColor(LCD_COLOR_WHITE); BSP_LCD_SetTextColor(LCD_COLOR_BLUE); /* Get the Joystick State */ JoyState = BSP_JOY_GetState(); switch (JoyState) { case JOY_UP: /* Increase volume by 5% */ if (uwVolume < 95) uwVolume += 5; else uwVolume = 100; sprintf((char*)FreqStr, " VOL: %3lu ", uwVolume); BSP_AUDIO_OUT_SetVolume(uwVolume); BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 65, (uint8_t *)FreqStr, CENTER_MODE); BSP_LCD_DisplayStringAt(0, LINE(14), (uint8_t *)" ", CENTER_MODE); break;case JOY_UP: case JOY_DOWN: /* Decrease volume by 5% */ if (uwVolume > 5) uwVolume -= 5; else uwVolume = 0; sprintf((char*)FreqStr, " VOL: %3lu ", uwVolume); BSP_AUDIO_OUT_SetVolume(uwVolume); BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 65, (uint8_t *)FreqStr, CENTER_MODE); BSP_LCD_DisplayStringAt(0, LINE(14), (uint8_t *)" ", CENTER_MODE); break;case JOY_DOWN: case JOY_LEFT: /*Decrease Frequency */ if (*AudioFreq_ptr != 8000) { AudioFreq_ptr--; sprintf((char*)FreqStr, " FREQ: %6lu ", *AudioFreq_ptr); BSP_AUDIO_OUT_Stop(CODEC_PDWN_SW); BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_BOTH, uwVolume, *AudioFreq_ptr); AUDIO_Start((uint32_t *)AUDIO_SRC_FILE_ADDRESS, (uint32_t)AUDIO_FILE_SIZE); BSP_AUDIO_OUT_SetVolume(uwVolume); }if (*AudioFreq_ptr != 8000) { ... } BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 50, (uint8_t *)FreqStr, CENTER_MODE); BSP_LCD_DisplayStringAt(0, LINE(14), (uint8_t *)" ", CENTER_MODE); break;case JOY_LEFT: case JOY_RIGHT: /* Increase Frequency */ if (*AudioFreq_ptr != 96000) { AudioFreq_ptr++; sprintf((char*)FreqStr, " FREQ: %6lu ", *AudioFreq_ptr); BSP_AUDIO_OUT_Stop(CODEC_PDWN_SW); BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_BOTH, uwVolume, *AudioFreq_ptr); AUDIO_Start((uint32_t *)AUDIO_SRC_FILE_ADDRESS, (uint32_t)AUDIO_FILE_SIZE); BSP_AUDIO_OUT_SetVolume(uwVolume); }if (*AudioFreq_ptr != 96000) { ... } BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 50, (uint8_t *)FreqStr, CENTER_MODE); BSP_LCD_DisplayStringAt(0, LINE(14), (uint8_t *)" ", CENTER_MODE); break;case JOY_RIGHT: case JOY_SEL: /* Set Pause / Resume */ if (uwPauseEnabledStatus == 1) { /* Pause is enabled, call Resume */ BSP_AUDIO_OUT_Resume(); uwPauseEnabledStatus = 0; BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 80, (uint8_t *)" PLAYING... ", CENTER_MODE); }if (uwPauseEnabledStatus == 1) { ... } else { /* Pause the playback */ BSP_AUDIO_OUT_Pause(); uwPauseEnabledStatus = 1; BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 80, (uint8_t *)" PAUSE ... ", CENTER_MODE); }else { ... } BSP_LCD_DisplayStringAt(0, LINE(14), (uint8_t *)" ", CENTER_MODE); HAL_Delay(200); break; case JOY_SEL: default: break;default }switch (JoyState) { ... } /* Toggle LED4 */ BSP_LED_Toggle(LED3); /* Insert 100 ms delay */ HAL_Delay(100); /* Toggle LED2 */ BSP_LED_Toggle(LED2); /* Insert 100 ms delay */ HAL_Delay(100); if (CheckForUserInput() > 0) { BSP_AUDIO_OUT_Stop(CODEC_PDWN_SW); return; }if (CheckForUserInput() > 0) { ... } }while (1) { ... } }{ ... } /** * @brief Display Audio demo hint * @param None * @retval None *//* ... */ static void Audio_SetHint(void) { /* Clear the LCD */ BSP_LCD_Clear(LCD_COLOR_WHITE); /* Set Audio Demo description */ BSP_LCD_SetTextColor(LCD_COLOR_BLUE); BSP_LCD_FillRect(0, 0, BSP_LCD_GetXSize(), 90); BSP_LCD_SetTextColor(LCD_COLOR_WHITE); BSP_LCD_SetBackColor(LCD_COLOR_BLUE); BSP_LCD_SetFont(&Font24); BSP_LCD_DisplayStringAt(0, 0, (uint8_t *)"AUDIO EXAMPLE", CENTER_MODE); BSP_LCD_SetFont(&Font12); BSP_LCD_DisplayStringAt(0, 30, (uint8_t *)"Press Tamper button for next menu ", CENTER_MODE); BSP_LCD_DisplayStringAt(0, 45, (uint8_t *)"Use Joystick Up/Down to change Volume ", CENTER_MODE); BSP_LCD_DisplayStringAt(0, 60, (uint8_t *)"Use Joystick Left/Right to change Frequency", CENTER_MODE); BSP_LCD_DisplayStringAt(0, 75, (uint8_t *)"Click on Joystick for Pause/Resume ", CENTER_MODE); /* Set the LCD Text Color */ BSP_LCD_SetTextColor(LCD_COLOR_BLUE); BSP_LCD_DrawRect(10, 100, BSP_LCD_GetXSize() - 20, BSP_LCD_GetYSize() - 110); BSP_LCD_DrawRect(11, 101, BSP_LCD_GetXSize() - 22, BSP_LCD_GetYSize() - 112); }{ ... } /** * @brief Starts Audio streaming. * @param None * @retval Audio error *//* ... */ AUDIO_ErrorTypeDef AUDIO_Start(uint32_t *psrc_address, uint32_t file_size) { uint32_t bytesread; buffer_ctl.state = BUFFER_OFFSET_NONE; buffer_ctl.AudioFileSize = file_size; buffer_ctl.SrcAddress = psrc_address; bytesread = GetData( (void *)psrc_address, 0, &buffer_ctl.buff[0], AUDIO_BUFFER_SIZE); if(bytesread > 0) { BSP_AUDIO_OUT_Play((uint16_t *)&buffer_ctl.buff[0], AUDIO_BUFFER_SIZE); audio_state = AUDIO_STATE_PLAYING; buffer_ctl.fptr = bytesread; return AUDIO_ERROR_NONE; }if (bytesread > 0) { ... } return AUDIO_ERROR_IO; }{ ... } /** * @brief Manages Audio process. * @param None * @retval Audio error *//* ... */ uint8_t AUDIO_Process(void) { uint32_t bytesread; AUDIO_ErrorTypeDef error_state = AUDIO_ERROR_NONE; switch(audio_state) { case AUDIO_STATE_PLAYING: if(buffer_ctl.fptr >= buffer_ctl.AudioFileSize) { /* Play audio sample again ... */ buffer_ctl.fptr = 0; error_state = AUDIO_ERROR_EOF; }if (buffer_ctl.fptr >= buffer_ctl.AudioFileSize) { ... } /* 1st half buffer played; so fill it and continue playing from bottom*/ if(buffer_ctl.state == BUFFER_OFFSET_HALF) { bytesread = GetData((void *)buffer_ctl.SrcAddress, buffer_ctl.fptr, &buffer_ctl.buff[0], AUDIO_BUFFER_SIZE /2); if( bytesread >0) { buffer_ctl.state = BUFFER_OFFSET_NONE; buffer_ctl.fptr += bytesread; }if (bytesread >0) { ... } }if (buffer_ctl.state == BUFFER_OFFSET_HALF) { ... } /* 2nd half buffer played; so fill it and continue playing from top */ if(buffer_ctl.state == BUFFER_OFFSET_FULL) { bytesread = GetData((void *)buffer_ctl.SrcAddress, buffer_ctl.fptr, &buffer_ctl.buff[AUDIO_BUFFER_SIZE /2], AUDIO_BUFFER_SIZE /2); if( bytesread > 0) { buffer_ctl.state = BUFFER_OFFSET_NONE; buffer_ctl.fptr += bytesread; }if (bytesread > 0) { ... } }if (buffer_ctl.state == BUFFER_OFFSET_FULL) { ... } break; case AUDIO_STATE_PLAYING: default: error_state = AUDIO_ERROR_NOTREADY; break;default }switch (audio_state) { ... } return (uint8_t) error_state; }{ ... } /** * @brief Gets Data from storage unit. * @param None * @retval None *//* ... */ static uint32_t GetData(void *pdata, uint32_t offset, uint8_t *pbuf, uint32_t NbrOfData) { uint8_t *lptr = pdata; uint32_t ReadDataNbr; ReadDataNbr = 0; while(((offset + ReadDataNbr) < buffer_ctl.AudioFileSize) && (ReadDataNbr < NbrOfData)) { pbuf[ReadDataNbr]= lptr [offset + ReadDataNbr]; ReadDataNbr++; }while (((offset + ReadDataNbr) < buffer_ctl.AudioFileSize) && (ReadDataNbr < NbrOfData)) { ... } return ReadDataNbr; }{ ... } /*------------------------------------------------------------------------------ Callbacks implementation: the callbacks API are defined __weak in the stm32469i_eval_audio.c file and their implementation should be done the user code if they are needed. Below some examples of callback implementations. ----------------------------------------------------------------------------*//* ... */ /** * @brief Manages the full Transfer complete event. * @param None * @retval None *//* ... */ void BSP_AUDIO_OUT_TransferComplete_CallBack(void) { if(audio_state == AUDIO_STATE_PLAYING) { /* allows AUDIO_Process() to refill 2nd part of the buffer */ buffer_ctl.state = BUFFER_OFFSET_FULL; }if (audio_state == AUDIO_STATE_PLAYING) { ... } }{ ... } /** * @brief Manages the DMA Half Transfer complete event. * @param None * @retval None *//* ... */ void BSP_AUDIO_OUT_HalfTransfer_CallBack(void) { if(audio_state == AUDIO_STATE_PLAYING) { /* allows AUDIO_Process() to refill 1st part of the buffer */ buffer_ctl.state = BUFFER_OFFSET_HALF; }if (audio_state == AUDIO_STATE_PLAYING) { ... } }{ ... } /** * @brief Manages the DMA FIFO error event. * @param None * @retval None *//* ... */ void BSP_AUDIO_OUT_Error_CallBack(void) { /* Display message on the LCD screen */ BSP_LCD_SetBackColor(LCD_COLOR_RED); BSP_LCD_DisplayStringAt(0, LINE(14), (uint8_t *)" DMA ERROR ", CENTER_MODE); BSP_LCD_SetBackColor(LCD_COLOR_WHITE); /* Stop the program with an infinite loop */ while (BSP_PB_GetState(BUTTON_TAMPER) != RESET) { return; }while (BSP_PB_GetState(BUTTON_TAMPER) != RESET) { ... } /* could also generate a system reset to recover from the error */ /* .... */ }{ ... } /** * @} *//* ... */ /** * @} *//* ... */