优化程序架构

This commit is contained in:
2026-08-25 14:59:39 +08:00
parent d5ec6ecde0
commit 5252471aca
25 changed files with 2964 additions and 1608 deletions
+148
View File
@@ -0,0 +1,148 @@
/******************************************************************************
* @file app_boot.c
* @brief 深睡唤醒 / 上电按键判定实现
* @author cyWu <1917507415@qq.com>
* @date 2026.08.25
* @version V1.0.0
* @history
* - V1.0.0, 2026.08.25, cyWu, 从 usr_jb_main 拆出
******************************************************************************/
#include "app_boot.h"
#include "app_pair.h"
#include "bsp_hw.h"
#include "board_pin.h"
#include "app_main.h"
#include "usr_le_api.h"
#include "asm/power/power_api.h"
#include "asm/power/power_reset.h"
#define LOG_TAG_CONST APP
#define LOG_TAG "[APP_BOOT]"
#define LOG_INFO_ENABLE
#include "debug.h"
extern void clr_wdt(void);
extern u32 timer_get_ms(void);
/**
* @brief 忙等若干毫秒,并喂狗
* @param ms 等待时长
* @return 无
*/
static void boot_delay_ms(uint32_t ms)
{
uint32_t t0 = timer_get_ms();
while ((timer_get_ms() - t0) < ms) {
clr_wdt();
}
}
/**
* @brief BLE 起来前进深睡(1ms 定时器尚未启动),原地喂狗不返回
* @note power_set_soft_poweroff() 只是向 SDK 底层发起软关机请求(置个标志位),
* 不是同步断电——仓库里其它调用点(如 app_power_manage.c)调用完都是
* 正常往下走/return,真正切电源是底层电源管理在之后某个时机才执行的。
* 此时 BLE 栈、1ms 业务定时器都还没起来,如果这里直接 return,代码会
* 接着把 usr_jb_init 剩下的初始化和开机流程跑完,等于白判定了。所以
* 必须用 while(1) 卡住不返回:一边喂狗防止看门狗把这个等待打断,
* 一边等底层真正把电断掉(或者外部条件变化触发复位)。
* @return 无(不会返回)
*/
static void boot_sleep(void)
{
log_info("boot sleep\n");
bsp_hw_enter_sleep_io();
power_set_soft_poweroff();
while (1) {
clr_wdt();
}
}
/**
* @brief 上电是否跳过 3s/5s 按键判定
* @note 只有“真的断电后重新插电池 / 用户主动按键”才需要走 3s/5s 判定;
* USB 在位、或者芯片自己意外重启的情况,都应该直接保持开机,
* 否则用户会遇到“设备莫名关机、还要求重新按 3 秒才能开机”的怪现象。
* @return 1=USB / 软复位 / LVD / 看门狗,保持开机且不进配对;0=需要走按键判定
*/
static uint8_t boot_skip_key_wait(void)
{
uint8_t i;
/* USB 在位(充电口有电):说明是插着电源,不是用户凭空按键开机 */
for (i = 0; i < 10; i++) {
if (bsp_chg_is_low() || bsp_vpwr_is_online()) {
log_info("boot usb, skip key wait\n");
return 1;
}
boot_delay_ms(5);
}
/* 以下都是“芯片自己意外重启了一下”,不是用户主动按开机键,
* 不能因此逼用户重新按键,否则相当于设备无故“关机”了一次 */
if (cpu_reset_by_soft() /* SDK 综合判断:是否软件复位 */
|| is_reset_source(MSYS_SOFT_RST) /* 主系统软件复位(断言/异常/OTA 后重启等) */
|| is_reset_source(P33_SOFT_RST) /* P33 软件复位,最常见的软复位入口 */
|| is_reset_source(P33_VDDIO_LVD_RST) /* VDDIO 低压复位(电机启动拉低电压误触发) */
|| is_reset_source(P11_WDT_RST)) { /* 看门狗复位(程序卡死被强制重启) */
log_info("boot reset-keep, skip key wait\n");
return 1;
}
return 0;
}
/**
* @brief 等待并判定开机按键
* @return 无(深睡分支不返回)
*/
void app_boot_wait_key(void)
{
uint32_t t0;
uint32_t elapsed;
uint8_t green_on = 0;
usr_var.usr_goto_pair = 0;
if (boot_skip_key_wait()) {
return;
}
if (!bsp_key_is_pressed()) {
log_info("boot no key, sleep\n");
boot_sleep();
}
t0 = timer_get_ms();
bsp_led_all_off();
log_info("boot key hold, wait 3s on / 5s pair\n");
while (bsp_key_is_pressed()) {
elapsed = timer_get_ms() - t0;
clr_wdt();
if (elapsed >= BOARD_KEY_PAIR_MS) {
usr_var.usr_goto_pair = 1;
usr_clear_pair_info_noreset();
app_pair_start();
log_info("boot hold %ums, power on + pair\n", (unsigned)elapsed);
return;
}
if (!green_on && (elapsed >= BOARD_KEY_POWER_MS)) {
green_on = 1;
bsp_led_green_set(1);
log_info("boot hold 3s, will power on\n");
}
}
elapsed = timer_get_ms() - t0;
if (elapsed < BOARD_KEY_POWER_MS) {
log_info("boot hold %ums < 3s, sleep\n", (unsigned)elapsed);
boot_sleep();
}
log_info("boot hold %ums, power on\n", (unsigned)elapsed);
}
+25
View File
@@ -0,0 +1,25 @@
/******************************************************************************
* @file app_boot.h
* @brief 深睡唤醒 / 上电按键判定:3s 开机,5s 开机并配对,不足 3s 松手再睡
* @author cyWu <1917507415@qq.com>
* @date 2026.08.25
* @version V1.0.0
* @history
* - V1.0.0, 2026.08.25, cyWu, 从 usr_jb_main 拆出
*
* @note 必须在 BLE 协议栈起来、1ms 定时器启动之前调用。杰理 jiffies 此时
* 往往 10ms 才跳一次,所以内部一律用 timer_get_ms 直接量时间,
* 不能借用按键库的 1ms 状态机(否则长按时长会被严重低估)。
******************************************************************************/
#ifndef __APP_BOOT_H__
#define __APP_BOOT_H__
/**
* @brief 等待并判定开机按键:不足 3s 松手、或全程未按键,直接原地深睡(不返回);
* 满 3s 视为正常开机;满 5s 视为开机并进入配对(内部会调用 app_pair_start
* @return 无(深睡分支通过 while(1) 喂狗不返回;其余情况正常返回)
*/
void app_boot_wait_key(void);
#endif /* __APP_BOOT_H__ */
-621
View File
@@ -1,621 +0,0 @@
/******************************************************************************
* @file app_cbc2.c
* @brief CBC2 电源 / 充电 / 按键 / 配对 / DP 协调
* @author cyWu <1917507415@qq.com>
* @date 2026.08.24
* @version V1.0.2
* @history
* - V1.0.0, 2026.08.21, cyWu, 首次发布
* - V1.0.2, 2026.08.24, cyWu, 上电即绑定广播,与 81bd2f7 行为对齐
******************************************************************************/
#include "system/includes.h"
#include "app_main.h"
#include "app_cbc2.h"
#include "app_nv.h"
#include "app_led.h"
#include "app_rope.h"
#include "app_mode.h"
#include "app_dnd.h"
#include "bsp_hw.h"
#include "usr_le_api.h"
#include "asm/power/power_api.h"
#define LOG_TAG_CONST APP
#define LOG_TAG "[APP_CBC2]"
#define LOG_ERROR_ENABLE
#define LOG_DEBUG_ENABLE
#define LOG_INFO_ENABLE
#include "debug.h"
extern void sys_enter_soft_poweroff(void *priv);
/*============================================================================*/
/* 类型 */
/*============================================================================*/
typedef enum {
APP_LIFE_CHARGE_ONLY = 0,
APP_LIFE_ON,
APP_LIFE_PAIR
} AppLife_t;
typedef struct {
uint8_t inited;
AppLife_t life;
uint8_t power; /* DP0 APP 功能开关 */
uint8_t play_mode;
uint8_t battery;
uint8_t charge_dp; /* 1 充电中 2 未充电 */
uint8_t alm_low;
uint8_t charge_led; /* 0 无 1 充 2 满 */
uint8_t usb_raw;
uint8_t usb_debounced;
uint8_t usb_cnt;
uint8_t restore_on; /* 插电前是否开机 */
uint8_t full_latched;
uint8_t from_deep_boot;
uint8_t boot_key_lock; /* 开机按键未松开前,忽略短按/关机 */
uint8_t key_raw;
uint8_t key_stable;
uint8_t key_cnt;
uint8_t key_pressed;
uint16_t key_hold_ms;
uint8_t req_click;
uint8_t req_poweroff;
uint8_t req_pair;
uint32_t pair_ms;
uint32_t lowbat_ms;
uint8_t lowbat_run;
uint16_t vbat_mv;
uint8_t pause_mode;
uint8_t pause_cal;
uint8_t pause_percent;
uint8_t pause_percent_valid;
uint8_t run_timer;
} AppCbc2_t;
/*============================================================================*/
/* 私有变量 */
/*============================================================================*/
static AppCbc2_t s_app;
/*============================================================================*/
/* 私有函数 */
/*============================================================================*/
static uint8_t app_cbc2_ctrl_allowed(void)
{
return (uint8_t)(s_app.life != APP_LIFE_CHARGE_ONLY);
}
static void app_cbc2_apply_stop_reset(void)
{
app_mode_stop();
app_rope_stop();
s_app.play_mode = 0;
}
static void app_cbc2_apply_play_mode(uint8_t mode, uint8_t blink)
{
if (mode > 6) {
mode = 0;
}
app_rope_stop();
if (mode == 0) {
app_mode_stop();
} else {
app_mode_start(mode);
}
s_app.play_mode = mode;
if (blink) {
app_led_request_mode_blink();
}
log_info("play_mode=%d\n", mode);
}
static void app_cbc2_do_poweroff(void)
{
log_info("enter deep off\n");
app_mode_stop();
bsp_hw_enter_sleep_io();
app_nv_save_if_dirty();
sys_enter_soft_poweroff(NULL);
}
static void app_cbc2_boot_poweroff(void)
{
log_info("boot deep off\n");
bsp_hw_enter_sleep_io();
app_nv_save_if_dirty();
power_set_soft_poweroff();
}
static void app_cbc2_enter_pair(void)
{
log_info("enter pair mode\n");
app_cbc2_apply_stop_reset();
s_app.life = APP_LIFE_PAIR;
s_app.power = 1;
s_app.pair_ms = 0;
s_app.from_deep_boot = 0;
usr_var.usr_goto_pair = 1;
usr_goto_pair_mode();
}
static void app_cbc2_pair_end(uint8_t ok)
{
log_info("pair %s\n", ok ? "ok" : "fail");
app_led_start_pair_result(ok);
s_app.life = APP_LIFE_ON;
s_app.power = 1;
s_app.play_mode = 0;
usr_var.usr_goto_pair = 0;
}
static void app_cbc2_pause_functions(void)
{
s_app.pause_mode = s_app.play_mode;
s_app.pause_cal = app_rope_is_calibrating();
s_app.pause_percent_valid = 0;
if (app_rope_is_busy() && !s_app.pause_cal && app_rope_is_calibrated()
&& app_rope_get_turns()) {
int32_t max_x = (int32_t)app_rope_get_turns() * 100;
if (max_x > 0) {
int32_t pct = app_rope_remaining_x100() * 100 / max_x;
if (pct < 0) {
pct = 0;
}
if (pct > 100) {
pct = 100;
}
s_app.pause_percent = (uint8_t)pct;
s_app.pause_percent_valid = 1;
}
}
app_mode_stop();
app_rope_stop();
log_info("app power pause mode=%d cal=%d\n", s_app.pause_mode, s_app.pause_cal);
}
static void app_cbc2_resume_functions(void)
{
log_info("app power resume mode=%d cal=%d\n", s_app.pause_mode, s_app.pause_cal);
if (s_app.pause_cal) {
app_rope_cal_start();
} else if (s_app.pause_percent_valid) {
app_rope_goto_percent(s_app.pause_percent);
} else if (s_app.pause_mode >= 1 && s_app.pause_mode <= 6) {
app_cbc2_apply_play_mode(s_app.pause_mode, 0);
}
s_app.pause_cal = 0;
s_app.pause_percent_valid = 0;
}
static uint8_t app_cbc2_bat_percent(uint16_t mv)
{
int32_t span = BOARD_VBAT_FULL_MV - BOARD_VBAT_EMPTY_MV;
int32_t pct;
if (mv <= BOARD_VBAT_EMPTY_MV) {
return 0;
}
if (mv >= BOARD_VBAT_FULL_MV) {
return 100;
}
pct = ((int32_t)mv - BOARD_VBAT_EMPTY_MV) * 100 / span;
if (pct < 0) {
pct = 0;
}
if (pct > 100) {
pct = 100;
}
return (uint8_t)pct;
}
static void app_cbc2_sample_charge(void)
{
uint8_t usb = bsp_vpwr_is_online();
if (usb == s_app.usb_raw) {
if (s_app.usb_cnt < 5) {
s_app.usb_cnt++;
}
} else {
s_app.usb_raw = usb;
s_app.usb_cnt = 0;
}
if (s_app.usb_cnt >= 5 && s_app.usb_debounced != s_app.usb_raw) {
s_app.usb_debounced = s_app.usb_raw;
if (s_app.usb_debounced) {
s_app.restore_on = (uint8_t)(s_app.life != APP_LIFE_CHARGE_ONLY);
s_app.full_latched = 0;
app_cbc2_apply_stop_reset();
s_app.life = APP_LIFE_CHARGE_ONLY;
log_info("usb in, charge-only (restore_on=%d)\n", s_app.restore_on);
} else {
log_info("usb out full_latched=%d restore_on=%d\n",
s_app.full_latched, s_app.restore_on);
if (s_app.full_latched || !s_app.restore_on) {
s_app.req_poweroff = 1;
} else {
s_app.life = APP_LIFE_ON;
app_cbc2_apply_stop_reset();
}
}
}
if (s_app.usb_debounced) {
if (bsp_chg_is_low()) {
s_app.charge_led = 1;
s_app.charge_dp = 1;
s_app.full_latched = 0;
} else {
s_app.charge_led = 2;
s_app.charge_dp = 2; /* 充满:DP 报未充电 */
s_app.full_latched = 1;
}
usr_var.usr_power_charge_flag = 1;
} else {
s_app.charge_led = 0;
s_app.charge_dp = 2;
usr_var.usr_power_charge_flag = 2;
}
}
static void app_cbc2_sample_battery(void)
{
uint16_t mv = bsp_vbat_mv();
uint8_t alarm_on;
uint8_t alarm_off;
s_app.vbat_mv = mv;
s_app.battery = app_cbc2_bat_percent(mv);
alarm_on = BOARD_VBAT_LOW_MV;
alarm_off = BOARD_VBAT_LOW_MV + BOARD_VBAT_LOW_HYST_MV;
if (mv <= BOARD_VBAT_EMPTY_MV && !s_app.usb_debounced) {
if (s_app.life != APP_LIFE_CHARGE_ONLY) {
log_info("vbat %d mV lock\n", mv);
s_app.req_poweroff = 1;
}
s_app.alm_low = 1;
return;
}
if (!s_app.alm_low && mv < alarm_on) {
s_app.alm_low = 1;
s_app.lowbat_ms = 0;
s_app.lowbat_run = 1;
log_info("low battery warn %d mV\n", mv);
} else if (s_app.alm_low && mv > alarm_off) {
s_app.alm_low = 0;
s_app.lowbat_run = 0;
s_app.lowbat_ms = 0;
}
if (s_app.usb_debounced || s_app.life == APP_LIFE_CHARGE_ONLY) {
return;
}
if (s_app.lowbat_run && s_app.alm_low) {
s_app.lowbat_ms += 50;
if (s_app.lowbat_ms >= BOARD_LOWBAT_SLEEP_MS) {
log_info("low battery 1min, deep off\n");
s_app.req_poweroff = 1;
}
}
}
static void app_cbc2_handle_click(void)
{
uint8_t next;
if (s_app.life != APP_LIFE_ON) {
return;
}
if (s_app.power == 0) {
return;
}
if (app_rope_is_calibrating()) {
return;
}
next = (uint8_t)((s_app.play_mode + 1) % 7);
app_cbc2_apply_play_mode(next, 1);
}
static void app_cbc2_key_hold_check(void)
{
if (!s_app.key_pressed || s_app.boot_key_lock) {
return;
}
s_app.key_hold_ms++;
if (s_app.life == APP_LIFE_CHARGE_ONLY || s_app.life == APP_LIFE_PAIR) {
return;
}
if (s_app.life == APP_LIFE_ON && s_app.key_hold_ms == BOARD_KEY_POWER_MS) {
s_app.req_poweroff = 1;
}
}
static void app_cbc2_key_tick_1ms(void)
{
uint8_t raw = bsp_key_is_pressed();
if (raw == s_app.key_raw) {
if (s_app.key_cnt < BOARD_KEY_DEBOUNCE_MS) {
s_app.key_cnt++;
}
} else {
s_app.key_raw = raw;
s_app.key_cnt = 0;
}
if (s_app.key_cnt != BOARD_KEY_DEBOUNCE_MS) {
app_cbc2_key_hold_check();
return;
}
if (raw && !s_app.key_pressed) {
s_app.key_pressed = 1;
s_app.key_hold_ms = 0;
} else if (!raw && s_app.key_pressed) {
if (s_app.boot_key_lock) {
s_app.boot_key_lock = 0;
s_app.key_pressed = 0;
s_app.key_hold_ms = 0;
log_info("boot key released\n");
return;
}
if (s_app.life == APP_LIFE_ON &&
s_app.power &&
s_app.key_hold_ms < BOARD_KEY_CLICK_MS &&
s_app.key_hold_ms >= 10) {
s_app.req_click = 1;
}
s_app.key_pressed = 0;
s_app.key_hold_ms = 0;
}
app_cbc2_key_hold_check();
}
static void app_cbc2_boot_decide(void)
{
s_app.usb_raw = bsp_vpwr_is_online();
s_app.usb_debounced = s_app.usb_raw;
s_app.usb_cnt = 5;
s_app.vbat_mv = bsp_vbat_mv();
if (s_app.usb_debounced) {
s_app.life = APP_LIFE_CHARGE_ONLY;
s_app.restore_on = 0;
s_app.power = 0;
log_info("boot CHARGE_ONLY vbat=%d\n", s_app.vbat_mv);
return;
}
if (s_app.vbat_mv <= BOARD_VBAT_EMPTY_MV) {
log_info("boot vbat %d too low\n", s_app.vbat_mv);
app_cbc2_boot_poweroff();
return;
}
/* 与 81bd2f7 一致:上电即绑定广播,不等按键、不 deep off */
s_app.power = 1;
s_app.play_mode = 0;
s_app.from_deep_boot = 0;
s_app.boot_key_lock = bsp_key_is_pressed() ? 1 : 0;
s_app.key_pressed = s_app.boot_key_lock;
s_app.key_hold_ms = 0;
s_app.key_raw = s_app.key_pressed;
s_app.key_stable = s_app.key_pressed;
s_app.key_cnt = BOARD_KEY_DEBOUNCE_MS;
usr_var.usr_goto_pair = 1;
s_app.life = APP_LIFE_PAIR;
s_app.pair_ms = 0;
log_info("boot PAIR power-on bind adv\n");
}
/*============================================================================*/
/* 公有函数 */
/*============================================================================*/
AppCbc2_Ret_t app_cbc2_init(void)
{
if (s_app.inited) {
return APP_CBC2_OK;
}
memset(&s_app, 0, sizeof(s_app));
s_app.charge_dp = 2;
s_app.power = 0;
bsp_hw_init();
app_nv_init();
app_led_init();
app_rope_init();
app_mode_init();
app_dnd_init();
app_cbc2_boot_decide();
s_app.inited = 1;
sys_timer_add(NULL, app_cbc2_run, 50);
log_info("cbc2 init life=%d power=%d\n", s_app.life, s_app.power);
return APP_CBC2_OK;
}
AppCbc2_Ret_t app_cbc2_get_status(void)
{
return s_app.inited ? APP_CBC2_OK : APP_CBC2_ERR_NOT_INIT;
}
void app_cbc2_tick_1ms(void)
{
AppLedCtx_t led;
if (!s_app.inited) {
return;
}
app_rope_tick_1ms();
app_cbc2_key_tick_1ms();
(void)bsp_aloft_is_active();
memset(&led, 0, sizeof(led));
led.charge_led = s_app.charge_led;
led.pairing = (uint8_t)(s_app.life == APP_LIFE_PAIR);
led.app_power = s_app.power;
led.low_bat = s_app.alm_low;
led.life_on = (uint8_t)(s_app.life != APP_LIFE_CHARGE_ONLY);
app_led_tick_1ms(&led, 1);
}
void app_cbc2_run(void *priv)
{
(void)priv;
if (!s_app.inited) {
return;
}
app_cbc2_sample_charge();
app_cbc2_sample_battery();
app_dnd_run();
app_mode_run();
if (s_app.life == APP_LIFE_PAIR) {
s_app.pair_ms += 50;
if (usr_get_pair_flag()) {
app_cbc2_pair_end(1);
} else if (s_app.pair_ms >= BOARD_PAIR_TIMEOUT_MS) {
app_cbc2_pair_end(0);
}
}
/* 仅 BLE 已起来后才允许再调 usr_goto_pair_mode,避免和 bt_ble_init 抢广播 */
if (s_app.req_pair) {
s_app.req_pair = 0;
if (s_app.life != APP_LIFE_CHARGE_ONLY) {
app_cbc2_enter_pair();
}
}
if (s_app.req_click) {
s_app.req_click = 0;
app_cbc2_handle_click();
}
if (s_app.req_poweroff) {
s_app.req_poweroff = 0;
app_cbc2_do_poweroff();
}
app_nv_save_if_dirty();
}
void app_cbc2_fill_report(AppCbc2Report_t *out)
{
if (!out) {
return;
}
memset(out, 0, sizeof(*out));
out->power = s_app.power;
out->play_mode = s_app.play_mode;
out->calibration = app_rope_is_calibrating();
out->numberturns = app_rope_get_turns();
out->alm_low_battery = s_app.alm_low;
out->battery = s_app.battery;
out->charge = s_app.charge_dp;
app_dnd_get_raw(out->dnd);
}
AppCbc2_Ret_t app_cbc2_ctrl_power(uint8_t on)
{
if (!app_cbc2_ctrl_allowed()) {
log_info("ctrl power ignored (charge)\n");
return APP_CBC2_ERR_DENIED;
}
if (s_app.life == APP_LIFE_PAIR) {
return APP_CBC2_ERR_BUSY;
}
on = on ? 1 : 0;
if (on == s_app.power) {
return APP_CBC2_OK;
}
if (on) {
s_app.power = 1;
app_cbc2_resume_functions();
} else {
app_cbc2_pause_functions();
s_app.power = 0;
}
return APP_CBC2_OK;
}
AppCbc2_Ret_t app_cbc2_ctrl_play_mode(uint8_t mode)
{
if (!app_cbc2_ctrl_allowed() || s_app.power == 0) {
return APP_CBC2_ERR_DENIED;
}
if (app_rope_is_calibrating()) {
return APP_CBC2_ERR_BUSY;
}
app_cbc2_apply_play_mode(mode, (uint8_t)(mode != s_app.play_mode));
return APP_CBC2_OK;
}
AppCbc2_Ret_t app_cbc2_ctrl_hand(uint8_t reserved, uint8_t percent)
{
(void)reserved;
if (!app_cbc2_ctrl_allowed() || s_app.power == 0) {
return APP_CBC2_ERR_DENIED;
}
if (app_rope_is_calibrating()) {
return APP_CBC2_ERR_BUSY;
}
app_cbc2_apply_stop_reset();
if (app_rope_goto_percent(percent) != APP_ROPE_OK) {
return APP_CBC2_ERR_DENIED;
}
return APP_CBC2_OK;
}
AppCbc2_Ret_t app_cbc2_ctrl_cal(uint8_t on)
{
if (!app_cbc2_ctrl_allowed() || s_app.power == 0) {
return APP_CBC2_ERR_DENIED;
}
if (on) {
app_mode_stop();
s_app.play_mode = 0;
if (app_rope_cal_start() != APP_ROPE_OK) {
return APP_CBC2_ERR_DENIED;
}
} else {
app_rope_cal_stop();
}
return APP_CBC2_OK;
}
AppCbc2_Ret_t app_cbc2_ctrl_turns(uint8_t turns)
{
if (!app_cbc2_ctrl_allowed()) {
return APP_CBC2_ERR_DENIED;
}
app_rope_set_turns(turns);
return APP_CBC2_OK;
}
AppCbc2_Ret_t app_cbc2_ctrl_dnd(const uint8_t raw[APP_CBC2_DND_LEN])
{
if (!raw) {
return APP_CBC2_ERR_PARAM;
}
if (!app_cbc2_ctrl_allowed()) {
return APP_CBC2_ERR_DENIED;
}
app_dnd_set_raw(raw);
return APP_CBC2_OK;
}
+339
View File
@@ -0,0 +1,339 @@
/******************************************************************************
* @file app_function.c
* @brief 设备功能开关 + 自动玩法 + 手动点动 + 按键动作实现
* @author cyWu <1917507415@qq.com>
* @date 2026.08.25
* @version V1.0.0
* @history
* - V1.0.0, 2026.08.25, cyWu, 从 usr_jb_main 拆出
******************************************************************************/
#include "app_function.h"
#include "app_power.h"
#include "app_pair.h"
#include "app_mode.h"
#include "app_led.h"
#include "app_time_util.h"
#include "bsp_hw.h"
#include "jb_product.h"
#include "system/includes.h"
#define LOG_TAG_CONST APP
#define LOG_TAG "[APP_FUNC]"
#define LOG_INFO_ENABLE
#include "debug.h"
/** 功能模块总状态,替代原来一堆散落的 static 变量 */
typedef struct {
uint8_t app_power; /**< DP00=软关,电机/玩法停,BLE 仍在 */
uint8_t play_mode; /**< 当前自动模式 0~6 */
uint8_t hand_busy; /**< 手动点动进行中 */
uint8_t hand_cmd_last; /**< 边沿:同一 1/2 不重复启动 */
uint16_t hand_ms;
volatile uint8_t hand_write_pending; /**< DP2 刚写入(03 停转要靠事件) */
volatile uint8_t hand_write_cmd;
} AppFunctionCtx_t;
static AppFunctionCtx_t s_func;
static void function_hand_stop(void);
static void function_play_mode_apply(uint8_t mode);
static void function_play_mode_poll(void);
static void function_hand_poll(void);
static uint8_t function_on_click(void);
/**
* @brief 初始化功能模块状态
* @return 无
*/
void app_function_init(void)
{
memset(&s_func, 0, sizeof(s_func));
}
/**
* @brief DP2 写入钩子:仅记录第 1 字节,真正执行在 app_function_poll
* @param cmd 0=空闲,1=顺时针,2=逆时针,3=停
* @return 无
*/
void app_function_dp_hand_write(uint8_t cmd)
{
s_func.hand_write_cmd = cmd;
s_func.hand_write_pending = 1;
}
/**
* @brief 跟随 APP 下发的 DP0/DP1,并处理 DP2 手动点动
* @return 无
*/
void app_function_poll(void)
{
/* DP0APP 下发开关 */
if (!app_power_usb_online() && !app_power_poweroff_pending()) {
if (gDevData.power && !s_func.app_power) {
s_func.app_power = 1;
app_led_hint_power_on();
log_info("APP power on\n");
} else if (!gDevData.power && s_func.app_power) {
app_function_soft_off();
}
}
function_play_mode_poll();
function_hand_poll();
}
/**
* @brief 推进当前玩法节拍或手动刹车
* @param dt 距上次调用的毫秒数
* @return 无
*/
void app_function_tick_1ms(uint16_t dt)
{
if (s_func.hand_busy) {
bsp_motor_brake_tick();
s_func.hand_ms = app_add_ms(s_func.hand_ms, dt);
} else {
app_mode_run();
}
}
/**
* @brief 停手动点动(走电机反转刹车)
* @return 无
*/
static void function_hand_stop(void)
{
s_func.hand_busy = 0;
s_func.hand_ms = 0;
bsp_motor_set(BSP_MOTOR_STOP);
}
/**
* @brief 停手动 + 自动模式,并清 DP1
* @return 无
*/
void app_function_stop_all(void)
{
s_func.play_mode = 0;
gDevData.play_mode = 0;
app_mode_stop();
function_hand_stop();
}
/**
* @brief 软关:电机停、DP0=0,等 APP/短按再开。不断 BLE
* @return 无
*/
void app_function_soft_off(void)
{
s_func.app_power = 0;
gDevData.power = 0;
s_func.play_mode = 0;
gDevData.play_mode = 0;
app_mode_stop();
function_hand_stop();
log_info("soft off, wait APP power on\n");
}
/**
* @brief 上电默认开启功能(DP0=1),不带开机灯效提示
* @return 无
*/
void app_function_power_on_silent(void)
{
s_func.app_power = 1;
gDevData.power = 1;
}
/**
* @brief 查询功能开关状态(DP0)
* @return 1=开,0=关
*/
uint8_t app_function_is_power_on(void)
{
return s_func.app_power;
}
/**
* @brief 查询手动点动是否进行中
* @return 1=进行中,0=空闲
*/
uint8_t app_function_is_hand_busy(void)
{
return s_func.hand_busy;
}
/**
* @brief 查询当前自动玩法编号
* @return 0=停转,1~6=对应玩法
*/
uint8_t app_function_play_mode(void)
{
return s_func.play_mode;
}
/**
* @brief 切换自动模式 0~6
* @param mode 0=停转,1~6=对应玩法
* @return 无
*/
static void function_play_mode_apply(uint8_t mode)
{
if (mode > 6) {
mode = 0;
}
if (app_power_usb_online() || !s_func.app_power || app_power_poweroff_pending()
|| s_func.hand_busy || app_pair_is_active()) {
mode = 0;
}
s_func.play_mode = mode;
gDevData.play_mode = mode;
if (mode == 0) {
app_mode_stop();
} else {
app_mode_start(mode);
}
}
/**
* @brief 跟随 APP 下发的 DP1 自动模式
* @return 无
*/
static void function_play_mode_poll(void)
{
uint8_t mode;
if (app_power_usb_online() || !s_func.app_power || app_power_poweroff_pending()
|| s_func.hand_busy || app_pair_is_active()) {
return;
}
mode = gDevData.play_mode;
if (mode > 6) {
mode = 0;
gDevData.play_mode = 0;
}
if (mode == s_func.play_mode) {
return;
}
function_play_mode_apply(mode);
log_info("APP play_mode=%d\n", mode);
}
/**
* @brief DP2 手动:只看第 1 字节。0=空闲,1=顺时针,2=逆时针,3=停
* @note 00 00 是 DP 默认值,不当停止。百分比/力度、DP3、DP4 预留不接。
* @return 无
*/
static void function_hand_poll(void)
{
uint8_t cmd;
uint8_t got_write = s_func.hand_write_pending;
if (got_write) {
s_func.hand_write_pending = 0;
cmd = s_func.hand_write_cmd;
} else {
cmd = gDevData.hand_mode[0];
}
if (app_power_usb_online() || !s_func.app_power || app_power_poweroff_pending()
|| app_pair_is_active()) {
if (s_func.hand_busy || (got_write && (cmd == 3))) {
app_function_stop_all();
}
s_func.hand_cmd_last = cmd;
return;
}
if (cmd == 0) {
s_func.hand_cmd_last = 0;
return;
}
if (cmd == 3) {
if (got_write || s_func.hand_busy || app_mode_get_active()) {
app_function_stop_all();
log_info("hand stop cmd=3\n");
}
s_func.hand_cmd_last = 3;
return;
}
if ((cmd == 1 || cmd == 2) && (got_write || (cmd != s_func.hand_cmd_last))) {
s_func.play_mode = 0;
gDevData.play_mode = 0;
app_mode_stop();
s_func.hand_busy = 1;
s_func.hand_ms = 0;
/* 1=顺时针收绳,2=逆时针放绳(BOARD_MOTOR_IN_INA_HIGH=1 */
bsp_motor_set((cmd == 1) ? BSP_MOTOR_IN : BSP_MOTOR_OUT);
log_info("hand cmd=%d %s until 03\n", cmd, (cmd == 1) ? "cw" : "ccw");
}
s_func.hand_cmd_last = cmd;
}
/**
* @brief 短按:自动模式 0→1→…→6→0
* @return 1=已切换,0=手动点动中被忽略
*/
static uint8_t function_on_click(void)
{
if (s_func.hand_busy) {
log_info("key click ignored, hand busy\n");
return 0;
}
function_play_mode_apply((uint8_t)((s_func.play_mode + 1) % 7));
log_info("key click play_mode=%d\n", s_func.play_mode);
return 1;
}
/**
* @brief 短按回调:充电/配对忽略;软关时唤醒;否则切模式
* @return 无
*/
void usr_on_key_short(void)
{
if (app_power_poweroff_pending()) {
return;
}
if (app_power_usb_online()) {
log_info("key click ignored, charging\n");
return;
}
if (app_pair_is_active()) {
log_info("key click ignored, pairing\n");
return;
}
if (!s_func.app_power) {
s_func.app_power = 1;
gDevData.power = 1;
app_led_hint_power_on();
log_info("key click cancel soft off\n");
return;
}
if (function_on_click()) {
app_led_request_mode_blink();
}
}
/**
* @brief 长按 3s:运行中深睡(开机 3s/5s 不走这里)
* @return 无
*/
void usr_on_key_long(void)
{
if (app_power_poweroff_pending()) {
return;
}
if (app_power_usb_online()) {
log_info("key hold 3s ignored, charging\n");
return;
}
if (app_pair_is_active()) {
log_info("key hold 3s ignored, pairing\n");
return;
}
log_info("key hold 3s, poweroff\n");
app_power_request_poweroff();
}
+94
View File
@@ -0,0 +1,94 @@
/******************************************************************************
* @file app_function.h
* @brief 设备功能开关(DP0)+ 自动玩法(DP1)+ 手动点动(DP2)+ 按键动作
* @author cyWu <1917507415@qq.com>
* @date 2026.08.25
* @version V1.0.0
* @history
* - V1.0.0, 2026.08.25, cyWu, 从 usr_jb_main 拆出,状态收进 AppFunctionCtx_t
*
* @note 本模块决定“电机现在该不该转、转哪种”,会读 app_power/app_pair 的
* 状态作为阻塞条件(充电中/深睡中/配对中都不能转),也会触发
* app_led 的一次性提示灯(切模式闪、开机绿灯),但不会被它们反向依赖。
******************************************************************************/
#ifndef __APP_FUNCTION_H__
#define __APP_FUNCTION_H__
#include <stdint.h>
/**
* @brief 初始化功能模块状态(电机停,DP0=1 视调用方而定)
* @return 无
*/
void app_function_init(void);
/**
* @brief DP2 写入钩子:仅记录第 1 字节,真正执行在 app_function_poll
* @param cmd 0=空闲,1=顺时针,2=逆时针,3=停
* @return 无
*/
void app_function_dp_hand_write(uint8_t cmd);
/**
* @brief 跟随 APP 下发的 DP0/DP1,并处理 DP2 手动点动,每个业务 tick 调用一次
* @return 无
*/
void app_function_poll(void);
/**
* @brief 推进当前玩法节拍或手动刹车,每个业务 tick 调用一次
* @param dt 距上次调用的毫秒数
* @return 无
*/
void app_function_tick_1ms(uint16_t dt);
/**
* @brief 停手动 + 自动模式,并清 DP1(USB 插入 / 请求关机时用)
* @return 无
*/
void app_function_stop_all(void);
/**
* @brief 软关:电机停、DP0=0,等 APP/短按再开(USB 拔出 / APP 关时用)
* @return 无
*/
void app_function_soft_off(void);
/**
* @brief 上电默认开启功能(DP0=1),不带开机灯效提示
* @return 无
*/
void app_function_power_on_silent(void);
/**
* @brief 查询功能开关状态(DP0)
* @return 1=开,0=关
*/
uint8_t app_function_is_power_on(void);
/**
* @brief 查询手动点动是否进行中
* @return 1=进行中,0=空闲
*/
uint8_t app_function_is_hand_busy(void);
/**
* @brief 查询当前自动玩法编号
* @return 0=停转,1~6=对应玩法
*/
uint8_t app_function_play_mode(void);
/**
* @brief 业务层短按回调:由 key_manager 触发(key_manager.h 声明)
* @return 无
*/
void usr_on_key_short(void);
/**
* @brief 业务层长按 3s 回调:由 key_manager 触发(key_manager.h 声明)
* @return 无
*/
void usr_on_key_long(void);
#endif /* __APP_FUNCTION_H__ */
+134 -125
View File
@@ -1,29 +1,65 @@
/******************************************************************************
* @file app_led.c
* @brief CBC2 灯效:充电 > 配对 > APP 暂停 > 低电 > 切模式闪 > 开机绿灯提示后灭
* @brief CBC2 灯效适配:充电红与待机绿可同时亮,不再 if-else 互斥
* @author cyWu <1917507415@qq.com>
* @date 2026.08.25
* @version V1.1.0
* @version V2.0.0
* @history
* - V1.0.0, 2026.08.21, cyWu, 首次发布
* - V1.1.0, 2026.08.25, cyWu, 开机绿灯只提示几秒,避免待机常亮耗电
* - V1.1.0, 2026.08.25, cyWu, 开机绿灯只提示几秒
* - V2.0.0, 2026.08.25, cyWu, 改用充电板 LED 事件表,红绿通道独立
* - V2.1.0, 2026.08.25, cyWu, 自己从 app_power/app_pair 取数并内部组装
* - V2.2.0, 2026.08.25, cyWu, 重启/清场逻辑各提一个小函数,把意图写进注释
******************************************************************************/
#include "app_led.h"
#include "led_manager.h"
#include "bsp_hw.h"
#include "board_pin.h"
#include "app_power.h"
#include "app_pair.h"
static uint8_t s_initialized;
static uint16_t s_phase_ms;
static uint8_t s_mode_left;
static uint16_t s_mode_ms;
static uint8_t s_mode_on;
static uint16_t s_pair_res_ms;
static uint8_t s_pair_res; /* 0 none 1 ok 2 fail */
static uint16_t s_power_green_ms; /* 开机绿灯剩余 ms,0 则待机灭灯 */
static void led_start_pair_result(uint8_t ok);
/**
* @brief 灯效初始化,先全灭
* @brief 重新播放一次「一次性/限时」灯效,让它从第 0 帧重新开始
* @note led.c 的 LED_EventAdd 只有在“该优先级槽位当前不是这个事件”时才会把
* currentTime 重置为现在;如果动画正播到一半直接再 Add 一次,事件已经
* 占着槽位,不会重置,会接着上次的进度走。所以要先 Delete 清空槽位,
* 再 Add 才能强制重新计时——这不是多余代码,是“重启”这个事件的手段。
* @param evt 要重启的事件(切模式闪、开机绿灯提示、配对结果)
* @return 无
*/
static void led_restart(LED_Event_t evt)
{
LED_EventDelete(evt);
LED_EventAdd(evt);
}
/**
* @brief 绿灯正被切模式/配对结果占用(不含配对快闪本身)
* @return 1=绿灯忙
*/
static uint8_t green_pattern_busy(void)
{
return (uint8_t)(LED_IsEventExist(LED_EVENT_MODE)
|| LED_IsEventExist(LED_EVENT_PAIR_OK)
|| LED_IsEventExist(LED_EVENT_PAIR_FAIL));
}
/**
* @brief 绿灯正被配对/切模式/结果占用时,不要再挂待机绿
* @return 1=绿灯忙
*/
static uint8_t green_busy(void)
{
return (uint8_t)(LED_IsEventExist(LED_EVENT_PAIRING) || green_pattern_busy());
}
/**
* @brief 灯效初始化
* @return APP_LED_OK=成功
*/
AppLed_Ret_t app_led_init(void)
@@ -31,8 +67,7 @@ AppLed_Ret_t app_led_init(void)
if (s_initialized) {
return APP_LED_OK;
}
bsp_led_all_off();
s_power_green_ms = BOARD_LED_POWER_ON_MS;
user_led_init();
s_initialized = 1;
return APP_LED_OK;
}
@@ -47,160 +82,134 @@ AppLed_Ret_t app_led_get_status(void)
}
/**
* @brief 请求切模式绿灯闪两下
* @brief 请求切模式绿灯闪两下(与配对灯同优先级,会盖住配对闪)
* @return 无
*/
void app_led_request_mode_blink(void)
{
s_mode_left = BOARD_LED_MODE_BLINK_N;
s_mode_ms = 0;
s_mode_on = 0;
if (!s_initialized) {
return;
}
led_restart(LED_EVENT_MODE);
}
/**
* @brief 重新亮几秒开机绿灯(上电 / APP 再开
* @brief 重新挂待机绿灯(常亮或提示几秒,看 BOARD_LED_STANDBY_ON
* @return 无
*/
void app_led_hint_power_on(void)
{
s_power_green_ms = BOARD_LED_POWER_ON_MS;
if (!s_initialized) {
return;
}
led_restart(LED_EVENT_STANDBY);
}
/**
* @brief 播放一次性配对结果灯:成功常绿,失败红绿交替
* @param ok 1=成功,0=失败
* @brief 播放一次性配对结果灯
* @param ok 1=成功常绿0=失败红绿交替
* @return 无
*/
void app_led_start_pair_result(uint8_t ok)
static void led_start_pair_result(uint8_t ok)
{
s_pair_res = ok ? 1 : 2;
s_pair_res_ms = ok ? BOARD_LED_PAIR_OK_MS : BOARD_LED_PAIR_FAIL_MS;
s_phase_ms = 0;
LED_EventDelete(LED_EVENT_PAIRING);
led_restart(ok ? LED_EVENT_PAIR_OK : LED_EVENT_PAIR_FAIL);
}
/**
* @brief 按优先级刷新灯
* @param ctx 当前充电/配对/开关/低电;为空则直接返回
* @param dt 距上次调用的毫秒数
* @brief 功能关闭(DP0=0)时清掉所有“功能相关”的灯事件
* @note 故意不用 LED_EventAllDelete():那会把优先级表全清空,连充电红/满灯
* LED_EVENT_CHARGE / LED_EVENT_CHARGE_FULL)也会跟着灭掉。但充电状态
* 和 DP0 开关是两码事——设备功能关着也可能正在充电,充电灯必须继续亮,
* 所以这里只挑功能相关的几个事件逐个删,充电事件不动它。
* @return 无
*/
void app_led_tick_1ms(const AppLedCtx_t *ctx, uint16_t dt)
static void led_clear_function_events(void)
{
uint8_t red = 0;
uint8_t green = 0;
LED_EventDelete(LED_EVENT_STANDBY);
LED_EventDelete(LED_EVENT_PAIRING);
LED_EventDelete(LED_EVENT_MODE);
LED_EventDelete(LED_EVENT_PAIR_OK);
LED_EventDelete(LED_EVENT_PAIR_FAIL);
LED_EventDelete(LED_EVENT_LOWBAT);
}
if (!s_initialized || !ctx) {
return;
}
if (dt == 0) {
dt = 1;
}
/**
* @brief 按优先级刷新灯:充电/低电从 app_power 取,配对从 app_pair 取,
* 只有“功能是否开”这一项由调用方传入
* @param dt 距上次调用的毫秒数
* @param app_power 1=功能开(DP0),0=功能关,功能灯全灭
* @return 无
*/
void app_led_run(uint16_t dt, uint8_t app_power)
{
uint8_t charge_led;
uint8_t low_bat;
uint8_t pairing;
uint8_t func_on;
AppPairEvt_t pair_evt;
s_phase_ms = (uint16_t)(s_phase_ms + dt);
/* 1-2 充电独占 */
if (ctx->charge_led == 1) {
red = 1;
green = 0;
bsp_led_red_set(1);
bsp_led_green_set(0);
return;
}
if (ctx->charge_led == 2) {
bsp_led_red_set(0);
bsp_led_green_set(1);
if (!s_initialized) {
return;
}
/* 3 配对快闪 */
if (ctx->pairing) {
uint16_t t = s_phase_ms % (BOARD_LED_FAST_ON_MS + BOARD_LED_FAST_OFF_MS);
green = (t < BOARD_LED_FAST_ON_MS) ? 1 : 0;
bsp_led_red_set(0);
bsp_led_green_set(green);
return;
pair_evt = app_pair_tick_1ms(dt);
if (pair_evt == APP_PAIR_EVT_OK) {
led_start_pair_result(1);
} else if (pair_evt == APP_PAIR_EVT_FAIL) {
led_start_pair_result(0);
}
/* 4 配对结果一次性 */
if (s_pair_res && s_pair_res_ms) {
if (s_pair_res == 1) {
green = 1;
} else {
uint16_t t = s_phase_ms % (BOARD_LED_ALT_MS * 2);
if (t < BOARD_LED_ALT_MS) {
red = 1;
} else {
green = 1;
}
}
if (s_pair_res_ms > dt) {
s_pair_res_ms = (uint16_t)(s_pair_res_ms - dt);
} else {
s_pair_res_ms = 0;
s_pair_res = 0;
}
bsp_led_red_set(red);
bsp_led_green_set(green);
return;
charge_led = app_power_charge_led();
low_bat = app_power_low_bat();
func_on = app_power ? 1 : 0;
pairing = (uint8_t)(app_pair_is_active() && func_on);
if (charge_led == 1) {
LED_EventAdd(LED_EVENT_CHARGE);
} else {
LED_EventDelete(LED_EVENT_CHARGE);
}
/* 5 APP 暂停:功能灯灭 */
if (ctx->life_on && ctx->app_power == 0) {
bsp_led_all_off();
return;
if (charge_led == 2) {
LED_EventAdd(LED_EVENT_CHARGE_FULL);
} else {
LED_EventDelete(LED_EVENT_CHARGE_FULL);
}
/* 6 低电 */
if (ctx->life_on && ctx->low_bat && ctx->app_power) {
uint16_t t = s_phase_ms % BOARD_LED_LOWBAT_PERIOD_MS;
red = (t < BOARD_LED_LOWBAT_ON_MS) ? 1 : 0;
bsp_led_red_set(red);
bsp_led_green_set(0);
return;
if (pairing && !green_pattern_busy()) {
LED_EventAdd(LED_EVENT_PAIRING);
} else if (!pairing) {
LED_EventDelete(LED_EVENT_PAIRING);
}
/* 7 切模式闪 2 下 */
if (s_mode_left) {
s_mode_ms = (uint16_t)(s_mode_ms + dt);
if (!s_mode_on) {
if (s_mode_ms >= BOARD_LED_MODE_OFF_MS) {
s_mode_ms = 0;
s_mode_on = 1;
}
green = 0;
} else {
if (s_mode_ms >= BOARD_LED_MODE_ON_MS) {
s_mode_ms = 0;
s_mode_on = 0;
s_mode_left--;
}
green = 1;
}
bsp_led_red_set(0);
bsp_led_green_set(green);
return;
if (func_on && low_bat && (charge_led == 0)) {
LED_EventAdd(LED_EVENT_LOWBAT);
} else {
LED_EventDelete(LED_EVENT_LOWBAT);
}
if (!func_on) {
led_clear_function_events();
} else {
#if BOARD_LED_STANDBY_ON
/* 待机绿灯常亮 */
if (ctx->life_on && ctx->app_power) {
bsp_led_red_set(0);
bsp_led_green_set(1);
return;
}
#else
/* 开机绿灯只亮几秒,用来对比灭灯待机电流 */
if (ctx->life_on && ctx->app_power && s_power_green_ms) {
if (s_power_green_ms > dt) {
s_power_green_ms = (uint16_t)(s_power_green_ms - dt);
if ((charge_led != 2) && !pairing && !green_busy()) {
LED_EventAdd(LED_EVENT_STANDBY);
} else {
s_power_green_ms = 0;
LED_EventDelete(LED_EVENT_STANDBY);
}
bsp_led_red_set(0);
bsp_led_green_set(1);
return;
}
#endif
}
user_led_handle();
}
/**
* @brief 清空全部灯事件并灭灯
* @return 无
*/
void app_led_clear(void)
{
LED_EventAllDelete();
bsp_led_all_off();
}
+12 -17
View File
@@ -1,12 +1,15 @@
/******************************************************************************
* @file app_led.h
* @brief CBC2 灯效优先级:充电 > 配对 > 软关 > 低电 > 切模式闪 > 开机绿灯提示后灭
* @brief CBC2 灯效:红绿独立通道,充电红与待机绿可同时亮
* @author cyWu <1917507415@qq.com>
* @date 2026.08.25
* @version V1.1.0
* @version V2.0.0
* @history
* - V1.0.0, 2026.08.21, cyWu, 首次发布
* - V1.1.0, 2026.08.25, cyWu, 开机绿灯只提示几秒,避免待机常亮耗电
* - V2.0.0, 2026.08.25, cyWu, 改用充电板 LED 事件表
* - V2.1.0, 2026.08.25, cyWu, 改为自己从 app_power/app_pair 取数,
* 调用方不用再手填 AppLedCtx_t
******************************************************************************/
#ifndef __APP_LED_H__
@@ -19,14 +22,6 @@ typedef enum {
APP_LED_ERR_NOT_INIT
} AppLed_Ret_t;
typedef struct {
uint8_t charge_led; /**< 0=无 1=充电红常亮 2=充满绿常亮 */
uint8_t pairing; /**< 1=配对中绿快闪 */
uint8_t app_power; /**< DP0,为 0 则功能灯灭 */
uint8_t low_bat; /**< 1=低电红闪 */
uint8_t life_on; /**< 1=允许显示开机灯效 */
} AppLedCtx_t;
/**
* @brief 灯效初始化,先全灭
* @return APP_LED_OK=成功
@@ -34,12 +29,13 @@ typedef struct {
AppLed_Ret_t app_led_init(void);
/**
* @brief 按优先级刷新灯
* @param ctx 充电/配对/开关/低电状态,不可为空
* @param dt 距上次调用的毫秒数
* @brief 按优先级刷新灯:充电/低电从 app_power 取,配对从 app_pair 取,
* 只有“功能是否开”这一项由调用方(app_function)传入
* @param dt 距上次调用的毫秒数
* @param app_power 1=功能开(DP0),0=功能关,功能灯全灭
* @return 无
*/
void app_led_tick_1ms(const AppLedCtx_t *ctx, uint16_t dt);
void app_led_run(uint16_t dt, uint8_t app_power);
/**
* @brief 请求切模式绿灯闪两下
@@ -54,11 +50,10 @@ void app_led_request_mode_blink(void);
void app_led_hint_power_on(void);
/**
* @brief 播放一次性配对结果灯
* @param ok 1=成功常绿,0=失败红绿交替
* @brief 清空全部灯事件并灭灯(关机前调用)
* @return 无
*/
void app_led_start_pair_result(uint8_t ok);
void app_led_clear(void);
/**
* @brief 查询是否已初始化
+33 -28
View File
@@ -90,10 +90,15 @@ static const AppModeStep_t *const s_tables[7] = {
0, s_mode1, s_mode2, s_mode3, s_mode4, s_mode5, s_mode6
};
static uint8_t s_initialized;
static uint8_t s_active;
static uint8_t s_step;
static u32 s_step_t0;
/** 模块状态,替代原来散落的 4 个 static */
typedef struct {
uint8_t initialized;
uint8_t active; /**< 0=停转,1~6=当前玩法 */
uint8_t step; /**< 当前节拍在表里的下标 */
u32 step_t0; /**< 当前节拍起始时间戳,用于判断是否该跳下一步 */
} AppModeCtx_t;
static AppModeCtx_t s_mode;
extern u32 timer_get_ms(void);
@@ -103,20 +108,20 @@ extern u32 timer_get_ms(void);
*/
static void app_mode_apply_step(void)
{
const AppModeStep_t *tbl = s_tables[s_active];
const AppModeStep_t *tbl = s_tables[s_mode.active];
if (!tbl) {
bsp_motor_set(BSP_MOTOR_STOP);
return;
}
if (tbl[s_step].ms == 0) {
s_step = 0;
if (tbl[s_mode.step].ms == 0) {
s_mode.step = 0;
}
s_step_t0 = timer_get_ms();
if (tbl[s_step].dir == BSP_MOTOR_STOP) {
s_mode.step_t0 = timer_get_ms();
if (tbl[s_mode.step].dir == BSP_MOTOR_STOP) {
bsp_motor_set(BSP_MOTOR_STOP);
} else {
bsp_motor_set_pwm(tbl[s_step].dir, tbl[s_step].duty);
bsp_motor_set_pwm(tbl[s_mode.step].dir, tbl[s_mode.step].duty);
}
}
@@ -126,10 +131,10 @@ static void app_mode_apply_step(void)
*/
AppMode_Ret_t app_mode_init(void)
{
s_active = 0;
s_step = 0;
s_step_t0 = timer_get_ms();
s_initialized = 1;
s_mode.active = 0;
s_mode.step = 0;
s_mode.step_t0 = timer_get_ms();
s_mode.initialized = 1;
bsp_motor_set(BSP_MOTOR_STOP);
return APP_MODE_OK;
}
@@ -140,7 +145,7 @@ AppMode_Ret_t app_mode_init(void)
*/
AppMode_Ret_t app_mode_get_status(void)
{
return s_initialized ? APP_MODE_OK : APP_MODE_ERR_NOT_INIT;
return s_mode.initialized ? APP_MODE_OK : APP_MODE_ERR_NOT_INIT;
}
/**
@@ -149,7 +154,7 @@ AppMode_Ret_t app_mode_get_status(void)
*/
uint8_t app_mode_get_active(void)
{
return s_active;
return s_mode.active;
}
/**
@@ -159,15 +164,15 @@ uint8_t app_mode_get_active(void)
*/
AppMode_Ret_t app_mode_start(uint8_t mode)
{
if (!s_initialized) {
if (!s_mode.initialized) {
return APP_MODE_ERR_NOT_INIT;
}
if (mode < 1 || mode > 6) {
app_mode_stop();
return APP_MODE_ERR_PARAM;
}
s_active = mode;
s_step = 0;
s_mode.active = mode;
s_mode.step = 0;
app_mode_apply_step();
log_info("auto mode %d start\n", mode);
return APP_MODE_OK;
@@ -179,12 +184,12 @@ AppMode_Ret_t app_mode_start(uint8_t mode)
*/
void app_mode_stop(void)
{
if (s_active) {
log_info("auto mode %d stop\n", s_active);
if (s_mode.active) {
log_info("auto mode %d stop\n", s_mode.active);
}
s_active = 0;
s_step = 0;
s_step_t0 = timer_get_ms();
s_mode.active = 0;
s_mode.step = 0;
s_mode.step_t0 = timer_get_ms();
bsp_motor_set(BSP_MOTOR_STOP);
}
@@ -197,17 +202,17 @@ void app_mode_run(void)
const AppModeStep_t *tbl;
bsp_motor_brake_tick();
if (!s_initialized || s_active == 0) {
if (!s_mode.initialized || s_mode.active == 0) {
return;
}
tbl = s_tables[s_active];
tbl = s_tables[s_mode.active];
if (!tbl) {
return;
}
if ((timer_get_ms() - s_step_t0) >= tbl[s_step].ms) {
s_step++;
if ((timer_get_ms() - s_mode.step_t0) >= tbl[s_mode.step].ms) {
s_mode.step++;
app_mode_apply_step();
}
}
+75
View File
@@ -0,0 +1,75 @@
/******************************************************************************
* @file app_pair.c
* @brief 配对会话计时实现
* @author cyWu <1917507415@qq.com>
* @date 2026.08.25
* @version V1.0.0
* @history
* - V1.0.0, 2026.08.25, cyWu, 从 usr_jb_main 拆出
******************************************************************************/
#include "app_pair.h"
#include "board_pin.h"
#include "usr_le_api.h"
#include "system/includes.h"
#define LOG_TAG_CONST APP
#define LOG_TAG "[APP_PAIR]"
#define LOG_INFO_ENABLE
#include "debug.h"
/** 配对会话状态,替代原来散落的 s_pairing / s_pair_ms */
typedef struct {
uint8_t active;
uint32_t elapsed_ms;
} AppPairCtx_t;
static AppPairCtx_t s_pair;
/**
* @brief 开始一次配对会话(计时清零)
* @return 无
*/
void app_pair_start(void)
{
s_pair.active = 1;
s_pair.elapsed_ms = 0;
}
/**
* @brief 推进配对计时并检查结果,每个业务 tick 调用一次
* @param dt 距上次调用的毫秒数
* @return APP_PAIR_EVT_OK=配对成功,APP_PAIR_EVT_FAIL=超时,否则 NONE
*/
AppPairEvt_t app_pair_tick_1ms(uint16_t dt)
{
if (!s_pair.active) {
return APP_PAIR_EVT_NONE;
}
if (usr_get_pair_flag()) {
s_pair.active = 0;
s_pair.elapsed_ms = 0;
log_info("pair ok\n");
return APP_PAIR_EVT_OK;
}
s_pair.elapsed_ms += dt;
if (s_pair.elapsed_ms >= BOARD_PAIR_TIMEOUT_MS) {
s_pair.active = 0;
s_pair.elapsed_ms = 0;
log_info("pair timeout 60s, stay on\n");
return APP_PAIR_EVT_FAIL;
}
return APP_PAIR_EVT_NONE;
}
/**
* @brief 查询是否仍在配对会话中
* @return 1=进行中,0=未在配对
*/
uint8_t app_pair_is_active(void)
{
return s_pair.active;
}
+45
View File
@@ -0,0 +1,45 @@
/******************************************************************************
* @file app_pair.h
* @brief 配对会话计时:开机配对窗口是否成功/超时,与 BLE 配对本身解耦
* @author cyWu <1917507415@qq.com>
* @date 2026.08.25
* @version V1.0.0
* @history
* - V1.0.0, 2026.08.25, cyWu, 从 usr_jb_main 拆出
*
* @note 真正的 BLE 广播/绑定逻辑在 usr_le_code;本模块只负责“进入配对后
* 60s 内 usr_get_pair_flag() 是否变 1”这件事,纯状态,不碰灯/按键。
******************************************************************************/
#ifndef __APP_PAIR_H__
#define __APP_PAIR_H__
#include <stdint.h>
/** 配对结果边沿事件,仅在状态变化那一次返回一次 */
typedef enum {
APP_PAIR_EVT_NONE = 0,
APP_PAIR_EVT_OK,
APP_PAIR_EVT_FAIL
} AppPairEvt_t;
/**
* @brief 开始一次配对会话(计时清零)
* @return 无
*/
void app_pair_start(void);
/**
* @brief 推进配对计时并检查结果,每个业务 tick 调用一次
* @param dt 距上次调用的毫秒数
* @return APP_PAIR_EVT_OK=配对成功,APP_PAIR_EVT_FAIL=超时,否则 NONE
*/
AppPairEvt_t app_pair_tick_1ms(uint16_t dt);
/**
* @brief 查询是否仍在配对会话中
* @return 1=进行中,0=未在配对
*/
uint8_t app_pair_is_active(void);
#endif /* __APP_PAIR_H__ */
+453
View File
@@ -0,0 +1,453 @@
/******************************************************************************
* @file app_power.c
* @brief USB / 充电检测、电量采样与低电策略、深睡请求生命周期实现
* @author cyWu <1917507415@qq.com>
* @date 2026.08.25
* @version V1.0.0
* @history
* - V1.0.0, 2026.08.25, cyWu, 从 usr_jb_main 拆出
******************************************************************************/
#include "app_power.h"
#include "app_time_util.h"
#include "bsp_hw.h"
#include "board_pin.h"
#include "jb_product.h"
#include "app_main.h"
#include "usr_le_api.h"
#include "system/includes.h"
#include "ui_manage.h"
#include <string.h>
#define LOG_TAG_CONST APP
#define LOG_TAG "[APP_PWR]"
#define LOG_INFO_ENABLE
#include "debug.h"
#define BAT_SAMPLE_MS 100
#define BAT_AVG_N 8
#define BAT_BOOT_MS 3000 /* 上电 3s 内不因低电深睡 */
#define BAT_LOG_MS 10000
#define BAT_LOW_WIN 3 /* 连续约 2.4s 才进低电告警 */
#define BAT_EMPTY_WIN 5 /* 连续约 4s 才 3.2V 深睡 */
/** 充电检测:CHG/VPWR 消抖后的结果 */
typedef struct {
uint8_t usb_online;
uint8_t charge_led; /**< 0=无 1=充电红 2=充满绿 */
uint8_t charge_led_last; /**< 仅用于变化时打日志 */
uint16_t full_ms; /**< 拔电瞬间 CHG 先变高,需等满 1s 才当充满 */
} AppChargeState_t;
/** 电量采样:滑动平均 + 百分比 */
typedef struct {
uint16_t tick_ms;
uint16_t acc_mv;
uint8_t n;
uint16_t mv;
uint8_t pct;
uint16_t boot_ms;
uint16_t log_ms;
} AppBatterySample_t;
/** 低电告警:进入/退出都要连续窗口消抖,避免电机拉压降误报 */
typedef struct {
uint8_t low_bat;
uint32_t low_ms; /**< 低电已持续时长,满 1 分钟深睡 */
uint8_t empty_win;
uint8_t enter_win;
uint8_t exit_win;
} AppBatteryAlarm_t;
/** 电源模块总状态,替代原来一堆散落的 static 变量 */
typedef struct {
AppChargeState_t charge;
AppBatterySample_t sample;
AppBatteryAlarm_t alarm;
uint8_t req_poweroff;
AppPowerHook_t on_request;
AppPowerHook_t on_finalize;
} AppPowerCtx_t;
static AppPowerCtx_t s_power;
static void power_do_poweroff(void *priv);
static AppPowerEvt_t power_charge_tick(uint16_t dt);
static void power_battery_tick(uint16_t dt);
static uint8_t power_bat_percent(uint16_t mv);
static uint8_t power_battery_sample_ready(uint16_t dt, uint16_t *out_mv);
static void power_battery_update_percent(uint16_t mv);
static void power_battery_check_empty(uint16_t mv);
static void power_battery_check_low(uint16_t mv);
static void power_battery_check_low_timeout(uint16_t dt);
static void power_battery_periodic_log(uint16_t dt);
/**
* @brief 初始化电源模块状态
* @return 无
*/
void app_power_init(void)
{
memset(&s_power, 0, sizeof(s_power));
s_power.charge.charge_led_last = 0xff;
}
/**
* @brief 注册深睡请求的两段式回调
* @param on_request 刚请求深睡时调用(停电机、灯全灭)
* @param on_finalize 50ms 后调用(真正进深睡,不会返回)
* @return 无
*/
void app_power_set_hooks(AppPowerHook_t on_request, AppPowerHook_t on_finalize)
{
s_power.on_request = on_request;
s_power.on_finalize = on_finalize;
}
/**
* @brief 充电检测 + 电量采样,每个业务 tick 调用一次
* @param dt 距上次调用的毫秒数
* @return USB 插拔边沿事件,多数时候是 APP_POWER_EVT_NONE
*/
AppPowerEvt_t app_power_tick_1ms(uint16_t dt)
{
AppPowerEvt_t evt = power_charge_tick(dt);
power_battery_tick(dt);
return evt;
}
/**
* @brief 查询 USB 是否在位(含正在充电)
* @return 1=在位,0=未接
*/
uint8_t app_power_usb_online(void)
{
return s_power.charge.usb_online;
}
/**
* @brief 查询充电灯状态
* @return 0=无 1=充电中红灯 2=已充满绿灯
*/
uint8_t app_power_charge_led(void)
{
return s_power.charge.charge_led;
}
/**
* @brief 查询是否处于低电告警
* @return 1=低电,0=正常
*/
uint8_t app_power_low_bat(void)
{
return s_power.alarm.low_bat;
}
/**
* @brief 请求深睡:立即触发 on_request 钩子,50ms 后触发 on_finalize
* @return 无
*/
void app_power_request_poweroff(void)
{
if (s_power.req_poweroff) {
return;
}
s_power.req_poweroff = 1;
log_info("request deep off\n");
if (s_power.on_request) {
s_power.on_request();
}
sys_timeout_add(NULL, power_do_poweroff, 50);
}
/**
* @brief 查询深睡请求是否已发起
* @return 1=已发起,0=未发起
*/
uint8_t app_power_poweroff_pending(void)
{
return s_power.req_poweroff;
}
/**
* @brief 50ms 延时到点:真正进入深睡
* @param priv 未使用
* @return 无
*/
static void power_do_poweroff(void *priv)
{
(void)priv;
log_info("enter deep off\n");
if (s_power.on_finalize) {
s_power.on_finalize();
}
}
/**
* @brief 充电检测:CHG 低=充电中;VPWR 在且 CHG 高满 1s=充满;拔电报 USB_OUT
* @param dt 距上次调用的毫秒数
* @return USB 插拔边沿事件
*/
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;
AppPowerEvt_t evt = APP_POWER_EVT_NONE;
if (chg_low) {
c->full_ms = 0;
c->charge_led = 1;
gDevData.charge = CHARGE_1;
usb = 1;
} else if (vpwr) {
/* 拔电时 CHG 先变高、VPWR 还没掉,不能立刻当充满 */
c->full_ms = app_add_ms(c->full_ms, dt);
if (c->full_ms >= BOARD_CHG_FULL_MS) {
c->charge_led = 2;
}
gDevData.charge = (c->charge_led == 1) ? CHARGE_1 : CHARGE_2;
usb = 1;
} else {
c->full_ms = 0;
c->charge_led = 0;
gDevData.charge = CHARGE_2;
usb = 0;
}
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(),
usr_get_pair_flag(), usr_var.usr_goto_pair);
usr_var.usr_power_charge_flag = usb ? 1 : 2;
if (usb) {
evt = APP_POWER_EVT_USB_IN;
} else {
ui_update_status(STATUS_NORMAL_POWER);
evt = APP_POWER_EVT_USB_OUT;
}
}
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());
}
return evt;
}
/**
* @brief 电压换算电量:4.2V=100%3.2V=0%
* @param mv 电池电压,单位 mV
* @return 电量百分比,范围 0~100
*/
static uint8_t power_bat_percent(uint16_t mv)
{
int32_t span = (int32_t)BOARD_VBAT_FULL_MV - (int32_t)BOARD_VBAT_EMPTY_MV;
int32_t pct;
if (mv <= BOARD_VBAT_EMPTY_MV) {
return 0;
}
if (mv >= BOARD_VBAT_FULL_MV) {
return 100;
}
pct = ((int32_t)mv - BOARD_VBAT_EMPTY_MV) * 100 / span;
if (pct < 0) {
pct = 0;
}
if (pct > 100) {
pct = 100;
}
return (uint8_t)pct;
}
/**
* @brief 滑动平均采样:每 BAT_SAMPLE_MS 采一次电压,凑够 BAT_AVG_N 个才出新均值
* @note 靠采样点数(而非时间)判满,dt 波动也不会多算/少算样本
* @param dt 距上次调用的毫秒数
* @param out_mv 新均值输出,仅在返回 1 时有效
* @return 1=本次凑出新均值,0=还在累积
*/
static uint8_t power_battery_sample_ready(uint16_t dt, uint16_t *out_mv)
{
AppBatterySample_t *s = &s_power.sample;
s->tick_ms = app_add_ms(s->tick_ms, dt);
if (s->tick_ms < BAT_SAMPLE_MS) {
return 0;
}
s->tick_ms = 0;
s->acc_mv += bsp_vbat_mv();
s->n++;
if (s->n < BAT_AVG_N) {
return 0;
}
*out_mv = (uint16_t)(s->acc_mv / s->n);
s->acc_mv = 0;
s->n = 0;
s->mv = *out_mv;
return 1;
}
/**
* @brief 按新均值刷新电量百分比,仅在变化时才写协议层数据
* @param mv 本次均值电压,单位 mV
* @return 无
*/
static void power_battery_update_percent(uint16_t mv)
{
uint8_t pct = power_bat_percent(mv);
if (pct != s_power.sample.pct) {
s_power.sample.pct = pct;
gDevData.battery = pct;
}
}
/**
* @brief 电量耗尽判定:连续 BAT_EMPTY_WIN 次采样都 <= EMPTY_MV 才认定,直接请求深睡
* @note 充电中不判定;用连续窗口而非单次采样,避免电机拉压降瞬间跌破误报
* @param mv 本次均值电压,单位 mV
* @return 无
*/
static void power_battery_check_empty(uint16_t mv)
{
AppBatteryAlarm_t *a = &s_power.alarm;
if (s_power.charge.usb_online || mv > BOARD_VBAT_EMPTY_MV) {
a->empty_win = 0;
return;
}
if (a->empty_win < 0xFF) {
a->empty_win++;
}
if (a->empty_win < BAT_EMPTY_WIN) {
return;
}
a->low_bat = 1;
gDevData.alm_low_battery = 1;
if (!s_power.req_poweroff) {
log_info("vbat %dmV empty, deep off\n", mv);
app_power_request_poweroff();
}
}
/**
* @brief 低电迟滞判定:进入/退出各自要求连续 BAT_LOW_WIN 次采样,避免电压在门限附近抖动
* @param mv 本次均值电压,单位 mV
* @return 无
*/
static void power_battery_check_low(uint16_t mv)
{
AppBatteryAlarm_t *a = &s_power.alarm;
if (mv < BOARD_VBAT_LOW_MV) {
a->exit_win = 0;
if (a->low_bat || a->enter_win >= 0xFF) {
return;
}
a->enter_win++;
if (a->enter_win >= BAT_LOW_WIN) {
a->low_bat = 1;
a->low_ms = 0;
gDevData.alm_low_battery = 1;
log_info("low battery %dmV %d%%\n", mv, s_power.sample.pct);
}
} else if (mv > (BOARD_VBAT_LOW_MV + BOARD_VBAT_LOW_HYST_MV)) {
a->enter_win = 0;
if (!a->low_bat) {
return;
}
if (a->exit_win < 0xFF) {
a->exit_win++;
}
if (a->exit_win >= BAT_LOW_WIN) {
a->low_bat = 0;
a->low_ms = 0;
gDevData.alm_low_battery = 0;
log_info("battery recover %dmV %d%%\n", mv, s_power.sample.pct);
}
} else {
/* 低电门限与迟滞上限之间的死区,两个窗口都不动 */
a->enter_win = 0;
a->exit_win = 0;
}
}
/**
* @brief 低电持续满 BOARD_LOWBAT_SLEEP_MS1 分钟)才请求深睡
* @note 充电中 / 已请求深睡 / 仍在开机保护期内,计时清零不累加
* @param dt 距上次调用的毫秒数
* @return 无
*/
static void power_battery_check_low_timeout(uint16_t dt)
{
AppBatteryAlarm_t *a = &s_power.alarm;
if (s_power.charge.usb_online || s_power.req_poweroff
|| (s_power.sample.boot_ms < BAT_BOOT_MS)) {
a->low_ms = 0;
return;
}
if (!a->low_bat || a->low_ms >= BOARD_LOWBAT_SLEEP_MS) {
return;
}
a->low_ms += dt;
if (a->low_ms >= BOARD_LOWBAT_SLEEP_MS) {
a->low_ms = BOARD_LOWBAT_SLEEP_MS;
log_info("low battery 1min, deep off\n");
app_power_request_poweroff();
}
}
/**
* @brief 每 BAT_LOG_MS 打印一次电量状态,方便离线看日志排查
* @param dt 距上次调用的毫秒数
* @return 无
*/
static void power_battery_periodic_log(uint16_t dt)
{
AppBatterySample_t *s = &s_power.sample;
s->log_ms = app_add_ms(s->log_ms, dt);
if (s->log_ms >= BAT_LOG_MS) {
s->log_ms = 0;
log_info("bat %d%% %dmV alm=%d\n", s->pct, s->mv, s_power.alarm.low_bat);
}
}
/**
* @brief 电量采样与低电策略,每个业务 tick 调用一次
* @note 电机运转时只记电压、不改百分比/告警,避免电流拉垮 VBAT 误报。
* 电机是否忙由调用方(app_function)决定,本模块不认识电机,
* 这里用「外部传入」而非直接查询,保持单向依赖。
* @param dt 距上次调用的毫秒数
* @return 无
*/
static void power_battery_tick(uint16_t dt)
{
AppBatterySample_t *s = &s_power.sample;
uint16_t mv;
if (s->boot_ms < BAT_BOOT_MS) {
s->boot_ms = app_add_ms(s->boot_ms, dt);
}
if (power_battery_sample_ready(dt, &mv)) {
power_battery_update_percent(mv);
/* 开机保护期内只更新百分比,不判定任何告警 */
if (s->boot_ms >= BAT_BOOT_MS) {
power_battery_check_empty(mv);
power_battery_check_low(mv);
}
}
power_battery_check_low_timeout(dt);
power_battery_periodic_log(dt);
}
+81
View File
@@ -0,0 +1,81 @@
/******************************************************************************
* @file app_power.h
* @brief USB / 充电检测、电量采样与低电策略、深睡请求生命周期
* @author cyWu <1917507415@qq.com>
* @date 2026.08.25
* @version V1.0.0
* @history
* - V1.0.0, 2026.08.25, cyWu, 从 usr_jb_main 拆出,状态收进 AppPowerCtx_t
*
* @note 本模块只管“电从哪来、还剩多少、要不要关机”,不认识电机/灯/按键。
* 需要在 USB 插拔 / 深睡时联动其它模块的地方,一律走 app_power_set_hooks
* 注册的回调,调用方(usr_jb_main)负责接线,避免本模块反过来依赖别的模块。
******************************************************************************/
#ifndef __APP_POWER_H__
#define __APP_POWER_H__
#include <stdint.h>
/** USB 插拔的边沿事件,仅在状态变化那一次返回一次 */
typedef enum {
APP_POWER_EVT_NONE = 0,
APP_POWER_EVT_USB_IN,
APP_POWER_EVT_USB_OUT
} AppPowerEvt_t;
/** 深睡请求的钩子:on_request 用于立即停机(马达/灯),on_finalize 用于真正掉电 */
typedef void (*AppPowerHook_t)(void);
/**
* @brief 初始化电源模块状态
* @return 无
*/
void app_power_init(void);
/**
* @brief 注册深睡请求的两段式回调
* @param on_request 刚请求深睡时调用(停电机、灯全灭)
* @param on_finalize 50ms 后调用(真正进深睡,不会返回)
* @return 无
*/
void app_power_set_hooks(AppPowerHook_t on_request, AppPowerHook_t on_finalize);
/**
* @brief 充电检测 + 电量采样,每个业务 tick 调用一次
* @param dt 距上次调用的毫秒数
* @return USB 插拔边沿事件,多数时候是 APP_POWER_EVT_NONE
*/
AppPowerEvt_t app_power_tick_1ms(uint16_t dt);
/**
* @brief 查询 USB 是否在位(含正在充电)
* @return 1=在位,0=未接
*/
uint8_t app_power_usb_online(void);
/**
* @brief 查询充电灯状态
* @return 0=无 1=充电中红灯 2=已充满绿灯
*/
uint8_t app_power_charge_led(void);
/**
* @brief 查询是否处于低电告警
* @return 1=低电,0=正常
*/
uint8_t app_power_low_bat(void);
/**
* @brief 请求深睡:立即触发 on_request 钩子,50ms 后触发 on_finalize
* @return 无
*/
void app_power_request_poweroff(void);
/**
* @brief 查询深睡请求是否已发起(发起后其它模块应停止响应新操作)
* @return 1=已发起,0=未发起
*/
uint8_t app_power_poweroff_pending(void);
#endif /* __APP_POWER_H__ */
+29
View File
@@ -0,0 +1,29 @@
/******************************************************************************
* @file app_time_util.h
* @brief 业务模块共用的毫秒计时小工具,避免每个模块各写一份
* @author cyWu <1917507415@qq.com>
* @date 2026.08.25
* @version V1.0.0
* @history
* - V1.0.0, 2026.08.25, cyWu, 首次发布(从 usr_jb_main 拆出)
******************************************************************************/
#ifndef __APP_TIME_UTIL_H__
#define __APP_TIME_UTIL_H__
#include <stdint.h>
/**
* @brief 毫秒累加,饱和到 0xFFFF,避免 16 位计时溢出回绕
* @param v 当前值
* @param dt 本次增加量
* @return 累加结果,最大 0xFFFF
*/
static inline uint16_t app_add_ms(uint16_t v, uint16_t dt)
{
uint32_t n = (uint32_t)v + dt;
return (n > 0xffffu) ? 0xffffu : (uint16_t)n;
}
#endif /* __APP_TIME_UTIL_H__ */
+267
View File
@@ -0,0 +1,267 @@
/******************************************************************************
* @file key.c
* @brief 按键状态机实现
* @author cyWu <1917507415@qq.com>
* @date 2026.08.25
* @version V1.0.0
* @history
* - V1.0.0, 2026.08.25, cyWu, 从天誉 MJ-1.0 充电板移植到杰理 BR28
******************************************************************************/
#include "key.h"
#include <stddef.h>
static uint32_t s_key_count_time;
static uint8_t s_key_num = USR_KEY_COUNT;
keyCategory_t keyTable[USR_KEY_COUNT];
/**
* @brief 读按键电平到 fsm
* @param key 状态机
* @return 1=已读,0=被屏蔽
*/
static uint8_t get_key_level(keyFSM_t *key)
{
if (key->keyShield == KEY_DISABLE) {
return 0;
}
if (key->keyReadValue == NULL) {
return 0;
}
key->keyLevel = (key->keyReadValue() == key->keyDownLevel) ? Bit_SET : Bit_RESET;
return 1;
}
/**
* @brief 单击 / 双击间隔判定
* @param click 状态机
* @return 1=可以出事件
*/
static uint8_t key_click_frequency(keyFSM_t *click)
{
click->keyInterval++;
switch (click->keyFrequency) {
case CLICK:
if (click->keyInterval >= (KEY_INTERVAL / (DEBOUNCE_TIME * KEY_TIMER_MS))) {
click->eventType = SHORT_Event;
return 1;
}
break;
case DbLCLICK:
if (click->keyInterval >= (KEY_INTERVAL / (DEBOUNCE_TIME * KEY_TIMER_MS))) {
click->eventType = DBCL_Event;
return 1;
}
break;
default:
return 1;
}
return 0;
}
/**
* @brief 按键状态机步进
* @param key_buf 状态机
* @return 1=已处理
*/
static uint8_t read_key_status(keyFSM_t *key_buf)
{
if (!get_key_level(key_buf)) {
return 0;
}
switch (key_buf->keyStatus) {
case KEY_ST_NULL:
if (key_buf->keyLevel == Bit_SET) {
key_buf->keyStatus = KEY_ST_SURE;
}
break;
case KEY_ST_SURE:
if (key_buf->keyLevel == Bit_SET) {
key_buf->eventType = DOWN_Event;
key_buf->keyStatus = KEY_ST_DOWN;
key_buf->keyCount = 0;
key_buf->keyLongFlag = 1;
} else {
key_buf->keyStatus = KEY_ST_NULL;
}
break;
case KEY_ST_DOWN:
if (key_buf->keyLevel != Bit_SET) {
if (key_buf->keyShortFlag == 0) {
key_buf->keyShortFlag = 1;
key_buf->keyFrequency++;
key_buf->keyInterval = 0;
}
if (key_click_frequency(key_buf)) {
key_buf->keyFrequency = 0;
key_buf->keyStatus = KEY_ST_NULL;
}
if ((key_buf->keyCount >= key_buf->keyLongTime / DEBOUNCE_TIME)
&& (key_buf->keyCount < key_buf->keyLastTime / DEBOUNCE_TIME)) {
key_buf->keyFrequency = 0;
key_buf->keyStatus = KEY_ST_NULL;
key_buf->eventType = RELEASE_Event;
}
key_buf->keyCount = 0;
key_buf->keyLongFlag = 1;
} else {
if ((++key_buf->keyCount >= key_buf->keyLastTime / DEBOUNCE_TIME)) {
key_buf->keyCount = 0;
key_buf->keyFrequency = 0;
key_buf->keyStatus = KEY_ST_LONG;
key_buf->eventType = LAST_Event;
key_buf->keyLongFlag = 1;
}
if ((key_buf->keyCount >= key_buf->keyLongTime / DEBOUNCE_TIME)
&& (key_buf->keyCount < key_buf->keyLastTime / DEBOUNCE_TIME)) {
key_buf->keyFrequency = 0;
if (key_buf->keyLongFlag == 1) {
key_buf->eventType = LONG_Event;
key_buf->keyLongFlag = 0;
}
}
key_buf->keyShortFlag = 0;
}
break;
case KEY_ST_LONG:
if (key_buf->keyLevel != Bit_SET) {
key_buf->keyStatus = KEY_ST_NULL;
key_buf->eventType = RELEASE_Event;
}
break;
default:
break;
}
return 1;
}
/**
* @brief 扫描全部按键状态机
* @return 无
*/
static void key_event_process(void)
{
size_t i;
for (i = 0; i < s_key_num; i++) {
(void)read_key_status(&keyTable[i].fsm);
}
}
/**
* @brief 1ms 调用一次;满 DEBOUNCE_TIME 才真正采样
* @return 无
*/
void keyCheckProcess(void)
{
s_key_count_time++;
if (s_key_count_time >= (DEBOUNCE_TIME / KEY_TIMER_MS)) {
s_key_count_time = 0;
key_event_process();
}
}
/**
* @brief 拷贝产品按键表
* @param keys 按键数组,长度 USR_KEY_COUNT
* @return 无
*/
void keyParaInit(keyCategory_t *keys)
{
if (keys == NULL) {
return;
}
if (USR_KEY_COUNT >= KEY_MAX_NUMBER) {
s_key_num = KEY_MAX_NUMBER;
}
memcpy(keyTable, keys, sizeof(keyCategory_t) * s_key_num);
}
/**
* @brief 运行中改长按/连按时长并复位状态
* @param key_event 按键索引
* @param key_param 新参数
* @return 无
*/
void setKeyEventParams(uint8_t key_event, keyCategory_t key_param)
{
if (key_event >= s_key_num) {
return;
}
keyTable[key_event].fsm.keyShield = key_param.fsm.keyShield;
keyTable[key_event].fsm.keyLongTime = key_param.fsm.keyLongTime;
keyTable[key_event].fsm.keyLastTime = key_param.fsm.keyLastTime;
keyTable[key_event].fsm.keyStatus = KEY_ST_NULL;
keyTable[key_event].fsm.eventType = NULL_Event;
keyTable[key_event].fsm.keyCount = 0;
}
/**
* @brief 派发按键事件回调
* @return 无
*/
void keyHandle(void)
{
size_t i;
for (i = 0; i < s_key_num; i++) {
if (keyTable[i].fsm.eventType == NULL_Event) {
continue;
}
switch (keyTable[i].fsm.eventType) {
case RELEASE_Event:
if (keyTable[i].func.releasePressCb != NULL) {
keyTable[i].func.releasePressCb();
}
keyTable[i].fsm.eventType = NULL_Event;
break;
case SHORT_Event:
if (keyTable[i].func.ShortPressCb != NULL) {
keyTable[i].func.ShortPressCb();
}
keyTable[i].fsm.eventType = NULL_Event;
break;
case DOWN_Event:
if (keyTable[i].func.downPressCb != NULL) {
keyTable[i].func.downPressCb();
}
keyTable[i].fsm.eventType = NULL_Event;
break;
case LONG_Event:
if (keyTable[i].func.longPressCb != NULL) {
keyTable[i].func.longPressCb();
}
keyTable[i].fsm.eventType = NULL_Event;
break;
case LAST_Event:
if (keyTable[i].func.lastPressCb != NULL) {
keyTable[i].func.lastPressCb();
}
break;
case DBCL_Event:
if (keyTable[i].func.dbclPressCb != NULL) {
keyTable[i].func.dbclPressCb();
}
keyTable[i].fsm.eventType = NULL_Event;
break;
default:
break;
}
}
}
/**
* @brief 复位指定按键状态机
* @param key_index 按键索引
* @return 无
*/
void reset_key_Status(keyList key_index)
{
if (key_index >= USR_KEY_COUNT) {
return;
}
keyTable[key_index].fsm.keyStatus = KEY_ST_NULL;
keyTable[key_index].fsm.eventType = NULL_Event;
keyTable[key_index].fsm.keyCount = 0;
}
+100
View File
@@ -0,0 +1,100 @@
/******************************************************************************
* @file key.h
* @brief 按键状态机:短按 / 长按 / 释放
* @author cyWu <1917507415@qq.com>
* @date 2026.08.25
* @version V1.0.0
* @history
* - V1.0.0, 2026.08.25, cyWu, 从天誉 MJ-1.0 充电板移植到杰理 BR28
******************************************************************************/
#ifndef __KEY_H__
#define __KEY_H__
#include <stdint.h>
#include <string.h>
/*============================================================================*/
/* 宏定义 */
/*============================================================================*/
#define KEY_TIMER_MS 1
#define KEY_MAX_NUMBER 12
#define DEBOUNCE_TIME 30
#define KEY_INTERVAL 30
#define CLICK 1
#define DbLCLICK 2
typedef enum {
USR_KEY_USER = 0,
USR_KEY_COUNT
} keyList;
typedef enum {
Bit_RESET = 0,
Bit_SET
} BitAction_t;
typedef enum {
KEY_ST_NULL = 0,
KEY_ST_RELEASE,
KEY_ST_SURE,
KEY_ST_UP,
KEY_ST_DOWN,
KEY_ST_LONG
} keyStatus_t;
typedef enum {
NULL_Event = 0,
DOWN_Event,
SHORT_Event,
LONG_Event,
LAST_Event,
DBCL_Event,
RELEASE_Event
} keyEvent_t;
typedef enum {
KEY_DISABLE = 0,
KEY_ENABLE = !KEY_DISABLE
} keyEnable_t;
typedef struct {
uint8_t keyShortFlag;
uint8_t keyLongFlag;
uint8_t keyInterval;
uint8_t keyFrequency;
uint16_t keyLongTime;
uint16_t keyLastTime;
uint32_t keyCount;
keyEnable_t keyShield;
BitAction_t keyLevel;
BitAction_t keyDownLevel;
keyStatus_t keyStatus;
keyEvent_t eventType;
uint8_t (*keyReadValue)(void);
} keyFSM_t;
typedef struct {
void (*nullPressCb)(void);
void (*releasePressCb)(void);
void (*downPressCb)(void);
void (*ShortPressCb)(void);
void (*longPressCb)(void);
void (*lastPressCb)(void);
void (*dbclPressCb)(void);
} keyFunc_t;
typedef struct {
keyFSM_t fsm;
keyFunc_t func;
} keyCategory_t;
void keyParaInit(keyCategory_t *keys);
void setKeyEventParams(uint8_t key_event, keyCategory_t key_param);
void keyCheckProcess(void);
void keyHandle(void);
void reset_key_Status(keyList key_index);
#endif /* __KEY_H__ */
+147
View File
@@ -0,0 +1,147 @@
/******************************************************************************
* @file key_manager.c
* @brief CBC2 按键:读 PB1,短按/长按交给 usr_jb_main
* @author cyWu <1917507415@qq.com>
* @date 2026.08.25
* @version V1.0.0
* @history
* - V1.0.0, 2026.08.25, cyWu, 从天誉 MJ-1.0 充电板移植到杰理 BR28
******************************************************************************/
#include "system/includes.h"
#include "key_manager.h"
#include "bsp_hw.h"
#include "board_pin.h"
#define LOG_TAG_CONST APP
#define LOG_TAG "[KEY]"
#define LOG_INFO_ENABLE
#include "debug.h"
/** 模块状态,替代原来 2 个散落的 static */
typedef struct {
uint8_t initialized;
uint8_t boot_key_lock; /**< 1=开机判定时键还按着,等松开才认短按/长按 */
} KeyManagerCtx_t;
static KeyManagerCtx_t s_km;
/**
* @brief 读按键:1=按下
* @return 1=按下,0=松开
*/
static uint8_t key_user_read(void)
{
return bsp_key_is_pressed();
}
/**
* @brief 松开:清掉开机按着键的锁
* @return 无
*/
static void key_user_release(void)
{
if (s_km.boot_key_lock) {
s_km.boot_key_lock = 0;
log_info("boot key released\n");
}
}
/**
* @brief 短按:开机未松手前忽略
* @return 无
*/
static void key_user_short(void)
{
if (s_km.boot_key_lock) {
s_km.boot_key_lock = 0;
log_info("boot key released\n");
return;
}
usr_on_key_short();
}
/**
* @brief 长按 3s:开机未松手前忽略,避免开机手还按着就关机
* @return 无
*/
static void key_user_long(void)
{
if (s_km.boot_key_lock) {
return;
}
usr_on_key_long();
}
static keyCategory_t s_keys[USR_KEY_COUNT] = {
[USR_KEY_USER] = {
.fsm.keyShield = KEY_ENABLE,
.fsm.keyDownLevel = Bit_SET,
.fsm.eventType = NULL_Event,
.fsm.keyLongTime = BOARD_KEY_POWER_MS,
.fsm.keyLastTime = (BOARD_KEY_POWER_MS + 7000), /* 大于长按即可,不使用持续按 */
.fsm.keyReadValue = key_user_read,
.func.ShortPressCb = key_user_short,
.func.longPressCb = key_user_long,
.func.releasePressCb = key_user_release,
},
};
/**
* @brief 装载按键表
* @return 无
*/
void user_key_init(void)
{
if (s_km.initialized) {
return;
}
keyParaInit(s_keys);
s_km.boot_key_lock = 0;
s_km.initialized = 1;
}
/**
* @brief 开机判定结束后:键还按着则锁住,松开才认短按/长按
* @return 无
*/
void user_key_lock_if_held(void)
{
reset_key_Status(USR_KEY_USER);
if (bsp_key_is_pressed()) {
s_km.boot_key_lock = 1;
log_info("boot key held, wait release\n");
}
}
/**
* @brief 按真实毫秒推进状态机
* @param dt 距上次调用的毫秒数
* @return 无
*/
void user_key_scan(uint16_t dt)
{
uint16_t i;
if (!s_km.initialized) {
return;
}
if (dt == 0) {
dt = 1;
}
for (i = 0; i < dt; i++) {
keyCheckProcess();
}
}
/**
* @brief 派发按键回调
* @return 无
*/
void user_key_handle(void)
{
if (!s_km.initialized) {
return;
}
keyHandle();
}
+53
View File
@@ -0,0 +1,53 @@
/******************************************************************************
* @file key_manager.h
* @brief CBC2 单按键:短按切模式,长按 3s 关机;开机 3s/5s 不走本库
* @author cyWu <1917507415@qq.com>
* @date 2026.08.25
* @version V1.0.0
* @history
* - V1.0.0, 2026.08.25, cyWu, 从天誉 MJ-1.0 充电板移植
******************************************************************************/
#ifndef __KEY_MANAGER_H__
#define __KEY_MANAGER_H__
#include "key.h"
/**
* @brief 业务层短按回调(app_function 实现)
* @return 无
*/
void usr_on_key_short(void);
/**
* @brief 业务层长按 3s 回调(app_function 实现)
* @return 无
*/
void usr_on_key_long(void);
/**
* @brief 装载按键表
* @return 无
*/
void user_key_init(void);
/**
* @brief 开机判定结束后:若键还按着则锁住,松开才认短按/长按
* @return 无
*/
void user_key_lock_if_held(void);
/**
* @brief 按真实毫秒推进状态机(空闲 20ms 节拍会循环 dt 次)
* @param dt 距上次调用的毫秒数
* @return 无
*/
void user_key_scan(uint16_t dt);
/**
* @brief 派发按键回调
* @return 无
*/
void user_key_handle(void);
#endif /* __KEY_MANAGER_H__ */
+328
View File
@@ -0,0 +1,328 @@
/******************************************************************************
* @file led.c
* @brief LED 事件框架实现:只改 ledMask 对应通道,红绿互不抢
* @author cyWu <1917507415@qq.com>
* @date 2026.08.25
* @version V1.0.0
* @history
* - V1.0.0, 2026.08.25, cyWu, 从天誉 MJ-1.0 充电板移植到杰理 BR28
******************************************************************************/
#include "led.h"
#include <stddef.h>
static LED_EventTableItem_t s_led_priority_table[PRIORITY_MAX];
static LED_EventTableItem_t s_led_event_table[LED_EVENT_MAX];
static LED_SetState_t s_led_set_state;
static TIME_Get_t s_time_get;
static uint32_t get_tick_diff(uint32_t meiosis);
static uint8_t led_lighting_update(LED_Config_t *led);
#if (BLINK_ENABLE)
static uint8_t led_blinking_update(LED_Config_t *led);
#endif
#if (ALTERNATING_BLINK_ENABLE)
static uint8_t led_alternating_update(LED_Config_t *led);
#endif
/**
* @brief 注册写灯回调,只对掩码里的通道动手
* @param setState 回调,led=通道掩码,state=LED_ON/OFF
* @return 无
*/
void LED_SetStateInit(LED_SetState_t setState)
{
s_led_set_state = setState;
}
/**
* @brief 注册毫秒时间戳回调
* @param getTime 回调
* @return 无
*/
void SET_TimeGetInit(TIME_Get_t getTime)
{
s_time_get = getTime;
}
/**
* @brief 拷贝产品事件表
* @param ledEvent 长度必须为 LED_EVENT_MAX
* @return 无
*/
void LED_EventTableInit(LED_EventTableItem_t ledEvent[])
{
if (ledEvent == NULL) {
return;
}
memset(s_led_priority_table, 0, sizeof(s_led_priority_table));
memset(s_led_event_table, 0, sizeof(s_led_event_table));
memcpy(s_led_event_table, ledEvent, sizeof(s_led_event_table));
}
/**
* @brief 把事件挂到对应优先级槽;同优先级会被覆盖
* @param ledEvent 事件号
* @return 无
*/
void LED_EventAdd(LED_Event_t ledEvent)
{
size_t i;
LED_EventTableItem_t *item;
LED_EventPriority_t prio;
if ((s_time_get == NULL) || (s_led_set_state == NULL)) {
return;
}
if (ledEvent >= LED_EVENT_MAX) {
return;
}
for (i = 0; i < LED_EVENT_MAX; i++) {
item = &s_led_event_table[i];
if (item->event != ledEvent) {
continue;
}
prio = item->priority;
if (prio >= PRIORITY_MAX) {
return;
}
if (s_led_priority_table[prio].event != ledEvent) {
item->config.currentTime = s_time_get();
if (s_led_priority_table[prio].event != LED_EVENT_NONE) {
/* 同优先级换事件:只关掉旧灯里新事件用不到的通道 */
s_led_set_state((uint16_t)(s_led_priority_table[prio].config.ledMask
^ item->config.ledMask),
LED_OFF);
}
}
s_led_priority_table[prio] = *item;
return;
}
}
/**
* @brief 清空优先级表(不改 GPIO,由调用方灭灯)
* @return 无
*/
void LED_EventAllDelete(void)
{
memset(s_led_priority_table, 0, sizeof(s_led_priority_table));
}
/**
* @brief 删除指定事件并关掉它占用的通道
* @param ledEvent 事件号
* @return 无
*/
void LED_EventDelete(LED_Event_t ledEvent)
{
size_t i;
for (i = 0; i < PRIORITY_MAX; i++) {
if (s_led_priority_table[i].event != ledEvent) {
continue;
}
memset(&s_led_priority_table[i], 0, sizeof(LED_EventTableItem_t));
if ((s_led_set_state != NULL) && (ledEvent < LED_EVENT_MAX)) {
s_led_set_state(s_led_event_table[ledEvent].config.ledMask, LED_OFF);
}
return;
}
}
/**
* @brief 从高优先级往下跑事件
* @note handler 返回 1 会拦住更低优先级。红绿要共存时 handler 必须返回 0
* @return 无
*/
void LED_EventHandle(void)
{
int i;
uint8_t ret;
for (i = (int)PRIORITY_MAX - 1; i >= 0; i--) {
if (s_led_priority_table[i].event == LED_EVENT_NONE) {
continue;
}
if (s_led_priority_table[i].ledEventHandler == NULL) {
continue;
}
ret = s_led_priority_table[i].ledEventHandler(&s_led_priority_table[i].config);
if (ret) {
return;
}
}
}
/**
* @brief 是否有事件在跑
* @return 1=有,0=无
*/
uint8_t LED_IsEventActive(void)
{
size_t i;
for (i = 0; i < PRIORITY_MAX; i++) {
if (s_led_priority_table[i].event != LED_EVENT_NONE) {
return 1;
}
}
return 0;
}
/**
* @brief 指定事件是否挂在优先级表里
* @param ledEvent 事件号
* @return 1=在,0=不在
*/
uint8_t LED_IsEventExist(LED_Event_t ledEvent)
{
size_t i;
for (i = 0; i < PRIORITY_MAX; i++) {
if (s_led_priority_table[i].event == ledEvent) {
return 1;
}
}
return 0;
}
/**
* @brief 按配置刷新一次灯效
* @param led 配置,不可为空
* @return 1=还在跑,0=次数用完
*/
uint8_t LED_Update(LED_Config_t *led)
{
if (led == NULL) {
return 0;
}
switch (led->state) {
case LED_LIGHT:
return led_lighting_update(led);
#if (BLINK_ENABLE)
case LED_BLINKING:
return led_blinking_update(led);
#endif
#if (ALTERNATING_BLINK_ENABLE)
case LED_ALTERNATING_BLINK:
return led_alternating_update(led);
#endif
default:
return 0;
}
}
/**
* @brief 毫秒时间差,兼容溢出
* @param meiosis 起点时间戳
* @return 差值 ms
*/
static uint32_t get_tick_diff(uint32_t meiosis)
{
uint32_t now;
if (s_time_get == NULL) {
return 0;
}
now = s_time_get();
if (now >= meiosis) {
return now - meiosis;
}
return (0xFFFFFFFFu - meiosis) + now;
}
/**
* @brief 常亮;runNum 按秒计,0=一直亮
* @param led 配置
* @return 1=还在亮,0=到点灭
*/
static uint8_t led_lighting_update(LED_Config_t *led)
{
uint16_t sec_count;
if (s_led_set_state == NULL) {
return 0;
}
sec_count = (uint16_t)(get_tick_diff(led->currentTime) / 1000u);
if ((led->runNum > 0) && (sec_count >= led->runNum)) {
s_led_set_state(led->ledMask, LED_OFF);
return 0;
}
s_led_set_state(led->ledMask, LED_ON);
return 1;
}
#if (BLINK_ENABLE)
/**
* @brief 闪烁;runNum 按完整周期计,0=一直闪
* @param led 配置
* @return 1=还在闪,0=到点灭
*/
static uint8_t led_blinking_update(LED_Config_t *led)
{
uint32_t run_cycle;
uint16_t blink_count;
uint32_t sec_left;
if (s_led_set_state == NULL) {
return 0;
}
run_cycle = led->blinkParams.onTime + led->blinkParams.offTime;
if (run_cycle == 0) {
return 0;
}
blink_count = (uint16_t)(get_tick_diff(led->currentTime) / run_cycle);
sec_left = get_tick_diff(led->currentTime) % run_cycle;
if ((led->runNum > 0) && (blink_count >= led->runNum)) {
s_led_set_state(led->ledMask, LED_OFF);
return 0;
}
if (sec_left <= led->blinkParams.onTime) {
s_led_set_state(led->ledMask, led->blinkParams.orDer ? LED_OFF : LED_ON);
} else {
s_led_set_state(led->ledMask, led->blinkParams.orDer ? LED_ON : LED_OFF);
}
return 1;
}
#endif
#if (ALTERNATING_BLINK_ENABLE)
/**
* @brief 两组灯按时间片轮流亮
* @param led 配置
* @return 1=还在跑,0=到点灭
*/
static uint8_t led_alternating_update(LED_Config_t *led)
{
uint16_t blink_count;
uint32_t sec_left;
uint32_t time_slice;
size_t i;
if (s_led_set_state == NULL) {
return 0;
}
if (led->alternatingBlinkParams.Cycle == 0) {
return 0;
}
blink_count = (uint16_t)(get_tick_diff(led->currentTime)
/ led->alternatingBlinkParams.Cycle);
sec_left = get_tick_diff(led->currentTime) % led->alternatingBlinkParams.Cycle;
if ((led->runNum > 0) && (blink_count >= led->runNum)) {
s_led_set_state(led->ledMask, LED_OFF);
return 0;
}
time_slice = led->alternatingBlinkParams.Cycle / ALTERNAT_GROUP;
s_led_set_state(led->ledMask, LED_OFF);
for (i = 0; i < ALTERNAT_GROUP; i++) {
if ((sec_left > (time_slice * i)) && (sec_left <= (time_slice * (i + 1)))) {
s_led_set_state(led->alternatingBlinkParams.Group[i], LED_ON);
break;
}
}
return 1;
}
#endif
+122
View File
@@ -0,0 +1,122 @@
/******************************************************************************
* @file led.h
* @brief LED 事件框架:按通道掩码独立开关,红绿可同时亮
* @author cyWu <1917507415@qq.com>
* @date 2026.08.25
* @version V1.0.0
* @history
* - V1.0.0, 2026.08.25, cyWu, 从天誉 MJ-1.0 充电板移植到杰理 BR28
******************************************************************************/
#ifndef __LED_H__
#define __LED_H__
#include <stdint.h>
#include <string.h>
/*============================================================================*/
/* 宏定义 */
/*============================================================================*/
#define BLINK_ENABLE 1
#define ALTERNATING_BLINK_ENABLE 1
#define BREATH_ENABLE 0
#define RUNNING_ENABLE 0
#define MARQUEE_ENABLE 0
#define LED_ON 1
#define LED_OFF 0
#define LED_CHANNEL(user_led) (1u << (user_led))
#define ledPrintf(level, fmt, ...) ((void)0)
/*============================================================================*/
/* 类型 */
/*============================================================================*/
typedef enum {
LED_LIGHT = 0, /**< 常亮 */
LED_BLINKING, /**< 闪烁 */
LED_ALTERNATING_BLINK /**< 红绿交替 */
} LED_State_t;
typedef enum {
LED_EVENT_NONE = 0,
LED_EVENT_STANDBY, /**< 待机绿灯常亮 */
LED_EVENT_CHARGE, /**< 充电红灯常亮 */
LED_EVENT_CHARGE_FULL, /**< 充满绿灯常亮 */
LED_EVENT_LOWBAT, /**< 低电红闪 */
LED_EVENT_PAIRING, /**< 配对绿快闪 */
LED_EVENT_MODE, /**< 切模式绿闪两下 */
LED_EVENT_PAIR_OK, /**< 配对成功常绿若干秒 */
LED_EVENT_PAIR_FAIL, /**< 配对失败红绿交替 */
LED_EVENT_MAX
} LED_Event_t;
/**
* 同优先级会互相覆盖;红绿要共存必须放在不同优先级,且 handler 返回 0
*/
typedef enum {
PRIORITY_1 = 0, /**< 待机绿灯 */
PRIORITY_2, /**< 低电红闪 */
PRIORITY_3, /**< 充电红 / 充满绿 */
PRIORITY_4, /**< 配对 / 切模式 / 配对结果 */
PRIORITY_MAX
} LED_EventPriority_t;
#if (BLINK_ENABLE)
typedef struct {
uint8_t orDer; /**< 1=先灭后亮,0=先亮后灭 */
uint32_t onTime; /**< 亮时间 ms */
uint32_t offTime; /**< 灭时间 ms */
} LED_BlinkParams_t;
#endif
#if (ALTERNATING_BLINK_ENABLE)
#define ALTERNAT_GROUP 2
typedef struct {
uint16_t Group[ALTERNAT_GROUP]; /**< 交替两组的通道掩码 */
uint32_t Cycle; /**< 一整轮交替周期 ms */
} LED_AlternatingBlinkParams_t;
#endif
typedef struct {
LED_State_t state;
uint16_t ledMask; /**< 本事件只动这些通道,其它灯不受影响 */
uint16_t runNum; /**< 0=无限;常亮按秒计,闪烁按周期计 */
uint32_t currentTime;
#if (BLINK_ENABLE)
LED_BlinkParams_t blinkParams;
#endif
#if (ALTERNATING_BLINK_ENABLE)
LED_AlternatingBlinkParams_t alternatingBlinkParams;
#endif
} LED_Config_t;
typedef struct {
LED_Event_t event;
LED_EventPriority_t priority;
LED_Config_t config;
uint8_t (*ledEventHandler)(LED_Config_t *config);
} LED_EventTableItem_t;
typedef void (*LED_SetState_t)(uint16_t led, uint8_t state);
typedef uint32_t (*TIME_Get_t)(void);
/*============================================================================*/
/* 外部接口 */
/*============================================================================*/
void LED_SetStateInit(LED_SetState_t setState);
void SET_TimeGetInit(TIME_Get_t getTime);
void LED_EventTableInit(LED_EventTableItem_t ledEvent[]);
void LED_EventAdd(LED_Event_t ledEvent);
void LED_EventDelete(LED_Event_t ledEvent);
void LED_EventAllDelete(void);
void LED_EventHandle(void);
uint8_t LED_IsEventActive(void);
uint8_t LED_IsEventExist(LED_Event_t ledEvent);
uint8_t LED_Update(LED_Config_t *led);
#endif /* __LED_H__ */
+226
View File
@@ -0,0 +1,226 @@
/******************************************************************************
* @file led_manager.c
* @brief CBC2 灯事件:充电红与待机绿走不同通道,可同时亮
* @author cyWu <1917507415@qq.com>
* @date 2026.08.25
* @version V1.0.0
* @history
* - V1.0.0, 2026.08.25, cyWu, 从天誉 MJ-1.0 充电板移植到杰理 BR28
******************************************************************************/
#include "led_manager.h"
#include "system/includes.h"
#include "bsp_hw.h"
#include "board_pin.h"
static uint8_t led_keep_handler(LED_Config_t *led_config);
static uint8_t led_standby_handler(LED_Config_t *led_config);
static uint8_t led_pairing_handler(LED_Config_t *led_config);
static uint8_t led_mode_handler(LED_Config_t *led_config);
static uint8_t led_pair_ok_handler(LED_Config_t *led_config);
static uint8_t led_pair_fail_handler(LED_Config_t *led_config);
/**
* @brief 只改掩码里的通道,其它灯保持原样
* @param led 通道掩码
* @param state LED_ON / LED_OFF
* @return 无
*/
static void set_led_multi(uint16_t led, uint8_t state)
{
if (led & LED_CHANNEL(LED_CH_RED)) {
bsp_led_red_set(state);
}
if (led & LED_CHANNEL(LED_CH_GREEN)) {
bsp_led_green_set(state);
}
}
/**
* @brief 常亮/闪烁一直跑:返回 0,让其它通道的事件继续执行
* @param led_config 配置
* @return 0
*/
static uint8_t led_keep_handler(LED_Config_t *led_config)
{
(void)LED_Update(led_config);
return 0;
}
/**
* @brief 待机绿灯;BOARD_LED_STANDBY_ON=0 时亮满 runNum 秒后删事件
* @param led_config 配置
* @return 0
*/
static uint8_t led_standby_handler(LED_Config_t *led_config)
{
if (LED_Update(led_config) == 0) {
LED_EventDelete(LED_EVENT_STANDBY);
}
return 0;
}
/**
* @brief 配对绿快闪(无限)
* @param led_config 配置
* @return 0
*/
static uint8_t led_pairing_handler(LED_Config_t *led_config)
{
(void)LED_Update(led_config);
return 0;
}
/**
* @brief 切模式闪完两下后删事件
* @param led_config 配置
* @return 0
*/
static uint8_t led_mode_handler(LED_Config_t *led_config)
{
if (LED_Update(led_config) == 0) {
LED_EventDelete(LED_EVENT_MODE);
}
return 0;
}
/**
* @brief 配对成功常绿若干秒后删事件
* @param led_config 配置
* @return 0
*/
static uint8_t led_pair_ok_handler(LED_Config_t *led_config)
{
if (LED_Update(led_config) == 0) {
LED_EventDelete(LED_EVENT_PAIR_OK);
}
return 0;
}
/**
* @brief 配对失败红绿交替完后删事件
* @param led_config 配置
* @return 0
*/
static uint8_t led_pair_fail_handler(LED_Config_t *led_config)
{
if (LED_Update(led_config) == 0) {
LED_EventDelete(LED_EVENT_PAIR_FAIL);
return 0;
}
/* 红绿都占用,拦住充电红,避免交替被冲掉 */
return 1;
}
/**
* @brief 注册回调并装载 CBC2 事件表(GPIO 已在 bsp_hw_init
* @return 无
*/
void user_led_init(void)
{
static LED_EventTableItem_t led_event_category[LED_EVENT_MAX] = {
[LED_EVENT_STANDBY] = {
/* 待机绿灯常亮,可与充电红共存 */
.event = LED_EVENT_STANDBY,
.priority = PRIORITY_1,
.config.ledMask = LED_CHANNEL(LED_CH_GREEN),
.config.state = LED_LIGHT,
#if BOARD_LED_STANDBY_ON
.config.runNum = 0, /* 0=一直亮 */
#else
.config.runNum = (uint16_t)((BOARD_LED_POWER_ON_MS + 999u) / 1000u),
#endif
.ledEventHandler = led_standby_handler,
},
[LED_EVENT_LOWBAT] = {
/* 低电红闪 */
.event = LED_EVENT_LOWBAT,
.priority = PRIORITY_2,
.config.ledMask = LED_CHANNEL(LED_CH_RED),
.config.state = LED_BLINKING,
.config.blinkParams.orDer = 0,
.config.blinkParams.onTime = BOARD_LED_LOWBAT_ON_MS,
.config.blinkParams.offTime = (BOARD_LED_LOWBAT_PERIOD_MS - BOARD_LED_LOWBAT_ON_MS),
.config.runNum = 0,
.ledEventHandler = led_keep_handler,
},
[LED_EVENT_CHARGE] = {
/* 充电红常亮,与待机绿不同通道 */
.event = LED_EVENT_CHARGE,
.priority = PRIORITY_2,
.config.ledMask = LED_CHANNEL(LED_CH_RED),
.config.state = LED_LIGHT,
.config.runNum = 0,
.ledEventHandler = led_keep_handler,
},
[LED_EVENT_CHARGE_FULL] = {
/* 充满绿常亮(与充电红同优先级,互斥覆盖) */
.event = LED_EVENT_CHARGE_FULL,
.priority = PRIORITY_2,
.config.ledMask = LED_CHANNEL(LED_CH_GREEN),
.config.state = LED_LIGHT,
.config.runNum = 0,
.ledEventHandler = led_keep_handler,
},
[LED_EVENT_PAIRING] = {
/* 配对绿快闪 */
.event = LED_EVENT_PAIRING,
.priority = PRIORITY_3,
.config.ledMask = LED_CHANNEL(LED_CH_GREEN),
.config.state = LED_BLINKING,
.config.blinkParams.orDer = 0,
.config.blinkParams.onTime = BOARD_LED_FAST_ON_MS,
.config.blinkParams.offTime = BOARD_LED_FAST_OFF_MS,
.config.runNum = 0,
.ledEventHandler = led_pairing_handler,
},
[LED_EVENT_MODE] = {
/* 切模式绿闪两下 */
.event = LED_EVENT_MODE,
.priority = PRIORITY_4,
.config.ledMask = LED_CHANNEL(LED_CH_GREEN),
.config.state = LED_BLINKING,
.config.blinkParams.orDer = 1,
.config.blinkParams.onTime = BOARD_LED_MODE_ON_MS,
.config.blinkParams.offTime = BOARD_LED_MODE_OFF_MS,
.config.runNum = BOARD_LED_MODE_BLINK_N,
.ledEventHandler = led_mode_handler,
},
[LED_EVENT_PAIR_OK] = {
/* 配对成功常绿若干秒 */
.event = LED_EVENT_PAIR_OK,
.priority = PRIORITY_3,
.config.ledMask = LED_CHANNEL(LED_CH_GREEN),
.config.state = LED_LIGHT,
.config.runNum = (uint16_t)((BOARD_LED_PAIR_OK_MS + 999u) / 1000u),
.ledEventHandler = led_pair_ok_handler,
},
[LED_EVENT_PAIR_FAIL] = {
/* 配对失败红绿交替 */
.event = LED_EVENT_PAIR_FAIL,
.priority = PRIORITY_3,
.config.ledMask = (uint16_t)(LED_CHANNEL(LED_CH_RED) | LED_CHANNEL(LED_CH_GREEN)),
.config.state = LED_ALTERNATING_BLINK,
.config.alternatingBlinkParams.Group[0] = LED_CHANNEL(LED_CH_RED),
.config.alternatingBlinkParams.Group[1] = LED_CHANNEL(LED_CH_GREEN),
.config.alternatingBlinkParams.Cycle = (uint32_t)(BOARD_LED_ALT_MS * 2u),
.config.runNum = (uint16_t)((BOARD_LED_PAIR_FAIL_MS + (BOARD_LED_ALT_MS * 2u) - 1u)
/ (BOARD_LED_ALT_MS * 2u)),
.ledEventHandler = led_pair_fail_handler,
},
};
LED_SetStateInit(set_led_multi);
SET_TimeGetInit((TIME_Get_t)timer_get_ms);
LED_EventTableInit(led_event_category);
bsp_led_all_off();
}
/**
* @brief 跑一轮 LED 事件
* @return 无
*/
void user_led_handle(void)
{
LED_EventHandle();
}
+34
View File
@@ -0,0 +1,34 @@
/******************************************************************************
* @file led_manager.h
* @brief CBC2 红绿 LED 通道与事件表
* @author cyWu <1917507415@qq.com>
* @date 2026.08.25
* @version V1.0.0
* @history
* - V1.0.0, 2026.08.25, cyWu, 从天誉 MJ-1.0 充电板移植
******************************************************************************/
#ifndef __LED_MANAGER_H__
#define __LED_MANAGER_H__
#include "led.h"
typedef enum {
LED_CH_RED = 0,
LED_CH_GREEN,
LED_CH_MAX
} LedChannel_t;
/**
* @brief 注册 GPIO 回调并装载 CBC2 事件表
* @return 无
*/
void user_led_init(void);
/**
* @brief 跑一轮 LED 事件
* @return 无
*/
void user_led_handle(void);
#endif /* __LED_MANAGER_H__ */
+115 -760
View File
@@ -1,18 +1,27 @@
/******************************************************************************
* @file usr_jb_main.c
* @brief CBC2 业务主循环:按键 / 充电 / 电量 / 灯 / 6 档玩法 / 手动点动
* @brief CBC2 业务组合根:只做“先后调用谁、谁通知谁”,不含具体业务算法
* @author cyWu <1917507415@qq.com>
* @date 2026.08.25
* @version V1.4.0
* @version V2.0.0
* @history
* - V1.0.0, 2026.08.21, cyWu, 首次发布
* - V1.1.0, 2026.08.24, cyWu, 收口未接线模块;DP2 仅第 1 字节点动 1s
* - V1.2.0, 2026.08.24, cyWu, 按键:深睡 3s 开机、5s 开机+配对;开机中 3s 深睡
* - V1.3.0, 2026.08.24, cyWu, DP200=空闲,01/02 转,03 停
* - V1.4.0, 2026.08.25, cyWu, 空闲改可休眠定时器,系统才能进 sleep
* - V1.5.0, 2026.08.25, cyWu, LED/按键改充电板事件库,红绿可同时亮
* - V2.0.0, 2026.08.25, cyWu, 重构:电源/配对/开机/功能拆成独立模块,
* 本文件只剩初始化顺序、1ms 调度、模块间接线
*
* @note 协议生成文件 jb_product.c 不要填业务。APP 下行写 gDevData。
* usr_timer_loop 由 BLE 回调,勿再注册第二份。
* 具体业务逻辑请去对应模块找:
* app_power —— USB/充电/电量/深睡请求
* app_pair —— 配对会话计时
* app_boot —— 开机 3s/5s 按键判定
* app_function —— DP0/DP1/DP2 与按键动作
* app_led —— 灯效
******************************************************************************/
#include "system/includes.h"
@@ -28,18 +37,21 @@
#include "usr_le_product.h"
#include "usr_le_api.h"
#include "bsp_hw.h"
#include "app_led.h"
#include "app_power.h"
#include "app_pair.h"
#include "app_boot.h"
#include "app_function.h"
#include "app_mode.h"
#include "ui_manage.h"
#include "asm/power/power_api.h"
#include "asm/power/power_reset.h"
#include <string.h>
#include "app_led.h"
#include "key_manager.h"
#define LOG_TAG_CONST APP
#define LOG_TAG "[USR_JB]"
#define LOG_INFO_ENABLE
#include "debug.h"
extern void sys_enter_soft_poweroff(void *priv);
/* 向 usr_le_code 提供产品身份(换产品只改 jb_protocol.h 宏即可) */
const uint8_t g_usr_le_prodcode[USR_LE_PRODCODE_LEN] = {
JB_CATEGORY_CODE[0], JB_CATEGORY_CODE[1],
@@ -50,770 +62,66 @@ const uint8_t g_usr_le_pcode[USR_LE_PCODE_LEN] = {
JB_PRODUCT_CODE[3], JB_PRODUCT_CODE[4], JB_PRODUCT_CODE[5]
};
#define USR_TIMER_IDLE_MS 20 /**< 空闲节拍:可 sleep;按键 20ms 消抖对齐 */
/** 调度器状态,替代原来 2 个散落的 static */
typedef struct {
int timer_id;
uint8_t fast; /**< 1=1ms 高优(禁睡),0=空闲可休眠 */
} UsrSchedulerCtx_t;
static UsrSchedulerCtx_t s_sched;
static void usr_timer_1_ms(void *priv);
static void usr_timer_rearm(uint8_t fast);
void usr_timer_loop(void);
extern void clr_wdt(void);
extern u32 timer_get_ms(void);
static int jb_timer_id;
static uint8_t s_timer_fast; /**< 1=1ms 高优(禁睡),0=空闲可休眠 */
static uint8_t s_pairing; /**< 1=深睡 5s 进入配对中 */
static uint8_t s_play_mode; /**< 当前自动模式 0~6 */
static uint8_t s_key_raw;
static uint8_t s_key_cnt;
static uint8_t s_key_pressed;
static uint8_t s_boot_key_lock; /**< 上电按着键,松开前不当短按 */
static uint16_t s_key_hold_ms;
static uint16_t s_key_led_override_ms; /**< 切模式闪灯期间不显示配对快闪 */
static uint8_t s_req_poweroff;
static uint8_t s_usb_online;
static uint8_t s_charge_led; /**< 0=无 1=充电红 2=充满绿 */
static uint8_t s_charge_led_last;
static uint16_t s_full_ms;
static uint8_t s_app_power; /**< DP00=软关,电机/玩法停,BLE 仍在 */
static uint16_t s_bat_tick;
static uint16_t s_bat_acc;
static uint8_t s_bat_n;
static uint16_t s_bat_mv;
static uint8_t s_bat_pct;
static uint8_t s_low_bat;
static uint32_t s_low_ms;
static uint8_t s_empty_win;
static uint8_t s_low_enter_win;
static uint8_t s_low_exit_win;
static uint16_t s_bat_boot_ms;
static uint16_t s_bat_log_ms;
static uint8_t s_hand_busy; /**< 手动点动进行中 */
static uint8_t s_hand_cmd_last; /**< 边沿:同一 1/2 不重复启动 */
static uint16_t s_hand_ms;
static volatile uint8_t s_hand_write_pending; /**< DP2 刚写入(03 停转要靠事件) */
static volatile uint8_t s_hand_write_cmd;
static uint32_t s_pair_ms; /**< 配对已持续 ms,满 60s 判失败 */
/**
* @brief 毫秒累加,饱和到 0xFFFF
* @param v 当前值
* @param dt 增加量
* @return 累加结果
*/
static uint16_t usr_add_ms(uint16_t v, uint16_t dt)
{
uint32_t n = (uint32_t)v + dt;
return (n > 0xffffu) ? 0xffffu : (uint16_t)n;
}
#define BAT_SAMPLE_MS 100
#define BAT_AVG_N 8
#define BAT_BOOT_MS 3000 /* 上电 3s 内不因低电深睡 */
#define BAT_LOG_MS 10000
#define USR_TIMER_IDLE_MS 20 /* 空闲节拍:可 sleep;按键 20ms 消抖对齐 */
#define BAT_LOW_WIN 3 /* 连续约 2.4s 才进低电告警 */
#define BAT_EMPTY_WIN 5 /* 连续约 4s 才 3.2V 深睡 */
/**
* @brief 忙等若干毫秒,并喂狗
* @param ms 等待时长
* @brief USB 插拔的联动:插入即停电机,拔出即软关并刷新系统电源图标
* @param evt app_power_tick_1ms 上报的边沿事件
* @return 无
*/
static void usr_boot_delay_ms(uint32_t ms)
static void usr_on_power_evt(AppPowerEvt_t evt)
{
uint32_t t0 = timer_get_ms();
while ((timer_get_ms() - t0) < ms) {
clr_wdt();
switch (evt) {
case APP_POWER_EVT_USB_IN:
app_function_stop_all();
log_info("usb in, motor stop\n");
break;
case APP_POWER_EVT_USB_OUT:
app_function_soft_off();
break;
default:
break;
}
}
/**
* @brief BLE 起来前进深睡(1ms 定时器尚未启动
* @brief 深睡请求钩子:立即停电机、灯全灭(真正掉电在 50ms 后
* @return 无
*/
static void usr_boot_sleep(void)
static void usr_poweroff_request_hook(void)
{
log_info("boot sleep\n");
bsp_hw_enter_sleep_io();
power_set_soft_poweroff();
while (1) {
clr_wdt();
}
app_function_stop_all();
app_led_clear();
}
/**
* @brief 上电是否跳过 3s/5s 按键判定
* @return 1=USB / 软复位 / LVD / 看门狗,保持开机且不进配对
*/
static uint8_t usr_boot_skip_key_wait(void)
{
uint8_t i;
for (i = 0; i < 10; i++) {
if (bsp_chg_is_low() || bsp_vpwr_is_online()) {
log_info("boot usb, skip key wait\n");
return 1;
}
usr_boot_delay_ms(5);
}
if (cpu_reset_by_soft()
|| is_reset_source(MSYS_SOFT_RST)
|| is_reset_source(P33_SOFT_RST)
|| is_reset_source(P33_VDDIO_LVD_RST)
|| is_reset_source(P11_WDT_RST)) {
log_info("boot reset-keep, skip key wait\n");
return 1;
}
return 0;
}
/**
* @brief 深睡唤醒/上电:长按 3s 开机,长按 5s 开机并配对;不足 3s 松手再睡
* @note 必须在 BLE 起来之前调用。配对只置 usr_goto_pair,不调用 usr_goto_pair_mode
* @brief 深睡请求钩子:50ms 到点,真正进入深睡(不会返回)
* @return 无
*/
static void usr_boot_key_wait(void)
static void usr_poweroff_finalize_hook(void)
{
uint32_t t0;
uint32_t elapsed;
uint8_t green_on = 0;
usr_var.usr_goto_pair = 0;
s_pairing = 0;
s_pair_ms = 0;
if (usr_boot_skip_key_wait()) {
return;
}
if (!bsp_key_is_pressed()) {
log_info("boot no key, sleep\n");
usr_boot_sleep();
}
t0 = timer_get_ms();
bsp_led_all_off();
log_info("boot key hold, wait 3s on / 5s pair\n");
while (bsp_key_is_pressed()) {
elapsed = timer_get_ms() - t0;
clr_wdt();
if (elapsed >= BOARD_KEY_PAIR_MS) {
usr_var.usr_goto_pair = 1;
usr_clear_pair_info_noreset();
s_pairing = 1;
s_pair_ms = 0;
log_info("boot hold %ums, power on + pair\n", (unsigned)elapsed);
return;
}
if (!green_on && (elapsed >= BOARD_KEY_POWER_MS)) {
green_on = 1;
bsp_led_green_set(1);
log_info("boot hold 3s, will power on\n");
}
}
elapsed = timer_get_ms() - t0;
if (elapsed < BOARD_KEY_POWER_MS) {
log_info("boot hold %ums < 3s, sleep\n", (unsigned)elapsed);
usr_boot_sleep();
}
log_info("boot hold %ums, power on\n", (unsigned)elapsed);
}
/**
* @brief 停手动点动(走电机反转刹车)
* @return 无
*/
static void usr_hand_stop(void)
{
s_hand_busy = 0;
s_hand_ms = 0;
bsp_motor_set(BSP_MOTOR_STOP);
}
/**
* @brief 停手动 + 自动模式,并清 DP1
* @return 无
*/
static void usr_motor_all_stop(void)
{
s_play_mode = 0;
gDevData.play_mode = 0;
app_mode_stop();
usr_hand_stop();
}
/**
* @brief DP2 写入钩子(jb_product 薄适配调用,不在此驱电机)
* @param cmd 第 1 字节:0=空闲,1=顺时针,2=逆时针,3=停
* @return 无
*/
void usr_dp_hand_write(uint8_t cmd)
{
s_hand_write_cmd = cmd;
s_hand_write_pending = 1;
}
/**
* @brief 软关:电机停、DP0=0,等 APP/短按再开。不断 BLE
* @return 无
*/
static void usr_app_soft_off(void)
{
s_app_power = 0;
gDevData.power = 0;
s_play_mode = 0;
gDevData.play_mode = 0;
app_mode_stop();
usr_hand_stop();
log_info("soft off, wait APP power on\n");
}
/**
* @brief 切换自动模式 0~6
* @param mode 0=停转,1~6=对应玩法
* @return 无
*/
static void usr_play_mode_apply(uint8_t mode)
{
if (mode > 6) {
mode = 0;
}
if (s_usb_online || !s_app_power || s_req_poweroff || s_hand_busy || s_pairing) {
mode = 0;
}
s_play_mode = mode;
gDevData.play_mode = mode;
if (mode == 0) {
app_mode_stop();
} else {
app_mode_start(mode);
}
}
/**
* @brief 跟随 APP 下发的 DP1 自动模式
* @return 无
*/
static void usr_play_mode_poll(void)
{
uint8_t mode;
if (s_usb_online || !s_app_power || s_req_poweroff || s_hand_busy || s_pairing) {
return;
}
mode = gDevData.play_mode;
if (mode > 6) {
mode = 0;
gDevData.play_mode = 0;
}
if (mode == s_play_mode) {
return;
}
usr_play_mode_apply(mode);
log_info("APP play_mode=%d\n", mode);
}
/**
* @brief 跟随 APP 下发的 DP0 开关
* @return 无
*/
static void usr_app_power_poll(void)
{
if (s_usb_online || s_req_poweroff) {
return;
}
if (gDevData.power && !s_app_power) {
s_app_power = 1;
app_led_hint_power_on();
log_info("APP power on\n");
} else if (!gDevData.power && s_app_power) {
usr_app_soft_off();
}
}
/**
* @brief DP2 手动:只看第 1 字节。0=空闲,1=顺时针,2=逆时针,3=停
* @note 00 00 是 DP 默认值,不当停止。百分比/力度、DP3、DP4 预留不接。
* @return 无
*/
static void usr_hand_poll(void)
{
uint8_t cmd;
uint8_t got_write = s_hand_write_pending;
if (got_write) {
s_hand_write_pending = 0;
cmd = s_hand_write_cmd;
} else {
cmd = gDevData.hand_mode[0];
}
if (s_usb_online || !s_app_power || s_req_poweroff || s_pairing) {
if (s_hand_busy || (got_write && (cmd == 3))) {
usr_motor_all_stop();
}
s_hand_cmd_last = cmd;
return;
}
if (cmd == 0) {
s_hand_cmd_last = 0;
return;
}
if (cmd == 3) {
if (got_write || s_hand_busy || app_mode_get_active()) {
usr_motor_all_stop();
log_info("hand stop cmd=3\n");
}
s_hand_cmd_last = 3;
return;
}
if ((cmd == 1 || cmd == 2) && (got_write || (cmd != s_hand_cmd_last))) {
s_play_mode = 0;
gDevData.play_mode = 0;
app_mode_stop();
s_hand_busy = 1;
s_hand_ms = 0;
/* 1=顺时针收绳,2=逆时针放绳(BOARD_MOTOR_IN_INA_HIGH=1 */
bsp_motor_set((cmd == 1) ? BSP_MOTOR_IN : BSP_MOTOR_OUT);
log_info("hand cmd=%d %s until 03\n", cmd,
(cmd == 1) ? "cw" : "ccw");
}
s_hand_cmd_last = cmd;
}
/**
* @brief 手动运行时推进刹车;停转改由 DP2 写 03
* @return 无
*/
static void usr_hand_tick_1ms(uint16_t dt)
{
if (!s_hand_busy) {
return;
}
bsp_motor_brake_tick();
s_hand_ms = usr_add_ms(s_hand_ms, dt);
}
/**
* @brief 深睡入口(须在 1ms 高优定时器之外调用)
* @param priv 未使用
* @return 无
*/
static void usr_do_poweroff(void *priv)
{
(void)priv;
log_info("enter deep off\n");
app_mode_stop();
usr_hand_stop();
bsp_hw_enter_sleep_io();
sys_enter_soft_poweroff(NULL);
}
/**
* @brief 请求深睡:先停电机,50ms 后真正关机
* @return 无
*/
static void usr_key_request_poweroff(void)
{
if (s_req_poweroff) {
return;
}
s_req_poweroff = 1;
log_info("request deep off\n");
app_mode_stop();
usr_hand_stop();
bsp_led_all_off();
sys_timeout_add(NULL, usr_do_poweroff, 50);
}
/**
* @brief 电压换算电量:4.2V=100%3.2V=0%
* @param mv 电池电压,单位 mV
* @return 电量百分比,范围 0~100
*/
static uint8_t usr_bat_percent(uint16_t mv)
{
int32_t span = (int32_t)BOARD_VBAT_FULL_MV - (int32_t)BOARD_VBAT_EMPTY_MV;
int32_t pct;
if (mv <= BOARD_VBAT_EMPTY_MV) {
return 0;
}
if (mv >= BOARD_VBAT_FULL_MV) {
return 100;
}
pct = ((int32_t)mv - BOARD_VBAT_EMPTY_MV) * 100 / span;
if (pct < 0) {
pct = 0;
}
if (pct > 100) {
pct = 100;
}
return (uint8_t)pct;
}
/**
* @brief 电量采样与低电策略
* @note 电机运转时只记电压、不改百分比/告警,避免电流拉垮 VBAT 误报
* @return 无
*/
static void usr_bat_tick_1ms(uint16_t dt)
{
uint16_t mv;
uint8_t pct;
uint8_t motor_busy;
if (s_bat_boot_ms < BAT_BOOT_MS) {
s_bat_boot_ms = usr_add_ms(s_bat_boot_ms, dt);
}
s_bat_tick = usr_add_ms(s_bat_tick, dt);
if (s_bat_tick >= BAT_SAMPLE_MS) {
s_bat_tick = 0;
s_bat_acc += bsp_vbat_mv();
s_bat_n++;
if (s_bat_n >= BAT_AVG_N) {
mv = (uint16_t)(s_bat_acc / s_bat_n);
s_bat_acc = 0;
s_bat_n = 0;
s_bat_mv = mv;
motor_busy = (uint8_t)(app_mode_get_active() != 0 || s_hand_busy);
if (!motor_busy) {
pct = usr_bat_percent(mv);
if (pct != s_bat_pct) {
s_bat_pct = pct;
gDevData.battery = pct;
}
if (s_bat_boot_ms >= BAT_BOOT_MS) {
if (!s_usb_online && mv <= BOARD_VBAT_EMPTY_MV) {
if (s_empty_win < 0xFF) {
s_empty_win++;
}
if (s_empty_win >= BAT_EMPTY_WIN) {
s_low_bat = 1;
gDevData.alm_low_battery = 1;
if (!s_req_poweroff) {
log_info("vbat %dmV empty, deep off\n", mv);
usr_key_request_poweroff();
}
}
} else {
s_empty_win = 0;
}
if (mv < BOARD_VBAT_LOW_MV) {
s_low_exit_win = 0;
if (!s_low_bat && s_low_enter_win < 0xFF) {
s_low_enter_win++;
if (s_low_enter_win >= BAT_LOW_WIN) {
s_low_bat = 1;
s_low_ms = 0;
gDevData.alm_low_battery = 1;
log_info("low battery %dmV %d%%\n", mv, s_bat_pct);
}
}
} else if (mv > (BOARD_VBAT_LOW_MV + BOARD_VBAT_LOW_HYST_MV)) {
s_low_enter_win = 0;
if (s_low_bat) {
if (s_low_exit_win < 0xFF) {
s_low_exit_win++;
}
if (s_low_exit_win >= BAT_LOW_WIN) {
s_low_bat = 0;
s_low_ms = 0;
gDevData.alm_low_battery = 0;
log_info("battery recover %dmV %d%%\n", mv, s_bat_pct);
}
}
} else {
s_low_enter_win = 0;
s_low_exit_win = 0;
}
}
}
}
}
if (s_usb_online || s_req_poweroff || (s_bat_boot_ms < BAT_BOOT_MS)) {
s_low_ms = 0;
} else if (s_low_bat && (s_low_ms < BOARD_LOWBAT_SLEEP_MS)) {
s_low_ms += dt;
if (s_low_ms >= BOARD_LOWBAT_SLEEP_MS) {
s_low_ms = BOARD_LOWBAT_SLEEP_MS;
log_info("low battery 1min, deep off\n");
usr_key_request_poweroff();
}
}
s_bat_log_ms = usr_add_ms(s_bat_log_ms, dt);
if (s_bat_log_ms >= BAT_LOG_MS) {
s_bat_log_ms = 0;
log_info("bat %d%% %dmV alm=%d\n", s_bat_pct, s_bat_mv, s_low_bat);
}
}
/**
* @brief 短按:自动模式 0→1→…→6→0
* @return 无
*/
static void usr_key_on_click(void)
{
if (s_hand_busy) {
log_info("key click ignored, hand busy\n");
return;
}
usr_play_mode_apply((uint8_t)((s_play_mode + 1) % 7));
app_led_request_mode_blink();
s_key_led_override_ms = (uint16_t)((BOARD_LED_MODE_OFF_MS + BOARD_LED_MODE_ON_MS)
* BOARD_LED_MODE_BLINK_N + 50);
log_info("key click play_mode=%d\n", s_play_mode);
}
/**
* @brief 按键状态初值
* @return 无
*/
static void usr_key_init(void)
{
s_play_mode = 0;
s_boot_key_lock = bsp_key_is_pressed() ? 1 : 0;
s_key_pressed = s_boot_key_lock;
s_key_raw = s_key_pressed;
s_key_cnt = BOARD_KEY_DEBOUNCE_MS;
s_key_hold_ms = 0;
if (s_boot_key_lock) {
log_info("boot key held, wait release\n");
}
}
/**
* @brief 按键消抖:开机后 1s 内短按切模式,按住 3s 深睡;充电时忽略
* @note 开机中即使按满 5s 也不进配对。配对只在深睡长按 5s、BLE 起来前判定
* @return 无
*/
static void usr_key_tick_1ms(uint16_t dt)
{
uint8_t raw;
uint16_t hold_old;
if (s_req_poweroff) {
return;
}
raw = bsp_key_is_pressed();
if (raw == s_key_raw) {
if (s_key_cnt < BOARD_KEY_DEBOUNCE_MS) {
s_key_cnt = (uint8_t)usr_add_ms(s_key_cnt, dt);
if (s_key_cnt > BOARD_KEY_DEBOUNCE_MS) {
s_key_cnt = BOARD_KEY_DEBOUNCE_MS;
}
}
} else {
s_key_raw = raw;
s_key_cnt = 0;
}
if (s_key_cnt != BOARD_KEY_DEBOUNCE_MS) {
if (s_key_pressed && !s_boot_key_lock) {
s_key_hold_ms = usr_add_ms(s_key_hold_ms, dt);
}
return;
}
if (raw && !s_key_pressed) {
s_key_pressed = 1;
s_key_hold_ms = 0;
} else if (!raw && s_key_pressed) {
if (s_boot_key_lock) {
s_boot_key_lock = 0;
log_info("boot key released\n");
} else if (s_key_hold_ms < BOARD_KEY_CLICK_MS && s_key_hold_ms >= 10) {
if (s_usb_online) {
log_info("key click ignored, charging\n");
} else if (s_pairing) {
log_info("key click ignored, pairing\n");
} else if (!s_app_power) {
s_app_power = 1;
gDevData.power = 1;
app_led_hint_power_on();
log_info("key click cancel soft off\n");
} else {
usr_key_on_click();
}
}
s_key_pressed = 0;
s_key_hold_ms = 0;
}
if (s_key_pressed && !s_boot_key_lock) {
hold_old = s_key_hold_ms;
s_key_hold_ms = usr_add_ms(s_key_hold_ms, dt);
if ((hold_old < BOARD_KEY_POWER_MS) && (s_key_hold_ms >= BOARD_KEY_POWER_MS)) {
if (s_usb_online) {
log_info("key hold 3s ignored, charging\n");
} else if (s_pairing) {
log_info("key hold 3s ignored, pairing\n");
} else {
log_info("key hold 3s, poweroff\n");
usr_key_request_poweroff();
}
}
}
}
/**
* @brief 充电检测:CHG 低=充电中;VPWR 在且 CHG 高满 1s=充满;拔电软关
* @return 无
*/
static void usr_chg_tick_1ms(uint16_t dt)
{
uint8_t chg_low = bsp_chg_is_low();
uint8_t vpwr = bsp_vpwr_is_online();
uint8_t usb;
if (chg_low) {
s_full_ms = 0;
s_charge_led = 1;
gDevData.charge = CHARGE_1;
usb = 1;
} else if (vpwr) {
/* 拔电时 CHG 先变高、VPWR 还没掉,不能立刻当充满 */
s_full_ms = usr_add_ms(s_full_ms, dt);
if (s_full_ms >= BOARD_CHG_FULL_MS) {
s_charge_led = 2;
}
gDevData.charge = (s_charge_led == 1) ? CHARGE_1 : CHARGE_2;
usb = 1;
} else {
s_full_ms = 0;
s_charge_led = 0;
gDevData.charge = CHARGE_2;
usb = 0;
}
if (usb != s_usb_online) {
s_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(),
usr_get_pair_flag(), usr_var.usr_goto_pair);
if (usb) {
app_mode_stop();
usr_hand_stop();
s_play_mode = 0;
gDevData.play_mode = 0;
log_info("usb in, motor stop\n");
} else {
ui_update_status(STATUS_NORMAL_POWER);
usr_app_soft_off();
}
}
usr_var.usr_power_charge_flag = s_usb_online ? 1 : 2;
if (s_charge_led != s_charge_led_last) {
s_charge_led_last = s_charge_led;
log_info("charge_led=%d chg=%d vpwr=%d mv=%d\n",
s_charge_led, chg_low, vpwr, bsp_vpwr_mv());
}
}
/**
* @brief 组装灯效上下文:充电 > 配对 > 软关灭灯 > 低电 > 切模式闪 > 绿灯
* @return 无
*/
static void usr_led_tick_1ms(uint16_t dt)
{
AppLedCtx_t led;
uint8_t paired;
if (s_req_poweroff) {
bsp_led_all_off();
return;
}
paired = usr_get_pair_flag();
if (s_pairing) {
if (paired) {
app_led_start_pair_result(1);
s_pairing = 0;
s_pair_ms = 0;
log_info("pair ok\n");
} else {
s_pair_ms += dt;
if (s_pair_ms >= BOARD_PAIR_TIMEOUT_MS) {
s_pairing = 0;
s_pair_ms = 0;
app_led_start_pair_result(0);
log_info("pair timeout 60s, stay on\n");
}
}
}
if (s_key_led_override_ms) {
if (s_key_led_override_ms > dt) {
s_key_led_override_ms = (uint16_t)(s_key_led_override_ms - dt);
} else {
s_key_led_override_ms = 0;
}
}
memset(&led, 0, sizeof(led));
led.charge_led = s_charge_led;
led.pairing = (uint8_t)(s_pairing && s_app_power && (s_key_led_override_ms == 0));
led.app_power = s_app_power;
led.low_bat = s_low_bat;
led.life_on = 1;
app_led_tick_1ms(&led, dt);
}
/**
* @brief 业务初始化(在 BLE 栈起来之前由 app_main 调用)
* @note 内部会等按键 3s/5s;不足 3s 直接深睡,不会启动 1ms 定时器
* @return 无
*/
void usr_jb_init(void)
{
jbInit();
userInit();
bsp_hw_init();
app_led_init();
app_mode_init();
usr_boot_key_wait();
usr_key_init();
s_app_power = 1;
gDevData.power = 1;
s_hand_busy = 0;
s_hand_cmd_last = 0;
s_hand_write_pending = 0;
s_hand_write_cmd = 0;
s_charge_led_last = 0xff;
usr_timer_rearm(0);
log_info("timer idle %ums, sleep ok\n", (unsigned)USR_TIMER_IDLE_MS);
}
/**
* @brief 电机/按键按住时用 1ms 高优定时器;空闲用可休眠节拍
* @return 1=需要 1ms 高优,0=可休眠
*/
static uint8_t usr_timer_should_fast(void)
{
if (s_hand_busy || s_key_pressed || s_req_poweroff) {
if (app_function_is_hand_busy() || bsp_key_is_pressed() || app_power_poweroff_pending()) {
return 1;
}
if (app_mode_get_active() || bsp_motor_is_busy()) {
@@ -829,53 +137,100 @@ static uint8_t usr_timer_should_fast(void)
*/
static void usr_timer_rearm(uint8_t fast)
{
if (jb_timer_id) {
usr_timer_del((u16)jb_timer_id);
jb_timer_id = 0;
if (s_sched.timer_id) {
usr_timer_del((u16)s_sched.timer_id);
s_sched.timer_id = 0;
}
s_timer_fast = fast;
s_sched.fast = fast;
if (fast) {
jb_timer_id = (int)sys_hi_timer_add(NULL, usr_timer_1_ms, 1);
s_sched.timer_id = (int)sys_hi_timer_add(NULL, usr_timer_1_ms, 1);
} else {
jb_timer_id = (int)sys_s_hi_timer_add(NULL, usr_timer_1_ms, USR_TIMER_IDLE_MS);
s_sched.timer_id = (int)sys_s_hi_timer_add(NULL, usr_timer_1_ms, USR_TIMER_IDLE_MS);
}
}
/**
* @brief 业务定时器:空闲可 sleep;玩法/点动时 1ms 且禁 sleep
* @param priv 未使用
* @note 禁止在这里直接 sys_enter_soft_poweroff
* @note 禁止在这里直接 sys_enter_soft_poweroff,一律走 app_power 的深睡钩子
* @return 无
*/
static void usr_timer_1_ms(void *priv)
{
uint16_t dt = s_timer_fast ? 1 : USR_TIMER_IDLE_MS;
uint16_t dt = s_sched.fast ? 1 : USR_TIMER_IDLE_MS;
uint16_t i;
uint8_t want;
AppPowerEvt_t pwr_evt;
(void)priv;
for (i = 0; i < dt; i++) {
jbTimerIrq();
}
usr_key_tick_1ms(dt);
usr_chg_tick_1ms(dt);
usr_bat_tick_1ms(dt);
usr_app_power_poll();
usr_hand_poll();
usr_play_mode_poll();
if (s_hand_busy) {
usr_hand_tick_1ms(dt);
if (!app_power_poweroff_pending()) {
user_key_scan(dt);
user_key_handle();
}
pwr_evt = app_power_tick_1ms(dt);
usr_on_power_evt(pwr_evt);
app_function_poll();
app_function_tick_1ms(dt);
if (app_power_poweroff_pending()) {
app_led_clear();
} else {
app_mode_run();
app_led_run(dt, app_function_is_power_on());
}
usr_led_tick_1ms(dt);
want = usr_timer_should_fast();
if (want != s_timer_fast) {
if (want != s_sched.fast) {
usr_timer_rearm(want);
}
}
/**
* @brief 业务初始化(在 BLE 栈起来之前由 app_main 调用)
* @note 内部会等按键 3s/5s;不足 3s 直接深睡,不会启动 1ms 定时器。
* 按键判定只摸 bsp_hw 的裸 GPIO,不经过协议栈/灯效表/电机状态机,
* 所以只有 bsp_hw_init() 需要排在它前面——一旦判定要睡回去,
* boot_sleep() 原地喂狗不再返回,后面这些初始化不会被浪费。
* @return 无
*/
void usr_jb_init(void)
{
bsp_hw_init();
app_boot_wait_key(); /* 深睡分支不会返回;返回即代表确定要开机 */
jbInit();
userInit();
app_led_init();
app_mode_init();
app_power_init();
app_power_set_hooks(usr_poweroff_request_hook, usr_poweroff_finalize_hook);
app_function_init();
user_key_init();
user_key_lock_if_held();
app_function_power_on_silent();
usr_timer_rearm(0);
log_info("timer idle %ums, sleep ok\n", (unsigned)USR_TIMER_IDLE_MS);
}
/**
* @brief DP2 写入钩子(jb_product 薄适配调用,不在此驱电机)
* @param cmd 第 1 字节:0=空闲,1=顺时针,2=逆时针,3=停
* @return 无
*/
void usr_dp_hand_write(uint8_t cmd)
{
app_function_dp_hand_write(cmd);
}
/**
* @brief BLE 收到见宝载荷,送入协议环形缓冲
* @param data 载荷指针
+63 -57
View File
@@ -26,13 +26,19 @@
#include "debug.h"
static uint8_t s_initialized;
static BspMotorDir_t s_motor_dir;
static BspMotorDir_t s_motor_pending;
static uint16_t s_motor_duty;
static uint16_t s_pending_duty;
static uint16_t s_brake_ms;
static u32 s_brake_t0;
static uint8_t s_pwm_clk_on; /**< 1=MCPWM 已打开 */
/** 电机状态,替代原来散落的 7 个 static */
typedef struct {
BspMotorDir_t dir; /**< 当前实际方向(含正在刹车时的反向) */
BspMotorDir_t pending; /**< 死区/刹车结束后要切到的方向 */
uint16_t duty; /**< 当前实际占空比 */
uint16_t pending_duty; /**< 死区/刹车结束后要用的占空比 */
uint16_t brake_ms; /**< 换向死区/反转刹车剩余时长,0=空闲 */
u32 brake_t0; /**< 死区/刹车起始时间戳 */
uint8_t pwm_clk_on; /**< 1=MCPWM 时钟已打开 */
} BspMotorCtx_t;
static BspMotorCtx_t s_motor;
extern u32 timer_get_ms(void);
@@ -101,7 +107,7 @@ static void bsp_motor_pwm_init(void)
mcpwm_set_duty(pwm_ch0, 0);
mcpwm_set_duty(pwm_ch1, 0);
s_pwm_clk_on = 1;
s_motor.pwm_clk_on = 1;
log_info("motor pwm %uHz duty=%u/10000\n",
(unsigned)BOARD_MOTOR_PWM_HZ, (unsigned)BOARD_MOTOR_PWM_DUTY);
}
@@ -112,7 +118,7 @@ static void bsp_motor_pwm_init(void)
*/
static void bsp_motor_pwm_clock_on(void)
{
if (s_pwm_clk_on) {
if (s_motor.pwm_clk_on) {
return;
}
mcpwm_open(pwm_ch0);
@@ -121,7 +127,7 @@ static void bsp_motor_pwm_clock_on(void)
gpio_och_sel_output_signal(BOARD_MOTOR_INB_PIN, OUTPUT_CH_SIGNAL_MC_PWM1_H);
gpio_set_direction(BOARD_MOTOR_INA_PIN, 0);
gpio_set_direction(BOARD_MOTOR_INB_PIN, 0);
s_pwm_clk_on = 1;
s_motor.pwm_clk_on = 1;
}
/**
@@ -132,13 +138,13 @@ static void bsp_motor_pwm_clock_off(void)
{
mcpwm_set_duty(pwm_ch0, 0);
mcpwm_set_duty(pwm_ch1, 0);
if (s_pwm_clk_on) {
if (s_motor.pwm_clk_on) {
mcpwm_close(pwm_ch0);
mcpwm_close(pwm_ch1);
gpio_och_disable_output_signal(BOARD_MOTOR_INA_PIN, OUTPUT_CH_SIGNAL_MC_PWM0_H);
gpio_och_disable_output_signal(BOARD_MOTOR_INB_PIN, OUTPUT_CH_SIGNAL_MC_PWM1_H);
JL_MCPWM->MCPWM_CON0 = 0;
s_pwm_clk_on = 0;
s_motor.pwm_clk_on = 0;
}
gpio_direction_output(BOARD_MOTOR_INA_PIN, 0);
gpio_direction_output(BOARD_MOTOR_INB_PIN, 0);
@@ -219,11 +225,11 @@ BspHw_Ret_t bsp_hw_init(void)
#if BOARD_MOTOR_ENABLE
bsp_motor_pwm_init();
s_motor_dir = BSP_MOTOR_STOP;
s_motor_pending = BSP_MOTOR_STOP;
s_motor_duty = 0;
s_pending_duty = 0;
s_brake_ms = 0;
s_motor.dir = BSP_MOTOR_STOP;
s_motor.pending = BSP_MOTOR_STOP;
s_motor.duty = 0;
s_motor.pending_duty = 0;
s_motor.brake_ms = 0;
#if BOARD_MOTOR_PWM_IDLE_CLOSE
bsp_motor_pwm_clock_off();
#endif
@@ -314,57 +320,57 @@ void bsp_motor_set_pwm(BspMotorDir_t dir, uint16_t duty)
}
/* 同向只改占空比:直接改 PWM,不走换向死区 */
if (dir == s_motor_dir && s_brake_ms == 0) {
if (duty == s_motor_duty) {
if (dir == s_motor.dir && s_motor.brake_ms == 0) {
if (duty == s_motor.duty) {
return;
}
s_motor_duty = duty;
s_motor_pending = dir;
s_pending_duty = duty;
s_motor.duty = duty;
s_motor.pending = dir;
s_motor.pending_duty = duty;
bsp_motor_gpio(dir, duty);
return;
}
/* 已经在反转刹车等到停,忽略重复 STOP */
if (dir == BSP_MOTOR_STOP && s_motor_pending == BSP_MOTOR_STOP && s_brake_ms) {
if (dir == BSP_MOTOR_STOP && s_motor.pending == BSP_MOTOR_STOP && s_motor.brake_ms) {
return;
}
/* 正在转时要停:先反向驱动再滑行,短接对此驱动无效 */
if (dir == BSP_MOTOR_STOP
&& (s_motor_dir == BSP_MOTOR_IN || s_motor_dir == BSP_MOTOR_OUT)) {
BspMotorDir_t rev = (s_motor_dir == BSP_MOTOR_OUT) ? BSP_MOTOR_IN : BSP_MOTOR_OUT;
uint16_t brake_duty = s_motor_duty ? s_motor_duty : BOARD_MOTOR_PWM_DUTY;
&& (s_motor.dir == BSP_MOTOR_IN || s_motor.dir == BSP_MOTOR_OUT)) {
BspMotorDir_t rev = (s_motor.dir == BSP_MOTOR_OUT) ? BSP_MOTOR_IN : BSP_MOTOR_OUT;
uint16_t brake_duty = s_motor.duty ? s_motor.duty : BOARD_MOTOR_PWM_DUTY;
bsp_motor_gpio(rev, brake_duty);
s_motor_dir = rev;
s_motor_duty = brake_duty;
s_motor_pending = BSP_MOTOR_STOP;
s_pending_duty = 0;
s_brake_ms = BOARD_MOTOR_STOP_BRAKE_MS;
s_brake_t0 = timer_get_ms();
s_motor.dir = rev;
s_motor.duty = brake_duty;
s_motor.pending = BSP_MOTOR_STOP;
s_motor.pending_duty = 0;
s_motor.brake_ms = BOARD_MOTOR_STOP_BRAKE_MS;
s_motor.brake_t0 = timer_get_ms();
log_info("motor rev-brake %ums duty=%u\n",
(unsigned)BOARD_MOTOR_STOP_BRAKE_MS, (unsigned)brake_duty);
return;
}
/* 换向:先松开再输出,避免 H 桥直通 */
if (dir != BSP_MOTOR_STOP && s_motor_dir != BSP_MOTOR_STOP && dir != s_motor_dir) {
if (dir != BSP_MOTOR_STOP && s_motor.dir != BSP_MOTOR_STOP && dir != s_motor.dir) {
bsp_motor_gpio(BSP_MOTOR_STOP, 0);
s_motor_pending = dir;
s_pending_duty = duty;
s_brake_ms = BOARD_MOTOR_REVERSE_MS;
s_brake_t0 = timer_get_ms();
s_motor_dir = BSP_MOTOR_STOP;
s_motor_duty = 0;
s_motor.pending = dir;
s_motor.pending_duty = duty;
s_motor.brake_ms = BOARD_MOTOR_REVERSE_MS;
s_motor.brake_t0 = timer_get_ms();
s_motor.dir = BSP_MOTOR_STOP;
s_motor.duty = 0;
return;
}
s_brake_ms = 0;
s_motor_dir = dir;
s_motor_pending = dir;
s_motor_duty = duty;
s_pending_duty = duty;
s_motor.brake_ms = 0;
s_motor.dir = dir;
s_motor.pending = dir;
s_motor.duty = duty;
s_motor.pending_duty = duty;
bsp_motor_gpio(dir, duty);
#endif
}
@@ -378,16 +384,16 @@ void bsp_motor_brake_tick(void)
#if !BOARD_MOTOR_ENABLE
return;
#else
if (s_brake_ms == 0) {
if (s_motor.brake_ms == 0) {
return;
}
if ((timer_get_ms() - s_brake_t0) < s_brake_ms) {
if ((timer_get_ms() - s_motor.brake_t0) < s_motor.brake_ms) {
return;
}
s_brake_ms = 0;
s_motor_dir = s_motor_pending;
s_motor_duty = s_pending_duty;
bsp_motor_gpio(s_motor_dir, s_motor_duty);
s_motor.brake_ms = 0;
s_motor.dir = s_motor.pending;
s_motor.duty = s_motor.pending_duty;
bsp_motor_gpio(s_motor.dir, s_motor.duty);
#endif
}
@@ -486,13 +492,13 @@ uint8_t bsp_motor_is_busy(void)
#if !BOARD_MOTOR_ENABLE
return 0;
#else
if (s_brake_ms) {
if (s_motor.brake_ms) {
return 1;
}
if (s_motor_dir != BSP_MOTOR_STOP) {
if (s_motor.dir != BSP_MOTOR_STOP) {
return 1;
}
if (s_motor_pending != BSP_MOTOR_STOP) {
if (s_motor.pending != BSP_MOTOR_STOP) {
return 1;
}
return 0;
@@ -507,10 +513,10 @@ void bsp_hw_enter_sleep_io(void)
{
bsp_motor_set(BSP_MOTOR_STOP);
#if BOARD_MOTOR_ENABLE
s_brake_ms = 0;
s_motor_dir = BSP_MOTOR_STOP;
s_motor_pending = BSP_MOTOR_STOP;
s_motor_duty = 0;
s_motor.brake_ms = 0;
s_motor.dir = BSP_MOTOR_STOP;
s_motor.pending = BSP_MOTOR_STOP;
s_motor.duty = 0;
bsp_motor_pwm_clock_off();
#endif
bsp_led_all_off();