功耗减低到900uA,还有优化空间,还要陈杰明那边支持一下

This commit is contained in:
2026-08-25 11:16:54 +08:00
parent e0f5da6f39
commit d5ec6ecde0
8 changed files with 290 additions and 72 deletions
+3
View File
@@ -80,6 +80,8 @@
#define BOARD_LED_MODE_OFF_MS 150 /**< 切模式闪:灭 */
#define BOARD_LED_MODE_ON_MS 150 /**< 切模式闪:亮 */
#define BOARD_LED_MODE_BLINK_N 2 /**< 切模式闪次数 */
#define BOARD_LED_STANDBY_ON 1 /**< 1=待机绿灯常亮;0=亮完 BOARD_LED_POWER_ON_MS 后灭(对比电流) */
#define BOARD_LED_POWER_ON_MS 3000 /**< BOARD_LED_STANDBY_ON=0 时的开机绿灯时长 */
/*============================================================================*/
/* 电机 */
@@ -90,5 +92,6 @@
#define BOARD_HAND_PULSE_MS 1000 /**< 预留;手动 1/2 一直转到写 03 停止 */
#define BOARD_MOTOR_PWM_HZ 20000 /**< MCPWM 频率(Hz)20k 避开可听啸叫 */
#define BOARD_MOTOR_PWM_DUTY 8000 /**< 占空比 0~10000=0%~100%80% */
#define BOARD_MOTOR_PWM_IDLE_CLOSE 1 /**< 1=空闲/轻睡关 MCPWM;0=保持开着,方便对比待机电流 */
#endif /* __BOARD_PIN_H__ */
+97 -9
View File
@@ -3,12 +3,13 @@
* @brief CBC2 板级 GPIO / ADC:电机 H 桥、LED、按键、充电检测
* @author cyWu <1917507415@qq.com>
* @date 2026.08.24
* @version V1.4.0
* @version V1.5.0
* @history
* - V1.0.0, 2026.08.21, cyWu, 首次发布
* - V1.1.0, 2026.08.24, cyWu, 停转反转刹车;去掉限位脚
* - V1.3.0, 2026.08.24, cyWu, 电机改 MCPWM 占空比降速
* - V1.4.0, 2026.08.24, cyWu, 增加按占空比驱动,供玩法变速
* - V1.5.0, 2026.08.25, cyWu, 空闲关闭 MCPWM 时钟
******************************************************************************/
#include "system/includes.h"
@@ -31,6 +32,7 @@ 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 已打开 */
extern u32 timer_get_ms(void);
@@ -99,10 +101,49 @@ static void bsp_motor_pwm_init(void)
mcpwm_set_duty(pwm_ch0, 0);
mcpwm_set_duty(pwm_ch1, 0);
s_pwm_clk_on = 1;
log_info("motor pwm %uHz duty=%u/10000\n",
(unsigned)BOARD_MOTOR_PWM_HZ, (unsigned)BOARD_MOTOR_PWM_DUTY);
}
/**
* @brief 打开 MCPWM 并绑回 INA/INB
* @return 无
*/
static void bsp_motor_pwm_clock_on(void)
{
if (s_pwm_clk_on) {
return;
}
mcpwm_open(pwm_ch0);
mcpwm_open(pwm_ch1);
gpio_och_sel_output_signal(BOARD_MOTOR_INA_PIN, OUTPUT_CH_SIGNAL_MC_PWM0_H);
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;
}
/**
* @brief 关闭 MCPWM 时钟,INA/INB 拉低(待机必关)
* @return 无
*/
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) {
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;
}
gpio_direction_output(BOARD_MOTOR_INA_PIN, 0);
gpio_direction_output(BOARD_MOTOR_INB_PIN, 0);
}
/**
* @brief 立刻改 INA/INB PWM。运转脚输出 duty,另一脚 0%STOP=两路 0%
* @param dir 目标方向
@@ -132,6 +173,14 @@ static void bsp_motor_gpio(BspMotorDir_t dir, uint16_t duty)
}
}
if ((ina_duty == 0) && (inb_duty == 0)) {
#if BOARD_MOTOR_PWM_IDLE_CLOSE
bsp_motor_pwm_clock_off();
return;
#endif
} else {
bsp_motor_pwm_clock_on();
}
mcpwm_set_duty(pwm_ch0, ina_duty);
mcpwm_set_duty(pwm_ch1, inb_duty);
}
@@ -175,6 +224,9 @@ BspHw_Ret_t bsp_hw_init(void)
s_motor_duty = 0;
s_pending_duty = 0;
s_brake_ms = 0;
#if BOARD_MOTOR_PWM_IDLE_CLOSE
bsp_motor_pwm_clock_off();
#endif
#endif
s_initialized = 1;
@@ -408,6 +460,45 @@ uint8_t bsp_vpwr_is_online(void)
#endif
}
/**
* @brief 轻睡前关掉空闲电机 PWM(电机忙碌时不关,避免玩法被掐断)
* @return 无
*/
void bsp_hw_sleep_pwm_off(void)
{
#if BOARD_MOTOR_ENABLE && BOARD_MOTOR_PWM_IDLE_CLOSE
if (!s_initialized) {
return;
}
if (bsp_motor_is_busy()) {
return;
}
bsp_motor_pwm_clock_off();
#endif
}
/**
* @brief 电机是否还在转或刹车
* @return 1=忙碌,0=已停且 PWM 已关
*/
uint8_t bsp_motor_is_busy(void)
{
#if !BOARD_MOTOR_ENABLE
return 0;
#else
if (s_brake_ms) {
return 1;
}
if (s_motor_dir != BSP_MOTOR_STOP) {
return 1;
}
if (s_motor_pending != BSP_MOTOR_STOP) {
return 1;
}
return 0;
#endif
}
/**
* @brief 进深睡前把电机 PWM 关掉并拉低,灯全灭
* @return 无
@@ -416,14 +507,11 @@ void bsp_hw_enter_sleep_io(void)
{
bsp_motor_set(BSP_MOTOR_STOP);
#if BOARD_MOTOR_ENABLE
mcpwm_set_duty(pwm_ch0, 0);
mcpwm_set_duty(pwm_ch1, 0);
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);
gpio_direction_output(BOARD_MOTOR_INA_PIN, 0);
gpio_direction_output(BOARD_MOTOR_INB_PIN, 0);
s_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();
}
+14 -1
View File
@@ -3,12 +3,13 @@
* @brief CBC2 板级硬件:电机 / LED / 按键 / 充电 / 电量
* @author cyWu <1917507415@qq.com>
* @date 2026.08.24
* @version V1.4.0
* @version V1.5.0
* @history
* - V1.0.0, 2026.08.21, cyWu, 首次发布
* - V1.1.0, 2026.08.24, cyWu, 停转改为反转刹车;去掉未贴件限位
* - V1.3.0, 2026.08.24, cyWu, 电机改 MCPWM 50% 占空比降速
* - V1.4.0, 2026.08.24, cyWu, 增加按占空比驱动,供玩法变速
* - V1.5.0, 2026.08.25, cyWu, 空闲关闭 MCPWM 时钟,避免待机空转耗电
******************************************************************************/
#ifndef __BSP_HW_H__
@@ -83,6 +84,12 @@ void bsp_motor_set_pwm(BspMotorDir_t dir, uint16_t duty);
*/
void bsp_motor_brake_tick(void);
/**
* @brief 电机是否还在转或刹车
* @return 1=忙碌,0=已停且 PWM 已关
*/
uint8_t bsp_motor_is_busy(void);
/**
* @brief 读取按键
* @return 1=按下,0=未按下
@@ -113,6 +120,12 @@ uint16_t bsp_vpwr_mv(void);
*/
uint8_t bsp_vpwr_is_online(void);
/**
* @brief 轻睡前关掉空闲电机 PWM(电机忙碌时不关)
* @return 无
*/
void bsp_hw_sleep_pwm_off(void);
/**
* @brief 进深睡前把电机 PWM 关掉并拉低,灯全灭
* @return 无