请提供C语言代码:把GD32F150 的flash 当成EEPROM ,请完成(INA219 的校准参数(2byte)、5个电压线性修正系数、产品序列号(16byte)、产品lic(16byte)) 各个部分独立的读取/修改函数。请考虑到删除操作会删除整个page
时间: 2023-03-29 07:04:46 浏览: 98
以下是代码示例:
// 定义EEPROM的起始地址
#define EEPROM_START_ADDRESS x080F800
// 定义每个page的大小
#define PAGE_SIZE 2048
// 定义每个部分的偏移量
#define CALIBRATION_PARAM_OFFSET
#define VOLTAGE_CORRECTION_COEFFICIENT_OFFSET 2
#define PRODUCT_SERIAL_NUMBER_OFFSET 12
#define PRODUCT_LIC_OFFSET 28
// 定义每个部分的长度
#define CALIBRATION_PARAM_LENGTH 2
#define VOLTAGE_CORRECTION_COEFFICIENT_LENGTH 10
#define PRODUCT_SERIAL_NUMBER_LENGTH 16
#define PRODUCT_LIC_LENGTH 16
// 读取校准参数
void read_calibration_param(uint16_t *calibration_param) {
memcpy(calibration_param, (uint16_t *)(EEPROM_START_ADDRESS + CALIBRATION_PARAM_OFFSET), CALIBRATION_PARAM_LENGTH);
}
// 写入校准参数
void write_calibration_param(uint16_t *calibration_param) {
FLASH_Unlock();
FLASH_ErasePage(EEPROM_START_ADDRESS);
FLASH_ProgramHalfWord(EEPROM_START_ADDRESS + CALIBRATION_PARAM_OFFSET, *calibration_param);
FLASH_Lock();
}
// 读取电压线性修正系数
void read_voltage_correction_coefficient(uint16_t *voltage_correction_coefficient) {
memcpy(voltage_correction_coefficient, (uint16_t *)(EEPROM_START_ADDRESS + VOLTAGE_CORRECTION_COEFFICIENT_OFFSET), VOLTAGE_CORRECTION_COEFFICIENT_LENGTH);
}
// 写入电压线性修正系数
void write_voltage_correction_coefficient(uint16_t *voltage_correction_coefficient) {
FLASH_Unlock();
FLASH_ErasePage(EEPROM_START_ADDRESS);
FLASH_ProgramHalfWord(EEPROM_START_ADDRESS + VOLTAGE_CORRECTION_COEFFICIENT_OFFSET, *voltage_correction_coefficient);
FLASH_Lock();
}
// 读取产品序列号
void read_product_serial_number(uint8_t *product_serial_number) {
memcpy(product_serial_number, (uint8_t *)(EEPROM_START_ADDRESS + PRODUCT_SERIAL_NUMBER_OFFSET), PRODUCT_SERIAL_NUMBER_LENGTH);
}
// 写入产品序列号
void write_product_serial_number(uint8_t *product_serial_number) {
FLASH_Unlock();
FLASH_ErasePage(EEPROM_START_ADDRESS);
memcpy((uint8_t *)(EEPROM_START_ADDRESS + PRODUCT_SERIAL_NUMBER_OFFSET), product_serial_number, PRODUCT_SERIAL_NUMBER_LENGTH);
FLASH_Lock();
}
// 读取产品lic
void read_product_lic(uint8_t *product_lic) {
memcpy(product_lic, (uint8_t *)(EEPROM_START_ADDRESS + PRODUCT_LIC_OFFSET), PRODUCT_LIC_LENGTH);
}
// 写入产品lic
void write_product_lic(uint8_t *product_lic) {
FLASH_Unlock();
FLASH_ErasePage(EEPROM_START_ADDRESS);
memcpy((uint8_t *)(EEPROM_START_ADDRESS + PRODUCT_LIC_OFFSET), product_lic, PRODUCT_LIC_LENGTH);
FLASH_Lock();
}
// 删除EEPROM
void delete_eeprom() {
FLASH_Unlock();
FLASH_ErasePage(EEPROM_START_ADDRESS);
FLASH_Lock();
}
阅读全文