1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
22
23
24
25
29
32
33
34
39
50
51
56
57
58
59
60
61
62
63
64
65
72
73
74
75
80
81
82
83
84
85
86
87
88
89
95
96
97
98
99
100
101
102
103
104
109
110
111
112
113
114
115
116
117
118
123
124
125
126
129
130
131
132
133
134
139
140
141
142
143
144
145
150
151
152
153
159
160
165
174
175
180
189
190
195
196
197
198
199
200
201
202
204
205
206
207
/* ... */
#include "audio_if.h"
Includes
static AUDIO_BufferTypeDef buffer_ctl;
static AUDIO_PLAYBACK_StateTypeDef audio_state;
Private variables
static uint32_t GetData(void *pdata, uint32_t offset, uint8_t *pbuf, uint32_t NbrOfData);
Private function prototypes
/* ... */
AUDIO_ErrorTypeDef AUDIO_Init(void)
{
audio_state = AUDIO_STATE_IDLE;
if(BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_AUTO, AUDIO_DEFAULT_VOLUME, I2S_AUDIOFREQ_8K) == 0)
{
audio_state = AUDIO_STATE_INIT;
return AUDIO_ERROR_NONE;
}if (BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_AUTO, AUDIO_DEFAULT_VOLUME, I2S_AUDIOFREQ_8K) == 0) { ... }
return AUDIO_ERROR_IO;
}{ ... }
/* ... */
AUDIO_ErrorTypeDef AUDIO_Start(void)
{
uint32_t bytesread;
buffer_ctl.state = BUFFER_OFFSET_NONE;
bytesread = GetData( (void *)AUDIO_FILE_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;
}{ ... }
/* ... */
AUDIO_ErrorTypeDef AUDIO_Process(void)
{
uint32_t bytesread;
AUDIO_ErrorTypeDef error_state = AUDIO_ERROR_NONE;
switch(audio_state)
{
case AUDIO_STATE_PLAYING:
if(buffer_ctl.fptr >= AUDIO_FILE_SIZE)
{
buffer_ctl.fptr = 0;
error_state = AUDIO_ERROR_EOF;
}if (buffer_ctl.fptr >= AUDIO_FILE_SIZE) { ... }
if(buffer_ctl.state == BUFFER_OFFSET_HALF)
{
bytesread = GetData((void *)AUDIO_FILE_ADDRESS,
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 *)AUDIO_FILE_ADDRESS,
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 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) < AUDIO_FILE_SIZE) && (ReadDataNbr < NbrOfData))
{
pbuf[ReadDataNbr]= lptr [offset + ReadDataNbr];
ReadDataNbr++;
}while (((offset + ReadDataNbr) < AUDIO_FILE_SIZE) && (ReadDataNbr < NbrOfData)) { ... }
return ReadDataNbr;
}{ ... }
/* ... */
/* ... */
void BSP_AUDIO_OUT_TransferComplete_CallBack(void)
{
if(audio_state == AUDIO_STATE_PLAYING)
{
BSP_AUDIO_OUT_ChangeBuffer((uint16_t*)&buffer_ctl.buff[0], AUDIO_BUFFER_SIZE /2);
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)
{
BSP_AUDIO_OUT_ChangeBuffer((uint16_t*)&buffer_ctl.buff[AUDIO_BUFFER_SIZE /2], AUDIO_BUFFER_SIZE /2);
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_DisplayStringAtLine(8, (uint8_t *)" DMA ERROR ");
while (1)
{}while (1) { ... }
}{ ... }