CARPLAY版本整理
This commit is contained in:
@ -0,0 +1,650 @@
|
||||
#include "FreeRTOS.h"
|
||||
#include "chip.h"
|
||||
#include "board.h"
|
||||
#include "audio.h"
|
||||
|
||||
enum
|
||||
{
|
||||
REPLAY_EVT_NONE = 0x00,
|
||||
REPLAY_EVT_START = 0x01,
|
||||
REPLAY_EVT_STOP = 0x02,
|
||||
};
|
||||
|
||||
struct audio_queue_data
|
||||
{
|
||||
uint8_t *data;
|
||||
int size;
|
||||
};
|
||||
|
||||
struct audio_device *audio_dev[I2S_NUMS] = {NULL, NULL};
|
||||
|
||||
static int audio_send_replay_frame(struct audio_device *audio)
|
||||
{
|
||||
int result = 0;
|
||||
uint8_t *data;
|
||||
size_t dst_size, src_size;
|
||||
uint16_t position, remain_bytes = 0, index = 0;
|
||||
struct audio_buf_info *buf_info;
|
||||
struct audio_queue_data qdata;
|
||||
|
||||
configASSERT(audio != 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 (xQueueIsQueueEmptyFromISR(audio->replay->queue) == pdTRUE)
|
||||
{
|
||||
/* ack stop event */
|
||||
if (audio->replay->event & REPLAY_EVT_STOP) {
|
||||
xQueueSendFromISR(audio->replay->cmp, NULL, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* send zero frames */
|
||||
memset(&buf_info->buffer[audio->replay->pos], 0, dst_size);
|
||||
|
||||
audio->replay->pos += dst_size;
|
||||
audio->replay->pos %= buf_info->total_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
memset(&buf_info->buffer[audio->replay->pos], 0, dst_size);
|
||||
|
||||
/* copy data from memory pool to hardware device fifo */
|
||||
while (index < dst_size)
|
||||
{
|
||||
result = xQueuePeekFromISR(audio->replay->queue, &qdata);
|
||||
if (result != pdTRUE)
|
||||
{
|
||||
TRACE_DEBUG("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 = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
data = qdata.data;
|
||||
src_size = qdata.size;
|
||||
remain_bytes = configMIN((dst_size - index), (src_size - audio->replay->read_index));
|
||||
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)
|
||||
{
|
||||
audio->replay->read_index = 0;
|
||||
xQueueReceiveFromISR(audio->replay->queue, &qdata, 0);
|
||||
for (int i = 0; i < AUDIO_REPLAY_MP_BLOCK_COUNT; i++) {
|
||||
if (qdata.data == audio->replay->mempool + AUDIO_REPLAY_MP_BLOCK_SIZE * i) {
|
||||
audio->replay->mpstatus[i] = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (audio->ops->transmit != NULL)
|
||||
{
|
||||
if (audio->ops->transmit(audio, &buf_info->buffer[position], NULL, dst_size) != dst_size)
|
||||
result = -1;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int audio_receive_record_frame(struct audio_device *audio)
|
||||
{
|
||||
struct audio_buf_info *buf_info;
|
||||
size_t dst_size;
|
||||
int read_pos;
|
||||
int result = 0;
|
||||
|
||||
configASSERT(audio != NULL);
|
||||
|
||||
/* ack stop event */
|
||||
if (audio->record->event & REPLAY_EVT_STOP) {
|
||||
xQueueSendFromISR(audio->record->cmp, NULL, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
buf_info = &audio->record->buf_info;
|
||||
if(buf_info->buffer && buf_info->total_size)
|
||||
{
|
||||
if(audio->record->remain_size <= 0)
|
||||
{
|
||||
if(audio->record->read_index == buf_info->total_size)
|
||||
{
|
||||
if(audio->record->receive_cb)
|
||||
audio->record->receive_cb(audio);
|
||||
}
|
||||
audio->record->remain_size = buf_info->total_size;
|
||||
audio->record->read_index = 0;
|
||||
}
|
||||
|
||||
read_pos = audio->record->read_index;
|
||||
dst_size = buf_info->block_size;
|
||||
|
||||
if(audio->record->remain_size < buf_info->block_size)
|
||||
dst_size = audio->record->remain_size;
|
||||
|
||||
if(audio->ops->transmit)
|
||||
{
|
||||
if(audio->ops->transmit(audio, NULL, &buf_info->buffer[read_pos], dst_size) != dst_size)
|
||||
{
|
||||
printf("%s() transmit failed.\n", __func__);
|
||||
result = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
audio->record->read_index += dst_size;
|
||||
audio->record->remain_size -= dst_size;
|
||||
if(audio->record->remain_size < 0)
|
||||
{
|
||||
printf("%s() Invalid remain_size:%d.\n", __func__, audio->record->remain_size);
|
||||
audio->record->remain_size = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!buf_info->buffer)
|
||||
printf("%s() record buffer is NULL.\n", __func__);
|
||||
if(!buf_info->total_size)
|
||||
printf("%s() record buffer size is 0.\n", __func__);
|
||||
result = -1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static int audio_flush_replay_frame(struct audio_device *audio)
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
if (audio->replay->write_index)
|
||||
{
|
||||
struct audio_queue_data qdata = {audio->replay->write_data, audio->replay->write_index};
|
||||
result = xQueueSend(audio->replay->queue, &qdata, portMAX_DELAY);
|
||||
audio->replay->write_index = 0;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int audio_replay_start(struct audio_device *audio)
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
if (audio->replay->activated != true)
|
||||
{
|
||||
/* start playback hardware device */
|
||||
if (audio->ops->start)
|
||||
result = audio->ops->start(audio, AUDIO_STREAM_REPLAY);
|
||||
|
||||
audio->replay->activated = true;
|
||||
TRACE_DEBUG("start audio replay device");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int audio_replay_stop(struct audio_device *audio)
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
if (audio->replay->activated == 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 */
|
||||
xQueueReset(audio->replay->cmp);
|
||||
xQueueReceive(audio->replay->cmp, NULL, pdMS_TO_TICKS(2000));
|
||||
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 = false;
|
||||
TRACE_DEBUG("stop audio replay device");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int audio_record_start(struct audio_device *audio)
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
if (audio->record->activated != true)
|
||||
{
|
||||
/* start record hardware device */
|
||||
if (audio->ops->start)
|
||||
result = audio->ops->start(audio, AUDIO_STREAM_RECORD);
|
||||
|
||||
audio->record->activated = true;
|
||||
TRACE_DEBUG("start audio record device");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int audio_record_stop(struct audio_device *audio)
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
if (audio->record->activated == true)
|
||||
{
|
||||
/* notify irq(or thread) to stop the data transmission */
|
||||
audio->record->event |= REPLAY_EVT_STOP;
|
||||
|
||||
/* waiting for the remaining data transfer to complete */
|
||||
xQueueReset(audio->record->cmp);
|
||||
xQueueReceive(audio->record->cmp, NULL, pdMS_TO_TICKS(1000));
|
||||
|
||||
/* stop record hardware device */
|
||||
if (audio->ops->stop)
|
||||
result = audio->ops->stop(audio, AUDIO_STREAM_RECORD);
|
||||
|
||||
audio->record->event &= ~REPLAY_EVT_STOP;
|
||||
|
||||
audio->record->activated = false;
|
||||
TRACE_DEBUG("stop audio record device");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int audio_dev_init(struct audio_device *audio)
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
configASSERT(audio != NULL);
|
||||
|
||||
/* initialize replay & record */
|
||||
audio->replay = NULL;
|
||||
audio->record = NULL;
|
||||
|
||||
/* initialize replay */
|
||||
if ((audio->flag & AUDIO_FLAG_REPLAY) == AUDIO_FLAG_REPLAY)
|
||||
{
|
||||
struct audio_replay *replay = (struct audio_replay *) pvPortMalloc(sizeof(struct audio_replay));
|
||||
|
||||
if (replay == NULL)
|
||||
return -ENOMEM;
|
||||
memset(replay, 0, sizeof(struct audio_replay));
|
||||
|
||||
/* alloc mempool */
|
||||
replay->mempool = pvPortMalloc(AUDIO_REPLAY_MP_BLOCK_SIZE * AUDIO_REPLAY_MP_BLOCK_COUNT);
|
||||
if (!replay->mempool)
|
||||
return -ENOMEM;
|
||||
|
||||
/* init queue for audio replay */
|
||||
replay->queue = xQueueCreate(CFG_AUDIO_REPLAY_QUEUE_COUNT, sizeof(struct audio_queue_data));
|
||||
|
||||
/* init mutex lock for audio replay */
|
||||
replay->lock = xSemaphoreCreateMutex();
|
||||
|
||||
replay->cmp = xQueueCreate(1, 0);
|
||||
|
||||
replay->activated = false;
|
||||
audio->replay = replay;
|
||||
|
||||
/* get replay buffer information */
|
||||
if (audio->ops->buffer_info)
|
||||
audio->ops->buffer_info(audio, &audio->replay->buf_info, AUDIO_FLAG_REPLAY);
|
||||
}
|
||||
|
||||
/* initialize record */
|
||||
if ((audio->flag & AUDIO_FLAG_RECORD) == AUDIO_FLAG_RECORD)
|
||||
{
|
||||
struct audio_record *record = (struct audio_record *) pvPortMalloc(sizeof(struct audio_record));
|
||||
//uint8_t *buffer;
|
||||
|
||||
if (record == NULL)
|
||||
return -ENOMEM;
|
||||
memset(record, 0, sizeof(struct audio_record));
|
||||
|
||||
/* init pipe for record*/
|
||||
/* buffer = pvPortMalloc(AUDIO_RECORD_PIPE_SIZE);
|
||||
if (buffer == NULL)
|
||||
{
|
||||
vPortFree(record);
|
||||
TRACE_ERROR("malloc memory for for record pipe failed");
|
||||
return -ENOMEM;
|
||||
}
|
||||
audio_pipe_init(&record->pipe, "record",
|
||||
(int32_t)(RT_PIPE_FLAG_FORCE_WR | RT_PIPE_FLAG_BLOCK_RD),
|
||||
buffer,
|
||||
RT_AUDIO_RECORD_PIPE_SIZE); */
|
||||
record->cmp = xQueueCreate(1, 0);
|
||||
|
||||
record->activated = false;
|
||||
audio->record = record;
|
||||
|
||||
/* get record buffer information */
|
||||
if (audio->ops->buffer_info)
|
||||
audio->ops->buffer_info(audio, &audio->record->buf_info, AUDIO_FLAG_RECORD);
|
||||
}
|
||||
|
||||
/* initialize hardware configuration */
|
||||
if (audio->ops->init)
|
||||
audio->ops->init(audio);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
struct audio_device *audio_dev_open(uint32_t oflag)
|
||||
{
|
||||
struct audio_device *audio = NULL;
|
||||
|
||||
#ifdef AUDIO_REPLAY_I2S
|
||||
/* initialize the Rx/Tx structure according to open flag */
|
||||
if ((oflag & AUDIO_FLAG_REPLAY) == AUDIO_FLAG_REPLAY)
|
||||
{
|
||||
audio = audio_dev[AUDIO_REPLAY_I2S];
|
||||
if (audio && audio->replay->activated != true)
|
||||
{
|
||||
TRACE_DEBUG("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;
|
||||
for (int i = 0; i < AUDIO_REPLAY_MP_BLOCK_COUNT; i++)
|
||||
audio->replay->mpstatus[i] = 0;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef AUDIO_RECORD_I2S
|
||||
if ((oflag & AUDIO_FLAG_RECORD) == AUDIO_FLAG_RECORD)
|
||||
{
|
||||
audio = audio_dev[AUDIO_RECORD_I2S];
|
||||
|
||||
if (audio && audio->record->activated != true)
|
||||
{
|
||||
TRACE_DEBUG("open audio record device ,oflag = %x\n", oflag);
|
||||
audio->record->event = REPLAY_EVT_NONE;
|
||||
audio->record->read_index = 0;
|
||||
audio->record->remain_size = 0;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return audio;
|
||||
}
|
||||
|
||||
int audio_dev_close(struct audio_device *audio, uint32_t oflag)
|
||||
{
|
||||
configASSERT(audio != NULL);
|
||||
|
||||
if ((oflag & AUDIO_FLAG_REPLAY) == AUDIO_FLAG_REPLAY)
|
||||
{
|
||||
/* stop replay stream */
|
||||
audio_replay_stop(audio);
|
||||
}
|
||||
|
||||
if ((oflag & AUDIO_FLAG_RECORD) == AUDIO_FLAG_RECORD)
|
||||
{
|
||||
/* stop record stream */
|
||||
audio_record_stop(audio);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int audio_dev_register_record_callback(struct audio_device *audio, int (*callback)(struct audio_device *audio))
|
||||
{
|
||||
configASSERT(audio != NULL);
|
||||
|
||||
if (((audio->flag & AUDIO_FLAG_RECORD) != AUDIO_FLAG_RECORD) || (audio->record == NULL))
|
||||
return -EINVAL;
|
||||
|
||||
audio->record->receive_cb = callback;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int audio_dev_record_set_param(struct audio_device *audio, uint8_t *buf, int size)
|
||||
{
|
||||
if(buf && ((size > 0) && (size%32 == 0))) //size should align with 32 bytes.
|
||||
{
|
||||
audio->record->buf_info.total_size = size;
|
||||
audio->record->remain_size = audio->record->buf_info.total_size;
|
||||
audio->record->buf_info.buffer = buf;
|
||||
audio->record->remain_size = 0;
|
||||
audio->record->read_index = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
int audio_dev_record_start(struct audio_device *audio)
|
||||
{
|
||||
configASSERT(audio != NULL);
|
||||
|
||||
if (audio->record->activated != true)
|
||||
{
|
||||
if(!audio->record->buf_info.buffer || !audio->record->buf_info.total_size)
|
||||
{
|
||||
printf("%s() Invalid buffer:%p or size:%d\n", __func__, audio->record->buf_info.buffer, audio->record->buf_info.total_size);
|
||||
return -1;
|
||||
}
|
||||
audio->record->remain_size = 0;
|
||||
audio->record->read_index = 0;
|
||||
audio_record_start(audio);
|
||||
audio->record->activated = true;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int audio_dev_record_stop(struct audio_device *audio)
|
||||
{
|
||||
if (audio->flag == AUDIO_FLAG_RECORD)
|
||||
{
|
||||
/* stop record stream */
|
||||
audio_record_stop(audio);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t audio_dev_read(struct audio_device *audio, void *buffer, size_t size)
|
||||
{
|
||||
configASSERT(audio != NULL);
|
||||
|
||||
if ((audio->flag != AUDIO_FLAG_RECORD) || (audio->record == NULL))
|
||||
return 0;
|
||||
|
||||
printf("%s() Invalid interface.\n", __func__);
|
||||
|
||||
return 0;//device_read(RT_DEVICE(&audio->record->pipe), pos, buffer, size);
|
||||
}
|
||||
|
||||
size_t audio_dev_write(struct audio_device *audio, const void *buffer, size_t size)
|
||||
{
|
||||
|
||||
uint8_t *ptr;
|
||||
uint16_t block_size, remain_bytes, index = 0;
|
||||
|
||||
configASSERT(audio != NULL);
|
||||
|
||||
if (!((audio->flag & AUDIO_FLAG_REPLAY) == AUDIO_FLAG_REPLAY) || (audio->replay == NULL))
|
||||
return 0;
|
||||
|
||||
/* push a new frame to replay data queue */
|
||||
ptr = (uint8_t *)buffer;
|
||||
block_size = AUDIO_REPLAY_MP_BLOCK_SIZE;
|
||||
|
||||
xSemaphoreTake(audio->replay->lock, portMAX_DELAY);
|
||||
while (index < size)
|
||||
{
|
||||
/* request buffer from replay memory pool */
|
||||
if (audio->replay->write_index % block_size == 0)
|
||||
{
|
||||
uint8_t *mpbuf = NULL;
|
||||
uint32_t st = xTaskGetTickCount();
|
||||
while(1) {
|
||||
int i;
|
||||
portENTER_CRITICAL();
|
||||
for (i = 0; i < AUDIO_REPLAY_MP_BLOCK_COUNT; i++) {
|
||||
if (!audio->replay->mpstatus[i]) {
|
||||
mpbuf = audio->replay->mempool + AUDIO_REPLAY_MP_BLOCK_SIZE * i;
|
||||
audio->replay->mpstatus[i] = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
portEXIT_CRITICAL();
|
||||
if (mpbuf)
|
||||
break;
|
||||
if (xTaskGetTickCount() - st > pdMS_TO_TICKS(1000)) {
|
||||
printf("wait mempool free timeout.\n");
|
||||
mpbuf = audio->replay->mempool;
|
||||
for (i = 1; i < AUDIO_REPLAY_MP_BLOCK_COUNT; i++)
|
||||
audio->replay->mpstatus[i] = 0;
|
||||
break;
|
||||
}
|
||||
vTaskDelay(1);
|
||||
}
|
||||
audio->replay->write_data = mpbuf;
|
||||
memset(audio->replay->write_data, 0, block_size);
|
||||
}
|
||||
|
||||
/* copy data to replay memory pool */
|
||||
remain_bytes = configMIN((block_size - audio->replay->write_index), (size - index));
|
||||
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)
|
||||
{
|
||||
struct audio_queue_data qdata = {audio->replay->write_data, block_size};
|
||||
xQueueSend(audio->replay->queue, &qdata, portMAX_DELAY);
|
||||
}
|
||||
}
|
||||
xSemaphoreGive(audio->replay->lock);
|
||||
|
||||
/* check replay state */
|
||||
if (audio->replay->activated != true)
|
||||
{
|
||||
audio_replay_start(audio);
|
||||
audio->replay->activated = true;
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
int audio_dev_configure(struct audio_device *audio, struct audio_caps *caps)
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
if (audio->ops->configure != NULL)
|
||||
{
|
||||
result = audio->ops->configure(audio, caps);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int audio_register(struct audio_device *audio)
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
configASSERT(audio != NULL);
|
||||
|
||||
audio->rx_indicate = NULL;
|
||||
audio->tx_complete = NULL;
|
||||
|
||||
/* initialize audio device */
|
||||
result = audio_dev_init(audio);
|
||||
|
||||
audio_dev[audio->id] = audio;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int audio_samplerate_to_speed(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;
|
||||
}
|
||||
|
||||
void audio_tx_complete(struct audio_device *audio)
|
||||
{
|
||||
/* try to send next frame */
|
||||
audio_send_replay_frame(audio);
|
||||
}
|
||||
|
||||
int audio_rx_complete(struct audio_device *audio)
|
||||
{
|
||||
/* try to receive next frame */
|
||||
return audio_receive_record_frame(audio);
|
||||
}
|
||||
|
||||
|
||||
void audio_rx_done(struct audio_device *audio, uint8_t *pbuf, size_t len)
|
||||
{
|
||||
/* save data to record pipe */
|
||||
//device_write(RT_DEVICE(&audio->record->pipe), 0, pbuf, len);
|
||||
|
||||
/* invoke callback */
|
||||
/* if (audio->parent.rx_indicate != NULL)
|
||||
audio->parent.rx_indicate(&audio->parent, len); */
|
||||
}
|
@ -0,0 +1,195 @@
|
||||
#ifndef __AUDIO_H__
|
||||
#define __AUDIO_H__
|
||||
|
||||
/* AUDIO command */
|
||||
#define _AUDIO_CTL(a) (0x10 + a)
|
||||
|
||||
#define AUDIO_CTL_GETCAPS _AUDIO_CTL(1)
|
||||
#define AUDIO_CTL_CONFIGURE _AUDIO_CTL(2)
|
||||
#define AUDIO_CTL_START _AUDIO_CTL(3)
|
||||
#define AUDIO_CTL_STOP _AUDIO_CTL(4)
|
||||
#define AUDIO_CTL_GETBUFFERINFO _AUDIO_CTL(5)
|
||||
|
||||
/* Audio Device Types */
|
||||
#define AUDIO_TYPE_QUERY 0x00
|
||||
#define AUDIO_TYPE_INPUT 0x01
|
||||
#define AUDIO_TYPE_OUTPUT 0x02
|
||||
#define AUDIO_TYPE_MIXER 0x04
|
||||
|
||||
/* Supported Sampling Rates */
|
||||
#define AUDIO_SAMP_RATE_8K 0x0001
|
||||
#define AUDIO_SAMP_RATE_11K 0x0002
|
||||
#define AUDIO_SAMP_RATE_16K 0x0004
|
||||
#define AUDIO_SAMP_RATE_22K 0x0008
|
||||
#define AUDIO_SAMP_RATE_32K 0x0010
|
||||
#define AUDIO_SAMP_RATE_44K 0x0020
|
||||
#define AUDIO_SAMP_RATE_48K 0x0040
|
||||
#define AUDIO_SAMP_RATE_96K 0x0080
|
||||
#define AUDIO_SAMP_RATE_128K 0x0100
|
||||
#define AUDIO_SAMP_RATE_160K 0x0200
|
||||
#define AUDIO_SAMP_RATE_172K 0x0400
|
||||
#define AUDIO_SAMP_RATE_192K 0x0800
|
||||
|
||||
/* Supported Bit Rates */
|
||||
#define AUDIO_BIT_RATE_22K 0x01
|
||||
#define AUDIO_BIT_RATE_44K 0x02
|
||||
#define AUDIO_BIT_RATE_48K 0x04
|
||||
#define AUDIO_BIT_RATE_96K 0x08
|
||||
#define AUDIO_BIT_RATE_128K 0x10
|
||||
#define AUDIO_BIT_RATE_160K 0x20
|
||||
#define AUDIO_BIT_RATE_172K 0x40
|
||||
#define AUDIO_BIT_RATE_192K 0x80
|
||||
|
||||
/* Support Dsp(input/output) Units controls */
|
||||
#define AUDIO_DSP_PARAM 0 /* get/set all params */
|
||||
#define AUDIO_DSP_SAMPLERATE 1 /* samplerate */
|
||||
#define AUDIO_DSP_CHANNELS 2 /* channels */
|
||||
#define AUDIO_DSP_SAMPLEBITS 3 /* sample bits width */
|
||||
|
||||
/* Supported Mixer Units controls */
|
||||
#define AUDIO_MIXER_QUERY 0x0000
|
||||
#define AUDIO_MIXER_MUTE 0x0001
|
||||
#define AUDIO_MIXER_VOLUME 0x0002
|
||||
#define AUDIO_MIXER_BASS 0x0004
|
||||
#define AUDIO_MIXER_MID 0x0008
|
||||
#define AUDIO_MIXER_TREBLE 0x0010
|
||||
#define AUDIO_MIXER_EQUALIZER 0x0020
|
||||
#define AUDIO_MIXER_LINE 0x0040
|
||||
#define AUDIO_MIXER_DIGITAL 0x0080
|
||||
#define AUDIO_MIXER_MIC 0x0100
|
||||
#define AUDIO_MIXER_VITURAL 0x0200
|
||||
#define AUDIO_MIXER_EXTEND 0x8000 /* extend mixer command */
|
||||
|
||||
#define AUDIO_VOLUME_MAX (100)
|
||||
#define AUDIO_VOLUME_MIN (0)
|
||||
|
||||
#define CFG_AUDIO_REPLAY_QUEUE_COUNT 8
|
||||
|
||||
/**
|
||||
* audio flags defitions
|
||||
*/
|
||||
//#define AUDIO_FLAG_REPLAY 0
|
||||
//#define AUDIO_FLAG_RECORD 1
|
||||
|
||||
#define AUDIO_REPLAY_MP_BLOCK_SIZE 4096
|
||||
#define AUDIO_REPLAY_MP_BLOCK_COUNT 4
|
||||
#define AUDIO_RECORD_PIPE_SIZE 2048
|
||||
|
||||
//typedef int (*audio_record_callback)(struct audio_device *audio, void *buffer, int size);
|
||||
|
||||
enum
|
||||
{
|
||||
AUDIO_STREAM_REPLAY = 0,
|
||||
AUDIO_STREAM_RECORD = 1,
|
||||
};
|
||||
|
||||
/* the preferred number and size of audio pipeline buffer for the audio device */
|
||||
struct audio_buf_info
|
||||
{
|
||||
uint8_t *buffer;
|
||||
uint16_t block_size;
|
||||
uint16_t block_count;
|
||||
uint32_t total_size;
|
||||
};
|
||||
|
||||
struct audio_device;
|
||||
struct audio_caps;
|
||||
struct audio_configure;
|
||||
struct audio_ops
|
||||
{
|
||||
int (*getcaps)(struct audio_device *audio, struct audio_caps *caps);
|
||||
int (*configure)(struct audio_device *audio, struct audio_caps *caps);
|
||||
int (*init)(struct audio_device *audio);
|
||||
int (*start)(struct audio_device *audio, int stream);
|
||||
int (*stop)(struct audio_device *audio, int stream);
|
||||
size_t (*transmit)(struct audio_device *audio, const void *writeBuf, void *readBuf, size_t size);
|
||||
/* get page size of codec or private buffer's info */
|
||||
void (*buffer_info)(struct audio_device *audio, struct audio_buf_info *info, int flags);
|
||||
};
|
||||
|
||||
struct audio_configure
|
||||
{
|
||||
uint32_t samplerate;
|
||||
uint16_t channels;
|
||||
uint16_t samplebits;
|
||||
};
|
||||
|
||||
struct audio_caps
|
||||
{
|
||||
int main_type;
|
||||
int sub_type;
|
||||
|
||||
union
|
||||
{
|
||||
uint32_t mask;
|
||||
int value;
|
||||
struct audio_configure config;
|
||||
} udata;
|
||||
};
|
||||
|
||||
struct audio_replay
|
||||
{
|
||||
QueueHandle_t queue;
|
||||
SemaphoreHandle_t lock;
|
||||
QueueHandle_t cmp;
|
||||
struct audio_buf_info buf_info;
|
||||
uint8_t *mempool;
|
||||
uint8_t mpstatus[AUDIO_REPLAY_MP_BLOCK_COUNT];
|
||||
uint8_t *write_data;
|
||||
uint16_t write_index;
|
||||
uint16_t read_index;
|
||||
uint32_t pos;
|
||||
uint8_t event;
|
||||
bool activated;
|
||||
};
|
||||
|
||||
struct audio_record
|
||||
{
|
||||
QueueHandle_t cmp;
|
||||
struct audio_buf_info buf_info;
|
||||
int read_index;
|
||||
int remain_size;
|
||||
uint8_t event;
|
||||
bool activated;
|
||||
int (*receive_cb)(struct audio_device *audio);
|
||||
};
|
||||
|
||||
struct audio_device
|
||||
{
|
||||
struct audio_ops *ops;
|
||||
struct audio_replay *replay;
|
||||
struct audio_record *record;
|
||||
/* device call back */
|
||||
int (*rx_indicate)(struct audio_device *audio, size_t size);
|
||||
int (*tx_complete)(struct audio_device *audio, void *buffer);
|
||||
uint32_t flag;
|
||||
uint32_t id;
|
||||
void *user_data; /**< device private data */
|
||||
};
|
||||
|
||||
int audio_register(struct audio_device *audio);
|
||||
void audio_tx_complete(struct audio_device *audio);
|
||||
int audio_rx_complete(struct audio_device *audio);
|
||||
void audio_rx_done(struct audio_device *audio, uint8_t *pbuf, size_t len);
|
||||
struct audio_device *audio_dev_open(uint32_t oflag);
|
||||
int audio_dev_close(struct audio_device *audio, uint32_t oflag);
|
||||
size_t audio_dev_read(struct audio_device *audio, void *buffer, size_t size);
|
||||
size_t audio_dev_write(struct audio_device *audio, const void *buffer, size_t size);
|
||||
int audio_dev_configure(struct audio_device *audio, struct audio_caps *caps);
|
||||
int audio_dev_register_record_callback(struct audio_device *audio, int (*callback)(struct audio_device *audio));
|
||||
int audio_dev_record_set_param(struct audio_device *audio, uint8_t *buf, int size);
|
||||
int audio_dev_record_start(struct audio_device *audio);
|
||||
int audio_dev_record_stop(struct audio_device *audio);
|
||||
|
||||
|
||||
/* Device Control Commands */
|
||||
#define CODEC_CMD_RESET 0
|
||||
#define CODEC_CMD_SET_VOLUME 1
|
||||
#define CODEC_CMD_GET_VOLUME 2
|
||||
#define CODEC_CMD_SAMPLERATE 3
|
||||
#define CODEC_CMD_EQ 4
|
||||
#define CODEC_CMD_3D 5
|
||||
|
||||
#define CODEC_VOLUME_MAX (63)
|
||||
|
||||
#endif /* __AUDIO_H__ */
|
@ -0,0 +1,591 @@
|
||||
#include "FreeRTOS.h"
|
||||
#include "chip.h"
|
||||
#include "timers.h"
|
||||
#include "board.h"
|
||||
#include "i2s.h"
|
||||
#include "audio.h"
|
||||
|
||||
#define TX_FIFO_SIZE (4096)
|
||||
#define RX_FIFO_SIZE (4096)
|
||||
|
||||
|
||||
#define I2S_DAC_NCO_REG 0x6000006C
|
||||
#define I2S1_DAC_NCO_REG 0x60000148
|
||||
|
||||
|
||||
struct ark_i2s_data amt630hv100_i2s_dac[I2S_NUMS] = {
|
||||
{
|
||||
.base = REGS_I2S_BASE,
|
||||
.nco_reg = I2S_DAC_NCO_REG,
|
||||
.id = I2S_ID0,
|
||||
.clkid = CLK_I2S,
|
||||
.extdata = NULL,
|
||||
.dma_txch = NULL,
|
||||
.dma_rxch = NULL,
|
||||
},
|
||||
{
|
||||
.base = REGS_I2S1_BASE,
|
||||
.nco_reg = I2S1_DAC_NCO_REG,
|
||||
.id = I2S_ID1,
|
||||
.clkid = CLK_I2S1,
|
||||
.extdata = NULL,
|
||||
.dma_txch = NULL,
|
||||
.dma_rxch = NULL,
|
||||
}
|
||||
};
|
||||
|
||||
struct sound_device
|
||||
{
|
||||
struct ark_i2s_data *i2s;
|
||||
struct audio_device audio;
|
||||
struct audio_configure replay_config;
|
||||
struct audio_configure record_config;
|
||||
TimerHandle_t guard_tx_timer;
|
||||
TimerHandle_t guard_rx_timer;
|
||||
uint8_t volume;
|
||||
uint8_t *tx_fifo;
|
||||
uint8_t *rx_fifo;
|
||||
};
|
||||
|
||||
static struct sound_device snd_dev[I2S_NUMS] = {0};
|
||||
|
||||
static int ark_audio_init(struct audio_device *audio)
|
||||
{
|
||||
struct sound_device *sdev;
|
||||
|
||||
configASSERT(audio != NULL);
|
||||
|
||||
sdev = (struct sound_device *)audio->user_data;
|
||||
configASSERT(sdev != NULL);
|
||||
|
||||
ark_i2s_init(sdev->i2s, audio->flag);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ark_audio_start(struct audio_device *audio, int stream)
|
||||
{
|
||||
struct sound_device *sdev;
|
||||
|
||||
configASSERT(audio != NULL);
|
||||
|
||||
sdev = (struct sound_device *)audio->user_data;
|
||||
configASSERT(sdev != NULL);
|
||||
|
||||
if (stream == AUDIO_STREAM_REPLAY)
|
||||
{
|
||||
struct audio_caps caps;
|
||||
caps.main_type = AUDIO_TYPE_OUTPUT;
|
||||
caps.sub_type = AUDIO_DSP_SAMPLERATE;
|
||||
audio->ops->getcaps(audio, &caps);
|
||||
audio->ops->configure(audio, &caps);
|
||||
audio_tx_complete(audio);
|
||||
ark_i2s_startup(sdev->i2s, stream);
|
||||
}
|
||||
else if (stream == AUDIO_STREAM_RECORD)
|
||||
{
|
||||
struct audio_caps caps;
|
||||
caps.main_type = AUDIO_TYPE_INPUT;
|
||||
caps.sub_type = AUDIO_DSP_PARAM;
|
||||
audio->ops->getcaps(audio, &caps);
|
||||
audio->ops->configure(audio, &caps);
|
||||
if(audio_rx_complete(audio) == 0)
|
||||
{
|
||||
if(sdev->guard_rx_timer)
|
||||
xTimerStopFromISR(sdev->guard_rx_timer, 0);
|
||||
ark_i2s_startup(sdev->i2s, stream);
|
||||
if(sdev->guard_rx_timer)
|
||||
{
|
||||
xTimerResetFromISR(sdev->guard_rx_timer, 0);
|
||||
xTimerStartFromISR(sdev->guard_rx_timer, 0);
|
||||
}
|
||||
}
|
||||
else
|
||||
printf("%s() audio_rx_complete failed.\n", __func__);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ark_audio_stop(struct audio_device *audio, int stream)
|
||||
{
|
||||
struct sound_device *sdev;
|
||||
|
||||
configASSERT(audio != NULL);
|
||||
|
||||
sdev = (struct sound_device *)audio->user_data;
|
||||
configASSERT(sdev != NULL);
|
||||
configASSERT(sdev->i2s != NULL);
|
||||
|
||||
ark_i2s_stop(sdev->i2s, stream);
|
||||
|
||||
if(stream == AUDIO_STREAM_REPLAY)
|
||||
dma_stop_channel(sdev->i2s->dma_txch);
|
||||
else if(stream == AUDIO_STREAM_RECORD)
|
||||
dma_stop_channel(sdev->i2s->dma_rxch);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ark_audio_getcaps(struct audio_device *audio, struct audio_caps *caps)
|
||||
{
|
||||
struct sound_device *sdev;
|
||||
int result = 0;
|
||||
|
||||
configASSERT(audio != NULL);
|
||||
|
||||
sdev = (struct sound_device *)audio->user_data;
|
||||
configASSERT(sdev != NULL);
|
||||
|
||||
switch (caps->main_type)
|
||||
{
|
||||
case AUDIO_TYPE_QUERY: /* qurey the types of hw_codec device */
|
||||
{
|
||||
switch (caps->sub_type)
|
||||
{
|
||||
case AUDIO_TYPE_QUERY:
|
||||
caps->udata.mask = AUDIO_TYPE_OUTPUT | AUDIO_TYPE_MIXER;
|
||||
break;
|
||||
|
||||
default:
|
||||
result = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case AUDIO_TYPE_OUTPUT: /* Provide capabilities of OUTPUT unit */
|
||||
{
|
||||
switch (caps->sub_type)
|
||||
{
|
||||
case AUDIO_DSP_PARAM:
|
||||
caps->udata.config.samplerate = sdev->replay_config.samplerate;
|
||||
caps->udata.config.channels = sdev->replay_config.channels;
|
||||
caps->udata.config.samplebits = sdev->replay_config.samplebits;
|
||||
break;
|
||||
|
||||
case AUDIO_DSP_SAMPLERATE:
|
||||
caps->udata.config.samplerate = sdev->replay_config.samplerate;
|
||||
break;
|
||||
|
||||
case AUDIO_DSP_CHANNELS:
|
||||
caps->udata.config.channels = sdev->replay_config.channels;
|
||||
break;
|
||||
|
||||
case AUDIO_DSP_SAMPLEBITS:
|
||||
caps->udata.config.samplebits = sdev->replay_config.samplebits;
|
||||
break;
|
||||
|
||||
default:
|
||||
result = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case AUDIO_TYPE_INPUT:
|
||||
{
|
||||
switch (caps->sub_type)
|
||||
{
|
||||
case AUDIO_DSP_PARAM:
|
||||
caps->udata.config.samplerate = sdev->record_config.samplerate;
|
||||
caps->udata.config.channels = sdev->record_config.channels;
|
||||
caps->udata.config.samplebits = sdev->record_config.samplebits;
|
||||
break;
|
||||
case AUDIO_DSP_SAMPLERATE:
|
||||
caps->udata.config.samplerate = sdev->record_config.samplerate;
|
||||
break;
|
||||
case AUDIO_DSP_CHANNELS:
|
||||
caps->udata.config.channels = sdev->record_config.channels;
|
||||
break;
|
||||
case AUDIO_DSP_SAMPLEBITS:
|
||||
caps->udata.config.samplebits = sdev->record_config.samplebits;
|
||||
break;
|
||||
default:
|
||||
result = -1;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case AUDIO_TYPE_MIXER: /* report the Mixer Units */
|
||||
{
|
||||
switch (caps->sub_type)
|
||||
{
|
||||
case AUDIO_MIXER_QUERY:
|
||||
caps->udata.mask = AUDIO_MIXER_VOLUME;
|
||||
break;
|
||||
|
||||
case AUDIO_MIXER_VOLUME:
|
||||
caps->udata.value = sdev->volume;
|
||||
break;
|
||||
|
||||
default:
|
||||
result = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
result = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int ark_audio_configure(struct audio_device *audio, struct audio_caps *caps)
|
||||
{
|
||||
struct sound_device *sdev;
|
||||
int result = 0;
|
||||
|
||||
configASSERT(audio != NULL);
|
||||
|
||||
sdev = (struct sound_device *)audio->user_data;
|
||||
configASSERT(sdev != NULL);
|
||||
|
||||
switch (caps->main_type)
|
||||
{
|
||||
case AUDIO_TYPE_MIXER:
|
||||
{
|
||||
switch (caps->sub_type)
|
||||
{
|
||||
case AUDIO_MIXER_MUTE:
|
||||
{
|
||||
ark_i2s_set_volume(sdev->i2s, 0, 0);
|
||||
sdev->volume = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
case AUDIO_MIXER_VOLUME:
|
||||
{
|
||||
int volume = caps->udata.value;
|
||||
|
||||
ark_i2s_set_volume(sdev->i2s, volume, volume);
|
||||
sdev->volume = volume;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case AUDIO_TYPE_OUTPUT:
|
||||
{
|
||||
switch (caps->sub_type)
|
||||
{
|
||||
case AUDIO_DSP_PARAM:
|
||||
{
|
||||
struct audio_configure config = caps->udata.config;
|
||||
sdev->replay_config.channels = config.channels;
|
||||
sdev->replay_config.samplebits = config.samplebits;
|
||||
sdev->replay_config.samplerate = config.samplerate;
|
||||
//ark_i2s_set_rate(sdev->i2s, config.samplerate);
|
||||
ark_i2s_set_params(sdev->i2s, AUDIO_STREAM_REPLAY, config.samplerate, config.channels, config.samplebits);
|
||||
break;
|
||||
}
|
||||
|
||||
case AUDIO_DSP_SAMPLERATE:
|
||||
{
|
||||
struct audio_configure config = caps->udata.config;
|
||||
sdev->replay_config.samplerate = config.samplerate;
|
||||
ark_i2s_set_rate(sdev->i2s, AUDIO_STREAM_REPLAY, config.samplerate);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
result = -1;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case AUDIO_TYPE_INPUT:
|
||||
{
|
||||
switch (caps->sub_type)
|
||||
{
|
||||
case AUDIO_DSP_PARAM:
|
||||
{
|
||||
struct audio_configure config = caps->udata.config;
|
||||
sdev->record_config.channels = config.channels;
|
||||
sdev->record_config.samplebits = config.samplebits;
|
||||
sdev->record_config.samplerate = config.samplerate;
|
||||
ark_i2s_set_params(sdev->i2s, AUDIO_STREAM_RECORD, config.samplerate, config.channels, config.samplebits);
|
||||
break;
|
||||
}
|
||||
case AUDIO_DSP_SAMPLERATE:
|
||||
{
|
||||
struct audio_configure config = caps->udata.config;
|
||||
sdev->record_config.samplerate = config.samplerate;
|
||||
ark_i2s_set_rate(sdev->i2s, AUDIO_STREAM_RECORD, config.samplerate);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
result = -1;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void ark_i2s_dma_tx_callback(void *param, unsigned int mask)
|
||||
{
|
||||
struct sound_device *sdev = (struct sound_device *)param;
|
||||
|
||||
configASSERT(sdev != NULL);
|
||||
|
||||
if(sdev->guard_tx_timer)
|
||||
xTimerStopFromISR(sdev->guard_tx_timer, 0);
|
||||
audio_tx_complete(&sdev->audio);
|
||||
}
|
||||
|
||||
static void ark_i2s_dma_rx_callback(void *param, unsigned int mask)
|
||||
{
|
||||
struct sound_device *sdev = (struct sound_device *)param;
|
||||
|
||||
configASSERT(sdev != NULL);
|
||||
|
||||
if(sdev->guard_rx_timer)
|
||||
xTimerStopFromISR(sdev->guard_rx_timer, 0);
|
||||
|
||||
audio_rx_complete(&sdev->audio);
|
||||
}
|
||||
|
||||
#if 1
|
||||
static void ark_i2s_dma_tx_timeout_callback(TimerHandle_t timer)
|
||||
{
|
||||
struct sound_device *sdev = (struct sound_device *)pvTimerGetTimerID(timer);
|
||||
|
||||
configASSERT(sdev != NULL);
|
||||
configASSERT(sdev->i2s != NULL);
|
||||
|
||||
if(sdev->audio.flag & AUDIO_FLAG_REPLAY == AUDIO_FLAG_REPLAY) {
|
||||
printf("i2s%d dma tx timeout.\n", sdev->i2s->id);
|
||||
if(sdev->guard_tx_timer)
|
||||
xTimerStopFromISR(sdev->guard_tx_timer, 0);
|
||||
audio_tx_complete(&sdev->audio);
|
||||
}
|
||||
}
|
||||
|
||||
static void ark_i2s_dma_rx_timeout_callback(TimerHandle_t timer)
|
||||
{
|
||||
struct sound_device *sdev = (struct sound_device *)pvTimerGetTimerID(timer);
|
||||
|
||||
configASSERT(sdev != NULL);
|
||||
configASSERT(sdev->i2s != NULL);
|
||||
|
||||
if(sdev->audio.flag & AUDIO_FLAG_RECORD == AUDIO_FLAG_RECORD) {
|
||||
printf("i2s:%d dma rx timeout.\n", sdev->i2s->id);
|
||||
if(sdev->guard_rx_timer)
|
||||
xTimerStopFromISR(sdev->guard_rx_timer, 0);
|
||||
audio_rx_complete(&sdev->audio);
|
||||
}
|
||||
}
|
||||
#else
|
||||
static void ark_i2s_dma_timeout_callback(TimerHandle_t timer)
|
||||
{
|
||||
struct sound_device *sdev = (struct sound_device *)pvTimerGetTimerID(timer);
|
||||
|
||||
configASSERT(sdev != NULL);
|
||||
|
||||
if((sdev->audio.flag & AUDIO_FLAG_REPLAY) == AUDIO_FLAG_REPLAY) {
|
||||
printf("i2s dma tx timeout.\n");
|
||||
if(sdev->guard_tx_timer)
|
||||
xTimerStopFromISR(sdev->guard_tx_timer, 0);
|
||||
audio_tx_complete(&sdev->audio);
|
||||
} else if((sdev->audio.flag & AUDIO_FLAG_RECORD) == AUDIO_FLAG_RECORD) {
|
||||
printf("i2s dma rx timeout.\n");
|
||||
if(sdev->guard_rx_timer)
|
||||
xTimerStopFromISR(sdev->guard_rx_timer, 0);
|
||||
ark_i2s_restart_audio(sdev->i2s);
|
||||
audio_rx_complete(&sdev->audio);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static size_t ark_audio_transmit(struct audio_device *audio, const void *writeBuf, void *readBuf, size_t size)
|
||||
{
|
||||
struct ark_i2s_data *i2s;
|
||||
struct dma_config cfg = {0};
|
||||
struct sound_device *sdev = (struct sound_device *)audio->user_data;
|
||||
int ret;
|
||||
|
||||
configASSERT(sdev != NULL);
|
||||
|
||||
i2s = sdev->i2s;
|
||||
|
||||
cfg.dst_maxburst = 16;
|
||||
cfg.src_maxburst = 16;
|
||||
cfg.transfer_size = size;
|
||||
if(writeBuf)
|
||||
{
|
||||
cfg.dst_addr_width = DMA_BUSWIDTH_4_BYTES;
|
||||
cfg.src_addr_width = DMA_BUSWIDTH_4_BYTES;
|
||||
cfg.src_addr = (dma_addr_t)writeBuf;
|
||||
cfg.dst_addr = i2s->base + I2S_SADR;
|
||||
cfg.direction = DMA_MEM_TO_DEV;
|
||||
if(i2s->id == I2S_ID1)
|
||||
cfg.dst_id = I2S1_TX;
|
||||
else /*if(i2s->id == I2S_ID0)*/
|
||||
cfg.dst_id = I2S_TX;
|
||||
|
||||
ret = dma_config_channel(i2s->dma_txch, &cfg);
|
||||
if (ret) {
|
||||
printf("%s, dma_config_channel tx failed.\n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
dma_register_complete_callback(i2s->dma_txch, ark_i2s_dma_tx_callback, sdev);
|
||||
|
||||
/* clean cache before write */
|
||||
CP15_clean_dcache_for_dma((u32)writeBuf, (u32)writeBuf + size);
|
||||
dma_start_channel(i2s->dma_txch);
|
||||
if(sdev->guard_tx_timer)
|
||||
{
|
||||
xTimerResetFromISR(sdev->guard_tx_timer, 0);
|
||||
xTimerStartFromISR(sdev->guard_tx_timer, 0);
|
||||
}
|
||||
}
|
||||
else if(readBuf)
|
||||
{
|
||||
if(i2s->cfg[AUDIO_STREAM_RECORD].channels == 2) {
|
||||
cfg.dst_addr_width = DMA_BUSWIDTH_4_BYTES;
|
||||
cfg.src_addr_width = DMA_BUSWIDTH_4_BYTES;
|
||||
} else {
|
||||
cfg.dst_addr_width = DMA_BUSWIDTH_2_BYTES;
|
||||
cfg.src_addr_width = DMA_BUSWIDTH_2_BYTES;
|
||||
}
|
||||
cfg.src_addr = i2s->base + I2S_SADR;
|
||||
cfg.dst_addr = (dma_addr_t)readBuf;
|
||||
cfg.direction = DMA_DEV_TO_MEM;
|
||||
if(i2s->id == I2S_ID1)
|
||||
cfg.src_id = I2S1_RX;
|
||||
else /*if(i2s->id == I2S_ID0)*/
|
||||
cfg.src_id = I2S_RX;
|
||||
|
||||
ret = dma_config_channel(i2s->dma_rxch, &cfg);
|
||||
if (ret) {
|
||||
printf("%s, dma_config_channel rx failed.\n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
dma_register_complete_callback(i2s->dma_rxch, ark_i2s_dma_rx_callback, sdev);
|
||||
|
||||
/* clean cache before read */
|
||||
CP15_flush_dcache_for_dma((u32)readBuf, (u32)readBuf + size);
|
||||
|
||||
dma_start_channel(i2s->dma_rxch);
|
||||
if(sdev->guard_rx_timer)
|
||||
{
|
||||
xTimerResetFromISR(sdev->guard_rx_timer, 0);
|
||||
xTimerStartFromISR(sdev->guard_rx_timer, 0);
|
||||
}
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
static void ark_audio_buffer_info(struct audio_device *audio, struct audio_buf_info *info, int flags)
|
||||
{
|
||||
configASSERT(audio != NULL);
|
||||
struct sound_device *sdev = (struct sound_device *)audio->user_data;
|
||||
configASSERT(sdev != NULL);
|
||||
/**
|
||||
* TX_FIFO
|
||||
* +----------------+----------------+
|
||||
* | block1 | block2 |
|
||||
* +----------------+----------------+
|
||||
* \ block_size /
|
||||
*/
|
||||
if(flags == AUDIO_FLAG_REPLAY)
|
||||
{
|
||||
info->buffer = sdev->tx_fifo;
|
||||
info->total_size = TX_FIFO_SIZE;
|
||||
info->block_size = TX_FIFO_SIZE / 2;
|
||||
info->block_count = 2;
|
||||
}
|
||||
else if(flags == AUDIO_FLAG_RECORD)
|
||||
{
|
||||
info->buffer = sdev->rx_fifo;
|
||||
info->total_size = RX_FIFO_SIZE;
|
||||
info->block_size = RX_FIFO_SIZE/2;
|
||||
info->block_count = 2;
|
||||
}
|
||||
}
|
||||
|
||||
static struct audio_ops audio_ops =
|
||||
{
|
||||
.getcaps = ark_audio_getcaps,
|
||||
.configure = ark_audio_configure,
|
||||
.init = ark_audio_init,
|
||||
.start = ark_audio_start,
|
||||
.stop = ark_audio_stop,
|
||||
.transmit = ark_audio_transmit,
|
||||
.buffer_info = ark_audio_buffer_info,
|
||||
};
|
||||
|
||||
int sound_init(void)
|
||||
{
|
||||
uint8_t *tx_fifo = NULL;
|
||||
struct sound_device *sdev;
|
||||
int flags;
|
||||
int i;
|
||||
|
||||
for(i=0; i<I2S_NUMS; i++)
|
||||
{
|
||||
flags = 0;
|
||||
|
||||
#ifdef AUDIO_REPLAY_I2S
|
||||
if(AUDIO_REPLAY_I2S == i) {
|
||||
flags |= AUDIO_FLAG_REPLAY;
|
||||
}
|
||||
#endif
|
||||
#ifdef AUDIO_RECORD_I2S
|
||||
if(AUDIO_RECORD_I2S == i) {
|
||||
flags |= AUDIO_FLAG_RECORD;
|
||||
}
|
||||
#endif
|
||||
if(!(flags & AUDIO_FLAG_REPLAY_RECORD))
|
||||
continue;
|
||||
|
||||
sdev = &snd_dev[i];
|
||||
sdev->i2s = &amt630hv100_i2s_dac[i];
|
||||
|
||||
/* init default configuration */
|
||||
if((flags & AUDIO_FLAG_REPLAY) == AUDIO_FLAG_REPLAY)
|
||||
{
|
||||
tx_fifo = pvPortMalloc(TX_FIFO_SIZE);
|
||||
if (!tx_fifo)
|
||||
{
|
||||
printf("%s, pvPortMalloc failed\n", __func__);
|
||||
return -ENOMEM;
|
||||
}
|
||||
sdev->tx_fifo = tx_fifo;
|
||||
sdev->replay_config.samplerate = 44100; //will be reset when open() audio.
|
||||
sdev->replay_config.channels = 2;
|
||||
sdev->replay_config.samplebits = 16;
|
||||
sdev->volume = 100;
|
||||
sdev->guard_tx_timer = xTimerCreate("Replay Timer", pdMS_TO_TICKS(200), pdFALSE,
|
||||
//NULL, ark_i2s_dma_timeout_callback);
|
||||
sdev, ark_i2s_dma_tx_timeout_callback);
|
||||
}
|
||||
if((flags & AUDIO_FLAG_RECORD) == AUDIO_FLAG_RECORD)
|
||||
{
|
||||
sdev->rx_fifo = NULL;
|
||||
sdev->record_config.samplerate = 16000; //will be reset when open() audio.
|
||||
sdev->record_config.channels = 2;
|
||||
sdev->record_config.samplebits = 16;
|
||||
sdev->volume = 100;
|
||||
sdev->guard_rx_timer = xTimerCreate("Record Timer", pdMS_TO_TICKS(200), pdFALSE,
|
||||
sdev, ark_i2s_dma_rx_timeout_callback);
|
||||
}
|
||||
|
||||
sdev->audio.ops = &audio_ops;
|
||||
sdev->audio.user_data = (void *)sdev;
|
||||
sdev->audio.flag = flags;
|
||||
sdev->audio.id = i;
|
||||
audio_register(&sdev->audio);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user