1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
32
33
34
35
36
37
38
39
40
41
43
44
45
46
67
68
69
70
71
72
73
74
75
78
79
80
86
87
88
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
118
119
121
127
134
135
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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
227
233
234
235
236
237
239
240
241
242
243
244
245
246
247
248
249
250
251
252
257
258
259
260
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
296
297
298
299
300
301
302
303
304
305
306
307
308
315
316
317
318
323
324
325
326
327
328
329
330
331
332
338
339
340
341
342
343
344
345
346
347
352
353
354
355
356
357
358
359
360
361
366
367
368
369
372
373
374
375
376
381
382
383
384
385
386
387
392
393
394
395
401
406
414
415
420
428
429
434
435
436
437
438
439
440
441
442
446
447
448
449
450
451
452
453
456
457
/* ... */
#include "main.h"
#include <stdio.h>
/* ... */
/* ... */
Includes
#define AUDIO_DEFAULT_VOLUME 70
/* ... */
#define AUDIO_START_OFFSET_ADDRESS 0
#define AUDIO_BUFFER_SIZE 2048
Private define
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
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
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
/* ... */
void AudioPlay_demo (void)
{
uint8_t status = 0;
uint32_t *AudioFreq_ptr;
AudioFreq_ptr = AudioFreq+6;
uint8_t FreqStr[256] = {0};
uwPauseEnabledStatus = 1;
uwVolume = 40;
Audio_SetHint();
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_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 { ... }
/* ... */
AUDIO_Start((uint32_t *)AUDIO_SRC_FILE_ADDRESS, (uint32_t)AUDIO_FILE_SIZE);
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);
/* ... */
while (1)
{
BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
BSP_LCD_SetTextColor(LCD_COLOR_BLUE);
JoyState = BSP_JOY_GetState();
switch (JoyState)
{
case JOY_UP:
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:
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:
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:
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:
if (uwPauseEnabledStatus == 1)
{
BSP_AUDIO_OUT_Resume();
uwPauseEnabledStatus = 0;
BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 80, (uint8_t *)" PLAYING... ", CENTER_MODE);
}if (uwPauseEnabledStatus == 1) { ... }
else
{
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) { ... }
BSP_LED_Toggle(LED3);
HAL_Delay(100);
BSP_LED_Toggle(LED2);
HAL_Delay(100);
if (CheckForUserInput() > 0)
{
BSP_AUDIO_OUT_Stop(CODEC_PDWN_SW);
return;
}if (CheckForUserInput() > 0) { ... }
}while (1) { ... }
}{ ... }
/* ... */
static void Audio_SetHint(void)
{
BSP_LCD_Clear(LCD_COLOR_WHITE);
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);
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);
}{ ... }
/* ... */
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;
}{ ... }
/* ... */
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)
{
buffer_ctl.fptr = 0;
error_state = AUDIO_ERROR_EOF;
}if (buffer_ctl.fptr >= buffer_ctl.AudioFileSize) { ... }
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) { ... }
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;
}{ ... }
/* ... */
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;
}{ ... }
/* ... */
/* ... */
void BSP_AUDIO_OUT_TransferComplete_CallBack(void)
{
if(audio_state == AUDIO_STATE_PLAYING)
{
buffer_ctl.state = BUFFER_OFFSET_FULL;
}if (audio_state == AUDIO_STATE_PLAYING) { ... }
}{ ... }
/* ... */
void BSP_AUDIO_OUT_HalfTransfer_CallBack(void)
{
if(audio_state == AUDIO_STATE_PLAYING)
{
buffer_ctl.state = BUFFER_OFFSET_HALF;
}if (audio_state == AUDIO_STATE_PLAYING) { ... }
}{ ... }
/* ... */
void BSP_AUDIO_OUT_Error_CallBack(void)
{
BSP_LCD_SetBackColor(LCD_COLOR_RED);
BSP_LCD_DisplayStringAt(0, LINE(14), (uint8_t *)" DMA ERROR ", CENTER_MODE);
BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
while (BSP_PB_GetState(BUTTON_TAMPER) != RESET)
{
return;
}while (BSP_PB_GetState(BUTTON_TAMPER) != RESET) { ... }
}{ ... }
/* ... */
/* ... */