ecode.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #ifndef __ECODE_H__
  11. #define __ECODE_H__
  12. #include <rtthread.h>
  13. #include <stdint.h>
  14. /* 魔数:用于标识分区已被初始化 ("ECOD" 的 ASCII) */
  15. #define ECODE_MAGIC 0xEC0DE09D
  16. #define ECODE_VERSION 3
  17. #include "peri.h"
  18. extern struct rtc_time time_now;
  19. /* 错误码枚举(与原一致) */
  20. typedef enum
  21. {
  22. e_reboot = 1,
  23. e_clearecode,
  24. e_test_fun,
  25. e_update_timeout,
  26. e_bak_restore,
  27. e_bak_backup,
  28. e_update,
  29. e_update_ok,
  30. e_update_fail,
  31. e_update_WSUCC,
  32. e_update_WFAIL,
  33. e_hardfault,
  34. e_iwdg,
  35. e_mqtt0_outtime,
  36. e_BL,
  37. e_typeMAX
  38. } ecode_list;
  39. typedef struct __attribute__((packed))
  40. {
  41. struct TM time_stamp; // 时间戳
  42. uint16_t code; // 异常码
  43. uint16_t para; // 附带参数
  44. uint32_t num; // 序号(全局递增)
  45. char detail[50]; // 内容
  46. } ecode_record_t;
  47. /* 模块初始化(可显式调用,也可由内部自动触发) */
  48. int ecode_init(void);
  49. /* 写入一条记录(与原有函数名完全一致) */
  50. void Recode_e_code(ecode_list code, uint16_t para, char *detail);
  51. /* 读取最新第 index 条记录(index=0 为最新) */
  52. int ecode_read(uint32_t index, ecode_record_t *rec);
  53. /* 获取当前有效记录总数 */
  54. uint32_t ecode_get_count(void);
  55. /* 清空所有记录(重置头部,并可选擦除数据区) */
  56. void ecode_clear(void);
  57. #endif /* __ECODE_H__ */