feat(CAN): 实现多节点CAN总线通信与节点ID管理功能

- 添加CAN调试宏定义,支持条件编译调试信息输出
- 实现节点ID相关功能,包括构建CAN ID、消息过滤、获取/设置节点ID
- 支持Flash存储节点ID,提供加载和保存功能
- 实现节点ID设置和查询命令处理
- 更新CANFD发送函数使用节点ID构建发送ID
- 添加消息过滤逻辑,只处理目标节点的消息
- 重构DMA中断服务程序,优化数据传输处理
- 替换printf为调试宏,便于生产环境控制调试输出
This commit is contained in:
cbb
2026-03-15 02:38:50 +08:00
parent 2b93a4537c
commit 9f56ab394e
10 changed files with 751 additions and 461 deletions
+55 -43
View File
@@ -11,7 +11,21 @@
#include <cstdio>
#include <cstring>
extern CAN_transmiter CAN0_transmiter; // CAN初始化
// OTA 调试宏定义
#if OTA_DEBUG
#define OTA_DEBUG_PRINT(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define OTA_DEBUG_PRINT(fmt, ...) ((void)0)
#endif
// EEP 调试宏定义
#if EEP_DEBUG
#define EEP_DEBUG_PRINT(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define EEP_DEBUG_PRINT(fmt, ...) ((void)0)
#endif
extern CAN_transmiter CAN0_transmiter; // CAN 初始化
VersionManager Version;
VersionManager::VersionManager() {
@@ -74,11 +88,11 @@ char *VersionManager::versionToString(const VersionInfo &version, char *buffer,
void VersionManager::sendVersionInfoViaCan() const {
// 准备发送版本信息 (ID: CANFD_VERSION_ID)
// 只使用必要的字节数(24字节)来传输三个版本的信息(每个版本6字节)
// 只使用必要的字节数(24 字节)来传输三个版本的信息(每个版本 6 字节)
u8 versionData[24] = {0};
// 将版本信息打包到24字节数组中
// 前8字节:软件版本信息 (4个uint16_t,每个占2字节)
// 将版本信息打包到 24 字节数组中
// 前 8 字节:软件版本信息 (4 个 uint16_t,每个占 2 字节)
versionData[0] = softwareVersion.major & 0xFF; // 低字节
versionData[1] = (softwareVersion.major >> 8) & 0xFF; // 高字节
versionData[2] = softwareVersion.minor & 0xFF; // 低字节
@@ -88,7 +102,7 @@ void VersionManager::sendVersionInfoViaCan() const {
versionData[6] = softwareVersion.build & 0xFF; // 低字节
versionData[7] = (softwareVersion.build >> 8) & 0xFF; // 高字节
// 8-15字节:硬件版本信息 (4个uint16_t,每个占2字节)
// 8-15 字节:硬件版本信息 (4 个 uint16_t,每个占 2 字节)
versionData[8] = hardwareVersion.major & 0xFF; // 低字节
versionData[9] = (hardwareVersion.major >> 8) & 0xFF; // 高字节
versionData[10] = hardwareVersion.minor & 0xFF; // 低字节
@@ -98,7 +112,7 @@ void VersionManager::sendVersionInfoViaCan() const {
versionData[14] = hardwareVersion.build & 0xFF; // 低字节
versionData[15] = (hardwareVersion.build >> 8) & 0xFF; // 高字节
// 16-23字节:BOOT版本信息 (4个uint16_t,每个占2字节)
// 16-23 字节:BOOT 版本信息 (4 个 uint16_t,每个占 2 字节)
versionData[16] = bootVersion.major & 0xFF; // 低字节
versionData[17] = (bootVersion.major >> 8) & 0xFF; // 高字节
versionData[18] = bootVersion.minor & 0xFF; // 低字节
@@ -108,21 +122,24 @@ void VersionManager::sendVersionInfoViaCan() const {
versionData[22] = bootVersion.build & 0xFF; // 低字节
versionData[23] = (bootVersion.build >> 8) & 0xFF; // 高字节
// 使用CANFD发送函数发送版本信息
CAN0_transmiter.CANFD_AsyncSend(CANFD_VERSION_ID, 24, versionData);
// 使用 CANFD 发送函数发送版本信息,使用 build_can_id 构建带 node_id 的 CAN
// ID
CAN0_transmiter.CANFD_AsyncSend(
CAN0_transmiter.build_can_id(CANFD_VERSION_ID), 24, versionData);
}
void VersionManager::printVersions() const {
char versionStr[32];
printf(
OTA_DEBUG_PRINT(
"Software Version: %s\r\n",
versionToString(getSoftwareVersion(), versionStr, sizeof(versionStr)));
printf(
OTA_DEBUG_PRINT(
"Hardware Version: %s\r\n",
versionToString(getHardwareVersion(), versionStr, sizeof(versionStr)));
printf("BOOT Version: %s\r\n",
versionToString(getBootVersion(), versionStr, sizeof(versionStr)));
OTA_DEBUG_PRINT(
"BOOT Version: %s\r\n",
versionToString(getBootVersion(), versionStr, sizeof(versionStr)));
}
/* retarget the C library printf function to the USART */
@@ -154,9 +171,9 @@ int fputc(int ch, FILE *f) throw() {
}
void unused_pin_init() {
rcu_periph_clock_enable(RCU_GPIOA); // 使能GPIOA时钟
rcu_periph_clock_enable(RCU_GPIOB); // 使能GPIOB时钟
rcu_periph_clock_enable(RCU_GPIOC); // 使能GPIOC时钟
rcu_periph_clock_enable(RCU_GPIOA); // 使能 GPIOA 时钟
rcu_periph_clock_enable(RCU_GPIOB); // 使能 GPIOB 时钟
rcu_periph_clock_enable(RCU_GPIOC); // 使能 GPIOC 时钟
gpio_init(GPIOA, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ,
GPIO_PIN_8 | GPIO_PIN_11 | GPIO_PIN_12);
gpio_init(GPIOB, GPIO_MODE_IPU, GPIO_OSPEED_2MHZ,
@@ -177,43 +194,43 @@ void unused_pin_init() {
u16 get_reset_cause(void) {
u16 cause = 0;
if (RESET != rcu_flag_get(RCU_FLAG_FWDGTRST)) {
// printf("Free Watchdog Timer Reset\r\n");
// OTA_DEBUG_PRINT("Free Watchdog Timer Reset\r\n");
cause = RCU_FLAG_FWDGTRST;
}
if (RESET != rcu_flag_get(RCU_FLAG_EPRST)) {
// printf("External Pin Reset\r\n");
// OTA_DEBUG_PRINT("External Pin Reset\r\n");
cause = RCU_FLAG_EPRST;
}
if (RESET != rcu_flag_get(RCU_FLAG_PORRST)) {
// printf("Power Reset\r\n");
// OTA_DEBUG_PRINT("Power Reset\r\n");
cause = RCU_FLAG_PORRST;
}
if (RESET != rcu_flag_get(RCU_FLAG_WWDGTRST)) {
// printf("Window Watchdog Timer Reset\r\n");
// OTA_DEBUG_PRINT("Window Watchdog Timer Reset\r\n");
cause = RCU_FLAG_WWDGTRST;
}
if (RESET != rcu_flag_get(RCU_FLAG_LPRST)) {
// printf("Low Power Reset\r\n");
// OTA_DEBUG_PRINT("Low Power Reset\r\n");
cause = RCU_FLAG_LPRST;
}
if (RESET != rcu_flag_get(RCU_FLAG_SWRST)) {
// printf("Software Reset\r\n");
// OTA_DEBUG_PRINT("Software Reset\r\n");
cause = RCU_FLAG_SWRST;
}
// 清除所有复位标志
rcu_all_reset_flag_clear();
// printf("cause:%d\r\n", cause);
// OTA_DEBUG_PRINT("cause:%d\r\n", cause);
return cause;
}
void OTA_initial(void) {
#if OTA_DEBUG
u32 temp_val[6];
// 3. 对于需要将0改回1的操作,必须先擦除整个Flash页
// 3. 对于需要将 0 改回 1 的操作,必须先擦除整个 Flash 页
volatile u32 *flash_ptr1 = (volatile u32 *)n_APP_LATEST_AVAL_ADDR;
temp_val[0] = *flash_ptr1;
flash_ptr1 = (volatile u32 *)n_APP_BACKUP_AVAL_ADDR;
@@ -226,20 +243,19 @@ void OTA_initial(void) {
temp_val[4] = *flash_ptr1;
flash_ptr1 = (volatile u32 *)n_APP_BACKUP_CHECK_ADDR;
temp_val[5] = *flash_ptr1;
// 修复:完整输出所有6个配置项的值,便于调试
printf("APP:CHECK LatestAval:0x%x, BackupAval:0x%x, LatestEn:0x%x, "
"BackupEn:0x%x, LatestCheck:0x%x, BackupCheck:0x%x\r\n",
temp_val[0], temp_val[1], temp_val[2], temp_val[3], temp_val[4],
temp_val[5]);
// 修复:完整输出所有 6 个配置项的值,便于调试
OTA_DEBUG_PRINT(
"APP:CHECK LatestAval:0x%x, BackupAval:0x%x, LatestEn:0x%x, "
"BackupEn:0x%x, LatestCheck:0x%x, BackupCheck:0x%x\r\n",
temp_val[0], temp_val[1], temp_val[2], temp_val[3], temp_val[4],
temp_val[5]);
#endif
// 确认当前APP可用
// 确认当前 APP 可用
volatile u32 *flash_ptr = (volatile u32 *)n_APP_LATEST_CHECK_ADDR;
u32 app_check = *flash_ptr;
if (app_check == OTA_NOT_AVAL) { // 新的APP刷入后第一次启动
#if OTA_DEBUG
std::printf("APP enabled\r\n");
#endif
if (app_check == OTA_NOT_AVAL) { // 新的 APP 刷入后第一次启动
OTA_DEBUG_PRINT("APP enabled\r\n");
move_app_data();
}
@@ -248,7 +264,7 @@ void OTA_initial(void) {
fmc_lock();
u8 transmit_data[2] = {0x0A, 0xAC};
// 确认APP可用
// 确认 APP 可用
CAN0_transmiter.CANFD_AsyncSend(CANFD_OTA_ACK_ID, 2, transmit_data);
flash_ptr = (volatile u32 *)n_APP_BACKUP_AVAL_ADDR;
@@ -256,18 +272,14 @@ void OTA_initial(void) {
flash_ptr = (volatile u32 *)n_APP_BACKUP_CHECK_ADDR;
u32 back_check = *flash_ptr;
if (back_aval == OTA_NOT_AVAL) { // 如果BACK不可用
#if OTA_DEBUG
std::printf("notaval\r\n");
#endif
if (back_aval == OTA_NOT_AVAL) { // 如果 BACK 不可用
OTA_DEBUG_PRINT("notaval\r\n");
move_app_data();
} else if (back_aval == OTA_AVAL &&
back_check == OTA_NOT_AVAL) { // BACK已刷写,但未检查
#if OTA_DEBUG
std::printf("aval\r\n");
#endif
back_check == OTA_NOT_AVAL) { // BACK 已刷写,但未检查
OTA_DEBUG_PRINT("aval\r\n");
move_app_data();
} else {
printf("check\r\n");
EEP_DEBUG_PRINT("check\r\n");
}
}