A truly modular keyboard
C
keyword: dock keeps its own resume point, don't give up on halt runs
Nathan38e1cc77b940
diff --git a/firmware/keyword/Core/Src/main.c b/firmware/keyword/Core/Src/main.cindex 40cc043..2098d11 100644--- a/firmware/keyword/Core/Src/main.c+++ b/firmware/keyword/Core/Src/main.c@@ -45,6 +45,7 @@#define KEYBIT_POLL_MS 250#define KEYBIT_SCAN_MS 250#define KEYBIT_KEYS_MS 5+#define HALT_TRIES_MAX 60 /* dock polls (250 ms) with a failed halt before giving up */#define KEYBIT_MISSES_MAX 2 /* scans a Keybit may miss in a row before it counts as gone *//* USER CODE END PD */@@ -159,6 +160,8 @@ static ch32_status_t Keybit_Dock_Uid(uint32_t uid[3])/* One flash attempt on the docked board: done if it verifies or programs,* failed once 5 attempts in a row get nowhere. */static int32_t best_blk; /* furthest block the current board's attempts got to */+static uint32_t next_blk; /* where flashing the current board carries on */+static uint8_t checked; /* the current board's whole image has been checked once */static uint8_t stalls; /* attempts in a row that got no further than where they started *//* Boards flashed or found up to date since boot; the image is fixed per@@ -189,6 +192,8 @@ static void Keybit_Dock_Start(const uint32_t uid[3])}dock_state = DOCK_WORKING;best_blk = -1;+ next_blk = 0;+ checked = 0;stalls = 0;LED_Update(); /* blue while flashing */}@@ -202,6 +207,19 @@ static void Keybit_Dock_Attempt(const uint32_t uid[3])* If the halt fails anyway, detect again and give it one more go. */ch32_status_t h = ch32_halt(&keybit_swio, 0);if (h != CH32_OK && ch32_detect(&keybit_swio) == CH32_OK) h = ch32_halt(&keybit_swio, 0);+ /* The halt fails in runs and then goes through, so a failed one isn't a+ * stuck attempt: just try again next poll, and only give up after+ * HALT_TRIES_MAX of them in a row (~15 s). */+ static uint8_t halt_fails;+ if (h != CH32_OK) {+ if (++halt_fails >= HALT_TRIES_MAX) {+ halt_fails = 0;+ dock_state = DOCK_FAILED;+ PROTO_Send("DOCK failed: halt=%d", h);+ }+ return;+ }+ halt_fails = 0;/* The first words all reading blank is more likely a board that isn't* seated yet than an erased one, so give it about a second before* believing it (an attempt that died after erasing block 0 does leave it blank). */@@ -213,8 +231,8 @@ static void Keybit_Dock_Attempt(const uint32_t uid[3])if (blank && ++blank_polls < 4) return;}blank_polls = 0;- /* Mid-flash there's nothing to check yet: carry straight on. */- int resuming = h == CH32_OK && ch32_resume_pending(uid);+ /* Check the whole image once per docking; after that, carry straight on. */+ int resuming = h == CH32_OK && checked;ch32_status_t v = resuming ? CH32_ERR_VERIFY: h == CH32_OK ? ch32_verify(&keybit_swio, CH32_FLASH_BASE, keybit_image, keybit_image_len) : h;/* Only a real mismatch means it needs flashing; a garbled read just gets checked again. */@@ -232,7 +250,12 @@ static void Keybit_Dock_Attempt(const uint32_t uid[3])PROTO_Send("DOCK up to date");return;}- ch32_status_t p = ch32_program(&keybit_swio, keybit_image, keybit_image_len);+ if (h == CH32_OK && !checked && v == CH32_ERR_VERIFY) {+ /* Everything before the first mismatch already matches. */+ checked = 1;+ next_blk = (ch32_bad_addr - CH32_FLASH_BASE) / CH32_FLASH_BLOCK;+ }+ ch32_status_t p = ch32_program(&keybit_swio, keybit_image, keybit_image_len, &next_blk);if (p == CH32_MORE) { /* a couple of blocks done; the rest next poll */stalls = 0;return;diff --git a/firmware/keyword/Drivers/CH32V003/ch32v003_swio.c b/firmware/keyword/Drivers/CH32V003/ch32v003_swio.cindex d583208..143d0a5 100644--- a/firmware/keyword/Drivers/CH32V003/ch32v003_swio.c+++ b/firmware/keyword/Drivers/CH32V003/ch32v003_swio.c@@ -631,15 +631,6 @@ uint32_t ch32_retries;int32_t ch32_fail_blk = -1;uint32_t ch32_resume_from;-/* Where the last unfinished ch32_program got to, and on which chip (UID),- * so the next attempt on the same chip carries on instead of starting over. */-static struct { uint32_t uid[3]; uint32_t next_blk; uint8_t valid; } resume;--int ch32_resume_pending(const uint32_t uid[3])-{- return resume.valid && resume.next_blk > 0 && memcmp(uid, resume.uid, sizeof(resume.uid)) == 0;-}-/* Blocks per ch32_program call: few enough that the caller gets its main* loop (the side bus, USB) back every couple of hundred ms. */#define BLOCKS_PER_CALL 2@@ -661,7 +652,7 @@ static void recover(ch32_swio_t *s)/* Flashes the image block by block. A frame now and then still comes back* garbled, and a flash takes thousands of them, so each step gets a few* tries: the unlock up to 5, each 64-byte block (written, then read back) up to 8. */-ch32_status_t ch32_program(ch32_swio_t *s, const uint8_t *image, uint32_t len)+ch32_status_t ch32_program(ch32_swio_t *s, const uint8_t *image, uint32_t len, uint32_t *next_blk){ch32_status_t r;ch32_retries = 0;@@ -683,22 +674,10 @@ ch32_status_t ch32_program(ch32_swio_t *s, const uint8_t *image, uint32_t len)}if (r != CH32_OK) return r;- /* Only trust the UID when two reads agree, or a garbled one restarts at block 0. */- uint32_t uid[3], again[3];- int have_uid = 0;- for (int t = 0; t < 3 && !have_uid; t++) {- have_uid = 1;- for (int i = 0; i < 3; i++)- if (ch32_read_word(s, 0x1FFFF7E8u + 4u * i, &uid[i]) != CH32_OK ||- ch32_read_word(s, 0x1FFFF7E8u + 4u * i, &again[i]) != CH32_OK || uid[i] != again[i]) have_uid = 0;- }- if (!have_uid || !resume.valid || memcmp(uid, resume.uid, sizeof(uid)) != 0) resume.next_blk = 0;- resume.valid = have_uid;- if (have_uid) memcpy(resume.uid, uid, sizeof(uid));- ch32_resume_from = resume.next_blk;+ ch32_resume_from = *next_blk;uint32_t done = 0;- for (uint32_t off = resume.next_blk * CH32_FLASH_BLOCK; off < len; off += CH32_FLASH_BLOCK) {+ for (uint32_t off = *next_blk * CH32_FLASH_BLOCK; off < len; off += CH32_FLASH_BLOCK) {if (done++ == BLOCKS_PER_CALL) return CH32_MORE;uint32_t n = len - off < CH32_FLASH_BLOCK ? len - off : CH32_FLASH_BLOCK;IWDG->KR = 0xAAAA; /* the whole image takes longer than the bootloader's ~8 s watchdog */@@ -714,9 +693,8 @@ ch32_status_t ch32_program(ch32_swio_t *s, const uint8_t *image, uint32_t len)ch32_fail_blk = (int32_t)(off / CH32_FLASH_BLOCK);return r;}- resume.next_blk = off / CH32_FLASH_BLOCK + 1;+ *next_blk = off / CH32_FLASH_BLOCK + 1;}- resume.valid = 0;ch32_write_word(s, CH_FLASH_CTLR, 0);ch32_reboot(s);return CH32_OK;diff --git a/firmware/keyword/Drivers/CH32V003/ch32v003_swio.h b/firmware/keyword/Drivers/CH32V003/ch32v003_swio.hindex b57254a..70ab746 100644--- a/firmware/keyword/Drivers/CH32V003/ch32v003_swio.h+++ b/firmware/keyword/Drivers/CH32V003/ch32v003_swio.h@@ -22,13 +22,11 @@ extern uint32_t ch32_t1, ch32_t0, ch32_hi;extern uint32_t ch32_t_kick; /* push-pull read: cycles driven high before release (DOCK? k=) */extern uint32_t ch32_t_sample; /* read: cycles from release to sampling (DOCK? s=) */extern uint8_t ch32_open_drain;-extern uint32_t ch32_last_acs;+extern uint32_t ch32_last_acs; /* last DMABSTRACTCS read while waiting on a command */extern uint32_t ch32_bad_addr, ch32_bad_want, ch32_bad_got; /* first mismatch ch32_verify found */extern uint32_t ch32_retries; /* steps ch32_program had to retry */extern int32_t ch32_fail_blk; /* block ch32_program gave up on, -1 if none */-extern uint32_t ch32_resume_from;-/* 1 if ch32_program has unfinished work for the chip with this UID. */-int ch32_resume_pending(const uint32_t uid[3]); /* block ch32_program started at (resumed from an earlier try) */ /* last DMABSTRACTCS read while waiting on a command */+extern uint32_t ch32_resume_from; /* block the last ch32_program call started at *//* Idle time after each frame. 2 us is sometimes too short. */#ifndef CH32_SWIO_FRAME_GAP_US#define CH32_SWIO_FRAME_GAP_US 8@@ -126,8 +124,10 @@ ch32_status_t ch32_write_flash(ch32_swio_t *s, uint32_t addr,ch32_status_t ch32_verify(ch32_swio_t *s, uint32_t addr,const uint8_t *data, uint32_t len);-/* Whole job: detect, halt with reset, flash image at 0x08000000, verify,- * reboot. */-ch32_status_t ch32_program(ch32_swio_t *s, const uint8_t *image, uint32_t len);+/* Flash image at 0x08000000 from block *next_blk on, a couple of blocks per+ * call: CH32_MORE means call again; *next_blk moves past every block written+ * and read back, so the caller decides where to carry on. Reboots the chip+ * when the last block is done. */+ch32_status_t ch32_program(ch32_swio_t *s, const uint8_t *image, uint32_t len, uint32_t *next_blk);#endif /* CH32V003_SWIO_H */