优化程序架构

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
+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();