pinebuds/services/audioflinger/audioflinger.c

3141 lines
84 KiB
C
Raw Normal View History

2022-08-15 04:20:27 -05:00
/***************************************************************************
*
* Copyright 2015-2019 BES.
* All rights reserved. All unpublished rights reserved.
*
* No part of this work may be used or reproduced in any form or by any
* means, or stored in a database or retrieval system, without prior written
* permission of BES.
*
* Use of this work is governed by a license granted by BES.
* This work contains confidential and proprietary information of
* BES. which is protected by copyright, trade secret,
* trademark and other intellectual property rights.
*
****************************************************************************/
#include "audioflinger.h"
#include "codec_int.h"
#include "codec_tlv32aic32.h"
#include "hal_btpcm.h"
#include "hal_codec.h"
2022-08-15 04:20:27 -05:00
#include "hal_dma.h"
#include "hal_i2s.h"
#include "hal_spdif.h"
#include "hal_tdm.h"
2022-08-15 04:20:27 -05:00
#include "analog.h"
2022-08-15 04:20:27 -05:00
#include "hal_cmu.h"
#include "hal_timer.h"
#include "hal_trace.h"
2022-08-15 04:20:27 -05:00
#include "pmu.h"
#include "string.h"
2022-08-15 04:20:27 -05:00
#include "tgt_hardware.h"
#include "cmsis.h"
#ifdef __RAND_FROM_MIC__
#include "randfrommic.h"
#endif
#ifdef TWS_PROMPT_SYNC
extern void tws_playback_ticks_check_for_mix_prompt(void);
#endif
#ifdef RTOS
#include "cmsis_os.h"
#else
#include "hal_sleep.h"
#endif
#ifdef AUDIO_OUTPUT_SW_LIMITER
#include "floatlimiter.h"
#endif
#define AF_TRACE_DEBUG() // TRACE(2,"%s:%d\n", __func__, __LINE__)
2022-08-15 04:20:27 -05:00
// #define AF_STREAM_ID_0_PLAYBACK_FADEOUT
2022-08-15 04:20:27 -05:00
// #define CORRECT_SAMPLE_VALUE
2022-08-15 04:20:27 -05:00
#ifdef AUDIO_OUTPUT_DC_CALIB
#ifdef CHIP_BEST1000
#define AUDIO_OUTPUT_DC_CALIB_SW
#define AUDIO_OUTPUT_PA_ON_FADE_IN
#if !(defined(__TWS__) || defined(IBRT))
#define AUDIO_OUTPUT_PA_OFF_FADE_OUT
#endif
#elif (defined(__TWS__) || defined(IBRT)) && !defined(ANC_APP)
#define AUDIO_OUTPUT_PA_ON_FADE_IN
#endif // !CHIP_BEST1000 && __TWS__
#elif defined(AUDIO_OUTPUT_DC_CALIB_ANA) && \
(defined(__TWS__) || defined(IBRT)) && !defined(ANC_APP)
2022-08-15 04:20:27 -05:00
#define AUDIO_OUTPUT_PA_ON_FADE_IN
#endif // !AUDIO_OUTPUT_DC_CALIB && AUDIO_OUTPUT_DC_CALIB_ANA && __TWS__
#define AF_FADE_OUT_SIGNAL_ID 15
#define AF_FADE_IN_SIGNAL_ID 14
2022-08-15 04:20:27 -05:00
/* config params */
#define AF_CODEC_INST HAL_CODEC_ID_0
#define AF_SPDIF_INST HAL_SPDIF_ID_0
#define AF_BTPCM_INST HAL_BTPCM_ID_0
#define AF_CPU_WAKE_USER HAL_CPU_WAKE_LOCK_USER_AUDIOFLINGER
2022-08-15 04:20:27 -05:00
#define AF_CODEC_RIGHT_CHAN_ATTN 0.968277856 // -0.28 dB
2022-08-15 04:20:27 -05:00
#define AF_CODEC_VOL_UPDATE_STEP 0.00002
2022-08-15 04:20:27 -05:00
#define AF_CODEC_DC_MAX_SCALE 32767
2022-08-15 04:20:27 -05:00
#define AF_CODEC_DC_STABLE_INTERVAL MS_TO_TICKS(4)
2022-08-15 04:20:27 -05:00
#ifdef AUDIO_OUTPUT_PA_OFF_FADE_OUT
#define AF_CODEC_PA_RESTART_INTERVAL MS_TO_TICKS(160)
2022-08-15 04:20:27 -05:00
#else
#define AF_CODEC_PA_RESTART_INTERVAL MS_TO_TICKS(8)
2022-08-15 04:20:27 -05:00
#endif
// The following might have been defined in tgt_hardware.h
#ifndef AF_CODEC_FADE_IN_MS
#define AF_CODEC_FADE_IN_MS 20
2022-08-15 04:20:27 -05:00
#endif
#ifndef AF_CODEC_FADE_OUT_MS
#define AF_CODEC_FADE_OUT_MS 20
2022-08-15 04:20:27 -05:00
#endif
#define AF_CODEC_FADE_MIN_SAMPLE_CNT 200
2022-08-15 04:20:27 -05:00
#define PP_PINGPANG(v) (v == PP_PING ? PP_PANG : PP_PING)
2022-08-15 04:20:27 -05:00
#ifdef AUDIO_ANC_FB_ADJ_MC
#define AF_STACK_SIZE (1024 * 10)
2022-08-15 04:20:27 -05:00
#else
#ifndef AF_STACK_SIZE
#define AF_STACK_SIZE (1024 * 8)
2022-08-15 04:20:27 -05:00
#endif
#endif
#if defined(AUDIO_ANC_FB_MC) && defined(ANC_APP)
#define DYNAMIC_AUDIO_BUFFER_COUNT
#endif
#ifdef DYNAMIC_AUDIO_BUFFER_COUNT
#define MAX_AUDIO_BUFFER_COUNT 16
#define MIN_AUDIO_BUFFER_COUNT 2
#define AUDIO_BUFFER_COUNT (role->dma_desc_cnt)
2022-08-15 04:20:27 -05:00
#else
#define MAX_AUDIO_BUFFER_COUNT 4
#define AUDIO_BUFFER_COUNT MAX_AUDIO_BUFFER_COUNT
2022-08-15 04:20:27 -05:00
#if (AUDIO_BUFFER_COUNT & 0x1)
#error "AUDIO_BUFFER_COUNT must be an even number"
#endif
#endif
/* internal use */
enum AF_BOOL_T { AF_FALSE = 0, AF_TRUE = 1 };
2022-08-15 04:20:27 -05:00
enum AF_RESULT_T { AF_RES_SUCCESS = 0, AF_RES_FAILD = 1 };
2022-08-15 04:20:27 -05:00
enum AF_DAC_PA_STATE_T {
AF_DAC_PA_NULL,
AF_DAC_PA_ON_TRIGGER,
AF_DAC_PA_ON_SOFT_START,
AF_DAC_PA_OFF_TRIGGER,
AF_DAC_PA_OFF_SOFT_END,
AF_DAC_PA_OFF_SOFT_END_DONE,
AF_DAC_PA_OFF_DC_START,
2022-08-15 04:20:27 -05:00
};
// status machine
enum AF_STATUS_T {
AF_STATUS_NULL = 0x00,
AF_STATUS_OPEN_CLOSE = 0x01,
AF_STATUS_STREAM_OPEN_CLOSE = 0x02,
AF_STATUS_STREAM_START_STOP = 0x04,
AF_STATUS_STREAM_PAUSE_RESTART = 0x08,
AF_STATUS_MASK = 0x0F,
2022-08-15 04:20:27 -05:00
};
struct af_stream_ctl_t {
enum AF_PP_T pp_index; // pingpong operate
uint8_t pp_cnt; // use to count the lost signals
uint8_t status; // status machine
enum AUD_STREAM_USE_DEVICE_T use_device;
2022-08-15 04:20:27 -05:00
};
struct af_stream_cfg_t {
// used inside
struct af_stream_ctl_t ctl;
2022-08-15 04:20:27 -05:00
// dma buf parameters, RAM can be alloced in different way
uint8_t *dma_buf_ptr;
uint32_t dma_buf_size;
2022-08-15 04:20:27 -05:00
// store stream cfg parameters
struct AF_STREAM_CONFIG_T cfg;
2022-08-15 04:20:27 -05:00
// dma cfg parameters
2022-08-15 04:20:27 -05:00
#ifdef DYNAMIC_AUDIO_BUFFER_COUNT
uint8_t dma_desc_cnt;
2022-08-15 04:20:27 -05:00
#endif
struct HAL_DMA_DESC_T dma_desc[MAX_AUDIO_BUFFER_COUNT];
struct HAL_DMA_CH_CFG_T dma_cfg;
2022-08-15 04:20:27 -05:00
// callback function
AF_STREAM_HANDLER_T handler;
2022-08-15 04:20:27 -05:00
};
static struct af_stream_cfg_t af_stream[AUD_STREAM_ID_NUM][AUD_STREAM_NUM];
static uint8_t af_sig_lost_cnt[AUD_STREAM_ID_NUM][AUD_STREAM_NUM];
static AF_CODEC_PLAYBACK_POST_HANDLER_T codec_play_post_hdlr;
#ifdef AUDIO_OUTPUT_SW_GAIN
volatile static float saved_output_coef;
#ifdef AUDIO_OUTPUT_SW_LIMITER
static FloatLimiterPtr FloatLimiterP;
#else
typedef struct {
float coefs_b[3];
float coefs_a[3];
float history_x[2];
float history_y[2];
} SW_GAIN_IIR_T;
SW_GAIN_IIR_T sw_gain_iir = {
.coefs_b = {0.00000042780818597736f, 0.00000085561637195472f,
0.00000042780818597736f},
.coefs_a = {1.00000000000000000000f, -1.99738371810092970000f,
0.99738542933367369000f},
.history_x = {0, 0},
.history_y = {0, 0},
};
2022-08-15 04:20:27 -05:00
#endif
#endif
#ifdef AUDIO_OUTPUT_DC_CALIB
static bool dac_dc_valid;
#ifdef AUDIO_OUTPUT_DC_CALIB_SW
static int16_t dac_dc[2];
#endif
#endif
#if defined(AUDIO_OUTPUT_PA_ON_FADE_IN) || defined(AUDIO_OUTPUT_PA_OFF_FADE_OUT)
static volatile enum AF_DAC_PA_STATE_T dac_pa_state;
static uint32_t dac_dc_start_time;
static uint32_t dac_pa_stop_time;
#endif
#if defined(AUDIO_OUTPUT_PA_OFF_FADE_OUT) && defined(RTOS)
static osThreadId fade_thread_id;
#ifdef AF_STREAM_ID_0_PLAYBACK_FADEOUT
#error "Cannot enable 2 kinds of fade out codes at the same time"
#endif
#endif
#ifdef RTOS
#ifdef AF_STREAM_ID_0_PLAYBACK_FADEOUT
struct af_stream_fade_out_t {
bool stop_on_process;
uint8_t stop_process_cnt;
osThreadId stop_request_tid;
uint32_t need_fadeout_len;
uint32_t need_fadeout_len_processed;
2022-08-15 04:20:27 -05:00
};
static struct af_stream_fade_out_t af_stream_fade_out = {
.stop_on_process = false,
.stop_process_cnt = 0,
.stop_request_tid = NULL,
.need_fadeout_len = 0,
.need_fadeout_len_processed = 0,
2022-08-15 04:20:27 -05:00
};
#endif
static osThreadId af_thread_tid;
static void af_thread(void const *argument);
osThreadDef(af_thread, osPriorityAboveNormal, 1, AF_STACK_SIZE,
"audio_flinger");
2022-08-15 04:20:27 -05:00
static int af_default_priority;
osMutexId audioflinger_mutex_id = NULL;
osMutexDef(audioflinger_mutex);
static AF_IRQ_NOTIFICATION_T irq_notif;
#else // !RTOS
static volatile uint32_t af_flag_lock;
#endif // RTOS
#ifdef CODEC_DSD
static bool af_dsd_enabled;
#endif
void af_lock_thread(void) {
void *POSSIBLY_UNUSED lr = __builtin_return_address(0);
2022-08-15 04:20:27 -05:00
#ifdef RTOS
osMutexWait(audioflinger_mutex_id, osWaitForever);
2022-08-15 04:20:27 -05:00
#else
static void *POSSIBLY_UNUSED locked_lr;
ASSERT(af_flag_lock == 0, "audioflinger has been locked by %p. LR=%p",
(void *)locked_lr, (void *)lr);
af_flag_lock = 1;
locked_lr = lr;
2022-08-15 04:20:27 -05:00
#endif
}
void af_unlock_thread(void) {
void *POSSIBLY_UNUSED lr = __builtin_return_address(0);
2022-08-15 04:20:27 -05:00
#ifdef RTOS
osMutexRelease(audioflinger_mutex_id);
2022-08-15 04:20:27 -05:00
#else
static void *POSSIBLY_UNUSED unlocked_lr;
ASSERT(af_flag_lock == 1,
"audioflinger not locked before (lastly unlocked by %p). LR=%p",
(void *)unlocked_lr, (void *)lr);
af_flag_lock = 0;
unlocked_lr = lr;
2022-08-15 04:20:27 -05:00
#endif
}
#if defined(RTOS) && defined(AF_STREAM_ID_0_PLAYBACK_FADEOUT)
int af_stream_fadeout_start(uint32_t sample) {
TRACE(1, "fadein_config sample:%d", sample);
af_stream_fade_out.need_fadeout_len = sample;
af_stream_fade_out.need_fadeout_len_processed = sample;
return 0;
}
int af_stream_fadeout_stop(void) {
af_stream_fade_out.stop_process_cnt = 0;
af_stream_fade_out.stop_on_process = false;
return 0;
}
uint32_t af_stream_fadeout(int16_t *buf, uint32_t len,
enum AUD_CHANNEL_NUM_T num) {
uint32_t i;
uint32_t j = 0;
uint32_t start;
uint32_t end;
start = af_stream_fade_out.need_fadeout_len_processed;
end = af_stream_fade_out.need_fadeout_len_processed > len
? af_stream_fade_out.need_fadeout_len_processed - len
: 0;
if (start <= end) {
// TRACE(0,"skip fadeout");
memset(buf, 0, len * 2);
2022-08-15 04:20:27 -05:00
return len;
}
// TRACE(3,"fadeout l:%d start:%d end:%d", len, start, end);
// DUMP16("%05d ", buf, 10);
// DUMP16("%05d ", buf+len-10, 10);
if (num == AUD_CHANNEL_NUM_1) {
for (i = start; i > end; i--) {
*(buf + j) = *(buf + j) * i / af_stream_fade_out.need_fadeout_len;
j++;
}
} else if (num == AUD_CHANNEL_NUM_2) {
for (i = start; i > end; i -= 2) {
*(buf + j) = *(buf + j) * i / af_stream_fade_out.need_fadeout_len;
j++;
*(buf + j) = *(buf + j) * i / af_stream_fade_out.need_fadeout_len;
j++;
}
} else if (num == AUD_CHANNEL_NUM_4) {
for (i = start; i > end; i -= 4) {
*(buf + j) = *(buf + j) * i / af_stream_fade_out.need_fadeout_len;
j++;
*(buf + j) = *(buf + j) * i / af_stream_fade_out.need_fadeout_len;
j++;
*(buf + j) = *(buf + j) * i / af_stream_fade_out.need_fadeout_len;
j++;
*(buf + j) = *(buf + j) * i / af_stream_fade_out.need_fadeout_len;
j++;
}
} else if (num == AUD_CHANNEL_NUM_8) {
for (i = start; i > end; i -= 8) {
*(buf + j) = *(buf + j) * i / af_stream_fade_out.need_fadeout_len;
j++;
*(buf + j) = *(buf + j) * i / af_stream_fade_out.need_fadeout_len;
j++;
*(buf + j) = *(buf + j) * i / af_stream_fade_out.need_fadeout_len;
j++;
*(buf + j) = *(buf + j) * i / af_stream_fade_out.need_fadeout_len;
j++;
*(buf + j) = *(buf + j) * i / af_stream_fade_out.need_fadeout_len;
j++;
*(buf + j) = *(buf + j) * i / af_stream_fade_out.need_fadeout_len;
j++;
*(buf + j) = *(buf + j) * i / af_stream_fade_out.need_fadeout_len;
j++;
*(buf + j) = *(buf + j) * i / af_stream_fade_out.need_fadeout_len;
j++;
}
}
af_stream_fade_out.need_fadeout_len_processed -= j;
// TRACE(3,"out i:%d process:%d x:%d", i,
// af_stream_fade_out.need_fadeout_len_processed,
// end+((start-end)/AUD_CHANNEL_NUM_2)); DUMP16("%05d ", buf, 10);
// DUMP16("%05d ", buf+len-10, 10);
return len;
}
void af_stream_stop_wait_finish() {
af_stream_fade_out.stop_on_process = true;
af_stream_fade_out.stop_request_tid = osThreadGetId();
osSignalClear(af_stream_fade_out.stop_request_tid,
(1 << AF_FADE_OUT_SIGNAL_ID));
af_unlock_thread();
osSignalWait((1 << AF_FADE_OUT_SIGNAL_ID), 300);
af_lock_thread();
}
void af_stream_stop_process(struct af_stream_cfg_t *af_cfg, uint8_t *buf,
uint32_t len) {
af_lock_thread();
if (af_stream_fade_out.stop_on_process) {
// TRACE(5,"%s num:%d size:%d len:%d cnt:%d", __func__,
// af_cfg->cfg.channel_num, af_cfg->cfg.data_size, len,
// af_stream_fade_out.stop_process_cnt);
af_stream_fadeout((int16_t *)buf, len / 2, af_cfg->cfg.channel_num);
// TRACE(3,"process ret:%d %d %d", *(int16_t *)(buf+len-2-2-2),
// *(int16_t *)(buf+len-2-2), *(int16_t *)(buf+len-2));
if (af_stream_fade_out.stop_process_cnt++ > 3) {
TRACE(0, "stop_process end");
osSignalSet(af_stream_fade_out.stop_request_tid,
(1 << AF_FADE_OUT_SIGNAL_ID));
}
}
af_unlock_thread();
2022-08-15 04:20:27 -05:00
}
#endif // RTOS && AF_STREAM_ID_0_PLAYBACK_FADEOUT
// used by dma irq and af_thread
static inline struct af_stream_cfg_t *
af_get_stream_role(enum AUD_STREAM_ID_T id, enum AUD_STREAM_T stream) {
ASSERT(id < AUD_STREAM_ID_NUM, "[%s] Bad id=%d", __func__, id);
ASSERT(stream < AUD_STREAM_NUM, "[%s] Bad stream=%d", __func__, stream);
2022-08-15 04:20:27 -05:00
return &af_stream[id][stream];
2022-08-15 04:20:27 -05:00
}
static void af_set_status(enum AUD_STREAM_ID_T id, enum AUD_STREAM_T stream,
enum AF_STATUS_T status) {
struct af_stream_cfg_t *role = NULL;
2022-08-15 04:20:27 -05:00
role = af_get_stream_role(id, stream);
2022-08-15 04:20:27 -05:00
role->ctl.status |= status;
2022-08-15 04:20:27 -05:00
}
static void af_clear_status(enum AUD_STREAM_ID_T id, enum AUD_STREAM_T stream,
enum AF_STATUS_T status) {
struct af_stream_cfg_t *role = NULL;
2022-08-15 04:20:27 -05:00
role = af_get_stream_role(id, stream);
2022-08-15 04:20:27 -05:00
role->ctl.status &= ~status;
2022-08-15 04:20:27 -05:00
}
// get current stream config parameters
uint32_t af_stream_get_cfg(enum AUD_STREAM_ID_T id, enum AUD_STREAM_T stream,
struct AF_STREAM_CONFIG_T **cfg, bool needlock) {
AF_TRACE_DEBUG();
2022-08-15 04:20:27 -05:00
struct af_stream_cfg_t *role;
enum AF_RESULT_T ret;
2022-08-15 04:20:27 -05:00
if (needlock) {
af_lock_thread();
}
role = af_get_stream_role(id, stream);
// check stream is open
if (role->ctl.status & AF_STATUS_STREAM_OPEN_CLOSE) {
*cfg = &(role->cfg);
ret = AF_RES_SUCCESS;
} else {
ret = AF_RES_FAILD;
}
if (needlock) {
af_unlock_thread();
}
return ret;
2022-08-15 04:20:27 -05:00
}
#if 0
static void af_dump_cfg()
{
struct af_stream_cfg_t *role = NULL;
TRACE(0,"dump cfg.........start");
//initial parameter
for(uint8_t id=0; id< AUD_STREAM_ID_NUM; id++)
{
for(uint8_t stream=0; stream < AUD_STREAM_NUM; stream++)
{
role = af_get_stream_role((enum AUD_STREAM_ID_T)id, (enum AUD_STREAM_T)stream);
TRACE(2,"id = %d, stream = %d:", id, stream);
TRACE(1,"ctl.use_device = %d", role->ctl.use_device);
TRACE(1,"cfg.device = %d", role->cfg.device);
TRACE(1,"dma_cfg.ch = %d", role->dma_cfg.ch);
}
}
TRACE(0,"dump cfg.........end");
}
#endif
#ifdef AUDIO_OUTPUT_SW_GAIN
static void af_codec_output_gain_changed(float coef) {
saved_output_coef = coef;
2022-08-15 04:20:27 -05:00
}
#ifndef AUDIO_OUTPUT_SW_LIMITER
static float af_codec_sw_gain_filter(float gain_in) {
float gain_out = saved_output_coef * sw_gain_iir.coefs_b[0] +
sw_gain_iir.history_x[0] * sw_gain_iir.coefs_b[1] +
sw_gain_iir.history_x[1] * sw_gain_iir.coefs_b[2] -
sw_gain_iir.history_y[0] * sw_gain_iir.coefs_a[1] -
sw_gain_iir.history_y[1] * sw_gain_iir.coefs_a[2];
sw_gain_iir.history_y[1] = sw_gain_iir.history_y[0];
sw_gain_iir.history_y[0] = gain_out;
sw_gain_iir.history_x[1] = sw_gain_iir.history_x[0];
sw_gain_iir.history_x[0] = saved_output_coef;
return gain_out;
}
static void af_codec_sw_gain_process(uint8_t *buf, uint32_t size,
enum AUD_BITS_T bits,
enum AUD_CHANNEL_NUM_T chans) {
uint32_t i, pcm_len;
int32_t pcm_out;
float output_gain = 1.0f;
// TRACE(1,"af_codec_sw_gain_process:%d",(int32_t)(saved_output_coef*1000));
if (chans == AUD_CHANNEL_NUM_1) {
if (bits <= AUD_BITS_16) {
pcm_len = size / sizeof(int16_t);
int16_t *pcm_buf = (int16_t *)buf;
for (i = 0; i < pcm_len; i++) {
output_gain = af_codec_sw_gain_filter(saved_output_coef);
pcm_out = (int32_t)(pcm_buf[i] * output_gain);
pcm_buf[i] = __SSAT(pcm_out, 16);
}
} else {
pcm_len = size / sizeof(int32_t);
int32_t *pcm_buf = (int32_t *)buf;
for (i = 0; i < pcm_len; i++) {
output_gain = af_codec_sw_gain_filter(saved_output_coef);
2022-08-15 04:20:27 -05:00
pcm_out = (int32_t)(pcm_buf[i] * output_gain);
pcm_buf[i] = __SSAT(pcm_out, 24);
}
2022-08-15 04:20:27 -05:00
}
} else if (chans == AUD_CHANNEL_NUM_2) {
if (bits <= AUD_BITS_16) {
pcm_len = size / sizeof(int16_t);
int16_t *pcm_buf = (int16_t *)buf;
for (i = 0; i < pcm_len; i = i + 2) {
output_gain = af_codec_sw_gain_filter(saved_output_coef);
pcm_out = (int32_t)(pcm_buf[i] * output_gain);
pcm_buf[i] = __SSAT(pcm_out, 16);
pcm_out = (int32_t)(pcm_buf[i + 1] * output_gain);
pcm_buf[i + 1] = __SSAT(pcm_out, 16);
}
} else {
pcm_len = size / sizeof(int32_t);
int32_t *pcm_buf = (int32_t *)buf;
for (i = 0; i < pcm_len; i = i + 2) {
output_gain = af_codec_sw_gain_filter(saved_output_coef);
pcm_out = (int32_t)(pcm_buf[i] * output_gain);
pcm_buf[i] = __SSAT(pcm_out, 24);
pcm_out = (int32_t)(pcm_buf[i + 1] * output_gain);
pcm_buf[i + 1] = __SSAT(pcm_out, 24);
}
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
}
#endif
#endif // AUDIO_OUTPUT_SW_GAIN
static uint32_t POSSIBLY_UNUSED af_codec_get_sample_count(
uint32_t size, enum AUD_BITS_T bits, enum AUD_CHANNEL_NUM_T chans) {
uint32_t div;
2022-08-15 04:20:27 -05:00
if (bits <= AUD_BITS_16) {
div = 2;
} else {
div = 4;
}
div *= chans;
return size / div;
2022-08-15 04:20:27 -05:00
}
static void POSSIBLY_UNUSED af_zero_mem(void *dst, unsigned int size) {
uint32_t *d = dst;
uint32_t count = size / 4;
2022-08-15 04:20:27 -05:00
while (count--) {
*d++ = 0;
}
2022-08-15 04:20:27 -05:00
}
static bool af_codec_playback_pre_handler(uint8_t *buf, uint32_t len,
const struct af_stream_cfg_t *role) {
uint32_t POSSIBLY_UNUSED time;
2022-08-15 04:20:27 -05:00
if (0) {
2022-08-15 04:20:27 -05:00
#ifdef AUDIO_OUTPUT_PA_ON_FADE_IN
} else if (dac_pa_state == AF_DAC_PA_ON_TRIGGER &&
(time = hal_sys_timer_get()) - dac_dc_start_time >=
AF_CODEC_DC_STABLE_INTERVAL &&
time - dac_pa_stop_time >= AF_CODEC_PA_RESTART_INTERVAL) {
analog_aud_codec_speaker_enable(true);
dac_pa_state = AF_DAC_PA_NULL;
2022-08-15 04:20:27 -05:00
#endif // AUDIO_OUTPUT_PA_ON_FADE_IN
#ifdef AUDIO_OUTPUT_PA_OFF_FADE_OUT
} else if (dac_pa_state == AF_DAC_PA_OFF_TRIGGER) {
dac_dc_start_time = hal_sys_timer_get();
dac_pa_state = AF_DAC_PA_OFF_DC_START;
} else if (dac_pa_state == AF_DAC_PA_OFF_DC_START) {
time = hal_sys_timer_get();
if (time - dac_dc_start_time >= AF_CODEC_DC_STABLE_INTERVAL) {
dac_pa_state = AF_DAC_PA_NULL;
analog_aud_codec_speaker_enable(false);
dac_pa_stop_time = time;
2022-08-15 04:20:27 -05:00
#ifdef RTOS
osSignalSet(fade_thread_id, (1 << AF_FADE_OUT_SIGNAL_ID));
2022-08-15 04:20:27 -05:00
#endif
}
#endif // AUDIO_OUTPUT_PA_OFF_FADE_OUT
}
2022-08-15 04:20:27 -05:00
#if defined(AUDIO_OUTPUT_PA_ON_FADE_IN) || defined(AUDIO_OUTPUT_PA_OFF_FADE_OUT)
if (dac_pa_state == AF_DAC_PA_ON_TRIGGER ||
dac_pa_state == AF_DAC_PA_OFF_DC_START) {
af_zero_mem(buf, len);
return true;
}
2022-08-15 04:20:27 -05:00
#endif
return false;
2022-08-15 04:20:27 -05:00
}
#ifdef AUDIO_OUTPUT_DC_CALIB_SW
static void af_codec_playback_sw_dc_calib(uint8_t *buf, uint32_t len,
enum AUD_BITS_T bits,
enum AUD_CHANNEL_NUM_T chans) {
uint32_t cnt;
2022-08-15 04:20:27 -05:00
if (bits <= AUD_BITS_16) {
int16_t *ptr16 = (int16_t *)buf;
2022-08-15 04:20:27 -05:00
#ifdef AUDIO_OUTPUT_DC_CALIB_SW
int16_t dc_l = dac_dc[0];
int16_t dc_r = dac_dc[1];
2022-08-15 04:20:27 -05:00
#endif
if (chans == AUD_CHANNEL_NUM_1) {
cnt = len / sizeof(int16_t);
while (cnt-- > 0) {
2022-08-15 04:20:27 -05:00
#ifdef AUDIO_OUTPUT_DC_CALIB_SW
*ptr16 = __SSAT(*ptr16 + dc_l, 16);
2022-08-15 04:20:27 -05:00
#endif
ptr16++;
}
} else {
cnt = len / sizeof(int16_t) / 2;
while (cnt-- > 0) {
2022-08-15 04:20:27 -05:00
#ifdef AUDIO_OUTPUT_DC_CALIB_SW
*ptr16 = __SSAT(*ptr16 + dc_l, 16);
*(ptr16 + 1) = __SSAT(*(ptr16 + 1) + dc_r, 16);
2022-08-15 04:20:27 -05:00
#endif
ptr16 += 2;
}
}
} else {
int32_t *ptr32 = (int32_t *)buf;
int32_t dac_bits =
2022-08-15 04:20:27 -05:00
#ifdef CHIP_BEST1000
CODEC_PLAYBACK_BIT_DEPTH;
2022-08-15 04:20:27 -05:00
#else
24;
2022-08-15 04:20:27 -05:00
#endif
int32_t val_shift;
2022-08-15 04:20:27 -05:00
if (dac_bits < 24) {
val_shift = 24 - dac_bits;
} else {
val_shift = 0;
}
2022-08-15 04:20:27 -05:00
#ifdef AUDIO_OUTPUT_DC_CALIB_SW
int32_t dc_l = dac_dc[0] << 8;
int32_t dc_r = dac_dc[1] << 8;
2022-08-15 04:20:27 -05:00
#endif
if (chans == AUD_CHANNEL_NUM_1) {
cnt = len / sizeof(int32_t);
while (cnt-- > 0) {
2022-08-15 04:20:27 -05:00
#ifdef CORRECT_SAMPLE_VALUE
*ptr32 = ((*ptr32) << (32 - dac_bits)) >> (32 - dac_bits);
2022-08-15 04:20:27 -05:00
#endif
#ifdef AUDIO_OUTPUT_DC_CALIB_SW
*ptr32 = __SSAT((*ptr32 + dc_l) >> val_shift, dac_bits);
2022-08-15 04:20:27 -05:00
#elif defined(CORRECT_SAMPLE_VALUE)
*ptr32 = __SSAT(*ptr32 >> val_shift, dac_bits);
2022-08-15 04:20:27 -05:00
#else
*ptr32 = *ptr32 >> val_shift;
2022-08-15 04:20:27 -05:00
#endif
ptr32++;
}
} else {
cnt = len / sizeof(int32_t) / 2;
while (cnt-- > 0) {
2022-08-15 04:20:27 -05:00
#ifdef CORRECT_SAMPLE_VALUE
*ptr32 = ((*ptr32) << (32 - dac_bits)) >> (32 - dac_bits);
*(ptr32 + 1) = ((*(ptr32 + 1)) << (32 - dac_bits)) >> (32 - dac_bits);
2022-08-15 04:20:27 -05:00
#endif
#ifdef AUDIO_OUTPUT_DC_CALIB_SW
*ptr32 = __SSAT((*ptr32 + dc_l) >> val_shift, dac_bits);
*(ptr32 + 1) = __SSAT((*(ptr32 + 1) + dc_r) >> val_shift, dac_bits);
2022-08-15 04:20:27 -05:00
#elif defined(CORRECT_SAMPLE_VALUE)
*ptr32 = __SSAT(*ptr32 >> val_shift, dac_bits);
*(ptr32 + 1) = __SSAT(*(ptr32 + 1) >> val_shift, dac_bits);
2022-08-15 04:20:27 -05:00
#else
*ptr32 = *ptr32 >> val_shift;
*(ptr32 + 1) = *(ptr32 + 1) >> val_shift;
2022-08-15 04:20:27 -05:00
#endif
ptr32 += 2;
}
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
}
#endif // AUDIO_OUTPUT_DC_CALIB_SW
static void af_codec_playback_post_handler(uint8_t *buf, uint32_t len,
const struct af_stream_cfg_t *role) {
POSSIBLY_UNUSED enum AUD_BITS_T bits;
POSSIBLY_UNUSED enum AUD_CHANNEL_NUM_T chans;
POSSIBLY_UNUSED uint32_t cnt;
2022-08-15 04:20:27 -05:00
bits = role->cfg.bits;
chans = role->cfg.channel_num;
2022-08-15 04:20:27 -05:00
#ifdef AUDIO_OUTPUT_SW_GAIN
#ifdef AUDIO_OUTPUT_SW_LIMITER
if (bits <= AUD_BITS_16) {
cnt = len / sizeof(int16_t);
} else {
cnt = len / sizeof(int32_t);
}
ApplyFloatLimiter(FloatLimiterP, buf, saved_output_coef, cnt);
2022-08-15 04:20:27 -05:00
#else
af_codec_sw_gain_process(buf, len, bits, chans);
2022-08-15 04:20:27 -05:00
#endif
#endif
if (codec_play_post_hdlr) {
codec_play_post_hdlr(buf, len, (void *)&role->cfg);
}
2022-08-15 04:20:27 -05:00
#if defined(AUDIO_OUTPUT_INVERT_RIGHT_CHANNEL)
if (chans == AUD_CHANNEL_NUM_2) {
if (bits == AUD_BITS_16) {
int16_t *buf16 = (int16_t *)buf;
for (int i = 1; i < len / sizeof(int16_t); i += 2) {
int32_t tmp = -buf16[i];
buf16[i] = __SSAT(tmp, 16);
}
} else {
int32_t *buf32 = (int32_t *)buf;
for (int i = 1; i < len / sizeof(int32_t); i += 2) {
int32_t tmp = -buf32[i];
buf32[i] = __SSAT(tmp, 24);
}
}
}
#endif
#if (defined(CHIP_BEST1000) || defined(CHIP_BEST2000)) && \
defined(AUDIO_OUTPUT_CALIB_GAIN_MISSMATCH)
// gain must less than 1.0
float gain = 0.9441f;
if (chans == AUD_CHANNEL_NUM_2) {
if (bits == AUD_BITS_16) {
int16_t *buf16 = (int16_t *)buf;
for (int i = 1; i < len / sizeof(int16_t); i += 2) {
int32_t tmp = (int)(buf16[i + 1] * gain);
buf16[i + 1] = __SSAT(tmp, 16);
}
} else {
int32_t *buf32 = (int32_t *)buf;
for (int i = 1; i < len / sizeof(int32_t); i += 2) {
int32_t tmp = (int)(buf32[i + 1] * gain);
buf32[i + 1] = __SSAT(tmp, 24);
}
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
#endif
#if defined(AUDIO_OUTPUT_DC_CALIB_SW)
af_codec_playback_sw_dc_calib(buf, len, bits, chans);
2022-08-15 04:20:27 -05:00
#elif defined(CHIP_BEST1000)
if (bits == AUD_BITS_24 || bits == AUD_BITS_32) {
int32_t *ptr32 = (int32_t *)buf;
int32_t val_shift;
2022-08-15 04:20:27 -05:00
if (bits == AUD_BITS_24) {
val_shift = 24 - CODEC_PLAYBACK_BIT_DEPTH;
} else {
val_shift = 32 - CODEC_PLAYBACK_BIT_DEPTH;
}
2022-08-15 04:20:27 -05:00
cnt = len / sizeof(int32_t);
while (cnt-- > 0) {
*ptr32 >>= val_shift;
ptr32++;
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
#endif
#if defined(CHIP_BEST1000) && defined(AUDIO_OUTPUT_GAIN_M60DB_CHECK)
hal_codec_dac_gain_m60db_check(HAL_CODEC_PERF_TEST_M60DB);
2022-08-15 04:20:27 -05:00
#endif
}
static inline void af_thread_stream_handler(enum AUD_STREAM_ID_T id,
enum AUD_STREAM_T stream) {
uint32_t lock;
struct af_stream_cfg_t *role;
uint32_t dma_addr, hw_pos;
uint8_t *buf;
uint32_t len;
uint32_t pp_cnt;
bool codec_playback;
bool skip_handler;
enum AF_PP_T pp_index;
2022-08-15 04:20:27 -05:00
role = af_get_stream_role(id, stream);
2022-08-15 04:20:27 -05:00
af_lock_thread();
2022-08-15 04:20:27 -05:00
if (role->handler && (role->ctl.status & AF_STATUS_STREAM_START_STOP)) {
lock = int_lock();
pp_cnt = role->ctl.pp_cnt;
role->ctl.pp_cnt = 0;
int_unlock(lock);
2022-08-15 04:20:27 -05:00
af_sig_lost_cnt[id][stream] = pp_cnt - 1;
if (af_sig_lost_cnt[id][stream]) {
TRACE(3, "af_thread:WARNING: id=%d stream=%d lost %u signals", id, stream,
af_sig_lost_cnt[id][stream]);
}
2022-08-15 04:20:27 -05:00
pp_index = PP_PINGPANG(role->ctl.pp_index);
// Get PP index from accurate DMA pos
dma_addr = af_stream_get_cur_dma_addr(id, stream);
hw_pos = dma_addr - (uint32_t)role->dma_buf_ptr;
if (hw_pos > role->dma_buf_size) {
TRACE(
3,
"af_thread: Failed to get valid dma addr for id=%d stream=%d: 0x%08x",
id, stream, dma_addr);
}
2022-08-15 04:20:27 -05:00
#ifndef CHIP_BEST1000
if (role->cfg.chan_sep_buf && role->cfg.channel_num > AUD_CHANNEL_NUM_1) {
uint32_t chan_size;
chan_size = role->dma_buf_size / role->cfg.channel_num;
if (hw_pos <= role->dma_buf_size) {
hw_pos = hw_pos % chan_size;
if (hw_pos < chan_size / 2) {
pp_index = PP_PANG;
} else if (hw_pos < chan_size) {
pp_index = PP_PING;
}
}
if (pp_index == PP_PING) {
buf = role->dma_buf_ptr;
} else {
buf = role->dma_buf_ptr + chan_size / 2;
}
} else
2022-08-15 04:20:27 -05:00
#endif
{
if (hw_pos < role->dma_buf_size / 2) {
pp_index = PP_PANG;
} else if (hw_pos < role->dma_buf_size) {
pp_index = PP_PING;
}
if (pp_index == PP_PING) {
buf = role->dma_buf_ptr;
} else {
buf = role->dma_buf_ptr + role->dma_buf_size / 2;
}
}
2022-08-15 04:20:27 -05:00
if (stream == AUD_STREAM_PLAYBACK &&
role->ctl.use_device == AUD_STREAM_USE_INT_CODEC) {
codec_playback = true;
} else {
codec_playback = false;
}
2022-08-15 04:20:27 -05:00
skip_handler = false;
len = role->dma_buf_size / 2;
2022-08-15 04:20:27 -05:00
if (codec_playback) {
skip_handler = af_codec_playback_pre_handler(buf, len, role);
}
2022-08-15 04:20:27 -05:00
if (!skip_handler) {
2022-08-15 04:20:27 -05:00
#ifdef __RAND_FROM_MIC__
if ((AUD_STREAM_CAPTURE == stream) &&
(AUD_STREAM_USE_INT_CODEC == role->cfg.device)) {
random_data_process(buf, role->dma_buf_size / 2, role->cfg.bits,
role->cfg.channel_num);
}
2022-08-15 04:20:27 -05:00
#endif
role->handler(buf, len);
}
2022-08-15 04:20:27 -05:00
if (codec_playback) {
af_codec_playback_post_handler(buf, len, role);
}
2022-08-15 04:20:27 -05:00
#if defined(RTOS) && defined(AF_STREAM_ID_0_PLAYBACK_FADEOUT)
af_stream_stop_process(role, buf, len);
2022-08-15 04:20:27 -05:00
#endif
if (role->ctl.pp_cnt) {
TRACE(3,
"af_thread:WARNING: id=%d stream=%d hdlr ran too long (pp_cnt=%u)",
id, stream, role->ctl.pp_cnt);
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
af_unlock_thread();
2022-08-15 04:20:27 -05:00
}
#ifdef RTOS
static void af_thread(void const *argument) {
osEvent evt;
uint32_t signals = 0;
enum AUD_STREAM_ID_T id;
enum AUD_STREAM_T stream;
2022-08-15 04:20:27 -05:00
while (1) {
// wait any signal
evt = osSignalWait(0x0, osWaitForever);
signals = evt.value.signals;
// TRACE(3,"[%s] status = %x, signals = %d", __func__, evt.status,
// evt.value.signals);
2022-08-15 04:20:27 -05:00
if (evt.status == osEventSignal) {
for (uint8_t i = 0; i < AUD_STREAM_ID_NUM * AUD_STREAM_NUM; i++) {
if (signals & (1 << i)) {
id = (enum AUD_STREAM_ID_T)(i >> 1);
stream = (enum AUD_STREAM_T)(i & 1);
2022-08-15 04:20:27 -05:00
af_thread_stream_handler(id, stream);
2022-08-15 04:20:27 -05:00
}
}
} else {
TRACE(2, "[%s] ERROR: evt.status = %d", __func__, evt.status);
continue;
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
}
#else // !RTOS
#include "cmsis.h"
static volatile uint32_t af_flag_open;
static volatile uint32_t af_flag_signal;
static void af_set_flag(volatile uint32_t *flag, uint32_t set) {
uint32_t lock;
2022-08-15 04:20:27 -05:00
lock = int_lock();
*flag |= set;
int_unlock(lock);
2022-08-15 04:20:27 -05:00
}
static void af_clear_flag(volatile uint32_t *flag, uint32_t clear) {
uint32_t lock;
2022-08-15 04:20:27 -05:00
lock = int_lock();
*flag &= ~clear;
int_unlock(lock);
2022-08-15 04:20:27 -05:00
}
static bool af_flag_active(volatile uint32_t *flag, uint32_t bits) {
return !!(*flag & bits);
2022-08-15 04:20:27 -05:00
}
void af_thread(void const *argument) {
uint32_t lock;
uint32_t i;
enum AUD_STREAM_ID_T id;
enum AUD_STREAM_T stream;
2022-08-15 04:20:27 -05:00
if (af_flag_open == 0) {
return;
}
2022-08-15 04:20:27 -05:00
for (i = 0; i < AUD_STREAM_ID_NUM * AUD_STREAM_NUM; i++) {
if (!af_flag_active(&af_flag_signal, 1 << i)) {
continue;
}
af_clear_flag(&af_flag_signal, 1 << i);
2022-08-15 04:20:27 -05:00
id = (enum AUD_STREAM_ID_T)(i >> 1);
stream = (enum AUD_STREAM_T)(i & 1);
2022-08-15 04:20:27 -05:00
af_thread_stream_handler(id, stream);
}
2022-08-15 04:20:27 -05:00
lock = int_lock();
if (af_flag_signal == 0) {
hal_cpu_wake_unlock(AF_CPU_WAKE_USER);
}
int_unlock(lock);
2022-08-15 04:20:27 -05:00
}
#endif // !RTOS
static void af_dma_irq_handler(uint8_t ch, uint32_t remain_dst_tsize,
uint32_t error, struct HAL_DMA_DESC_T *lli) {
struct af_stream_cfg_t *role = NULL;
// initial parameter
for (uint8_t id = 0; id < AUD_STREAM_ID_NUM; id++) {
for (uint8_t stream = 0; stream < AUD_STREAM_NUM; stream++) {
role = af_get_stream_role((enum AUD_STREAM_ID_T)id,
(enum AUD_STREAM_T)stream);
if (role->dma_cfg.ch == ch) {
role->ctl.pp_index = PP_PINGPANG(role->ctl.pp_index);
role->ctl.pp_cnt++;
// TRACE(4,"[%s] id = %d, stream = %d, ch = %d", __func__, id, stream,
// ch); TRACE(2,"[%s] PLAYBACK pp_cnt = %d", __func__,
// role->ctl.pp_cnt);
2022-08-15 04:20:27 -05:00
#ifdef RTOS
if (irq_notif) {
irq_notif(id, stream);
}
osSignalSet(af_thread_tid, 0x01 << (id * 2 + stream));
2022-08-15 04:20:27 -05:00
#else
af_set_flag(&af_flag_signal, 0x01 << (id * 2 + stream));
hal_cpu_wake_lock(AF_CPU_WAKE_USER);
2022-08-15 04:20:27 -05:00
#endif
return;
}
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
// invalid dma irq
ASSERT(0, "[%s] ERROR: channel id = %d", __func__, ch);
2022-08-15 04:20:27 -05:00
}
#ifdef __CODEC_ASYNC_CLOSE__
static void af_codec_async_close(void) {
af_lock_thread();
codec_int_close(CODEC_CLOSE_ASYNC_REAL);
af_unlock_thread();
2022-08-15 04:20:27 -05:00
}
#endif
void *af_thread_tid_get(void) {
2022-08-15 04:20:27 -05:00
#ifdef RTOS
return (void *)af_thread_tid;
2022-08-15 04:20:27 -05:00
#else
return NULL;
2022-08-15 04:20:27 -05:00
#endif
}
#ifdef CODEC_ANC_BOOST
static void af_codec_anc_boost_delay(uint32_t ms) {
af_unlock_thread();
2022-08-15 04:20:27 -05:00
osDelay(ms);
2022-08-15 04:20:27 -05:00
af_lock_thread();
2022-08-15 04:20:27 -05:00
}
#endif
uint32_t af_open(void) {
AF_TRACE_DEBUG();
struct af_stream_cfg_t *role = NULL;
2022-08-15 04:20:27 -05:00
#ifdef RTOS
if (audioflinger_mutex_id == NULL) {
audioflinger_mutex_id = osMutexCreate((osMutex(audioflinger_mutex)));
}
2022-08-15 04:20:27 -05:00
#endif
af_lock_thread();
2022-08-15 04:20:27 -05:00
#ifdef AUDIO_OUTPUT_DC_CALIB
if (!dac_dc_valid) {
int16_t dc_l, dc_r;
uint16_t max_dc;
float attn;
dac_dc_valid = true;
analog_aud_get_dc_calib_value(&dc_l, &dc_r);
if (ABS(dc_l) >= ABS(dc_r)) {
max_dc = ABS(dc_l);
} else {
max_dc = ABS(dc_r);
}
ASSERT(max_dc + 1 < AF_CODEC_DC_MAX_SCALE, "Bad dc values: (%d, %d)", dc_l,
dc_r);
if (max_dc) {
attn = 1 - (float)(max_dc + 1) / AF_CODEC_DC_MAX_SCALE;
} else {
attn = 1;
}
hal_codec_set_dac_dc_gain_attn(attn);
2022-08-15 04:20:27 -05:00
#ifdef AUDIO_OUTPUT_DC_CALIB_SW
dac_dc[0] = dc_l;
dac_dc[1] = dc_r;
2022-08-15 04:20:27 -05:00
#else
hal_codec_set_dac_dc_offset(dc_l, dc_r);
2022-08-15 04:20:27 -05:00
#endif
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AUDIO_OUTPUT_SW_GAIN
hal_codec_set_sw_output_coef_callback(af_codec_output_gain_changed);
2022-08-15 04:20:27 -05:00
#endif
#ifdef __CODEC_ASYNC_CLOSE__
codec_int_set_close_handler(af_codec_async_close);
2022-08-15 04:20:27 -05:00
#endif
#ifdef CODEC_ANC_BOOST
codec_set_anc_boost_delay_func(af_codec_anc_boost_delay);
2022-08-15 04:20:27 -05:00
#endif
// initial parameters
for (uint8_t id = 0; id < AUD_STREAM_ID_NUM; id++) {
for (uint8_t stream = 0; stream < AUD_STREAM_NUM; stream++) {
role = af_get_stream_role((enum AUD_STREAM_ID_T)id,
(enum AUD_STREAM_T)stream);
2022-08-15 04:20:27 -05:00
if (role->ctl.status == AF_STATUS_NULL) {
role->dma_buf_ptr = NULL;
role->dma_buf_size = 0;
role->ctl.pp_index = PP_PING;
role->ctl.status = AF_STATUS_OPEN_CLOSE;
role->ctl.use_device = AUD_STREAM_USE_DEVICE_NULL;
role->dma_cfg.ch = HAL_DMA_CHAN_NONE;
} else {
ASSERT(0, "[%s] ERROR: id = %d, stream = %d", __func__, id, stream);
}
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
#ifdef RTOS
af_thread_tid = osThreadCreate(osThread(af_thread), NULL);
af_default_priority = af_get_priority();
osSignalSet(af_thread_tid, 0x0);
2022-08-15 04:20:27 -05:00
#endif
#ifdef __RAND_FROM_MIC__
randInit();
2022-08-15 04:20:27 -05:00
#endif
af_unlock_thread();
2022-08-15 04:20:27 -05:00
return AF_RES_SUCCESS;
2022-08-15 04:20:27 -05:00
}
static void af_stream_update_dma_buffer(enum AUD_STREAM_T stream,
struct af_stream_cfg_t *role,
const struct AF_STREAM_CONFIG_T *cfg) {
int i;
enum HAL_DMA_RET_T dma_ret;
struct HAL_DMA_DESC_T *dma_desc, *next_desc;
struct HAL_DMA_CH_CFG_T *dma_cfg;
int irq;
uint32_t desc_xfer_size;
uint32_t align;
uint8_t samp_size;
enum HAL_DMA_WDITH_T width;
2022-08-15 04:20:27 -05:00
#ifndef CHIP_BEST1000
bool dma_2d_en;
uint32_t chan_desc_xfer_size = 0;
2022-08-15 04:20:27 -05:00
#endif
dma_desc = &role->dma_desc[0];
dma_cfg = &role->dma_cfg;
2022-08-15 04:20:27 -05:00
#ifdef CODEC_DSD
if (stream == AUD_STREAM_PLAYBACK &&
role->cfg.device == AUD_STREAM_USE_INT_CODEC) {
if (dma_cfg->dst_periph == HAL_AUDMA_DSD_TX) {
dma_cfg->dst_bsize = HAL_DMA_BSIZE_1;
} else {
dma_cfg->dst_bsize = HAL_DMA_BSIZE_4;
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
#endif
if (role->cfg.device == AUD_STREAM_USE_BT_PCM) {
dma_cfg->dst_bsize = HAL_DMA_BSIZE_1;
dma_cfg->src_bsize = HAL_DMA_BSIZE_1;
dma_cfg->try_burst = 0;
}
2022-08-15 04:20:27 -05:00
role->dma_buf_ptr = cfg->data_ptr;
role->dma_buf_size = cfg->data_size;
2022-08-15 04:20:27 -05:00
if (cfg->bits == AUD_BITS_24 || cfg->bits == AUD_BITS_32) {
width = HAL_DMA_WIDTH_WORD;
samp_size = 4;
} else if (cfg->bits == AUD_BITS_16) {
width = HAL_DMA_WIDTH_HALFWORD;
samp_size = 2;
} else {
ASSERT(false, "%s: Invalid stream config bits=%d", __func__, cfg->bits);
width = HAL_DMA_WIDTH_BYTE;
samp_size = 1;
}
2022-08-15 04:20:27 -05:00
#ifdef DYNAMIC_AUDIO_BUFFER_COUNT
uint32_t desc_cnt;
2022-08-15 04:20:27 -05:00
desc_cnt = (cfg->data_size / samp_size + HAL_DMA_MAX_DESC_XFER_SIZE - 1) /
HAL_DMA_MAX_DESC_XFER_SIZE;
if (desc_cnt < 2) {
desc_cnt = 2;
} else if (desc_cnt & (desc_cnt - 1)) {
desc_cnt = 1 << (31 - __CLZ(desc_cnt) + 1);
}
TRACE(4, "%s: desc_cnt=%u data_size=%u samp_size=%u", __func__, desc_cnt,
cfg->data_size, samp_size);
ASSERT(MIN_AUDIO_BUFFER_COUNT <= desc_cnt &&
desc_cnt <= MAX_AUDIO_BUFFER_COUNT,
"%s: Bad desc_cnt=%u", __func__, desc_cnt);
role->dma_desc_cnt = desc_cnt;
2022-08-15 04:20:27 -05:00
#endif
desc_xfer_size = cfg->data_size / AUDIO_BUFFER_COUNT;
2022-08-15 04:20:27 -05:00
#ifndef CHIP_BEST1000
if (cfg->chan_sep_buf && cfg->channel_num > AUD_CHANNEL_NUM_1) {
dma_2d_en = true;
} else {
dma_2d_en = false;
}
2022-08-15 04:20:27 -05:00
if (dma_2d_en) {
enum HAL_DMA_BSIZE_T bsize;
uint8_t burst_size;
2022-08-15 04:20:27 -05:00
chan_desc_xfer_size = desc_xfer_size / cfg->channel_num;
2022-08-15 04:20:27 -05:00
if (stream == AUD_STREAM_PLAYBACK) {
bsize = dma_cfg->src_bsize;
} else {
bsize = dma_cfg->dst_bsize;
}
if (bsize == HAL_DMA_BSIZE_1) {
burst_size = 1;
} else if (bsize == HAL_DMA_BSIZE_4) {
burst_size = 4;
} else {
burst_size = 8;
}
align = burst_size * samp_size * cfg->channel_num;
// Ensure word-aligned too
if (align & 0x1) {
align *= 4;
} else if (align & 0x2) {
align *= 2;
}
} else
#endif
{
align = 4;
}
ASSERT(desc_xfer_size * AUDIO_BUFFER_COUNT == cfg->data_size &&
(desc_xfer_size % align) == 0,
"%s: Dma data size is not aligned: data_size=%u AUDIO_BUFFER_COUNT=%u "
"align=%u",
__func__, cfg->data_size, AUDIO_BUFFER_COUNT, align);
dma_cfg->dst_width = width;
dma_cfg->src_width = width;
dma_cfg->src_tsize = desc_xfer_size / samp_size;
for (i = 0; i < AUDIO_BUFFER_COUNT; i++) {
if (i == AUDIO_BUFFER_COUNT - 1) {
next_desc = &dma_desc[0];
irq = 1;
} else {
next_desc = &dma_desc[i + 1];
if (i == AUDIO_BUFFER_COUNT / 2 - 1) {
irq = 1;
} else {
irq = 0;
}
2022-08-15 04:20:27 -05:00
}
if (stream == AUD_STREAM_PLAYBACK) {
2022-08-15 04:20:27 -05:00
#ifndef CHIP_BEST1000
if (dma_2d_en) {
dma_cfg->src = (uint32_t)(role->dma_buf_ptr + chan_desc_xfer_size * i);
} else
2022-08-15 04:20:27 -05:00
#endif
{
dma_cfg->src = (uint32_t)(role->dma_buf_ptr + desc_xfer_size * i);
}
} else {
2022-08-15 04:20:27 -05:00
#ifndef CHIP_BEST1000
if (dma_2d_en) {
dma_cfg->dst = (uint32_t)(role->dma_buf_ptr + chan_desc_xfer_size * i);
} else
2022-08-15 04:20:27 -05:00
#endif
{
dma_cfg->dst = (uint32_t)(role->dma_buf_ptr + desc_xfer_size * i);
}
2022-08-15 04:20:27 -05:00
}
dma_ret = hal_audma_init_desc(&dma_desc[i], dma_cfg, next_desc, irq);
ASSERT(dma_ret == HAL_DMA_OK,
"[%s] Failed to init dma desc for stream %d: ret=%d", __func__,
stream, dma_ret);
}
2022-08-15 04:20:27 -05:00
}
// Support memory<-->peripheral
// Note: Do not support peripheral <--> peripheral
uint32_t af_stream_open(enum AUD_STREAM_ID_T id, enum AUD_STREAM_T stream,
const struct AF_STREAM_CONFIG_T *cfg) {
AF_TRACE_DEBUG();
struct af_stream_cfg_t *role;
enum AF_RESULT_T ret;
enum AUD_STREAM_USE_DEVICE_T device;
struct HAL_DMA_CH_CFG_T *dma_cfg = NULL;
2022-08-15 04:20:27 -05:00
#ifdef __RAND_FROM_MIC__
random_status_sync();
2022-08-15 04:20:27 -05:00
#endif
role = af_get_stream_role(id, stream);
TRACE(3, "[%s] id = %d, stream = %d", __func__, id, stream);
2022-08-15 04:20:27 -05:00
ASSERT(cfg->data_ptr != NULL, "[%s] ERROR: data_ptr == NULL!!!", __func__);
ASSERT(((uint32_t)cfg->data_ptr) % 4 == 0,
"[%s] ERROR: data_ptr(%p) is not align!!!", __func__, cfg->data_ptr);
ASSERT(cfg->data_size != 0, "[%s] ERROR: data_size == 0!!!", __func__);
2022-08-15 04:20:27 -05:00
#ifndef CHIP_BEST1000
if (cfg->chan_sep_buf && cfg->channel_num > AUD_CHANNEL_NUM_1) {
ASSERT(cfg->device == AUD_STREAM_USE_INT_CODEC ||
cfg->device == AUD_STREAM_USE_I2S0_MASTER ||
cfg->device == AUD_STREAM_USE_I2S0_SLAVE ||
cfg->device == AUD_STREAM_USE_I2S1_MASTER ||
cfg->device == AUD_STREAM_USE_I2S1_SLAVE ||
cfg->device == AUD_STREAM_USE_TDM0_MASTER ||
cfg->device == AUD_STREAM_USE_TDM0_SLAVE ||
cfg->device == AUD_STREAM_USE_TDM1_MASTER ||
cfg->device == AUD_STREAM_USE_TDM1_SLAVE,
"[%s] ERROR: Unsupport chan_sep_buf for device %d", __func__,
cfg->device);
}
#endif
ret = AF_RES_FAILD;
af_lock_thread();
// check af is open
if (role->ctl.status != AF_STATUS_OPEN_CLOSE) {
TRACE(2, "[%s] ERROR: status = %d", __func__, role->ctl.status);
goto _exit;
}
role->cfg = *cfg;
device = cfg->device;
role->ctl.use_device = device;
dma_cfg = &role->dma_cfg;
memset(dma_cfg, 0, sizeof(*dma_cfg));
if (device == AUD_STREAM_USE_BT_PCM) {
dma_cfg->dst_bsize = HAL_DMA_BSIZE_1;
dma_cfg->src_bsize = HAL_DMA_BSIZE_1;
dma_cfg->try_burst = 0;
} else {
dma_cfg->dst_bsize = HAL_DMA_BSIZE_4;
2022-08-15 04:20:27 -05:00
#ifndef CHIP_BEST1000
// If channel num > 1, burst size should be set to 1 for:
// 1) all playback streams with 2D DMA enabled; or
// 2) internal codec capture streams with 2D DMA disabled.
if (cfg->channel_num > AUD_CHANNEL_NUM_1 &&
((stream == AUD_STREAM_PLAYBACK && cfg->chan_sep_buf) ||
(device == AUD_STREAM_USE_INT_CODEC && stream == AUD_STREAM_CAPTURE &&
!cfg->chan_sep_buf))) {
dma_cfg->src_bsize = HAL_DMA_BSIZE_1;
} else
2022-08-15 04:20:27 -05:00
#endif
{
dma_cfg->src_bsize = HAL_DMA_BSIZE_4;
2022-08-15 04:20:27 -05:00
}
dma_cfg->try_burst = 1;
}
2022-08-15 04:20:27 -05:00
dma_cfg->handler = af_dma_irq_handler;
2022-08-15 04:20:27 -05:00
if (stream == AUD_STREAM_PLAYBACK) {
AF_TRACE_DEBUG();
dma_cfg->src_periph = (enum HAL_DMA_PERIPH_T)0;
dma_cfg->type = HAL_DMA_FLOW_M2P_DMA;
2022-08-15 04:20:27 -05:00
// open device and stream
if (0) {
2022-08-15 04:20:27 -05:00
}
2022-08-15 04:20:27 -05:00
#ifdef AF_DEVICE_EXT_CODEC
else if (device == AUD_STREAM_USE_EXT_CODEC) {
AF_TRACE_DEBUG();
tlv32aic32_open();
tlv32aic32_stream_open(stream);
2022-08-15 04:20:27 -05:00
dma_cfg->dst_periph = HAL_AUDMA_I2S0_TX;
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_I2S
else if (device == AUD_STREAM_USE_I2S0_MASTER) {
AF_TRACE_DEBUG();
hal_i2s_open(HAL_I2S_ID_0, stream, HAL_I2S_MODE_MASTER);
dma_cfg->dst_periph = HAL_AUDMA_I2S0_TX;
} else if (device == AUD_STREAM_USE_I2S0_SLAVE) {
AF_TRACE_DEBUG();
hal_i2s_open(HAL_I2S_ID_0, stream, HAL_I2S_MODE_SLAVE);
dma_cfg->dst_periph = HAL_AUDMA_I2S0_TX;
}
2022-08-15 04:20:27 -05:00
#if defined(CHIP_HAS_I2S) && (CHIP_HAS_I2S > 1)
else if (device == AUD_STREAM_USE_I2S1_MASTER) {
AF_TRACE_DEBUG();
hal_i2s_open(HAL_I2S_ID_1, stream, HAL_I2S_MODE_MASTER);
dma_cfg->dst_periph = HAL_AUDMA_I2S1_TX;
} else if (device == AUD_STREAM_USE_I2S1_SLAVE) {
AF_TRACE_DEBUG();
hal_i2s_open(HAL_I2S_ID_1, stream, HAL_I2S_MODE_SLAVE);
dma_cfg->dst_periph = HAL_AUDMA_I2S1_TX;
}
2022-08-15 04:20:27 -05:00
#endif
#endif
#ifdef AF_DEVICE_TDM
else if (device == AUD_STREAM_USE_TDM0_MASTER) {
AF_TRACE_DEBUG();
hal_tdm_open(HAL_I2S_ID_0, stream, HAL_I2S_MODE_MASTER);
dma_cfg->dst_periph = HAL_AUDMA_I2S0_TX;
} else if (device == AUD_STREAM_USE_TDM0_SLAVE) {
AF_TRACE_DEBUG();
hal_tdm_open(HAL_I2S_ID_0, stream, HAL_I2S_MODE_SLAVE);
dma_cfg->dst_periph = HAL_AUDMA_I2S0_TX;
}
2022-08-15 04:20:27 -05:00
#if defined(CHIP_HAS_I2S) && (CHIP_HAS_I2S > 1)
else if (device == AUD_STREAM_USE_TDM1_MASTER) {
AF_TRACE_DEBUG();
hal_tdm_open(HAL_I2S_ID_1, stream, HAL_I2S_MODE_MASTER);
dma_cfg->dst_periph = HAL_AUDMA_I2S1_TX;
} else if (device == AUD_STREAM_USE_TDM1_SLAVE) {
AF_TRACE_DEBUG();
hal_tdm_open(HAL_I2S_ID_1, stream, HAL_I2S_MODE_SLAVE);
dma_cfg->dst_periph = HAL_AUDMA_I2S1_TX;
}
2022-08-15 04:20:27 -05:00
#endif
#endif
#ifdef AF_DEVICE_INT_CODEC
else if (device == AUD_STREAM_USE_INT_CODEC) {
AF_TRACE_DEBUG();
codec_int_open();
codec_int_stream_open(stream);
2022-08-15 04:20:27 -05:00
#ifdef AUDIO_OUTPUT_SW_GAIN
#ifdef AUDIO_OUTPUT_SW_LIMITER
FLOATLIMITER_ERROR limiter_err;
if (FloatLimiterP == NULL) {
FloatLimiterP = CreateFloatLimiter(
FL_ATTACK_DEFAULT_MS, FL_RELEASE_DEFAULT_MS, FL_THRESHOLD,
MAX_CHANEL, MAX_SAMPLERATE, MAX_SAMPLEBIT);
}
limiter_err = SetFloatLimiterNChannels(FloatLimiterP, cfg->channel_num);
if (limiter_err != FLOATLIMIT_OK) {
goto _exit;
}
limiter_err = SetFloatLimiterSampleRate(FloatLimiterP, cfg->sample_rate);
if (limiter_err != FLOATLIMIT_OK) {
goto _exit;
}
limiter_err = SetFloatLimiterSampleBit(FloatLimiterP, cfg->bits);
if (limiter_err != FLOATLIMIT_OK) {
goto _exit;
}
2022-08-15 04:20:27 -05:00
#else
sw_gain_iir.history_x[0] = 0.0f;
sw_gain_iir.history_x[1] = 0.0f;
sw_gain_iir.history_y[0] = 0.0f;
sw_gain_iir.history_y[1] = 0.0f;
#endif
2022-08-15 04:20:27 -05:00
#endif
#ifdef CODEC_DSD
if (af_dsd_enabled) {
dma_cfg->dst_periph = HAL_AUDMA_DSD_TX;
dma_cfg->dst_bsize = HAL_DMA_BSIZE_1;
} else
#endif
{
dma_cfg->dst_periph = HAL_AUDMA_CODEC_TX;
}
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_SPDIF
else if (device == AUD_STREAM_USE_INT_SPDIF) {
AF_TRACE_DEBUG();
hal_spdif_open(AF_SPDIF_INST, stream);
dma_cfg->dst_periph = HAL_AUDMA_SPDIF0_TX;
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_BT_PCM
else if (device == AUD_STREAM_USE_BT_PCM) {
AF_TRACE_DEBUG();
hal_btpcm_open(AF_BTPCM_INST, stream);
dma_cfg->dst_periph = HAL_AUDMA_BTPCM_TX;
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AUDIO_ANC_FB_MC
else if (device == AUD_STREAM_USE_MC) {
AF_TRACE_DEBUG();
dma_cfg->dst_periph = HAL_AUDMA_MC_RX;
}
2022-08-15 04:20:27 -05:00
#endif
else {
ASSERT(0, "[%s] ERROR: device %d is not defined!", __func__, device);
2022-08-15 04:20:27 -05:00
}
dma_cfg->ch = hal_audma_get_chan(dma_cfg->dst_periph, HAL_DMA_HIGH_PRIO);
} else {
AF_TRACE_DEBUG();
dma_cfg->dst_periph = (enum HAL_DMA_PERIPH_T)0;
dma_cfg->type = HAL_DMA_FLOW_P2M_DMA;
// open device and stream
if (0) {
}
2022-08-15 04:20:27 -05:00
#ifdef AF_DEVICE_EXT_CODEC
else if (device == AUD_STREAM_USE_EXT_CODEC) {
AF_TRACE_DEBUG();
tlv32aic32_open();
tlv32aic32_stream_open(stream);
2022-08-15 04:20:27 -05:00
dma_cfg->src_periph = HAL_AUDMA_I2S0_RX;
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_I2S
else if (device == AUD_STREAM_USE_I2S0_MASTER) {
AF_TRACE_DEBUG();
hal_i2s_open(HAL_I2S_ID_0, stream, HAL_I2S_MODE_MASTER);
dma_cfg->src_periph = HAL_AUDMA_I2S0_RX;
} else if (device == AUD_STREAM_USE_I2S0_SLAVE) {
AF_TRACE_DEBUG();
hal_i2s_open(HAL_I2S_ID_0, stream, HAL_I2S_MODE_SLAVE);
dma_cfg->src_periph = HAL_AUDMA_I2S0_RX;
}
2022-08-15 04:20:27 -05:00
#if defined(CHIP_HAS_I2S) && (CHIP_HAS_I2S > 1)
else if (device == AUD_STREAM_USE_I2S1_MASTER) {
AF_TRACE_DEBUG();
hal_i2s_open(HAL_I2S_ID_1, stream, HAL_I2S_MODE_MASTER);
dma_cfg->src_periph = HAL_AUDMA_I2S1_RX;
} else if (device == AUD_STREAM_USE_I2S1_SLAVE) {
AF_TRACE_DEBUG();
hal_i2s_open(HAL_I2S_ID_1, stream, HAL_I2S_MODE_SLAVE);
dma_cfg->src_periph = HAL_AUDMA_I2S1_RX;
}
2022-08-15 04:20:27 -05:00
#endif
#endif
#ifdef AF_DEVICE_TDM
else if (device == AUD_STREAM_USE_TDM0_MASTER) {
AF_TRACE_DEBUG();
hal_tdm_open(HAL_I2S_ID_0, stream, HAL_I2S_MODE_MASTER);
dma_cfg->src_periph = HAL_AUDMA_I2S0_RX;
AF_TRACE_DEBUG();
} else if (device == AUD_STREAM_USE_TDM0_SLAVE) {
AF_TRACE_DEBUG();
hal_tdm_open(HAL_I2S_ID_0, stream, HAL_I2S_MODE_SLAVE);
dma_cfg->src_periph = HAL_AUDMA_I2S0_RX;
}
2022-08-15 04:20:27 -05:00
#if defined(CHIP_HAS_I2S) && (CHIP_HAS_I2S > 1)
else if (device == AUD_STREAM_USE_TDM1_MASTER) {
AF_TRACE_DEBUG();
hal_tdm_open(HAL_I2S_ID_1, stream, HAL_I2S_MODE_MASTER);
dma_cfg->src_periph = HAL_AUDMA_I2S1_RX;
} else if (device == AUD_STREAM_USE_TDM1_SLAVE) {
AF_TRACE_DEBUG();
hal_tdm_open(HAL_I2S_ID_1, stream, HAL_I2S_MODE_SLAVE);
dma_cfg->src_periph = HAL_AUDMA_I2S1_RX;
}
2022-08-15 04:20:27 -05:00
#endif
#endif
#ifdef AF_DEVICE_INT_CODEC
else if (device == AUD_STREAM_USE_INT_CODEC) {
AF_TRACE_DEBUG();
codec_int_open();
codec_int_stream_open(stream);
2022-08-15 04:20:27 -05:00
dma_cfg->src_periph = HAL_AUDMA_CODEC_RX;
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_SPDIF
else if (device == AUD_STREAM_USE_INT_SPDIF) {
AF_TRACE_DEBUG();
hal_spdif_open(AF_SPDIF_INST, stream);
2022-08-15 04:20:27 -05:00
dma_cfg->src_periph = HAL_AUDMA_SPDIF0_RX;
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_BT_PCM
else if (device == AUD_STREAM_USE_BT_PCM) {
AF_TRACE_DEBUG();
hal_btpcm_open(AF_BTPCM_INST, stream);
2022-08-15 04:20:27 -05:00
dma_cfg->src_periph = HAL_AUDMA_BTPCM_RX;
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_DPD_RX
else if (device == AUD_STREAM_USE_DPD_RX) {
AF_TRACE_DEBUG();
dma_cfg->src_periph = HAL_AUDMA_DPD_RX;
}
2022-08-15 04:20:27 -05:00
#endif
else {
ASSERT(0, "[%s] ERROR: device %d is not defined!", __func__, device);
2022-08-15 04:20:27 -05:00
}
dma_cfg->ch = hal_audma_get_chan(dma_cfg->src_periph, HAL_DMA_HIGH_PRIO);
}
af_stream_update_dma_buffer(stream, role, cfg);
role->handler = cfg->handler;
af_set_status(id, stream, AF_STATUS_STREAM_OPEN_CLOSE);
2022-08-15 04:20:27 -05:00
#ifndef RTOS
af_clear_flag(&af_flag_signal, 1 << (id * 2 + stream));
af_set_flag(&af_flag_open, 1 << (id * 2 + stream));
2022-08-15 04:20:27 -05:00
#endif
AF_TRACE_DEBUG();
ret = AF_RES_SUCCESS;
2022-08-15 04:20:27 -05:00
_exit:
af_unlock_thread();
if (ret == AF_RES_SUCCESS) {
af_stream_setup(id, stream, &role->cfg);
}
2022-08-15 04:20:27 -05:00
return ret;
2022-08-15 04:20:27 -05:00
}
// volume, path, sample rate, channel num ...
uint32_t af_stream_setup(enum AUD_STREAM_ID_T id, enum AUD_STREAM_T stream,
const struct AF_STREAM_CONFIG_T *cfg) {
AF_TRACE_DEBUG();
2022-08-15 04:20:27 -05:00
struct af_stream_cfg_t *role;
enum AF_RESULT_T ret;
enum AUD_STREAM_USE_DEVICE_T device;
2022-08-15 04:20:27 -05:00
role = af_get_stream_role(id, stream);
TRACE(3, "[%s] id = %d, stream = %d", __func__, id, stream);
2022-08-15 04:20:27 -05:00
ret = AF_RES_FAILD;
2022-08-15 04:20:27 -05:00
af_lock_thread();
2022-08-15 04:20:27 -05:00
// check stream is open
if (!(role->ctl.status & AF_STATUS_STREAM_OPEN_CLOSE)) {
TRACE(2, "[%s] ERROR: status = %d", __func__, role->ctl.status);
goto _exit;
}
2022-08-15 04:20:27 -05:00
device = role->ctl.use_device;
2022-08-15 04:20:27 -05:00
if (&role->cfg != cfg) {
bool update_dma = false;
2022-08-15 04:20:27 -05:00
if (role->cfg.bits != cfg->bits) {
TRACE(4, "[%s] Change bits from %d to %d for stream %d", __func__,
role->cfg.bits, cfg->bits, stream);
update_dma = true;
}
if (role->cfg.data_ptr != cfg->data_ptr) {
TRACE(4, "[%s] Change data_ptr from %p to %p for stream %d", __func__,
role->cfg.data_ptr, cfg->data_ptr, stream);
update_dma = true;
}
if (role->cfg.data_size != cfg->data_size) {
TRACE(4, "[%s] Change data_size from %d to %d for stream %d", __func__,
role->cfg.data_size, cfg->data_size, stream);
update_dma = true;
}
if (update_dma) {
// To avoid FIFO corruption, streams must be stopped before changing
// sample size
ASSERT((role->ctl.status & AF_STATUS_STREAM_START_STOP) == 0,
"[%s] ERROR: Update dma while stream %d started", __func__,
stream);
2022-08-15 04:20:27 -05:00
af_stream_update_dma_buffer(stream, role, cfg);
}
2022-08-15 04:20:27 -05:00
if (role->cfg.sample_rate != cfg->sample_rate) {
TRACE(4, "[%s] Change sample rate from %d to %d for stream %d", __func__,
role->cfg.sample_rate, cfg->sample_rate, stream);
2022-08-15 04:20:27 -05:00
// To avoid L/R sample misalignment, streams must be stopped before
// changing sample rate
ASSERT((role->ctl.status & AF_STATUS_STREAM_START_STOP) == 0,
"[%s] ERROR: Change sample rate from %d to %d while stream %d "
"started",
__func__, role->cfg.sample_rate, cfg->sample_rate, stream);
2022-08-15 04:20:27 -05:00
}
role->cfg = *cfg;
}
AF_TRACE_DEBUG();
if (0) {
}
2022-08-15 04:20:27 -05:00
#ifdef AF_DEVICE_EXT_CODEC
else if (device == AUD_STREAM_USE_EXT_CODEC) {
AF_TRACE_DEBUG();
2022-08-15 04:20:27 -05:00
struct tlv32aic32_config_t tlv32aic32_cfg;
2022-08-15 04:20:27 -05:00
memset(&tlv32aic32_cfg, 0, sizeof(tlv32aic32_cfg));
tlv32aic32_cfg.bits = cfg->bits;
tlv32aic32_cfg.channel_num = cfg->channel_num;
tlv32aic32_cfg.channel_map = cfg->channel_map;
tlv32aic32_cfg.sample_rate = cfg->sample_rate;
tlv32aic32_cfg.use_dma = AF_TRUE;
tlv32aic32_cfg.chan_sep_buf = cfg->chan_sep_buf;
tlv32aic32_cfg.sync_start = cfg->sync_start;
tlv32aic32_cfg.slot_cycles = cfg->slot_cycles;
tlv32aic32_stream_setup(stream, &tlv32aic32_cfg);
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_I2S
else if (device == AUD_STREAM_USE_I2S0_MASTER ||
device == AUD_STREAM_USE_I2S0_SLAVE
2022-08-15 04:20:27 -05:00
#if defined(CHIP_HAS_I2S) && (CHIP_HAS_I2S > 1)
|| device == AUD_STREAM_USE_I2S1_MASTER ||
device == AUD_STREAM_USE_I2S1_SLAVE
2022-08-15 04:20:27 -05:00
#endif
) {
AF_TRACE_DEBUG();
2022-08-15 04:20:27 -05:00
struct HAL_I2S_CONFIG_T i2s_cfg;
enum HAL_I2S_ID_T i2s_id;
2022-08-15 04:20:27 -05:00
i2s_id = HAL_I2S_ID_0;
2022-08-15 04:20:27 -05:00
#if defined(CHIP_HAS_I2S) && (CHIP_HAS_I2S > 1)
if (device == AUD_STREAM_USE_I2S1_MASTER ||
device == AUD_STREAM_USE_I2S1_SLAVE) {
i2s_id = HAL_I2S_ID_1;
}
2022-08-15 04:20:27 -05:00
#endif
memset(&i2s_cfg, 0, sizeof(i2s_cfg));
i2s_cfg.use_dma = AF_TRUE;
i2s_cfg.chan_sep_buf = cfg->chan_sep_buf;
i2s_cfg.sync_start = cfg->sync_start;
i2s_cfg.cycles = cfg->slot_cycles;
i2s_cfg.bits = cfg->bits;
i2s_cfg.channel_num = cfg->channel_num;
i2s_cfg.channel_map = cfg->channel_map;
i2s_cfg.sample_rate = cfg->sample_rate;
hal_i2s_setup_stream(i2s_id, stream, &i2s_cfg);
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_TDM
else if (device == AUD_STREAM_USE_TDM0_MASTER ||
device == AUD_STREAM_USE_TDM0_SLAVE
2022-08-15 04:20:27 -05:00
#if defined(CHIP_HAS_I2S) && (CHIP_HAS_I2S > 1)
|| device == AUD_STREAM_USE_TDM1_MASTER ||
device == AUD_STREAM_USE_TDM1_SLAVE
2022-08-15 04:20:27 -05:00
#endif
) {
AF_TRACE_DEBUG();
2022-08-15 04:20:27 -05:00
struct HAL_TDM_CONFIG_T tdm_cfg;
enum HAL_I2S_ID_T i2s_id;
2022-08-15 04:20:27 -05:00
i2s_id = HAL_I2S_ID_0;
2022-08-15 04:20:27 -05:00
#if defined(CHIP_HAS_I2S) && (CHIP_HAS_I2S > 1)
if (device == AUD_STREAM_USE_TDM1_MASTER ||
device == AUD_STREAM_USE_TDM1_SLAVE) {
i2s_id = HAL_I2S_ID_1;
}
2022-08-15 04:20:27 -05:00
#endif
memset(&tdm_cfg, 0, sizeof(tdm_cfg));
tdm_cfg.mode = HAL_TDM_MODE_FS_ASSERTED_AT_FIRST;
tdm_cfg.edge = HAL_TDM_FS_EDGE_POSEDGE;
tdm_cfg.cycles = (cfg->channel_num * cfg->slot_cycles);
tdm_cfg.fs_cycles = cfg->fs_cycles;
tdm_cfg.slot_cycles = cfg->slot_cycles;
tdm_cfg.data_offset = 0;
tdm_cfg.sync_start = cfg->sync_start;
TRACE(5, "%s: cycle = %d, fs_cycles = %d, slot_cycles = %d,wait = %d.",
__func__, tdm_cfg.cycles, tdm_cfg.fs_cycles, tdm_cfg.slot_cycles,
tdm_cfg.sync_start);
hal_tdm_setup_stream(i2s_id, stream, cfg->sample_rate, &tdm_cfg);
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_INT_CODEC
else if (device == AUD_STREAM_USE_INT_CODEC) {
AF_TRACE_DEBUG();
struct HAL_CODEC_CONFIG_T codec_cfg;
memset(&codec_cfg, 0, sizeof(codec_cfg));
codec_cfg.bits = cfg->bits;
codec_cfg.sample_rate = cfg->sample_rate;
codec_cfg.channel_num = cfg->channel_num;
codec_cfg.channel_map = cfg->channel_map;
codec_cfg.use_dma = AF_TRUE;
codec_cfg.vol = cfg->vol;
codec_cfg.io_path = cfg->io_path;
2022-08-15 04:20:27 -05:00
AF_TRACE_DEBUG();
codec_int_stream_setup(stream, &codec_cfg);
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_SPDIF
else if (device == AUD_STREAM_USE_INT_SPDIF) {
AF_TRACE_DEBUG();
struct HAL_SPDIF_CONFIG_T spdif_cfg;
2022-08-15 04:20:27 -05:00
memset(&spdif_cfg, 0, sizeof(spdif_cfg));
spdif_cfg.use_dma = AF_TRUE;
spdif_cfg.bits = cfg->bits;
spdif_cfg.channel_num = cfg->channel_num;
spdif_cfg.sample_rate = cfg->sample_rate;
hal_spdif_setup_stream(AF_SPDIF_INST, stream, &spdif_cfg);
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_BT_PCM
else if (device == AUD_STREAM_USE_BT_PCM) {
AF_TRACE_DEBUG();
struct HAL_BTPCM_CONFIG_T btpcm_cfg;
2022-08-15 04:20:27 -05:00
memset(&btpcm_cfg, 0, sizeof(btpcm_cfg));
btpcm_cfg.use_dma = AF_TRUE;
btpcm_cfg.bits = cfg->bits;
btpcm_cfg.channel_num = cfg->channel_num;
btpcm_cfg.sample_rate = cfg->sample_rate;
hal_btpcm_setup_stream(AF_BTPCM_INST, stream, &btpcm_cfg);
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_DPD_RX
else if (device == AUD_STREAM_USE_DPD_RX) {
AF_TRACE_DEBUG();
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AUDIO_ANC_FB_MC
else if (device == AUD_STREAM_USE_MC) {
AF_TRACE_DEBUG();
hal_codec_setup_mc(cfg->channel_num, cfg->bits);
}
2022-08-15 04:20:27 -05:00
#endif
else {
ASSERT(0, "[%s] ERROR: device %d is not defined!", __func__, device);
}
2022-08-15 04:20:27 -05:00
AF_TRACE_DEBUG();
2022-08-15 04:20:27 -05:00
ret = AF_RES_SUCCESS;
2022-08-15 04:20:27 -05:00
_exit:
af_unlock_thread();
2022-08-15 04:20:27 -05:00
return ret;
2022-08-15 04:20:27 -05:00
}
uint32_t af_stream_mute(enum AUD_STREAM_ID_T id, enum AUD_STREAM_T stream,
bool mute) {
AF_TRACE_DEBUG();
2022-08-15 04:20:27 -05:00
struct af_stream_cfg_t *role;
enum AF_RESULT_T ret;
enum AUD_STREAM_USE_DEVICE_T device;
2022-08-15 04:20:27 -05:00
ret = AF_RES_FAILD;
role = af_get_stream_role(id, stream);
2022-08-15 04:20:27 -05:00
af_lock_thread();
2022-08-15 04:20:27 -05:00
if ((role->ctl.status & AF_STATUS_STREAM_OPEN_CLOSE) == 0) {
TRACE(2, "[%s] ERROR: status = %d", __func__, role->ctl.status);
goto _exit;
}
2022-08-15 04:20:27 -05:00
device = role->ctl.use_device;
2022-08-15 04:20:27 -05:00
if (device == AUD_STREAM_USE_INT_CODEC) {
codec_int_stream_mute(stream, mute);
ret = AF_RES_SUCCESS;
}
2022-08-15 04:20:27 -05:00
_exit:
af_unlock_thread();
2022-08-15 04:20:27 -05:00
return ret;
2022-08-15 04:20:27 -05:00
}
uint32_t af_stream_set_chan_vol(enum AUD_STREAM_ID_T id,
enum AUD_STREAM_T stream,
enum AUD_CHANNEL_MAP_T ch_map, uint8_t vol) {
AF_TRACE_DEBUG();
2022-08-15 04:20:27 -05:00
struct af_stream_cfg_t *role;
enum AF_RESULT_T ret;
enum AUD_STREAM_USE_DEVICE_T device;
2022-08-15 04:20:27 -05:00
ret = AF_RES_FAILD;
role = af_get_stream_role(id, stream);
2022-08-15 04:20:27 -05:00
af_lock_thread();
2022-08-15 04:20:27 -05:00
if ((role->ctl.status & AF_STATUS_STREAM_OPEN_CLOSE) == 0) {
TRACE(2, "[%s] ERROR: status = %d", __func__, role->ctl.status);
goto _exit;
}
2022-08-15 04:20:27 -05:00
device = role->ctl.use_device;
2022-08-15 04:20:27 -05:00
if (device == AUD_STREAM_USE_INT_CODEC) {
codec_int_stream_set_chan_vol(stream, ch_map, vol);
ret = AF_RES_SUCCESS;
}
2022-08-15 04:20:27 -05:00
_exit:
af_unlock_thread();
2022-08-15 04:20:27 -05:00
return ret;
2022-08-15 04:20:27 -05:00
}
uint32_t af_stream_restore_chan_vol(enum AUD_STREAM_ID_T id,
enum AUD_STREAM_T stream) {
AF_TRACE_DEBUG();
2022-08-15 04:20:27 -05:00
struct af_stream_cfg_t *role;
enum AF_RESULT_T ret;
enum AUD_STREAM_USE_DEVICE_T device;
2022-08-15 04:20:27 -05:00
ret = AF_RES_FAILD;
role = af_get_stream_role(id, stream);
2022-08-15 04:20:27 -05:00
af_lock_thread();
2022-08-15 04:20:27 -05:00
if ((role->ctl.status & AF_STATUS_STREAM_OPEN_CLOSE) == 0) {
TRACE(2, "[%s] ERROR: status = %d", __func__, role->ctl.status);
goto _exit;
}
2022-08-15 04:20:27 -05:00
device = role->ctl.use_device;
2022-08-15 04:20:27 -05:00
if (device == AUD_STREAM_USE_INT_CODEC) {
codec_int_stream_restore_chan_vol(stream);
ret = AF_RES_SUCCESS;
}
2022-08-15 04:20:27 -05:00
_exit:
af_unlock_thread();
2022-08-15 04:20:27 -05:00
return ret;
2022-08-15 04:20:27 -05:00
}
#ifdef CODEC_PLAY_BEFORE_CAPTURE
static struct af_stream_cfg_t *codec_capture_role;
static void af_codec_stream_pre_start(enum AUD_STREAM_T stream) {
struct af_stream_cfg_t *role = NULL;
2022-08-15 04:20:27 -05:00
if (stream == AUD_STREAM_CAPTURE) {
return;
}
2022-08-15 04:20:27 -05:00
for (uint8_t id = 0; id < AUD_STREAM_ID_NUM; id++) {
role = af_get_stream_role((enum AUD_STREAM_ID_T)id, AUD_STREAM_CAPTURE);
if (role->cfg.device == AUD_STREAM_USE_INT_CODEC &&
role->ctl.status ==
(AF_STATUS_OPEN_CLOSE | AF_STATUS_STREAM_OPEN_CLOSE |
AF_STATUS_STREAM_START_STOP)) {
hal_audma_stop(role->dma_cfg.ch);
codec_int_stream_stop(AUD_STREAM_CAPTURE);
codec_capture_role = role;
return;
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
}
static void af_codec_stream_post_start(enum AUD_STREAM_T stream) {
if (stream == AUD_STREAM_CAPTURE) {
return;
}
2022-08-15 04:20:27 -05:00
if (codec_capture_role) {
hal_audma_sg_start(&codec_capture_role->dma_desc[0],
&codec_capture_role->dma_cfg);
codec_int_stream_start(AUD_STREAM_CAPTURE);
codec_capture_role = NULL;
}
2022-08-15 04:20:27 -05:00
}
static void af_codec_stream_pre_stop(enum AUD_STREAM_T stream) {
struct af_stream_cfg_t *role = NULL;
2022-08-15 04:20:27 -05:00
if (stream == AUD_STREAM_CAPTURE) {
return;
}
2022-08-15 04:20:27 -05:00
for (uint8_t id = 0; id < AUD_STREAM_ID_NUM; id++) {
role = af_get_stream_role((enum AUD_STREAM_ID_T)id, AUD_STREAM_CAPTURE);
if (role->cfg.device == AUD_STREAM_USE_INT_CODEC &&
role->ctl.status ==
(AF_STATUS_OPEN_CLOSE | AF_STATUS_STREAM_OPEN_CLOSE |
AF_STATUS_STREAM_START_STOP)) {
hal_audma_stop(role->dma_cfg.ch);
codec_int_stream_stop(AUD_STREAM_CAPTURE);
codec_capture_role = role;
return;
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
}
static void af_codec_stream_post_stop(enum AUD_STREAM_T stream) {
if (stream == AUD_STREAM_CAPTURE) {
return;
}
2022-08-15 04:20:27 -05:00
if (codec_capture_role) {
hal_audma_sg_start(&codec_capture_role->dma_desc[0],
&codec_capture_role->dma_cfg);
codec_int_stream_start(AUD_STREAM_CAPTURE);
codec_capture_role = NULL;
}
2022-08-15 04:20:27 -05:00
}
#endif
uint32_t af_stream_start(enum AUD_STREAM_ID_T id, enum AUD_STREAM_T stream) {
struct af_stream_cfg_t *role;
enum AF_RESULT_T ret;
enum AUD_STREAM_USE_DEVICE_T device;
enum HAL_DMA_RET_T dma_ret;
2022-08-15 04:20:27 -05:00
role = af_get_stream_role(id, stream);
TRACE(3, "[%s] id = %d, stream = %d", __func__, id, stream);
2022-08-15 04:20:27 -05:00
af_lock_thread();
2022-08-15 04:20:27 -05:00
// check stream is open and not start.
if (role->ctl.status !=
(AF_STATUS_OPEN_CLOSE | AF_STATUS_STREAM_OPEN_CLOSE)) {
TRACE(2, "[%s] ERROR: status = %d", __func__, role->ctl.status);
ret = AF_RES_FAILD;
goto _exit;
}
2022-08-15 04:20:27 -05:00
device = role->ctl.use_device;
2022-08-15 04:20:27 -05:00
role->ctl.pp_index = PP_PING;
role->ctl.pp_cnt = 0;
2022-08-15 04:20:27 -05:00
#ifndef RTOS
af_clear_flag(&af_flag_signal, 1 << (id * 2 + stream));
2022-08-15 04:20:27 -05:00
#endif
if (device == AUD_STREAM_USE_INT_CODEC && stream == AUD_STREAM_PLAYBACK) {
2022-08-15 04:20:27 -05:00
#ifdef AUDIO_OUTPUT_PA_ON_FADE_IN
dac_pa_state = AF_DAC_PA_ON_TRIGGER;
// Has the buffer been zero-ed out?
af_zero_mem(role->dma_buf_ptr, role->dma_buf_size);
2022-08-15 04:20:27 -05:00
#ifdef AUDIO_OUTPUT_DC_CALIB_SW
af_codec_playback_sw_dc_calib(role->dma_buf_ptr, role->dma_buf_size,
role->cfg.bits, role->cfg.channel_num);
2022-08-15 04:20:27 -05:00
#endif
dac_dc_start_time = hal_sys_timer_get();
2022-08-15 04:20:27 -05:00
#endif
}
2022-08-15 04:20:27 -05:00
#ifndef CHIP_BEST1000
if (role->cfg.chan_sep_buf && role->cfg.channel_num > AUD_CHANNEL_NUM_1) {
struct HAL_DMA_2D_CFG_T *src, *dst;
struct HAL_DMA_2D_CFG_T dma_2d_cfg;
uint8_t burst_size = 1;
uint32_t chan_xfer_cnt;
src = NULL;
dst = NULL;
if (stream == AUD_STREAM_PLAYBACK) {
if (role->cfg.channel_num == AUD_CHANNEL_NUM_2) {
ASSERT(role->dma_cfg.src_bsize == HAL_DMA_BSIZE_1,
"Play 2D DMA: Bad src burst size: %d", role->dma_cfg.src_bsize);
burst_size = 1;
src = &dma_2d_cfg;
}
} else {
if (role->cfg.channel_num > AUD_CHANNEL_NUM_1) {
ASSERT(role->dma_cfg.src_bsize == role->dma_cfg.dst_bsize,
"Cap 2D DMA: src burst size (%d) != dst (%d)",
role->dma_cfg.src_bsize, role->dma_cfg.dst_bsize);
if (role->dma_cfg.dst_bsize == HAL_DMA_BSIZE_1) {
burst_size = 1;
} else if (role->dma_cfg.dst_bsize == HAL_DMA_BSIZE_4) {
burst_size = 4;
} else if (role->dma_cfg.dst_bsize == HAL_DMA_BSIZE_8) {
burst_size = 8;
2022-08-15 04:20:27 -05:00
} else {
ASSERT(false, "Cap 2D DMA: Bad dst burst size: %d",
role->dma_cfg.dst_bsize);
2022-08-15 04:20:27 -05:00
}
dst = &dma_2d_cfg;
}
}
2022-08-15 04:20:27 -05:00
if (src || dst) {
chan_xfer_cnt =
role->dma_cfg.src_tsize * AUDIO_BUFFER_COUNT / role->cfg.channel_num;
dma_2d_cfg.xcount = role->cfg.channel_num;
dma_2d_cfg.xmodify = chan_xfer_cnt - burst_size;
dma_2d_cfg.ycount = chan_xfer_cnt / burst_size;
dma_2d_cfg.ymodify = -chan_xfer_cnt * (role->cfg.channel_num - 1);
}
2022-08-15 04:20:27 -05:00
dma_ret = hal_dma_sg_2d_start(&role->dma_desc[0], &role->dma_cfg, src, dst);
} else
2022-08-15 04:20:27 -05:00
#endif
{
dma_ret = hal_dma_sg_start(&role->dma_desc[0], &role->dma_cfg);
}
ASSERT(dma_ret == HAL_DMA_OK,
"[%s] Failed to start dma for stream %d: ret=%d", __func__, stream,
dma_ret);
2022-08-15 04:20:27 -05:00
AF_TRACE_DEBUG();
if (0) {
}
2022-08-15 04:20:27 -05:00
#ifdef AF_DEVICE_EXT_CODEC
else if (device == AUD_STREAM_USE_EXT_CODEC) {
AF_TRACE_DEBUG();
tlv32aic32_stream_start(stream);
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_I2S
else if (device == AUD_STREAM_USE_I2S0_MASTER ||
device == AUD_STREAM_USE_I2S0_SLAVE) {
hal_i2s_start_stream(HAL_I2S_ID_0, stream);
}
2022-08-15 04:20:27 -05:00
#if defined(CHIP_HAS_I2S) && (CHIP_HAS_I2S > 1)
else if (device == AUD_STREAM_USE_I2S1_MASTER ||
device == AUD_STREAM_USE_I2S1_SLAVE) {
hal_i2s_start_stream(HAL_I2S_ID_1, stream);
}
2022-08-15 04:20:27 -05:00
#endif
#endif
#ifdef AF_DEVICE_TDM
else if (device == AUD_STREAM_USE_TDM0_MASTER ||
device == AUD_STREAM_USE_TDM0_SLAVE) {
hal_tdm_start_stream(HAL_I2S_ID_0, stream);
}
2022-08-15 04:20:27 -05:00
#if defined(CHIP_HAS_I2S) && (CHIP_HAS_I2S > 1)
else if (device == AUD_STREAM_USE_TDM1_MASTER ||
device == AUD_STREAM_USE_TDM1_SLAVE) {
hal_tdm_start_stream(HAL_I2S_ID_1, stream);
}
2022-08-15 04:20:27 -05:00
#endif
#endif
#ifdef AF_DEVICE_INT_CODEC
else if (device == AUD_STREAM_USE_INT_CODEC) {
AF_TRACE_DEBUG();
2022-08-15 04:20:27 -05:00
#ifdef CODEC_PLAY_BEFORE_CAPTURE
af_codec_stream_pre_start(stream);
2022-08-15 04:20:27 -05:00
#endif
codec_int_stream_start(stream);
2022-08-15 04:20:27 -05:00
#ifdef CODEC_PLAY_BEFORE_CAPTURE
af_codec_stream_post_start(stream);
2022-08-15 04:20:27 -05:00
#endif
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_SPDIF
else if (device == AUD_STREAM_USE_INT_SPDIF) {
AF_TRACE_DEBUG();
hal_spdif_start_stream(AF_SPDIF_INST, stream);
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_BT_PCM
else if (device == AUD_STREAM_USE_BT_PCM) {
AF_TRACE_DEBUG();
hal_btpcm_start_stream(AF_BTPCM_INST, stream);
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_DPD_RX
else if (device == AUD_STREAM_USE_DPD_RX) {
AF_TRACE_DEBUG();
hal_btpcm_start_stream(AF_BTPCM_INST, stream);
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AUDIO_ANC_FB_MC
else if (device == AUD_STREAM_USE_MC) {
AF_TRACE_DEBUG();
}
2022-08-15 04:20:27 -05:00
#endif
else {
ASSERT(0, "[%s] ERROR: device %d is not defined!", __func__, device);
}
2022-08-15 04:20:27 -05:00
AF_TRACE_DEBUG();
af_set_status(id, stream, AF_STATUS_STREAM_START_STOP);
2022-08-15 04:20:27 -05:00
ret = AF_RES_SUCCESS;
2022-08-15 04:20:27 -05:00
_exit:
af_unlock_thread();
2022-08-15 04:20:27 -05:00
return ret;
2022-08-15 04:20:27 -05:00
}
uint32_t af_stream_stop(enum AUD_STREAM_ID_T id, enum AUD_STREAM_T stream) {
struct af_stream_cfg_t *role;
enum AF_RESULT_T ret;
enum AUD_STREAM_USE_DEVICE_T device;
2022-08-15 04:20:27 -05:00
role = af_get_stream_role(id, stream);
TRACE(3, "[%s] id = %d, stream = %d", __func__, id, stream);
2022-08-15 04:20:27 -05:00
af_lock_thread();
2022-08-15 04:20:27 -05:00
device = role->ctl.use_device;
2022-08-15 04:20:27 -05:00
if (device == AUD_STREAM_USE_INT_CODEC &&
role->ctl.status ==
(AF_STATUS_OPEN_CLOSE | AF_STATUS_STREAM_OPEN_CLOSE |
AF_STATUS_STREAM_START_STOP | AF_STATUS_STREAM_PAUSE_RESTART)) {
af_clear_status(id, stream, AF_STATUS_STREAM_PAUSE_RESTART);
goto _pause_stop;
}
2022-08-15 04:20:27 -05:00
// check stream is start and not stop
if (role->ctl.status != (AF_STATUS_OPEN_CLOSE | AF_STATUS_STREAM_OPEN_CLOSE |
AF_STATUS_STREAM_START_STOP)) {
TRACE(2, "[%s] ERROR: status = %d", __func__, role->ctl.status);
ret = AF_RES_FAILD;
goto _exit;
}
2022-08-15 04:20:27 -05:00
#if defined(RTOS) && defined(AF_STREAM_ID_0_PLAYBACK_FADEOUT)
if (id == AUD_STREAM_ID_0 && stream == AUD_STREAM_PLAYBACK) {
af_stream_fadeout_start(512);
af_stream_stop_wait_finish();
}
2022-08-15 04:20:27 -05:00
#endif
if (device == AUD_STREAM_USE_INT_CODEC && stream == AUD_STREAM_PLAYBACK) {
2022-08-15 04:20:27 -05:00
#ifdef AUDIO_OUTPUT_PA_OFF_FADE_OUT
dac_pa_state = AF_DAC_PA_OFF_TRIGGER;
af_unlock_thread();
while (dac_pa_state != AF_DAC_PA_NULL) {
2022-08-15 04:20:27 -05:00
#ifdef RTOS
osSignalWait((1 << AF_FADE_OUT_SIGNAL_ID), 300);
osSignalClear(fade_thread_id, (1 << AF_FADE_OUT_SIGNAL_ID));
2022-08-15 04:20:27 -05:00
#else
af_thread(NULL);
2022-08-15 04:20:27 -05:00
#endif
}
af_lock_thread();
2022-08-15 04:20:27 -05:00
#elif defined(AUDIO_OUTPUT_PA_ON_FADE_IN)
dac_pa_state = AF_DAC_PA_NULL;
analog_aud_codec_speaker_enable(false);
dac_pa_stop_time = hal_sys_timer_get();
2022-08-15 04:20:27 -05:00
#endif // !AUDIO_OUTPUT_PA_OFF_FADE_OUT && AUDIO_OUTPUT_PA_ON_FADE_IN
}
2022-08-15 04:20:27 -05:00
hal_audma_stop(role->dma_cfg.ch);
2022-08-15 04:20:27 -05:00
#if defined(RTOS) && defined(AF_STREAM_ID_0_PLAYBACK_FADEOUT)
if (id == AUD_STREAM_ID_0 && stream == AUD_STREAM_PLAYBACK) {
af_stream_fadeout_stop();
}
2022-08-15 04:20:27 -05:00
#endif
if (0) {
}
2022-08-15 04:20:27 -05:00
#ifdef AF_DEVICE_EXT_CODEC
else if (device == AUD_STREAM_USE_EXT_CODEC) {
AF_TRACE_DEBUG();
tlv32aic32_stream_stop(stream);
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_I2S
else if (device == AUD_STREAM_USE_I2S0_MASTER ||
device == AUD_STREAM_USE_I2S0_SLAVE) {
AF_TRACE_DEBUG();
hal_i2s_stop_stream(HAL_I2S_ID_0, stream);
}
2022-08-15 04:20:27 -05:00
#if defined(CHIP_HAS_I2S) && (CHIP_HAS_I2S > 1)
else if (device == AUD_STREAM_USE_I2S1_MASTER ||
device == AUD_STREAM_USE_I2S1_SLAVE) {
AF_TRACE_DEBUG();
hal_i2s_stop_stream(HAL_I2S_ID_1, stream);
}
2022-08-15 04:20:27 -05:00
#endif
#endif
#ifdef AF_DEVICE_TDM
else if (device == AUD_STREAM_USE_TDM0_MASTER ||
device == AUD_STREAM_USE_TDM0_SLAVE) {
AF_TRACE_DEBUG();
hal_tdm_stop_stream(HAL_I2S_ID_0, stream);
}
2022-08-15 04:20:27 -05:00
#if defined(CHIP_HAS_I2S) && (CHIP_HAS_I2S > 1)
else if (device == AUD_STREAM_USE_TDM1_MASTER ||
device == AUD_STREAM_USE_TDM1_SLAVE) {
AF_TRACE_DEBUG();
hal_tdm_stop_stream(HAL_I2S_ID_1, stream);
}
2022-08-15 04:20:27 -05:00
#endif
#endif
#ifdef AF_DEVICE_INT_CODEC
else if (device == AUD_STREAM_USE_INT_CODEC) {
AF_TRACE_DEBUG();
2022-08-15 04:20:27 -05:00
#ifdef CODEC_PLAY_BEFORE_CAPTURE
af_codec_stream_pre_stop(stream);
2022-08-15 04:20:27 -05:00
#endif
codec_int_stream_stop(stream);
2022-08-15 04:20:27 -05:00
#ifdef CODEC_PLAY_BEFORE_CAPTURE
af_codec_stream_post_stop(stream);
2022-08-15 04:20:27 -05:00
#endif
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_SPDIF
else if (device == AUD_STREAM_USE_INT_SPDIF) {
AF_TRACE_DEBUG();
hal_spdif_stop_stream(AF_SPDIF_INST, stream);
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_BT_PCM
else if (device == AUD_STREAM_USE_BT_PCM) {
AF_TRACE_DEBUG();
hal_btpcm_stop_stream(AF_BTPCM_INST, stream);
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_DPD_RX
else if (device == AUD_STREAM_USE_DPD_RX) {
AF_TRACE_DEBUG();
hal_btpcm_stop_stream(AF_BTPCM_INST, stream);
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AUDIO_ANC_FB_MC
else if (device == AUD_STREAM_USE_MC) {
AF_TRACE_DEBUG();
}
2022-08-15 04:20:27 -05:00
#endif
else {
ASSERT(0, "[%s] ERROR: device %d is not defined!", __func__, device);
}
2022-08-15 04:20:27 -05:00
_pause_stop:
AF_TRACE_DEBUG();
af_clear_status(id, stream, AF_STATUS_STREAM_START_STOP);
2022-08-15 04:20:27 -05:00
#ifndef RTOS
af_clear_flag(&af_flag_signal, 1 << (id * 2 + stream));
2022-08-15 04:20:27 -05:00
#endif
ret = AF_RES_SUCCESS;
2022-08-15 04:20:27 -05:00
_exit:
af_unlock_thread();
2022-08-15 04:20:27 -05:00
return ret;
2022-08-15 04:20:27 -05:00
}
uint32_t af_stream_pause(enum AUD_STREAM_ID_T id, enum AUD_STREAM_T stream) {
2022-08-15 04:20:27 -05:00
#ifdef CHIP_BEST1000
struct af_stream_cfg_t *role = NULL;
enum AF_RESULT_T ret;
2022-08-15 04:20:27 -05:00
role = af_get_stream_role(id, stream);
TRACE(3, "[%s] id = %d, stream = %d", __func__, id, stream);
2022-08-15 04:20:27 -05:00
af_lock_thread();
2022-08-15 04:20:27 -05:00
if (role->cfg.device != AUD_STREAM_USE_INT_CODEC) {
ret = AF_RES_FAILD;
goto _exit;
}
if (role->ctl.status != (AF_STATUS_OPEN_CLOSE | AF_STATUS_STREAM_OPEN_CLOSE |
AF_STATUS_STREAM_START_STOP)) {
TRACE(2, "[%s] ERROR: status = %d", __func__, role->ctl.status);
ret = AF_RES_FAILD;
goto _exit;
}
2022-08-15 04:20:27 -05:00
#if defined(AUDIO_OUTPUT_PA_ON_FADE_IN) || defined(AUDIO_OUTPUT_PA_OFF_FADE_OUT)
if (AUD_STREAM_PLAYBACK == stream) {
dac_pa_state = AF_DAC_PA_NULL;
analog_aud_codec_speaker_enable(false);
dac_pa_stop_time = hal_sys_timer_get();
}
2022-08-15 04:20:27 -05:00
#endif
hal_audma_stop(role->dma_cfg.ch);
2022-08-15 04:20:27 -05:00
codec_int_stream_stop(stream);
2022-08-15 04:20:27 -05:00
af_set_status(id, stream, AF_STATUS_STREAM_PAUSE_RESTART);
2022-08-15 04:20:27 -05:00
ret = AF_RES_SUCCESS;
2022-08-15 04:20:27 -05:00
_exit:
af_unlock_thread();
2022-08-15 04:20:27 -05:00
return ret;
2022-08-15 04:20:27 -05:00
#else
return af_stream_stop(id, stream);
2022-08-15 04:20:27 -05:00
#endif
}
uint32_t af_stream_restart(enum AUD_STREAM_ID_T id, enum AUD_STREAM_T stream) {
2022-08-15 04:20:27 -05:00
#ifdef CHIP_BEST1000
struct af_stream_cfg_t *role = NULL;
enum AF_RESULT_T ret;
2022-08-15 04:20:27 -05:00
role = af_get_stream_role(id, stream);
TRACE(3, "[%s] id = %d, stream = %d", __func__, id, stream);
2022-08-15 04:20:27 -05:00
af_lock_thread();
2022-08-15 04:20:27 -05:00
if (role->cfg.device != AUD_STREAM_USE_INT_CODEC) {
ret = AF_RES_FAILD;
goto _exit;
}
if (role->ctl.status !=
(AF_STATUS_OPEN_CLOSE | AF_STATUS_STREAM_OPEN_CLOSE |
AF_STATUS_STREAM_START_STOP | AF_STATUS_STREAM_PAUSE_RESTART)) {
TRACE(2, "[%s] ERROR: status = %d", __func__, role->ctl.status);
ret = AF_RES_FAILD;
goto _exit;
}
2022-08-15 04:20:27 -05:00
role->ctl.pp_index = PP_PING;
role->ctl.pp_cnt = 0;
2022-08-15 04:20:27 -05:00
#ifdef AUDIO_OUTPUT_PA_ON_FADE_IN
if (AUD_STREAM_PLAYBACK == stream) {
dac_pa_state = AF_DAC_PA_ON_TRIGGER;
af_zero_mem(role->dma_buf_ptr, role->dma_buf_size);
2022-08-15 04:20:27 -05:00
#ifdef AUDIO_OUTPUT_DC_CALIB_SW
af_codec_playback_sw_dc_calib(role->dma_buf_ptr, role->dma_buf_size,
role->cfg.bits, role->cfg.channel_num);
2022-08-15 04:20:27 -05:00
#endif
dac_dc_start_time = hal_sys_timer_get();
}
2022-08-15 04:20:27 -05:00
#endif
hal_audma_sg_start(&role->dma_desc[0], &role->dma_cfg);
codec_int_stream_start(stream);
af_clear_status(id, stream, AF_STATUS_STREAM_PAUSE_RESTART);
ret = AF_RES_SUCCESS;
2022-08-15 04:20:27 -05:00
_exit:
af_unlock_thread();
2022-08-15 04:20:27 -05:00
return ret;
2022-08-15 04:20:27 -05:00
#else
return af_stream_start(id, stream);
2022-08-15 04:20:27 -05:00
#endif
}
uint32_t af_stream_close(enum AUD_STREAM_ID_T id, enum AUD_STREAM_T stream) {
struct af_stream_cfg_t *role;
enum AF_RESULT_T ret;
enum AUD_STREAM_USE_DEVICE_T device;
2022-08-15 04:20:27 -05:00
role = af_get_stream_role(id, stream);
TRACE(3, "[%s] id = %d, stream = %d", __func__, id, stream);
2022-08-15 04:20:27 -05:00
af_lock_thread();
2022-08-15 04:20:27 -05:00
// check stream is stop and not close.
if (role->ctl.status !=
(AF_STATUS_OPEN_CLOSE | AF_STATUS_STREAM_OPEN_CLOSE)) {
TRACE(2, "[%s] ERROR: status = %d", __func__, role->ctl.status);
ret = AF_RES_FAILD;
goto _exit;
}
2022-08-15 04:20:27 -05:00
device = role->ctl.use_device;
2022-08-15 04:20:27 -05:00
memset(role->dma_buf_ptr, 0, role->dma_buf_size);
hal_audma_free_chan(role->dma_cfg.ch);
2022-08-15 04:20:27 -05:00
// TODO: more parameter should be set!!!
// memset(role, 0xff, sizeof(struct af_stream_cfg_t));
role->handler = NULL;
role->ctl.pp_index = PP_PING;
role->ctl.use_device = AUD_STREAM_USE_DEVICE_NULL;
role->dma_buf_ptr = NULL;
role->dma_buf_size = 0;
2022-08-15 04:20:27 -05:00
role->dma_cfg.ch = HAL_DMA_CHAN_NONE;
2022-08-15 04:20:27 -05:00
if (0) {
}
2022-08-15 04:20:27 -05:00
#ifdef AF_DEVICE_EXT_CODEC
else if (device == AUD_STREAM_USE_EXT_CODEC) {
AF_TRACE_DEBUG();
tlv32aic32_stream_close(stream);
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_I2S
else if (device == AUD_STREAM_USE_I2S0_MASTER ||
device == AUD_STREAM_USE_I2S0_SLAVE) {
AF_TRACE_DEBUG();
hal_i2s_close(HAL_I2S_ID_0, stream);
}
2022-08-15 04:20:27 -05:00
#if defined(CHIP_HAS_I2S) && (CHIP_HAS_I2S > 1)
else if (device == AUD_STREAM_USE_I2S1_MASTER ||
device == AUD_STREAM_USE_I2S1_SLAVE) {
AF_TRACE_DEBUG();
hal_i2s_close(HAL_I2S_ID_1, stream);
}
2022-08-15 04:20:27 -05:00
#endif
#endif
#ifdef AF_DEVICE_TDM
else if (device == AUD_STREAM_USE_TDM0_MASTER ||
device == AUD_STREAM_USE_TDM0_SLAVE) {
AF_TRACE_DEBUG();
hal_tdm_close(HAL_I2S_ID_0, stream);
}
2022-08-15 04:20:27 -05:00
#if defined(CHIP_HAS_I2S) && (CHIP_HAS_I2S > 1)
else if (device == AUD_STREAM_USE_TDM1_MASTER ||
device == AUD_STREAM_USE_TDM1_SLAVE) {
AF_TRACE_DEBUG();
hal_tdm_close(HAL_I2S_ID_1, stream);
}
2022-08-15 04:20:27 -05:00
#endif
#endif
#ifdef AF_DEVICE_INT_CODEC
else if (device == AUD_STREAM_USE_INT_CODEC) {
AF_TRACE_DEBUG();
codec_int_stream_close(stream);
codec_int_close(CODEC_CLOSE_NORMAL);
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_SPDIF
else if (device == AUD_STREAM_USE_INT_SPDIF) {
AF_TRACE_DEBUG();
hal_spdif_close(AF_SPDIF_INST, stream);
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_BT_PCM
else if (device == AUD_STREAM_USE_BT_PCM) {
AF_TRACE_DEBUG();
hal_btpcm_close(AF_BTPCM_INST, stream);
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AF_DEVICE_DPD_RX
else if (device == AUD_STREAM_USE_DPD_RX) {
AF_TRACE_DEBUG();
}
2022-08-15 04:20:27 -05:00
#endif
#ifdef AUDIO_ANC_FB_MC
else if (device == AUD_STREAM_USE_MC) {
AF_TRACE_DEBUG();
}
2022-08-15 04:20:27 -05:00
#endif
else {
ASSERT(0, "[%s] ERROR: device %d is not defined!", __func__, device);
}
2022-08-15 04:20:27 -05:00
AF_TRACE_DEBUG();
af_clear_status(id, stream, AF_STATUS_STREAM_OPEN_CLOSE);
2022-08-15 04:20:27 -05:00
#ifndef RTOS
af_clear_flag(&af_flag_open, 1 << (id * 2 + stream));
if (af_flag_open == 0) {
hal_cpu_wake_unlock(AF_CPU_WAKE_USER);
}
2022-08-15 04:20:27 -05:00
#endif
ret = AF_RES_SUCCESS;
2022-08-15 04:20:27 -05:00
_exit:
af_unlock_thread();
2022-08-15 04:20:27 -05:00
return ret;
2022-08-15 04:20:27 -05:00
}
uint32_t af_close(void) {
struct af_stream_cfg_t *role;
2022-08-15 04:20:27 -05:00
// Avoid blocking shutdown process
// af_lock_thread();
2022-08-15 04:20:27 -05:00
for (uint8_t id = 0; id < AUD_STREAM_ID_NUM; id++) {
for (uint8_t stream = 0; stream < AUD_STREAM_NUM; stream++) {
role = af_get_stream_role((enum AUD_STREAM_ID_T)id,
(enum AUD_STREAM_T)stream);
role->ctl.status = AF_STATUS_NULL;
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
codec_int_close(CODEC_CLOSE_FORCED);
2022-08-15 04:20:27 -05:00
// af_unlock_thread();
2022-08-15 04:20:27 -05:00
return AF_RES_SUCCESS;
2022-08-15 04:20:27 -05:00
}
uint32_t af_stream_get_cur_dma_addr(enum AUD_STREAM_ID_T id,
enum AUD_STREAM_T stream) {
struct af_stream_cfg_t *role;
int i;
uint32_t addr = 0;
role = af_get_stream_role(id, stream);
// check stream is start and not stop
if (role->ctl.status == (AF_STATUS_OPEN_CLOSE | AF_STATUS_STREAM_OPEN_CLOSE |
AF_STATUS_STREAM_START_STOP)) {
if (role->dma_cfg.ch != HAL_DMA_CHAN_NONE) {
for (i = 0; i < 2; i++) {
if (stream == AUD_STREAM_PLAYBACK) {
addr = hal_audma_get_cur_src_addr(role->dma_cfg.ch);
} else {
addr = hal_audma_get_cur_dst_addr(role->dma_cfg.ch);
}
if (addr) {
break;
2022-08-15 04:20:27 -05:00
}
// Previous link list item was just finished. Read current DMA address
// again.
}
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
return addr;
2022-08-15 04:20:27 -05:00
}
int af_stream_get_cur_dma_pos(enum AUD_STREAM_ID_T id,
enum AUD_STREAM_T stream) {
struct af_stream_cfg_t *role;
uint32_t addr;
int pos;
2022-08-15 04:20:27 -05:00
role = af_get_stream_role(id, stream);
2022-08-15 04:20:27 -05:00
addr = af_stream_get_cur_dma_addr(id, stream);
2022-08-15 04:20:27 -05:00
pos = addr - (uint32_t)role->dma_buf_ptr;
2022-08-15 04:20:27 -05:00
if (pos < 0 || pos > role->dma_buf_size) {
return -1;
}
2022-08-15 04:20:27 -05:00
#ifndef CHIP_BEST1000
if (role->cfg.chan_sep_buf && role->cfg.channel_num > AUD_CHANNEL_NUM_1) {
uint32_t chan_size;
uint8_t chan_idx;
uint8_t desc_idx;
uint16_t chan_desc_offset;
uint16_t chan_desc_xfer_size;
uint32_t chan_offset;
2022-08-15 04:20:27 -05:00
chan_size = role->dma_buf_size / role->cfg.channel_num;
chan_desc_xfer_size = chan_size / AUDIO_BUFFER_COUNT;
2022-08-15 04:20:27 -05:00
chan_idx = pos / chan_size;
chan_offset = pos % chan_size;
desc_idx = chan_offset / chan_desc_xfer_size;
chan_desc_offset = chan_offset % chan_desc_xfer_size;
2022-08-15 04:20:27 -05:00
pos = desc_idx * (role->dma_buf_size / AUDIO_BUFFER_COUNT) +
chan_idx * chan_desc_xfer_size + chan_desc_offset;
}
2022-08-15 04:20:27 -05:00
#endif
return pos;
2022-08-15 04:20:27 -05:00
}
int af_stream_buffer_error(enum AUD_STREAM_ID_T id, enum AUD_STREAM_T stream) {
return af_sig_lost_cnt[id][stream];
2022-08-15 04:20:27 -05:00
}
uint32_t af_stream_dma_tc_irq_enable(enum AUD_STREAM_ID_T id,
enum AUD_STREAM_T stream) {
struct af_stream_cfg_t *role;
2022-08-15 04:20:27 -05:00
role = af_get_stream_role(id, stream);
2022-08-15 04:20:27 -05:00
// Check if opened
if ((role->ctl.status &
(AF_STATUS_OPEN_CLOSE | AF_STATUS_STREAM_OPEN_CLOSE)) ==
(AF_STATUS_OPEN_CLOSE | AF_STATUS_STREAM_OPEN_CLOSE)) {
hal_dma_tc_irq_enable(role->dma_cfg.ch);
return AF_RES_SUCCESS;
}
2022-08-15 04:20:27 -05:00
return AF_RES_FAILD;
2022-08-15 04:20:27 -05:00
}
uint32_t af_stream_dma_tc_irq_disable(enum AUD_STREAM_ID_T id,
enum AUD_STREAM_T stream) {
struct af_stream_cfg_t *role;
2022-08-15 04:20:27 -05:00
role = af_get_stream_role(id, stream);
2022-08-15 04:20:27 -05:00
// Check if opened
if ((role->ctl.status &
(AF_STATUS_OPEN_CLOSE | AF_STATUS_STREAM_OPEN_CLOSE)) ==
(AF_STATUS_OPEN_CLOSE | AF_STATUS_STREAM_OPEN_CLOSE)) {
hal_dma_tc_irq_disable(role->dma_cfg.ch);
return AF_RES_SUCCESS;
}
2022-08-15 04:20:27 -05:00
return AF_RES_FAILD;
2022-08-15 04:20:27 -05:00
}
#ifdef RTOS
void af_set_irq_notification(AF_IRQ_NOTIFICATION_T notif) { irq_notif = notif; }
2022-08-15 04:20:27 -05:00
static int af_priority[AF_USER_NUM];
void af_set_priority(AF_USER_E user, int priority) {
uint8_t i = 0;
int max_priority = 0;
af_priority[user] = priority;
2022-08-15 04:20:27 -05:00
for (i = 0; i < AF_USER_NUM; i++) {
if (max_priority < af_priority[i]) {
max_priority = af_priority[i];
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
if (max_priority != af_get_priority()) {
osThreadSetPriority(af_thread_tid, max_priority);
}
2022-08-15 04:20:27 -05:00
}
int af_get_priority(void) { return (int)osThreadGetPriority(af_thread_tid); }
2022-08-15 04:20:27 -05:00
int af_get_default_priority(void) { return af_default_priority; }
2022-08-15 04:20:27 -05:00
void af_reset_priority(void) {
osThreadSetPriority(af_thread_tid, af_default_priority);
2022-08-15 04:20:27 -05:00
}
#endif
void af_codec_tune_resample_rate(enum AUD_STREAM_T stream, float ratio) {
af_lock_thread();
2022-08-15 04:20:27 -05:00
af_codec_direct_tune_resample_rate(stream, ratio);
2022-08-15 04:20:27 -05:00
af_unlock_thread();
2022-08-15 04:20:27 -05:00
}
void af_codec_direct_tune_resample_rate(enum AUD_STREAM_T stream, float ratio) {
ASSERT(stream < AUD_STREAM_NUM, "[%s] Bad stream=%d", __func__, stream);
2022-08-15 04:20:27 -05:00
hal_codec_tune_resample_rate(stream, ratio);
2022-08-15 04:20:27 -05:00
}
void af_codec_tune_both_resample_rate(float ratio) {
af_lock_thread();
2022-08-15 04:20:27 -05:00
hal_codec_tune_both_resample_rate(ratio);
2022-08-15 04:20:27 -05:00
af_unlock_thread();
2022-08-15 04:20:27 -05:00
}
void af_codec_tune_pll(float ratio) {
af_lock_thread();
2022-08-15 04:20:27 -05:00
analog_aud_pll_tune(ratio);
2022-08-15 04:20:27 -05:00
af_unlock_thread();
2022-08-15 04:20:27 -05:00
}
void af_codec_tune_xtal(float ratio) {
af_lock_thread();
2022-08-15 04:20:27 -05:00
analog_aud_xtal_tune(ratio);
2022-08-15 04:20:27 -05:00
af_unlock_thread();
2022-08-15 04:20:27 -05:00
}
void af_codec_tune(enum AUD_STREAM_T stream, float ratio) {
af_lock_thread();
2022-08-15 04:20:27 -05:00
af_codec_direct_tune(stream, ratio);
2022-08-15 04:20:27 -05:00
af_unlock_thread();
2022-08-15 04:20:27 -05:00
}
void af_codec_direct_tune(enum AUD_STREAM_T stream, float ratio) {
ASSERT(stream <= AUD_STREAM_NUM, "[%s] Bad stream=%d", __func__, stream);
2022-08-15 04:20:27 -05:00
#ifdef __AUDIO_RESAMPLE__
if (hal_cmu_get_audio_resample_status()) {
if (stream < AUD_STREAM_NUM) {
hal_codec_tune_resample_rate(stream, ratio);
} else {
hal_codec_tune_both_resample_rate(ratio);
2022-08-15 04:20:27 -05:00
}
} else
#endif
{
analog_aud_pll_tune(ratio);
}
2022-08-15 04:20:27 -05:00
}
void af_codec_set_perf_test_power(int type) {
af_lock_thread();
2022-08-15 04:20:27 -05:00
hal_codec_dac_gain_m60db_check((enum HAL_CODEC_PERF_TEST_POWER_T)type);
2022-08-15 04:20:27 -05:00
af_unlock_thread();
2022-08-15 04:20:27 -05:00
}
void af_codec_set_noise_reduction(bool enable) {
af_lock_thread();
2022-08-15 04:20:27 -05:00
hal_codec_set_noise_reduction(enable);
2022-08-15 04:20:27 -05:00
af_unlock_thread();
2022-08-15 04:20:27 -05:00
}
void af_codec_bt_trigger_config(bool en,
AF_CODEC_BT_TRIGGER_CALLBACK callback) {
if (en) {
hal_codec_set_bt_trigger_callback(callback);
hal_codec_bt_trigger_start();
} else {
hal_codec_bt_trigger_stop();
}
2022-08-15 04:20:27 -05:00
}
void af_codec_sync_config(enum AUD_STREAM_T stream,
enum AF_CODEC_SYNC_TYPE_T type, bool enable) {
2022-08-15 04:20:27 -05:00
#ifndef CHIP_BEST1000
af_lock_thread();
2022-08-15 04:20:27 -05:00
if (stream == AUD_STREAM_PLAYBACK) {
if (enable) {
hal_codec_sync_dac_enable((enum HAL_CODEC_SYNC_TYPE_T)type);
2022-08-15 04:20:27 -05:00
} else {
hal_codec_sync_dac_disable();
}
} else {
if (enable) {
hal_codec_sync_adc_enable((enum HAL_CODEC_SYNC_TYPE_T)type);
} else {
hal_codec_sync_adc_disable();
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
af_unlock_thread();
2022-08-15 04:20:27 -05:00
#endif
}
void af_i2s_sync_config(enum AUD_STREAM_T stream, enum AF_I2S_SYNC_TYPE_T type,
bool enable) {
2022-08-15 04:20:27 -05:00
#ifdef CHIP_BEST2300A
af_lock_thread();
2022-08-15 04:20:27 -05:00
if (stream == AUD_STREAM_PLAYBACK) {
if (enable) {
hal_i2s_clk_sync_enable((enum HAL_I2S_SYNC_TYPE_T)type);
2022-08-15 04:20:27 -05:00
} else {
hal_i2s_clk_sync_disable();
2022-08-15 04:20:27 -05:00
}
} else {
if (enable) {
hal_i2s_clk_sync_enable((enum HAL_I2S_SYNC_TYPE_T)type);
} else {
hal_i2s_clk_sync_disable();
}
}
2022-08-15 04:20:27 -05:00
af_unlock_thread();
2022-08-15 04:20:27 -05:00
#endif
}
void af_codec_sync_resample_rate_config(enum AUD_STREAM_T stream,
enum AF_CODEC_SYNC_TYPE_T type,
bool enable) {
2022-08-15 04:20:27 -05:00
#ifndef CHIP_BEST1000
af_lock_thread();
2022-08-15 04:20:27 -05:00
if (stream == AUD_STREAM_PLAYBACK) {
if (enable) {
hal_codec_sync_dac_resample_rate_enable((enum HAL_CODEC_SYNC_TYPE_T)type);
2022-08-15 04:20:27 -05:00
} else {
hal_codec_sync_dac_resample_rate_disable();
}
} else {
if (enable) {
hal_codec_sync_adc_resample_rate_enable((enum HAL_CODEC_SYNC_TYPE_T)type);
} else {
hal_codec_sync_adc_resample_rate_disable();
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
af_unlock_thread();
2022-08-15 04:20:27 -05:00
#endif
}
void af_codec_sync_gain_config(enum AUD_STREAM_T stream,
enum AF_CODEC_SYNC_TYPE_T type, bool enable) {
2022-08-15 04:20:27 -05:00
#ifndef CHIP_BEST1000
af_lock_thread();
2022-08-15 04:20:27 -05:00
if (stream == AUD_STREAM_PLAYBACK) {
if (enable) {
hal_codec_sync_dac_gain_enable((enum HAL_CODEC_SYNC_TYPE_T)type);
2022-08-15 04:20:27 -05:00
} else {
hal_codec_sync_dac_gain_disable();
2022-08-15 04:20:27 -05:00
}
} else {
if (enable) {
hal_codec_sync_adc_gain_enable((enum HAL_CODEC_SYNC_TYPE_T)type);
} else {
hal_codec_sync_adc_gain_disable();
}
}
2022-08-15 04:20:27 -05:00
af_unlock_thread();
2022-08-15 04:20:27 -05:00
#endif
}
void af_codec_swap_output(bool swap) {
af_lock_thread();
2022-08-15 04:20:27 -05:00
hal_codec_swap_output(swap);
2022-08-15 04:20:27 -05:00
af_unlock_thread();
2022-08-15 04:20:27 -05:00
}
void af_codec_set_playback_post_handler(AF_CODEC_PLAYBACK_POST_HANDLER_T hdlr) {
codec_play_post_hdlr = hdlr;
2022-08-15 04:20:27 -05:00
}
int af_anc_open(enum ANC_TYPE_T type, enum AUD_SAMPRATE_T play_rate,
enum AUD_SAMPRATE_T capture_rate, AF_ANC_HANDLER handler) {
FUNC_ENTRY_TRACE();
2022-08-15 04:20:27 -05:00
af_lock_thread();
2022-08-15 04:20:27 -05:00
codec_anc_open(type, play_rate, capture_rate, handler);
2022-08-15 04:20:27 -05:00
af_unlock_thread();
2022-08-15 04:20:27 -05:00
return AF_RES_SUCCESS;
2022-08-15 04:20:27 -05:00
}
int af_anc_close(enum ANC_TYPE_T type) {
FUNC_ENTRY_TRACE();
2022-08-15 04:20:27 -05:00
af_lock_thread();
2022-08-15 04:20:27 -05:00
codec_anc_close(type);
2022-08-15 04:20:27 -05:00
af_unlock_thread();
2022-08-15 04:20:27 -05:00
return AF_RES_SUCCESS;
2022-08-15 04:20:27 -05:00
}
#ifdef VOICE_DETECTOR_EN
int af_vad_open(const struct AUD_VAD_CONFIG_T *cfg) {
FUNC_ENTRY_TRACE();
2022-08-15 04:20:27 -05:00
af_lock_thread();
2022-08-15 04:20:27 -05:00
codec_vad_open(cfg);
2022-08-15 04:20:27 -05:00
af_unlock_thread();
2022-08-15 04:20:27 -05:00
return AF_RES_SUCCESS;
2022-08-15 04:20:27 -05:00
}
int af_vad_close(void) {
FUNC_ENTRY_TRACE();
2022-08-15 04:20:27 -05:00
af_lock_thread();
2022-08-15 04:20:27 -05:00
codec_vad_close();
2022-08-15 04:20:27 -05:00
af_unlock_thread();
2022-08-15 04:20:27 -05:00
return AF_RES_SUCCESS;
2022-08-15 04:20:27 -05:00
}
int af_vad_start(void) {
FUNC_ENTRY_TRACE();
2022-08-15 04:20:27 -05:00
af_lock_thread();
2022-08-15 04:20:27 -05:00
hal_codec_vad_start();
2022-08-15 04:20:27 -05:00
af_unlock_thread();
2022-08-15 04:20:27 -05:00
return AF_RES_SUCCESS;
2022-08-15 04:20:27 -05:00
}
int af_vad_stop(void) {
FUNC_ENTRY_TRACE();
2022-08-15 04:20:27 -05:00
af_lock_thread();
2022-08-15 04:20:27 -05:00
hal_codec_vad_stop();
2022-08-15 04:20:27 -05:00
af_unlock_thread();
2022-08-15 04:20:27 -05:00
return AF_RES_SUCCESS;
2022-08-15 04:20:27 -05:00
}
uint32_t af_vad_get_data(uint8_t *buf, uint32_t len) {
return hal_codec_vad_recv_data(buf, len);
2022-08-15 04:20:27 -05:00
}
void af_vad_get_data_info(struct CODEC_VAD_BUF_INFO_T *vad_buf_info) {
hal_codec_get_vad_data_info(vad_buf_info);
2022-08-15 04:20:27 -05:00
}
#endif
#ifdef CODEC_DSD
void af_dsd_enable(void) {
enum AUD_STREAM_ID_T id;
struct af_stream_cfg_t *role;
bool opened = false;
2022-08-15 04:20:27 -05:00
af_lock_thread();
2022-08-15 04:20:27 -05:00
for (id = AUD_STREAM_ID_0; id < AUD_STREAM_ID_NUM; id++) {
role = af_get_stream_role(id, AUD_STREAM_PLAYBACK);
if (role->ctl.status & AF_STATUS_STREAM_OPEN_CLOSE) {
if (role->cfg.device == AUD_STREAM_USE_INT_CODEC &&
role->dma_cfg.dst_periph == HAL_AUDMA_CODEC_TX) {
ASSERT(role->ctl.status ==
(AF_STATUS_OPEN_CLOSE | AF_STATUS_STREAM_OPEN_CLOSE),
"Bad stream status when DSD enabled: 0x%X", role->ctl.status);
role->dma_cfg.dst_periph = HAL_AUDMA_DSD_TX;
af_stream_update_dma_buffer(AUD_STREAM_PLAYBACK, role, &role->cfg);
opened = true;
break;
}
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
hal_codec_dsd_enable();
af_dsd_enabled = true;
2022-08-15 04:20:27 -05:00
af_unlock_thread();
2022-08-15 04:20:27 -05:00
if (opened) {
// Enable DSD sample rate handling
af_stream_setup(id, AUD_STREAM_PLAYBACK, &role->cfg);
}
2022-08-15 04:20:27 -05:00
}
void af_dsd_disable(void) {
enum AUD_STREAM_ID_T id;
struct af_stream_cfg_t *role;
bool opened = false;
2022-08-15 04:20:27 -05:00
af_lock_thread();
2022-08-15 04:20:27 -05:00
for (id = AUD_STREAM_ID_0; id < AUD_STREAM_ID_NUM; id++) {
role = af_get_stream_role(id, AUD_STREAM_PLAYBACK);
if (role->ctl.status & AF_STATUS_STREAM_OPEN_CLOSE) {
if (role->cfg.device == AUD_STREAM_USE_INT_CODEC &&
role->dma_cfg.dst_periph == HAL_AUDMA_DSD_TX) {
ASSERT(role->ctl.status ==
(AF_STATUS_OPEN_CLOSE | AF_STATUS_STREAM_OPEN_CLOSE),
"Bad stream status when DSD disabled: 0x%X", role->ctl.status);
role->dma_cfg.dst_periph = HAL_AUDMA_CODEC_TX;
af_stream_update_dma_buffer(AUD_STREAM_PLAYBACK, role, &role->cfg);
opened = true;
break;
}
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
hal_codec_dsd_disable();
af_dsd_enabled = false;
2022-08-15 04:20:27 -05:00
af_unlock_thread();
2022-08-15 04:20:27 -05:00
if (opened) {
// Disable DSD sample rate handling
af_stream_setup(id, AUD_STREAM_PLAYBACK, &role->cfg);
}
2022-08-15 04:20:27 -05:00
}
#endif
#ifdef __RAND_FROM_MIC__
uint8_t random_mic_is_on(uint8_t *deviceId) {
struct af_stream_cfg_t *role = NULL;
2022-08-15 04:20:27 -05:00
*deviceId = 0;
2022-08-15 04:20:27 -05:00
for (uint8_t id = 0; id < AUD_STREAM_ID_NUM; id++) {
role = af_get_stream_role((enum AUD_STREAM_ID_T)id, AUD_STREAM_CAPTURE);
if (role->cfg.device == AUD_STREAM_USE_INT_CODEC &&
role->ctl.status ==
(AF_STATUS_OPEN_CLOSE | AF_STATUS_STREAM_OPEN_CLOSE |
AF_STATUS_STREAM_START_STOP)) {
return RAND_STATUS_MIC_STARTED;
} else if (role->cfg.device == AUD_STREAM_USE_INT_CODEC &&
role->ctl.status ==
(AF_STATUS_OPEN_CLOSE | AF_STATUS_STREAM_OPEN_CLOSE)) {
*deviceId = id;
return RAND_STATUS_MIC_OPENED;
2022-08-15 04:20:27 -05:00
}
}
2022-08-15 04:20:27 -05:00
return RAND_STATUS_CLOSE;
}
#endif