feat(can): 增加CAN睡眠与唤醒功能并优化通信检查逻辑

- 在CAN通信类中添加了CAN_Sleep()和CAN_Wakeup()方法,支持设备进入低功耗模式
- 优化CAN通信检查逻辑,通过解析接收数据的首字节和末字节实现多种控制命令
- 支持远程复位电机、保存角度、清零角度、进入睡眠等操作
- 解决CAN发送邮箱满时可能导致的通信阻塞问题,增加重初始化机制
- 调整mailbox不可用计数逻辑,防止持续打印错误信息

refactor(flash): 移除软件CRC计算模块,改用硬件CRC外设
- 删除自定义CRC.cpp和CRC.h文件及其相关类实现
- 使用GD32硬件CRC模块替代软件算法,提高效率和可靠性
- 更新FlashEEPROM类中的CRC校验方式,调用crc_block_data_calculate进行计算
- 修正Flash地址遍历范围以适配新CRC逻辑及页面管理策略
- 修改save_angle接口签名,不再手动传入角度值,改为内部读取FOC实时角度

feat(timer): 新增RTC闹钟中断处理函数声明

- 在Timer.h中声明rtc_set_wakeup_interval和RTC_ALARM_IRQHandler
- 为后续实现基于RTC的定时唤醒功能做准备

chore(config): 同步csolution工具版本更新并调整组件配置

- 更新RTE_Components.h生成工具版本至2.12.0
- 启用BKP、CRC、RTC等标准外设组件宏定义
- 从项目配置文件中移除旧CRC源文件引用
- 添加SystemCoreClockUpdate调用确保系统时钟同步

refactor(main): 主循环任务调度重构并增强初始化流程

