移除调试打印语句,改进Flash读写稳定性。 使用类型别名统一数据访问,增强代码可读性。 修正CAN通信中角度保存逻辑,确保正确写入实时角度值。 完善错误处理机制,增加对齐检查和状态反馈。 调整EEPROM起始地址以避免潜在冲突。
67 lines
2.1 KiB
C++
67 lines
2.1 KiB
C++
#ifndef __FLASH_H
|
|
#define __FLASH_H
|
|
|
|
#include "gd32c10x.h"
|
|
#include <cstdint>
|
|
#include <stdint.h>
|
|
// 修改后 - 强制4字节对齐
|
|
#define ALIGN_4BYTE(addr) ((addr) & 0xFFFFFFFC) // 4字节对齐
|
|
|
|
// EEPROM操作状态
|
|
typedef enum {
|
|
FLASH_EEPROM_OK = 0, // 操作成功
|
|
FLASH_EEPROM_ERROR, // 操作失败
|
|
FLASH_EEPROM_NOT_FOUND, // 未找到指定数据
|
|
FLASH_EEPROM_INVALID_ADDR, // 无效地址
|
|
FLASH_EEPROM_WRITE_ERROR, // 写入错误
|
|
FLASH_EEPROM_ERASE_ERROR, // 擦除错误
|
|
FLASH_EEPROM_ALIGNMENT_ERROR,// 对齐错误
|
|
} FlashEepromStatus;
|
|
|
|
// Flash EEPROM类
|
|
class FlashEEPROM {
|
|
public:
|
|
// EEPROM emulation constants
|
|
static constexpr uint32_t EEPROM_START_ADDRESS =
|
|
0x0801F800; // Flash最后一页(2KB)
|
|
static constexpr uint32_t EEPROM_END_ADDRESS = 0x08020000; // Flash结束地址
|
|
static constexpr uint32_t EEPROM_PAGE_SIZE = 0x800; // 页大小2KB
|
|
static constexpr uint32_t EEPROM_MAX_SIZE = 0x800; // EEPROM最大容量
|
|
|
|
// 定义用户自定义的虚拟地址
|
|
static constexpr uint16_t ADDR_MOTOR_KP = 0x0010; // 电机KP参数地址
|
|
static constexpr uint16_t ADDR_MOTOR_KI = 0x0011; // 电机KI参数地址
|
|
static constexpr uint16_t ADDR_MOTOR_KD = 0x0012; // 电机KD参数地址
|
|
static constexpr uint16_t ADDR_SYSTEM_CONFIG = 0x0020; // 系统配置参数地址
|
|
static constexpr uint16_t ADDR_ERROR_COUNT = 0x0030; // 错误计数地址
|
|
|
|
// 角度数据存储地址
|
|
static constexpr u16 ADDR_ANGLE_DATA = 0x0013;
|
|
bool angle_saved = false;
|
|
// 构造函数
|
|
FlashEEPROM();
|
|
|
|
// 初始化Flash EEPROM模拟
|
|
u8 init();
|
|
|
|
// 从Flash EEPROM读取数据
|
|
FlashEepromStatus read(uint16_t virtualAddr, uint16_t *data) const;
|
|
|
|
// 向Flash EEPROM写入数据
|
|
FlashEepromStatus write(uint16_t virtualAddr, uint16_t data);
|
|
|
|
// 格式化Flash EEPROM页面
|
|
FlashEepromStatus format();
|
|
|
|
void flash_eeprom_example_init(void);
|
|
void update_pid_parameters(uint16_t kp, uint16_t ki, uint16_t kd);
|
|
void update_system_config(uint16_t config);
|
|
void increment_error_count(void);
|
|
FlashEepromStatus save_angle(u16 angle, bool saved);
|
|
float read_angle();
|
|
|
|
private:
|
|
};
|
|
|
|
void flash_eeprom_example_main(void);
|
|
#endif /* __FLASH_H */ |