first commit for chrg

This commit is contained in:
wmano
2025-08-16 22:58:22 +08:00
commit 52a3ed5862
2306 changed files with 1021208 additions and 0 deletions
@@ -0,0 +1,21 @@
config RT_USING_AUDIO
bool "Using Audio device drivers"
default n
if RT_USING_AUDIO
config RT_AUDIO_REPLAY_MP_BLOCK_SIZE
int "Replay memory pool block size"
default 4096
config RT_AUDIO_REPLAY_MP_BLOCK_COUNT
int "Replay memory pool block count"
default 2
config RT_AUDIO_RECORD_PIPE_SIZE
int "Record pipe size"
default 2048
config RT_UTEST_USING_AUDIO_DRIVER
bool "Enable rt_audio_api testcase"
default n
endif
@@ -0,0 +1,14 @@
from building import *
cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = [cwd]
group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_AUDIO'], CPPPATH = CPPPATH)
list = os.listdir(cwd)
for item in list:
if os.path.isfile(os.path.join(cwd, item, 'SConscript')):
group = group + SConscript(os.path.join(item, 'SConscript'))
Return('group')
@@ -0,0 +1,784 @@
/*
* Copyright (c) 2006-2025 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2017-05-09 Urey first version
* 2019-07-09 Zero-Free improve device ops interface and data flows
* 2025-03-04 wumingzi add doxygen comments.
*/
#include <stdio.h>
#include <string.h>
#include <rthw.h>
#include <rtdevice.h>
#define DBG_TAG "audio"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
/**
* @addtogroup group_drivers_audio
*/
/** @{ */
enum
{
REPLAY_EVT_NONE = 0x00,
REPLAY_EVT_START = 0x01,
REPLAY_EVT_STOP = 0x02,
};
/**
* @brief Send a replay frame to the audio hardware device
*
* This function handles sending audio data from the memory queue to the hardware buffer for playback.
* If there is no data available in the queue, it sends zero frames. Otherwise, it copies data from the memory pool
* to the hardware device FIFO and manages the read index and position accordingly.
*
* @param[in] audio pointer to the audio device structure
*
* @return error code, RT_EOK is successful otherwise means failure
*
* @note This function may temporarily disable interrupts or perform time-consuming operations like memcpy,
* which could affect system responsiveness
*/
static rt_err_t _audio_send_replay_frame(struct rt_audio_device *audio)
{
rt_err_t result = RT_EOK;
rt_uint8_t *data;
rt_size_t dst_size, src_size;
rt_uint16_t position, remain_bytes = 0, index = 0;
struct rt_audio_buf_info *buf_info;
RT_ASSERT(audio != RT_NULL);
buf_info = &audio->replay->buf_info;
/* save current pos */
position = audio->replay->pos;
dst_size = buf_info->block_size;
/* check replay queue is empty */
if (rt_data_queue_peek(&audio->replay->queue, (const void **)&data, &src_size) != RT_EOK)
{
/* ack stop event */
if (audio->replay->event & REPLAY_EVT_STOP)
rt_completion_done(&audio->replay->cmp);
/* send zero frames */
rt_memset(&buf_info->buffer[audio->replay->pos], 0, dst_size);
audio->replay->pos += dst_size;
audio->replay->pos %= buf_info->total_size;
}
else
{
rt_memset(&buf_info->buffer[audio->replay->pos], 0, dst_size);
/* copy data from memory pool to hardware device fifo */
while (index < dst_size)
{
result = rt_data_queue_peek(&audio->replay->queue, (const void **)&data, &src_size);
if (result != RT_EOK)
{
LOG_D("under run %d, remain %d", audio->replay->pos, remain_bytes);
audio->replay->pos -= remain_bytes;
audio->replay->pos += dst_size;
audio->replay->pos %= buf_info->total_size;
audio->replay->read_index = 0;
result = -RT_EEMPTY;
break;
}
remain_bytes = MIN((dst_size - index), (src_size - audio->replay->read_index));
rt_memcpy(&buf_info->buffer[audio->replay->pos],
&data[audio->replay->read_index], remain_bytes);
index += remain_bytes;
audio->replay->read_index += remain_bytes;
audio->replay->pos += remain_bytes;
audio->replay->pos %= buf_info->total_size;
if (audio->replay->read_index == src_size)
{
/* free memory */
audio->replay->read_index = 0;
rt_data_queue_pop(&audio->replay->queue, (const void **)&data, &src_size, RT_WAITING_NO);
rt_mp_free(data);
/* notify transmitted complete. */
if (audio->parent.tx_complete != RT_NULL)
audio->parent.tx_complete(&audio->parent, (void *)data);
}
}
}
if (audio->ops->transmit != RT_NULL)
{
if (audio->ops->transmit(audio, &buf_info->buffer[position], RT_NULL, dst_size) != dst_size)
result = -RT_ERROR;
}
return result;
}
/**
* @brief Write replay frame into audio device replay queue
*
* @param[in] audio pointer to audio device
*
* @return error code, RT_EOK is successful otherwise means failure
*/
static rt_err_t _audio_flush_replay_frame(struct rt_audio_device *audio)
{
rt_err_t result = RT_EOK;
if (audio->replay->write_index)
{
result = rt_data_queue_push(&audio->replay->queue,
(const void **)audio->replay->write_data,
audio->replay->write_index,
RT_WAITING_FOREVER);
audio->replay->write_index = 0;
}
return result;
}
/**
* @brief Replay audio
*
* @param[in] audio pointer to audio device
*
* @return error code, RT_EOK is successful otherwise means failure
*/
static rt_err_t _aduio_replay_start(struct rt_audio_device *audio)
{
rt_err_t result = RT_EOK;
if (audio->replay->activated != RT_TRUE)
{
/* start playback hardware device */
if (audio->ops->start)
result = audio->ops->start(audio, AUDIO_STREAM_REPLAY);
audio->replay->activated = RT_TRUE;
LOG_D("start audio replay device");
}
return result;
}
/**
* @brief Stop replaying audio
*
* When audio->replay->queue is empty and the audio->replay->event was set REPLAY_EVT_STOP,
* _audio_send_replay_frame will send completion to stop replaying audio.
*
* @param[in] audio pointer to audio device
*
* @return error code, RT_EOK is successful otherwise means failure
*/
static rt_err_t _aduio_replay_stop(struct rt_audio_device *audio)
{
rt_err_t result = RT_EOK;
if (audio->replay->activated == RT_TRUE)
{
/* flush replay remian frames */
_audio_flush_replay_frame(audio);
/* notify irq(or thread) to stop the data transmission */
audio->replay->event |= REPLAY_EVT_STOP;
/* waiting for the remaining data transfer to complete */
rt_completion_init(&audio->replay->cmp);
rt_completion_wait(&audio->replay->cmp, RT_WAITING_FOREVER);
audio->replay->event &= ~REPLAY_EVT_STOP;
/* stop playback hardware device */
if (audio->ops->stop)
result = audio->ops->stop(audio, AUDIO_STREAM_REPLAY);
audio->replay->activated = RT_FALSE;
LOG_D("stop audio replay device");
}
return result;
}
/**
* @brief Open audio pipe and start to record audio
*
* @param[in] audio pointer to audio device
*
* @return error code, RT_EOK is successful otherwise means failure
*/
static rt_err_t _audio_record_start(struct rt_audio_device *audio)
{
rt_err_t result = RT_EOK;
if (audio->record->activated != RT_TRUE)
{
/* open audio record pipe */
rt_device_open(RT_DEVICE(&audio->record->pipe), RT_DEVICE_OFLAG_RDONLY);
/* start record hardware device */
if (audio->ops->start)
result = audio->ops->start(audio, AUDIO_STREAM_RECORD);
audio->record->activated = RT_TRUE;
LOG_D("start audio record device");
}
return result;
}
/**
* @brief stop recording audio and closeaudio pipe
*
* @param[in] audio pointer to audio device
*
* @return error code, RT_EOK is successful otherwise means failure
*/
static rt_err_t _audio_record_stop(struct rt_audio_device *audio)
{
rt_err_t result = RT_EOK;
if (audio->record->activated == RT_TRUE)
{
/* stop record hardware device */
if (audio->ops->stop)
result = audio->ops->stop(audio, AUDIO_STREAM_RECORD);
/* close audio record pipe */
rt_device_close(RT_DEVICE(&audio->record->pipe));
audio->record->activated = RT_FALSE;
LOG_D("stop audio record device");
}
return result;
}
/**
* @brief Init audio pipe
*
* In kernel, this function will set replay or record function depending on device
* flag. For replaying, it will malloc for managing audio replay struct meanwhile
* creating mempool and dataqueue.For recording, it will creat audio pipe and
* it's ringbuffer.
* In driver, this function will only execute hardware driver initialization code
* and get hardware buffer infomation.
*
* @param[in] dev pointer to audio device
*
* @return error code, RT_EOK is successful otherwise means failure
*/
static rt_err_t _audio_dev_init(struct rt_device *dev)
{
rt_err_t result = RT_EOK;
struct rt_audio_device *audio;
RT_ASSERT(dev != RT_NULL);
audio = (struct rt_audio_device *) dev;
/* initialize replay & record */
audio->replay = RT_NULL;
audio->record = RT_NULL;
/* initialize replay */
if (dev->flag & RT_DEVICE_FLAG_WRONLY)
{
struct rt_audio_replay *replay = (struct rt_audio_replay *) rt_malloc(sizeof(struct rt_audio_replay));
if (replay == RT_NULL)
return -RT_ENOMEM;
rt_memset(replay, 0, sizeof(struct rt_audio_replay));
/* init memory pool for replay */
replay->mp = rt_mp_create("adu_mp", RT_AUDIO_REPLAY_MP_BLOCK_COUNT, RT_AUDIO_REPLAY_MP_BLOCK_SIZE);
if (replay->mp == RT_NULL)
{
rt_free(replay);
LOG_E("create memory pool for replay failed");
return -RT_ENOMEM;
}
/* init queue for audio replay */
rt_data_queue_init(&replay->queue, CFG_AUDIO_REPLAY_QUEUE_COUNT, 0, RT_NULL);
/* init mutex lock for audio replay */
rt_mutex_init(&replay->lock, "replay", RT_IPC_FLAG_PRIO);
replay->activated = RT_FALSE;
audio->replay = replay;
}
/* initialize record */
if (dev->flag & RT_DEVICE_FLAG_RDONLY)
{
struct rt_audio_record *record = (struct rt_audio_record *) rt_malloc(sizeof(struct rt_audio_record));
rt_uint8_t *buffer;
if (record == RT_NULL)
return -RT_ENOMEM;
rt_memset(record, 0, sizeof(struct rt_audio_record));
/* init pipe for record*/
buffer = rt_malloc(RT_AUDIO_RECORD_PIPE_SIZE);
if (buffer == RT_NULL)
{
rt_free(record);
LOG_E("malloc memory for for record pipe failed");
return -RT_ENOMEM;
}
rt_audio_pipe_init(&record->pipe, "record",
(rt_int32_t)(RT_PIPE_FLAG_FORCE_WR | RT_PIPE_FLAG_BLOCK_RD),
buffer,
RT_AUDIO_RECORD_PIPE_SIZE);
record->activated = RT_FALSE;
audio->record = record;
}
/* initialize hardware configuration */
if (audio->ops->init)
audio->ops->init(audio);
/* get replay buffer information */
if (audio->ops->buffer_info)
audio->ops->buffer_info(audio, &audio->replay->buf_info);
return result;
}
/**
* @brief Start record audio
*
* @param[in] dev pointer to audio device
*
* @param[in] oflag device flag
*
* @return error code, RT_EOK is successful otherwise means failure
*/
static rt_err_t _audio_dev_open(struct rt_device *dev, rt_uint16_t oflag)
{
struct rt_audio_device *audio;
RT_ASSERT(dev != RT_NULL);
audio = (struct rt_audio_device *) dev;
/* check device flag with the open flag */
if ((oflag & RT_DEVICE_OFLAG_RDONLY) && !(dev->flag & RT_DEVICE_FLAG_RDONLY))
return -RT_EIO;
if ((oflag & RT_DEVICE_OFLAG_WRONLY) && !(dev->flag & RT_DEVICE_FLAG_WRONLY))
return -RT_EIO;
/* get open flags */
dev->open_flag = oflag & 0xff;
/* initialize the Rx/Tx structure according to open flag */
if (oflag & RT_DEVICE_OFLAG_WRONLY)
{
if (audio->replay->activated != RT_TRUE)
{
LOG_D("open audio replay device, oflag = %x\n", oflag);
audio->replay->write_index = 0;
audio->replay->read_index = 0;
audio->replay->pos = 0;
audio->replay->event = REPLAY_EVT_NONE;
}
dev->open_flag |= RT_DEVICE_OFLAG_WRONLY;
}
if (oflag & RT_DEVICE_OFLAG_RDONLY)
{
/* open record pipe */
if (audio->record->activated != RT_TRUE)
{
LOG_D("open audio record device ,oflag = %x\n", oflag);
_audio_record_start(audio);
audio->record->activated = RT_TRUE;
}
dev->open_flag |= RT_DEVICE_OFLAG_RDONLY;
}
return RT_EOK;
}
/**
* @brief Stop record, replay or both.
*
* @param[in] dev pointer to audio device
*
* @return useless param
*/
static rt_err_t _audio_dev_close(struct rt_device *dev)
{
struct rt_audio_device *audio;
RT_ASSERT(dev != RT_NULL);
audio = (struct rt_audio_device *) dev;
if (dev->open_flag & RT_DEVICE_OFLAG_WRONLY)
{
/* stop replay stream */
_aduio_replay_stop(audio);
dev->open_flag &= ~RT_DEVICE_OFLAG_WRONLY;
}
if (dev->open_flag & RT_DEVICE_OFLAG_RDONLY)
{
/* stop record stream */
_audio_record_stop(audio);
dev->open_flag &= ~RT_DEVICE_OFLAG_RDONLY;
}
return RT_EOK;
}
/**
* @brief Read audio device
*
* @param[in] dev pointer to device
*
* @param[in] pos position when reading
*
* @param[out] buffer a data buffer to save the read data
*
* @param[in] size buffer size
*
* @return the actually read size on successfully, otherwise 0 will be returned.
*
* @note
*/
static rt_ssize_t _audio_dev_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size)
{
struct rt_audio_device *audio;
RT_ASSERT(dev != RT_NULL);
audio = (struct rt_audio_device *) dev;
if (!(dev->open_flag & RT_DEVICE_OFLAG_RDONLY) || (audio->record == RT_NULL))
return 0;
return rt_device_read(RT_DEVICE(&audio->record->pipe), pos, buffer, size);
}
/**
* @brief Write data into replay data queue and replay it
*
* @param[in] dev pointer to device
*
* @param[in] pos useless param
*
* @param[in] buffer a data buffer to be written into data queue
*
* @param[in] size buffer size
*
* @return the actually read size on successfully, otherwise 0 will be returned.
*
* @note This function will take mutex.
*/
static rt_ssize_t _audio_dev_write(struct rt_device *dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
struct rt_audio_device *audio;
rt_uint8_t *ptr;
rt_uint16_t block_size, remain_bytes, index = 0;
RT_ASSERT(dev != RT_NULL);
audio = (struct rt_audio_device *) dev;
if (!(dev->open_flag & RT_DEVICE_OFLAG_WRONLY) || (audio->replay == RT_NULL))
return 0;
/* push a new frame to replay data queue */
ptr = (rt_uint8_t *)buffer;
block_size = RT_AUDIO_REPLAY_MP_BLOCK_SIZE;
rt_mutex_take(&audio->replay->lock, RT_WAITING_FOREVER);
while (index < size)
{
/* request buffer from replay memory pool */
if (audio->replay->write_index % block_size == 0)
{
audio->replay->write_data = rt_mp_alloc(audio->replay->mp, RT_WAITING_FOREVER);
rt_memset(audio->replay->write_data, 0, block_size);
}
/* copy data to replay memory pool */
remain_bytes = MIN((block_size - audio->replay->write_index), (size - index));
rt_memcpy(&audio->replay->write_data[audio->replay->write_index], &ptr[index], remain_bytes);
index += remain_bytes;
audio->replay->write_index += remain_bytes;
audio->replay->write_index %= block_size;
if (audio->replay->write_index == 0)
{
rt_data_queue_push(&audio->replay->queue,
audio->replay->write_data,
block_size,
RT_WAITING_FOREVER);
}
}
rt_mutex_release(&audio->replay->lock);
/* check replay state */
if (audio->replay->activated != RT_TRUE)
{
_aduio_replay_start(audio);
audio->replay->activated = RT_TRUE;
}
return index;
}
/**
* @brief Control audio device
*
* @param[in] dev pointer to device
*
* @param[in] cmd audio cmd, it can be one of value in @ref group_audio_control
*
* @param[in] args command argument
*
* @return error code, RT_EOK is successful otherwise means failure
*/
static rt_err_t _audio_dev_control(struct rt_device *dev, int cmd, void *args)
{
rt_err_t result = RT_EOK;
struct rt_audio_device *audio;
RT_ASSERT(dev != RT_NULL);
audio = (struct rt_audio_device *) dev;
/* dev stat...*/
switch (cmd)
{
case AUDIO_CTL_GETCAPS:
{
struct rt_audio_caps *caps = (struct rt_audio_caps *) args;
LOG_D("AUDIO_CTL_GETCAPS: main_type = %d,sub_type = %d", caps->main_type, caps->sub_type);
if (audio->ops->getcaps != RT_NULL)
{
result = audio->ops->getcaps(audio, caps);
}
break;
}
case AUDIO_CTL_CONFIGURE:
{
struct rt_audio_caps *caps = (struct rt_audio_caps *) args;
LOG_D("AUDIO_CTL_CONFIGURE: main_type = %d,sub_type = %d", caps->main_type, caps->sub_type);
if (audio->ops->configure != RT_NULL)
{
result = audio->ops->configure(audio, caps);
}
break;
}
case AUDIO_CTL_START:
{
int stream = *(int *) args;
LOG_D("AUDIO_CTL_START: stream = %d", stream);
if (stream == AUDIO_STREAM_REPLAY)
{
result = _aduio_replay_start(audio);
}
else
{
result = _audio_record_start(audio);
}
break;
}
case AUDIO_CTL_STOP:
{
int stream = *(int *) args;
LOG_D("AUDIO_CTL_STOP: stream = %d", stream);
if (stream == AUDIO_STREAM_REPLAY)
{
result = _aduio_replay_stop(audio);
}
else
{
result = _audio_record_stop(audio);
}
break;
}
default:
break;
}
return result;
}
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops audio_ops =
{
_audio_dev_init,
_audio_dev_open,
_audio_dev_close,
_audio_dev_read,
_audio_dev_write,
_audio_dev_control
};
#endif
/**
* @brief Register and initialize audio device
*
* @param[in] audio pointer to audio deive
*
* @param[in] name device name
*
* @param[in] flag device flags
*
* @param[in] data user data
*
* @return error code, RT_EOK is successful otherwise means failure
*/
rt_err_t rt_audio_register(struct rt_audio_device *audio, const char *name, rt_uint32_t flag, void *data)
{
rt_err_t result = RT_EOK;
struct rt_device *device;
RT_ASSERT(audio != RT_NULL);
device = &(audio->parent);
device->type = RT_Device_Class_Sound;
device->rx_indicate = RT_NULL;
device->tx_complete = RT_NULL;
#ifdef RT_USING_DEVICE_OPS
device->ops = &audio_ops;
#else
device->init = _audio_dev_init;
device->open = _audio_dev_open;
device->close = _audio_dev_close;
device->read = _audio_dev_read;
device->write = _audio_dev_write;
device->control = _audio_dev_control;
#endif
device->user_data = data;
/* register a character device */
result = rt_device_register(device, name, flag | RT_DEVICE_FLAG_REMOVABLE);
/* initialize audio device */
if (result == RT_EOK)
result = rt_device_init(device);
return result;
}
/**
* @brief Set audio sample rate
*
* @param[in] bitValue audio sample rate, it can be one of value in @ref group_audio_samp_rates
*
* @return speed has been set
*/
int rt_audio_samplerate_to_speed(rt_uint32_t bitValue)
{
int speed = 0;
switch (bitValue)
{
case AUDIO_SAMP_RATE_8K:
speed = 8000;
break;
case AUDIO_SAMP_RATE_11K:
speed = 11052;
break;
case AUDIO_SAMP_RATE_16K:
speed = 16000;
break;
case AUDIO_SAMP_RATE_22K:
speed = 22050;
break;
case AUDIO_SAMP_RATE_32K:
speed = 32000;
break;
case AUDIO_SAMP_RATE_44K:
speed = 44100;
break;
case AUDIO_SAMP_RATE_48K:
speed = 48000;
break;
case AUDIO_SAMP_RATE_96K:
speed = 96000;
break;
case AUDIO_SAMP_RATE_128K:
speed = 128000;
break;
case AUDIO_SAMP_RATE_160K:
speed = 160000;
break;
case AUDIO_SAMP_RATE_172K:
speed = 176400;
break;
case AUDIO_SAMP_RATE_192K:
speed = 192000;
break;
default:
break;
}
return speed;
}
/**
* @brief Send a replay frame to the audio hardware device
*
* See _audio_send_replay_frame for details
*
* @param[in] audio pointer to audio device
*
* @return void
*/
void rt_audio_tx_complete(struct rt_audio_device *audio)
{
/* try to send next frame */
_audio_send_replay_frame(audio);
}
/**
* @brief Receive recording from audio device
*
* @param[in] audio pointer to audio device
*
* @param[in] pbuf pointer ro data to be received
*
* @param[in] len buffer size
*
* @return void
*/
void rt_audio_rx_done(struct rt_audio_device *audio, rt_uint8_t *pbuf, rt_size_t len)
{
/* save data to record pipe */
rt_device_write(RT_DEVICE(&audio->record->pipe), 0, pbuf, len);
/* invoke callback */
if (audio->parent.rx_indicate != RT_NULL)
audio->parent.rx_indicate(&audio->parent, len);
}
/** @} group_drivers_audio */
@@ -0,0 +1,370 @@
/*
* Copyright (c) 2006-2025 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2012-09-30 Bernard first version.
* 2025-03-04 wumingzi add doxygen comments.
*/
#include <rthw.h>
#include <rtdevice.h>
#include "dev_audio_pipe.h"
static void _rt_audio_pipe_resume_writer(struct rt_audio_pipe *pipe)
{
if (!rt_list_isempty(&pipe->suspended_write_list))
{
rt_thread_t thread;
RT_ASSERT(pipe->flag & RT_PIPE_FLAG_BLOCK_WR);
/* get suspended thread */
thread = RT_THREAD_LIST_NODE_ENTRY(pipe->suspended_write_list.next);
/* resume the write thread */
rt_thread_resume(thread);
rt_schedule();
}
}
/**
* @brief Read audio pipe
*
* @param[in] dev pointer to audio device will be read
*
* @param[in] pos useless param
*
* @param[in] buffer pointer to ringbuffer of audio pipe to be read
*
* @param[in] size number of bytes will be read
*
* @return number of read bytes
*
* @note This function will execute time-consuming or affecting the
* system operations like memcpy and disable interrupt.
*/
static rt_ssize_t rt_audio_pipe_read(rt_device_t dev,
rt_off_t pos,
void *buffer,
rt_size_t size)
{
rt_base_t level;
rt_thread_t thread;
struct rt_audio_pipe *pipe;
rt_size_t read_nbytes;
pipe = (struct rt_audio_pipe *)dev;
RT_ASSERT(pipe != RT_NULL);
if (!(pipe->flag & RT_PIPE_FLAG_BLOCK_RD))
{
level = rt_hw_interrupt_disable();
read_nbytes = rt_ringbuffer_get(&(pipe->ringbuffer), (rt_uint8_t *)buffer, size);
/* if the ringbuffer is empty, there won't be any writer waiting */
if (read_nbytes)
_rt_audio_pipe_resume_writer(pipe);
rt_hw_interrupt_enable(level);
return read_nbytes;
}
thread = rt_thread_self();
/* current context checking */
RT_DEBUG_NOT_IN_INTERRUPT;
do
{
level = rt_hw_interrupt_disable();
read_nbytes = rt_ringbuffer_get(&(pipe->ringbuffer), (rt_uint8_t *)buffer, size);
if (read_nbytes == 0)
{
rt_thread_suspend(thread);
/* waiting on suspended read list */
rt_list_insert_before(&(pipe->suspended_read_list),
&RT_THREAD_LIST_NODE(thread));
rt_hw_interrupt_enable(level);
rt_schedule();
}
else
{
_rt_audio_pipe_resume_writer(pipe);
rt_hw_interrupt_enable(level);
break;
}
}
while (read_nbytes == 0);
return read_nbytes;
}
/**
* @brief Resume audio pipe reader thread
*
* @param[in] pipe pointer to suspended audio pipe thread
*/
static void _rt_audio_pipe_resume_reader(struct rt_audio_pipe *pipe)
{
if (pipe->parent.rx_indicate)
pipe->parent.rx_indicate(&pipe->parent,
rt_ringbuffer_data_len(&pipe->ringbuffer));
if (!rt_list_isempty(&pipe->suspended_read_list))
{
rt_thread_t thread;
RT_ASSERT(pipe->flag & RT_PIPE_FLAG_BLOCK_RD);
/* get suspended thread */
thread = RT_THREAD_LIST_NODE_ENTRY(pipe->suspended_read_list.next);
/* resume the read thread */
rt_thread_resume(thread);
rt_schedule();
}
}
/**
* @brief Write data into audio pipe
*
* @param[in] dev pointer to audio pipe that has been configured
*
* @param[in] pos useless param
*
* @param[in] buffer pointer to buffer of ringbuffer
*
* @param[in] size size of data will be written
*
* @return number of written bytes
*
* @note The function will disable interrupt and may suspend current thread
*/
static rt_ssize_t rt_audio_pipe_write(rt_device_t dev,
rt_off_t pos,
const void *buffer,
rt_size_t size)
{
rt_base_t level;
rt_thread_t thread;
struct rt_audio_pipe *pipe;
rt_size_t write_nbytes;
pipe = (struct rt_audio_pipe *)dev;
RT_ASSERT(pipe != RT_NULL);
if ((pipe->flag & RT_PIPE_FLAG_FORCE_WR) ||
!(pipe->flag & RT_PIPE_FLAG_BLOCK_WR))
{
level = rt_hw_interrupt_disable();
if (pipe->flag & RT_PIPE_FLAG_FORCE_WR)
write_nbytes = rt_ringbuffer_put_force(&(pipe->ringbuffer),
(const rt_uint8_t *)buffer, size);
else
write_nbytes = rt_ringbuffer_put(&(pipe->ringbuffer),
(const rt_uint8_t *)buffer, size);
_rt_audio_pipe_resume_reader(pipe);
rt_hw_interrupt_enable(level);
return write_nbytes;
}
thread = rt_thread_self();
/* current context checking */
RT_DEBUG_NOT_IN_INTERRUPT;
do
{
level = rt_hw_interrupt_disable();
write_nbytes = rt_ringbuffer_put(&(pipe->ringbuffer), (const rt_uint8_t *)buffer, size);
if (write_nbytes == 0)
{
/* pipe full, waiting on suspended write list */
rt_thread_suspend(thread);
/* waiting on suspended read list */
rt_list_insert_before(&(pipe->suspended_write_list),
&RT_THREAD_LIST_NODE(thread));
rt_hw_interrupt_enable(level);
rt_schedule();
}
else
{
_rt_audio_pipe_resume_reader(pipe);
rt_hw_interrupt_enable(level);
break;
}
}
while (write_nbytes == 0);
return write_nbytes;
}
/**
* @brief Control audio pipe
*
* @param[in] dev pointer to pipe
*
* @param[in] cmd control command
*
* @param[in] args control argument
*
* @return error code, RT_EOK is successful otherwise means failure
*/
static rt_err_t rt_audio_pipe_control(rt_device_t dev, int cmd, void *args)
{
struct rt_audio_pipe *pipe;
pipe = (struct rt_audio_pipe *)dev;
if (cmd == PIPE_CTRL_GET_SPACE && args)
*(rt_size_t *)args = rt_ringbuffer_space_len(&pipe->ringbuffer);
return RT_EOK;
}
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops audio_pipe_ops =
{
RT_NULL,
RT_NULL,
RT_NULL,
rt_audio_pipe_read,
rt_audio_pipe_write,
rt_audio_pipe_control
};
#endif
/**
* @brief Init audio pipe
*
* This function will initialize a pipe device and put it under control of
* resource management.
*
* @param pipe the pipe device
*
* @param name the name of pipe device
*
* @param flag the attribute of the pipe device
*
* @param buf the buffer of pipe device
*
* @param size the size of pipe device buffer
*
* @return the operation status, RT_EOK on successful
*/
rt_err_t rt_audio_pipe_init(struct rt_audio_pipe *pipe,
const char *name,
rt_int32_t flag,
rt_uint8_t *buf,
rt_size_t size)
{
RT_ASSERT(pipe);
RT_ASSERT(buf);
/* initialize suspended list */
rt_list_init(&pipe->suspended_read_list);
rt_list_init(&pipe->suspended_write_list);
/* initialize ring buffer */
rt_ringbuffer_init(&pipe->ringbuffer, buf, size);
pipe->flag = flag;
/* create pipe */
pipe->parent.type = RT_Device_Class_Pipe;
#ifdef RT_USING_DEVICE_OPS
pipe->parent.ops = &audio_pipe_ops;
#else
pipe->parent.init = RT_NULL;
pipe->parent.open = RT_NULL;
pipe->parent.close = RT_NULL;
pipe->parent.read = rt_audio_pipe_read;
pipe->parent.write = rt_audio_pipe_write;
pipe->parent.control = rt_audio_pipe_control;
#endif
return rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR);
}
/**
* @brief This function will detach a pipe device from resource management
*
* @param pipe the pipe device
*
* @return the operation status, RT_EOK on successful
*/
rt_err_t rt_audio_pipe_detach(struct rt_audio_pipe *pipe)
{
return rt_device_unregister(&pipe->parent);
}
/**
* @brief Creat audio pipe
*
* @param[in] name pipe name
*
* @param[in] flag pipe flags, it can be one of enum rt_audio_pipe_flag items
*
* @param[in] size ringbuffer size
*
* @return error code, RT_EOK on initialization successfully
*
* @note depend on RT_USING_HEAP
*/
#ifdef RT_USING_HEAP
rt_err_t rt_audio_pipe_create(const char *name, rt_int32_t flag, rt_size_t size)
{
rt_uint8_t *rb_memptr = RT_NULL;
struct rt_audio_pipe *pipe = RT_NULL;
/* get aligned size */
size = RT_ALIGN(size, RT_ALIGN_SIZE);
pipe = (struct rt_audio_pipe *)rt_calloc(1, sizeof(struct rt_audio_pipe));
if (pipe == RT_NULL)
return -RT_ENOMEM;
/* create ring buffer of pipe */
rb_memptr = (rt_uint8_t *)rt_malloc(size);
if (rb_memptr == RT_NULL)
{
rt_free(pipe);
return -RT_ENOMEM;
}
return rt_audio_pipe_init(pipe, name, flag, rb_memptr, size);
}
/**
* @brief Detachaudio pipe and free its ringbuffer
*
* @param[in] pipe pointer to the pipe will be destory
*
* @note depend on RT_USING_HEAP
*/
void rt_audio_pipe_destroy(struct rt_audio_pipe *pipe)
{
if (pipe == RT_NULL)
return;
/* un-register pipe device */
rt_audio_pipe_detach(pipe);
/* release memory */
rt_free(pipe->ringbuffer.buffer_ptr);
rt_free(pipe);
return;
}
#endif /* RT_USING_HEAP */
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2006-2025 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-03-04 wumingzi add doxygen comments.
*/
#ifndef __DEV_AUDIO_PIPE_H__
#define __DEV_AUDIO_PIPE_H__
/**
* Pipe Device
*/
#include <rtdevice.h>
#ifndef RT_PIPE_BUFSZ
#define PIPE_BUFSZ 512
#else
#define PIPE_BUFSZ RT_PIPE_BUFSZ
#endif
/**
* @brief Portal device
*/
struct rt_audio_portal_device
{
struct rt_device parent;
struct rt_device *write_dev;
struct rt_device *read_dev;
};
/**
* @brief Aduio pipe flags
*/
enum rt_audio_pipe_flag
{
RT_PIPE_FLAG_NONBLOCK_RDWR = 0x00, /**< both read and write won't block */
RT_PIPE_FLAG_BLOCK_RD = 0x01, /**< read would block */
RT_PIPE_FLAG_BLOCK_WR = 0x02, /**< write would block */
RT_PIPE_FLAG_FORCE_WR = 0x04, /**< write to this pipe will discard some data when the pipe is full.
* When this flag is set, RT_PIPE_FLAG_BLOCK_WR will be ignored since write
* operation will always be success. */
};
/**
* @brief Audio buffer info
*
* The preferred number and size of audio pipeline buffer for the audio device, it
* will be used in rt_audio_replay struct.
*
*/
struct rt_audio_pipe
{
struct rt_device parent;
struct rt_ringbuffer ringbuffer; /**< ring buffer in pipe device */
rt_int32_t flag;
rt_list_t suspended_read_list; /**< suspended thread list for reading */
rt_list_t suspended_write_list; /**< suspended thread list for writing */
struct rt_audio_portal_device *write_portal;
struct rt_audio_portal_device *read_portal;
};
#define PIPE_CTRL_GET_SPACE 0x14 /**< get the remaining size of a pipe device */
rt_err_t rt_audio_pipe_init(struct rt_audio_pipe *pipe,
const char *name,
rt_int32_t flag,
rt_uint8_t *buf,
rt_size_t size);
rt_err_t rt_audio_pipe_detach(struct rt_audio_pipe *pipe);
#ifdef RT_USING_HEAP
rt_err_t rt_audio_pipe_create(const char *name, rt_int32_t flag, rt_size_t size);
void rt_audio_pipe_destroy(struct rt_audio_pipe *pipe);
#endif /* RT_USING_HEAP */
#endif /* __DEV_AUDIO_PIPE_H__ */
@@ -0,0 +1,13 @@
Import('rtconfig')
from building import *
cwd = GetCurrentDir()
src = []
CPPPATH = [cwd]
if GetDepend('RT_UTEST_USING_ALL_CASES') or GetDepend('RT_UTEST_USING_AUDIO_DRIVER'):
src += Glob('tc_*.c')
group = DefineGroup('utestcases', src, depend = ['RT_USING_UTESTCASES', 'RT_USING_AUDIO'], CPPPATH = CPPPATH)
Return('group')
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2006-2025 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-05-02 wumingzi first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <rttypes.h>
#include "utest.h"
/* DMA buffer of audio player device refresh is triggered only when the amount of transmitted data is
* greater than the size of a single block in the data queue */
#define TX_DMA_BLOCK_SIZE RT_AUDIO_REPLAY_MP_BLOCK_SIZE
#define TX_DMA_FIFO_SIZE (RT_AUDIO_REPLAY_MP_BLOCK_SIZE * 2)
#define RX_DMA_BLOCK_SIZE RT_AUDIO_RECORD_PIPE_SIZE
#define RX_DMA_FIFO_SIZE (RT_AUDIO_RECORD_PIPE_SIZE * 2)
#define SOUND_PLAYER_DEVICE_NAME "sound0"
#define SOUND_MIC_DEVICE_NAME "mic0"
#define PLAYER_SAMPLEBITS 16
#define PLAYER_SAMPLERATE 16000
#define PLAYER_CHANNEL 2
#define PLAYER_VOLUME 30
#define MIC_SAMPLEBITS 16
#define MIC_SAMPLERATE 16000
#define MIC_CHANNEL 2
#define MIC_TIME_MS 5000
extern rt_uint8_t audio_fsm_step ;
struct mic_device
{
struct rt_audio_device audio;
struct rt_audio_configure config;
rt_uint8_t *rx_fifo;
};
struct sound_device
{
struct rt_audio_device audio;
struct rt_audio_configure config;
rt_uint8_t volume;
rt_uint8_t *tx_fifo;
};
@@ -0,0 +1,136 @@
/*
* Copyright (c) 2006-2025 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-05-02 wumingzi First version
*/
#include "tc_audio_common.h"
#define THREAD_PRIORITY 9
#define THREAD_TIMESLICE 5
#define thread_simulate_intr_create_stacksize 1024
static rt_thread_t thread_simulate_intr_handle;
static struct mic_device mic_dev;
/* Simulate callback function */
static void thread_simulate_intr(void *parameter)
{
/* Send the data(0xAA) from DMA buffer to kernel */
rt_memset((void*)&mic_dev.rx_fifo[0], 0xAA, RX_DMA_BLOCK_SIZE);
rt_audio_rx_done((struct rt_audio_device *)&(mic_dev.audio), mic_dev.rx_fifo, RX_DMA_BLOCK_SIZE);
audio_fsm_step = 1;
while (1)
{
if(audio_fsm_step == 2)
{
/* Send the the data(0x55) from DMA buffer to kernel */
rt_memset((void*)&mic_dev.rx_fifo[RX_DMA_BLOCK_SIZE], 0x55, RX_DMA_BLOCK_SIZE);
rt_audio_rx_done(&mic_dev.audio, &mic_dev.rx_fifo[RX_DMA_BLOCK_SIZE], RX_DMA_BLOCK_SIZE);
audio_fsm_step = 3;
break;
}
if(audio_fsm_step == 4)
{
rt_thread_mdelay(10);
rt_audio_rx_done(&mic_dev.audio, &mic_dev.rx_fifo[RX_DMA_BLOCK_SIZE], RX_DMA_BLOCK_SIZE);
break;
}
rt_thread_mdelay(10);
}
while(1)
{
rt_thread_mdelay(10);
}
}
static void thread_simulate_intr_create(void)
{
thread_simulate_intr_handle = rt_thread_create(
"thread_simulate_intr",
thread_simulate_intr,
RT_NULL,
thread_simulate_intr_create_stacksize,
THREAD_PRIORITY - 1, THREAD_TIMESLICE);
if (thread_simulate_intr_handle == RT_NULL)
{
rt_kprintf("Error: Failed to create thread!\n");
return;
}
if (rt_thread_startup(thread_simulate_intr_handle) != RT_EOK)
{
rt_kprintf("Error: Failed to start thread!\n");
thread_simulate_intr_handle = RT_NULL;
}
}
static rt_err_t mic_device_init(struct rt_audio_device *audio)
{
return RT_EOK;
}
/* Simulate DMA interrupt */
static rt_err_t mic_device_start(struct rt_audio_device *audio, int stream)
{
thread_simulate_intr_create();
return RT_EOK;
}
static rt_err_t mic_device_stop(struct rt_audio_device *audio, int stream)
{
if (thread_simulate_intr_handle != RT_NULL)
{
rt_thread_delete(thread_simulate_intr_handle);
thread_simulate_intr_handle = RT_NULL;
}
return RT_EOK;
}
static rt_err_t mic_device_getcaps(struct rt_audio_device *audio, struct rt_audio_caps *caps)
{
return RT_EOK;
}
static rt_err_t mic_device_configure(struct rt_audio_device *audio, struct rt_audio_caps *caps)
{
return RT_EOK;
}
static struct rt_audio_ops _mic_audio_ops =
{
.getcaps = mic_device_getcaps,
.configure = mic_device_configure,
.init = mic_device_init,
.start = mic_device_start,
.stop = mic_device_stop,
.transmit = RT_NULL,
.buffer_info = RT_NULL,
};
static int rt_hw_mic_init(void)
{
struct rt_audio_device *audio = &mic_dev.audio;
/* mic default */
mic_dev.rx_fifo = rt_malloc(RX_DMA_FIFO_SIZE);
if (mic_dev.rx_fifo == RT_NULL)
{
return -RT_ENOMEM;
}
mic_dev.config.channels = MIC_CHANNEL;
mic_dev.config.samplerate = MIC_SAMPLERATE;
mic_dev.config.samplebits = MIC_SAMPLEBITS;
/* register mic device */
audio->ops = &_mic_audio_ops;
rt_audio_register(audio, SOUND_MIC_DEVICE_NAME, RT_DEVICE_FLAG_RDONLY, (void *)&mic_dev);
return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_mic_init);
@@ -0,0 +1,150 @@
/*
* Copyright (c) 2006-2025 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-05-02 wumingzi First version
*/
#include "tc_audio_common.h"
#define THREAD_PRIORITY 9
#define THREAD_TIMESLICE 5
#define thread_simulate_intr_create_stacksize 1024
static rt_thread_t thread_simulate_intr_handle;
static struct sound_device snd_dev;
static void thread_simulate_intr(void *parameter)
{
rt_flag_t exec_once = 0;
while(1)
{
if(audio_fsm_step == 1 && exec_once == 0)
{
/* Move the data(0xAA) from kernel to DMA buffer */
rt_audio_tx_complete(&snd_dev.audio);
audio_fsm_step = 2;
exec_once = 1;
rt_thread_mdelay(10);
}
else if(audio_fsm_step == 2 && exec_once == 1)
{
/* Move the data(0x55) from kernel to DMA buffer */
rt_audio_tx_complete(&snd_dev.audio);
audio_fsm_step = 3;
rt_thread_mdelay(10);
}
else if(audio_fsm_step == 4)
{
/* rt_device_close will call rt_completion_wait(FOREVER), so we need delay to
* let system run the point */
rt_thread_mdelay(10);
rt_audio_tx_complete(&snd_dev.audio);
break;
}
rt_thread_mdelay(10);
}
while (1)
{
rt_thread_mdelay(10);
}
}
static void thread_simulate_intr_create(void)
{
thread_simulate_intr_handle = rt_thread_create(
"thread_simulate_intr",
thread_simulate_intr,
RT_NULL,
thread_simulate_intr_create_stacksize,
THREAD_PRIORITY - 1, THREAD_TIMESLICE);
rt_thread_startup(thread_simulate_intr_handle);
}
static rt_err_t player_device_init(struct rt_audio_device *audio)
{
return RT_EOK;
}
/* Simulate DMA interrupt */
static rt_err_t player_device_start(struct rt_audio_device *audio, int stream)
{
thread_simulate_intr_create();
return RT_EOK;
}
static rt_err_t player_device_stop(struct rt_audio_device *audio, int stream)
{
rt_thread_delete(thread_simulate_intr_handle);
return RT_EOK;
}
static rt_err_t player_device_getcaps(struct rt_audio_device *audio, struct rt_audio_caps *caps)
{
return RT_EOK;
}
static rt_err_t player_device_configure(struct rt_audio_device *audio, struct rt_audio_caps *caps)
{
return RT_EOK;
}
static rt_ssize_t player_device_transmit(struct rt_audio_device *audio, const void *writeBuf, void *readBuf, rt_size_t size)
{
return size;
}
static void player_device_buffer_info(struct rt_audio_device *audio, struct rt_audio_buf_info *info)
{
RT_ASSERT(audio != RT_NULL);
/**
* TX_FIFO
* +----------------+----------------+
* | block1 | block2 |
* +----------------+----------------+
* \ block_size /
*/
info->buffer = snd_dev.tx_fifo;
info->total_size = TX_DMA_FIFO_SIZE;
info->block_size = TX_DMA_BLOCK_SIZE;
info->block_count = RT_AUDIO_REPLAY_MP_BLOCK_COUNT;
}
static struct rt_audio_ops audio_ops =
{
.getcaps = player_device_getcaps,
.configure = player_device_configure,
.init = player_device_init,
.start = player_device_start,
.stop = player_device_stop,
.transmit = player_device_transmit,
.buffer_info = player_device_buffer_info,
};
static int rt_hw_sound_init(void)
{
rt_uint8_t *tx_fifo = RT_NULL;
tx_fifo = rt_malloc(TX_DMA_FIFO_SIZE);
if (tx_fifo == NULL)
{
return -RT_ENOMEM;
}
snd_dev.tx_fifo = tx_fifo;
/* Init default configuration */
{
snd_dev.config.samplerate = PLAYER_SAMPLERATE;
snd_dev.config.channels = PLAYER_CHANNEL;
snd_dev.config.samplebits = PLAYER_SAMPLEBITS;
snd_dev.volume = PLAYER_VOLUME;
}
snd_dev.audio.ops = &audio_ops;
rt_audio_register(&snd_dev.audio, SOUND_PLAYER_DEVICE_NAME, RT_DEVICE_FLAG_WRONLY, &snd_dev);
return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_sound_init);
@@ -0,0 +1,309 @@
/*
* 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
*/
/* The file can test the rt-thread audio driver framework including following api via memory
* simulation.
*
* rt_audio_register
* rt_audio_rx_done
* rt_audio_tx_complete
*
* When audio devices generate or receive new data, the corresponding buffer in device will
* receive date from kernel or surroundings. The same phenomenon will also happen at the
* application level. Thus we can fill memory to simulate the generation of data then track
* and check memory to ensure kernel processing audio data correctly. And this depends on
* implementations of audio drivers.
*
* Therefore, if the player_test testcase failed, it could mean rt_audio_register or
* rt_audio_tx_complete existing bugs. Similarly, if mic_test testcase failed, it could mean
* rt_audio_register or rt_audio_rx_done existing bugs.
*/
#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, "audio.tc_audio_main", utest_tc_init, utest_tc_cleanup, 10);