魔盒最新版本,更新了充电检测,鸟叫,dp4对声音的控制,声音模块由于硬件问题暂未实测

This commit is contained in:
2026-09-09 15:32:50 +08:00
parent 3ab37b7845
commit b576f31113
22 changed files with 871 additions and 528 deletions
+14
View File
@@ -0,0 +1,14 @@
#ifndef APP_AUDIO_DEBUG_H
#define APP_AUDIO_DEBUG_H
#include "app_config.h"
#include "system/includes.h"
/* UTF-8 中文日志;只在状态变化时打印,不在音频采样循环内连续打印。 */
#if TCFG_MAGIC_AUDIO_DEBUG_ENABLE
#define MAGIC_AUDIO_LOG(fmt, ...) printf("[魔盒声音] " fmt "\n", ##__VA_ARGS__)
#else
#define MAGIC_AUDIO_LOG(...) do { } while (0)
#endif
#endif /* APP_AUDIO_DEBUG_H */
+4 -2
View File
@@ -127,8 +127,10 @@ static uint8_t boot_skip_key_wait(void)
/* USB 在位(充电口有电):说明是插着电源,不是用户凭空按键开机 */
for (i = 0; i < 10; i++) {
if (bsp_chg_is_low() || bsp_vpwr_is_online()
|| get_ldo5v_online_hw()) {
/* VPWR/LDO5V are the authoritative external-power indicators.
* CHRG is open-drain and is only meaningful after VPWR is present;
* do not let an abnormal low PG8 skip the key wait by itself. */
if (get_ldo5v_online_hw()) {
log_info("boot usb, skip key wait\n");
return 1;
}
+21 -19
View File
@@ -19,6 +19,9 @@
#include "app_mode.h"
#include "app_led.h"
#include "app_shake_wake.h"
#include "app_no_disturb.h"
#include "car_audio_test.h"
#include "app_audio_debug.h"
#include "app_time_util.h"
#include "bsp_hw.h"
#include "board_pin.h"
@@ -46,7 +49,6 @@ typedef struct {
volatile uint8_t play_write_pending;
volatile uint8_t play_write_mode;
uint8_t shake_wake;
uint8_t no_disturb;
} AppFunctionCtx_t;
static AppFunctionCtx_t s_func;
@@ -71,7 +73,6 @@ void app_function_init(void)
app_shake_wake_init(function_shake_wake_allowed,
function_shake_wake_trigger);
s_func.shake_wake = !!gDevData.shake_wake;
s_func.no_disturb = !!gDevData.no_disturb_enable[0];
app_shake_wake_set_enable(s_func.shake_wake);
}
@@ -105,12 +106,6 @@ void app_function_set_shake_wake(uint8_t enable)
app_shake_wake_set_enable(s_func.shake_wake);
}
void app_function_set_no_disturb(uint8_t enable)
{
s_func.no_disturb = !!enable;
app_shake_wake_reset();
}
/**
* @brief 跟随 APP 下发的 DP0/DP1,并处理 DP2 手动点动
* @return 无
@@ -120,10 +115,6 @@ void app_function_poll(void)
if (s_func.shake_wake != !!gDevData.shake_wake) {
app_function_set_shake_wake(gDevData.shake_wake);
}
if (s_func.no_disturb != !!gDevData.no_disturb_enable[0]) {
app_function_set_no_disturb(gDevData.no_disturb_enable[0]);
}
if (s_func.power_write_pending) {
uint8_t enable = s_func.power_write_value;
s_func.power_write_pending = 0;
@@ -265,6 +256,10 @@ static void function_play_mode_apply(uint8_t mode)
{
uint8_t old_mode = s_func.play_mode;
MAGIC_AUDIO_LOG("模式切换请求:旧=%u,请求=%u,DP4=%u,功能开关=%u,充电=%u,关机中=%uOTA=%u",
old_mode, mode, gDevData.beep_trig, s_func.app_power,
app_power_usb_online(), app_power_poweroff_pending(), app_led_is_ota());
if (mode > PLAY_MODE_5) {
mode = 0;
}
@@ -284,10 +279,17 @@ static void function_play_mode_apply(uint8_t mode)
} else {
app_mode_start(mode);
}
// MAGIC_AUDIO_LOG("模式切换结果:旧=%u,实际=%u;当前测试不拦截DP4", old_mode, mode);
if (mode != old_mode) {
app_led_request_mode_blink(); /* 模式真变了才闪两下 */
app_led_request_mode_blink(); /* 模式真变了才闪两下 */
/* DP4:0=关闭模式切换鸟叫,1=允许播放 */
//&& gDevData.beep_trig
if ((mode != PLAY_MODE_0) && gDevData.beep_trig) {
Car_Audio_Test_Play();
}
}
}
/**
* @brief 跟随 APP 下发的 DP1 自动模式
@@ -384,7 +386,7 @@ static void function_hand_poll(void)
s_func.play_mode = 0;
s_func.play_ms = 0;
gDevData.play_mode = 0;
app_mode_stop(); /* 自动切手动:先停自动玩法 */
app_mode_stop();
s_func.hand_busy = 1;
s_func.hand_ms = 0;
gDevData.hand_mode = cmd;
@@ -395,10 +397,10 @@ static void function_hand_poll(void)
bsp_drive_set(BSP_MOTOR_STOP, 0,
BSP_MOTOR_FORWARD, 5500);
}
app_led_request_mode_blink(); /* 自动/空闲切到手动,闪两下提示 */
log_info("hand cmd=%d %s max=%ums\n", cmd,
(cmd == HAND_MODE_1) ? "upper" : "lower",
(unsigned)BOARD_HAND_PULSE_MS);
app_led_request_mode_blink();
if (gDevData.beep_trig) {
Car_Audio_Test_Play();
}
} else {
gDevData.hand_mode = HAND_MODE_0;
if (s_func.hand_busy) {
@@ -501,7 +503,7 @@ static uint8_t function_power_on(uint8_t show_hint)
static uint8_t function_shake_wake_allowed(void)
{
return s_func.shake_wake &&
!s_func.no_disturb &&
!app_no_disturb_is_active() &&
!s_func.app_power &&
!app_power_usb_online() &&
!app_power_poweroff_pending() &&
-3
View File
@@ -39,9 +39,6 @@ void app_function_dp_hand_write(uint8_t cmd);
/** CBC4 DP6 震动开机开关。 */
void app_function_set_shake_wake(uint8_t enable);
/** 勿扰时段是否正在生效;生效时屏蔽震动开机。 */
void app_function_set_no_disturb(uint8_t enable);
/**
* @brief 跟随 APP 下发的 DP0/DP1,并处理 DP2 手动点动,每个业务 tick 调用一次
* @return 无
+145
View File
@@ -0,0 +1,145 @@
#include "app_no_disturb.h"
#include "system/includes.h"
#include "usr_rtc.h"
#define APP_NO_DISTURB_VM_ID 28U
#define APP_NO_DISTURB_MAGIC 0x4e44U
#define APP_NO_DISTURB_VERSION 1U
#define APP_MINUTES_PER_DAY (24U * 60U)
typedef struct __attribute__((packed)) {
uint16_t magic;
uint8_t version;
uint8_t cfg[APP_NO_DISTURB_CFG_LEN];
} AppNoDisturbVm_t;
typedef struct {
uint8_t enabled;
uint8_t weekdays_mask;
uint16_t start_minute;
uint16_t end_minute;
} AppNoDisturbCtx_t;
static AppNoDisturbCtx_t s_no_disturb;
static uint8_t app_no_disturb_cfg_valid(const uint8_t cfg[APP_NO_DISTURB_CFG_LEN])
{
uint8_t i;
if (!cfg || cfg[0] > 1U || cfg[9] >= 24U || cfg[11] >= 24U ||
cfg[10] >= 60U || cfg[12] >= 60U) {
return 0;
}
for (i = 0; i < 7U; i++) {
if (cfg[2U + i] > 1U) {
return 0;
}
}
return 1;
}
void app_no_disturb_configure(uint8_t enable, uint8_t weekdays_mask,
uint16_t start_minute, uint16_t end_minute)
{
s_no_disturb.enabled = !!enable;
s_no_disturb.weekdays_mask = weekdays_mask & 0x7fU;
s_no_disturb.start_minute = start_minute;
s_no_disturb.end_minute = end_minute;
}
uint8_t app_no_disturb_storage_load(uint8_t cfg[APP_NO_DISTURB_CFG_LEN])
{
AppNoDisturbVm_t vm;
int ret;
if (!cfg) {
return 0;
}
memset(&vm, 0, sizeof(vm));
ret = syscfg_read(APP_NO_DISTURB_VM_ID, &vm, sizeof(vm));
if (ret != (int)sizeof(vm) || vm.magic != APP_NO_DISTURB_MAGIC ||
vm.version != APP_NO_DISTURB_VERSION || !app_no_disturb_cfg_valid(vm.cfg)) {
memset(cfg, 0, APP_NO_DISTURB_CFG_LEN);
return 0;
}
memcpy(cfg, vm.cfg, APP_NO_DISTURB_CFG_LEN);
return 1;
}
uint8_t app_no_disturb_storage_save(const uint8_t cfg[APP_NO_DISTURB_CFG_LEN])
{
AppNoDisturbVm_t vm;
if (!app_no_disturb_cfg_valid(cfg)) {
return 0;
}
memset(&vm, 0, sizeof(vm));
vm.magic = APP_NO_DISTURB_MAGIC;
vm.version = APP_NO_DISTURB_VERSION;
memcpy(vm.cfg, cfg, APP_NO_DISTURB_CFG_LEN);
return (uint8_t)(syscfg_write(APP_NO_DISTURB_VM_ID, &vm, sizeof(vm)) ==
(int)sizeof(vm));
}
static uint8_t app_no_disturb_weekday_monday0(const struct sys_time *time)
{
static const uint8_t month_offset[12] = {
0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4
};
uint32_t year = time->year;
uint8_t sunday0;
if (time->month < 3U) {
year--;
}
sunday0 = (uint8_t)((year + year / 4U - year / 100U + year / 400U +
month_offset[time->month - 1U] + time->day) % 7U);
return (uint8_t)((sunday0 + 6U) % 7U);
}
static uint8_t app_no_disturb_day_selected(uint8_t weekday)
{
return (uint8_t)((s_no_disturb.weekdays_mask & (1U << weekday)) != 0U);
}
uint8_t app_no_disturb_is_active_at(const struct sys_time *time)
{
uint8_t weekday;
uint16_t now_minute;
if (!time || !s_no_disturb.enabled || !s_no_disturb.weekdays_mask ||
s_no_disturb.start_minute >= APP_MINUTES_PER_DAY ||
s_no_disturb.end_minute >= APP_MINUTES_PER_DAY ||
time->month < 1U || time->month > 12U || time->day < 1U ||
time->day > 31U || time->hour > 23U || time->min > 59U) {
return 0;
}
weekday = app_no_disturb_weekday_monday0(time);
now_minute = (uint16_t)(time->hour * 60U + time->min);
if (s_no_disturb.start_minute == s_no_disturb.end_minute) {
return app_no_disturb_day_selected(weekday);
}
if (s_no_disturb.start_minute < s_no_disturb.end_minute) {
return app_no_disturb_day_selected(weekday) &&
now_minute >= s_no_disturb.start_minute &&
now_minute < s_no_disturb.end_minute;
}
if (now_minute >= s_no_disturb.start_minute) {
return app_no_disturb_day_selected(weekday);
}
if (now_minute < s_no_disturb.end_minute) {
return app_no_disturb_day_selected((uint8_t)((weekday + 6U) % 7U));
}
return 0;
}
uint8_t app_no_disturb_is_active(void)
{
struct sys_time time;
if (!usr_rtc_get_time(&time)) {
return 0;
}
return app_no_disturb_is_active_at(&time);
}
+17
View File
@@ -0,0 +1,17 @@
#ifndef __APP_NO_DISTURB_H__
#define __APP_NO_DISTURB_H__
#include <stdint.h>
#include "system/sys_time.h"
#define APP_NO_DISTURB_CFG_LEN 13
/* DP7: bytes 2..8 are Monday..Sunday, byte 1 is reserved. */
void app_no_disturb_configure(uint8_t enable, uint8_t weekdays_mask,
uint16_t start_minute, uint16_t end_minute);
uint8_t app_no_disturb_storage_load(uint8_t cfg[APP_NO_DISTURB_CFG_LEN]);
uint8_t app_no_disturb_storage_save(const uint8_t cfg[APP_NO_DISTURB_CFG_LEN]);
uint8_t app_no_disturb_is_active(void);
uint8_t app_no_disturb_is_active_at(const struct sys_time *time);
#endif /* __APP_NO_DISTURB_H__ */
+71 -30
View File
@@ -36,14 +36,21 @@
#define BAT_BOOT_MS 3000 /* 上电 3s 内不因低电深睡 */
#define BAT_LOG_MS 10000
#define BAT_PERIODIC_LOG_ENABLE 0 /* Production standby must not wake UART periodically. */
#define CHG_ON_CONFIRM_MS 50u
#define CHG_OFF_CONFIRM_MS 150u
#define CHG_FULL_RECHECK_MS 1000u
#define BAT_LOW_WIN 3 /* 连续约 2.4s 才进低电告警(仅闪灯,不关机) */
/** 充电检测:CHG/VPWR 消抖后的结果 */
/** TP4056 CHRG/STDBY debounced charging state. */
typedef struct {
uint8_t usb_online;
uint8_t charge_led; /**< 0=无 1=充电红 2=充满绿 */
uint8_t charge_led_last; /**< 仅用于变化时打日志 */
uint16_t full_ms; /**< 拔电瞬间 CHG 先变高,需等满 1s 才当充满 */
uint8_t charge_led;
uint8_t charge_led_last;
uint16_t charging_ms;
uint16_t unplugged_ms;
uint16_t full_ms;
uint16_t full_recheck_ms;
uint8_t full_latched;
} AppChargeState_t;
/** 电量采样:滑动平均 + 百分比 */
@@ -221,7 +228,7 @@ static void power_do_poweroff(void *priv)
}
/**
* @brief 充电检测:CHG 低=充电中;VPWR 在且 CHG 高满 1s=充满;拔电报 USB_OUT
* @brief TP4056 CHRG/STDBY 组合检测,稳定状态变化后报告 USB 事件
* @param dt 距上次调用的毫秒数
* @return USB 插拔边沿事件
*/
@@ -229,37 +236,71 @@ static AppPowerEvt_t power_charge_tick(uint16_t dt)
{
AppChargeState_t *c = &s_power.charge;
uint8_t chg_low = bsp_chg_is_low();
uint8_t vpwr = bsp_vpwr_is_online();
uint8_t usb;
uint8_t stby_low = bsp_stby_is_low();
uint8_t usb = c->usb_online;
AppPowerEvt_t evt = APP_POWER_EVT_NONE;
if (chg_low) {
/* CHG 拉低:正在充电,红灯 */
if (chg_low && stby_low) {
/* 00 is invalid: keep the previous stable state. */
c->charging_ms = 0;
c->unplugged_ms = 0;
c->full_ms = 0;
c->charge_led = 1;
gDevData.charge = CHARGE_1;
usb = 1;
} else if (vpwr) {
/* VPWR 在但 CHG 已高:可能是充满,也可能是拔电瞬间 CHG 先变高。
* 等满 BOARD_CHG_FULL_MS 才当真充满,避免拔电误报绿灯 */
c->full_ms = app_add_ms(c->full_ms, dt);
if (c->full_ms >= BOARD_CHG_FULL_MS) {
c->charge_led = 2;
c->full_recheck_ms = 0;
} else if (!chg_low && !stby_low) {
/* 11: charger absent. */
c->charging_ms = 0;
c->full_ms = 0;
c->full_recheck_ms = 0;
c->unplugged_ms = app_add_ms(c->unplugged_ms, dt);
if (c->unplugged_ms >= CHG_OFF_CONFIRM_MS) {
c->charge_led = 0;
c->full_latched = 0;
usb = 0;
}
gDevData.charge = (c->charge_led == 1) ? CHARGE_1 : CHARGE_2;
usb = 1;
} else {
/* USB 不在位:充电灯灭 */
} else if (chg_low) {
/* 01: charging. */
c->unplugged_ms = 0;
c->full_ms = 0;
c->charge_led = 0;
gDevData.charge = CHARGE_2;
usb = 0;
if (c->full_latched) {
c->charging_ms = 0;
c->full_recheck_ms = app_add_ms(c->full_recheck_ms, dt);
if (c->full_recheck_ms >= CHG_FULL_RECHECK_MS) {
c->full_latched = 0;
c->charge_led = 1;
}
usb = 1;
} else {
c->full_recheck_ms = 0;
c->charging_ms = app_add_ms(c->charging_ms, dt);
if (c->charging_ms >= CHG_ON_CONFIRM_MS) {
c->charge_led = 1;
usb = 1;
}
}
} else {
/* 10: charge complete. */
c->charging_ms = 0;
c->unplugged_ms = 0;
c->full_recheck_ms = 0;
if (c->full_latched) {
c->charge_led = 2;
usb = 1;
} else {
c->full_ms = app_add_ms(c->full_ms, dt);
if (c->full_ms >= BOARD_CHG_FULL_MS) {
c->charge_led = 2;
c->full_latched = 1;
usb = 1;
}
}
}
gDevData.charge = (c->charge_led == 1) ? CHARGE_1 : CHARGE_2;
if (usb != c->usb_online) {
c->usb_online = usb;
log_info("usb %s vpwr=%d chg=%d mv=%d pair=%d goto=%d\n",
usb ? "in" : "out", vpwr, chg_low, bsp_vpwr_mv(),
log_info("charge input %s chg_low=%d stby_low=%d pair=%d goto=%d\n",
usb ? "in" : "out", chg_low, stby_low,
usr_get_pair_flag(), usr_var.usr_goto_pair);
usr_var.usr_power_charge_flag = usb ? 1 : 2;
if (usb) {
@@ -272,10 +313,10 @@ static AppPowerEvt_t power_charge_tick(uint16_t dt)
if (c->charge_led != c->charge_led_last) {
c->charge_led_last = c->charge_led;
log_info("charge_led=%d chg=%d vpwr=%d mv=%d\n",
c->charge_led, chg_low, vpwr, bsp_vpwr_mv());
log_info("charge_led=%d chg_low=%d stby_low=%d\n",
c->charge_led, chg_low, stby_low);
if (c->charge_led == 2) {
/* 硬件(CHG+VPWR 满 BOARD_CHG_FULL_MS判定充满,电量强制收尾到
/* STDBY 持续为低满 BOARD_CHG_FULL_MS判定充满,电量强制收尾到
* 100%,避免电压拟合曲线因基准/温漂等原因走不到 100% 就卡住 */
app_battery_set_percent(100);
s_power.sample.pct = 100;
+51
View File
@@ -0,0 +1,51 @@
/******************************************************************************
* @file car_audio_test.c
* @brief Car audio hardware test using the SDK key-tone sine path.
*
* Notes:
* - The SDK sine sample rate is 16 kHz; points is the PCM sample count.
* - freq is specified as actual frequency * 512.
* - decay=0 keeps amplitude constant for observing DAC/PA end behavior.
* - The parameter array has static storage because playback is asynchronous.
******************************************************************************/
#include <stdint.h>
#include "system/includes.h"
#include "tone_player.h"
#include "application/audio_dec_app.h"
#include "car_audio_test.h"
#include "app_audio_debug.h"
static uint16_t s_car_audio_restart_guard_ms;
int Car_Audio_Test_Play(void)
{
/* bird.mp3 is packed as a standalone file in the internal resource area. */
int ret;
MAGIC_AUDIO_LOG("鸟叫接口进入:资源=%s,当前播放状态=%d",
SDFILE_RES_ROOT_PATH"bird.mp3", audio_key_tone_is_play());
ret = audio_key_tone_play_name(SDFILE_RES_ROOT_PATH"bird.mp3", 1);
MAGIC_AUDIO_LOG("鸟叫请求返回=%d(0仅表示请求已提交,不代表已经出声)", ret);
return ret;
}
void Car_Audio_Test_Loop(uint16_t dt_ms)
{
if (s_car_audio_restart_guard_ms > dt_ms) {
s_car_audio_restart_guard_ms -= dt_ms;
return;
}
if (audio_key_tone_is_play()) {
return;
}
if (Car_Audio_Test_Play() == 0) {
/*
* 防止播放请求刚入队、SDK 尚未更新播放状态时被重复提交。
*/
s_car_audio_restart_guard_ms = 50;
}
}
+26
View File
@@ -0,0 +1,26 @@
/******************************************************************************
* @file car_audio_test.h
* @brief Car audio hardware test API using the SDK key-tone sine path.
******************************************************************************/
#include <stdint.h>
#ifndef __CAR_AUDIO_TEST_H__
#define __CAR_AUDIO_TEST_H__
/**
* @brief Play the bird-call MP3 stored in the internal resource area.
*
* The API only queues a request. Playback is asynchronous in the SDK key-tone
* task; it does not wait for completion or block the caller. This test is not
* triggered automatically; call it explicitly from test code.
*
* @return 0=request queued; negative value=module unavailable/request failed.
*/
int Car_Audio_Test_Play(void);
/**
* @brief 开机状态下维护循环测试音。
* @param dt_ms 距离上次调用的毫秒数
*/
void Car_Audio_Test_Loop(uint16_t dt_ms);
#endif /* __CAR_AUDIO_TEST_H__ */