|
|
@@ -0,0 +1,117 @@
|
|
|
+/*
|
|
|
+ * 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 (1MB) and LOG_AREA_SIZE (8KB),
|
|
|
+ * inside the 3MB "easyflash" FAL partition. */
|
|
|
+#define MIRROR_SECTOR_SIZE 4096U
|
|
|
+#define MIRROR_BASE_OFFSET (1UL * 1024UL * 1024UL + 8UL * 1024UL) /* 0x102000 */
|
|
|
+#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);
|
|
|
+
|
|
|
+/**
|
|
|
+ * 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_ */
|