param_mirror.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Anti-loss parameter mirror implementation.
  3. *
  4. * Lives in the FAL "easyflash" partition. Uses two slots (A/B) for redundancy,
  5. * each slot is one flash sector (4096 bytes on this board).
  6. */
  7. #include <rtthread.h>
  8. #include <rtdevice.h>
  9. #include <fal.h>
  10. #include <string.h>
  11. #include "param_mirror.h"
  12. #include "control.h"
  13. #define DBG_TAG "param_mirror"
  14. #define DBG_LVL DBG_LOG
  15. #include <rtdbg.h>
  16. #define MIRROR_PART_NAME "easyflash"
  17. #define MIRROR_MAGIC 0x50415242U /* 'PARB' */
  18. /* Header lives in the first 16 bytes of each slot, payload follows. */
  19. #define HDR_SIZE 16U
  20. Device_Status_typedef device_status;
  21. static const struct fal_partition *s_part = RT_NULL;
  22. /* ---- 防写放大/并发保护 ----
  23. * 1) 内容未变化时直接跳过(避免每条 JSON/每次同步都擦写 2 个扇区,耗尽 Flash 寿命);
  24. * 2) 用互斥锁串行化多线程的镜像读写(HMI/DTU/工作流程线程都会调用)。
  25. */
  26. static struct rt_mutex s_mirror_lock;
  27. static rt_bool_t s_mirror_lock_ready = RT_FALSE;
  28. static param_payload_t s_last_payload;
  29. static rt_bool_t s_last_valid = RT_FALSE;
  30. static void mirror_lock(void)
  31. {
  32. if (!s_mirror_lock_ready)
  33. {
  34. rt_base_t level = rt_hw_interrupt_disable();
  35. if (!s_mirror_lock_ready)
  36. {
  37. if (rt_mutex_init(&s_mirror_lock, "pm_mtx", RT_IPC_FLAG_PRIO) == RT_EOK)
  38. {
  39. s_mirror_lock_ready = RT_TRUE;
  40. }
  41. }
  42. rt_hw_interrupt_enable(level);
  43. }
  44. if (s_mirror_lock_ready)
  45. {
  46. rt_mutex_take(&s_mirror_lock, RT_WAITING_FOREVER);
  47. }
  48. }
  49. static void mirror_unlock(void)
  50. {
  51. if (s_mirror_lock_ready)
  52. {
  53. rt_mutex_release(&s_mirror_lock);
  54. }
  55. }
  56. /* 只比较配置体(跳过 magic/version/write_seq/crc32 头部) */
  57. static rt_bool_t payload_body_equal(const param_payload_t *a, const param_payload_t *b)
  58. {
  59. return (rt_memcmp((const rt_uint8_t *)a + HDR_SIZE,
  60. (const rt_uint8_t *)b + HDR_SIZE,
  61. sizeof(param_payload_t) - HDR_SIZE) == 0) ? RT_TRUE : RT_FALSE;
  62. }
  63. static uint32_t calc_crc32(const void *buf, size_t size)
  64. {
  65. /* EF ships ef_calc_crc32; reuse to avoid bringing our own table. */
  66. extern uint32_t ef_calc_crc32(uint32_t crc, const void *buf, size_t size);
  67. return ef_calc_crc32(0, buf, size);
  68. }
  69. static int slot_read(uint32_t slot_addr, param_payload_t *out)
  70. {
  71. if (s_part == RT_NULL)
  72. {
  73. return -RT_ERROR;
  74. }
  75. return (fal_partition_read(s_part, slot_addr, (uint8_t *)out,
  76. sizeof(param_payload_t)) > 0)
  77. ? RT_EOK : -RT_ERROR;
  78. }
  79. static int slot_write(uint32_t slot_addr, const param_payload_t *in)
  80. {
  81. if (s_part == RT_NULL)
  82. {
  83. return -RT_ERROR;
  84. }
  85. if (fal_partition_erase(s_part, slot_addr, MIRROR_SLOT_SIZE) < 0)
  86. {
  87. return -RT_ERROR;
  88. }
  89. if (fal_partition_write(s_part, slot_addr, (const uint8_t *)in,
  90. sizeof(param_payload_t)) < 0)
  91. {
  92. return -RT_ERROR;
  93. }
  94. return RT_EOK;
  95. }
  96. static bool slot_is_valid(const param_payload_t *p)
  97. {
  98. if (p->magic != MIRROR_MAGIC)
  99. {
  100. return false;
  101. }
  102. if (p->version != PARAM_PAYLOAD_VER)
  103. {
  104. return false;
  105. }
  106. /* crc32 covers everything after the 16-byte header. */
  107. const uint8_t *body = (const uint8_t *)p + HDR_SIZE;
  108. size_t body_len = sizeof(param_payload_t) - HDR_SIZE;
  109. uint32_t crc = calc_crc32(body, body_len);
  110. return crc == p->crc32;
  111. }
  112. int param_mirror_load(param_payload_t *dst)
  113. {
  114. if (dst == RT_NULL)
  115. {
  116. return -RT_ERROR;
  117. }
  118. if (s_part == RT_NULL)
  119. {
  120. s_part = fal_partition_find(MIRROR_PART_NAME);
  121. if (s_part == RT_NULL)
  122. {
  123. return -RT_ERROR;
  124. }
  125. }
  126. param_payload_t a, b;
  127. bool va = (slot_read(MIRROR_SLOT_A_ADDR, &a) == RT_EOK) && slot_is_valid(&a);
  128. bool vb = (slot_read(MIRROR_SLOT_B_ADDR, &b) == RT_EOK) && slot_is_valid(&b);
  129. if (!va && !vb)
  130. {
  131. return -RT_ERROR;
  132. }
  133. if (va && !vb)
  134. {
  135. *dst = a;
  136. }
  137. else if (!va && vb)
  138. {
  139. *dst = b;
  140. }
  141. else
  142. {
  143. /* both valid: pick the one with higher write_seq */
  144. *dst = (a.write_seq >= b.write_seq) ? a : b;
  145. }
  146. /* 记录已加载内容:后续 param_mirror_sync() 内容相同则跳过擦写(防写放大) */
  147. s_last_payload = *dst;
  148. s_last_valid = RT_TRUE;
  149. return RT_EOK;
  150. }
  151. int param_mirror_save(const param_payload_t *src)
  152. {
  153. if (src == RT_NULL)
  154. {
  155. return -RT_ERROR;
  156. }
  157. if (s_part == RT_NULL)
  158. {
  159. s_part = fal_partition_find(MIRROR_PART_NAME);
  160. if (s_part == RT_NULL)
  161. {
  162. return -RT_ERROR;
  163. }
  164. }
  165. /* Determine which slot is older by reading both and comparing write_seq. */
  166. param_payload_t a, b;
  167. bool va = (slot_read(MIRROR_SLOT_A_ADDR, &a) == RT_EOK) && slot_is_valid(&a);
  168. bool vb = (slot_read(MIRROR_SLOT_B_ADDR, &b) == RT_EOK) && slot_is_valid(&b);
  169. param_payload_t newer, older;
  170. uint32_t newer_addr, older_addr;
  171. uint32_t next_seq;
  172. if (!va && !vb)
  173. {
  174. /* cold start: both empty. Write B first, then A. */
  175. newer_addr = MIRROR_SLOT_B_ADDR;
  176. older_addr = MIRROR_SLOT_A_ADDR;
  177. next_seq = 1;
  178. }
  179. else if (va && !vb)
  180. {
  181. newer = a;
  182. newer_addr = MIRROR_SLOT_A_ADDR;
  183. older_addr = MIRROR_SLOT_B_ADDR;
  184. next_seq = newer.write_seq + 1;
  185. }
  186. else if (!va && vb)
  187. {
  188. newer = b;
  189. newer_addr = MIRROR_SLOT_B_ADDR;
  190. older_addr = MIRROR_SLOT_A_ADDR;
  191. next_seq = newer.write_seq + 1;
  192. }
  193. else
  194. {
  195. /* both valid */
  196. if (a.write_seq >= b.write_seq)
  197. {
  198. newer = a; newer_addr = MIRROR_SLOT_A_ADDR;
  199. older = b; older_addr = MIRROR_SLOT_B_ADDR;
  200. }
  201. else
  202. {
  203. newer = b; newer_addr = MIRROR_SLOT_B_ADDR;
  204. older = a; older_addr = MIRROR_SLOT_A_ADDR;
  205. }
  206. next_seq = newer.write_seq + 1;
  207. }
  208. /* Build the record. Reuse the caller's payload for everything except
  209. * magic/version/seq/crc32, which we own. */
  210. param_payload_t out = *src;
  211. out.magic = MIRROR_MAGIC;
  212. out.version = PARAM_PAYLOAD_VER;
  213. out.write_seq = next_seq;
  214. /* Recompute crc32 over the body (everything past the 16-byte header). */
  215. uint8_t *body = (uint8_t *)&out + HDR_SIZE;
  216. size_t body_len = sizeof(param_payload_t) - HDR_SIZE;
  217. out.crc32 = calc_crc32(body, body_len);
  218. /* Write the older slot first, then the newer slot.
  219. * If we crash between the two writes, the loader still finds a valid
  220. * copy in the previous-newer slot (with seq = next_seq - 1). */
  221. if (slot_write(older_addr, &out) != RT_EOK)
  222. {
  223. LOG_W("mirror write older slot (0x%08x) failed", older_addr);
  224. return -RT_ERROR;
  225. }
  226. if (slot_write(newer_addr, &out) != RT_EOK)
  227. {
  228. LOG_W("mirror write newer slot (0x%08x) failed", newer_addr);
  229. /* Still acceptable: loader can fall back to the older copy. */
  230. }
  231. return RT_EOK;
  232. }
  233. void param_mirror_pack_defaults(param_payload_t *out)
  234. {
  235. memset(out, 0, sizeof(*out));
  236. /* Caller will overwrite magic/version/seq/crc32 when actually saving.
  237. * Pre-fill magic/version here so a subsequent read still validates. */
  238. out->magic = MIRROR_MAGIC;
  239. out->version = PARAM_PAYLOAD_VER;
  240. /* write_seq left at 0; will be set on first save. */
  241. }
  242. /* The three helpers below bridge between device_status (defined in control.h)
  243. * and the packed mirror layout. They live here (instead of hmi.c) so any
  244. * code path that touches persistent config can call param_mirror_sync()
  245. * without needing to know device_status internals. */
  246. static void device_status_to_mirror(param_payload_t *out)
  247. {
  248. /* Zero the whole struct so header fields (magic/version/seq/crc32)
  249. * are clean before param_mirror_save() overwrites them. */
  250. memset(out, 0, sizeof(*out));
  251. out->iSave = device_status.iSave;
  252. out->dtype = device_status.dtype;
  253. out->ds = device_status.ds;
  254. out->clt_time = device_status.clt_time;
  255. out->heattime = device_status.heattime;
  256. out->heatvalue = device_status.heatvalue;
  257. out->thsp = device_status.thsp;
  258. out->sunBar = device_status.sunBar;
  259. out->startBar = device_status.startBar;
  260. out->stopBar = device_status.stopBar;
  261. out->timeSwitch = device_status.timeSwitch;
  262. out->tph = device_status.tph;
  263. out->tpl = device_status.tpl;
  264. out->shake_ws = device_status.shake_ws;
  265. out->shake_sec = device_status.shake_sec;
  266. out->passWord = device_status.passWord;
  267. out->dattim = device_status.dattim;
  268. out->version_type = device_status.version_type;
  269. out->Power_Type = device_status.Power_Type;
  270. out->handle_bugger = device_status.handle_bugger;
  271. }
  272. uint32_t mirror_to_device_status(const param_payload_t *in)
  273. {
  274. device_status.iSave = in->iSave;
  275. device_status.dtype = in->dtype;
  276. device_status.ds = in->ds;
  277. device_status.clt_time = in->clt_time;
  278. device_status.heattime = in->heattime;
  279. device_status.heatvalue = in->heatvalue;
  280. device_status.thsp = in->thsp;
  281. device_status.sunBar = in->sunBar;
  282. device_status.startBar = in->startBar;
  283. device_status.stopBar = in->stopBar;
  284. device_status.timeSwitch = in->timeSwitch;
  285. device_status.tph = in->tph;
  286. device_status.tpl = in->tpl;
  287. device_status.shake_ws = in->shake_ws;
  288. device_status.shake_sec = in->shake_sec;
  289. device_status.passWord = in->passWord;
  290. device_status.dattim = in->dattim;
  291. device_status.version_type = in->version_type;
  292. device_status.Power_Type = in->Power_Type;
  293. device_status.handle_bugger = in->handle_bugger;
  294. return in->write_seq;
  295. }
  296. /* NOTE: do NOT name the local `p` here — rtdbg.h defines a file-scope
  297. * `struct tm *p` and any LOG_* call inside this function would expand
  298. * `p = localtime(&timep)` and clash with our local type. */
  299. void param_mirror_sync(void)
  300. {
  301. param_payload_t payload;
  302. device_status_to_mirror(&payload);
  303. mirror_lock();
  304. /* 内容未变化:直接跳过。避免每条 JSON(心跳/查询应答) 都触发
  305. * 两次 4KB 扇区擦除,既保护 Flash 寿命也避免阻塞串口接收线程。 */
  306. if (s_last_valid && payload_body_equal(&payload, &s_last_payload))
  307. {
  308. mirror_unlock();
  309. return;
  310. }
  311. if (param_mirror_save(&payload) != RT_EOK)
  312. {
  313. LOG_W("param mirror save failed");
  314. mirror_unlock();
  315. return;
  316. }
  317. s_last_payload = payload;
  318. s_last_valid = RT_TRUE;
  319. mirror_unlock();
  320. }