- 将主循环中的任务处理提取为独立do_task函数,提升代码可读性
- 提前执行电机初始化,并根据看门狗复位标志决定是否立即进入深度睡眠
- 调整EEPROM初始化顺序,保证依赖关系正确
- 添加sys/initial.h头文件包含以支持新增系统初始化逻辑
This commit is contained in:
cbb
2025-12-20 19:53:52 +08:00
parent 7c2860f302
commit 7354a71c51
19 changed files with 366 additions and 418 deletions
+54 -6
View File
@@ -3,11 +3,11 @@
#include "Filter.h"
#include "adc.h"
#include "ang_sensor.h"
#include "core_cmFunc.h"
#include "flash.h"
#include "gd32c10x.h"
#include "gd32c10x_can.h"
#include "gd32c10x_dma.h"
#include "gd32c10x_gpio.h"
#include "motor.h"
#include "systick.h"
#include <cstdio>
@@ -28,7 +28,8 @@ CAN_transmiter CAN0_transmiter; // CAN初始化
extern FOC_Controlroller FOC;
extern sampler sampler;
extern angle_sensor KTH7823;
extern EncoderVelocityKalman Vel_kalman;
extern EncoderAngleKalman Angle_kalman; // 角度滤波
extern EncoderVelocityKalman Vel_kalman; // 速度滤波
extern FlashEEPROM flashEEP;
// 添加DMA相关变量
@@ -566,8 +567,14 @@ u8 CAN_transmiter::CANFD_Send(u32 id, u8 length, u8 *data) {
// 检查是否成功获取mailbox
if (CAN_NOMAILBOX == transmit_mailbox) {
static u32 count = 0;
// 没有可用的mailbox,可能所有mailbox都在使用中
count++;
printf("No available mailbox for CANFD transmission\r\n");
if (count > 0xff and !is_sleep) {
init();
count = 0;
}
return 0;
}
return 1;
@@ -695,10 +702,27 @@ void CAN_transmiter::communication_check() {
unpack_bytes_to_float(receive_message.rx_data, 11).f;
FOC.target_velocity = target_velocity_union.f;
} else if (receive_message.rx_efid == 0x00010301) {
motor_reset();
flashEEP.save_angle(FOC.rt_angle, false);
flashEEP.save_system_config(false);
u8 first_byte = receive_message.rx_data[0];
u8 eigth_byte = receive_message.rx_data[7];
if (first_byte & 0x01) {
motor_reset();
}
if (first_byte >> 1 & 0x01) {
flashEEP.save_angle(false);
}
if (first_byte >> 2 & 0x01) {
flashEEP.save_system_config(false);
}
if (first_byte >> 3 & 0x01) {
KTH7823.angle_set(0);
Angle_kalman.init(0); // 角度滤波
Vel_kalman.init(0); // 角度滤波
}
if (eigth_byte & 0x80 >> 7) {
prepare_deep_sleep(true);
// CAN_Sleep();
}
} else {
printf("Unknown CAN ID: %X\n", receive_message.rx_efid);
}
@@ -706,6 +730,30 @@ void CAN_transmiter::communication_check() {
}
}
/**
* @brief CAN睡眠
* @param None
* @retval None
*/
u8 CAN_transmiter::CAN_Sleep() {
gpio_bit_set(GPIOB, GPIO_PIN_13);
is_sleep = true;
return 0;
}
/**
* @brief CAN唤醒
* @param None
* @retval None
*/
u8 CAN_transmiter::CAN_Wakeup() {
printf("can wake\r\n");
is_sleep = false;
canfd_config();
return 0;
}
/**
* @brief CAN0中断服务程序
* @retval 无
+35 -31
View File
@@ -53,11 +53,11 @@ static inline float_union_t unpack_bytes_to_float(u8 *buffer, u16 offset) {
return float_union;
}
u8 can_gpio_init(void); // CAN引脚初始化
void can_config(void); // CAN初始化
void canfd_config(void);// CANFD初始化
void can_dma_init(void); // CAN DMA初始化
void CAN0_RX0_IRQHandler(void);// CAN接收中断处理函数
u8 can_gpio_init(void); // CAN引脚初始化
void can_config(void); // CAN初始化
void canfd_config(void); // CANFD初始化
void can_dma_init(void); // CAN DMA初始化
void CAN0_RX0_IRQHandler(void); // CAN接收中断处理函数
void DMA0_Channel4_IRQHandler(void); // DMA通道4中断处理函数(CAN接收)
void DMA0_Channel5_IRQHandler(void); // DMA通道5中断处理函数(CAN发送)
@@ -71,47 +71,51 @@ void can_loopback_test(void);
class CAN_transmiter {
private:
u8 controller_Mode = 0; // 电机模式
float_union_t limit_velocity_union = {0}; // 限制速度
u16 limit_voltage = 0; // 限制电压
short mcu_temp = 0; // MCU温度
short motor_temp = 0; // 电机温度
u16 Power_Voltage = 0; // 电源电压
float_union_t rt_angle_union = {0}; // 实时角度
float_union_t rt_velocity_union = {0}; // 实时速度
bool MotorStatus = 0; // 电机状态码
float_union_t target_angle_union = {0}; // 目标角度
u8 Target_Control_Mode = 0; // 目标控制模式
u8 EEPROMStatus = 0; // EEPROM状态码
float_union_t target_velocity_union = {0}; // 目标速度
float_union_t u_current_union = {0}; // U相电流
float_union_t w_current_union = {0}; // W相电流
float_union_t v_current_union = {0}; // V相电流
bool is_sleep = false; // 是否休眠
u8 controller_Mode = 0; // 电机模式
float_union_t limit_velocity_union = {0}; // 限制速度
u16 limit_voltage = 0; // 限制电压
short mcu_temp = 0; // MCU温度
short motor_temp = 0; // 电机温度
u16 Power_Voltage = 0; // 电源电压
float_union_t rt_angle_union = {0}; // 实时角度
float_union_t rt_velocity_union = {0}; // 实时速度
bool MotorStatus = 0; // 电机状态码
float_union_t target_angle_union = {0}; // 目标角度
u8 Target_Control_Mode = 0; // 目标控制模式
u8 EEPROMStatus = 0; // EEPROM状态码
float_union_t target_velocity_union = {0}; // 目标速度
float_union_t u_current_union = {0}; // U相电流
float_union_t w_current_union = {0}; // W相电流
float_union_t v_current_union = {0}; // V相电流
can_trasnmit_message_struct transmit_message; // 发送消息结构体
u8 transmit_mailbox = CAN_NOMAILBOX; // 初始化为无mailbox状态
u8 transmit_data[64]; // CANFD最大64字节
void update(); // 更新数据
void update(); // 更新数据
u8 CAN_Receive(); // CAN接收
u8 CANFD_Receive(); // CANFD接收
u8 CAN_Send(u32 ID, u8 length, u8 *data); // CAN发送
u8 CANFD_Send(u32 ID, u8 length, u8 *data); // CANFD发送
u8 CAN_all_Send(); // CAN发送
u8 CANFD_Send(u32 ID, u8 length, u8 *data); // CANFD发送
u8 CAN_all_Send(); // CAN发送
u8 CANFD_all_Send(); // CANFD发送
public:
// 添加构造函数声明
u8 init(void);
can_receive_message_struct receive_message; // 接收消息结构体
FlagStatus can0_receive_flag; // CAN0接收标志
FlagStatus can0_send_flag; // CAN0发送标志
float_union_t testfloat_union = {0}; // 测试用
can_receive_message_struct receive_message; // 接收消息结构体
FlagStatus can0_receive_flag; // CAN0接收标志
FlagStatus can0_send_flag; // CAN0发送标志
float_union_t testfloat_union = {0}; // 测试用
bool MagStatus = 0; // 磁状态码
bool ThermalStatus = 0; // 热能状态码
void communication_check(); // 通信检查
bool MagStatus = 0; // 磁状态码
bool ThermalStatus = 0; // 热能状态码
void communication_check(); // 通信检查
u8 CAN_Sleep(); // CAN进入睡眠
u8 CAN_Wakeup(); // CAN进入睡眠
};
#ifdef __cplusplus
-1
View File
@@ -97,7 +97,6 @@ void User_timer_init(void) {
nvic_config();
}
/**
* @brief TIMER2中断处理函数
* @return void
+2 -9
View File
@@ -216,7 +216,8 @@ u8 angle_sensor::init(void) {
* @return 0成功/1失败
*/
u8 angle_sensor::angle_set(float angle){
m_cumulativeAngle = flashEEP.read_angle()*REDUCTION_RATIO; // 从Flash读取上次保存的角度
m_cumulativeAngle = angle*REDUCTION_RATIO; // 从Flash读取上次保存的角度
m_firstUpdate = true;
return 0;
}
@@ -303,11 +304,3 @@ float angle_sensor::getRawAngle(void) const { return m_cumulativeAngle; }
float angle_sensor::getcurrent_sensorangle(void) const {
return current_angle_deg;
}
/**
* @brief 重置累积角度值
*/
void angle_sensor::reset(void) {
m_cumulativeAngle = 0.0f;
m_firstUpdate = true;
}