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
|
/*
* Copyright (c) 2006-2025 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-05-01 wumingzi first version
* 2025-11-24 ChuanN-sudo add standardized utest documentation block
*/
/**
* Test Case Name: Audio Test
*
* Test Objectives:
* - Validate RT-Thread audio driver framework registration and data flow mechanisms.
* - Verify audio playback (TX) and recording (RX) data transmission correctness.
* - Ensure proper buffer management and memory integrity during audio operations.
* - Test core APIs: rt_audio_register(), rt_audio_tx_complete(), rt_audio_rx_done()
*
* Test Scenarios:
* - Locate audio devices using SOUND_PLAYER_DEVICE_NAME and SOUND_MIC_DEVICE_NAME macros.
* - Write known byte patterns (0xAA, 0x55) to simulate audio data flow through TX/RX buffers.
* - Synchronize player and microphone test execution using audio_fsm_step state machine.
* - Test two consecutive block transfers for both TX and RX paths.
*
* Verification Metrics:
* - Written audio data appears correctly in device TX buffers with exact byte patterns.
* - Data read from microphone device matches expected fill patterns (0xAA then 0x55) without corruption.
* - Buffer boundaries are respected with no overflow or underflow conditions.
* - Device open/close operations complete successfully without resource leaks.
*
* Dependencies:
* - Hardware requirements: QEMU emulator or any hardware platform that supports RT-Thread.
* - Software configuration:
* - RT_USING_UTESTCASES must be enabled (select "RT-Thread Utestcases" in menuconfig).
* - RT_USING_AUDIO must be enabled (enable via: RT-Thread Components -> Device Drivers -> Using Audio device drivers).
* - RT_UTEST_USING_AUDIO_DRIVER must be enabled (enable via: RT-Thread Utestcases -> Kernel Components-> Drivers -> Audio Test).
* - Environmental Assumptions: Audio devices registered and functional, sufficient memory available.
*
* Expected Results:
* - Final output: "[ PASSED ] [ result ] testcase (components.drivers.audio.tc_audio_main)"
* - No memory leaks detected during buffer allocation/deallocation.
* - No assertions triggered during test execution.
* - Buffer content verification passes for all test patterns.
*/
#include "tc_audio_common.h"
rt_uint8_t audio_fsm_step = 0;
/* Allocate and initialize memory filled by fill_byte */
static void *alloc_filled_mem(rt_uint8_t fill_byte, rt_size_t size)
{
void *ptr = rt_malloc(size);
if (ptr != NULL)
{
rt_memset(ptr, fill_byte, size);
}
return ptr;
}
/* Check if the memory is filled with fill_byte */
static rt_err_t check_filled_mem(rt_uint8_t fill_byte, rt_uint8_t *mem, size_t size)
{
rt_uint8_t *p = mem;
for (size_t i = 0; i < size; ++i)
{
if (*(p+i) != fill_byte)
{
return -RT_ERROR;
}
}
return RT_EOK;
}
static void player_test(void)
{
int res = 0;
void* player_buffer = RT_NULL;
rt_device_t dev_obj;
dev_obj = rt_device_find(SOUND_PLAYER_DEVICE_NAME);
if (dev_obj == RT_NULL)
{
uassert_not_null(dev_obj);
goto __exit;
}
if (dev_obj->type != RT_Device_Class_Sound)
{
LOG_E("Not an audio player device\n");
goto __exit;
}
res = rt_device_open(dev_obj, RT_DEVICE_OFLAG_WRONLY);
if (res != RT_EOK)
{
LOG_E("Audio player device failed\n");
uassert_true(0);
goto __exit;
}
/* The sampling rate is set by the driver default, so there isn't configuration step */
struct rt_audio_device *audio_dev = rt_container_of(dev_obj, struct rt_audio_device, parent);
struct rt_audio_buf_info buf_info = audio_dev->replay->buf_info;
struct sound_device *snd_dev = rt_container_of(audio_dev, struct sound_device, audio);
player_buffer = alloc_filled_mem(0xAA, TX_DMA_BLOCK_SIZE);
if (player_buffer == RT_NULL)
{
rt_kprintf("Allocate test memory failed\n");
uassert_true(0);
goto __exit;
}
if(snd_dev->tx_fifo == RT_NULL)
{
rt_kprintf("snd_dev->tx_fifo == RT_NULL ");
uassert_true(0);
goto __exit;
}
res = rt_device_write(dev_obj, 0, player_buffer, TX_DMA_BLOCK_SIZE);
if (res != RT_EOK && res != TX_DMA_BLOCK_SIZE)
{
rt_kprintf("Failed to write data to the player device, res = %d\n",res);
uassert_true(0);
goto __exit;
}
audio_fsm_step = 1;
while (1)
{
if(audio_fsm_step == 2)
{
break;
}
rt_thread_mdelay(10);
}
res = check_filled_mem(0xAA, &buf_info.buffer[0], TX_DMA_BLOCK_SIZE);
if (res != RT_EOK)
{
rt_kprintf("The first memory check failed! Buffer dump\n");
for (rt_size_t i = 0; i < TX_DMA_FIFO_SIZE; i++)
{
rt_kprintf("%02X ", buf_info.buffer[i]);
if (i % 16 == 15) rt_kprintf("\n");
}
rt_kprintf("\n");
uassert_true(0);
goto __exit;
}
rt_free(player_buffer);
player_buffer = RT_NULL;
player_buffer = alloc_filled_mem(0x55, TX_DMA_BLOCK_SIZE);
if (player_buffer == RT_NULL)
{
rt_kprintf("Allocate test memory failed\n");
uassert_true(0);
goto __exit;
}
res = rt_device_write(dev_obj, TX_DMA_BLOCK_SIZE, player_buffer, TX_DMA_BLOCK_SIZE);
if (res != RT_EOK && res != TX_DMA_BLOCK_SIZE)
{
rt_kprintf("Failed to write data to the player device, res = %d\n",res);
uassert_true(0);
goto __exit;
}
audio_fsm_step = 2;
while (res != RT_EOK)
{
if(audio_fsm_step == 3)
{
break;
}
rt_thread_mdelay(10);
}
res = check_filled_mem(0x55,&buf_info.buffer[TX_DMA_BLOCK_SIZE], TX_DMA_BLOCK_SIZE);
if (res != RT_EOK)
{
rt_kprintf("The second memory check failed! Buffer dump\n");
for (rt_size_t i = 0; i < TX_DMA_FIFO_SIZE; i++)
{
rt_kprintf("%02X ", buf_info.buffer[i]);
if (i % 16 == 15) rt_kprintf("\n");
}
rt_kprintf("\n");
uassert_true(0);
goto __exit;
}
__exit:
if (player_buffer)
{
rt_free(player_buffer);
player_buffer = RT_NULL;
}
if (dev_obj != RT_NULL)
{
audio_fsm_step = 4;
rt_device_close(dev_obj);
}
}
static void mic_test(void)
{
rt_device_t dev_obj;
rt_uint8_t *mic_buffer = RT_NULL;
rt_ssize_t res = 0;
rt_ssize_t length = 0;
mic_buffer = (rt_uint8_t *)rt_malloc(RX_DMA_BLOCK_SIZE);
if (mic_buffer == RT_NULL)
{
rt_kprintf("The mic_buffer memory allocate failed\n");
uassert_true(0);
goto __exit;
}
dev_obj = rt_device_find(SOUND_MIC_DEVICE_NAME);
if (dev_obj == RT_NULL)
{
LOG_E("Not a mic device\n");
uassert_true(0);
goto __exit;
}
res = rt_device_open(dev_obj, RT_DEVICE_OFLAG_RDONLY);
if (res != RT_EOK)
{
LOG_E("Audio player device failed\n");
uassert_true(0);
goto __exit;
}
length = rt_device_read(dev_obj, 0, mic_buffer,RX_DMA_BLOCK_SIZE);
if(length < 0)
{
LOG_E("Mic device read err\n");
}
if(audio_fsm_step == 1)
{
res = check_filled_mem(0xAA, (rt_uint8_t*)(mic_buffer), length);
}
if (res != RT_EOK)
{
LOG_E("The first memory check failed! Buffer dump\n");
for (rt_size_t i = 0; i < RX_DMA_FIFO_SIZE; i++)
{
rt_kprintf("%02X ",mic_buffer[i]);
if (i % 16 == 15) rt_kprintf("\n");
}
rt_kprintf("\n");
uassert_true(0);
goto __exit;
}
audio_fsm_step = 2;
while (1)
{
if(audio_fsm_step == 3)
{
length = rt_device_read(dev_obj, 0, mic_buffer, RX_DMA_FIFO_SIZE);
if(length < 0)
{
LOG_E("Mic device read err\n");
}
res = check_filled_mem(0x55, (rt_uint8_t*)(&mic_buffer[0]), length);
if(res != RT_EOK)
{
LOG_E("The second memory check failed! Buffer dump\n");
for (rt_size_t i = 0; i < RX_DMA_FIFO_SIZE; i++)
{
rt_kprintf("%02X ",mic_buffer[i]);
if (i % 16 == 15) rt_kprintf("\n");
}
rt_kprintf("\n");
uassert_true(0);
goto __exit;
}
break;
}
rt_thread_mdelay(100);
}
__exit:
if (mic_buffer)
{
rt_free(mic_buffer);
}
if (dev_obj != RT_NULL)
{
audio_fsm_step = 4;
rt_device_close(dev_obj);
}
}
static void testcase(void)
{
UTEST_UNIT_RUN(player_test);
UTEST_UNIT_RUN(mic_test);
}
static rt_err_t utest_tc_init(void)
{
return RT_EOK;
}
static rt_err_t utest_tc_cleanup(void)
{
return RT_EOK;
}
UTEST_TC_EXPORT(testcase, "components.drivers.audio.tc_audio_main", utest_tc_init, utest_tc_cleanup, 10);
|