2025-12-16 22:39:39 +08:00
|
|
|
#ifndef __FLASH_H
|
|
|
|
|
#define __FLASH_H
|
|
|
|
|
|
|
|
|
|
#include "gd32c10x.h"
|
|
|
|
|
#include <cstdint>
|
2025-12-17 01:38:48 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
|
// 修改后 - 强制4字节对齐
|
|
|
|
|
#define ALIGN_4BYTE(addr) ((addr) & 0xFFFFFFFC) // 4字节对齐
|
2025-12-16 22:39:39 +08:00
|
|
|
|
|
|
|
|
// EEPROM操作状态
|
|
|
|
|
typedef enum {
|
|
|
|
|
FLASH_EEPROM_OK = 0, // 操作成功
|
|
|
|
|
FLASH_EEPROM_ERROR, // 操作失败
|
|
|
|
|
FLASH_EEPROM_NOT_FOUND, // 未找到指定数据
|
|
|
|
|
FLASH_EEPROM_INVALID_ADDR, // 无效地址
|
|
|
|
|
FLASH_EEPROM_WRITE_ERROR, // 写入错误
|
2025-12-17 01:38:48 +08:00
|
|
|
FLASH_EEPROM_ERASE_ERROR, // 擦除错误
|
|
|
|
|
FLASH_EEPROM_ALIGNMENT_ERROR,// 对齐错误
|
2025-12-16 22:39:39 +08:00
|
|
|
} FlashEepromStatus;
|
|
|
|
|
|
|
|
|
|
// Flash EEPROM类
|
|
|
|
|
class FlashEEPROM {
|
2025-12-17 01:38:48 +08:00
|
|
|
public:
|
|
|
|
|
// EEPROM emulation constants
|
2025-12-16 22:39:39 +08:00
|
|
|
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; // 错误计数地址
|
|
|
|
|
|
|
|
|
|
// 角度数据存储地址
|
2025-12-17 01:38:48 +08:00
|
|
|
static constexpr u16 ADDR_ANGLE_DATA = 0x0013;
|
2025-12-16 22:39:39 +08:00
|
|
|
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);
|
2025-12-17 01:38:48 +08:00
|
|
|
FlashEepromStatus save_angle(u16 angle, bool saved);
|
2025-12-16 22:39:39 +08:00
|
|
|
float read_angle();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void flash_eeprom_example_main(void);
|
|
|
|
|
#endif /* __FLASH_H */
|