/** * @file eeprom_state.c * @brief EEPROM Boot 状态记录 A/B 双槽读写 * * 布局见 boot_layout.h: 记录 A @ EE_BOOT_REC_A_ADDR, B @ EE_BOOT_REC_B_ADDR, 各 64B。 * 规则: * 读: 两条都读, magic+crc16 各自过滤, 有效者取 seq 大(32bit 差值比较防回绕)。 * 写: 最新记录所在槽位取反, 写"另一个"槽(完整新记录 seq+1), 回读 verify, * 失败重试 ≤3; 仍失败返回错误但不破坏旧槽。 * 依赖: Int_eeprom (BL24C16F, I2C2)。crc16 覆盖 rec 头部到 crc16 字段之前。 */ #include "eeprom_state.h" #include "Int_eeprom.h" #include "boot_crc.h" #include #include /* crc16 覆盖范围: 记录起始 ~ crc16 字段之前(boot_layout.h 口径: 0x00~0x2C, 45 B)。 * 注意!终点只能取 crc16 字段自身, 不能取 &r->padding —— 那会把 crc16 自己圈进校验范围: * 写入按旧值算、校验按新值算 ⇒ rec_check() 永远失败 ⇒ 升级链路整条断掉 * (BL 侧表现为 commit 从未成功、err=81 恒现、YMODEM 传完后一行 PROGRAMMING 都不打; * App 侧表现为 Task_update_confirm() 永远提交不了 ⇒ 重启两次即回滚)。 * 注意!本文件在 Bootloader 与 App 下各有一份, 改动必须两处同步。 */ #define BOOT_REC_COVER_LEN ((uint16_t)offsetof(boot_record_t, crc16)) static boot_record_t s_rec; /* RAM 影子 */ static uint8_t s_valid; /* 影子可用(上电读到有效记录或已合成默认) */ /* crc16 重算/校验 */ static uint16_t rec_calc_crc(const boot_record_t *r) { return boot_crc16_ccitt((const uint8_t *)r, (uint32_t)BOOT_REC_COVER_LEN); } static uint8_t rec_check(const boot_record_t *r) { if (r->magic != BOOT_REC_MAGIC) return 0; return (r->crc16 == rec_calc_crc(r)) ? 1 : 0; } static uint8_t rec_read(uint16_t ee_addr, boot_record_t *out) { if (Int_eeprom_read((uint8_t *)out, ee_addr, EE_BOOT_REC_SIZE) != 0) return 0; return rec_check(out); } /* 写一条记录到指定槽并回读验证 */ static uint8_t rec_write(uint16_t ee_addr, const boot_record_t *in) { uint8_t i; for (i = 0; i < 3; i++) { if (Int_eeprom_write((uint8_t *)in, ee_addr, EE_BOOT_REC_SIZE) != 0) continue; { boot_record_t rd; if (rec_read(ee_addr, &rd) && memcmp(&rd, in, EE_BOOT_REC_SIZE) == 0) return 1; } } return 0; } /* 上电自检标记: 是否有槽"读不回来"(I2C 层失败)。用于把"记录区不通"与 * "片是空的(新板/擦过)"区分开 —— 后者的 magic 全 0xFF, 是正常现象, 不该报警。 */ static uint8_t s_rd_fail; void eeprom_state_init(void) { boot_record_t a, b; uint8_t fa = (Int_eeprom_read((uint8_t *)&a, EE_BOOT_REC_A_ADDR, EE_BOOT_REC_SIZE) != 0) ? 1U : 0U; uint8_t fb = (Int_eeprom_read((uint8_t *)&b, EE_BOOT_REC_B_ADDR, EE_BOOT_REC_SIZE) != 0) ? 1U : 0U; uint8_t va = fa ? 0U : rec_check(&a); uint8_t vb = fb ? 0U : rec_check(&b); s_rd_fail = (fa || fb) ? 1U : 0U; s_valid = 0; if (va && vb) { int32_t d = (int32_t)(a.seq - b.seq); s_rec = (d >= 0) ? a : b; s_valid = 1; } else if (va) { s_rec = a; s_valid = 1; } else if (vb) { s_rec = b; s_valid = 1; } if (!s_valid) { /* 新板/双记录损坏: 合成 IDLE 默认影子, 但本次不落盘 * (避免 EEPROM 写故障时无限尝试; 首个状态变化时再写) */ memset(&s_rec, 0, sizeof(s_rec)); s_rec.magic = BOOT_REC_MAGIC; s_rec.seq = 0; s_rec.state = BOOT_STATE_IDLE; s_rec.last_err = BOOT_ERR_EEPROM_FAIL; s_rec.backup_valid = BOOT_BACKUP_INVALID; s_rec.confirm_state = BOOT_CONFIRM_OK; s_rec.crc16 = rec_calc_crc(&s_rec); } } uint8_t eeprom_state_read_fail(void) { return s_rd_fail; } const boot_record_t *eeprom_state_get(void) { return &s_rec; } boot_record_t *eeprom_state_modify(void) { return &s_rec; } uint8_t eeprom_state_commit(void) { boot_record_t a, b; uint8_t va = rec_read(EE_BOOT_REC_A_ADDR, &a); uint8_t vb = rec_read(EE_BOOT_REC_B_ADDR, &b); uint16_t next_addr; uint32_t next_seq; /* seq 基于当前盘上最新记录 +1 (不信任影子 seq, 防双槽读失败时倒退) */ if (va && vb) { int32_t d = (int32_t)(a.seq - b.seq); next_addr = (d >= 0) ? EE_BOOT_REC_B_ADDR : EE_BOOT_REC_A_ADDR; next_seq = ((d >= 0) ? a.seq : b.seq) + 1U; } else if (va) { next_addr = EE_BOOT_REC_B_ADDR; next_seq = a.seq + 1U; } else if (vb) { next_addr = EE_BOOT_REC_A_ADDR; next_seq = b.seq + 1U; } else { next_addr = EE_BOOT_REC_A_ADDR; next_seq = 1U; } s_rec.magic = BOOT_REC_MAGIC; s_rec.seq = next_seq; s_rec.crc16 = rec_calc_crc(&s_rec); if (!rec_write(next_addr, &s_rec)) { s_rec.last_err = BOOT_ERR_EEPROM_FAIL; return 1; } s_valid = 1; return 0; } uint8_t eeprom_state_loaded_ok(void) { return s_valid; }