feat(can): 增加EEPROM状态码和系统配置保存功能
- 在CAN通信中增加EEPROM状态码字段,并通过CAN FD发送 - 实现Flash EEPROM对系统配置参数的CRC校验与保存 - 改进目标角度、速度等关键参数的读写逻辑,支持掉电保存 - 调整EEPROM地址映射,优化错误处理和状态反馈机制 - 修复CRC计算相关问题,增强数据可靠性
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
#include "CRC.h"
|
||||
#include <cstring>
|
||||
#include <stdint.h>
|
||||
|
||||
CRC_Calculator CRC_Calculator;
|
||||
// 静态成员初始化
|
||||
bool CRC_Calculator::table_initialized = false;
|
||||
u32 CRC_Calculator::crc_table[256];
|
||||
|
||||
/**
|
||||
* @brief 初始化CRC表
|
||||
*/
|
||||
void CRC_Calculator::init_crc_table() {
|
||||
if (table_initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t polynomial = 0xEDB88320; // CRC-32 IEEE 802.3标准多项式
|
||||
|
||||
for (int i = 0; i < 256; i++) {
|
||||
uint32_t crc = i;
|
||||
for (int j = 0; j < 8; j++) {
|
||||
if (crc & 1) {
|
||||
crc = (crc >> 1) ^ polynomial;
|
||||
} else {
|
||||
crc >>= 1;
|
||||
}
|
||||
}
|
||||
crc_table[i] = crc;
|
||||
}
|
||||
|
||||
table_initialized = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 更新CRC值
|
||||
* @param crc 当前CRC值
|
||||
* @param data 要处理的数据
|
||||
* @return 更新后的CRC值
|
||||
*/
|
||||
uint32_t CRC_Calculator::update_crc(uint32_t crc, uint8_t data) {
|
||||
// 确保表已初始化
|
||||
if (!table_initialized) {
|
||||
init_crc_table();
|
||||
}
|
||||
|
||||
uint8_t table_index = (crc ^ data) & 0xFF;
|
||||
return (crc >> 8) ^ crc_table[table_index];
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 计算单个float值的CRC32
|
||||
* @param data float值
|
||||
* @return CRC32校验值
|
||||
*/
|
||||
uint32_t CRC_Calculator::calculate(const float& data) {
|
||||
// 强制初始化CRC表(如果尚未初始化)
|
||||
if (!table_initialized) {
|
||||
init_crc_table();
|
||||
}
|
||||
|
||||
uint32_t crc = 0xFFFFFFFF;
|
||||
|
||||
// 将float值按字节处理
|
||||
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&data);
|
||||
for (size_t i = 0; i < sizeof(float); i++) {
|
||||
crc = update_crc(crc, bytes[i]);
|
||||
}
|
||||
|
||||
return crc ^ 0xFFFFFFFF;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 计算float数组的CRC32
|
||||
* @param data float数组指针
|
||||
* @param length 数组长度
|
||||
* @return CRC32校验值
|
||||
*/
|
||||
uint32_t CRC_Calculator::calculate(const float* data, size_t length) {
|
||||
// 强制初始化CRC表(如果尚未初始化)
|
||||
if (!table_initialized) {
|
||||
init_crc_table();
|
||||
}
|
||||
|
||||
uint32_t crc = 0xFFFFFFFF;
|
||||
|
||||
// 处理每个float值
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&(data[i]));
|
||||
for (size_t j = 0; j < sizeof(float); j++) {
|
||||
crc = update_crc(crc, bytes[j]);
|
||||
}
|
||||
}
|
||||
|
||||
return crc ^ 0xFFFFFFFF;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 验证单个float值的CRC32
|
||||
* @param data float值
|
||||
* @param expected_crc 期望的CRC值
|
||||
* @return true表示校验成功,false表示校验失败
|
||||
*/
|
||||
bool CRC_Calculator::verify(const float& data, uint32_t expected_crc) {
|
||||
return calculate(data) == expected_crc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 验证float数组的CRC32
|
||||
* @param data float数组指针
|
||||
* @param length 数组长度
|
||||
* @param expected_crc 期望的CRC值
|
||||
* @return true表示校验成功,false表示校验失败
|
||||
*/
|
||||
bool CRC_Calculator::verify(const float* data, size_t length, uint32_t expected_crc) {
|
||||
return calculate(data, length) == expected_crc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 获取CRC表初始化状态
|
||||
* @return true表示已初始化,false表示未初始化
|
||||
*/
|
||||
bool CRC_Calculator::isInitialized() {
|
||||
return table_initialized;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 强制重新初始化CRC表
|
||||
*/
|
||||
void CRC_Calculator::reinitialize() {
|
||||
table_initialized = false;
|
||||
init_crc_table();
|
||||
}
|
||||
Reference in New Issue
Block a user