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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2023-02-01 14:52:54 -06:00
|
|
|
#include "app_anc.h"
|
|
|
|
#include "anc_assist.h"
|
|
|
|
#include "anc_process.h"
|
|
|
|
#include "anc_wnr.h"
|
|
|
|
#include "app_ibrt_keyboard.h"
|
|
|
|
#include "app_ibrt_ui.h"
|
|
|
|
#include "app_thread.h"
|
|
|
|
#include "apps.h"
|
2022-08-15 04:20:27 -05:00
|
|
|
#include "aud_section.h"
|
|
|
|
#include "audioflinger.h"
|
|
|
|
#include "cmsis.h"
|
|
|
|
#include "cmsis_os.h"
|
|
|
|
#include "hal_bootmode.h"
|
|
|
|
#include "hal_codec.h"
|
2023-02-01 14:52:54 -06:00
|
|
|
#include "hal_sysfreq.h"
|
|
|
|
#include "hal_timer.h"
|
|
|
|
#include "hal_trace.h"
|
|
|
|
#include "hwtimer_list.h"
|
|
|
|
#include "plat_types.h"
|
|
|
|
#include "pmu.h"
|
|
|
|
#include "tgt_hardware.h"
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
#include "app_status_ind.h"
|
|
|
|
#ifdef __SIMPLE_INTERNAL_PLAYER_SUPPORT__
|
|
|
|
#include "simple_internal_player.h"
|
|
|
|
#endif
|
|
|
|
#include "hal_aud.h"
|
|
|
|
#ifdef IBRT
|
2023-02-01 14:52:54 -06:00
|
|
|
#include "app_tws_ctrl_thread.h"
|
2022-08-15 04:20:27 -05:00
|
|
|
#include "app_tws_ibrt.h"
|
|
|
|
#include "app_tws_ibrt_cmd_handler.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "co_math.h"
|
2023-02-01 14:52:54 -06:00
|
|
|
//#define ANC_MODE_SWITCH_WITHOUT_FADE //Comment this line if you need fade
|
|
|
|
// function between anc mode
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
#ifndef _ANC_FADE_STACK_SIZE
|
2023-02-01 14:52:54 -06:00
|
|
|
#define _ANC_FADE_STACK_SIZE (1 * 1024)
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static osThreadId anc_fade_thread_tid;
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
#define FADE_IN 0x0001
|
|
|
|
#define FADE_OUT 0x0002
|
2022-08-15 04:20:27 -05:00
|
|
|
#define CHANGE_FROM_ANC_TO_TT_DIRECTLY 0x0003
|
|
|
|
|
|
|
|
static void anc_fade_thread(void const *argument);
|
2023-02-01 14:52:54 -06:00
|
|
|
osThreadDef(anc_fade_thread, osPriorityBelowNormal, 1, _ANC_FADE_STACK_SIZE,
|
|
|
|
"anc_fade_thread");
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
extern uint8_t app_poweroff_flag;
|
|
|
|
uint32_t app_anc_get_anc_status(void);
|
|
|
|
|
|
|
|
void app_anc_disable(void);
|
|
|
|
void app_anc_close_anc(void);
|
|
|
|
osStatus osDelay(uint32_t millisec);
|
|
|
|
void app_anc_timer_set(uint32_t request, uint32_t delay);
|
|
|
|
void app_anc_timer_close(void);
|
2023-02-01 14:52:54 -06:00
|
|
|
extern bool app_mode_is_usbaudio(void); // 96k 384k
|
|
|
|
extern bool app_mode_is_i2s(void); // 96k 384k
|
2022-08-15 04:20:27 -05:00
|
|
|
void app_anc_init_timer(void);
|
|
|
|
extern void i2s_player_send_stop(void);
|
|
|
|
extern void fb_anti_howl_start(void);
|
|
|
|
extern void fb_anti_howl_stop(void);
|
2023-02-01 14:52:54 -06:00
|
|
|
extern void hal_codec_reconfig_pll_freq(enum AUD_SAMPRATE_T dac_rate,
|
|
|
|
enum AUD_SAMPRATE_T adc_rate);
|
2022-08-15 04:20:27 -05:00
|
|
|
extern void anc_status_sync(void);
|
|
|
|
extern void anc_status_sync_init(void);
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
extern uint8_t is_sbc_mode(void);
|
|
|
|
extern uint8_t is_sco_mode(void);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
void app_anc_switch_set_edge(uint8_t down_edge);
|
|
|
|
|
|
|
|
#ifdef __ANC_STICK_SWITCH_USE_GPIO__
|
|
|
|
typedef void (*ANC_KEY_CALLBACK)(uint8_t status);
|
|
|
|
static ANC_KEY_CALLBACK app_switch_callback;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern int app_shutdown(void);
|
|
|
|
extern int app_reset(void);
|
|
|
|
|
|
|
|
extern void analog_aud_codec_speaker_enable(bool en);
|
|
|
|
|
|
|
|
#ifdef __SUPPORT_ANC_SINGLE_MODE_WITHOUT_BT__
|
|
|
|
extern bool anc_single_mode_is_on(void);
|
|
|
|
#endif
|
2023-02-01 14:52:54 -06:00
|
|
|
enum {
|
|
|
|
ANC_STATUS_OFF = 0,
|
|
|
|
ANC_STATUS_ON,
|
|
|
|
ANC_STATUS_WAITING_ON,
|
|
|
|
ANC_STATUS_WAITING_OFF,
|
|
|
|
ANC_STATUS_INIT_ON,
|
|
|
|
ANC_STATUS_NONE
|
2022-08-15 04:20:27 -05:00
|
|
|
};
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
enum {
|
|
|
|
ANC_EVENT_INIT = 0,
|
|
|
|
ANC_EVENT_OPEN,
|
|
|
|
ANC_EVENT_CLOSE,
|
|
|
|
ANC_EVENT_FADE_IN,
|
|
|
|
ANC_EVENT_FADE_OUT,
|
|
|
|
ANC_EVENT_CHANGE_SAMPLERATE,
|
|
|
|
ANC_EVENT_CHANGE_STATUS,
|
|
|
|
ANC_EVENT_HOWL_PROCESS,
|
|
|
|
ANC_EVENT_SYNC_STATUS,
|
|
|
|
ANC_EVENT_SYNC_INIT,
|
|
|
|
ANC_EVENT_PWR_KEY_MONITOR,
|
|
|
|
ANC_EVENT_PWR_KEY_MONITOR_REBOOT,
|
|
|
|
ANC_EVENT_SWITCH_KEY_DEBONCE,
|
|
|
|
SIMPLE_PLAYER_CLOSE_CODEC_EVT,
|
|
|
|
SIMPLE_PLAYER_DELAY_STOP_EVT,
|
|
|
|
ANC_EVENT_NONE
|
2022-08-15 04:20:27 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
static uint32_t anc_work_status = ANC_STATUS_OFF;
|
|
|
|
static uint32_t anc_timer_request = ANC_EVENT_NONE;
|
|
|
|
|
|
|
|
static enum AUD_SAMPRATE_T anc_sample_rate[AUD_STREAM_NUM];
|
|
|
|
|
|
|
|
static HWTIMER_ID anc_timerid = NULL;
|
|
|
|
#define anc_gain_adjust_gap (60)
|
|
|
|
|
|
|
|
#define anc_init_switch_off_time (MS_TO_TICKS(1000 * 60 * 2))
|
|
|
|
#define anc_auto_power_off_time (MS_TO_TICKS(1000 * 60 * 60))
|
|
|
|
#define anc_switch_on_time (MS_TO_TICKS(600))
|
|
|
|
#define anc_close_delay_time (MS_TO_TICKS(1000 * 20))
|
|
|
|
#define anc_pwr_key_monitor_time (MS_TO_TICKS(1500))
|
|
|
|
#define anc_switch_key_debonce_time (MS_TO_TICKS(40))
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
#if 0 // def ANC_FB_CHECK
|
2022-08-15 04:20:27 -05:00
|
|
|
uint32_t app_fb_check_threshold_vale=0x4000000;
|
|
|
|
enum ANC_TYPE_T app_anc_check_type = ANC_FEEDBACK;
|
|
|
|
uint32_t app_fb_check_delay_time=((MS_TO_TICKS(5000)));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ANC_FB_CHECK
|
2023-02-01 14:52:54 -06:00
|
|
|
uint32_t app_fb_check_threshold_vale = 0x4000000;
|
|
|
|
enum ANC_TYPE_T app_anc_check_type = ANC_FEEDFORWARD;
|
|
|
|
uint32_t app_fb_check_decrease_resume_delay_time = (MS_TO_TICKS(400));
|
2022-08-15 04:20:27 -05:00
|
|
|
uint8_t app_fb_check_decrease_value = 18;
|
|
|
|
uint32_t app_fb_check_delay_time = (MS_TO_TICKS(3000));
|
|
|
|
uint8_t app_fb_check_delay_time_flag = 0;
|
|
|
|
uint32_t app_fb_check_delay_time2 = (MS_TO_TICKS(3000));
|
|
|
|
uint32_t app_fb_check_threshold_count = 20;
|
2023-02-01 14:52:54 -06:00
|
|
|
uint8_t app_gain_resume_status = 1;
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
enum {
|
|
|
|
APP_ANC_IDLE = 0,
|
|
|
|
APP_ANC_FADE_IN,
|
|
|
|
APP_ANC_FADE_OUT,
|
2022-08-15 04:20:27 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
uint32_t app_anc_fade_status = APP_ANC_IDLE;
|
|
|
|
|
|
|
|
static bool app_init_done = false;
|
|
|
|
#ifdef ANC_SWITCH_PIN
|
|
|
|
static bool app_anc_shutdown = false;
|
|
|
|
#endif
|
|
|
|
bool anc_set_dac_pa_delay = false;
|
|
|
|
static enum ANC_INDEX anc_coef_idx = 0;
|
|
|
|
|
|
|
|
extern void analog_aud_enable_dac_pa(uint8_t dac);
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
bool app_anc_is_on(void) { return (ANC_STATUS_ON == anc_work_status); }
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
uint32_t app_anc_get_sample_rate(void) {
|
|
|
|
return (uint32_t)anc_sample_rate[AUD_STREAM_CAPTURE];
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_set_coef_idx(uint8_t idx) { anc_coef_idx = idx; }
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
uint8_t app_anc_get_coef_idx(void) { return anc_coef_idx; }
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
#if defined(IBRT)
|
|
|
|
|
|
|
|
static enum ANC_INDEX anc_peer_coef_idx = 0;
|
|
|
|
static bool anc_status_sync_flag = false;
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_set_peer_coef_idx(uint8_t idx) { anc_peer_coef_idx = idx; }
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
uint8_t app_anc_get_peer_coef_idx(void) { return anc_peer_coef_idx; }
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_set_status_sync_flag(bool status) {
|
|
|
|
anc_status_sync_flag = status;
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
bool app_anc_get_status_sync_flag(void) { return anc_status_sync_flag; }
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_set_init_done(void) { app_init_done = true; }
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
void app_anc_switch_turnled(bool on);
|
|
|
|
|
|
|
|
#ifdef __SUPPORT_ANC_SINGLE_MODE_WITHOUT_BT__
|
|
|
|
|
|
|
|
static ANC_KEY_CALLBACK app_pwr_key_monitor_callback;
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
static void app_pwr_key_monitor_handler(enum HAL_GPIO_PIN_T pin) {
|
|
|
|
uint8_t gpio_val = hal_gpio_pin_get_val(pin);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
TRACE(2, " %s :%d ", __func__, gpio_val);
|
|
|
|
if (app_pwr_key_monitor_callback)
|
|
|
|
app_pwr_key_monitor_callback(gpio_val);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
bool app_pwr_key_monitor_get_val(void) {
|
|
|
|
return (bool)hal_gpio_pin_get_val((enum HAL_GPIO_PIN_T)PWR_KEY_MONITOR_PIN);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_pwr_key_monitor_set_int_edge(uint8_t down_edge) {
|
|
|
|
struct HAL_GPIO_IRQ_CFG_T gpiocfg;
|
|
|
|
gpiocfg.irq_enable = true;
|
|
|
|
gpiocfg.irq_debounce = true;
|
|
|
|
gpiocfg.irq_type = HAL_GPIO_IRQ_TYPE_EDGE_SENSITIVE;
|
|
|
|
gpiocfg.irq_polarity = (down_edge == 1) ? HAL_GPIO_IRQ_POLARITY_LOW_FALLING
|
|
|
|
: HAL_GPIO_IRQ_POLARITY_HIGH_RISING;
|
|
|
|
gpiocfg.irq_handler = app_pwr_key_monitor_handler;
|
|
|
|
TRACE(2, " %s :%d ", __func__, down_edge);
|
|
|
|
hal_gpio_setup_irq((enum HAL_GPIO_PIN_T)PWR_KEY_MONITOR_PIN, &gpiocfg);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_pwr_key_monitor_init(ANC_KEY_CALLBACK callback) {
|
|
|
|
app_pwr_key_monitor_callback = callback;
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ANC_LED_PIN
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_switch_turnled(bool on) {
|
|
|
|
TRACE(2, " %s on %d ", __func__, on);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (cfg_anc_led.pin != HAL_IOMUX_PIN_NUM) {
|
|
|
|
if (on) {
|
|
|
|
TRACE(0, "on\n");
|
|
|
|
hal_gpio_pin_set((enum HAL_GPIO_PIN_T)cfg_anc_led.pin);
|
|
|
|
} else {
|
|
|
|
TRACE(0, "off\n");
|
|
|
|
hal_gpio_pin_clr((enum HAL_GPIO_PIN_T)cfg_anc_led.pin);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
2023-02-01 14:52:54 -06:00
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __ANC_STICK_SWITCH_USE_GPIO__
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
static void app_anc_switch_int_handler(enum HAL_GPIO_PIN_T pin) {
|
|
|
|
// uint8_t gpio_val;
|
|
|
|
uint8_t gpio_val = hal_gpio_pin_get_val(pin);
|
|
|
|
TRACE(3, " %s ,pin %d,status %d", __func__, pin, gpio_val);
|
2022-08-15 04:20:27 -05:00
|
|
|
#ifdef ANC_LED_PIN
|
2023-02-01 14:52:54 -06:00
|
|
|
if (gpio_val)
|
|
|
|
app_anc_switch_turnled(true);
|
|
|
|
else
|
|
|
|
app_anc_switch_turnled(false);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
2023-02-01 14:52:54 -06:00
|
|
|
app_anc_switch_set_edge(gpio_val);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
app_anc_timer_set(ANC_EVENT_SWITCH_KEY_DEBONCE, anc_switch_key_debonce_time);
|
|
|
|
// if (app_switch_callback)
|
|
|
|
// app_switch_callback(gpio_val);
|
|
|
|
// app_anc_key_handler();
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_switch_init(ANC_KEY_CALLBACK callback) {
|
|
|
|
app_switch_callback = callback;
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ANC_SWITCH_PIN
|
2023-02-01 14:52:54 -06:00
|
|
|
bool app_anc_set_reboot(void) {
|
|
|
|
return (app_anc_shutdown &&
|
|
|
|
hal_gpio_pin_get_val((enum HAL_GPIO_PIN_T)ANC_SWITCH_PIN));
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
bool app_anc_switch_get_val(void) {
|
|
|
|
return (bool)hal_gpio_pin_get_val((enum HAL_GPIO_PIN_T)ANC_SWITCH_PIN);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_switch_set_edge(uint8_t down_edge) {
|
|
|
|
struct HAL_GPIO_IRQ_CFG_T gpiocfg;
|
|
|
|
gpiocfg.irq_enable = true;
|
|
|
|
gpiocfg.irq_debounce = true;
|
|
|
|
gpiocfg.irq_type = HAL_GPIO_IRQ_TYPE_EDGE_SENSITIVE;
|
|
|
|
gpiocfg.irq_polarity = (down_edge == 1) ? HAL_GPIO_IRQ_POLARITY_LOW_FALLING
|
|
|
|
: HAL_GPIO_IRQ_POLARITY_HIGH_RISING;
|
|
|
|
gpiocfg.irq_handler = app_anc_switch_int_handler;
|
|
|
|
TRACE(2, " %s :%d ", __func__, down_edge);
|
|
|
|
hal_gpio_setup_irq((enum HAL_GPIO_PIN_T)ANC_SWITCH_PIN, &gpiocfg);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
int __anc_usb_app_fadein(enum ANC_TYPE_T type) {
|
|
|
|
int32_t gain0_curr, gain1_curr;
|
|
|
|
int32_t gain0_tg, gain1_tg;
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_get_gain(&gain0_curr, &gain1_curr, type);
|
|
|
|
anc_get_cfg_gain(&gain0_tg, &gain1_tg, type);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (gain0_tg > 0) {
|
|
|
|
if (gain0_curr < gain0_tg) {
|
|
|
|
gain0_curr++;
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
2023-02-01 14:52:54 -06:00
|
|
|
} else {
|
|
|
|
if (gain0_curr > gain0_tg) {
|
|
|
|
gain0_curr--;
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
2023-02-01 14:52:54 -06:00
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (gain1_tg > 0) {
|
|
|
|
if (gain1_curr < gain1_tg) {
|
|
|
|
gain1_curr++;
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
2023-02-01 14:52:54 -06:00
|
|
|
} else {
|
|
|
|
if (gain1_curr > gain1_tg) {
|
|
|
|
gain1_curr--;
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
2023-02-01 14:52:54 -06:00
|
|
|
}
|
|
|
|
hal_sys_timer_delay_us(25);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_set_gain(gain0_curr, gain1_curr, type);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if ((gain0_curr == gain0_tg) && (gain1_curr == gain1_tg)) {
|
|
|
|
TRACE(3, "[%s] cur gain: %d %d", __func__, gain0_curr, gain1_curr);
|
|
|
|
return 0;
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
return 1;
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
int __anc_usb_app_fadeout(enum ANC_TYPE_T type) {
|
|
|
|
int32_t gain0_curr, gain1_curr;
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_get_gain(&gain0_curr, &gain1_curr, type);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (gain0_curr > 0) {
|
|
|
|
gain0_curr--;
|
|
|
|
} else if (gain0_curr < 0) {
|
|
|
|
gain0_curr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gain1_curr > 0) {
|
|
|
|
gain1_curr--;
|
|
|
|
} else if (gain1_curr < 0) {
|
|
|
|
gain1_curr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// gain0_curr = gain1_curr = 0 ;
|
|
|
|
hal_sys_timer_delay_us(25);
|
|
|
|
|
|
|
|
anc_set_gain(gain0_curr, gain1_curr, type);
|
|
|
|
|
|
|
|
if ((gain0_curr == 0) && (gain1_curr == 0)) {
|
|
|
|
TRACE(3, "[%s] gain: %d, %d", __func__, gain0_curr, gain1_curr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(ANC_FF_ENABLED) && defined(ANC_FB_ENABLED)
|
|
|
|
int __anc_usb_app_fadein_ff_fb(void) {
|
|
|
|
int32_t gain0_curr, gain1_curr;
|
|
|
|
int32_t gain2_curr, gain3_curr;
|
|
|
|
|
|
|
|
int32_t gain0_tg, gain1_tg;
|
|
|
|
int32_t gain2_tg, gain3_tg;
|
|
|
|
|
|
|
|
anc_get_gain(&gain0_curr, &gain1_curr, ANC_FEEDFORWARD);
|
|
|
|
anc_get_gain(&gain2_curr, &gain3_curr, ANC_FEEDBACK);
|
|
|
|
anc_get_cfg_gain(&gain0_tg, &gain1_tg, ANC_FEEDFORWARD);
|
|
|
|
anc_get_cfg_gain(&gain2_tg, &gain3_tg, ANC_FEEDBACK);
|
|
|
|
|
|
|
|
uint32_t random_factor = rand();
|
|
|
|
random_factor %= 5;
|
|
|
|
if (random_factor == 0)
|
|
|
|
random_factor++;
|
|
|
|
|
|
|
|
uint32_t random_factor_inverse = 5 - random_factor;
|
|
|
|
if (random_factor_inverse == 0)
|
|
|
|
random_factor_inverse++;
|
|
|
|
|
|
|
|
random_factor_inverse = 1;
|
|
|
|
|
|
|
|
if (is_sbc_mode() || is_sco_mode()) {
|
|
|
|
if (gain0_tg > 0) {
|
|
|
|
if (gain0_curr < gain0_tg) {
|
|
|
|
gain0_curr = MIN(gain0_curr + random_factor_inverse, gain0_tg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (gain0_curr > gain0_tg) {
|
|
|
|
gain0_curr = MAX(gain0_curr - random_factor_inverse, gain0_tg);
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
2023-02-01 14:52:54 -06:00
|
|
|
|
|
|
|
if (gain1_tg > 0) {
|
|
|
|
if (gain1_curr < gain1_tg) {
|
|
|
|
gain1_curr = MIN(gain1_curr + random_factor_inverse, gain1_tg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (gain1_curr > gain1_tg) {
|
|
|
|
gain1_curr = MAX(gain1_curr - random_factor_inverse, gain1_tg);
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (gain2_tg > 0) {
|
|
|
|
if (gain2_curr < gain2_tg) {
|
|
|
|
gain2_curr = MIN(gain2_curr + random_factor_inverse, gain2_tg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (gain2_curr > gain2_tg) {
|
|
|
|
gain2_curr = MAX(gain2_curr - random_factor_inverse, gain2_tg);
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
2023-02-01 14:52:54 -06:00
|
|
|
|
|
|
|
if (gain3_tg > 0) {
|
|
|
|
if (gain3_curr < gain3_tg) {
|
|
|
|
gain3_curr = MIN(gain3_curr + random_factor_inverse, gain3_tg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (gain3_curr > gain3_tg) {
|
|
|
|
gain3_curr = MAX(gain3_curr - random_factor_inverse, gain3_tg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (gain0_tg > 0) {
|
|
|
|
if (gain0_curr < gain0_tg) {
|
|
|
|
gain0_curr = MIN(gain0_curr + random_factor_inverse, gain0_tg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (gain0_curr > gain0_tg) {
|
|
|
|
gain0_curr = MAX(gain0_curr - random_factor_inverse, gain0_tg);
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (gain1_tg > 0) {
|
|
|
|
if (gain1_curr < gain1_tg) {
|
|
|
|
gain1_curr = MIN(gain1_curr + random_factor_inverse, gain1_tg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (gain1_curr > gain1_tg) {
|
|
|
|
gain1_curr = MAX(gain1_curr - random_factor_inverse, gain1_tg);
|
|
|
|
}
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (gain2_tg > 0) {
|
|
|
|
if (gain2_curr < gain2_tg) {
|
|
|
|
gain2_curr = MIN(gain2_curr + random_factor_inverse, gain2_tg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (gain2_curr > gain2_tg) {
|
|
|
|
gain2_curr = MAX(gain2_curr - random_factor_inverse, gain2_tg);
|
|
|
|
}
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (gain3_tg > 0) {
|
|
|
|
if (gain3_curr < gain3_tg) {
|
|
|
|
gain3_curr = MIN(gain3_curr + random_factor_inverse, gain3_tg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (gain3_curr > gain3_tg) {
|
|
|
|
gain3_curr = MAX(gain3_curr - random_factor_inverse, gain3_tg);
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
2023-02-01 14:52:54 -06:00
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_set_gain(gain0_curr, gain1_curr, ANC_FEEDFORWARD);
|
|
|
|
anc_set_gain(gain2_curr, gain3_curr, ANC_FEEDBACK);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
// TRACE(5,"[%s] cur gain: %d %d %d %d", __func__, gain0_curr, gain1_curr,
|
|
|
|
// gain2_curr, gain3_curr);
|
|
|
|
|
|
|
|
// osDelay(random_factor);
|
|
|
|
|
|
|
|
hal_sys_timer_delay_us(40);
|
|
|
|
if (gain0_curr % 40 == 0) {
|
|
|
|
// osDelay(random_factor);
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if ((gain0_curr == gain0_tg) && (gain1_curr == gain1_tg) &&
|
|
|
|
(gain2_curr == gain2_tg) && (gain3_curr == gain3_tg)) {
|
|
|
|
anc_disable_gain_updated_when_pass0(1);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
anc_set_gain(gain0_curr, gain1_curr, ANC_FEEDFORWARD);
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_set_gain(gain2_curr, gain3_curr, ANC_FEEDBACK);
|
|
|
|
TRACE(5, "[%s] end cur gain: %d, %d, %d, %d", __func__, gain0_curr,
|
|
|
|
gain1_curr, gain2_curr, gain3_curr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
int __anc_usb_app_fadeout_ff_fb(void) {
|
|
|
|
int32_t gain0_curr, gain1_curr;
|
|
|
|
int32_t gain2_curr, gain3_curr;
|
|
|
|
anc_get_gain(&gain0_curr, &gain1_curr, ANC_FEEDFORWARD);
|
|
|
|
anc_get_gain(&gain2_curr, &gain3_curr, ANC_FEEDBACK);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
uint32_t random_factor = rand();
|
|
|
|
random_factor = random_factor % 5;
|
|
|
|
if (random_factor == 0)
|
|
|
|
random_factor++;
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
uint32_t random_factor_inverse = 5 - random_factor;
|
|
|
|
if (random_factor_inverse == 0)
|
|
|
|
random_factor_inverse++;
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
random_factor_inverse = 1;
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (is_sbc_mode() || is_sco_mode()) {
|
|
|
|
if (gain0_curr > 0) {
|
|
|
|
gain0_curr = MAX(gain0_curr - random_factor_inverse, 0);
|
|
|
|
} else if (gain0_curr < 0) {
|
|
|
|
gain0_curr = MIN(gain0_curr + random_factor_inverse, 0);
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (gain1_curr > 0) {
|
|
|
|
gain1_curr = MAX(gain1_curr - random_factor_inverse, 0);
|
|
|
|
} else if (gain1_curr < 0) {
|
|
|
|
gain1_curr = MIN(gain1_curr + random_factor_inverse, 0);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (gain2_curr > 0) {
|
|
|
|
gain2_curr = MAX(gain2_curr - random_factor_inverse, 0);
|
|
|
|
} else if (gain2_curr < 0) {
|
|
|
|
gain2_curr = MIN(gain2_curr + random_factor_inverse, 0);
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (gain3_curr > 0) {
|
|
|
|
gain3_curr = MAX(gain3_curr - random_factor_inverse, 0);
|
|
|
|
} else if (gain3_curr < 0) {
|
|
|
|
gain3_curr = MIN(gain3_curr + random_factor_inverse, 0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (gain0_curr > 0) {
|
|
|
|
gain0_curr = MAX(gain0_curr - random_factor_inverse, 0);
|
|
|
|
} else if (gain0_curr < 0) {
|
|
|
|
gain0_curr = MIN(gain0_curr + random_factor_inverse, 0);
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (gain1_curr > 0) {
|
|
|
|
gain1_curr = MAX(gain1_curr - random_factor_inverse, 0);
|
|
|
|
} else if (gain1_curr < 0) {
|
|
|
|
gain1_curr = MIN(gain1_curr + random_factor_inverse, 0);
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (gain2_curr > 0) {
|
|
|
|
gain2_curr = MAX(gain2_curr - random_factor_inverse, 0);
|
|
|
|
} else if (gain2_curr < 0) {
|
|
|
|
gain2_curr = MIN(gain2_curr + random_factor_inverse, 0);
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (gain3_curr > 0) {
|
|
|
|
gain3_curr = MAX(gain3_curr - random_factor_inverse, 0);
|
|
|
|
} else if (gain3_curr < 0) {
|
|
|
|
gain3_curr = MIN(gain3_curr + random_factor_inverse, 0);
|
|
|
|
}
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_set_gain(gain0_curr, gain1_curr, ANC_FEEDFORWARD);
|
|
|
|
anc_set_gain(gain2_curr, gain3_curr, ANC_FEEDBACK);
|
|
|
|
// TRACE(5,"[%s] cur gain: %d %d %d %d", __func__, gain0_curr, gain1_curr,
|
|
|
|
// gain2_curr, gain3_curr);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
// osDelay(random_factor);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
hal_sys_timer_delay_us(40);
|
|
|
|
if (gain0_curr % 40 == 0) {
|
|
|
|
// osDelay(random_factor);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((gain0_curr == 0) && (gain1_curr == 0) && (gain2_curr == 0) &&
|
|
|
|
(gain3_curr == 0)) {
|
|
|
|
anc_disable_gain_updated_when_pass0(1);
|
|
|
|
|
|
|
|
anc_set_gain(gain0_curr, gain1_curr, ANC_FEEDFORWARD);
|
|
|
|
anc_set_gain(gain2_curr, gain3_curr, ANC_FEEDBACK);
|
|
|
|
TRACE(5, "[%s] end cur gain: %d, %d, %d, %d", __func__, gain0_curr,
|
|
|
|
gain1_curr, gain2_curr, gain3_curr);
|
|
|
|
return 0;
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
return 1;
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void anc_gain_fade_handle(void) {
|
|
|
|
TRACE(2, " %s %d ", __func__, app_anc_fade_status);
|
|
|
|
if (app_anc_fade_status == APP_ANC_FADE_OUT) {
|
|
|
|
osSignalSet(anc_fade_thread_tid, FADE_OUT);
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (app_anc_fade_status == APP_ANC_FADE_IN) {
|
2022-08-15 04:20:27 -05:00
|
|
|
#ifdef ANC_LED_PIN
|
2023-02-01 14:52:54 -06:00
|
|
|
app_anc_switch_turnled(true);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
osSignalSet(anc_fade_thread_tid, FADE_IN);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (anc_set_dac_pa_delay) {
|
|
|
|
anc_set_dac_pa_delay = false;
|
|
|
|
// osDelay(600);
|
|
|
|
// analog_aud_enable_dac_pa(48);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
2023-02-01 14:52:54 -06:00
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void anc_fade_thread(void const *argument) {
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
osEvent evt;
|
|
|
|
evt.status = 0;
|
|
|
|
uint32_t singles = 0;
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
while (1) {
|
|
|
|
evt = osSignalWait(0, osWaitForever);
|
|
|
|
|
|
|
|
singles = evt.value.signals;
|
|
|
|
TRACE(2, "anc_fade_thread. %d", singles);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (evt.status == osEventSignal) {
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
switch (singles) {
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
case FADE_IN:
|
2022-08-15 04:20:27 -05:00
|
|
|
#if defined(ANC_FF_ENABLED) && defined(ANC_FB_ENABLED)
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_disable_gain_updated_when_pass0(0);
|
|
|
|
while (__anc_usb_app_fadein_ff_fb())
|
|
|
|
;
|
2022-08-15 04:20:27 -05:00
|
|
|
#else
|
|
|
|
#ifdef ANC_FF_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
while (__anc_usb_app_fadein(ANC_FEEDFORWARD))
|
|
|
|
;
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
#ifdef ANC_FB_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
while (__anc_usb_app_fadein(ANC_FEEDBACK))
|
|
|
|
;
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
#endif
|
2023-02-01 14:52:54 -06:00
|
|
|
app_anc_fade_status = APP_ANC_IDLE;
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
break;
|
|
|
|
case FADE_OUT:
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
#if defined(ANC_FF_ENABLED) && defined(ANC_FB_ENABLED)
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_disable_gain_updated_when_pass0(0);
|
|
|
|
while (__anc_usb_app_fadeout_ff_fb())
|
|
|
|
;
|
2022-08-15 04:20:27 -05:00
|
|
|
#else
|
|
|
|
#ifdef ANC_FF_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
while (__anc_usb_app_fadeout(ANC_FEEDFORWARD))
|
|
|
|
;
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
#ifdef ANC_FB_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
while (__anc_usb_app_fadeout(ANC_FEEDBACK))
|
|
|
|
;
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
app_anc_fade_status = APP_ANC_IDLE;
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
#ifdef ANC_FB_CHECK
|
2023-02-01 14:52:54 -06:00
|
|
|
hal_codec_anc_fb_check_set_irq_handler(anc_fb_check_irq_handler);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_fb_check_param();
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case CHANGE_FROM_ANC_TO_TT_DIRECTLY:
|
2022-08-15 04:20:27 -05:00
|
|
|
#if defined(ANC_FF_ENABLED) && defined(ANC_FB_ENABLED)
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_disable_gain_updated_when_pass0(0);
|
|
|
|
while (__anc_usb_app_fadeout_ff_fb())
|
|
|
|
;
|
2022-08-15 04:20:27 -05:00
|
|
|
#else
|
|
|
|
#ifdef ANC_FF_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
while (__anc_usb_app_fadeout(ANC_FEEDFORWARD))
|
|
|
|
;
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ANC_FB_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
while (__anc_usb_app_fadeout(ANC_FEEDBACK))
|
|
|
|
;
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(ANC_FF_ENABLED) && defined(ANC_FB_ENABLED)
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_select_coef(anc_sample_rate[AUD_STREAM_PLAYBACK], anc_coef_idx,
|
|
|
|
ANC_FEEDFORWARD, ANC_GAIN_DELAY);
|
|
|
|
anc_select_coef(anc_sample_rate[AUD_STREAM_PLAYBACK], anc_coef_idx,
|
|
|
|
ANC_FEEDBACK, ANC_GAIN_DELAY);
|
2022-08-15 04:20:27 -05:00
|
|
|
#ifdef AUDIO_ANC_FB_MC_HW
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_select_coef(anc_sample_rate[AUD_STREAM_PLAYBACK], anc_coef_idx,
|
|
|
|
ANC_MUSICCANCLE, ANC_GAIN_DELAY);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#ifdef ANC_FF_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_select_coef(anc_sample_rate[AUD_STREAM_PLAYBACK], anc_coef_idx,
|
|
|
|
ANC_FEEDFORWARD, ANC_GAIN_DELAY);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
2023-02-01 14:52:54 -06:00
|
|
|
|
2022-08-15 04:20:27 -05:00
|
|
|
#ifdef ANC_FB_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_select_coef(anc_sample_rate[AUD_STREAM_PLAYBACK], anc_coef_idx,
|
|
|
|
ANC_FEEDBACK, ANC_GAIN_DELAY);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(ANC_FF_ENABLED) && defined(ANC_FB_ENABLED)
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_disable_gain_updated_when_pass0(0);
|
|
|
|
while (__anc_usb_app_fadein_ff_fb())
|
|
|
|
;
|
2022-08-15 04:20:27 -05:00
|
|
|
#else
|
|
|
|
#ifdef ANC_FF_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
while (__anc_usb_app_fadein(ANC_FEEDFORWARD))
|
|
|
|
;
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
2023-02-01 14:52:54 -06:00
|
|
|
|
2022-08-15 04:20:27 -05:00
|
|
|
#ifdef ANC_FB_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
while (__anc_usb_app_fadein(ANC_FEEDBACK))
|
|
|
|
;
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
app_anc_fade_status = APP_ANC_IDLE;
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
// recommand to play "ANC SWITCH" prompt here...
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
} else {
|
|
|
|
TRACE(2, "anc fade thread evt error");
|
|
|
|
continue;
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
2023-02-01 14:52:54 -06:00
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_gain_fadein(void) {
|
|
|
|
APP_MESSAGE_BLOCK msg;
|
|
|
|
TRACE(1, " %s ", __func__);
|
|
|
|
if (app_poweroff_flag)
|
|
|
|
return;
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
app_anc_fade_status = APP_ANC_FADE_IN;
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
msg.mod_id = APP_MODUAL_ANC;
|
|
|
|
msg.msg_body.message_id = ANC_EVENT_FADE_IN;
|
|
|
|
app_mailbox_put(&msg);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_post_anc_codec_close(void) {
|
|
|
|
APP_MESSAGE_BLOCK msg;
|
|
|
|
TRACE(1, " %s ", __func__);
|
|
|
|
if (app_poweroff_flag)
|
|
|
|
return;
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
msg.mod_id = APP_MODUAL_ANC;
|
|
|
|
msg.msg_body.message_id = SIMPLE_PLAYER_CLOSE_CODEC_EVT;
|
|
|
|
app_mailbox_put(&msg);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_gain_fadeout(void) {
|
|
|
|
APP_MESSAGE_BLOCK msg;
|
|
|
|
TRACE(1, " %s ", __func__);
|
|
|
|
if (app_poweroff_flag)
|
|
|
|
return;
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
app_anc_fade_status = APP_ANC_FADE_OUT;
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
msg.mod_id = APP_MODUAL_ANC;
|
|
|
|
msg.msg_body.message_id = ANC_EVENT_FADE_OUT;
|
|
|
|
app_mailbox_put(&msg);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_status_post(uint8_t status) {
|
|
|
|
APP_MESSAGE_BLOCK msg;
|
|
|
|
TRACE(2, " %s status %d", __func__, status);
|
|
|
|
if (app_poweroff_flag)
|
|
|
|
return;
|
|
|
|
|
|
|
|
msg.mod_id = APP_MODUAL_ANC;
|
|
|
|
msg.msg_body.message_id = ANC_EVENT_CHANGE_STATUS;
|
|
|
|
msg.msg_body.message_Param0 = status;
|
|
|
|
app_mailbox_put(&msg);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_status_sync(uint8_t status) {
|
|
|
|
APP_MESSAGE_BLOCK msg;
|
|
|
|
TRACE(2, " %s status %d", __func__, status);
|
|
|
|
if (app_poweroff_flag)
|
|
|
|
return;
|
|
|
|
|
|
|
|
msg.mod_id = APP_MODUAL_ANC;
|
|
|
|
msg.msg_body.message_id = ANC_EVENT_SYNC_STATUS;
|
|
|
|
msg.msg_body.message_Param0 = status;
|
|
|
|
app_mailbox_put(&msg);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_status_sync_init(void) {
|
|
|
|
APP_MESSAGE_BLOCK msg;
|
|
|
|
TRACE(1, " %s ", __func__);
|
|
|
|
if (app_poweroff_flag)
|
|
|
|
return;
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
msg.mod_id = APP_MODUAL_ANC;
|
|
|
|
msg.msg_body.message_id = ANC_EVENT_SYNC_INIT;
|
|
|
|
app_mailbox_put(&msg);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_do_init(void) {
|
|
|
|
APP_MESSAGE_BLOCK msg;
|
|
|
|
TRACE(1, " %s DO INIT", __func__);
|
|
|
|
if (app_poweroff_flag)
|
|
|
|
return;
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
msg.mod_id = APP_MODUAL_ANC;
|
|
|
|
msg.msg_body.message_id = ANC_EVENT_INIT;
|
|
|
|
app_mailbox_put(&msg);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_send_howl_evt(uint32_t howl) {
|
|
|
|
APP_MESSAGE_BLOCK msg;
|
|
|
|
TRACE(2, " %s %d", __func__, howl);
|
|
|
|
if (app_poweroff_flag)
|
|
|
|
return;
|
|
|
|
|
|
|
|
msg.mod_id = APP_MODUAL_ANC;
|
|
|
|
msg.msg_body.message_id = ANC_EVENT_HOWL_PROCESS;
|
|
|
|
msg.msg_body.message_Param0 = howl;
|
|
|
|
app_mailbox_put(&msg);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_send_pwr_key_monitor_evt(uint8_t level) {
|
|
|
|
APP_MESSAGE_BLOCK msg;
|
|
|
|
TRACE(2, " %s %d", __func__, level);
|
|
|
|
if (app_poweroff_flag)
|
|
|
|
return;
|
|
|
|
|
|
|
|
msg.mod_id = APP_MODUAL_ANC;
|
|
|
|
msg.msg_body.message_id = ANC_EVENT_PWR_KEY_MONITOR;
|
|
|
|
msg.msg_body.message_Param0 = level;
|
|
|
|
app_mailbox_put(&msg);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extern void simple_player_delay_stop(void);
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_post_simplayer_stop_evt(void) {
|
|
|
|
APP_MESSAGE_BLOCK msg;
|
|
|
|
TRACE(1, " %s d", __func__);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
msg.mod_id = APP_MODUAL_ANC;
|
|
|
|
msg.msg_body.message_id = SIMPLE_PLAYER_DELAY_STOP_EVT;
|
|
|
|
msg.msg_body.message_Param0 = 0;
|
|
|
|
app_mailbox_put(&msg);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
bool anc_enabled(void) { return (anc_work_status == ANC_STATUS_ON); }
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_resample(uint32_t res_ratio, uint32_t *in, uint32_t *out,
|
|
|
|
uint32_t samples) {
|
|
|
|
uint32_t flag = int_lock();
|
|
|
|
for (int i = samples; i > 0; i--) {
|
|
|
|
for (int j = 0; j < res_ratio; j++) {
|
|
|
|
*(out + (i - 1) * res_ratio + j) = *(in + i - 1);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
2023-02-01 14:52:54 -06:00
|
|
|
}
|
|
|
|
int_unlock(flag);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_select_coef(void) {
|
|
|
|
TRACE(2, "enter %s %d\n", __FUNCTION__, __LINE__);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
#ifdef ANC_FF_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_select_coef(anc_sample_rate[AUD_STREAM_PLAYBACK], anc_coef_idx,
|
|
|
|
ANC_FEEDFORWARD, ANC_GAIN_DELAY);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ANC_FB_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_select_coef(anc_sample_rate[AUD_STREAM_PLAYBACK], anc_coef_idx,
|
|
|
|
ANC_FEEDBACK, ANC_GAIN_DELAY);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef AUDIO_ANC_FB_MC_HW
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_select_coef(anc_sample_rate[AUD_STREAM_PLAYBACK], anc_coef_idx,
|
|
|
|
ANC_MUSICCANCLE, ANC_GAIN_NO_DELAY);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
TRACE(2, "exit %s %d\n", __FUNCTION__, __LINE__);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_enable(void) {
|
|
|
|
TRACE(2, "enter %s %d\n", __FUNCTION__, __LINE__);
|
|
|
|
// anc_active_codec();
|
2022-08-15 04:20:27 -05:00
|
|
|
#ifndef __SIMPLE_INTERNAL_PLAYER_SUPPORT__
|
2023-02-01 14:52:54 -06:00
|
|
|
analog_aud_codec_speaker_enable(true);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
#ifdef ANC_FF_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_open(ANC_FEEDFORWARD);
|
|
|
|
anc_select_coef(anc_sample_rate[AUD_STREAM_PLAYBACK], anc_coef_idx,
|
|
|
|
ANC_FEEDFORWARD, ANC_GAIN_DELAY);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ANC_FB_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_open(ANC_FEEDBACK);
|
|
|
|
anc_select_coef(anc_sample_rate[AUD_STREAM_PLAYBACK], anc_coef_idx,
|
|
|
|
ANC_FEEDBACK, ANC_GAIN_DELAY);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef AUDIO_ANC_FB_MC_HW
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_open(ANC_MUSICCANCLE);
|
|
|
|
anc_select_coef(anc_sample_rate[AUD_STREAM_PLAYBACK], anc_coef_idx,
|
|
|
|
ANC_MUSICCANCLE, ANC_GAIN_NO_DELAY);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef AUDIO_ANC_FB_MC_HW
|
2023-02-01 14:52:54 -06:00
|
|
|
int32_t gain_ch_l = 0;
|
|
|
|
int32_t gain_ch_r = 0;
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_get_cfg_gain(&gain_ch_l, &gain_ch_r, ANC_MUSICCANCLE);
|
|
|
|
anc_set_gain(gain_ch_l, gain_ch_r, ANC_MUSICCANCLE);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(ANC_WNR_ENABLED)
|
2023-02-01 14:52:54 -06:00
|
|
|
if (is_sco_mode()) {
|
|
|
|
anc_wnr_open(ANC_WNR_OPEN_MODE_CONFIGURE);
|
|
|
|
} else {
|
|
|
|
anc_wnr_open(ANC_WNR_OPEN_MODE_STANDALONE);
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(ANC_ASSIST_ENABLED)
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_assist_open(ANC_ASSIST_STANDALONE);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
TRACE(2, "exit %s %d\n", __FUNCTION__, __LINE__);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_disable(void) {
|
|
|
|
TRACE(2, "enter %s %d\n", __FUNCTION__, __LINE__);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
#if defined(ANC_WNR_ENABLED)
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_wnr_close();
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(ANC_ASSIST_ENABLED)
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_assist_close();
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef AUDIO_ANC_FB_MC_HW
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_close(ANC_MUSICCANCLE);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ANC_FF_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_close(ANC_FEEDFORWARD);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ANC_FB_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_close(ANC_FEEDBACK);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
TRACE(2, "exit %s %d\n", __FUNCTION__, __LINE__);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
static void anc_sample_rate_change(enum AUD_STREAM_T stream,
|
|
|
|
enum AUD_SAMPRATE_T rate,
|
|
|
|
enum AUD_SAMPRATE_T *new_play,
|
|
|
|
enum AUD_SAMPRATE_T *new_cap) {
|
|
|
|
enum AUD_SAMPRATE_T play_rate, cap_rate;
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (anc_sample_rate[stream] != rate) {
|
2022-08-15 04:20:27 -05:00
|
|
|
#ifdef CHIP_BEST1000
|
2023-02-01 14:52:54 -06:00
|
|
|
if (stream == AUD_STREAM_PLAYBACK) {
|
|
|
|
play_rate = rate;
|
|
|
|
cap_rate = rate * (anc_sample_rate[AUD_STREAM_CAPTURE] /
|
|
|
|
anc_sample_rate[AUD_STREAM_PLAYBACK]);
|
|
|
|
} else {
|
|
|
|
play_rate = rate / (anc_sample_rate[AUD_STREAM_CAPTURE] /
|
|
|
|
anc_sample_rate[AUD_STREAM_PLAYBACK]);
|
|
|
|
cap_rate = rate;
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
#else
|
2023-02-01 14:52:54 -06:00
|
|
|
play_rate = rate;
|
|
|
|
cap_rate = rate;
|
2022-08-15 04:20:27 -05:00
|
|
|
#ifdef ANC_FF_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_select_coef(play_rate, anc_coef_idx, ANC_FEEDFORWARD,
|
|
|
|
ANC_GAIN_NO_DELAY);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ANC_FB_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_select_coef(play_rate, anc_coef_idx, ANC_FEEDBACK, ANC_GAIN_NO_DELAY);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef AUDIO_ANC_FB_MC_HW
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_select_coef(play_rate, anc_coef_idx, ANC_MUSICCANCLE,
|
|
|
|
ANC_GAIN_NO_DELAY);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
#endif // CHIP_BEST1000
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
TRACE(5, "%s: Update anc sample rate from %u/%u to %u/%u", __func__,
|
|
|
|
anc_sample_rate[AUD_STREAM_PLAYBACK],
|
|
|
|
anc_sample_rate[AUD_STREAM_CAPTURE], play_rate, cap_rate);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (new_play) {
|
|
|
|
*new_play = play_rate;
|
|
|
|
}
|
|
|
|
if (new_cap) {
|
|
|
|
*new_cap = cap_rate;
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
2023-02-01 14:52:54 -06:00
|
|
|
|
|
|
|
anc_sample_rate[AUD_STREAM_PLAYBACK] = play_rate;
|
|
|
|
anc_sample_rate[AUD_STREAM_CAPTURE] = cap_rate;
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_open_anc(void) {
|
|
|
|
enum AUD_SAMPRATE_T playback_rate;
|
|
|
|
enum AUD_SAMPRATE_T capture_rate;
|
|
|
|
AF_ANC_HANDLER handler;
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
TRACE(2, "enter %s %d\n", __FUNCTION__, __LINE__);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
handler = anc_sample_rate_change;
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
#ifdef __SIMPLE_INTERNAL_PLAYER_SUPPORT__
|
2023-02-01 14:52:54 -06:00
|
|
|
app_start_player_by_anc();
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (anc_sample_rate[AUD_STREAM_PLAYBACK] == AUD_SAMPRATE_NULL) {
|
2022-08-15 04:20:27 -05:00
|
|
|
#ifdef CHIP_BEST1000
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_sample_rate[AUD_STREAM_PLAYBACK] = AUD_SAMPRATE_96000;
|
|
|
|
anc_sample_rate[AUD_STREAM_CAPTURE] = AUD_SAMPRATE_384000;
|
2022-08-15 04:20:27 -05:00
|
|
|
#else // !CHIP_BEST1000
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_sample_rate[AUD_STREAM_PLAYBACK] = AUD_SAMPRATE_48000;
|
|
|
|
anc_sample_rate[AUD_STREAM_CAPTURE] = AUD_SAMPRATE_48000;
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_sample_rate[AUD_STREAM_PLAYBACK] =
|
|
|
|
hal_codec_anc_convert_rate(anc_sample_rate[AUD_STREAM_PLAYBACK]);
|
|
|
|
anc_sample_rate[AUD_STREAM_CAPTURE] =
|
|
|
|
hal_codec_anc_convert_rate(anc_sample_rate[AUD_STREAM_CAPTURE]);
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
playback_rate = anc_sample_rate[AUD_STREAM_PLAYBACK];
|
|
|
|
capture_rate = anc_sample_rate[AUD_STREAM_CAPTURE];
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
pmu_anc_config(1);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
#ifdef ANC_FF_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
af_anc_open(ANC_FEEDFORWARD, playback_rate, capture_rate, handler);
|
|
|
|
anc_open(ANC_FEEDFORWARD);
|
|
|
|
anc_select_coef(anc_sample_rate[AUD_STREAM_PLAYBACK], anc_coef_idx,
|
|
|
|
ANC_FEEDFORWARD, ANC_GAIN_DELAY);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ANC_FB_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
af_anc_open(ANC_FEEDBACK, playback_rate, capture_rate, handler);
|
|
|
|
anc_open(ANC_FEEDBACK);
|
|
|
|
anc_select_coef(anc_sample_rate[AUD_STREAM_PLAYBACK], anc_coef_idx,
|
|
|
|
ANC_FEEDBACK, ANC_GAIN_DELAY);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef AUDIO_ANC_FB_MC_HW
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_open(ANC_MUSICCANCLE);
|
|
|
|
anc_select_coef(anc_sample_rate[AUD_STREAM_PLAYBACK], anc_coef_idx,
|
|
|
|
ANC_MUSICCANCLE, ANC_GAIN_NO_DELAY);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(AUDIO_ANC_TT_HW)
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_open(ANC_TALKTHRU);
|
|
|
|
anc_select_coef(anc_sample_rate[AUD_STREAM_PLAYBACK], anc_coef_idx,
|
|
|
|
ANC_TALKTHRU, ANC_GAIN_NO_DELAY);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef AUDIO_ANC_FB_MC_HW
|
2023-02-01 14:52:54 -06:00
|
|
|
int32_t gain_ch_l = 0;
|
|
|
|
int32_t gain_ch_r = 0;
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_get_cfg_gain(&gain_ch_l, &gain_ch_r, ANC_MUSICCANCLE);
|
|
|
|
anc_set_gain(gain_ch_l, gain_ch_r, ANC_MUSICCANCLE);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(ANC_WNR_ENABLED)
|
2023-02-01 14:52:54 -06:00
|
|
|
if (is_sco_mode()) {
|
|
|
|
anc_wnr_open(ANC_WNR_OPEN_MODE_CONFIGURE);
|
|
|
|
} else {
|
|
|
|
anc_wnr_open(ANC_WNR_OPEN_MODE_STANDALONE);
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
#if defined(ANC_ASSIST_ENABLED)
|
|
|
|
anc_assist_open(ANC_ASSIST_STANDALONE);
|
|
|
|
#endif
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
TRACE(2, "exit %s %d\n", __FUNCTION__, __LINE__);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_close_anc(void) {
|
|
|
|
TRACE(2, "enter %s %d\n", __FUNCTION__, __LINE__);
|
|
|
|
anc_set_dac_pa_delay = false;
|
2022-08-15 04:20:27 -05:00
|
|
|
#ifdef __SUPPORT_ANC_SINGLE_MODE_WITHOUT_BT__
|
2023-02-01 14:52:54 -06:00
|
|
|
if (anc_single_mode_is_on()) {
|
|
|
|
analog_aud_codec_speaker_enable(false);
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ANC_WNR_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_wnr_close();
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(ANC_ASSIST_ENABLED)
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_assist_close();
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(AUDIO_ANC_TT_HW)
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_close(ANC_TALKTHRU);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ANC_FF_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_close(ANC_FEEDFORWARD);
|
|
|
|
af_anc_close(ANC_FEEDFORWARD);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ANC_FB_ENABLED
|
|
|
|
#if defined(AUDIO_ANC_FB_MC_HW)
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_close(ANC_MUSICCANCLE);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_close(ANC_FEEDBACK);
|
|
|
|
af_anc_close(ANC_FEEDBACK);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
pmu_anc_config(0);
|
2022-08-15 04:20:27 -05:00
|
|
|
#ifdef __SIMPLE_INTERNAL_PLAYER_SUPPORT__
|
2023-02-01 14:52:54 -06:00
|
|
|
app_stop_player_by_anc();
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
2023-02-01 14:52:54 -06:00
|
|
|
TRACE(2, "exit %s %d\n", __FUNCTION__, __LINE__);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_bitrate_reopen(void) {
|
|
|
|
TRACE(2, " %s status %d", __func__, anc_work_status);
|
|
|
|
if (anc_work_status == ANC_STATUS_INIT_ON) {
|
|
|
|
// hal_codec_reconfig_pll_freq(playback_rate, capture_rate);
|
|
|
|
} else if (anc_work_status == ANC_STATUS_ON) {
|
2022-08-15 04:20:27 -05:00
|
|
|
// hal_codec_reconfig_pll_freq(playback_rate, capture_rate);
|
2023-02-01 14:52:54 -06:00
|
|
|
} else if (anc_work_status == ANC_STATUS_WAITING_ON) {
|
|
|
|
} else {
|
|
|
|
TRACE(0, "no deal situation.");
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_status_change(void) {
|
2022-08-15 04:20:27 -05:00
|
|
|
#if defined(IBRT)
|
2023-02-01 14:52:54 -06:00
|
|
|
ibrt_ctrl_t *p_ibrt_ctrl = app_tws_ibrt_get_bt_ctrl_ctx();
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (p_ibrt_ctrl->current_role == IBRT_SLAVE) {
|
|
|
|
anc_coef_idx = anc_peer_coef_idx;
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
TRACE(3, "%s anc_work_status: %d anc_coef_idx: %d", __func__, anc_work_status,
|
|
|
|
anc_coef_idx);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
switch (anc_work_status) {
|
|
|
|
case ANC_STATUS_OFF:
|
|
|
|
anc_work_status = ANC_STATUS_WAITING_ON;
|
|
|
|
app_anc_timer_set(ANC_EVENT_OPEN, anc_switch_on_time);
|
|
|
|
app_anc_open_anc();
|
|
|
|
break;
|
|
|
|
case ANC_STATUS_ON:
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
#if (ANC_COEF_LIST_NUM > 1)
|
|
|
|
|
|
|
|
#ifdef __BT_ANC_KEY__
|
2023-02-01 14:52:54 -06:00
|
|
|
if (anc_status_sync_flag == false) {
|
|
|
|
anc_coef_idx++;
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (anc_coef_idx < ANC_COEF_LIST_NUM) {
|
|
|
|
TRACE(2, " %s set coef idx: %d ", __func__, anc_coef_idx);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
#ifdef ANC_MODE_SWITCH_WITHOUT_FADE
|
|
|
|
|
|
|
|
#ifdef ANC_FF_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_select_coef(anc_sample_rate[AUD_STREAM_PLAYBACK], anc_coef_idx,
|
|
|
|
ANC_FEEDFORWARD, ANC_GAIN_NO_DELAY);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
#ifdef ANC_FB_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_select_coef(anc_sample_rate[AUD_STREAM_PLAYBACK], anc_coef_idx,
|
|
|
|
ANC_FEEDBACK, ANC_GAIN_NO_DELAY);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
#ifdef AUDIO_ANC_FB_MC_HW
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_select_coef(anc_sample_rate[AUD_STREAM_PLAYBACK], anc_coef_idx,
|
|
|
|
ANC_MUSICCANCLE, ANC_GAIN_NO_DELAY);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
// recommand to play "ANC SWITCH" prompt here...
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
#else
|
2023-02-01 14:52:54 -06:00
|
|
|
osSignalSet(anc_fade_thread_tid, CHANGE_FROM_ANC_TO_TT_DIRECTLY);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
anc_coef_idx = 0;
|
|
|
|
app_anc_timer_set(ANC_EVENT_CLOSE, anc_close_delay_time);
|
|
|
|
app_anc_gain_fadeout();
|
|
|
|
anc_work_status = ANC_STATUS_WAITING_OFF;
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
2023-02-01 14:52:54 -06:00
|
|
|
#else
|
|
|
|
anc_coef_idx = 0;
|
|
|
|
app_anc_timer_set(ANC_EVENT_CLOSE, anc_close_delay_time);
|
|
|
|
app_anc_gain_fadeout();
|
|
|
|
anc_work_status = ANC_STATUS_WAITING_OFF;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case ANC_STATUS_INIT_ON:
|
|
|
|
app_anc_select_coef();
|
|
|
|
app_anc_gain_fadein();
|
|
|
|
anc_work_status = ANC_STATUS_WAITING_ON;
|
|
|
|
app_anc_timer_close();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (anc_status_sync_flag == true)
|
|
|
|
anc_status_sync_flag = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int app_anc_handle_process(APP_MESSAGE_BODY *msg_body) {
|
|
|
|
uint32_t evt = msg_body->message_id;
|
|
|
|
uint32_t arg0 = msg_body->message_Param0;
|
|
|
|
|
|
|
|
TRACE(4, " %s evt: %d, arg0: %d , anc status :%d", __func__, evt, arg0,
|
|
|
|
anc_work_status);
|
|
|
|
|
|
|
|
switch (evt) {
|
|
|
|
case ANC_EVENT_INIT:
|
|
|
|
// init anc_timer
|
|
|
|
app_anc_init_timer();
|
|
|
|
// init anc&open codec
|
2022-08-15 04:20:27 -05:00
|
|
|
#ifdef __AUDIO_SECTION_SUPPT__
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_load_cfg();
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __ANC_INIT_ON__
|
2023-02-01 14:52:54 -06:00
|
|
|
app_anc_open_anc();
|
|
|
|
anc_work_status = ANC_STATUS_INIT_ON;
|
|
|
|
app_anc_timer_set(ANC_EVENT_CLOSE, anc_init_switch_off_time);
|
2022-08-15 04:20:27 -05:00
|
|
|
#else
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_work_status = ANC_STATUS_OFF;
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
#ifdef __ANC_STICK_SWITCH_USE_GPIO__
|
2023-02-01 14:52:54 -06:00
|
|
|
if (app_anc_switch_get_val() == ANC_SWITCH_ON_LEVEL) {
|
|
|
|
app_anc_switch_set_edge(ANC_SWITCH_ON_LEVEL);
|
|
|
|
app_anc_status_post(ANC_SWITCH_ON_LEVEL);
|
|
|
|
} else {
|
|
|
|
app_anc_switch_set_edge(!(bool)ANC_SWITCH_ON_LEVEL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// anc_coef_idx = 0;
|
|
|
|
anc_fade_thread_tid = osThreadCreate(osThread(anc_fade_thread), NULL);
|
|
|
|
break;
|
|
|
|
case ANC_EVENT_FADE_IN:
|
|
|
|
case ANC_EVENT_FADE_OUT:
|
|
|
|
anc_gain_fade_handle();
|
|
|
|
if (evt == ANC_EVENT_FADE_IN) {
|
|
|
|
anc_work_status = ANC_STATUS_ON;
|
|
|
|
// recommand to play "ANC ON" prompt here...
|
|
|
|
app_voice_report(APP_STATUS_INDICATION_ALEXA_START,
|
|
|
|
0); // close latlatency mode
|
|
|
|
}
|
|
|
|
if (evt == ANC_EVENT_FADE_OUT) {
|
|
|
|
anc_work_status = ANC_STATUS_INIT_ON;
|
|
|
|
// recommand to play "ANC OFF" prompt here...
|
|
|
|
app_voice_report(APP_STATUS_INDICATION_ALEXA_STOP,
|
|
|
|
0); // close latlatency mode
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ANC_EVENT_CHANGE_SAMPLERATE:
|
|
|
|
app_anc_bitrate_reopen();
|
|
|
|
break;
|
|
|
|
case ANC_EVENT_CHANGE_STATUS:
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
#ifdef __ANC_STICK_SWITCH_USE_GPIO__
|
2023-02-01 14:52:54 -06:00
|
|
|
osDelay(10);
|
|
|
|
if (app_anc_switch_get_val() != (bool)arg0) { // debonce
|
|
|
|
TRACE(0, "io level not equeue exit");
|
|
|
|
break;
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
#ifdef __SUPPORT_ANC_SINGLE_MODE_WITHOUT_BT__
|
2023-02-01 14:52:54 -06:00
|
|
|
if (anc_single_mode_is_on()) {
|
|
|
|
if (arg0 != ANC_SWITCH_ON_LEVEL) {
|
|
|
|
TRACE(1, " Anc ON but exit %d ", app_init_done);
|
|
|
|
if (app_init_done) {
|
|
|
|
app_anc_shutdown = true;
|
|
|
|
app_shutdown();
|
|
|
|
} else {
|
|
|
|
app_anc_timer_set(ANC_EVENT_SWITCH_KEY_DEBONCE,
|
|
|
|
anc_switch_key_debonce_time);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (arg0 != ANC_SWITCH_ON_LEVEL)
|
|
|
|
app_anc_switch_set_edge((!(bool)ANC_SWITCH_ON_LEVEL));
|
|
|
|
else
|
|
|
|
app_anc_switch_set_edge(ANC_SWITCH_ON_LEVEL);
|
|
|
|
|
|
|
|
if (((arg0 == ANC_SWITCH_ON_LEVEL) && (anc_work_status == ANC_STATUS_ON)) ||
|
|
|
|
((arg0 != ANC_SWITCH_ON_LEVEL) &&
|
|
|
|
(anc_work_status == ANC_STATUS_OFF))) {
|
|
|
|
// status same, no handle
|
|
|
|
TRACE(0, " Anc NOT ON, exit");
|
|
|
|
return 0;
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(IBRT)
|
2023-02-01 14:52:54 -06:00
|
|
|
if (((BOOL)arg0 == false) && (app_anc_work_status() == false)) {
|
|
|
|
anc_coef_idx = 0;
|
|
|
|
anc_peer_coef_idx = 0;
|
|
|
|
anc_status_sync_flag = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (((BOOL)arg0 == true) && (app_anc_work_status() == true) &&
|
|
|
|
(anc_coef_idx == anc_peer_coef_idx)) {
|
|
|
|
anc_status_sync_flag = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
app_anc_status_change();
|
|
|
|
|
|
|
|
break;
|
|
|
|
case ANC_EVENT_SYNC_STATUS:
|
2022-08-15 04:20:27 -05:00
|
|
|
#if defined(IBRT)
|
2023-02-01 14:52:54 -06:00
|
|
|
TRACE(4, "%s anc_work_status %d--->%d anc_coef_idx:%d ", __func__,
|
|
|
|
anc_work_status, arg0, anc_coef_idx);
|
|
|
|
|
|
|
|
if (ANC_STATUS_ON == arg0) {
|
|
|
|
arg0 = 1;
|
|
|
|
} else {
|
|
|
|
arg0 = 0;
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
uint8_t buf[3] = {0};
|
|
|
|
buf[0] = (uint8_t)arg0;
|
|
|
|
buf[1] = (uint8_t)anc_coef_idx;
|
|
|
|
buf[2] = false; // set peer anc_status_sync_flag = false
|
|
|
|
tws_ctrl_send_cmd(APP_TWS_CMD_SYNC_ANC_STATUS, buf, 3);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case ANC_EVENT_SYNC_INIT:
|
|
|
|
break;
|
|
|
|
case ANC_EVENT_HOWL_PROCESS:
|
|
|
|
// disable anc,set max gain,enable anc
|
2022-08-15 04:20:27 -05:00
|
|
|
{
|
2023-02-01 14:52:54 -06:00
|
|
|
// uint32_t howl = arg0;
|
|
|
|
if (anc_work_status == ANC_STATUS_ON) {
|
|
|
|
//__anc_usb_app_howl_set_gain(howl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#ifdef __SUPPORT_ANC_SINGLE_MODE_WITHOUT_BT__
|
|
|
|
case ANC_EVENT_PWR_KEY_MONITOR:
|
|
|
|
osDelay(10);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (app_pwr_key_monitor_get_val() != (bool)arg0) // debonce
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ((bool)arg0) {
|
|
|
|
app_anc_timer_set(ANC_EVENT_PWR_KEY_MONITOR_REBOOT,
|
|
|
|
anc_pwr_key_monitor_time);
|
|
|
|
app_pwr_key_monitor_set_int_edge(1);
|
|
|
|
} else {
|
|
|
|
hwtimer_stop(anc_timerid);
|
|
|
|
app_pwr_key_monitor_set_int_edge(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case SIMPLE_PLAYER_DELAY_STOP_EVT:
|
|
|
|
#ifdef __SIMPLE_INTERNAL_PLAYER_SUPPORT__
|
|
|
|
simple_player_delay_stop();
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case SIMPLE_PLAYER_CLOSE_CODEC_EVT:
|
|
|
|
app_anc_close_anc();
|
|
|
|
anc_work_status = ANC_STATUS_OFF;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void anc_gain_fade_timer_handler(void *p) {
|
|
|
|
TRACE(2, " %s , request %d ", __func__, anc_timer_request);
|
|
|
|
switch (anc_timer_request) {
|
|
|
|
case ANC_EVENT_OPEN:
|
|
|
|
app_anc_gain_fadein();
|
|
|
|
break;
|
|
|
|
case ANC_EVENT_CLOSE:
|
|
|
|
// real close
|
|
|
|
// app_anc_close_anc();
|
|
|
|
// anc_work_status = ANC_STATUS_OFF;
|
|
|
|
app_anc_post_anc_codec_close();
|
|
|
|
|
|
|
|
break;
|
|
|
|
case ANC_EVENT_PWR_KEY_MONITOR_REBOOT:
|
|
|
|
TRACE(0, " reboot !");
|
|
|
|
hal_sw_bootmode_clear(HAL_SW_BOOTMODE_REBOOT);
|
|
|
|
hal_sw_bootmode_clear(HAL_SW_BOOTMODE_REBOOT_ANC_ON);
|
|
|
|
hal_sw_bootmode_set(HAL_SW_BOOTMODE_REBOOT_BT_ON);
|
|
|
|
app_reset();
|
|
|
|
break;
|
|
|
|
case ANC_EVENT_SWITCH_KEY_DEBONCE:
|
|
|
|
TRACE(0, " ANC SWITCH KEY ");
|
2022-08-15 04:20:27 -05:00
|
|
|
#ifdef __ANC_STICK_SWITCH_USE_GPIO__
|
2023-02-01 14:52:54 -06:00
|
|
|
if (app_switch_callback)
|
|
|
|
app_switch_callback(
|
|
|
|
hal_gpio_pin_get_val((enum HAL_GPIO_PIN_T)ANC_SWITCH_PIN));
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
2023-02-01 14:52:54 -06:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
anc_timer_request = ANC_EVENT_NONE;
|
|
|
|
hwtimer_stop(anc_timerid);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_init_timer(void) {
|
|
|
|
if (anc_timerid == NULL)
|
|
|
|
anc_timerid = hwtimer_alloc((HWTIMER_CALLBACK_T)anc_gain_fade_timer_handler,
|
|
|
|
&anc_timer_request);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_timer_set(uint32_t request, uint32_t delay) {
|
|
|
|
TRACE(3, " %s request %d , delay %ds ", __func__, request,
|
|
|
|
(TICKS_TO_MS(delay) / 1000));
|
|
|
|
if (anc_timerid == NULL)
|
|
|
|
return;
|
|
|
|
anc_timer_request = request;
|
|
|
|
hwtimer_stop(anc_timerid);
|
|
|
|
hwtimer_start(anc_timerid, delay);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_timer_close(void) {
|
|
|
|
TRACE(3, " %s enter...", __func__);
|
|
|
|
if (anc_timerid == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
anc_timer_request = ANC_EVENT_NONE;
|
|
|
|
hwtimer_stop(anc_timerid);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(IBRT)
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_cmd_receive_process(uint8_t *buf, uint16_t len) {
|
|
|
|
TRACE(1, "[%s] enter...", __func__);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (buf[0] == IBRT_ACTION_ANC_NOTIRY_MASTER_EXCHANGE_COEF) {
|
|
|
|
switch (buf[1]) {
|
2022-08-15 04:20:27 -05:00
|
|
|
#ifdef __BT_ANC_KEY__
|
2023-02-01 14:52:54 -06:00
|
|
|
case 0:
|
|
|
|
app_anc_key(NULL, NULL);
|
|
|
|
break; // slave invoke app_anc_key();
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
#ifndef __BT_ANC_KEY__
|
2023-02-01 14:52:54 -06:00
|
|
|
case 1:
|
|
|
|
app_anc_start();
|
|
|
|
break; // slave invoke app_anc_start();
|
|
|
|
case 2:
|
|
|
|
app_anc_switch_coef(buf[2]);
|
|
|
|
break; // slave invoke app_anc_switch_coef();
|
|
|
|
case 3:
|
|
|
|
app_anc_stop();
|
|
|
|
break; // slave invoke app_anc_stop();
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
2023-02-01 14:52:54 -06:00
|
|
|
default:
|
|
|
|
TRACE(1, "[%s] cmd invalid...", __func__);
|
|
|
|
break;
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
2023-02-01 14:52:54 -06:00
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
static void app_anc_notify_master_to_exchange_coef(uint8_t arg0, uint8_t arg1) {
|
|
|
|
uint8_t buf[3] = {0};
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
TRACE(3, "[%s] arg0:%d arg1:%d", __func__, arg0, arg1);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
buf[0] = IBRT_ACTION_ANC_NOTIRY_MASTER_EXCHANGE_COEF;
|
|
|
|
buf[1] = arg0;
|
|
|
|
buf[2] = arg1;
|
|
|
|
|
|
|
|
app_ibrt_ui_send_user_action(buf, 3);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __BT_ANC_KEY__
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_key(APP_KEY_STATUS *status, void *param) {
|
|
|
|
TRACE(3, "%s anc_work_st:%d , timer:%d", __func__, anc_work_status,
|
|
|
|
anc_timer_request);
|
|
|
|
bool flag = app_anc_work_status();
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
#if defined(IBRT)
|
2023-02-01 14:52:54 -06:00
|
|
|
ibrt_ctrl_t *p_ibrt_ctrl = app_tws_ibrt_get_bt_ctrl_ctx();
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
TRACE(2, "[%s] current_role: %d", __func__, p_ibrt_ctrl->current_role);
|
|
|
|
if (p_ibrt_ctrl->current_role == IBRT_SLAVE) {
|
|
|
|
app_anc_notify_master_to_exchange_coef(0, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
app_anc_status_sync(!flag);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
2023-02-01 14:52:54 -06:00
|
|
|
app_anc_status_post(!flag);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __BT_ANC_KEY__
|
2023-02-01 14:52:54 -06:00
|
|
|
int app_anc_start(void) {
|
|
|
|
TRACE(1, "[%s] enter...", __func__);
|
|
|
|
bool flag = app_anc_work_status();
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (flag == true) {
|
|
|
|
TRACE(1, "[%s] anc has been on...", __func__);
|
|
|
|
return -1;
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
#if defined(IBRT)
|
2023-02-01 14:52:54 -06:00
|
|
|
ibrt_ctrl_t *p_ibrt_ctrl = app_tws_ibrt_get_bt_ctrl_ctx();
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
TRACE(2, "[%s] current_role: %d", __func__, p_ibrt_ctrl->current_role);
|
|
|
|
if (p_ibrt_ctrl->current_role == IBRT_SLAVE) {
|
|
|
|
app_anc_notify_master_to_exchange_coef(1, 0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
anc_coef_idx = 0;
|
|
|
|
app_anc_status_sync(!flag);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_coef_idx = 0;
|
|
|
|
app_anc_status_post(!flag);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
return 0;
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
int app_anc_switch_coef(uint8_t index) {
|
|
|
|
TRACE(1, "[%s] enter...", __func__);
|
|
|
|
bool flag = app_anc_work_status();
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (flag == false) {
|
|
|
|
TRACE(1, "[%s] anc has been off...", __func__);
|
|
|
|
return -1;
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (index == anc_coef_idx) {
|
|
|
|
TRACE(1, "[%s] anc coef has been load...", __func__);
|
|
|
|
return -1;
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
#if defined(IBRT)
|
2023-02-01 14:52:54 -06:00
|
|
|
ibrt_ctrl_t *p_ibrt_ctrl = app_tws_ibrt_get_bt_ctrl_ctx();
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
TRACE(2, "[%s] current_role: %d", __func__, p_ibrt_ctrl->current_role);
|
|
|
|
if (p_ibrt_ctrl->current_role == IBRT_SLAVE) {
|
|
|
|
app_anc_notify_master_to_exchange_coef(2, index);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
anc_coef_idx = index;
|
|
|
|
app_anc_status_sync(!flag);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_coef_idx = index;
|
|
|
|
app_anc_status_post(!flag);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
return 0;
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
int app_anc_stop(void) {
|
|
|
|
TRACE(1, "[%s] enter...", __func__);
|
|
|
|
bool flag = app_anc_work_status();
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (flag == false) {
|
|
|
|
TRACE(1, "[%s] anc has been off...", __func__);
|
|
|
|
return -1;
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
#if defined(IBRT)
|
2023-02-01 14:52:54 -06:00
|
|
|
ibrt_ctrl_t *p_ibrt_ctrl = app_tws_ibrt_get_bt_ctrl_ctx();
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
TRACE(2, "[%s] current_role: %d", __func__, p_ibrt_ctrl->current_role);
|
|
|
|
if (p_ibrt_ctrl->current_role == IBRT_SLAVE) {
|
|
|
|
app_anc_notify_master_to_exchange_coef(3, 0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
anc_coef_idx = ANC_COEF_NUM;
|
|
|
|
app_anc_status_sync(!flag);
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_coef_idx = ANC_COEF_NUM;
|
|
|
|
app_anc_status_post(!flag);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
return 0;
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_ios_init(void) {
|
2022-08-15 04:20:27 -05:00
|
|
|
#ifdef __ANC_STICK_SWITCH_USE_GPIO__
|
2023-02-01 14:52:54 -06:00
|
|
|
if (cfg_anc_switch.pin != HAL_IOMUX_PIN_NUM) {
|
|
|
|
hal_iomux_init((struct HAL_IOMUX_PIN_FUNCTION_MAP *)&cfg_anc_switch, 1);
|
|
|
|
hal_gpio_pin_set_dir((enum HAL_GPIO_PIN_T)cfg_anc_switch.pin,
|
|
|
|
HAL_GPIO_DIR_IN, 0);
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
#ifdef __SUPPORT_ANC_SINGLE_MODE_WITHOUT_BT__
|
2023-02-01 14:52:54 -06:00
|
|
|
if (cfg_pwr_key_monitor.pin != HAL_IOMUX_PIN_NUM) {
|
|
|
|
hal_iomux_init((struct HAL_IOMUX_PIN_FUNCTION_MAP *)&cfg_pwr_key_monitor,
|
|
|
|
1);
|
|
|
|
hal_gpio_pin_set_dir((enum HAL_GPIO_PIN_T)cfg_pwr_key_monitor.pin,
|
|
|
|
HAL_GPIO_DIR_IN, 0);
|
|
|
|
}
|
|
|
|
if (cfg_anc_led.pin != HAL_IOMUX_PIN_NUM) {
|
|
|
|
hal_iomux_init((struct HAL_IOMUX_PIN_FUNCTION_MAP *)&cfg_anc_led, 1);
|
|
|
|
hal_gpio_pin_set_dir((enum HAL_GPIO_PIN_T)cfg_anc_led.pin, HAL_GPIO_DIR_OUT,
|
|
|
|
0);
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
#endif
|
|
|
|
#ifdef ANC_SWITCH_PIN
|
2023-02-01 14:52:54 -06:00
|
|
|
uint8_t gpio_val = hal_gpio_pin_get_val(ANC_SWITCH_PIN);
|
|
|
|
if (gpio_val) {
|
|
|
|
app_anc_switch_turnled(true);
|
|
|
|
TRACE(1, " %s turn on led", __func__);
|
|
|
|
} else {
|
|
|
|
app_anc_switch_turnled(false);
|
|
|
|
TRACE(1, " %s turn off led", __func__);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
int app_anc_open_module(void) {
|
|
|
|
TRACE(1, "%s ", __func__);
|
|
|
|
// set app module
|
|
|
|
app_set_threadhandle(APP_MODUAL_ANC, app_anc_handle_process);
|
|
|
|
app_anc_do_init();
|
2022-08-15 04:20:27 -05:00
|
|
|
#ifdef __ANC_STICK_SWITCH_USE_GPIO__
|
2023-02-01 14:52:54 -06:00
|
|
|
app_anc_switch_init(app_anc_status_post);
|
|
|
|
app_anc_switch_set_edge(app_anc_switch_get_val());
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
#ifdef __SUPPORT_ANC_SINGLE_MODE_WITHOUT_BT__
|
2023-02-01 14:52:54 -06:00
|
|
|
if (anc_single_mode_is_on()) {
|
|
|
|
// app_pwr_key_monitor_init(app_anc_send_pwr_key_monitor_evt);
|
|
|
|
// app_pwr_key_monitor_set_int_edge(app_pwr_key_monitor_get_val());
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
return 0;
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
int app_anc_close_module(void) {
|
|
|
|
TRACE(1, " %s ", __func__);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
anc_timer_request = ANC_EVENT_NONE;
|
|
|
|
if (anc_timerid) {
|
|
|
|
hwtimer_stop(anc_timerid);
|
|
|
|
hwtimer_free(anc_timerid);
|
|
|
|
anc_timerid = NULL;
|
|
|
|
}
|
|
|
|
if (app_anc_get_anc_status() != ANC_STATUS_OFF) {
|
|
|
|
anc_work_status = ANC_STATUS_OFF;
|
|
|
|
app_anc_disable();
|
|
|
|
app_anc_close_anc();
|
|
|
|
}
|
|
|
|
app_set_threadhandle(APP_MODUAL_ANC, NULL);
|
|
|
|
return 0;
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
enum AUD_SAMPRATE_T app_anc_get_play_rate(void) {
|
|
|
|
return anc_sample_rate[AUD_STREAM_PLAYBACK];
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
bool app_anc_work_status(void) {
|
|
|
|
// TRACE(2," %s st %d", __func__, anc_work_status);
|
|
|
|
return (anc_work_status == ANC_STATUS_ON ||
|
|
|
|
anc_work_status == ANC_STATUS_WAITING_ON);
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
uint32_t app_anc_get_anc_status(void) { return anc_work_status; }
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void test_anc(void) {
|
|
|
|
app_anc_open_anc();
|
|
|
|
app_anc_enable();
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
void test_anc_switch_coef(void) {
|
|
|
|
anc_coef_idx++;
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
TRACE(2, " %s set coef idx: %d ", __func__, anc_coef_idx);
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (ANC_STATUS_OFF == anc_work_status ||
|
|
|
|
ANC_STATUS_INIT_ON == anc_work_status) {
|
|
|
|
app_anc_status_change();
|
|
|
|
anc_coef_idx = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (anc_coef_idx < (ANC_COEF_LIST_NUM)) {
|
2022-08-15 04:20:27 -05:00
|
|
|
#ifdef ANC_FF_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
while (__anc_usb_app_fadeout(ANC_FEEDFORWARD))
|
|
|
|
;
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
#ifdef ANC_FB_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
while (__anc_usb_app_fadeout(ANC_FEEDBACK))
|
|
|
|
;
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
app_anc_disable();
|
|
|
|
anc_select_coef(anc_sample_rate[AUD_STREAM_PLAYBACK], anc_coef_idx,
|
|
|
|
ANC_FEEDFORWARD, ANC_GAIN_DELAY);
|
|
|
|
app_anc_enable();
|
2022-08-15 04:20:27 -05:00
|
|
|
|
|
|
|
#ifdef ANC_FF_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
while (__anc_usb_app_fadein(ANC_FEEDFORWARD))
|
|
|
|
;
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
|
|
|
#ifdef ANC_FB_ENABLED
|
2023-02-01 14:52:54 -06:00
|
|
|
while (__anc_usb_app_fadein(ANC_FEEDBACK))
|
|
|
|
;
|
2022-08-15 04:20:27 -05:00
|
|
|
#endif
|
2023-02-01 14:52:54 -06:00
|
|
|
} else {
|
|
|
|
anc_coef_idx = 0;
|
|
|
|
app_anc_timer_set(ANC_EVENT_CLOSE, anc_close_delay_time);
|
|
|
|
app_anc_gain_fadeout();
|
|
|
|
anc_work_status = ANC_STATUS_WAITING_OFF;
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(IBRT)
|
2023-02-01 14:52:54 -06:00
|
|
|
void app_anc_sync_status(void) {
|
|
|
|
uint8_t buf[3] = {0};
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
bool flag = app_anc_work_status();
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
buf[0] = (uint8_t)flag;
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
if (flag == true) {
|
|
|
|
buf[1] = (uint8_t)anc_coef_idx;
|
|
|
|
} else {
|
|
|
|
buf[1] = (uint8_t)ANC_COEF_NUM;
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
|
2023-02-01 14:52:54 -06:00
|
|
|
buf[2] = true; // set peer anc_status_sync_flag = true
|
|
|
|
|
|
|
|
anc_peer_coef_idx = 0;
|
|
|
|
|
|
|
|
TRACE(4, "%s anc_work_status:%d, anc_coef_idx:%d, anc_sync_status_flag:%d",
|
|
|
|
__func__, buf[0], buf[1], buf[2]);
|
|
|
|
ibrt_ctrl_t *p_ibrt_ctrl = app_tws_ibrt_get_bt_ctrl_ctx();
|
|
|
|
|
|
|
|
if (p_ibrt_ctrl->current_role == IBRT_MASTER) {
|
|
|
|
tws_ctrl_send_cmd(APP_TWS_CMD_SYNC_ANC_STATUS, buf, 3);
|
|
|
|
}
|
2022-08-15 04:20:27 -05:00
|
|
|
}
|
|
|
|
#endif
|