尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

多处冗余备份机制防止写入瞬间掉电而丢数据

多处冗余备份机制防止写入瞬间掉电而丢数据 多处冗余备份机制防止写入瞬间掉电而丢数据免责申明代码未经过严格验证请自行验证#include ../Middlewares/fatfs/w25q64.h#include core/net.h#include string.h#include ctype.h#include my_malloc.h#define rongyu_bak_canshu_bak_count 4#define rongyu_bak_canshu_unit_size 60000*40#define save_unit_length 40#define max_time_value 9999999u8 unit_buf1[save_unit_length * rongyu_bak_canshu_bak_count];u8 unit_buf1_for_write[save_unit_length];/* 缓存 */#define CACHE_SIZE 128typedef struct {int slot_index;char value[35];uint8_t valid;} slot_cache_t;static slot_cache_t *g_cache NULL; /* 改为指针 */void cache_init(void) {g_cache (slot_cache_t *)mymalloc(CACHE_SIZE * sizeof(slot_cache_t));memset(g_cache, 0, CACHE_SIZE * sizeof(slot_cache_t));}static uint8_t g_cache_next 0;static int cache_find(int slot_index) {for(int i 0; i CACHE_SIZE; i) {if(g_cache[i].valid g_cache[i].slot_index slot_index)return i;}return -1;}static void cache_set(int slot_index, const char *value) {/* 先查找是否已存在该 slot_index 的缓存直接更新 */int ci cache_find(slot_index);if(ci 0) {/* 更新已有记录 */strncpy(g_cache[ci].value, value, 34);g_cache[ci].value[34] \0;g_cache[ci].valid 1;return;}/* 不存在新增记录 */g_cache[g_cache_next].slot_index slot_index;strncpy(g_cache[g_cache_next].value, value, 34);g_cache[g_cache_next].value[34] \0;g_cache[g_cache_next].valid 1;g_cache_next (g_cache_next 1) % CACHE_SIZE;}void cache_invalidate(int slot_index) {for(int i 0; i CACHE_SIZE; i) {if(g_cache[i].valid g_cache[i].slot_index slot_index)g_cache[i].valid 0;}}/* 工具函数 */int is_all_digit(char* str) {for (int i 0; str[i] ! \0; i)if (!isdigit((unsigned char)str[i])) return 0;return 1;}/* 读单个槽位的时间戳4字节 */static uint32_t read_timestamp(int backup_idx, int canshuo_index) {uint32_t ts 0;uint32_t addr backup_idx * rongyu_bak_canshu_unit_size canshuo_index * save_unit_length 35;SPI_FLASH_BufferRead((u8*)ts, addr, 4, 0, 0);return ts;}/* 读单个槽位的值34字节 */static void read_value_only(int backup_idx, int canshuo_index, char *out) {uint32_t addr backup_idx * rongyu_bak_canshu_unit_size canshuo_index * save_unit_length;SPI_FLASH_BufferRead((u8*)out, addr, 34, 0, 0);out[34] \0;}/* 读单个槽位完整数据40字节 */static void read_slot_full(int backup_idx, int canshuo_index, u8 *buf) {uint32_t addr backup_idx * rongyu_bak_canshu_unit_size canshuo_index * save_unit_length;SPI_FLASH_BufferRead(buf, addr, save_unit_length, 0, 0);}/* 写操作 */void write_to_flash_do(int canshuo_index, u32 max_time_st, char* canshu_Name, char * canshu_value) {if(max_time_st (max_time_value - 3)) max_time_st 0;memset(unit_buf1_for_write, 0, save_unit_length);canshu_value[34] 0;strcpy((char*)unit_buf1_for_write, canshu_value);unit_buf1_for_write[35] max_time_st 0xff;unit_buf1_for_write[36] (max_time_st 8) 0xff;unit_buf1_for_write[37] (max_time_st 16) 0xff;unit_buf1_for_write[38] (max_time_st 24) 0xff;for(int i 0; i rongyu_bak_canshu_bak_count; i) {SPI_Flash_Write_by_earse_ok(unit_buf1_for_write,i * rongyu_bak_canshu_unit_size canshuo_index * save_unit_length,save_unit_length, 0);}/* 更新缓存 */cache_set(canshuo_index, canshu_value);}/* 快速判断槽位有效性 */static int is_slot_valid(int canshuo_index) {/* 先查缓存 */int ci cache_find(canshuo_index);if(ci 0) {return (g_cache[ci].value[0] ! 0 || g_cache[ci].value[1] ! \0);}/* 只读第0份备份的时间戳首字节 */uint32_t addr canshuo_index * save_unit_length;u8 first_bytes[2];SPI_FLASH_BufferRead(first_bytes, addr, 2, 0, 0);/* 值为0则无效 */if(first_bytes[0] 0 first_bytes[1] \0) {cache_set(canshuo_index, 0);return 0;}/* 读取时间戳判断是否擦除状态 */uint32_t ts read_timestamp(0, canshuo_index);if(ts max_time_value) { /* 0xFFFFFFFF 擦除态 */cache_set(canshuo_index, 0);return 0;}/* 有效需要读取完整值才能缓存 */return 1;}/* 快速读取值 */static void read_value_fast(int canshuo_index, char *out) {int ci cache_find(canshuo_index);if(ci 0) {strcpy(out, g_cache[ci].value);return;}/* 只读第0份备份的前34字节 */read_value_only(0, canshuo_index, out);/* 检查时间戳 */uint32_t ts read_timestamp(0, canshuo_index);if(ts max_time_value) {out[0] 0; out[1] \0;}cache_set(canshuo_index, out);}/* 读操作完整版需要时调用 */void read_a_canshu_with_bak(int canshuo_index, char* canshu_Name, char * out_canshu_str_value, unsigned int * out_value) {/* 先查缓存 */int ci cache_find(canshuo_index);if(ci 0) {strcpy(out_canshu_str_value, g_cache[ci].value);if(is_all_digit(out_canshu_str_value)) *out_value atoi(out_canshu_str_value);return;}/* 快速路径先只读所有份的时间戳每份4字节判断是否全部无效 */uint32_t ts_arr[rongyu_bak_canshu_bak_count];int all_invalid 1;int best_idx 0;uint32_t max_valid_ts 0;for(int i 0; i rongyu_bak_canshu_bak_count; i) {ts_arr[i] read_timestamp(i, canshuo_index);if(ts_arr[i] max_time_value) {all_invalid 0;if(ts_arr[i] max_valid_ts) {max_valid_ts ts_arr[i];best_idx i;}}}/* 如果所有份都无效时间戳都大于max_time_value */if(all_invalid) {out_canshu_str_value[0] 0;out_canshu_str_value[1] \0;*out_value 0;cache_set(canshuo_index, 0);return;}/* 只读最佳那份的完整数据 */read_slot_full(best_idx, canshuo_index, unit_buf1);/* 提取值 */unit_buf1[34] \0;strcpy(out_canshu_str_value, (char*)unit_buf1);if(is_all_digit(out_canshu_str_value)) *out_value atoi(out_canshu_str_value);cache_set(canshuo_index, out_canshu_str_value);}/* 删除 */void test_destory_a_canshu2(int canshuo_index,int bak_index) {memset(unit_buf1_for_write, 0xff, save_unit_length);SPI_Flash_Write_by_earse_ok(unit_buf1_for_write,bak_index * rongyu_bak_canshu_unit_size canshuo_index * save_unit_length,save_unit_length, 0);cache_invalidate(canshuo_index);}/* 快速保存省去先读再写的时间 */void save_a_canshu_with_bak(int canshuo_index, char* canshu_Name, char * canshu_value) {uint32_t max_time_st 0;/* 只读时间戳不读完整值 */uint32_t ts_arr[rongyu_bak_canshu_bak_count];int all_same 1;int all_erased 1;for(int i 0; i rongyu_bak_canshu_bak_count; i) {ts_arr[i] read_timestamp(i, canshuo_index);if(ts_arr[i] max_time_value) all_erased 0;if(i 0 ts_arr[i] ! ts_arr[0]) all_same 0;}if(all_erased) {max_time_st 1;} else if(all_same) {max_time_st ts_arr[0] 1;} else {for(int i 0; i rongyu_bak_canshu_bak_count; i) {if(ts_arr[i] max_time_value ts_arr[i] max_time_st)max_time_st ts_arr[i];}max_time_st;}if(max_time_st (max_time_value - 3)) max_time_st 0;write_to_flash_do(canshuo_index, max_time_st, canshu_Name, canshu_value);//// test_destory_a_canshu2(canshuo_index,0);//// test_destory_a_canshu2(canshuo_index,1);////// test_destory_a_canshu2(canshuo_index,3);}对于大块数据100KB用于批量快速存储产品列表等的冗余备份机制读写#include ../Middlewares/fatfs/w25q64.h#include core/net.h#include string.h#include ctype.h#include my_malloc.h#include lv_mem.h // 提供 lv_mem_alloc / lv_mem_free#include head_ruan.h // 提供 lv_mem_alloc / lv_mem_freevoid lcd_show_num(uint16_t x, uint16_t y, uint32_t num, uint8_t len, uint8_t size, uint32_t color);void lcd_show_string(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t size, char *p, uint32_t color);/* 可配置参数 */#define rongyu_bak_canshu_bak_count 4#define rongyu_bak_canshu_slot_count 2 // 只需2个用于存所有产品数据和历史故障记录最多1000条#define save_unit_length (100 * 1024) // 100KB可宏定义/* Flash 参数存储区起始偏移单位字节可修改 */#define FLASH_PARAM_BASE_OFFSET (2048 * 1024) // 2MB/* 每个备份区总字节数 */#define rongyu_bak_canshu_unit_size (rongyu_bak_canshu_slot_count * save_unit_length)/* 数据区长度 槽位长度 - 2字节CRC - 4字节时间戳 */#define VALUE_LENGTH (save_unit_length - 6)/* CRC16 偏移紧跟数据区之后 */#define CRC_OFFSET VALUE_LENGTH/* 时间戳偏移CRC之后 */#define TIMESTAMP_OFFSET (VALUE_LENGTH 2)#define max_time_value 9999999/*** brief 计算 Modbus CRC16* param data: 数据指针* param len : 数据长度(字节)* return 16位CRC值*/static uint16_t crc16_modbus(const uint8_t *data, uint32_t len){uint16_t crc 0x00;for (uint32_t i 0; i len; i) { //只简单判断校验和crccrcdata[i];}return crc;////// uint16_t crc 0xFFFF;// for (uint32_t i 0; i len; i) {////////// crc ^ data[i];// for (int j 0; j 8; j) {// if (crc 0x0001) {// crc (crc 1) ^ 0xA001;// } else {// crc 1;// }// }////////// }// return crc;}/* 大缓存动态分配 */static u8 *unit_buf1_prod NULL; // 用于读单个完整槽位加 _prod 后缀static u8 *unit_buf1_for_write_prod NULL; // 用于写单个完整槽位加 _prod 后缀/*** 初始化动态缓存需要在系统启动、LVGL内存可用后调用。*/void w25q64_param_buf_init(void){unit_buf1_prod (u8 *)lv_mem_alloc(save_unit_length);unit_buf1_for_write_prod (u8 *)lv_mem_alloc(save_unit_length);if (unit_buf1_prod ! NULL) {memset(unit_buf1_prod, 0, save_unit_length);}if (unit_buf1_for_write_prod ! NULL) {memset(unit_buf1_for_write_prod, 0, save_unit_length);}}void format_flash_1(){memset(unit_buf1_for_write_prod, 0xff, save_unit_length);for(int canshuo_index0;canshuo_indexrongyu_bak_canshu_slot_count;canshuo_index){for (int i 0; i rongyu_bak_canshu_bak_count; i) {uint32_t addr FLASH_PARAM_BASE_OFFSET i * rongyu_bak_canshu_unit_size canshuo_index * save_unit_length;SPI_Flash_Write_by_earse_ok(unit_buf1_for_write_prod,addr,save_unit_length, 0);}}lcd_show_string(0,260,100,50,16,( char *)format ok:,0x00);}/*** 释放动态缓存可选一般不需要*/void w25q64_param_buf_deinit(void){if (unit_buf1_prod ! NULL) {lv_mem_free(unit_buf1_prod);unit_buf1_prod NULL;}if (unit_buf1_for_write_prod ! NULL) {lv_mem_free(unit_buf1_for_write_prod);unit_buf1_for_write_prod NULL;}}/* 工具函数 */int is_all_digit_prod(char *str) // 加 _prod 后缀{for (int i 0; str[i] ! \0; i) {if (!isdigit((unsigned char)str[i])) {return 0;}}return 1;}/* 读单个槽位的时间戳4字节位于槽位末尾 */static uint32_t read_timestamp(int backup_idx, int canshuo_index){uint32_t ts 0;uint32_t addr FLASH_PARAM_BASE_OFFSET backup_idx * rongyu_bak_canshu_unit_size canshuo_index * save_unit_length TIMESTAMP_OFFSET;SPI_FLASH_BufferRead((u8 *)ts, addr, 4, 0, 0);return ts;}/* 只读值区不读时间戳 */static void read_value_only(int backup_idx, int canshuo_index, char *out){uint32_t addr FLASH_PARAM_BASE_OFFSET backup_idx * rongyu_bak_canshu_unit_size canshuo_index * save_unit_length;SPI_FLASH_BufferRead((u8 *)out, addr, VALUE_LENGTH, 0, 0);out[VALUE_LENGTH] \0; // 确保字符串结束}/* 读完整槽位值区 时间戳 */static void read_slot_full(int backup_idx, int canshuo_index, u8 *buf){uint32_t addr FLASH_PARAM_BASE_OFFSET backup_idx * rongyu_bak_canshu_unit_size canshuo_index * save_unit_length;SPI_FLASH_BufferRead(buf, addr, save_unit_length, 0, 0);}static void write_to_flash_do_prod(int canshuo_index, u32 max_time_st, char *canshu_Name, char *canshu_value){if (max_time_st (max_time_value - 3)) {max_time_st 0;}if (unit_buf1_for_write_prod NULL) {return; // 未初始化}memset(unit_buf1_for_write_prod, 0, save_unit_length);/* 产品数据长度 */uint32_t data_len g_total_rows_for_prods * sizeof(prod_data_for_save_to_w25q128);/* 防止数据超出数据区 */if (data_len VALUE_LENGTH) {return; // 数据过大}/* 拷贝数据 */if (canshuo_index 0) {data_len g_total_rows_for_prods * sizeof(prod_data_for_save_to_w25q128);memcpy(unit_buf1_for_write_prod, (char *)canshu_value, data_len);}/* 其他槽位暂未实现可在此扩展 *//* 计算 CRC16 并写入 CRC 偏移处小端 */uint16_t crc crc16_modbus(unit_buf1_for_write_prod, data_len);unit_buf1_for_write_prod[CRC_OFFSET 0] crc 0xff;unit_buf1_for_write_prod[CRC_OFFSET 1] (crc 8) 0xff;/* 时间戳写在槽位末尾4字节 */unit_buf1_for_write_prod[TIMESTAMP_OFFSET 0] max_time_st 0xff;unit_buf1_for_write_prod[TIMESTAMP_OFFSET 1] (max_time_st 8) 0xff;unit_buf1_for_write_prod[TIMESTAMP_OFFSET 2] (max_time_st 16) 0xff;unit_buf1_for_write_prod[TIMESTAMP_OFFSET 3] (max_time_st 24) 0xff;/* 写入所有备份 */for (int i 0; i rongyu_bak_canshu_bak_count; i) {uint32_t addr FLASH_PARAM_BASE_OFFSET i * rongyu_bak_canshu_unit_size canshuo_index * save_unit_length;SPI_Flash_Write_by_earse_ok(unit_buf1_for_write_prod,addr,save_unit_length, 0);}}static int is_slot_valid(int canshuo_index){uint32_t addr FLASH_PARAM_BASE_OFFSET canshuo_index * save_unit_length;u8 first_bytes[2];SPI_FLASH_BufferRead(first_bytes, addr, 2, 0, 0);if (first_bytes[0] 0 first_bytes[1] \0) {return 0;}uint32_t ts read_timestamp(0, canshuo_index);if (ts max_time_value) {return 0;}/* 增加 CRC 校验 */uint32_t data_len g_total_rows_for_prods * sizeof(prod_data_for_save_to_w25q128);if(canshuo_index0){data_len g_total_rows_for_prods * sizeof(prod_data_for_save_to_w25q128);}u8 crc_bytes[2];SPI_FLASH_BufferRead(crc_bytes, addr CRC_OFFSET, 2, 0, 0);uint16_t crc_stored crc_bytes[0] | (crc_bytes[1] 8);u8 *tmp (u8 *)lv_mem_alloc(data_len);if (tmp NULL) return 0;SPI_FLASH_BufferRead(tmp, addr, data_len, 0, 0);uint16_t crc_calc crc16_modbus(tmp, data_len);lv_mem_free(tmp);return (crc_stored crc_calc);}/* 快速读取值无缓存 */static void read_value_fast(int canshuo_index, char *out){read_value_only(0, canshuo_index, out);uint32_t ts read_timestamp(0, canshuo_index);if (ts max_time_value) {out[0] 0;out[1] \0;}}void read_a_canshu_with_bak_for_all_prod(int canshuo_index, char *canshu_Name,char *out_canshu_str_value, unsigned int *out_value){uint32_t data_len g_total_rows_for_prods * sizeof(prod_data_for_save_to_w25q128);if(canshuo_index0){/* 拷贝数据区到输出 */data_len g_total_rows_for_prods * sizeof(prod_data_for_save_to_w25q128);}int best_idx -1;uint32_t max_valid_ts 0;if (unit_buf1_prod NULL) {return;}/* 遍历所有备份找时间戳最新且 CRC 匹配的备份 */for (int i 0; i rongyu_bak_canshu_bak_count; i) {uint32_t ts read_timestamp(i, canshuo_index);/* 时间戳无效擦除态则跳过 */if (ts max_time_value) {continue;}/* 读完整槽位数据区 CRC 时间戳 */read_slot_full(i, canshuo_index, unit_buf1_prod);/* 读取存储的 CRC16小端 */uint16_t crc_stored unit_buf1_prod[CRC_OFFSET] |(unit_buf1_prod[CRC_OFFSET 1] 8);/* 计算数据区 CRC16 */uint16_t crc_calc crc16_modbus(unit_buf1_prod, data_len);/* CRC 不匹配跳过该备份 */if (crc_stored ! crc_calc) {continue;}else{best_idx i;break;}/* 更新最佳备份 */if (ts max_valid_ts) {max_valid_ts ts;best_idx i;}}/* 所有备份都无效或 CRC 都不匹配 */if (best_idx 0) {out_canshu_str_value[0] 0;out_canshu_str_value[1] \0;*out_value 0;return;}/* 读取最佳备份的完整数据 */read_slot_full(best_idx, canshuo_index, unit_buf1_prod);if(canshuo_index0){/* 拷贝数据区到输出 */memcpy(out_canshu_str_value, (char *)unit_buf1_prod, data_len);}/* 可选如果数据是数字字符串转换为数值 */// if (is_all_digit_prod(out_canshu_str_value)) {// *out_value atoi(out_canshu_str_value);// }}/* 删除某槽位某备份 */static void test_destory_a_canshu2_prod(int canshuo_index, int bak_index) // 加 _prod 后缀{if (unit_buf1_for_write_prod NULL) {return;}memset(unit_buf1_for_write_prod, 0xff, save_unit_length);uint32_t addr FLASH_PARAM_BASE_OFFSET bak_index * rongyu_bak_canshu_unit_size canshuo_index * save_unit_length;SPI_Flash_Write_by_earse_ok(unit_buf1_for_write_prod,addr,save_unit_length, 0);}/* 快速保存对外接口 */void save_a_canshu_with_bak_for_all_prod(int canshuo_index, char *canshu_Name, char *canshu_value){uint32_t max_time_st 0;uint32_t ts_arr[rongyu_bak_canshu_bak_count];int all_same 1;int all_erased 1;// 只读时间戳不读完整值for (int i 0; i rongyu_bak_canshu_bak_count; i) {ts_arr[i] read_timestamp(i, canshuo_index);if (ts_arr[i] max_time_value) {all_erased 0;}if (i 0 ts_arr[i] ! ts_arr[0]) {all_same 0;}}if (all_erased) {max_time_st 1;} else if (all_same) {max_time_st ts_arr[0] 1;} else {for (int i 0; i rongyu_bak_canshu_bak_count; i) {if (ts_arr[i] max_time_value ts_arr[i] max_time_st) {max_time_st ts_arr[i];}}max_time_st;}if (max_time_st (max_time_value - 3)) {max_time_st 0;}write_to_flash_do_prod(canshuo_index, max_time_st, canshu_Name, canshu_value);// 以下测试代码按需保留或删除函数名已改为 _prod 版本// test_destory_a_canshu2_prod(canshuo_index, 0);// test_destory_a_canshu2_prod(canshuo_index, 1);// test_destory_a_canshu2_prod(canshuo_index, 3);}
返回列表