Skip to content

A truly modular keyboard

C

keyword: fix SWIO dock flashing (separate write timing, verified writes, resume)

Nathan4c159abc9d38
diff --git a/firmware/keyword/Core/Inc/main.h b/firmware/keyword/Core/Inc/main.h
index b5fd3c8..d7ea058 100644
--- a/firmware/keyword/Core/Inc/main.h
+++ b/firmware/keyword/Core/Inc/main.h
@@ -54,6 +54,11 @@ void Error_Handler(void);
/* USER CODE BEGIN EFP */
int Keybit_Dock_Probe(uint32_t *cfg, uint32_t *status);
+int Keybit_Dock_Reads(int n);
+int Keybit_Dock_Score(int n, uint32_t *bw, uint32_t *br, int *bad, int *hr);
+int Keybit_Dock_Regs(uint32_t *v, uint32_t delay_ms, int halt, int x, int *hr);
+extern uint32_t keybit_dock_pause_until;
+void Keybit_Dock_Id(char *out, size_t len, int reset);
/* USER CODE END EFP */
diff --git a/firmware/keyword/Core/Src/main.c b/firmware/keyword/Core/Src/main.c
index db0691a..261b6d6 100644
--- a/firmware/keyword/Core/Src/main.c
+++ b/firmware/keyword/Core/Src/main.c
@@ -21,6 +21,7 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
+#include <stdio.h>
#include "app_entry.h"
#include "usb_device.h"
#include "ch32v003_swio.h"
@@ -59,6 +60,7 @@ PCD_HandleTypeDef hpcd_USB_FS;
/* USER CODE BEGIN PV */
static ch32_swio_t keybit_swio;
+uint32_t keybit_dock_pause_until; /* HAL tick; the dock poll stays off the line until then */
/* Keybits touching the keyword's sides: the list is rebuilt every
* KEYBIT_SCAN_MS, key_down refreshed every KEYBIT_KEYS_MS. keybit_changed
* collects bits of entries whose key_down changed; whoever consumes key
@@ -130,6 +132,7 @@ static void Keybit_Dock_Poll(void)
static uint8_t misses, done;
if (HAL_GetTick() - last_poll < KEYBIT_POLL_MS) return;
+ if ((int32_t)(keybit_dock_pause_until - HAL_GetTick()) > 0) return;
last_poll = HAL_GetTick();
if (ch32_detect(&keybit_swio) != CH32_OK) {
@@ -151,6 +154,9 @@ static void Keybit_Dock_Poll(void)
HAL_Delay(50); /* let it finish powering up off CN7 */
ch32_status_t h = ch32_halt(&keybit_swio, 1);
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++)
+ v = ch32_verify(&keybit_swio, CH32_FLASH_BASE, keybit_image, keybit_image_len);
if (v == CH32_OK) {
ch32_reboot(&keybit_swio); /* already up to date */
done = 1;
@@ -161,7 +167,9 @@ static void Keybit_Dock_Poll(void)
ch32_status_t p = ch32_program(&keybit_swio, keybit_image, keybit_image_len);
done = p == CH32_OK;
dock_failed = !done;
- PROTO_Send("DOCK flash halt=%d verify=%d program=%d", h, v, p);
+ 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);
}
/* DOCK? over serial: the raw answers from whatever is on the dock, to debug detection. */
@@ -174,6 +182,107 @@ int Keybit_Dock_Probe(uint32_t *cfg, uint32_t *status)
return r;
}
+/* DOCKSCAN: after a successful probe, read DMCFGR n more times and count
+ * how many come back exactly as the first good read, to rank timings by
+ * how reliably they read rather than by one lucky answer. */
+/* n reads alternating DMCFGR and DMHARTINFO against the values a V003
+ * gives; returns how many came back right. */
+int Keybit_Dock_Reads(int n)
+{
+ int good = 0;
+ for (int i = 0; i < n; i++) {
+ uint32_t v = 0;
+ uint8_t reg = (i & 1) ? CH32_DMHARTINFO : CH32_DMCFGR;
+ uint32_t want = (i & 1) ? 0x002120F4u : 0x5AA50401u;
+ if (ch32_read_reg(&keybit_swio, reg, &v) == CH32_OK && v == want) good++;
+ }
+ return good;
+}
+
+/* Halts the core, then n write/read-back round trips through DMDATA0 (four
+ * fixed patterns, then an LCG), then lets it run again. Returns how many DATA0 ones matched, or -1 if the halt
+ * failed (its result in *hr). The first DATA0 mismatch goes in *bw / *br,
+ * and *bad says whether there was one. */
+int Keybit_Dock_Score(int n, uint32_t *bw, uint32_t *br, int *bad, int *hr)
+{
+ static const uint32_t fixed[4] = { 0x00000000u, 0xFFFFFFFFu, 0xAAAAAAAAu, 0x55555555u };
+ uint32_t x = 0x12345678u;
+ int good = 0;
+ *bad = 0;
+ *hr = ch32_halt(&keybit_swio, 0);
+ if (*hr != CH32_OK) return -1;
+ ch32_write_reg(&keybit_swio, CH32_DMABSTRACTAUTO, 0);
+ for (int i = 0; i < n; i++) {
+ uint32_t w = i < 4 ? fixed[i] : (x = x * 1664525u + 1013904223u), r = 0;
+ ch32_write_reg(&keybit_swio, CH32_DMDATA0, w);
+ if (ch32_read_reg(&keybit_swio, CH32_DMDATA0, &r) == CH32_OK && r == w) good++;
+ else if (!*bad) { *bad = 1; *bw = w; *br = r; }
+ }
+ ch32_reboot(&keybit_swio);
+ return good;
+}
+
+/* Detect whatever is on the dock, optionally request a halt (DMCONTROL
+ * written twice) and wait delay_ms, then dump the debug module registers
+ * (HARTINFO four times) into v[0..7]; 0xEEEEEEEE for a failed read.
+ * x=1 or 2 runs ch32_halt (2 with reset) instead of the halt writes and
+ * puts its result in *hr. */
+int Keybit_Dock_Regs(uint32_t *v, uint32_t delay_ms, int halt, int x, int *hr)
+{
+ static const uint8_t regs[8] = { CH32_DMSTATUS, CH32_DMHARTINFO, CH32_DMHARTINFO, CH32_DMHARTINFO,
+ CH32_DMHARTINFO, CH32_DMABSTRACTCS, CH32_DMCFGR, CH32_DMCONTROL };
+ int r = ch32_detect(&keybit_swio);
+ *hr = 0;
+ if (r == CH32_OK && x) *hr = ch32_halt(&keybit_swio, x == 2);
+ else if (r == CH32_OK && halt) {
+ ch32_write_reg(&keybit_swio, CH32_DMCONTROL, 0x80000001u);
+ ch32_write_reg(&keybit_swio, CH32_DMCONTROL, 0x80000001u);
+ }
+ if (delay_ms) HAL_Delay(delay_ms);
+ for (int i = 0; i < 8; i++)
+ if (ch32_read_reg(&keybit_swio, regs[i], &v[i]) != CH32_OK) v[i] = 0xEEEEEEEEu;
+ return r;
+}
+
+/* DOCKID: walk detect, halt, chip ID, a few flash and FLASH register reads
+ * and the unlock, printing each step's result into out. Stops at the first
+ * failure, adding the last DMABSTRACTCS. */
+void Keybit_Dock_Id(char *out, size_t len, int reset)
+{
+ static const struct { const char *name; uint32_t addr; } words[] = {
+ { "w0", 0x08000000u }, { "w4", 0x08000004u }, { "ctlr", 0x40022010u }, { "statr", 0x4002200Cu },
+ };
+ size_t n = 0;
+ uint32_t v = 0;
+#define STEP(fmt, ...) n += (size_t)snprintf(out + n, n < len ? len - n : 0, fmt, __VA_ARGS__)
+#define FAIL(r) do { if (r != CH32_OK) { STEP(" acs=%08lx", (unsigned long)ch32_last_acs); return; } } while (0)
+ out[0] = 0;
+ int r = ch32_detect(&keybit_swio);
+ STEP("detect=%d", r);
+ FAIL(r);
+ r = ch32_halt(&keybit_swio, reset);
+ STEP(" halt=%d", r);
+ FAIL(r);
+ r = ch32_read_chip_id(&keybit_swio, &v);
+ STEP(" id=%d:%08lx", r, (unsigned long)v);
+ FAIL(r);
+ for (unsigned i = 0; i < sizeof(words) / sizeof(words[0]); i++) {
+ v = 0;
+ r = ch32_read_word(&keybit_swio, words[i].addr, &v);
+ STEP(" %s=%d:%08lx", words[i].name, r, (unsigned long)v);
+ FAIL(r);
+ }
+ r = ch32_unlock_flash(&keybit_swio);
+ STEP(" unlock=%d", r);
+ FAIL(r);
+ v = 0;
+ r = ch32_read_word(&keybit_swio, 0x40022010u, &v);
+ STEP(" ctlr2=%d:%08lx", r, (unsigned long)v);
+ FAIL(r);
+#undef STEP
+#undef FAIL
+}
+
/* Hand addresses to new Keybits, then refresh the list of who is there. */
static void Keybit_Bus_Poll(void)
{
diff --git a/firmware/keyword/Core/Src/proto.c b/firmware/keyword/Core/Src/proto.c
index f6f3acf..5cd0883 100644
--- a/firmware/keyword/Core/Src/proto.c
+++ b/firmware/keyword/Core/Src/proto.c
@@ -268,17 +268,87 @@ static void Handle_Line(proto_port_t *port, char *cmd)
}
else if (strcmp(cmd, "DOCK?") == 0)
{
- /* optional: t=<cycles per short pulse, 64 MHz> od=<0|1> */
+ /* optional: a=/b=/h=<"1" low, "0" low, high, wire cycles at 64 MHz>
+ * s=<read sample delay, cycles at 64 MHz> od=<0|1> */
const char *a;
- if ((a = strstr(args, "t=")) && atoi(a + 2) > 0) ch32_t_short = (uint32_t)atoi(a + 2);
+ 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, "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;
uint32_t cfg, status;
int r = Keybit_Dock_Probe(&cfg, &status);
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 t=%lu od=%u hclk=%lu cyc10ms=%lu", r, (unsigned long)cfg, (unsigned long)status,
- (unsigned long)ch32_t_short, ch32_open_drain, (unsigned long)HAL_RCC_GetHCLKFreq(), (unsigned long)t);
+ 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,
+ (unsigned long)HAL_RCC_GetHCLKFreq(), (unsigned long)t);
+ }
+ else if (strcmp(cmd, "DOCKSCAN") == 0)
+ {
+ /* Try write pulse widths ("1" low, "0" low, high) on whatever sits on
+ * the dock, open drain, s=14. Each one that passes detection gets 256
+ * register reads (rd), then a halt and 256 DMDATA0 write/read-back round
+ * trips (wr); score = rd + wr. Prints scores of 480 and up and keeps the
+ * best, a tie going to the one with more neighbours at 500 and up. Hold
+ * the Keybit still. */
+ static const uint8_t as[] = { 16, 17, 18, 19 };
+ static const uint8_t bs[] = { 44, 52, 60 };
+ static const uint8_t hs[] = { 34, 40, 46 };
+ enum { NA = sizeof(as), NB = sizeof(bs), NH = sizeof(hs) };
+ int sc[NA][NB][NH];
+ ch32_open_drain = 1;
+ ch32_t_sample = 14;
+ for (int i = 0; i < NA; i++)
+ for (int j = 0; j < NB; j++)
+ for (int k = 0; k < NH; k++)
+ {
+ keybit_dock_pause_until = HAL_GetTick() + 5000;
+ ch32_t1 = as[i];
+ ch32_t0 = bs[j];
+ ch32_hi = hs[k];
+ uint32_t cfg, status, bw, br;
+ int bad, hr;
+ sc[i][j][k] = -1;
+ if (Keybit_Dock_Probe(&cfg, &status) != 0) continue;
+ int rd = Keybit_Dock_Reads(256);
+ int wr = Keybit_Dock_Score(256, &bw, &br, &bad, &hr);
+ if (hr != 0) {
+ sc[i][j][k] = rd;
+ if (rd >= 240) Reply(port, "DOCKSCAN a=%u b=%u h=%u rd=%d/256 hr=%d", as[i], bs[j], hs[k], rd, hr);
+ continue;
+ }
+ sc[i][j][k] = rd + wr;
+ if (rd + wr >= 480) Reply(port, "DOCKSCAN a=%u b=%u h=%u rd=%d/256 wr=%d/256", as[i], bs[j], hs[k], rd, wr);
+ }
+ int bi = 0, bj = 0, bk = 0, best = -1, bn = -1;
+ for (int i = 0; i < NA; i++)
+ for (int j = 0; j < NB; j++)
+ for (int k = 0; k < NH; k++)
+ {
+ int n = (i > 0 && sc[i - 1][j][k] >= 500) + (i < NA - 1 && sc[i + 1][j][k] >= 500) +
+ (j > 0 && sc[i][j - 1][k] >= 500) + (j < NB - 1 && sc[i][j + 1][k] >= 500) +
+ (k > 0 && sc[i][j][k - 1] >= 500) + (k < NH - 1 && sc[i][j][k + 1] >= 500);
+ if (sc[i][j][k] > best || (sc[i][j][k] == best && n > bn)) { best = sc[i][j][k]; bn = n; bi = i; bj = j; bk = k; }
+ }
+ ch32_t1 = as[bi];
+ ch32_t0 = bs[bj];
+ ch32_hi = hs[bk];
+ keybit_dock_pause_until = HAL_GetTick() + 5000;
+ Reply(port, "DOCKSCAN best a=%u b=%u h=%u score=%d/512 nb=%d", as[bi], bs[bj], hs[bk], best, bn);
+ }
+ else if (strcmp(cmd, "DOCKID") == 0)
+ {
+ /* optional: h=<0|1> halt with reset (default 1) */
+ const char *a = strstr(args, "h=");
+ char buf[200];
+ keybit_dock_pause_until = HAL_GetTick() + 5000;
+ Keybit_Dock_Id(buf, sizeof(buf), a ? a[2] == '1' : 1);
+ /* lines cap at PROTO_LINE_MAX, so long results go out in pieces */
+ for (size_t o = 0, l = strlen(buf); o < l || o == 0; o += 100)
+ Reply(port, "DOCKID%s %.100s", o ? "+" : "", buf + o);
}
else if (strcmp(cmd, "SIDES?") == 0)
{
diff --git a/firmware/keyword/Drivers/CH32V003/ch32v003_swio.c b/firmware/keyword/Drivers/CH32V003/ch32v003_swio.c
index 96401e4..376df42 100644
--- a/firmware/keyword/Drivers/CH32V003/ch32v003_swio.c
+++ b/firmware/keyword/Drivers/CH32V003/ch32v003_swio.c
@@ -38,16 +38,24 @@
* line (needed for any read). */
#define DMCFG_OUTPUT_EN (0x5AA50000u | (1u << 10))
-enum { PROG_NONE, PROG_READ, PROG_WRITE };
+/* Which program sits in the progbuf; anything but NONE also means x10-x13
+ * are loaded (see load_prog_regs). */
+enum { PROG_NONE, PROG_READ, PROG_WRITE, PROG_WRITE_FLASH };
#define PHY __attribute__((optimize("O2")))
+/* The project builds with -Og, which doesn't inline across a different
+ * optimize attribute: cyc() and wait_from() stayed real calls and added
+ * 10-20 cycles to every pulse, stretching a 300 ns "1" to about 530 ns
+ * and pushing the read sample past the end of the target's "0". Force
+ * them inline so the cycle counts below are what goes on the wire. */
+#define FAST static inline __attribute__((always_inline))
/* ------------------------------------------------------------------------ */
/* Timing */
-static inline uint32_t cyc(void) { return DWT->CYCCNT; }
+FAST uint32_t cyc(void) { return DWT->CYCCNT; }
-static inline void wait_from(uint32_t start, uint32_t cycles)
+FAST void wait_from(uint32_t start, uint32_t cycles)
{
while ((cyc() - start) < cycles) { }
}
@@ -76,55 +84,80 @@ static void refresh_moder(ch32_swio_t *s)
* After its reset the target's SDI runs in "normal mode 2x" (slow), so every
* bit needs a long high after it:
* 1: ~300 ns low, ~850 ns high 0: ~850 ns low, ~850 ns high
- * read: a short low, release, sample ~330 ns later, wait for high
- * Cycles are at 64 MHz. DOCK? t=<n> scales them all by n/16 and od=<0|1>
- * picks open-drain (needs a 1k pull-up, like the BluePill) or push-pull,
- * the default, which works with or without one. */
-#define T1_LOW 19
-#define T0_LOW 54
-#define T_HIGH 54
-#define TR_LOW 4
-#define TR_SAMPLE 21
-uint32_t ch32_t_short = 16;
-uint8_t ch32_open_drain; /* 1 only with an external ~1k pull-up: the internal one is far too weak */
-
-static inline uint32_t scaled(uint32_t c) { return c * ch32_t_short / 16; }
-
-static inline PHY void line_high(ch32_swio_t *s)
+ * read: a short low, release, sample ~300 ns later, wait for high
+ * The T_ values are those wire times in cycles at 64 MHz. The code between
+ * the edges and the wait loops costs a fixed number of cycles on top (the
+ * LAG_ values, counted from the -Og build's disassembly), so phy_timing()
+ * takes that off; without it a "1" came out at ~530 ns, close to a "0".
+ * On the dock the three write times needed tuning apart, and the "1" low is
+ * touchy: about 16-19 cycles works, 20 is the edge, 22 and up fail. Reads
+ * are fine across the board. The defaults below came out of DOCKSCAN;
+ * DOCK? a=/b=/h= (ch32_t1, ch32_t0, ch32_hi), s= (sample point) and od=
+ * still change them live. Open-drain, with the dock's ~1k pull-up.
+ * Even tuned, about 1 write frame in 10 gets dropped, so the memory layer
+ * reads back every register write, checks DATA1 == addr + 4 after each
+ * command to catch a dropped one, and ch32_program retries per block and
+ * resumes by chip UID. */
+#define TR_LOW 10 /* ~150 ns; the target's SDI clock is about 8 MHz, so keep it over 125 ns */
+#define LAG_LOW 15
+#define LAG_HIGH 12
+#define LAG_RLOW 10
+uint32_t ch32_t1 = 19;
+uint32_t ch32_t0 = 52;
+uint32_t ch32_hi = 46;
+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;
+
+static uint32_t wire(uint32_t cycles, uint32_t lag)
+{
+ return cycles > lag ? cycles - lag : 1;
+}
+
+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 };
+}
+
+FAST void line_high(ch32_swio_t *s)
{
if (ch32_open_drain) s->port->MODER = s->moder_in;
else s->port->BSRR = s->pin;
}
-static inline PHY void send_bit(ch32_swio_t *s, int one)
+FAST void send_bit(ch32_swio_t *s, const phy_t *ph, int one)
{
GPIO_TypeDef *p = s->port;
- const uint32_t lo = scaled(one ? T1_LOW : T0_LOW), hi = scaled(T_HIGH);
- uint32_t t = cyc();
+ const uint32_t lo = one ? ph->t1 : ph->t0;
p->BRR = s->pin;
+ uint32_t t = cyc();
p->MODER = s->moder_out;
wait_from(t, lo);
- t = cyc();
line_high(s);
- wait_from(t, hi);
+ t = cyc();
+ wait_from(t, ph->hi);
}
/* Low = 0, high = 1, -1 = target never let go. */
-static inline PHY int read_bit(ch32_swio_t *s, uint32_t timeout_cycles)
+FAST int read_bit(ch32_swio_t *s, const phy_t *ph, uint32_t timeout_cycles)
{
GPIO_TypeDef *p = s->port;
- const uint32_t lo = scaled(TR_LOW), sample = scaled(TR_SAMPLE);
- uint32_t t = cyc();
p->BRR = s->pin;
+ uint32_t t = cyc();
p->MODER = s->moder_out;
- wait_from(t, lo);
- t = cyc();
+ 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;
p->MODER = s->moder_in;
}
- wait_from(t, sample);
+ t = cyc();
+ wait_from(t, ph->rs);
int bit = (p->IDR & s->pin) != 0;
t = cyc();
@@ -165,16 +198,17 @@ void ch32_init(ch32_swio_t *s, GPIO_TypeDef *port, uint16_t pin)
PHY void ch32_write_reg(ch32_swio_t *s, uint8_t reg, uint32_t value)
{
+ const phy_t ph = phy_timing();
uint32_t primask = __get_PRIMASK();
__disable_irq();
refresh_moder(s);
s->port->BSRR = s->pin;
s->port->MODER = s->moder_out;
- send_bit(s, 1);
- for (uint32_t m = 1u << 6; m; m >>= 1) send_bit(s, reg & m);
- send_bit(s, 1);
- for (uint32_t m = 1u << 31; m; m >>= 1) send_bit(s, value & m);
+ send_bit(s, &ph, 1);
+ for (uint32_t m = 1u << 6; m; m >>= 1) send_bit(s, &ph, reg & m);
+ send_bit(s, &ph, 1);
+ for (uint32_t m = 1u << 31; m; m >>= 1) send_bit(s, &ph, value & m);
__set_PRIMASK(primask);
delay_us(CH32_SWIO_FRAME_GAP_US);
@@ -182,6 +216,7 @@ PHY void ch32_write_reg(ch32_swio_t *s, uint8_t reg, uint32_t value)
PHY ch32_status_t ch32_read_reg(ch32_swio_t *s, uint8_t reg, uint32_t *value)
{
+ const phy_t ph = phy_timing();
uint32_t timeout = CH32_SWIO_READ_TIMEOUT_US * (SystemCoreClock / 1000000u);
uint32_t v = 0;
ch32_status_t ret = CH32_OK;
@@ -192,11 +227,11 @@ PHY ch32_status_t ch32_read_reg(ch32_swio_t *s, uint8_t reg, uint32_t *value)
s->port->BSRR = s->pin;
s->port->MODER = s->moder_out;
- send_bit(s, 1);
- for (uint32_t m = 1u << 6; m; m >>= 1) send_bit(s, reg & m);
- send_bit(s, 0);
+ send_bit(s, &ph, 1);
+ for (uint32_t m = 1u << 6; m; m >>= 1) send_bit(s, &ph, reg & m);
+ send_bit(s, &ph, 0);
for (int i = 0; i < 32; i++) {
- int b = read_bit(s, timeout);
+ int b = read_bit(s, &ph, timeout);
if (b < 0) { ret = CH32_ERR_TIMEOUT; break; }
v = (v << 1) | (uint32_t)b;
}
@@ -255,8 +290,6 @@ ch32_status_t ch32_detect(ch32_swio_t *s)
ch32_status_t ch32_halt(ch32_swio_t *s, int reset)
{
- ch32_write_reg(s, CH32_DMSHDWCFGR, DMCFG_OUTPUT_EN);
- ch32_write_reg(s, CH32_DMCFGR, DMCFG_OUTPUT_EN);
ch32_write_reg(s, CH32_DMCONTROL, 0x80000001); /* haltreq + dmactive */
if (reset) ch32_write_reg(s, CH32_DMCONTROL, 0x80000003); /* + ndmreset */
ch32_write_reg(s, CH32_DMCONTROL, 0x80000001); /* release reset, stay halted */
@@ -265,7 +298,7 @@ ch32_status_t ch32_halt(ch32_swio_t *s, int reset)
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) return CH32_ERR_NO_TARGET;
+ if (ch32_read_reg(s, CH32_DMSTATUS, &st) != CH32_OK || 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;
@@ -286,11 +319,14 @@ static ch32_status_t wait_done(ch32_swio_t *s)
{
uint32_t cs = 0;
int timeout = 1000;
- do {
- if (ch32_read_reg(s, CH32_DMABSTRACTCS, &cs) != CH32_OK) return CH32_ERR_TIMEOUT;
- if (timeout-- == 0) return CH32_ERR_BUSY;
- } while (cs & (1u << 12)); /* busy */
+ for (;;) {
+ if (ch32_read_reg(s, CH32_DMABSTRACTCS, &cs) != CH32_OK) { ch32_last_acs = cs; return CH32_ERR_TIMEOUT; }
+ if (timeout-- == 0) { ch32_last_acs = cs; return CH32_ERR_BUSY; }
+ if (((cs >> 24) & 0x1F) != 8) continue; /* progbufsize is always 8: a garbled read, ask again */
+ if (!(cs & (1u << 12))) break; /* busy */
+ }
+ ch32_last_acs = cs;
if ((cs >> 8) & 7) { /* cmderr */
ch32_write_reg(s, CH32_DMABSTRACTCS, 0x00000700);
return CH32_ERR_ABSTRACT;
@@ -298,6 +334,32 @@ static ch32_status_t wait_done(ch32_swio_t *s)
return CH32_OK;
}
+/* About 1 write frame in 10 still gets dropped on the dock, so every
+ * register we can read back is written until it reads back right. */
+static ch32_status_t write_v(ch32_swio_t *s, uint8_t reg, uint32_t value)
+{
+ for (int i = 0; i < 10; i++) {
+ uint32_t got = 0;
+ ch32_write_reg(s, reg, value);
+ if (ch32_read_reg(s, reg, &got) == CH32_OK && got == value) return CH32_OK;
+ }
+ return CH32_ERR_TIMEOUT;
+}
+
+/* Run an abstract command. COMMAND can't be read back, so on a command
+ * error clear it and send it again. A dropped COMMAND just looks like a
+ * finished one; callers catch that by reading results back. */
+static ch32_status_t exec(ch32_swio_t *s, uint32_t cmd)
+{
+ ch32_status_t r = CH32_ERR_ABSTRACT;
+ for (int i = 0; i < 5 && r == CH32_ERR_ABSTRACT; i++) {
+ ch32_write_reg(s, CH32_DMCOMMAND, cmd);
+ r = wait_done(s);
+ if (r == CH32_ERR_ABSTRACT) ch32_write_reg(s, CH32_DMABSTRACTCS, 0x08000700);
+ }
+ return r;
+}
+
/* Confirm it's a V003 and read its chip ID. The core must be halted. */
static ch32_status_t ensure_chip(ch32_swio_t *s)
{
@@ -310,19 +372,18 @@ static ch32_status_t ensure_chip(ch32_swio_t *s)
ch32_write_reg(s, CH32_DMCONTROL, 0x80000001);
ch32_write_reg(s, CH32_DMCONTROL, 0x80000001);
ch32_write_reg(s, CH32_DMABSTRACTCS, 0x08000700);
- ch32_write_reg(s, CH32_DMABSTRACTAUTO, 0);
+ s->prog_state = PROG_NONE;
+ ch32_status_t r;
+ if ((r = write_v(s, CH32_DMABSTRACTAUTO, 0)) != CH32_OK) return r;
/* c.lw x8, 0(x8); c.ebreak, with x8 = chip ID address */
- ch32_write_reg(s, CH32_DMPROGBUF0, 0x90024000);
- ch32_write_reg(s, CH32_DMDATA0, V003_CHIP_ID);
- ch32_write_reg(s, CH32_DMCOMMAND, 0x00271008); /* DATA0 -> x8, run progbuf */
- ch32_status_t r = wait_done(s);
- if (r != CH32_OK) return r;
- ch32_write_reg(s, CH32_DMCOMMAND, 0x00221008); /* x8 -> DATA0 */
+ if ((r = write_v(s, CH32_DMPROGBUF0, 0x90024000)) != CH32_OK) return r;
+ if ((r = write_v(s, CH32_DMDATA0, V003_CHIP_ID)) != CH32_OK) return r;
+ if ((r = exec(s, 0x00271008)) != CH32_OK) return r; /* DATA0 -> x8, run progbuf */
+ if ((r = exec(s, 0x00221008)) != CH32_OK) return r; /* x8 -> DATA0 */
if (ch32_read_reg(s, CH32_DMDATA0, &s->chip_id) != CH32_OK) return CH32_ERR_NO_TARGET;
s->chip_known = 1;
- s->prog_state = PROG_NONE;
return CH32_OK;
}
@@ -335,103 +396,87 @@ ch32_status_t ch32_read_chip_id(ch32_swio_t *s, uint32_t *id)
/* Load the registers the read/write programs use:
* x10 = &DATA0, x11 = &DATA1, x12 = &CH_FLASH_STATR, x13 = PAGE_PG|BUF_LOAD */
-static void load_prog_regs(ch32_swio_t *s)
+static ch32_status_t load_prog_regs(ch32_swio_t *s)
+{
+ const uint32_t v[4] = { V003_DATA0_ADDR, V003_DATA0_ADDR + 4, CH_FLASH_STATR, CTLR_PAGE_PG | CTLR_BUF_LOAD };
+ ch32_status_t r;
+ for (uint32_t i = 0; i < 4; i++) {
+ if ((r = write_v(s, CH32_DMDATA0, v[i])) != CH32_OK) return r;
+ if ((r = exec(s, 0x0023100A + i)) != CH32_OK) return r; /* DATA0 -> x10 + i */
+ }
+ return CH32_OK;
+}
+
+/* Put the read, write or flash-write program in the progbuf, unless it's
+ * already there. */
+static ch32_status_t load_prog(ch32_swio_t *s, uint8_t want)
{
- ch32_write_reg(s, CH32_DMABSTRACTAUTO, 0);
- ch32_write_reg(s, CH32_DMDATA0, V003_DATA0_ADDR);
- ch32_write_reg(s, CH32_DMCOMMAND, 0x0023100A);
- ch32_write_reg(s, CH32_DMDATA0, V003_DATA0_ADDR + 4);
- ch32_write_reg(s, CH32_DMCOMMAND, 0x0023100B);
- ch32_write_reg(s, CH32_DMDATA0, CH_FLASH_STATR);
- ch32_write_reg(s, CH32_DMCOMMAND, 0x0023100C);
- ch32_write_reg(s, CH32_DMDATA0, CTLR_PAGE_PG | CTLR_BUF_LOAD);
- ch32_write_reg(s, CH32_DMCOMMAND, 0x0023100D);
+ static const uint32_t rd[] = {
+ 0x40044180, /* c.lw x8,0(x11) address from DATA1; c.lw x9,0(x8) load it */
+ 0xC1040411, /* c.addi x8,4; c.sw x9,0(x10) value to DATA0 */
+ 0x9002C180, /* c.sw x8,0(x11) next address to DATA1; c.ebreak */
+ };
+ static const uint32_t wr[] = {
+ 0x41844100, /* c.lw x8,0(x10) value; c.lw x9,0(x11) address */
+ 0x0491C080, /* c.sw x8,0(x9) store; c.addi x9,4 */
+ 0x0001C184, /* c.sw x9,0(x11) next address to DATA1; c.nop */
+ 0x90029002, /* c.ebreak */
+ };
+ static const uint32_t wf[] = {
+ 0x41844100, 0x0491C080, 0x0001C184,
+ 0x4200C254, /* c.sw x13,4(x12) CTLR = PAGE_PG|BUF_LOAD; c.lw x8,0(x12) STATR */
+ 0xFC758805, /* c.andi x8,1 BSY; c.bnez x8,-4 wait */
+ 0x90029002, /* c.ebreak */
+ };
+ if (s->prog_state == want) return CH32_OK;
+
+ ch32_status_t r;
+ if (s->prog_state == PROG_NONE && (r = load_prog_regs(s)) != CH32_OK) return r;
+ const uint32_t *p = want == PROG_READ ? rd : want == PROG_WRITE ? wr : wf;
+ uint32_t n = want == PROG_READ ? 3 : want == PROG_WRITE ? 4 : 6;
+ for (uint32_t i = 0; i < n; i++)
+ if ((r = write_v(s, CH32_DMPROGBUF0 + i, p[i])) != CH32_OK) { s->prog_state = PROG_NONE; return r; }
+ s->prog_state = want;
+ return CH32_OK;
}
/* ------------------------------------------------------------------------ */
-/* Memory access. With autoexec on, every access to DATA0 reruns the
- * program buffer, so sequential words cost one frame each. */
+/* Memory access. One word per command, no autoexec: every DATA write is
+ * checked before the program runs. */
-ch32_status_t ch32_read_word(ch32_swio_t *s, uint32_t addr, uint32_t *data)
+/* Run the loaded read or write program on addr (and data, for a write).
+ * Both programs leave addr + 4 in DATA1, so if that isn't there afterwards
+ * the COMMAND frame was dropped and it all goes again. */
+static ch32_status_t run_at(ch32_swio_t *s, uint32_t addr, int write, uint32_t data)
{
- ch32_status_t r = ensure_chip(s);
- if (r != CH32_OK) return r;
-
- /* Polling flash registers must not move the address along. */
- uint8_t autoinc = !(addr == CH_FLASH_CTLR || addr == CH_FLASH_STATR);
-
- if (s->prog_state != PROG_READ || addr != s->next_addr || autoinc != s->autoincrement) {
- if (s->prog_state != PROG_READ || autoinc != s->autoincrement) {
- if (s->prog_state != PROG_WRITE) load_prog_regs(s);
- ch32_write_reg(s, CH32_DMABSTRACTAUTO, 0);
- /* c.lw x8,0(x11) address from DATA1; c.lw x9,0(x8) load it */
- ch32_write_reg(s, CH32_DMPROGBUF0, 0x40044180);
- /* c.addi x8,4 (or c.nop); c.sw x9,0(x10) value to DATA0 */
- ch32_write_reg(s, CH32_DMPROGBUF1, autoinc ? 0xC1040411 : 0xC1040001);
- /* c.sw x8,0(x11) next address to DATA1; c.ebreak */
- ch32_write_reg(s, CH32_DMPROGBUF2, 0x9002C180);
- ch32_write_reg(s, CH32_DMABSTRACTAUTO, 1);
- s->autoincrement = autoinc;
- }
- ch32_write_reg(s, CH32_DMDATA1, addr);
- ch32_write_reg(s, CH32_DMCOMMAND, 0x00240000); /* run progbuf */
- s->prog_state = PROG_READ;
- s->next_addr = addr;
+ ch32_status_t r;
+ for (int i = 0; i < 10; i++) {
+ if ((r = write_v(s, CH32_DMDATA1, addr)) != CH32_OK) return r;
+ if (write && (r = write_v(s, CH32_DMDATA0, data)) != CH32_OK) return r;
+ if ((r = exec(s, 0x00240000)) != CH32_OK) return r; /* run progbuf; for flash, also waits out the BUF_LOAD */
+ uint32_t next = 0;
+ if (ch32_read_reg(s, CH32_DMDATA1, &next) == CH32_OK && next == addr + 4) return CH32_OK;
}
- if (s->autoincrement) s->next_addr += 4;
+ return CH32_ERR_TIMEOUT;
+}
- r = wait_done(s);
- if (r != CH32_OK) return r;
+ch32_status_t ch32_read_word(ch32_swio_t *s, uint32_t addr, uint32_t *data)
+{
+ ch32_status_t r;
+ if ((r = ensure_chip(s)) != CH32_OK) return r;
+ if ((r = load_prog(s, PROG_READ)) != CH32_OK) return r;
+ if ((r = run_at(s, addr, 0, 0)) != CH32_OK) return r;
return ch32_read_reg(s, CH32_DMDATA0, data) == CH32_OK ? CH32_OK : CH32_ERR_TIMEOUT;
}
ch32_status_t ch32_write_word(ch32_swio_t *s, uint32_t addr, uint32_t data)
{
- ch32_status_t r = ensure_chip(s);
- if (r != CH32_OK) return r;
-
uint8_t is_flash = (addr & 0xFF000000u) == 0x08000000u ||
(addr & 0x1FFF0000u) == 0x1FFF0000u;
-
- if (s->prog_state != PROG_WRITE || is_flash != s->write_is_flash) {
- int started = 0;
- if (s->prog_state != PROG_WRITE) {
- ch32_write_reg(s, CH32_DMABSTRACTAUTO, 0);
- started = 1;
- if (s->prog_state != PROG_READ) load_prog_regs(s);
- /* c.lw x8,0(x10) value; c.lw x9,0(x11) address */
- ch32_write_reg(s, CH32_DMPROGBUF0, 0x41844100);
- /* c.sw x8,0(x9) store; c.addi x9,4 */
- ch32_write_reg(s, CH32_DMPROGBUF1, 0x0491C080);
- /* c.sw x9,0(x11) next address to DATA1; c.nop */
- ch32_write_reg(s, CH32_DMPROGBUF2, 0x0001C184);
- }
- if (is_flash) {
- /* c.sw x13,4(x12) CTLR = PAGE_PG|BUF_LOAD; c.lw x8,0(x12) STATR */
- ch32_write_reg(s, CH32_DMPROGBUF3, 0x4200C254);
- /* c.andi x8,1 BSY; c.bnez x8,-4 wait */
- ch32_write_reg(s, CH32_DMPROGBUF4, 0xFC758805);
- ch32_write_reg(s, CH32_DMPROGBUF5, 0x90029002); /* c.ebreak */
- } else {
- ch32_write_reg(s, CH32_DMPROGBUF3, 0x90029002);
- }
-
- ch32_write_reg(s, CH32_DMDATA1, addr);
- ch32_write_reg(s, CH32_DMDATA0, data);
- if (started) {
- ch32_write_reg(s, CH32_DMCOMMAND, 0x00240000);
- ch32_write_reg(s, CH32_DMABSTRACTAUTO, 1);
- }
- s->write_is_flash = is_flash;
- s->prog_state = PROG_WRITE;
- s->next_addr = addr;
- } else {
- if (addr != s->next_addr) ch32_write_reg(s, CH32_DMDATA1, addr);
- ch32_write_reg(s, CH32_DMDATA0, data); /* autoexec runs the store */
- }
- s->next_addr += 4;
-
- return is_flash ? wait_done(s) : CH32_OK;
+ ch32_status_t r;
+ if ((r = ensure_chip(s)) != CH32_OK) return r;
+ if ((r = load_prog(s, is_flash ? PROG_WRITE_FLASH : PROG_WRITE)) != CH32_OK) return r;
+ return run_at(s, addr, 1, data);
}
/* ------------------------------------------------------------------------ */
@@ -548,13 +593,75 @@ ch32_status_t ch32_verify(ch32_swio_t *s, uint32_t addr, const uint8_t *data, ui
return CH32_OK;
}
+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;
+
+/* 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)
+{
+ ch32_write_reg(s, CH32_DMABSTRACTCS, 0x08000700);
+ forget_state(s);
+ ch32_halt(s, 0);
+ ch32_retries++;
+}
+
+/* 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 r;
- if ((r = ch32_detect(s)) != CH32_OK) return r;
- if ((r = ch32_halt(s, 1)) != CH32_OK) return r;
- if ((r = ch32_write_flash(s, CH32_FLASH_BASE, image, len)) != CH32_OK) return r;
- if ((r = ch32_verify(s, CH32_FLASH_BASE, image, len)) != CH32_OK) return r;
+ ch32_retries = 0;
+ ch32_fail_blk = -1;
+ ch32_resume_from = 0;
+ /* The dock poll has usually just halted it; a fresh detect would reset
+ * the line again, so only do it if it isn't halted (read twice). */
+ uint32_t st1 = 0, st2 = 0;
+ int halted = ch32_read_reg(s, CH32_DMSTATUS, &st1) == CH32_OK && ch32_read_reg(s, CH32_DMSTATUS, &st2) == CH32_OK &&
+ 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;
+ }
+ for (int t = 0; t < 5; t++) {
+ if ((r = ensure_chip(s)) == CH32_OK && (r = ch32_unlock_flash(s)) == CH32_OK) break;
+ recover(s);
+ }
+ if (r != CH32_OK) return r;
+
+ uint32_t uid[3];
+ int have_uid = 1;
+ for (int i = 0; i < 3; i++)
+ if (ch32_read_word(s, 0x1FFFF7E8u + 4u * i, &uid[i]) != CH32_OK) 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;
+
+ for (uint32_t off = resume.next_blk * CH32_FLASH_BLOCK; off < len; off += CH32_FLASH_BLOCK) {
+ uint32_t n = len - off < CH32_FLASH_BLOCK ? len - off : CH32_FLASH_BLOCK;
+ for (int t = 0; t < 8; t++) {
+ r = ensure_chip(s);
+ if (r == CH32_OK && !s->flash_unlocked) r = ch32_unlock_flash(s);
+ if (r == CH32_OK) r = write_block(s, CH32_FLASH_BASE + off, image + off, n);
+ if (r == CH32_OK) r = ch32_verify(s, CH32_FLASH_BASE + off, image + off, n);
+ if (r == CH32_OK) break;
+ recover(s);
+ }
+ if (r != CH32_OK) {
+ ch32_fail_blk = (int32_t)(off / CH32_FLASH_BLOCK);
+ return r;
+ }
+ resume.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.h
index 90d898e..be9fa79 100644
--- a/firmware/keyword/Drivers/CH32V003/ch32v003_swio.h
+++ b/firmware/keyword/Drivers/CH32V003/ch32v003_swio.h
@@ -14,11 +14,17 @@
#include <stdint.h>
#include "stm32wbxx_hal.h"
-/* Bit timing lives in ch32v003_swio.c. These two are live (DOCK? t= od=):
- * every pulse is scaled by ch32_t_short / 16, and ch32_open_drain releases
- * the line for highs instead of driving them (needs a 1k pull-up). */
-extern uint32_t ch32_t_short;
+/* Bit timing lives in ch32v003_swio.c. These are live (DOCK? a= b= h= s= od=):
+ * the "1" low, "0" low and high times in wire cycles at 64 MHz, and
+ * 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_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_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 */
/* Idle time after each frame. 2 us is sometimes too short. */
#ifndef CH32_SWIO_FRAME_GAP_US
#define CH32_SWIO_FRAME_GAP_US 8