Merge pull request #803 from yygg/master

[DeviceDrivers] use data queue in audio device driver and clean up the code.
This commit is contained in:
Bernard Xiong 2017-08-16 10:59:02 +08:00 committed by GitHub
commit d6fa9f912d
2 changed files with 463 additions and 764 deletions

View File

@ -1,8 +1,25 @@
/* /*
* audio.c * File : audio.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
* *
* Created on: 2016Äê10ÔÂ19ÈÕ * This program is free software; you can redistribute it and/or modify
* Author: Urey * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017-05-09 Urey first version
*/ */
#include <stdio.h> #include <stdio.h>
@ -19,317 +36,6 @@
#define AUDIO_DBG(...) #define AUDIO_DBG(...)
#endif #endif
rt_err_t _audio_queue_init(struct rt_audio_queue *queue, rt_uint16_t size, rt_uint16_t lwm)
{
RT_ASSERT(queue != RT_NULL);
queue->count = 0;
queue->size = size;
queue->lwm = lwm;
queue->waiting_lwm = RT_FALSE;
queue->get_index = 0;
queue->put_index = 0;
rt_list_init(&(queue->suspended_push_list));
rt_list_init(&(queue->suspended_pop_list));
queue->queue = (struct rt_audio_frame *)rt_malloc(sizeof(struct rt_audio_frame) * size);
if (queue->queue == RT_NULL)
{
return -RT_ENOMEM;
}
return RT_EOK;
}
rt_err_t _audio_queue_push(struct rt_audio_queue *queue, struct rt_audio_frame *frame, rt_int32_t timeout)
{
rt_ubase_t level;
rt_thread_t thread;
rt_err_t result;
RT_ASSERT(queue != RT_NULL);
result = RT_EOK;
thread = rt_thread_self();
AUDIO_DBG("%s count = %d\n",__func__,queue->count);
level = rt_hw_interrupt_disable();
while(queue->count == queue->size)
{// audio queue is full
queue->waiting_lwm = RT_TRUE;
/* queue is full */
if (timeout == 0)
{
result = -RT_ETIMEOUT;
goto __exit;
}
/* current context checking */
RT_DEBUG_NOT_IN_INTERRUPT;
/* reset thread error number */
thread->error = RT_EOK;
/* suspend thread on the push list */
rt_thread_suspend(thread);
rt_list_insert_before(&(queue->suspended_push_list), &(thread->tlist));
/* start timer */
if (timeout > 0)
{
/* reset the timeout of thread timer and start it */
rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout);
rt_timer_start(&(thread->thread_timer));
}
/* enable interrupt */
rt_hw_interrupt_enable(level);
/* do schedule */
rt_schedule();
/* thread is waked up */
result = thread->error;
level = rt_hw_interrupt_disable();
if (result != RT_EOK) goto __exit;
}
queue->queue[queue->put_index].data_ptr = frame->data_ptr;
queue->queue[queue->put_index].data_size = frame->data_size;
queue->queue[queue->put_index].data_ofs = frame->data_ofs;
queue->put_index = (queue->put_index + 1) % queue->size;
queue->count ++;
if (!rt_list_isempty(&(queue->suspended_pop_list)))
{
/* there is at least one thread in suspended list */
/* get thread entry */
thread = rt_list_entry(queue->suspended_pop_list.next,
struct rt_thread,
tlist);
/* resume it */
rt_thread_resume(thread);
rt_hw_interrupt_enable(level);
/* perform a schedule */
rt_schedule();
return result;
}
__exit:
rt_hw_interrupt_enable(level);
return result;
}
rt_err_t _audio_queue_pop(struct rt_audio_queue *queue, struct rt_audio_frame *frame, rt_int32_t timeout)
{
rt_ubase_t level;
rt_thread_t thread;
rt_err_t result;
RT_ASSERT(queue != RT_NULL);
RT_ASSERT(frame != RT_NULL);
result = RT_EOK;
thread = rt_thread_self();
AUDIO_DBG("%s count = %d\n",__func__,queue->count);
level = rt_hw_interrupt_disable();
while (queue->count == 0)
{
/* queue is empty */
if (timeout == 0)
{
result = -RT_ETIMEOUT;
goto __exit;
}
/* current context checking */
RT_DEBUG_NOT_IN_INTERRUPT;
/* reset thread error number */
thread->error = RT_EOK;
/* suspend thread on the pop list */
rt_thread_suspend(thread);
rt_list_insert_before(&(queue->suspended_pop_list), &(thread->tlist));
/* start timer */
if (timeout > 0)
{
/* reset the timeout of thread timer and start it */
rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout);
rt_timer_start(&(thread->thread_timer));
}
/* enable interrupt */
rt_hw_interrupt_enable(level);
/* do schedule */
rt_schedule();
/* thread is waked up */
result = thread->error;
level = rt_hw_interrupt_disable();
if (result != RT_EOK)
goto __exit;
}
frame->data_ptr = queue->queue[queue->get_index].data_ptr;
frame->data_size = queue->queue[queue->get_index].data_size;
frame->data_ofs = queue->queue[queue->get_index].data_ofs;
queue->get_index = (queue->get_index + 1) % queue->size;
queue->count --;
if ((queue->waiting_lwm == RT_TRUE) &&
(queue->put_index - queue->get_index) <= queue->lwm)
{
queue->waiting_lwm = RT_FALSE;
/*
* there is at least one thread in suspended list
* and less than low water mark
*/
if (!rt_list_isempty(&(queue->suspended_push_list)))
{
/* get thread entry */
thread = rt_list_entry(queue->suspended_push_list.next,
struct rt_thread,
tlist);
/* resume it */
rt_thread_resume(thread);
rt_hw_interrupt_enable(level);
/* perform a schedule */
rt_schedule();
}
return result;
}
__exit:
rt_hw_interrupt_enable(level);
return result;
}
rt_err_t _audio_queue_peak(struct rt_audio_queue *queue, struct rt_audio_frame *frame)
{
rt_ubase_t level;
RT_ASSERT(queue != RT_NULL);
AUDIO_DBG("%s count = %d\n",__func__,queue->count);
level = rt_hw_interrupt_disable();
if (queue->count == 0)
{
rt_hw_interrupt_enable(level);
return -RT_EEMPTY;
}
frame->data_ptr = queue->queue[queue->get_index].data_ptr;
frame->data_size = queue->queue[queue->get_index].data_size;
frame->data_ofs = queue->queue[queue->get_index].data_ofs;
rt_hw_interrupt_enable(level);
return RT_EOK;
}
rt_err_t _audio_queue_unpeak(struct rt_audio_queue *queue, struct rt_audio_frame *frame)
{
rt_ubase_t level;
RT_ASSERT(queue != RT_NULL);
level = rt_hw_interrupt_disable();
if (queue->count == 0)
{
rt_hw_interrupt_enable(level);
return -RT_EEMPTY;
}
queue->queue[queue->get_index].data_ptr = frame->data_ptr;
queue->queue[queue->get_index].data_size = frame->data_size;
queue->queue[queue->get_index].data_ofs = frame->data_ofs;
rt_hw_interrupt_enable(level);
return RT_EOK;
}
rt_err_t _audio_queue_reset(struct rt_audio_queue *queue)
{
struct rt_thread *thread;
register rt_ubase_t temp;
rt_enter_critical();
/* wakeup all suspend threads */
/* resume on pop list */
while (!rt_list_isempty(&(queue->suspended_pop_list)))
{
/* disable interrupt */
temp = rt_hw_interrupt_disable();
/* get next suspend thread */
thread = rt_list_entry(queue->suspended_pop_list.next,
struct rt_thread,
tlist);
/* set error code to RT_ERROR */
thread->error = -RT_ERROR;
/*
* resume thread
* In rt_thread_resume function, it will remove current thread from
* suspend list
*/
rt_thread_resume(thread);
/* enable interrupt */
rt_hw_interrupt_enable(temp);
}
/* resume on push list */
while (!rt_list_isempty(&(queue->suspended_push_list)))
{
/* disable interrupt */
temp = rt_hw_interrupt_disable();
/* get next suspend thread */
thread = rt_list_entry(queue->suspended_push_list.next,
struct rt_thread,
tlist);
/* set error code to RT_ERROR */
thread->error = -RT_ERROR;
/*
* resume thread
* In rt_thread_resume function, it will remove current thread from
* suspend list
*/
rt_thread_resume(thread);
/* enable interrupt */
rt_hw_interrupt_enable(temp);
}
rt_exit_critical();
rt_schedule();
}
static rt_err_t _audio_send_replay_frame(struct rt_audio_device *audio) static rt_err_t _audio_send_replay_frame(struct rt_audio_device *audio)
{ {
rt_err_t result = RT_EOK; rt_err_t result = RT_EOK;
@ -339,7 +45,7 @@ static rt_err_t _audio_send_replay_frame(struct rt_audio_device *audio)
RT_ASSERT(audio != RT_NULL); RT_ASSERT(audio != RT_NULL);
//check repaly queue is empty //check repaly queue is empty
if(_audio_queue_peak(&audio->replay->queue,&frame) != RT_EOK) if (rt_data_queue_peak(&audio->replay->queue, &frame.data_ptr, &frame.data_size) != RT_EOK)
{ {
AUDIO_DBG("TX queue is empty\n"); AUDIO_DBG("TX queue is empty\n");
result = -RT_EEMPTY; result = -RT_EEMPTY;
@ -351,10 +57,10 @@ static rt_err_t _audio_send_replay_frame(struct rt_audio_device *audio)
goto _exit; goto _exit;
} }
if(audio->ops->transmit != RT_NULL) if (audio->ops->transmit != RT_NULL)
{ {
AUDIO_DBG("audio transmit...\n"); AUDIO_DBG("audio transmit...\n");
if(audio->ops->transmit(audio,frame.data_ptr,RT_NULL, frame.data_size) != frame.data_size) if (audio->ops->transmit(audio, frame.data_ptr, RT_NULL, frame.data_size) != frame.data_size)
{ {
result = -RT_EBUSY; result = -RT_EBUSY;
@ -363,27 +69,25 @@ static rt_err_t _audio_send_replay_frame(struct rt_audio_device *audio)
} }
//pop the head frame... //pop the head frame...
_audio_queue_pop(&audio->replay->queue,&frame,RT_WAITING_NO); rt_data_queue_pop(&audio->replay->queue, &frame.data_ptr, &frame.data_size, RT_WAITING_FOREVER);
_exit: _exit: return result;
return result;
} }
static rt_err_t _audio_flush_replay_frame(struct rt_audio_device *audio) static rt_err_t _audio_flush_replay_frame(struct rt_audio_device *audio)
{ {
struct rt_audio_frame frame; struct rt_audio_frame frame;
if(audio->replay == RT_NULL) if (audio->replay == RT_NULL)
return -RT_EIO; return -RT_EIO;
while (rt_data_queue_peak(&audio->replay->queue, &frame.data_ptr, &frame.data_size) == RT_EOK)
while(_audio_queue_peak(&audio->replay->queue,&frame) == RT_EOK)
{ {
//pop the head frame... //pop the head frame...
_audio_queue_pop(&audio->replay->queue,&frame,RT_WAITING_NO); rt_data_queue_pop(&audio->replay->queue, &frame.data_ptr, &frame.data_size, RT_WAITING_FOREVER);
/* notify transmitted complete. */ /* notify transmitted complete. */
if(audio->parent.tx_complete != RT_NULL) if (audio->parent.tx_complete != RT_NULL)
audio->parent.tx_complete(&audio->parent,(void *)frame.data_ptr); audio->parent.tx_complete(&audio->parent, (void *) frame.data_ptr);
} }
return RT_EOK; return RT_EOK;
@ -395,7 +99,7 @@ static rt_err_t _audio_dev_init(struct rt_device *dev)
struct rt_audio_device *audio; struct rt_audio_device *audio;
RT_ASSERT(dev != RT_NULL); RT_ASSERT(dev != RT_NULL);
audio = (struct rt_audio_device *)dev; audio = (struct rt_audio_device *) dev;
/* initialize replay & record */ /* initialize replay & record */
audio->replay = RT_NULL; audio->replay = RT_NULL;
@ -415,7 +119,7 @@ static rt_err_t _audio_dev_open(struct rt_device *dev, rt_uint16_t oflag)
struct rt_audio_device *audio; struct rt_audio_device *audio;
RT_ASSERT(dev != RT_NULL); RT_ASSERT(dev != RT_NULL);
audio = (struct rt_audio_device *)dev; audio = (struct rt_audio_device *) dev;
/* check device flag with the open flag */ /* check device flag with the open flag */
if ((oflag & RT_DEVICE_OFLAG_RDONLY) && !(dev->flag & RT_DEVICE_FLAG_RDONLY)) if ((oflag & RT_DEVICE_OFLAG_RDONLY) && !(dev->flag & RT_DEVICE_FLAG_RDONLY))
@ -430,18 +134,18 @@ static rt_err_t _audio_dev_open(struct rt_device *dev, rt_uint16_t oflag)
if (oflag & RT_DEVICE_OFLAG_WRONLY) if (oflag & RT_DEVICE_OFLAG_WRONLY)
{ {
AUDIO_DBG("open audio device ,oflag = %x\n",oflag); AUDIO_DBG("open audio device ,oflag = %x\n",oflag);
if(audio->replay == RT_NULL) if (audio->replay == RT_NULL)
{ {
struct rt_audio_replay *replay = (struct rt_audio_replay *)rt_malloc(sizeof(struct rt_audio_replay)); struct rt_audio_replay *replay = (struct rt_audio_replay *) rt_malloc(sizeof(struct rt_audio_replay));
if(replay == RT_NULL) if (replay == RT_NULL)
{ {
AUDIO_DBG("request memory for replay error\n"); AUDIO_DBG("request memory for replay error\n");
return -RT_ENOMEM; return -RT_ENOMEM;
} }
//init queue for audio replay //init queue for audio replay
_audio_queue_init(&replay->queue,CFG_AUDIO_REPLAY_QUEUE_COUNT,CFG_AUDIO_REPLAY_QUEUE_COUNT / 2); rt_data_queue_init(&replay->queue, CFG_AUDIO_REPLAY_QUEUE_COUNT, CFG_AUDIO_REPLAY_QUEUE_COUNT / 2, RT_NULL);
replay->activated = RT_FALSE; replay->activated = RT_FALSE;
audio->replay = replay; audio->replay = replay;
@ -450,13 +154,13 @@ static rt_err_t _audio_dev_open(struct rt_device *dev, rt_uint16_t oflag)
dev->open_flag |= RT_DEVICE_OFLAG_WRONLY; dev->open_flag |= RT_DEVICE_OFLAG_WRONLY;
} }
if(oflag & RT_DEVICE_OFLAG_RDONLY) if (oflag & RT_DEVICE_OFLAG_RDONLY)
{ {
if(audio->record == RT_NULL) if (audio->record == RT_NULL)
{ {
struct rt_audio_record *record = (struct rt_audio_record *)rt_malloc(sizeof(struct rt_audio_record)); struct rt_audio_record *record = (struct rt_audio_record *) rt_malloc(sizeof(struct rt_audio_record));
if(record == RT_NULL) if (record == RT_NULL)
{ {
AUDIO_DBG("request memory for record error\n"); AUDIO_DBG("request memory for record error\n");
return -RT_ENOMEM; return -RT_ENOMEM;
@ -467,7 +171,7 @@ static rt_err_t _audio_dev_open(struct rt_device *dev, rt_uint16_t oflag)
rt_size_t size = CFG_AUDIO_RECORD_PIPE_SIZE; rt_size_t size = CFG_AUDIO_RECORD_PIPE_SIZE;
rt_uint8_t *buf = rt_malloc(CFG_AUDIO_RECORD_PIPE_SIZE); rt_uint8_t *buf = rt_malloc(CFG_AUDIO_RECORD_PIPE_SIZE);
if(buf == RT_NULL) if (buf == RT_NULL)
{ {
rt_free(record); rt_free(record);
AUDIO_DBG("request pipe memory error\n"); AUDIO_DBG("request pipe memory error\n");
@ -475,7 +179,8 @@ static rt_err_t _audio_dev_open(struct rt_device *dev, rt_uint16_t oflag)
return -RT_ENOMEM; return -RT_ENOMEM;
} }
rt_pipe_init(&record->pipe,"recpipe",RT_PIPE_FLAG_FORCE_WR | RT_PIPE_FLAG_BLOCK_RD,buf,CFG_AUDIO_RECORD_PIPE_SIZE); rt_pipe_init(&record->pipe, "recpipe", RT_PIPE_FLAG_FORCE_WR | RT_PIPE_FLAG_BLOCK_RD, buf,
CFG_AUDIO_RECORD_PIPE_SIZE);
} }
record->activated = RT_FALSE; record->activated = RT_FALSE;
@ -483,9 +188,9 @@ static rt_err_t _audio_dev_open(struct rt_device *dev, rt_uint16_t oflag)
} }
//open record pipe //open record pipe
if(audio->record != RT_NULL) if (audio->record != RT_NULL)
{ {
rt_device_open(RT_DEVICE(&audio->record->pipe),RT_DEVICE_OFLAG_RDONLY); rt_device_open(RT_DEVICE(&audio->record->pipe), RT_DEVICE_OFLAG_RDONLY);
} }
dev->open_flag |= RT_DEVICE_OFLAG_RDONLY; dev->open_flag |= RT_DEVICE_OFLAG_RDONLY;
@ -498,38 +203,39 @@ static rt_err_t _audio_dev_close(struct rt_device *dev)
{ {
struct rt_audio_device *audio; struct rt_audio_device *audio;
RT_ASSERT(dev != RT_NULL); RT_ASSERT(dev != RT_NULL);
audio = (struct rt_audio_device *)dev; audio = (struct rt_audio_device *) dev;
//shutdown the lower device //shutdown the lower device
if(audio->ops->shutdown != RT_NULL) if (audio->ops->shutdown != RT_NULL)
audio->ops->shutdown(audio); audio->ops->shutdown(audio);
if(dev->open_flag & RT_DEVICE_OFLAG_WRONLY) if (dev->open_flag & RT_DEVICE_OFLAG_WRONLY)
{ {
struct rt_audio_frame frame; struct rt_audio_frame frame;
//stop replay stream //stop replay stream
audio->ops->stop(audio,AUDIO_STREAM_REPLAY); audio->ops->stop(audio, AUDIO_STREAM_REPLAY);
//flush all frame //flush all frame
while(_audio_queue_peak(&audio->replay->queue,&frame) == RT_EOK) while (rt_data_queue_peak(&audio->replay->queue, &frame.data_ptr, &frame.data_size) == RT_EOK)
{ {
_audio_queue_pop(&audio->replay->queue,&frame,RT_WAITING_NO); //pop the head frame...
rt_data_queue_pop(&audio->replay->queue, &frame.data_ptr, &frame.data_size, RT_WAITING_FOREVER);
//indicate this frame complete(maybe upper device need free data) /* notify transmitted complete. */
if(dev->tx_complete != RT_NULL) if (audio->parent.tx_complete != RT_NULL)
dev->tx_complete(dev,(void *)frame.data_ptr); audio->parent.tx_complete(&audio->parent, (void *) frame.data_ptr);
} }
dev->open_flag &= ~RT_DEVICE_OFLAG_WRONLY; dev->open_flag &= ~RT_DEVICE_OFLAG_WRONLY;
} }
if(dev->open_flag & RT_DEVICE_OFLAG_RDONLY) if (dev->open_flag & RT_DEVICE_OFLAG_RDONLY)
{ {
//stop record stream //stop record stream
audio->ops->stop(audio,AUDIO_STREAM_RECORD); audio->ops->stop(audio, AUDIO_STREAM_RECORD);
//close record pipe //close record pipe
if(audio->record != RT_NULL) if (audio->record != RT_NULL)
rt_device_close(RT_DEVICE(&audio->record->pipe)); rt_device_close(RT_DEVICE(&audio->record->pipe));
dev->open_flag &= ~RT_DEVICE_OFLAG_RDONLY; dev->open_flag &= ~RT_DEVICE_OFLAG_RDONLY;
@ -542,8 +248,8 @@ static rt_size_t _audio_dev_read(struct rt_device *dev, rt_off_t pos, void *buff
{ {
struct rt_audio_device *audio; struct rt_audio_device *audio;
RT_ASSERT(dev != RT_NULL); RT_ASSERT(dev != RT_NULL);
audio = (struct rt_audio_device *)dev; audio = (struct rt_audio_device *) dev;
if(!(dev->open_flag & RT_DEVICE_OFLAG_RDONLY) || (audio->record == RT_NULL)) if (!(dev->open_flag & RT_DEVICE_OFLAG_RDONLY) || (audio->record == RT_NULL))
return 0; return 0;
return rt_device_read(RT_DEVICE(&audio->record->pipe), pos, buffer, size); return rt_device_read(RT_DEVICE(&audio->record->pipe), pos, buffer, size);
@ -556,21 +262,17 @@ static rt_size_t _audio_dev_write(struct rt_device *dev, rt_off_t pos, const voi
struct rt_audio_device *audio; struct rt_audio_device *audio;
RT_ASSERT(dev != RT_NULL); RT_ASSERT(dev != RT_NULL);
audio = (struct rt_audio_device *)dev; audio = (struct rt_audio_device *) dev;
if(!(dev->open_flag & RT_DEVICE_OFLAG_WRONLY) || (audio->replay == RT_NULL)) if (!(dev->open_flag & RT_DEVICE_OFLAG_WRONLY) || (audio->replay == RT_NULL))
return 0; return 0;
AUDIO_DBG("audio write : pos = %d,buffer = %x,size = %d\n",pos,(rt_uint32_t)buffer,size); AUDIO_DBG("audio write : pos = %d,buffer = %x,size = %d\n",pos,(rt_uint32_t)buffer,size);
//push a new frame to tx queue //push a new frame to tx queue
{ {
struct rt_audio_frame frame; result = rt_data_queue_push(&audio->replay->queue, buffer, size,
frame.data_ptr = buffer; RT_WAITING_FOREVER);
frame.data_size = size; if (result != RT_EOK)
frame.data_ofs = 0;
result = _audio_queue_push(&audio->replay->queue,&frame,RT_WAITING_FOREVER);
if(result != RT_EOK)
{ {
AUDIO_DBG("TX frame queue push error\n"); AUDIO_DBG("TX frame queue push error\n");
rt_set_errno(-RT_EFULL); rt_set_errno(-RT_EFULL);
@ -580,7 +282,7 @@ static rt_size_t _audio_dev_write(struct rt_device *dev, rt_off_t pos, const voi
//check tx state... //check tx state...
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
if(audio->replay->activated != RT_TRUE) if (audio->replay->activated != RT_TRUE)
{ {
audio->replay->activated = RT_TRUE; audio->replay->activated = RT_TRUE;
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
@ -596,7 +298,7 @@ static rt_err_t _audio_dev_control(struct rt_device *dev, rt_uint8_t cmd, void *
rt_err_t result = RT_EOK; rt_err_t result = RT_EOK;
struct rt_audio_device *audio; struct rt_audio_device *audio;
RT_ASSERT(dev != RT_NULL); RT_ASSERT(dev != RT_NULL);
audio = (struct rt_audio_device *)dev; audio = (struct rt_audio_device *) dev;
//dev stat... //dev stat...
switch (cmd) switch (cmd)
@ -653,7 +355,7 @@ static rt_err_t _audio_dev_control(struct rt_device *dev, rt_uint8_t cmd, void *
if (audio->ops->start != RT_NULL) if (audio->ops->start != RT_NULL)
result = audio->ops->stop(audio, stream); result = audio->ops->stop(audio, stream);
if(stream == AUDIO_STREAM_REPLAY) if (stream == AUDIO_STREAM_REPLAY)
{ {
_audio_flush_replay_frame(audio); _audio_flush_replay_frame(audio);
} }
@ -677,35 +379,31 @@ static rt_err_t _audio_dev_control(struct rt_device *dev, rt_uint8_t cmd, void *
result = audio->ops->resume(audio, stream); result = audio->ops->resume(audio, stream);
//resume tx frame... //resume tx frame...
if(stream == AUDIO_STREAM_REPLAY) if (stream == AUDIO_STREAM_REPLAY)
_audio_send_replay_frame(audio); _audio_send_replay_frame(audio);
} }
break; break;
#ifdef AUDIO_DEVICE_USE_PRIVATE_BUFFER
case AUDIO_CTL_ALLOCBUFFER: case AUDIO_CTL_ALLOCBUFFER:
{ {
struct rt_audio_buf_desc *desc = (struct rt_audio_buf_desc *)args; struct rt_audio_buf_desc *desc = (struct rt_audio_buf_desc *) args;
if((audio->ops->buffer_alloc != RT_NULL) && (desc != RT_NULL)) if (desc)
{ {
result = audio->ops->buffer_alloc(audio,&desc->data_ptr,&desc->data_size); desc->data_size = AUDIO_DEVICE_DECODE_MP_BLOCK_SZ * 2;
break; desc->data_ptr = rt_mp_alloc(&audio->mp, RT_WAITING_FOREVER);
}
result = -RT_EIO; result = RT_EOK;
}
else result = -RT_EIO;
} }
break; break;
case AUDIO_CTL_FREEBUFFER: case AUDIO_CTL_FREEBUFFER:
{ {
rt_uint8_t *data_ptr = (rt_uint8_t *)args; rt_uint8_t *data_ptr = (rt_uint8_t *) args;
if((audio->ops->buffer_free != RT_NULL) && (data_ptr != RT_NULL)) if (data_ptr)
{ rt_mp_free(data_ptr);
audio->ops->buffer_free(audio,data_ptr);
break;
}
} }
break; break;
#endif
default: default:
result = audio->ops->control(audio, cmd, args); result = audio->ops->control(audio, cmd, args);
break; break;
@ -714,7 +412,6 @@ static rt_err_t _audio_dev_control(struct rt_device *dev, rt_uint8_t cmd, void *
return result; return result;
} }
rt_err_t rt_audio_register(struct rt_audio_device *audio, const char *name, rt_uint32_t flag, void *data) rt_err_t rt_audio_register(struct rt_audio_device *audio, const char *name, rt_uint32_t flag, void *data)
{ {
struct rt_device *device; struct rt_device *device;
@ -733,18 +430,17 @@ rt_err_t rt_audio_register(struct rt_audio_device *audio, const char *name, rt_u
device->control = _audio_dev_control; device->control = _audio_dev_control;
device->user_data = data; device->user_data = data;
//init memory pool for replay
{
rt_uint8_t *mempool = rt_malloc(AUDIO_DEVICE_DECODE_MP_SZ);
rt_mp_init(&audio->mp, "adu_mp", mempool, AUDIO_DEVICE_DECODE_MP_SZ,
AUDIO_DEVICE_DECODE_MP_BLOCK_SZ * 2);
}
/* register a character device */ /* register a character device */
return rt_device_register(device, name, flag | RT_DEVICE_FLAG_REMOVABLE); return rt_device_register(device, name, flag | RT_DEVICE_FLAG_REMOVABLE);
} }
rt_size_t rt_audio_get_buffer_size(struct rt_audio_device *audio)
{
// return (audio->config.period_count * audio->config.period_size);
return 0;
}
int rt_audio_samplerate_to_speed(rt_uint32_t bitValue) int rt_audio_samplerate_to_speed(rt_uint32_t bitValue)
{ {
int speed = 0; int speed = 0;
@ -794,8 +490,6 @@ int rt_audio_samplerate_to_speed(rt_uint32_t bitValue)
return speed; return speed;
} }
rt_uint32_t rt_audio_format_to_bits(rt_uint32_t format) rt_uint32_t rt_audio_format_to_bits(rt_uint32_t format)
{ {
switch (format) switch (format)
@ -813,7 +507,7 @@ rt_uint32_t rt_audio_format_to_bits(rt_uint32_t format)
}; };
} }
void rt_audio_tx_complete(struct rt_audio_device *audio,rt_uint8_t *pbuf) void rt_audio_tx_complete(struct rt_audio_device *audio, rt_uint8_t *pbuf)
{ {
rt_err_t result; rt_err_t result;
AUDIO_DBG("audio tx complete ptr=%x...\n",(rt_uint32_t)pbuf); AUDIO_DBG("audio tx complete ptr=%x...\n",(rt_uint32_t)pbuf);
@ -822,22 +516,22 @@ void rt_audio_tx_complete(struct rt_audio_device *audio,rt_uint8_t *pbuf)
do do
{ {
result = _audio_send_replay_frame(audio); result = _audio_send_replay_frame(audio);
}while(result == RT_EOK); } while (result == RT_EOK);
/* notify transmitted complete. */ /* notify transmitted complete. */
if(audio->parent.tx_complete != RT_NULL) if (audio->parent.tx_complete != RT_NULL)
audio->parent.tx_complete(&audio->parent,(void *)pbuf); audio->parent.tx_complete(&audio->parent, (void *) pbuf);
} }
void rt_audio_rx_done(struct rt_audio_device *audio,rt_uint8_t *pbuf,rt_size_t len) void rt_audio_rx_done(struct rt_audio_device *audio, rt_uint8_t *pbuf, rt_size_t len)
{ {
rt_err_t result = RT_EOK; rt_err_t result = RT_EOK;
//save data to record pipe //save data to record pipe
rt_device_write(RT_DEVICE(RT_DEVICE(&audio->record->pipe)),0,pbuf,len); rt_device_write(RT_DEVICE(RT_DEVICE(&audio->record->pipe)), 0, pbuf, len);
/* invoke callback */ /* invoke callback */
if(audio->parent.rx_indicate != RT_NULL) if (audio->parent.rx_indicate != RT_NULL)
audio->parent.rx_indicate(&audio->parent,len); audio->parent.rx_indicate(&audio->parent, len);
} }

View File

@ -1,9 +1,30 @@
/*
* File : audio.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017-05-09 Urey first version
*/
#ifndef __AUDIO_H__ #ifndef __AUDIO_H__
#define __AUDIO_H__ #define __AUDIO_H__
//#define AUDIO_DEVICE_USE_PRIVATE_BUFFER
/* AUDIO command */ /* AUDIO command */
#define _AUDIO_CTL(a) (0x10 + a) #define _AUDIO_CTL(a) (0x10 + a)
@ -93,6 +114,10 @@
#define CFG_AUDIO_REPLAY_QUEUE_COUNT 4 #define CFG_AUDIO_REPLAY_QUEUE_COUNT 4
#define CFG_AUDIO_RECORD_PIPE_SIZE (8 * 1024) #define CFG_AUDIO_RECORD_PIPE_SIZE (8 * 1024)
#define AUDIO_DEVICE_MP_CNT (4)
#define AUDIO_DEVICE_DECODE_MP_BLOCK_SZ (4352 * 4)
#define AUDIO_DEVICE_DECODE_MP_SZ ((AUDIO_DEVICE_DECODE_MP_BLOCK_SZ*2 + 4)*AUDIO_DEVICE_MP_CNT)
enum enum
{ {
@ -117,23 +142,6 @@ struct rt_audio_frame
{ {
const void *data_ptr; const void *data_ptr;
rt_size_t data_size; rt_size_t data_size;
rt_size_t data_ofs;
};
struct rt_audio_queue
{
rt_uint16_t count;
rt_uint16_t size;
rt_uint16_t lwm;
rt_bool_t waiting_lwm;
rt_uint16_t get_index;
rt_uint16_t put_index;
struct rt_audio_frame *queue;
rt_list_t suspended_push_list;
rt_list_t suspended_pop_list;
}; };
struct rt_audio_device; struct rt_audio_device;
@ -156,10 +164,6 @@ struct rt_audio_ops
//get page size of codec or private buffer's info //get page size of codec or private buffer's info
void (*buffer_info) (struct rt_audio_device *audio,struct rt_audio_buf_info *info ); void (*buffer_info) (struct rt_audio_device *audio,struct rt_audio_buf_info *info );
#ifdef AUDIO_DEVICE_USE_PRIVATE_BUFFER
rt_err_t (*buffer_alloc) (struct rt_audio_device *audio,rt_uint8_t **data_ptr,rt_size_t *size);
void (*buffer_free) (struct rt_audio_device *audio,rt_uint8_t *data_ptr);
#endif
}; };
@ -188,7 +192,7 @@ struct rt_audio_caps
struct rt_audio_replay struct rt_audio_replay
{ {
rt_bool_t activated; rt_bool_t activated;
struct rt_audio_queue queue; struct rt_data_queue queue;
}; };
struct rt_audio_record struct rt_audio_record
@ -202,14 +206,16 @@ struct rt_audio_device
struct rt_device parent; struct rt_device parent;
struct rt_audio_ops *ops; struct rt_audio_ops *ops;
struct rt_mempool mp;
struct rt_audio_replay *replay; struct rt_audio_replay *replay;
struct rt_audio_record *record; struct rt_audio_record *record;
}; };
rt_err_t rt_audio_register(struct rt_audio_device *audio, const char *name, rt_uint32_t flag, void *data); rt_err_t rt_audio_register (struct rt_audio_device *audio, const char *name, rt_uint32_t flag, void *data);
void rt_audio_tx_complete(struct rt_audio_device *audio,rt_uint8_t *pbuf); void rt_audio_tx_complete (struct rt_audio_device *audio,rt_uint8_t *pbuf);
void rt_audio_rx_done(struct rt_audio_device *audio,rt_uint8_t *pbuf,rt_size_t len); void rt_audio_rx_done (struct rt_audio_device *audio,rt_uint8_t *pbuf,rt_size_t len);
rt_uint32_t rt_audio_format_to_bits(rt_uint32_t format); rt_uint32_t rt_audio_format_to_bits (rt_uint32_t format);
/* Device Control Commands */ /* Device Control Commands */
@ -219,7 +225,6 @@ rt_uint32_t rt_audio_format_to_bits(rt_uint32_t format);
#define CODEC_CMD_SAMPLERATE 3 #define CODEC_CMD_SAMPLERATE 3
#define CODEC_CMD_EQ 4 #define CODEC_CMD_EQ 4
#define CODEC_CMD_3D 5 #define CODEC_CMD_3D 5
#define CODEC_CMD_SWITCH 6
#define CODEC_VOLUME_MAX (63) #define CODEC_VOLUME_MAX (63)