ecode.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. * Copyright (c) 2026, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2026-03-06 user first version
  9. */
  10. #define DBG_TAG "ecode"
  11. #define DBG_LVL DBG_INFO
  12. #include <rtdbg.h>
  13. #include <rthw.h>
  14. #include <fal.h>
  15. #include <string.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include "ecode.h"
  19. /* 分区名称(需在 fal_cfg.h 中定义) */
  20. #define ECODE_PART_NAME "ecode"
  21. /* NOR Flash 扇区大小(根据硬件设定,通常为4096) */
  22. #define SECTOR_SIZE 4096
  23. /* 每条记录固定为64字节(通过结构体大小控制) */
  24. #define RECORD_SIZE sizeof(ecode_record_t)
  25. /* 每扇区记录数 */
  26. #define RECORDS_PER_SECTOR (SECTOR_SIZE / RECORD_SIZE)
  27. /* 头部结构:魔数、版本、下一个写入的序列号 */
  28. typedef struct
  29. {
  30. uint32_t magic; // 魔数,用于标识是否已初始化
  31. uint32_t version; // 版本号
  32. uint32_t write_seq; // 下一个要写入的序列号(从0开始,每次写入+1)
  33. } ecode_header_t;
  34. static const struct fal_partition *ecode_part = RT_NULL;
  35. static uint32_t max_sectors = 0; // 数据扇区总数(头部扇区除外)
  36. static rt_mutex_t ecode_lock = RT_NULL; // 互斥锁(用于保护数据读写)
  37. static rt_bool_t initialized = RT_FALSE;
  38. static rt_mutex_t init_mutex = RT_NULL; // 仅用于保护初始化过程
  39. extern struct rtc_time time_now;
  40. /* 获取当前有效记录数 */
  41. static uint32_t get_valid_count(uint32_t write_seq)
  42. {
  43. uint32_t max_records = max_sectors * RECORDS_PER_SECTOR;
  44. if (write_seq < max_records)
  45. return write_seq;
  46. else
  47. return max_records;
  48. }
  49. /* 读取头部(需加锁) */
  50. static int read_header(ecode_header_t *hdr)
  51. {
  52. if (fal_partition_read(ecode_part, 0, (uint8_t *)hdr, sizeof(ecode_header_t)) < 0)
  53. {
  54. LOG_E("read header failed");
  55. return -RT_ERROR;
  56. }
  57. return RT_EOK;
  58. }
  59. /* 写入头部(需加锁) */
  60. static int write_header(const ecode_header_t *hdr)
  61. {
  62. uint8_t *sector_buf;
  63. int ret = RT_EOK;
  64. sector_buf = rt_malloc(SECTOR_SIZE);
  65. if (sector_buf == RT_NULL)
  66. {
  67. LOG_E("malloc sector buffer failed");
  68. return -RT_ENOMEM;
  69. }
  70. /* 读取整个头部扇区 */
  71. if (fal_partition_read(ecode_part, 0, sector_buf, SECTOR_SIZE) < 0)
  72. {
  73. LOG_E("read header sector failed");
  74. rt_free(sector_buf);
  75. return -RT_ERROR;
  76. }
  77. /* 更新头部 */
  78. memcpy(sector_buf, hdr, sizeof(ecode_header_t));
  79. /* 擦除头部扇区 */
  80. if (fal_partition_erase(ecode_part, 0, SECTOR_SIZE) < 0)
  81. {
  82. LOG_E("erase header sector failed");
  83. rt_free(sector_buf);
  84. return -RT_ERROR;
  85. }
  86. /* 写入整个扇区 */
  87. if (fal_partition_write(ecode_part, 0, sector_buf, SECTOR_SIZE) != SECTOR_SIZE)
  88. {
  89. LOG_E("write header sector failed");
  90. ret = -RT_ERROR;
  91. }
  92. rt_free(sector_buf);
  93. return ret;
  94. }
  95. /* 擦除整个分区(用于首次初始化) */
  96. static int erase_whole_partition(void)
  97. {
  98. if (fal_partition_erase(ecode_part, 0, ecode_part->len) < 0)
  99. {
  100. LOG_E("erase whole partition failed");
  101. return -RT_ERROR;
  102. }
  103. return RT_EOK;
  104. }
  105. /* 初始化核心函数 */
  106. static int ecode_init_core(void)
  107. {
  108. ecode_header_t hdr;
  109. uint32_t part_size;
  110. rt_err_t ret;
  111. ecode_part = fal_partition_find(ECODE_PART_NAME);
  112. if (ecode_part == RT_NULL)
  113. {
  114. LOG_E("partition '%s' not found", ECODE_PART_NAME);
  115. return -RT_ERROR;
  116. }
  117. part_size = ecode_part->len;
  118. LOG_D("partition size: %u bytes", part_size);
  119. if (part_size < SECTOR_SIZE * 2) // 至少需要一个头部扇区和一个数据扇区
  120. {
  121. LOG_E("partition too small");
  122. return -RT_ERROR;
  123. }
  124. max_sectors = (part_size - SECTOR_SIZE) / SECTOR_SIZE; // 头部占用一个扇区
  125. LOG_D("max_sectors=%u, records per sector=%u", max_sectors, RECORDS_PER_SECTOR);
  126. ecode_lock = rt_mutex_create("ecode", RT_IPC_FLAG_FIFO);
  127. if (ecode_lock == RT_NULL)
  128. {
  129. LOG_E("create mutex failed");
  130. return -RT_ERROR;
  131. }
  132. ret = read_header(&hdr);
  133. if (ret == RT_EOK && hdr.magic == ECODE_MAGIC && hdr.version == ECODE_VERSION)
  134. {
  135. LOG_D("ecode already initialized, write_seq=%u", hdr.write_seq);
  136. return RT_EOK;
  137. }
  138. LOG_W("partition not initialized or invalid, erasing...");
  139. if (erase_whole_partition() != RT_EOK)
  140. {
  141. LOG_E("erase failed");
  142. return -RT_ERROR;
  143. }
  144. hdr.magic = ECODE_MAGIC;
  145. hdr.version = ECODE_VERSION;
  146. hdr.write_seq = 0;
  147. if (write_header(&hdr) != RT_EOK)
  148. {
  149. LOG_E("write header failed");
  150. return -RT_ERROR;
  151. }
  152. LOG_I("ecode initialized, max_sectors=%d", max_sectors);
  153. return RT_EOK;
  154. }
  155. static int ensure_initialized(void)
  156. {
  157. if (initialized)
  158. return RT_EOK;
  159. if (init_mutex == RT_NULL)
  160. {
  161. init_mutex = rt_mutex_create("ecode_init", RT_IPC_FLAG_FIFO);
  162. if (init_mutex == RT_NULL)
  163. {
  164. LOG_E("create init mutex failed");
  165. return -RT_ERROR;
  166. }
  167. }
  168. rt_mutex_take(init_mutex, RT_WAITING_FOREVER);
  169. if (!initialized)
  170. {
  171. int ret = ecode_init_core();
  172. if (ret == RT_EOK)
  173. initialized = RT_TRUE;
  174. else
  175. LOG_E("ecode_init_core failed");
  176. }
  177. rt_mutex_release(init_mutex);
  178. return initialized ? RT_EOK : -RT_ERROR;
  179. }
  180. int ecode_init(void)
  181. {
  182. return ensure_initialized();
  183. }
  184. /* 写入记录(与原有函数名完全一致) */
  185. void Recode_e_code(ecode_list code, uint16_t para, char *detail)
  186. {
  187. ecode_header_t hdr;
  188. ecode_record_t rec;
  189. uint32_t seq, sector_index, record_offset_in_sector;
  190. uint32_t sector_start;
  191. uint8_t *sector_buf = RT_NULL;
  192. rt_base_t level;
  193. //rt_kprintf("RECORD_SIZE %d RECORDS_PER_SECTOR %d\n" , RECORD_SIZE , RECORDS_PER_SECTOR);
  194. if (ensure_initialized() != RT_EOK)
  195. {
  196. LOG_E("ecode not initialized, abort write");
  197. return;
  198. }
  199. rt_mutex_take(ecode_lock, RT_WAITING_FOREVER);
  200. if (read_header(&hdr) != RT_EOK)
  201. {
  202. LOG_E("read header failed, abort write");
  203. rt_mutex_release(ecode_lock);
  204. return;
  205. }
  206. if (hdr.magic != ECODE_MAGIC || hdr.version != ECODE_VERSION)
  207. {
  208. LOG_E("header corrupted, abort write");
  209. rt_mutex_release(ecode_lock);
  210. return;
  211. }
  212. seq = hdr.write_seq;
  213. /* 构建记录 */
  214. level = rt_hw_interrupt_disable();
  215. rec.time_stamp.tm_year = time_now.year - 2000;
  216. rec.time_stamp.tm_mon = time_now.month;
  217. rec.time_stamp.tm_mday = time_now.date;
  218. rec.time_stamp.tm_hour = time_now.hour;
  219. rec.time_stamp.tm_min = time_now.minute;
  220. rec.time_stamp.tm_sec = time_now.second;
  221. rt_hw_interrupt_enable(level);
  222. rec.code = (uint16_t)code;
  223. rec.para = para;
  224. rec.num = seq;
  225. rt_strncpy(rec.detail, detail, sizeof(rec.detail) - 1);
  226. rec.detail[sizeof(rec.detail) - 1] = '\0';
  227. /* 计算扇区索引和扇区起始地址:头部占扇区0,数据从扇区1开始 */
  228. sector_index = seq / RECORDS_PER_SECTOR;
  229. if (sector_index >= max_sectors)
  230. {
  231. sector_index = seq % max_sectors;
  232. }
  233. sector_start = SECTOR_SIZE + sector_index * SECTOR_SIZE;
  234. record_offset_in_sector = (seq % RECORDS_PER_SECTOR) * RECORD_SIZE;
  235. LOG_D("write: seq=%u sector_index=%u sector_start=0x%x record_offset=%u",
  236. seq, sector_index, sector_start, record_offset_in_sector);
  237. sector_buf = rt_malloc(SECTOR_SIZE);
  238. if (sector_buf == RT_NULL)
  239. {
  240. LOG_E("malloc sector buffer failed");
  241. rt_mutex_release(ecode_lock);
  242. return;
  243. }
  244. /* 读取整个扇区到缓冲区 */
  245. if (fal_partition_read(ecode_part, sector_start, sector_buf, SECTOR_SIZE) < 0)
  246. {
  247. LOG_E("read sector failed");
  248. rt_free(sector_buf);
  249. rt_mutex_release(ecode_lock);
  250. return;
  251. }
  252. /* 修改对应记录 */
  253. memcpy(sector_buf + record_offset_in_sector, &rec, RECORD_SIZE);
  254. /* 擦除整个扇区 */
  255. if (fal_partition_erase(ecode_part, sector_start, SECTOR_SIZE) < 0)
  256. {
  257. LOG_E("erase sector failed");
  258. rt_free(sector_buf);
  259. rt_mutex_release(ecode_lock);
  260. return;
  261. }
  262. /* 写回整个扇区 */
  263. if (fal_partition_write(ecode_part, sector_start, sector_buf, SECTOR_SIZE) != SECTOR_SIZE)
  264. {
  265. LOG_E("write sector failed");
  266. rt_free(sector_buf);
  267. rt_mutex_release(ecode_lock);
  268. return;
  269. }
  270. rt_free(sector_buf);
  271. /* 更新头部 */
  272. hdr.write_seq = seq + 1;
  273. if (write_header(&hdr) != RT_EOK)
  274. {
  275. LOG_E("write header failed, but record already written");
  276. }
  277. /* 打印写入成功日志(与原格式一致) */
  278. LOG_W("e_code SET 0 c %d p %d d %s", code, para , detail);
  279. /* 计算并打印已用空间信息 */
  280. uint32_t total_records = max_sectors * RECORDS_PER_SECTOR;
  281. uint32_t used_records = hdr.write_seq;
  282. if (used_records > total_records)
  283. used_records = total_records;
  284. uint32_t used_bytes = used_records * RECORD_SIZE;
  285. LOG_I("e_code used %d cnt %d/%d", used_bytes, used_records, total_records);
  286. rt_mutex_release(ecode_lock);
  287. }
  288. /* 读取最新第 index 条记录(index=0 为最新) */
  289. int ecode_read(uint32_t index, ecode_record_t *rec)
  290. {
  291. ecode_header_t hdr;
  292. uint32_t valid_count;
  293. uint32_t logical_seq, sector_index, record_offset_in_sector;
  294. uint32_t sector_start;
  295. uint8_t *sector_buf = RT_NULL;
  296. if (ensure_initialized() != RT_EOK)
  297. return -RT_ERROR;
  298. rt_mutex_take(ecode_lock, RT_WAITING_FOREVER);
  299. if (read_header(&hdr) != RT_EOK)
  300. {
  301. rt_mutex_release(ecode_lock);
  302. return -RT_ERROR;
  303. }
  304. if (hdr.magic != ECODE_MAGIC || hdr.version != ECODE_VERSION)
  305. {
  306. LOG_E("header corrupted");
  307. rt_mutex_release(ecode_lock);
  308. return -RT_ERROR;
  309. }
  310. valid_count = get_valid_count(hdr.write_seq);
  311. if (index >= valid_count)
  312. {
  313. rt_mutex_release(ecode_lock);
  314. return -RT_ERROR;
  315. }
  316. logical_seq = hdr.write_seq - 1 - index;
  317. sector_index = logical_seq / RECORDS_PER_SECTOR;
  318. if (sector_index >= max_sectors)
  319. {
  320. sector_index = logical_seq % max_sectors;
  321. }
  322. sector_start = SECTOR_SIZE + sector_index * SECTOR_SIZE;
  323. record_offset_in_sector = (logical_seq % RECORDS_PER_SECTOR) * RECORD_SIZE;
  324. LOG_D("read: index=%u logical_seq=%u sector_start=0x%x record_offset=%u",
  325. index, logical_seq, sector_start, record_offset_in_sector);
  326. sector_buf = rt_malloc(SECTOR_SIZE);
  327. if (sector_buf == RT_NULL)
  328. {
  329. LOG_E("malloc sector buffer failed");
  330. rt_mutex_release(ecode_lock);
  331. return -RT_ERROR;
  332. }
  333. if (fal_partition_read(ecode_part, sector_start, sector_buf, SECTOR_SIZE) < 0)
  334. {
  335. LOG_E("read sector failed");
  336. rt_free(sector_buf);
  337. rt_mutex_release(ecode_lock);
  338. return -RT_ERROR;
  339. }
  340. memcpy(rec, sector_buf + record_offset_in_sector, RECORD_SIZE);
  341. rt_free(sector_buf);
  342. /* 检查是否为全0xFF */
  343. uint8_t *p = (uint8_t *)rec;
  344. int all_ff = 1;
  345. for (uint32_t i = 0; i < RECORD_SIZE; i++)
  346. {
  347. if (p[i] != 0xFF)
  348. {
  349. all_ff = 0;
  350. break;
  351. }
  352. }
  353. if (all_ff)
  354. {
  355. LOG_W("record at index %d is all 0xFF (empty)", index);
  356. rt_mutex_release(ecode_lock);
  357. return -RT_ERROR;
  358. }
  359. if (rec->num > hdr.write_seq)
  360. {
  361. LOG_W("record at index %d has invalid num %u (expected <= %u)", index, rec->num, hdr.write_seq);
  362. rt_mutex_release(ecode_lock);
  363. return -RT_ERROR;
  364. }
  365. rt_mutex_release(ecode_lock);
  366. return RT_EOK;
  367. }
  368. uint32_t ecode_get_count(void)
  369. {
  370. ecode_header_t hdr;
  371. uint32_t count;
  372. if (ensure_initialized() != RT_EOK)
  373. return 0;
  374. rt_mutex_take(ecode_lock, RT_WAITING_FOREVER);
  375. if (read_header(&hdr) != RT_EOK)
  376. {
  377. rt_mutex_release(ecode_lock);
  378. return 0;
  379. }
  380. if (hdr.magic != ECODE_MAGIC || hdr.version != ECODE_VERSION)
  381. {
  382. LOG_E("header corrupted");
  383. rt_mutex_release(ecode_lock);
  384. return 0;
  385. }
  386. count = get_valid_count(hdr.write_seq);
  387. rt_mutex_release(ecode_lock);
  388. return count;
  389. }
  390. void ecode_clear(void)
  391. {
  392. ecode_header_t hdr;
  393. if (ensure_initialized() != RT_EOK)
  394. {
  395. LOG_E("ecode not initialized, abort clear");
  396. return;
  397. }
  398. rt_mutex_take(ecode_lock, RT_WAITING_FOREVER);
  399. if (erase_whole_partition() != RT_EOK)
  400. {
  401. LOG_E("erase whole partition failed");
  402. rt_mutex_release(ecode_lock);
  403. return;
  404. }
  405. hdr.magic = ECODE_MAGIC;
  406. hdr.version = ECODE_VERSION;
  407. hdr.write_seq = 0;
  408. if (write_header(&hdr) != RT_EOK)
  409. {
  410. LOG_E("write header failed after clear");
  411. }
  412. else
  413. {
  414. LOG_I("ecode cleared");
  415. }
  416. rt_mutex_release(ecode_lock);
  417. }
  418. /*=============================================================================
  419. * MSH 命令
  420. *============================================================================*/
  421. #include <finsh.h>
  422. static void recode_test(void)
  423. {
  424. Recode_e_code(e_test_fun, 6, "3-6 是一条测试数据 123456789");
  425. }
  426. MSH_CMD_EXPORT(recode_test, write a test ecode);
  427. static void pre(void)
  428. {
  429. ecode_record_t rec;
  430. uint32_t cnt = ecode_get_count();
  431. LOG_I("ecode count: %u", cnt);
  432. for (uint32_t i = 0; i < cnt; i++)
  433. {
  434. if (ecode_read(i, &rec) == 0)
  435. {
  436. LOG_I("%u - %02u/%02u/%02u %02u:%02u:%02u: (%u-%u) %s",
  437. rec.num,
  438. rec.time_stamp.tm_year, rec.time_stamp.tm_mon, rec.time_stamp.tm_mday,
  439. rec.time_stamp.tm_hour, rec.time_stamp.tm_min, rec.time_stamp.tm_sec,
  440. rec.code, rec.para, rec.detail);
  441. }
  442. else
  443. {
  444. LOG_I("read error at index %u", i);
  445. }
  446. }
  447. }
  448. MSH_CMD_EXPORT(pre, print all ecode records from newest to oldest);
  449. static void delecode(void)
  450. {
  451. ecode_clear();
  452. }
  453. MSH_CMD_EXPORT(delecode, clear all ecode records);
  454. static void ecode_test(int argc, char **argv)
  455. {
  456. ecode_record_t rec;
  457. uint32_t cnt_before, cnt_after;
  458. int i, ret;
  459. LOG_I("===== ecode module test start =====");
  460. ecode_clear();
  461. cnt_before = ecode_get_count();
  462. LOG_I("after clear, count=%u", cnt_before);
  463. if (cnt_before != 0)
  464. {
  465. LOG_E("clear failed");
  466. return;
  467. }
  468. for (i = 0; i < 10; i++)
  469. {
  470. char detail[64];
  471. rt_snprintf(detail, sizeof(detail), "test data %d", i);
  472. Recode_e_code(e_test_fun, i, detail);
  473. }
  474. cnt_after = ecode_get_count();
  475. LOG_I("after write 10, count=%u", cnt_after);
  476. if (cnt_after != 10)
  477. {
  478. LOG_E("write count mismatch");
  479. return;
  480. }
  481. for (i = 0; i < 10; i++)
  482. {
  483. ret = ecode_read(i, &rec);
  484. if (ret != 0)
  485. {
  486. LOG_E("read index %d failed", i);
  487. return;
  488. }
  489. LOG_I("read[%d]: num=%u time=%02u/%02u/%02u %02u:%02u:%02u code=%u para=%u detail=%s",
  490. i, rec.num,
  491. rec.time_stamp.tm_year, rec.time_stamp.tm_mon, rec.time_stamp.tm_mday,
  492. rec.time_stamp.tm_hour, rec.time_stamp.tm_min, rec.time_stamp.tm_sec,
  493. rec.code, rec.para, rec.detail);
  494. if (rec.num != (uint32_t)(9 - i))
  495. {
  496. LOG_E("num mismatch: expected %d, got %u", 9 - i, rec.num);
  497. }
  498. if (rec.code != e_test_fun || rec.para != (uint16_t)(9 - i))
  499. {
  500. LOG_E("code/para mismatch");
  501. }
  502. }
  503. ret = ecode_read(10, &rec);
  504. if (ret == 0)
  505. {
  506. LOG_E("read beyond count should fail");
  507. }
  508. else
  509. {
  510. LOG_I("read beyond count failed as expected");
  511. }
  512. ecode_clear();
  513. cnt_after = ecode_get_count();
  514. if (cnt_after != 0)
  515. {
  516. LOG_E("clear after test failed");
  517. }
  518. else
  519. {
  520. LOG_I("clear ok");
  521. }
  522. LOG_I("===== ecode module test passed =====");
  523. }
  524. MSH_CMD_EXPORT(ecode_test, run ecode module self - test);