基本功能已完成,还差功耗和架构优化

This commit is contained in:
2026-08-24 18:03:39 +08:00
parent 81bd2f724d
commit 0e6a93c12d
25 changed files with 4847 additions and 33 deletions
+94
View File
@@ -0,0 +1,94 @@
/******************************************************************************
* @file board_pin.h
* @brief CBC2 门夹弹力绳板级引脚、电平与时序(换板只改本文件)
* @author cyWu <1917507415@qq.com>
* @date 2026.08.24
* @version V1.3.0
* @history
* - V1.0.0, 2026.08.21, cyWu, 首次发布
* - V1.1.0, 2026.08.24, cyWu, 收口:去掉实验开关与未贴件限位
* - V1.2.0, 2026.08.24, cyWu, 按键:深睡 3s 开机、5s 开机+配对
* - V1.3.0, 2026.08.24, cyWu, 电机改 MCPWM 50% 占空比降速
******************************************************************************/
#ifndef __BOARD_PIN_H__
#define __BOARD_PIN_H__
#include "asm/gpio.h"
/*============================================================================*/
/* 功能开关:某路硬件异常时可单独关掉,不影响配网 */
/*============================================================================*/
#define BOARD_LED_ENABLE 1 /**< 1=红绿 LED */
#define BOARD_KEY_ENABLE 1 /**< 1=按键 PB1 */
#define BOARD_CHG_ENABLE 1 /**< 1=充电检测 PG8 + VPWR */
#define BOARD_MOTOR_ENABLE 1 /**< 1=电机 PA6/PC2 */
/*============================================================================*/
/* GPIO */
/*============================================================================*/
#define BOARD_MOTOR_INA_PIN IO_PORTA_06 /**< 电机 INA */
#define BOARD_MOTOR_INB_PIN IO_PORTC_02 /**< 电机 INB */
#define BOARD_LED_RED_PIN IO_PORTA_08 /**< 红灯 LED_PWR,低电平亮 */
#define BOARD_LED_GREEN_PIN IO_PORTA_07 /**< 绿灯 LED_CHG,低电平亮 */
#define BOARD_KEY_PIN IO_PORTB_01 /**< 按键,低电平按下 */
#define BOARD_CHG_PIN IO_PORTG_08 /**< ME4054 CHG,外部 2.2k 上拉 */
/* PA3 限位未贴件,空脚不初始化 */
#define BOARD_LED_ON_LEVEL 0 /**< LED 点亮电平:0=低亮 */
#define BOARD_KEY_ACTIVE_LEVEL 0 /**< 按键有效电平:0=按下为低 */
#define BOARD_CHG_CHARGING_LEVEL 0 /**< CHG 充电中为低 */
/**
* 收绳方向:1 = INA 高 / INB 低 为收绳(顺时针)
* 若实测方向反了,把本宏改为 0
*/
#define BOARD_MOTOR_IN_INA_HIGH 1
/*============================================================================*/
/* 电量 / 充电 */
/*============================================================================*/
#define BOARD_VBAT_FULL_MV 4200 /**< 100% */
#define BOARD_VBAT_EMPTY_MV 3200 /**< 0%,持续低于此值深睡 */
#define BOARD_VBAT_LOW_MV 3700 /**< 低电预警阈值 */
#define BOARD_VBAT_LOW_HYST_MV 50 /**< 低电恢复回差 */
#define BOARD_VPWR_ONLINE_MV 4000 /**< VPWR 判定 USB 插入 */
#define BOARD_VPWR_OFF_MV 1500 /**< 低于此值认定已拔出 */
#define BOARD_CHG_FULL_MS 1000 /**< CHG 已高且 VPWR 在,满 1s 认充满 */
#define BOARD_LOWBAT_SLEEP_MS (60u * 1000u) /**< 低电告警满 1 分钟后深睡 */
/*============================================================================*/
/* 按键 / 灯效 */
/*============================================================================*/
#define BOARD_KEY_CLICK_MS 1000 /**< 开机后:按下 <1s 松开为短按切模式 */
#define BOARD_KEY_POWER_MS 3000 /**< 深睡:按住 3s 开机;开机中:按住 3s 深睡 */
#define BOARD_KEY_PAIR_MS 5000 /**< 深睡:按住 5s 开机并进配对(开机中继续按不住配对) */
#define BOARD_KEY_DEBOUNCE_MS 20 /**< 按键消抖 */
#define BOARD_PAIR_TIMEOUT_MS (60u * 1000u) /**< 配对超时,失败后保持开机 */
#define BOARD_LED_FAST_ON_MS 100 /**< 配对绿快闪:亮 */
#define BOARD_LED_FAST_OFF_MS 100 /**< 配对绿快闪:灭 */
#define BOARD_LED_ALT_MS 200 /**< 配对失败红绿交替半周期 */
#define BOARD_LED_PAIR_OK_MS 3000 /**< 配对成功绿灯常亮时长 */
#define BOARD_LED_PAIR_FAIL_MS 3000 /**< 配对失败提示时长 */
#define BOARD_LED_LOWBAT_ON_MS 200 /**< 低电红闪:亮 */
#define BOARD_LED_LOWBAT_PERIOD_MS 2000 /**< 低电红闪:周期 */
#define BOARD_LED_MODE_OFF_MS 150 /**< 切模式闪:灭 */
#define BOARD_LED_MODE_ON_MS 150 /**< 切模式闪:亮 */
#define BOARD_LED_MODE_BLINK_N 2 /**< 切模式闪次数 */
/*============================================================================*/
/* 电机 */
/*============================================================================*/
#define BOARD_MOTOR_REVERSE_MS 1 /**< 换向前先松开,防 H 桥直通 */
#define BOARD_MOTOR_STOP_BRAKE_MS 30 /**< 停转先反转刹车(ms),太长会倒转 */
#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% */
#endif /* __BOARD_PIN_H__ */
+429
View File
@@ -0,0 +1,429 @@
/******************************************************************************
* @file bsp_hw.c
* @brief CBC2 板级 GPIO / ADC:电机 H 桥、LED、按键、充电检测
* @author cyWu <1917507415@qq.com>
* @date 2026.08.24
* @version V1.4.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, 增加按占空比驱动,供玩法变速
******************************************************************************/
#include "system/includes.h"
#include "asm/gpio.h"
#include "asm/adc_api.h"
#include "asm/charge.h"
#include "asm/mcpwm.h"
#include "bsp_hw.h"
#define LOG_TAG_CONST APP
#define LOG_TAG "[BSP_HW]"
#define LOG_ERROR_ENABLE
#define LOG_INFO_ENABLE
#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;
extern u32 timer_get_ms(void);
#if (BOARD_LED_ENABLE || BOARD_MOTOR_ENABLE)
/**
* @brief 配置 GPIO 为推挽输出
* @param pin GPIO 编号
* @param value 输出初值
* @return 无
*/
static void bsp_pin_output(u32 pin, int value)
{
gpio_set_pull_up(pin, 0);
gpio_set_pull_down(pin, 0);
gpio_set_die(pin, 1);
gpio_direction_output(pin, value);
}
#endif
#if (BOARD_KEY_ENABLE || BOARD_CHG_ENABLE)
/**
* @brief 配置 GPIO 为数字输入(可内部上拉)
* @param pin GPIO 编号
* @param pull_up 1=内部上拉,0=不上拉
* @return 无
*/
static void bsp_pin_input(u32 pin, int pull_up)
{
gpio_direction_output(pin, 1);
gpio_set_direction(pin, 1);
gpio_set_pull_down(pin, 0);
gpio_set_pull_up(pin, pull_up ? 1 : 0);
gpio_set_die(pin, 1);
}
#endif
#if BOARD_MOTOR_ENABLE
/**
* @brief 初始化 INA/INB 两路 MCPWM,初始占空比 0(停转)
* @return 无
*/
static void bsp_motor_pwm_init(void)
{
struct pwm_platform_data pwm;
gpio_set_pull_up(BOARD_MOTOR_INA_PIN, 0);
gpio_set_pull_down(BOARD_MOTOR_INA_PIN, 0);
gpio_set_die(BOARD_MOTOR_INA_PIN, 1);
gpio_set_pull_up(BOARD_MOTOR_INB_PIN, 0);
gpio_set_pull_down(BOARD_MOTOR_INB_PIN, 0);
gpio_set_die(BOARD_MOTOR_INB_PIN, 1);
pwm.pwm_aligned_mode = pwm_edge_aligned;
pwm.frequency = BOARD_MOTOR_PWM_HZ;
pwm.duty = 0;
pwm.l_pin = (u8) - 1;
pwm.complementary_en = 0;
pwm.pwm_ch_num = pwm_ch0;
pwm.h_pin = BOARD_MOTOR_INA_PIN;
mcpwm_init(&pwm);
pwm.pwm_ch_num = pwm_ch1;
pwm.h_pin = BOARD_MOTOR_INB_PIN;
mcpwm_init(&pwm);
mcpwm_set_duty(pwm_ch0, 0);
mcpwm_set_duty(pwm_ch1, 0);
log_info("motor pwm %uHz duty=%u/10000\n",
(unsigned)BOARD_MOTOR_PWM_HZ, (unsigned)BOARD_MOTOR_PWM_DUTY);
}
/**
* @brief 立刻改 INA/INB PWM。运转脚输出 duty,另一脚 0%STOP=两路 0%
* @param dir 目标方向
* @param duty 占空比 0~10000
* @return 无
*/
static void bsp_motor_gpio(BspMotorDir_t dir, uint16_t duty)
{
u16 ina_duty = 0;
u16 inb_duty = 0;
if (duty > 10000) {
duty = 10000;
}
if (dir == BSP_MOTOR_IN) {
if (BOARD_MOTOR_IN_INA_HIGH) {
ina_duty = duty;
} else {
inb_duty = duty;
}
} else if (dir == BSP_MOTOR_OUT) {
if (BOARD_MOTOR_IN_INA_HIGH) {
inb_duty = duty;
} else {
ina_duty = duty;
}
}
mcpwm_set_duty(pwm_ch0, ina_duty);
mcpwm_set_duty(pwm_ch1, inb_duty);
}
#endif
/**
* @brief 初始化 GPIO / 充电 ADC
* @return BSP_HW_OK=成功
*/
BspHw_Ret_t bsp_hw_init(void)
{
if (s_initialized) {
return BSP_HW_OK;
}
#if BOARD_LED_ENABLE
bsp_pin_output(BOARD_LED_RED_PIN, !BOARD_LED_ON_LEVEL);
bsp_pin_output(BOARD_LED_GREEN_PIN, !BOARD_LED_ON_LEVEL);
#endif
#if BOARD_KEY_ENABLE
bsp_pin_input(BOARD_KEY_PIN, 1);
#endif
#if BOARD_CHG_ENABLE
/* PORTG 作数字输入必须开 dieh,否则读不到 ME4054 CHG */
gpio_disable_fun_output_port(BOARD_CHG_PIN);
gpio_set_pull_up(BOARD_CHG_PIN, 0);
gpio_set_pull_down(BOARD_CHG_PIN, 0);
gpio_set_die(BOARD_CHG_PIN, 1);
gpio_set_dieh(BOARD_CHG_PIN, 1);
gpio_direction_input(BOARD_CHG_PIN);
adc_add_sample_ch(AD_CH_LDO5V);
adc_set_sample_freq(AD_CH_LDO5V, 500);
#endif
#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;
#endif
s_initialized = 1;
log_info("hw init led=%d key=%d chg=%d motor=%d\n",
BOARD_LED_ENABLE, BOARD_KEY_ENABLE, BOARD_CHG_ENABLE,
BOARD_MOTOR_ENABLE);
return BSP_HW_OK;
}
/**
* @brief 查询板级硬件是否已初始化
* @return BSP_HW_OK=已初始化,BSP_HW_ERR_NOT_INIT=未初始化
*/
BspHw_Ret_t bsp_hw_get_status(void)
{
return s_initialized ? BSP_HW_OK : BSP_HW_ERR_NOT_INIT;
}
/**
* @brief 红灯开关
* @param on 1=亮,0=灭
* @return 无
*/
void bsp_led_red_set(uint8_t on)
{
#if BOARD_LED_ENABLE
gpio_direction_output(BOARD_LED_RED_PIN, on ? BOARD_LED_ON_LEVEL : !BOARD_LED_ON_LEVEL);
#else
(void)on;
#endif
}
/**
* @brief 绿灯开关
* @param on 1=亮,0=灭
* @return 无
*/
void bsp_led_green_set(uint8_t on)
{
#if BOARD_LED_ENABLE
gpio_direction_output(BOARD_LED_GREEN_PIN, on ? BOARD_LED_ON_LEVEL : !BOARD_LED_ON_LEVEL);
#else
(void)on;
#endif
}
/**
* @brief 红绿灯全灭
* @return 无
*/
void bsp_led_all_off(void)
{
bsp_led_red_set(0);
bsp_led_green_set(0);
}
/**
* @brief 设置电机方向。STOP 时若正在转,先反转刹车再滑行
* @param dir 收绳 / 放绳 / 停转
* @return 无
*/
void bsp_motor_set(BspMotorDir_t dir)
{
bsp_motor_set_pwm(dir, (dir == BSP_MOTOR_STOP) ? 0 : BOARD_MOTOR_PWM_DUTY);
}
/**
* @brief 设置电机方向与 PWM 占空比
* @param dir 收绳 / 放绳 / 停转
* @param duty 占空比 0~10000STOP 时按 0 处理
* @return 无
*/
void bsp_motor_set_pwm(BspMotorDir_t dir, uint16_t duty)
{
#if !BOARD_MOTOR_ENABLE
(void)dir;
(void)duty;
return;
#else
if (duty > 10000) {
duty = 10000;
}
if (dir == BSP_MOTOR_STOP) {
duty = 0;
}
/* 同向只改占空比:直接改 PWM,不走换向死区 */
if (dir == s_motor_dir && s_brake_ms == 0) {
if (duty == s_motor_duty) {
return;
}
s_motor_duty = duty;
s_motor_pending = dir;
s_pending_duty = duty;
bsp_motor_gpio(dir, duty);
return;
}
/* 已经在反转刹车等到停,忽略重复 STOP */
if (dir == BSP_MOTOR_STOP && s_motor_pending == BSP_MOTOR_STOP && s_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;
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();
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) {
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;
return;
}
s_brake_ms = 0;
s_motor_dir = dir;
s_motor_pending = dir;
s_motor_duty = duty;
s_pending_duty = duty;
bsp_motor_gpio(dir, duty);
#endif
}
/**
* @brief 推进换向死区与停转反转刹车,按真实毫秒计时
* @return 无
*/
void bsp_motor_brake_tick(void)
{
#if !BOARD_MOTOR_ENABLE
return;
#else
if (s_brake_ms == 0) {
return;
}
if ((timer_get_ms() - s_brake_t0) < s_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);
#endif
}
/**
* @brief 读取按键电平
* @return 1=按下,0=未按下
*/
uint8_t bsp_key_is_pressed(void)
{
#if BOARD_KEY_ENABLE
return (uint8_t)(gpio_read(BOARD_KEY_PIN) == BOARD_KEY_ACTIVE_LEVEL);
#else
return 0;
#endif
}
/**
* @brief 读取 ME4054 CHG 脚:充电中为低
* @return 1=充电中,0=未充电
*/
uint8_t bsp_chg_is_low(void)
{
#if BOARD_CHG_ENABLE
return (uint8_t)(gpio_read(BOARD_CHG_PIN) == BOARD_CHG_CHARGING_LEVEL);
#else
return 0;
#endif
}
/**
* @brief 读取电池电压
* @return 电池电压,单位 mV
*/
uint16_t bsp_vbat_mv(void)
{
return (uint16_t)(adc_get_voltage(AD_CH_VBAT) * 4);
}
/**
* @brief 读取 5V 口电压,用于判断 USB
* @return VPWR 电压,单位 mV
*/
uint16_t bsp_vpwr_mv(void)
{
return (uint16_t)(adc_get_voltage(AD_CH_LDO5V) * 4);
}
/**
* @brief USB 是否在位(以 ADC 为主,比较器为辅)
* @return 1=插入,0=未插入
*/
uint8_t bsp_vpwr_is_online(void)
{
#if BOARD_CHG_ENABLE
uint16_t mv = bsp_vpwr_mv();
/* 以 ADC 为准:拔电后比较器可能仍为 1,但电压已经掉到几百 mV */
if (mv >= BOARD_VPWR_ONLINE_MV) {
return 1;
}
if (mv <= BOARD_VPWR_OFF_MV) {
return 0;
}
if (get_ldo5v_online_hw() || get_lvcmp_det()) {
return 1;
}
return 0;
#else
return 0;
#endif
}
/**
* @brief 进深睡前把电机 PWM 关掉并拉低,灯全灭
* @return 无
*/
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);
#endif
bsp_led_all_off();
}
+122
View File
@@ -0,0 +1,122 @@
/******************************************************************************
* @file bsp_hw.h
* @brief CBC2 板级硬件:电机 / LED / 按键 / 充电 / 电量
* @author cyWu <1917507415@qq.com>
* @date 2026.08.24
* @version V1.4.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, 增加按占空比驱动,供玩法变速
******************************************************************************/
#ifndef __BSP_HW_H__
#define __BSP_HW_H__
#include <stdint.h>
#include "board_pin.h"
typedef enum {
BSP_HW_OK = 0,
BSP_HW_ERR_PARAM,
BSP_HW_ERR_NOT_INIT
} BspHw_Ret_t;
typedef enum {
BSP_MOTOR_STOP = 0, /**< 滑行停(INA/INB 都低) */
BSP_MOTOR_IN, /**< 收绳(顺时针,见 BOARD_MOTOR_IN_INA_HIGH */
BSP_MOTOR_OUT /**< 放绳 */
} BspMotorDir_t;
/**
* @brief 初始化 GPIO / 充电 ADC
* @return BSP_HW_OK=成功
*/
BspHw_Ret_t bsp_hw_init(void);
/**
* @brief 查询是否已初始化
* @return BSP_HW_OK=已初始化,BSP_HW_ERR_NOT_INIT=未初始化
*/
BspHw_Ret_t bsp_hw_get_status(void);
/**
* @brief 红灯开关
* @param on 1=亮,0=灭
* @return 无
*/
void bsp_led_red_set(uint8_t on);
/**
* @brief 绿灯开关
* @param on 1=亮,0=灭
* @return 无
*/
void bsp_led_green_set(uint8_t on);
/**
* @brief 红绿灯全灭
* @return 无
*/
void bsp_led_all_off(void);
/**
* @brief 设置电机方向。STOP 时若正在转,先反转刹车再滑行
* @note 占空比用 BOARD_MOTOR_PWM_DUTY(手动点动)
* @param dir 收绳 / 放绳 / 停转
* @return 无
*/
void bsp_motor_set(BspMotorDir_t dir);
/**
* @brief 设置电机方向与 PWM 占空比
* @param dir 收绳 / 放绳 / 停转
* @param duty 占空比 0~100000%~100%);STOP 时忽略
* @return 无
*/
void bsp_motor_set_pwm(BspMotorDir_t dir, uint16_t duty);
/**
* @brief 推进换向死区与停转反转刹车,按真实毫秒计时
* @return 无
*/
void bsp_motor_brake_tick(void);
/**
* @brief 读取按键
* @return 1=按下,0=未按下
*/
uint8_t bsp_key_is_pressed(void);
/**
* @brief 读取 ME4054 CHG:充电中为低
* @return 1=充电中,0=未充电
*/
uint8_t bsp_chg_is_low(void);
/**
* @brief 读取电池电压
* @return 电池电压,单位 mV
*/
uint16_t bsp_vbat_mv(void);
/**
* @brief 读取 5V 口电压,用于判断 USB
* @return VPWR 电压,单位 mV
*/
uint16_t bsp_vpwr_mv(void);
/**
* @brief USB 是否在位(以 ADC 为主,比较器为辅)
* @return 1=插入,0=未插入
*/
uint8_t bsp_vpwr_is_online(void);
/**
* @brief 进深睡前把电机 PWM 关掉并拉低,灯全灭
* @return 无
*/
void bsp_hw_enter_sleep_io(void);
#endif /* __BSP_HW_H__ */
+12 -9
View File
@@ -64,7 +64,7 @@ static void usr_rtc_save_tz(int32_t tz_s);
/**
* @brief 打开 rtc 设备并加载时区
* @return UsrRtc_Ret_t
* @return USR_RTC_OK=成功,USR_RTC_ERR_NOT_INIT=设备打开失败或 RTC 未使能
*/
UsrRtc_Ret_t usr_rtc_init(void)
{
@@ -101,7 +101,7 @@ UsrRtc_Ret_t usr_rtc_init(void)
/**
* @brief 获取当前本地时间
* @param t 成功时赋值;失败不改动
* @return 1/0
* @return 1=已对时且读成功,0=未初始化/未对时/参数非法
*/
uint8_t usr_rtc_get_time(struct sys_time *t)
{
@@ -127,7 +127,7 @@ uint8_t usr_rtc_get_time(struct sys_time *t)
* @brief UTC 秒 + 时区 → 本地日历写入 RTC
* @param unix_utc UTC 秒
* @param tz_s 时区偏移秒
* @return UsrRtc_Ret_t
* @return USR_RTC_OK=成功,USR_RTC_ERR_NOT_INIT=未初始化,USR_RTC_ERR_PARAM=参数非法,USR_RTC_ERR_INVALID=年份非法
*/
UsrRtc_Ret_t usr_rtc_set_from_unix(uint64_t unix_utc, int32_t tz_s)
{
@@ -184,7 +184,7 @@ UsrRtc_Ret_t usr_rtc_set_from_unix(uint64_t unix_utc, int32_t tz_s)
/**
* @brief 写硬件日历
* @param t 本地日历
* @return UsrRtc_Ret_t
* @return USR_RTC_OK=成功,USR_RTC_ERR_PARAM=参数非法,USR_RTC_ERR_NOT_INIT=未初始化
*/
static UsrRtc_Ret_t usr_rtc_write(const struct sys_time *t)
{
@@ -216,7 +216,7 @@ static UsrRtc_Ret_t usr_rtc_write(const struct sys_time *t)
/**
* @brief 是否闰年
* @param year 公历年
* @return 1/0
* @return 1=闰年,0=平年
*/
static uint8_t usr_rtc_is_leap(uint16_t year)
{
@@ -235,7 +235,7 @@ static uint8_t usr_rtc_is_leap(uint16_t year)
* @brief 某月天数
* @param year 年
* @param month 月 1~12
* @return 天数非法月返回 0
* @return 该月天数非法月返回 0
*/
static uint8_t usr_rtc_days_in_month(uint16_t year, uint8_t month)
{
@@ -256,7 +256,7 @@ static uint8_t usr_rtc_days_in_month(uint16_t year, uint8_t month)
/**
* @brief 年份是否在合法窗口
* @param year 年
* @return 1/0
* @return 1=合法,0=非法
*/
static uint8_t usr_rtc_year_ok(uint16_t year)
{
@@ -266,8 +266,9 @@ static uint8_t usr_rtc_year_ok(uint16_t year)
/**
* @brief UTC 秒 + 时区 → 本地日历
* @param unix_utc UTC 秒
* @param tz_s 时区偏移
* @param t 输出
* @param tz_s 时区偏移
* @param t 输出本地日历
* @return 无
*/
static void usr_rtc_unix_to_calendar(uint32_t unix_utc, int32_t tz_s,
struct sys_time *t)
@@ -324,6 +325,7 @@ static void usr_rtc_unix_to_calendar(uint32_t unix_utc, int32_t tz_s,
/**
* @brief 从 VM 加载时区
* @return 无
*/
static void usr_rtc_load_tz(void)
{
@@ -350,6 +352,7 @@ static void usr_rtc_load_tz(void)
/**
* @brief 时区写入 VM
* @param tz_s 时区偏移秒
* @return 无
*/
static void usr_rtc_save_tz(int32_t tz_s)
{
+3 -4
View File
@@ -40,9 +40,8 @@ typedef enum {
/*============================================================================*/
/**
* @brief 打开 "rtc" 设备并加载时区
* @return UsrRtc_Ret_t
* @note app_main 启动时调用一次即可
* @brief 打开 rtc 设备并加载时区
* @return USR_RTC_OK=成功,USR_RTC_ERR_NOT_INIT=设备打开失败或 RTC 未使能
*/
UsrRtc_Ret_t usr_rtc_init(void);
@@ -57,7 +56,7 @@ uint8_t usr_rtc_get_time(struct sys_time *t);
* @brief App/网关 DEVICEINFOUTC 秒 + 时区写入 RTC
* @param unix_utc UTC Unix 秒;0 无效
* @param tz_s 时区偏移秒(东八区 28800)
* @return UsrRtc_Ret_t
* @return USR_RTC_OK=成功,USR_RTC_ERR_NOT_INIT=未初始化,USR_RTC_ERR_PARAM=参数非法,USR_RTC_ERR_INVALID=年份非法
*/
UsrRtc_Ret_t usr_rtc_set_from_unix(uint64_t unix_utc, int32_t tz_s);