A truly modular keyboard
C
keyword: flash a docked Keybit two blocks at a time
Nathaned1d388ba633
diff --git a/firmware/keyword/Core/Src/main.c b/firmware/keyword/Core/Src/main.cindex ea64cae..35f1c14 100644--- a/firmware/keyword/Core/Src/main.c+++ b/firmware/keyword/Core/Src/main.c@@ -211,12 +211,15 @@ static void Keybit_Dock_Attempt(const uint32_t uid[3])if (blank && ++blank_polls < 4) return;}blank_polls = 0;- ch32_status_t v = h == CH32_OK ? ch32_verify(&keybit_swio, CH32_FLASH_BASE, keybit_image, keybit_image_len) : h;+ /* Mid-flash there's nothing to check yet: carry straight on. */+ int resuming = h == CH32_OK && ch32_resume_pending(uid);+ 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. */for (int i = 0; i < 3 && h == CH32_OK && v != CH32_OK && v != CH32_ERR_VERIFY; i++)v = ch32_verify(&keybit_swio, CH32_FLASH_BASE, keybit_image, keybit_image_len);/* A mismatch has to show up twice before it's worth a reflash. */- if (v == CH32_ERR_VERIFY) {+ if (v == CH32_ERR_VERIFY && !resuming) {PROTO_Send("DOCK mismatch at %08lx want %08lx got %08lx", (unsigned long)ch32_bad_addr,(unsigned long)ch32_bad_want, (unsigned long)ch32_bad_got);v = ch32_verify(&keybit_swio, CH32_FLASH_BASE, keybit_image, keybit_image_len);@@ -228,6 +231,10 @@ static void Keybit_Dock_Attempt(const uint32_t uid[3])return;}ch32_status_t p = ch32_program(&keybit_swio, keybit_image, keybit_image_len);+ if (p == CH32_MORE) { /* a couple of blocks done; the rest next poll */+ stalls = 0;+ return;+ }PROTO_Send("DOCK flash halt=%d verify=%d program=%d acs=%08lx retries=%lu blk=%ld from=%lu", h, v, p,(unsigned long)ch32_last_acs, (unsigned long)ch32_retries, (long)ch32_fail_blk,(unsigned long)ch32_resume_from);diff --git a/firmware/keyword/Drivers/CH32V003/ch32v003_swio.c b/firmware/keyword/Drivers/CH32V003/ch32v003_swio.cindex 3ba00f2..0679d48 100644--- a/firmware/keyword/Drivers/CH32V003/ch32v003_swio.c+++ b/firmware/keyword/Drivers/CH32V003/ch32v003_swio.c@@ -635,6 +635,15 @@ uint32_t ch32_resume_from;* 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+/* After a failed step: clear the command error, drop what we cached about* the target and halt it again, so the step can be retried. */static void recover(ch32_swio_t *s)@@ -684,7 +693,9 @@ ch32_status_t ch32_program(ch32_swio_t *s, const uint8_t *image, uint32_t len)if (have_uid) memcpy(resume.uid, uid, sizeof(uid));ch32_resume_from = resume.next_blk;+ uint32_t done = 0;for (uint32_t off = resume.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 */for (int t = 0; t < 8; t++) {diff --git a/firmware/keyword/Drivers/CH32V003/ch32v003_swio.h b/firmware/keyword/Drivers/CH32V003/ch32v003_swio.hindex 8ac466d..b57254a 100644--- a/firmware/keyword/Drivers/CH32V003/ch32v003_swio.h+++ b/firmware/keyword/Drivers/CH32V003/ch32v003_swio.h@@ -26,7 +26,9 @@ extern uint32_t ch32_last_acs;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; /* block ch32_program started at (resumed from an earlier try) */ /* last DMABSTRACTCS read while waiting on a command */+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 *//* Idle time after each frame. 2 us is sometimes too short. */#ifndef CH32_SWIO_FRAME_GAP_US#define CH32_SWIO_FRAME_GAP_US 8@@ -70,6 +72,7 @@ typedef enum {CH32_ERR_FLASH = -7, /* flash stayed busy or write-protected */CH32_ERR_VERIFY = -8, /* read-back did not match */CH32_ERR_ARG = -9, /* bad address or length */+ CH32_MORE = -10, /* ch32_program: did a few blocks, call again to carry on */} ch32_status_t;typedef struct {