A truly modular keyboard
C
keyword: reliable dock detect/halt/verify, blink green once, push-pull read kick
Nathanbb68ff689b37
diff --git a/firmware/keyword/Core/Src/main.c b/firmware/keyword/Core/Src/main.cindex 8e172d6..9c87117 100644--- a/firmware/keyword/Core/Src/main.c+++ b/firmware/keyword/Core/Src/main.c@@ -140,8 +140,10 @@ static void Keybit_Dock_Poll(void)if (ch32_detect(&keybit_swio) != CH32_OK) {/* A few misses in a row before calling it gone, so a wobble while- * seating it doesn't trigger a second flash. */- if (dock_present && ++misses >= 3) {+ * seating it doesn't trigger a second flash. Detect still misses about+ * 1 time in 3, so once it's done, wait longer before calling it gone:+ * otherwise a done board gets re-verified (and blinks green) every few seconds. */+ if (dock_present && ++misses >= (done ? 8 : 3)) {dock_present = 0;dock_failed = 0;done = 0;@@ -155,7 +157,10 @@ static void Keybit_Dock_Poll(void)LED_Update(); /* blue while flashing */HAL_Delay(50); /* let it finish powering up off CN7 */- ch32_status_t h = ch32_halt(&keybit_swio, 1);+ /* No reset: with the Keybit firmware running, a reset-halt loses the line.+ * 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);ch32_status_t v = 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++)diff --git a/firmware/keyword/Core/Src/proto.c b/firmware/keyword/Core/Src/proto.cindex b6aefb9..5f90a63 100644--- a/firmware/keyword/Core/Src/proto.c+++ b/firmware/keyword/Core/Src/proto.c@@ -274,6 +274,7 @@ static void Handle_Line(proto_port_t *port, char *cmd)if ((a = strstr(args, "a=")) && atoi(a + 2) > 0) ch32_t1 = (uint32_t)atoi(a + 2);if ((a = strstr(args, "b=")) && atoi(a + 2) > 0) ch32_t0 = (uint32_t)atoi(a + 2);if ((a = strstr(args, "h=")) && atoi(a + 2) > 0) ch32_hi = (uint32_t)atoi(a + 2);+ if ((a = strstr(args, "k="))) ch32_t_kick = (uint32_t)atoi(a + 2);if ((a = strstr(args, "s=")) && atoi(a + 2) > 0) ch32_t_sample = (uint32_t)atoi(a + 2);if ((a = strstr(args, "od="))) ch32_open_drain = a[3] == '1';keybit_dock_pause_until = HAL_GetTick() + 5000;@@ -282,8 +283,8 @@ static void Handle_Line(proto_port_t *port, char *cmd)uint32_t t = DWT->CYCCNT;HAL_Delay(10);t = DWT->CYCCNT - t; /* ~640000 at 64 MHz: confirms the SWIO pulse timing base */- Reply(port, "DOCK r=%d cfg=%08lx status=%08lx a=%lu b=%lu h=%lu s=%lu od=%u hclk=%lu cyc10ms=%lu", r, (unsigned long)cfg,- (unsigned long)status, (unsigned long)ch32_t1, (unsigned long)ch32_t0, (unsigned long)ch32_hi, (unsigned long)ch32_t_sample, ch32_open_drain,+ Reply(port, "DOCK r=%d cfg=%08lx status=%08lx a=%lu b=%lu h=%lu k=%lu s=%lu od=%u hclk=%lu cyc10ms=%lu", r, (unsigned long)cfg,+ (unsigned long)status, (unsigned long)ch32_t1, (unsigned long)ch32_t0, (unsigned long)ch32_hi, (unsigned long)ch32_t_kick, (unsigned long)ch32_t_sample, ch32_open_drain,(unsigned long)HAL_RCC_GetHCLKFreq(), (unsigned long)t);}else if (strcmp(cmd, "DOCKSCAN") == 0)diff --git a/firmware/keyword/Drivers/CH32V003/ch32v003_swio.c b/firmware/keyword/Drivers/CH32V003/ch32v003_swio.cindex 376df42..1020e3b 100644--- a/firmware/keyword/Drivers/CH32V003/ch32v003_swio.c+++ b/firmware/keyword/Drivers/CH32V003/ch32v003_swio.c@@ -105,13 +105,14 @@ static void refresh_moder(ch32_swio_t *s)uint32_t ch32_t1 = 19;uint32_t ch32_t0 = 52;uint32_t ch32_hi = 46;+uint32_t ch32_t_kick = 2; /* push-pull read: cycles driven high after the low, before letting go */uint32_t ch32_t_sample = 14; /* cycles from release to sampling, plus ~3; ~250 ns */uint32_t ch32_last_acs; /* last DMABSTRACTCS seen by wait_done, for debugging */uint8_t ch32_open_drain = 1; /* needs the external ~1k pull-up: the internal one is far too weak *//* Pulse lengths in cycles for one frame, worked out before interrupts go* off so no multiply or divide sits between edges. */-typedef struct { uint32_t t1, t0, hi, rlo, rs; } phy_t;+typedef struct { uint32_t t1, t0, hi, rlo, rs, kick; } phy_t;static uint32_t wire(uint32_t cycles, uint32_t lag){@@ -121,7 +122,7 @@ static uint32_t wire(uint32_t cycles, uint32_t lag)static phy_t phy_timing(void){return (phy_t){ wire(ch32_t1, LAG_LOW), wire(ch32_t0, LAG_LOW), wire(ch32_hi, LAG_HIGH),- wire(TR_LOW, LAG_RLOW), ch32_t_sample };+ wire(TR_LOW, LAG_RLOW), ch32_t_sample, ch32_t_kick };}FAST void line_high(ch32_swio_t *s)@@ -152,8 +153,12 @@ FAST int read_bit(ch32_swio_t *s, const phy_t *ph, uint32_t timeout_cycles)p->MODER = s->moder_out;wait_from(t, ph->rlo);if (ch32_open_drain) p->MODER = s->moder_in;- else { /* push-pull: kick it high, then let the target pull against nothing */- p->BSRR = s->pin;+ else { /* push-pull: kick it high briefly, then leave it to the internal pull-up and the target */+ if (ph->kick) { /* 0: no kick, let go straight away */+ p->BSRR = s->pin;+ t = cyc();+ wait_from(t, ph->kick);+ }p->MODER = s->moder_in;}t = cyc();@@ -280,12 +285,16 @@ ch32_status_t ch32_detect(ch32_swio_t *s)/* minichlink only rejects an all-0/all-1 DMSTATUS, but a line that floats* low (open-drain with no pull-up) reads as random 0s and gets past that,* so also require the 0x5AA5 key back from DMCFGR. */- uint32_t st = 0, cfg = 0;- if (ch32_read_reg(s, CH32_DMSTATUS, &st) != CH32_OK) return CH32_ERR_NO_TARGET;- if (st == 0 || st == 0xFFFFFFFFu) return CH32_ERR_NO_TARGET;- if (ch32_read_reg(s, CH32_DMCFGR, &cfg) != CH32_OK) return CH32_ERR_NO_TARGET;- if ((cfg & 0xFFFF0000u) != 0x5AA50000u) return CH32_ERR_NO_TARGET;- return CH32_OK;+ /* The first reads after the handshake sometimes come back wrong while+ * the reads right after are fine, so give it a few tries. */+ for (int i = 0; i < 3; i++) {+ uint32_t st = 0, cfg = 0;+ if (ch32_read_reg(s, CH32_DMSTATUS, &st) == CH32_OK && st != 0 && st != 0xFFFFFFFFu &&+ ch32_read_reg(s, CH32_DMCFGR, &cfg) == CH32_OK && (cfg & 0xFFFF0000u) == 0x5AA50000u)+ return CH32_OK;+ delay_us(100);+ }+ return CH32_ERR_NO_TARGET;}ch32_status_t ch32_halt(ch32_swio_t *s, int reset)@@ -297,8 +306,11 @@ ch32_status_t ch32_halt(ch32_swio_t *s, int reset)forget_state(s);HAL_Delay(10); /* let a flash op that was in progress finish */- uint32_t st = 0;- if (ch32_read_reg(s, CH32_DMSTATUS, &st) != CH32_OK || st == 0xFFFFFFFFu) return CH32_ERR_NO_TARGET;+ /* One garbled read shouldn't fail the halt, so give it a few. */+ uint32_t st = 0xFFFFFFFFu;+ for (int i = 0; i < 3 && (st == 0xFFFFFFFFu || !(st & (1u << 9))); i++)+ if (ch32_read_reg(s, CH32_DMSTATUS, &st) != CH32_OK) st = 0xFFFFFFFFu;+ if (st == 0xFFFFFFFFu) return CH32_ERR_NO_TARGET;if (!(st & (1u << 9))) return CH32_ERR_BUSY; /* allhalted */ch32_write_reg(s, CH32_DMABSTRACTCS, 0x08000700); /* clear old errors */return CH32_OK;@@ -366,7 +378,8 @@ static ch32_status_t ensure_chip(ch32_swio_t *s)if (s->chip_known) return CH32_OK;uint32_t hart = 0;- if (ch32_read_reg(s, CH32_DMHARTINFO, &hart) != CH32_OK) return CH32_ERR_NO_TARGET;+ for (int i = 0; i < 3 && (0xE0000000u | (hart & 0x7FF)) != V003_DATA0_ADDR; i++)+ if (ch32_read_reg(s, CH32_DMHARTINFO, &hart) != CH32_OK) hart = 0;if ((0xE0000000u | (hart & 0x7FF)) != V003_DATA0_ADDR) return CH32_ERR_NOT_V003;ch32_write_reg(s, CH32_DMCONTROL, 0x80000001);@@ -586,8 +599,12 @@ ch32_status_t ch32_verify(ch32_swio_t *s, uint32_t addr, const uint8_t *data, uifor (uint32_t i = 0; i < len; i += 4) {uint32_t got = 0, want = 0xFFFFFFFFu;memcpy(&want, data + i, (len - i) < 4 ? (len - i) : 4);- ch32_status_t r = ch32_read_word(s, addr + i, &got);- if (r != CH32_OK) return r;+ /* A garbled read looks like a mismatch, so read a word that differs again before believing it. */+ for (int t = 0; t < 3; t++) {+ ch32_status_t r = ch32_read_word(s, addr + i, &got);+ if (r != CH32_OK) return r;+ if (got == want) break;+ }if (got != want) return CH32_ERR_VERIFY;}return CH32_OK;@@ -627,7 +644,8 @@ ch32_status_t ch32_program(ch32_swio_t *s, const uint8_t *image, uint32_t len)st1 != 0xFFFFFFFFu && st2 != 0xFFFFFFFFu && (st1 & st2 & (1u << 9));if (!halted) {if ((r = ch32_detect(s)) != CH32_OK) return r;- if ((r = ch32_halt(s, 1)) != CH32_OK) return r;+ if ((r = ch32_halt(s, 0)) != CH32_OK && (r = ch32_detect(s)) == CH32_OK) r = ch32_halt(s, 0);+ if (r != CH32_OK) return r;}for (int t = 0; t < 5; t++) {if ((r = ensure_chip(s)) == CH32_OK && (r = ch32_unlock_flash(s)) == CH32_OK) break;diff --git a/firmware/keyword/Drivers/CH32V003/ch32v003_swio.h b/firmware/keyword/Drivers/CH32V003/ch32v003_swio.hindex be9fa79..fcf753a 100644--- a/firmware/keyword/Drivers/CH32V003/ch32v003_swio.h+++ b/firmware/keyword/Drivers/CH32V003/ch32v003_swio.h@@ -19,6 +19,7 @@* ch32_open_drain releases the line for highs instead of driving them* (needs a 1k pull-up). */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;