| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615 |
- /*
- * Copyright (c) 2026, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2026-03-06 user first version
- */
- #define DBG_TAG "ecode"
- #define DBG_LVL DBG_INFO
- #include <rtdbg.h>
- #include <rthw.h>
- #include <fal.h>
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "ecode.h"
- /* 分区名称(需在 fal_cfg.h 中定义) */
- #define ECODE_PART_NAME "ecode"
- /* NOR Flash 扇区大小(根据硬件设定,通常为4096) */
- #define SECTOR_SIZE 4096
- /* 每条记录固定为64字节(通过结构体大小控制) */
- #define RECORD_SIZE sizeof(ecode_record_t)
- /* 每扇区记录数 */
- #define RECORDS_PER_SECTOR (SECTOR_SIZE / RECORD_SIZE)
- /* 头部结构:魔数、版本、下一个写入的序列号 */
- typedef struct
- {
- uint32_t magic; // 魔数,用于标识是否已初始化
- uint32_t version; // 版本号
- uint32_t write_seq; // 下一个要写入的序列号(从0开始,每次写入+1)
- } ecode_header_t;
- static const struct fal_partition *ecode_part = RT_NULL;
- static uint32_t max_sectors = 0; // 数据扇区总数(头部扇区除外)
- static rt_mutex_t ecode_lock = RT_NULL; // 互斥锁(用于保护数据读写)
- static rt_bool_t initialized = RT_FALSE;
- static rt_mutex_t init_mutex = RT_NULL; // 仅用于保护初始化过程
- extern struct rtc_time time_now;
- /* 获取当前有效记录数 */
- static uint32_t get_valid_count(uint32_t write_seq)
- {
- uint32_t max_records = max_sectors * RECORDS_PER_SECTOR;
- if (write_seq < max_records)
- return write_seq;
- else
- return max_records;
- }
- /* 读取头部(需加锁) */
- static int read_header(ecode_header_t *hdr)
- {
- if (fal_partition_read(ecode_part, 0, (uint8_t *)hdr, sizeof(ecode_header_t)) < 0)
- {
- LOG_E("read header failed");
- return -RT_ERROR;
- }
- return RT_EOK;
- }
- /* 写入头部(需加锁) */
- static int write_header(const ecode_header_t *hdr)
- {
- uint8_t *sector_buf;
- int ret = RT_EOK;
- sector_buf = rt_malloc(SECTOR_SIZE);
- if (sector_buf == RT_NULL)
- {
- LOG_E("malloc sector buffer failed");
- return -RT_ENOMEM;
- }
- /* 读取整个头部扇区 */
- if (fal_partition_read(ecode_part, 0, sector_buf, SECTOR_SIZE) < 0)
- {
- LOG_E("read header sector failed");
- rt_free(sector_buf);
- return -RT_ERROR;
- }
- /* 更新头部 */
- memcpy(sector_buf, hdr, sizeof(ecode_header_t));
- /* 擦除头部扇区 */
- if (fal_partition_erase(ecode_part, 0, SECTOR_SIZE) < 0)
- {
- LOG_E("erase header sector failed");
- rt_free(sector_buf);
- return -RT_ERROR;
- }
- /* 写入整个扇区 */
- if (fal_partition_write(ecode_part, 0, sector_buf, SECTOR_SIZE) != SECTOR_SIZE)
- {
- LOG_E("write header sector failed");
- ret = -RT_ERROR;
- }
- rt_free(sector_buf);
- return ret;
- }
- /* 擦除整个分区(用于首次初始化) */
- static int erase_whole_partition(void)
- {
- if (fal_partition_erase(ecode_part, 0, ecode_part->len) < 0)
- {
- LOG_E("erase whole partition failed");
- return -RT_ERROR;
- }
- return RT_EOK;
- }
- /* 初始化核心函数 */
- static int ecode_init_core(void)
- {
- ecode_header_t hdr;
- uint32_t part_size;
- rt_err_t ret;
- ecode_part = fal_partition_find(ECODE_PART_NAME);
- if (ecode_part == RT_NULL)
- {
- LOG_E("partition '%s' not found", ECODE_PART_NAME);
- return -RT_ERROR;
- }
- part_size = ecode_part->len;
- LOG_D("partition size: %u bytes", part_size);
- if (part_size < SECTOR_SIZE * 2) // 至少需要一个头部扇区和一个数据扇区
- {
- LOG_E("partition too small");
- return -RT_ERROR;
- }
- max_sectors = (part_size - SECTOR_SIZE) / SECTOR_SIZE; // 头部占用一个扇区
- LOG_D("max_sectors=%u, records per sector=%u", max_sectors, RECORDS_PER_SECTOR);
- ecode_lock = rt_mutex_create("ecode", RT_IPC_FLAG_FIFO);
- if (ecode_lock == RT_NULL)
- {
- LOG_E("create mutex failed");
- return -RT_ERROR;
- }
- ret = read_header(&hdr);
- if (ret == RT_EOK && hdr.magic == ECODE_MAGIC && hdr.version == ECODE_VERSION)
- {
- LOG_D("ecode already initialized, write_seq=%u", hdr.write_seq);
- return RT_EOK;
- }
- LOG_W("partition not initialized or invalid, erasing...");
- if (erase_whole_partition() != RT_EOK)
- {
- LOG_E("erase failed");
- return -RT_ERROR;
- }
- hdr.magic = ECODE_MAGIC;
- hdr.version = ECODE_VERSION;
- hdr.write_seq = 0;
- if (write_header(&hdr) != RT_EOK)
- {
- LOG_E("write header failed");
- return -RT_ERROR;
- }
- LOG_I("ecode initialized, max_sectors=%d", max_sectors);
- return RT_EOK;
- }
- static int ensure_initialized(void)
- {
- if (initialized)
- return RT_EOK;
- if (init_mutex == RT_NULL)
- {
- init_mutex = rt_mutex_create("ecode_init", RT_IPC_FLAG_FIFO);
- if (init_mutex == RT_NULL)
- {
- LOG_E("create init mutex failed");
- return -RT_ERROR;
- }
- }
- rt_mutex_take(init_mutex, RT_WAITING_FOREVER);
- if (!initialized)
- {
- int ret = ecode_init_core();
- if (ret == RT_EOK)
- initialized = RT_TRUE;
- else
- LOG_E("ecode_init_core failed");
- }
- rt_mutex_release(init_mutex);
- return initialized ? RT_EOK : -RT_ERROR;
- }
- int ecode_init(void)
- {
- return ensure_initialized();
- }
- /* 写入记录(与原有函数名完全一致) */
- void Recode_e_code(ecode_list code, uint16_t para, char *detail)
- {
- ecode_header_t hdr;
- ecode_record_t rec;
- uint32_t seq, sector_index, record_offset_in_sector;
- uint32_t sector_start;
- uint8_t *sector_buf = RT_NULL;
- rt_base_t level;
- //rt_kprintf("RECORD_SIZE %d RECORDS_PER_SECTOR %d\n" , RECORD_SIZE , RECORDS_PER_SECTOR);
- if (ensure_initialized() != RT_EOK)
- {
- LOG_E("ecode not initialized, abort write");
- return;
- }
- rt_mutex_take(ecode_lock, RT_WAITING_FOREVER);
- if (read_header(&hdr) != RT_EOK)
- {
- LOG_E("read header failed, abort write");
- rt_mutex_release(ecode_lock);
- return;
- }
- if (hdr.magic != ECODE_MAGIC || hdr.version != ECODE_VERSION)
- {
- LOG_E("header corrupted, abort write");
- rt_mutex_release(ecode_lock);
- return;
- }
- seq = hdr.write_seq;
- /* 构建记录 */
- level = rt_hw_interrupt_disable();
- rec.time_stamp.tm_year = time_now.year - 2000;
- rec.time_stamp.tm_mon = time_now.month;
- rec.time_stamp.tm_mday = time_now.date;
- rec.time_stamp.tm_hour = time_now.hour;
- rec.time_stamp.tm_min = time_now.minute;
- rec.time_stamp.tm_sec = time_now.second;
- rt_hw_interrupt_enable(level);
- rec.code = (uint16_t)code;
- rec.para = para;
- rec.num = seq;
- rt_strncpy(rec.detail, detail, sizeof(rec.detail) - 1);
- rec.detail[sizeof(rec.detail) - 1] = '\0';
- /* 计算扇区索引和扇区起始地址:头部占扇区0,数据从扇区1开始 */
- sector_index = seq / RECORDS_PER_SECTOR;
- if (sector_index >= max_sectors)
- {
- sector_index = seq % max_sectors;
- }
- sector_start = SECTOR_SIZE + sector_index * SECTOR_SIZE;
- record_offset_in_sector = (seq % RECORDS_PER_SECTOR) * RECORD_SIZE;
- LOG_D("write: seq=%u sector_index=%u sector_start=0x%x record_offset=%u",
- seq, sector_index, sector_start, record_offset_in_sector);
- sector_buf = rt_malloc(SECTOR_SIZE);
- if (sector_buf == RT_NULL)
- {
- LOG_E("malloc sector buffer failed");
- rt_mutex_release(ecode_lock);
- return;
- }
- /* 读取整个扇区到缓冲区 */
- if (fal_partition_read(ecode_part, sector_start, sector_buf, SECTOR_SIZE) < 0)
- {
- LOG_E("read sector failed");
- rt_free(sector_buf);
- rt_mutex_release(ecode_lock);
- return;
- }
- /* 修改对应记录 */
- memcpy(sector_buf + record_offset_in_sector, &rec, RECORD_SIZE);
- /* 擦除整个扇区 */
- if (fal_partition_erase(ecode_part, sector_start, SECTOR_SIZE) < 0)
- {
- LOG_E("erase sector failed");
- rt_free(sector_buf);
- rt_mutex_release(ecode_lock);
- return;
- }
- /* 写回整个扇区 */
- if (fal_partition_write(ecode_part, sector_start, sector_buf, SECTOR_SIZE) != SECTOR_SIZE)
- {
- LOG_E("write sector failed");
- rt_free(sector_buf);
- rt_mutex_release(ecode_lock);
- return;
- }
- rt_free(sector_buf);
- /* 更新头部 */
- hdr.write_seq = seq + 1;
- if (write_header(&hdr) != RT_EOK)
- {
- LOG_E("write header failed, but record already written");
- }
- /* 打印写入成功日志(与原格式一致) */
- LOG_W("e_code SET 0 c %d p %d d %s", code, para , detail);
- /* 计算并打印已用空间信息 */
- uint32_t total_records = max_sectors * RECORDS_PER_SECTOR;
- uint32_t used_records = hdr.write_seq;
- if (used_records > total_records)
- used_records = total_records;
- uint32_t used_bytes = used_records * RECORD_SIZE;
- LOG_I("e_code used %d cnt %d/%d", used_bytes, used_records, total_records);
- rt_mutex_release(ecode_lock);
- }
- /* 读取最新第 index 条记录(index=0 为最新) */
- int ecode_read(uint32_t index, ecode_record_t *rec)
- {
- ecode_header_t hdr;
- uint32_t valid_count;
- uint32_t logical_seq, sector_index, record_offset_in_sector;
- uint32_t sector_start;
- uint8_t *sector_buf = RT_NULL;
- if (ensure_initialized() != RT_EOK)
- return -RT_ERROR;
- rt_mutex_take(ecode_lock, RT_WAITING_FOREVER);
- if (read_header(&hdr) != RT_EOK)
- {
- rt_mutex_release(ecode_lock);
- return -RT_ERROR;
- }
- if (hdr.magic != ECODE_MAGIC || hdr.version != ECODE_VERSION)
- {
- LOG_E("header corrupted");
- rt_mutex_release(ecode_lock);
- return -RT_ERROR;
- }
- valid_count = get_valid_count(hdr.write_seq);
- if (index >= valid_count)
- {
- rt_mutex_release(ecode_lock);
- return -RT_ERROR;
- }
- logical_seq = hdr.write_seq - 1 - index;
- sector_index = logical_seq / RECORDS_PER_SECTOR;
- if (sector_index >= max_sectors)
- {
- sector_index = logical_seq % max_sectors;
- }
- sector_start = SECTOR_SIZE + sector_index * SECTOR_SIZE;
- record_offset_in_sector = (logical_seq % RECORDS_PER_SECTOR) * RECORD_SIZE;
- LOG_D("read: index=%u logical_seq=%u sector_start=0x%x record_offset=%u",
- index, logical_seq, sector_start, record_offset_in_sector);
- sector_buf = rt_malloc(SECTOR_SIZE);
- if (sector_buf == RT_NULL)
- {
- LOG_E("malloc sector buffer failed");
- rt_mutex_release(ecode_lock);
- return -RT_ERROR;
- }
- if (fal_partition_read(ecode_part, sector_start, sector_buf, SECTOR_SIZE) < 0)
- {
- LOG_E("read sector failed");
- rt_free(sector_buf);
- rt_mutex_release(ecode_lock);
- return -RT_ERROR;
- }
- memcpy(rec, sector_buf + record_offset_in_sector, RECORD_SIZE);
- rt_free(sector_buf);
- /* 检查是否为全0xFF */
- uint8_t *p = (uint8_t *)rec;
- int all_ff = 1;
- for (uint32_t i = 0; i < RECORD_SIZE; i++)
- {
- if (p[i] != 0xFF)
- {
- all_ff = 0;
- break;
- }
- }
- if (all_ff)
- {
- LOG_W("record at index %d is all 0xFF (empty)", index);
- rt_mutex_release(ecode_lock);
- return -RT_ERROR;
- }
- if (rec->num > hdr.write_seq)
- {
- LOG_W("record at index %d has invalid num %u (expected <= %u)", index, rec->num, hdr.write_seq);
- rt_mutex_release(ecode_lock);
- return -RT_ERROR;
- }
- rt_mutex_release(ecode_lock);
- return RT_EOK;
- }
- uint32_t ecode_get_count(void)
- {
- ecode_header_t hdr;
- uint32_t count;
- if (ensure_initialized() != RT_EOK)
- return 0;
- rt_mutex_take(ecode_lock, RT_WAITING_FOREVER);
- if (read_header(&hdr) != RT_EOK)
- {
- rt_mutex_release(ecode_lock);
- return 0;
- }
- if (hdr.magic != ECODE_MAGIC || hdr.version != ECODE_VERSION)
- {
- LOG_E("header corrupted");
- rt_mutex_release(ecode_lock);
- return 0;
- }
- count = get_valid_count(hdr.write_seq);
- rt_mutex_release(ecode_lock);
- return count;
- }
- void ecode_clear(void)
- {
- ecode_header_t hdr;
- if (ensure_initialized() != RT_EOK)
- {
- LOG_E("ecode not initialized, abort clear");
- return;
- }
- rt_mutex_take(ecode_lock, RT_WAITING_FOREVER);
- if (erase_whole_partition() != RT_EOK)
- {
- LOG_E("erase whole partition failed");
- rt_mutex_release(ecode_lock);
- return;
- }
- hdr.magic = ECODE_MAGIC;
- hdr.version = ECODE_VERSION;
- hdr.write_seq = 0;
- if (write_header(&hdr) != RT_EOK)
- {
- LOG_E("write header failed after clear");
- }
- else
- {
- LOG_I("ecode cleared");
- }
- rt_mutex_release(ecode_lock);
- }
- /*=============================================================================
- * MSH 命令
- *============================================================================*/
- #include <finsh.h>
- static void recode_test(void)
- {
- Recode_e_code(e_test_fun, 6, "3-6 是一条测试数据 123456789");
- }
- MSH_CMD_EXPORT(recode_test, write a test ecode);
- static void pre(void)
- {
- ecode_record_t rec;
- uint32_t cnt = ecode_get_count();
- LOG_I("ecode count: %u", cnt);
- for (uint32_t i = 0; i < cnt; i++)
- {
- if (ecode_read(i, &rec) == 0)
- {
- LOG_I("%u - %02u/%02u/%02u %02u:%02u:%02u: (%u-%u) %s",
- rec.num,
- rec.time_stamp.tm_year, rec.time_stamp.tm_mon, rec.time_stamp.tm_mday,
- rec.time_stamp.tm_hour, rec.time_stamp.tm_min, rec.time_stamp.tm_sec,
- rec.code, rec.para, rec.detail);
- }
- else
- {
- LOG_I("read error at index %u", i);
- }
- }
- }
- MSH_CMD_EXPORT(pre, print all ecode records from newest to oldest);
- static void delecode(void)
- {
- ecode_clear();
- }
- MSH_CMD_EXPORT(delecode, clear all ecode records);
- static void ecode_test(int argc, char **argv)
- {
- ecode_record_t rec;
- uint32_t cnt_before, cnt_after;
- int i, ret;
- LOG_I("===== ecode module test start =====");
- ecode_clear();
- cnt_before = ecode_get_count();
- LOG_I("after clear, count=%u", cnt_before);
- if (cnt_before != 0)
- {
- LOG_E("clear failed");
- return;
- }
- for (i = 0; i < 10; i++)
- {
- char detail[64];
- rt_snprintf(detail, sizeof(detail), "test data %d", i);
- Recode_e_code(e_test_fun, i, detail);
- }
- cnt_after = ecode_get_count();
- LOG_I("after write 10, count=%u", cnt_after);
- if (cnt_after != 10)
- {
- LOG_E("write count mismatch");
- return;
- }
- for (i = 0; i < 10; i++)
- {
- ret = ecode_read(i, &rec);
- if (ret != 0)
- {
- LOG_E("read index %d failed", i);
- return;
- }
- LOG_I("read[%d]: num=%u time=%02u/%02u/%02u %02u:%02u:%02u code=%u para=%u detail=%s",
- i, rec.num,
- rec.time_stamp.tm_year, rec.time_stamp.tm_mon, rec.time_stamp.tm_mday,
- rec.time_stamp.tm_hour, rec.time_stamp.tm_min, rec.time_stamp.tm_sec,
- rec.code, rec.para, rec.detail);
- if (rec.num != (uint32_t)(9 - i))
- {
- LOG_E("num mismatch: expected %d, got %u", 9 - i, rec.num);
- }
- if (rec.code != e_test_fun || rec.para != (uint16_t)(9 - i))
- {
- LOG_E("code/para mismatch");
- }
- }
- ret = ecode_read(10, &rec);
- if (ret == 0)
- {
- LOG_E("read beyond count should fail");
- }
- else
- {
- LOG_I("read beyond count failed as expected");
- }
- ecode_clear();
- cnt_after = ecode_get_count();
- if (cnt_after != 0)
- {
- LOG_E("clear after test failed");
- }
- else
- {
- LOG_I("clear ok");
- }
- LOG_I("===== ecode module test passed =====");
- }
- MSH_CMD_EXPORT(ecode_test, run ecode module self - test);
|