| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- /*
- * Copyright (c) 2026, BZY
- *
- * Anti-loss parameter mirror: store a packed copy of key config fields in
- * two dedicated sectors at the tail of the FAL "easyflash" partition.
- *
- * The mirror is independent from EasyFlash's GC/log areas. It protects
- * against:
- * - whole sector bit-rot / wear failure
- * - partial writes caused by power loss in the middle of EF set
- *
- * Layout (each slot fits in one 4096-byte sector):
- *
- * offset field
- * ------ -------------------------------------------------
- * 0x00 magic (0x50415242 'PARB')
- * 0x04 version (layout version, bump on incompatible change)
- * 0x08 write_seq (monotonic, used to pick newest copy)
- * 0x0C crc32 (over payload only)
- * 0x10 payload (param_payload_t, packed)
- *
- * Slot A : MIRROR_BASE_ADDR .. + MIRROR_SLOT_SIZE
- * Slot B : MIRROR_BASE_ADDR + SLOT .. + MIRROR_SLOT_SIZE
- */
- #ifndef APPLICATIONS_PARAM_MIRROR_H_
- #define APPLICATIONS_PARAM_MIRROR_H_
- #include <rtthread.h>
- #include <stdint.h>
- #include <stdbool.h>
- /* Packed copy of every user-settable config field.
- * Keep this struct append-only. When layout changes, bump PARAM_PAYLOAD_VER
- * and (optionally) keep a converter from the old version. */
- typedef struct __packed {
- /* version + bookkeeping, NOT config fields */
- uint32_t magic; /* 'PARB' */
- uint32_t version; /* PARAM_PAYLOAD_VER */
- uint32_t write_seq; /* monotonic counter */
- uint32_t crc32; /* over fields below */
- uint8_t iSave;
- uint8_t dtype;
- uint8_t ds;
- uint8_t clt_time;
- uint8_t heattime;
- uint8_t heatvalue;
- uint16_t thsp;
- uint8_t sunBar;
- uint8_t startBar;
- uint8_t stopBar;
- uint8_t timeSwitch;
- uint8_t tph;
- int8_t tpl;
- uint8_t shake_ws;
- uint8_t shake_sec;
- int passWord;
- uint16_t dattim;
- uint8_t version_type;
- uint8_t Power_Type;
- uint8_t handle_bugger;
- } param_payload_t;
- #define PARAM_PAYLOAD_VER 0x00010001U /* bump on incompatible change */
- /* Mirror slot layout (one 4096-byte sector per slot).
- * Place the mirror after ENV_AREA_SIZE (64KB) and LOG_AREA_SIZE (8KB),
- * inside the 3MB "easyflash" FAL partition.
- *
- * 2026-10-29:ENV_AREA_SIZE 由 1MB 降为 64KB(见 EasyFlash 的 ef_cfg.h),
- * 而 ef_log.c:101 的日志区起始 = EF_START_ADDR + ENV_AREA_SIZE,会随之从
- * 0x100000 前移到 0x10000。镜像必须同步前移,否则会落在日志区里被互相破坏。
- * 布局:ENV 0x00000~0x10000 | LOG 0x10000~0x12000 | 镜像A 0x12000 | 镜像B 0x13000 */
- #define MIRROR_SECTOR_SIZE 4096U
- #define MIRROR_BASE_OFFSET (64UL * 1024UL + 8UL * 1024UL) /* 0x12000 */
- #define MIRROR_SLOT_SIZE MIRROR_SECTOR_SIZE
- #define MIRROR_SLOT_A_ADDR (MIRROR_BASE_OFFSET)
- #define MIRROR_SLOT_B_ADDR (MIRROR_BASE_OFFSET + MIRROR_SLOT_SIZE)
- /**
- * Load the latest valid mirror copy into dst. Tries slot A and slot B,
- * picks the one with the higher write_seq and a correct CRC.
- *
- * @return RT_EOK if a valid copy was found, -RT_ERROR otherwise.
- */
- int param_mirror_load(param_payload_t *dst);
- /**
- * Erase both mirror slots and invalidate the in-RAM "last payload" cache.
- *
- * 2026-10-29 新增(供远程擦除 Flash 指令使用)。**必须清缓存**:本模块用
- * s_last_payload/s_last_valid 做写放大抑制,若只擦 Flash 不清缓存,后续
- * param_mirror_sync() 会因"内容未变化"直接返回,镜像将永久为空、失去防丢保护。
- *
- * @return RT_EOK on success.
- */
- int param_mirror_erase_all(void);
- /**
- * Atomically store src to the mirror. Writes to the older slot first,
- * verifies, then writes to the other slot. Bumps write_seq so the next
- * load prefers the freshly written copy.
- *
- * @return RT_EOK on success.
- */
- int param_mirror_save(const param_payload_t *src);
- /**
- * Pack the relevant fields from device_status into out.
- * Used by Check_ParamConfig() to seed the mirror with defaults.
- */
- void param_mirror_pack_defaults(param_payload_t *out);
- /**
- * Convenience: pack current device_status into a payload and write to the
- * mirror. Call this from any code path that mutates persistent config so
- * the mirror stays in sync with EF.
- *
- * Implemented in param_mirror.c; safe to call before Check_ParamConfig().
- */
- void param_mirror_sync(void);
- /**
- * Apply mirror fields back into device_status. Used during boot to
- * recover from a lost iSave when the mirror itself is intact.
- *
- * @return the mirror's write_seq on success, 0 on any failure (e.g. CRC
- * mismatch — caller should fall back to defaults in that case).
- */
- uint32_t mirror_to_device_status(const param_payload_t *in);
- #endif /* APPLICATIONS_PARAM_MIRROR_H_ */
|