Skip to content

A truly modular keyboard

C

keyword: dock works without the 1k pull-up, LED states for the dock

Nathan5d8f8a8f7a24
diff --git a/firmware/keyword/Core/Src/main.c b/firmware/keyword/Core/Src/main.c
index 9c87117..dfa1aa7 100644
--- a/firmware/keyword/Core/Src/main.c
+++ b/firmware/keyword/Core/Src/main.c
@@ -22,6 +22,7 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
+#include <string.h>
#include "app_entry.h"
#include "usb_device.h"
#include "ch32v003_swio.h"
@@ -103,7 +104,10 @@ static void LED_Set(uint8_t r, uint8_t g, uint8_t b)
* white blinking: a Keybit just connected on a side
* blue Keybit on the dock (flashed or up to date)
* white idle */
-static uint8_t dock_present, dock_failed, bus_held;
+/* Dock: EMPTY until something answers, WORKING while flashing (blue), then
+ * DONE (green) or FAILED (red, gave up) until that board is taken off. */
+enum { DOCK_EMPTY, DOCK_WORKING, DOCK_DONE, DOCK_FAILED };
+static uint8_t dock_state, bus_held;
static uint32_t white_until;
static uint32_t green_until; /* green blink after a Keybit is flashed or found up to date */
@@ -113,14 +117,15 @@ static uint32_t green_until; /* green blink after a Keybit is flashed or found
static void LED_Update(void)
{
uint32_t now = HAL_GetTick();
- if (dock_failed) LED_Set(1, 0, 0);
+ if (dock_state == DOCK_FAILED) LED_Set(1, 0, 0);
else if ((int32_t)(green_until - now) > 0) LED_Set(0, ((green_until - now) / GREEN_BLINK_MS) & 1, 0);
else if (bus_held) LED_Set(((now / 250) & 1) == 0, 0, 0);
else if ((int32_t)(white_until - now) > 0) {
uint8_t on = ((now / 100) & 1) == 0;
LED_Set(on, on, on);
}
- else if (dock_present) LED_Set(0, 0, 1);
+ else if (dock_state == DOCK_DONE) LED_Set(0, 1, 0);
+ else if (dock_state == DOCK_WORKING) LED_Set(0, 0, 1);
else LED_Set(1, 1, 1);
}
@@ -129,34 +134,42 @@ static void LED_Update(void)
* and flash keybit_image onto it. Until one has been flashed (or found up
* to date) it keeps retrying every poll, even after a failed attempt, and
* reports every attempt that got any answer over serial as DOCK lines. */
-static void Keybit_Dock_Poll(void)
+/* Halt whatever is on the dock and read its 96-bit UID, trusting it only
+ * when two reads agree. */
+static ch32_status_t Keybit_Dock_Uid(uint32_t uid[3])
{
- static uint32_t last_poll;
- 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) {
- /* A few misses in a row before calling it gone, so a wobble while
- * 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;
- PROTO_Send("DOCK gone");
+ ch32_status_t r = ch32_halt(&keybit_swio, 0);
+ if (r != CH32_OK && ch32_detect(&keybit_swio) == CH32_OK) r = ch32_halt(&keybit_swio, 0);
+ if (r != CH32_OK) return r;
+ for (int t = 0; t < 3; t++) {
+ int ok = 1;
+ for (int i = 0; i < 3 && ok; i++) {
+ uint32_t again = 0;
+ ok = ch32_read_word(&keybit_swio, 0x1FFFF7E8u + 4u * i, &uid[i]) == CH32_OK &&
+ ch32_read_word(&keybit_swio, 0x1FFFF7E8u + 4u * i, &again) == CH32_OK && uid[i] == again;
}
- return;
+ if (ok) return CH32_OK;
}
- misses = 0;
- if (done) return;
- dock_present = 1;
- LED_Update(); /* blue while flashing */
+ return CH32_ERR_VERIFY;
+}
+
+/* One flash attempt on the docked board: done if it verifies or programs,
+ * failed once 5 attempts in a row get no further than before. */
+static int32_t best_blk; /* furthest block the current board's attempts got to */
+static uint8_t stalls; /* attempts in a row that got no further */
+
+static void Keybit_Dock_Start(void)
+{
+ dock_state = DOCK_WORKING;
+ best_blk = -1;
+ stalls = 0;
+ LED_Update(); /* blue while flashing */
+}
+
+static void Keybit_Dock_Attempt(void)
+{
+ if (dock_state != DOCK_WORKING) return;
- HAL_Delay(50); /* let it finish powering up off CN7 */
/* 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);
@@ -166,20 +179,71 @@ static void Keybit_Dock_Poll(void)
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;
- dock_failed = 0;
+ ch32_reboot(&keybit_swio);
+ dock_state = DOCK_DONE;
+ stalls = 0;
green_until = HAL_GetTick() + 6 * GREEN_BLINK_MS;
PROTO_Send("DOCK up to date");
return;
}
ch32_status_t p = ch32_program(&keybit_swio, keybit_image, keybit_image_len);
- done = p == CH32_OK;
- dock_failed = !done;
- if (done) green_until = HAL_GetTick() + 6 * GREEN_BLINK_MS;
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);
+ if (p == CH32_OK) {
+ dock_state = DOCK_DONE;
+ stalls = 0;
+ green_until = HAL_GetTick() + 6 * GREEN_BLINK_MS;
+ return;
+ }
+ if (ch32_fail_blk > best_blk) {
+ best_blk = ch32_fail_blk;
+ stalls = 0;
+ } else if (++stalls >= 5) {
+ dock_state = DOCK_FAILED;
+ stalls = 0;
+ PROTO_Send("DOCK failed at block %ld", (long)best_blk);
+ }
+}
+
+static void Keybit_Dock_Poll(void)
+{
+ static uint32_t last_poll, uid[3];
+ static uint8_t misses;
+
+ 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) {
+ /* Only call it gone after about a second of misses: detect still drops one now and then. */
+ if (dock_state != DOCK_EMPTY && ++misses >= 4) {
+ dock_state = DOCK_EMPTY;
+ misses = 0;
+ PROTO_Send("DOCK gone");
+ }
+ return;
+ }
+
+ if (dock_state == DOCK_EMPTY) {
+ HAL_Delay(50); /* let it finish powering up off CN7 */
+ if (Keybit_Dock_Uid(uid) != CH32_OK) return;
+ misses = 0;
+ Keybit_Dock_Start();
+ } else if (misses) {
+ /* It answered again after a gap: the same board carries on where it was,
+ * a different one starts over. */
+ uint32_t now_uid[3];
+ misses = 0;
+ if (Keybit_Dock_Uid(now_uid) != CH32_OK) return;
+ if (memcmp(now_uid, uid, sizeof(uid)) != 0) {
+ memcpy(uid, now_uid, sizeof(uid));
+ Keybit_Dock_Start();
+ } else if (dock_state != DOCK_WORKING) {
+ ch32_reboot(&keybit_swio); /* back to running its firmware */
+ }
+ }
+ Keybit_Dock_Attempt();
}
/* DOCK? over serial: the raw answers from whatever is on the dock, to debug detection. */
diff --git a/firmware/keyword/Core/Src/proto.c b/firmware/keyword/Core/Src/proto.c
index 5f90a63..6b6488a 100644
--- a/firmware/keyword/Core/Src/proto.c
+++ b/firmware/keyword/Core/Src/proto.c
@@ -289,29 +289,29 @@ static void Handle_Line(proto_port_t *port, char *cmd)
}
else if (strcmp(cmd, "DOCKSCAN") == 0)
{
- /* Try write pulse widths ("1" low, "0" low, high) on whatever sits on
- * the dock, s=14, open drain or not (od=<0|1>, default the current
- * mode). 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 };
+ /* Try write pulse widths ("1" low, "0" low) and read sample points on
+ * whatever sits on the dock, high time fixed, open drain or not
+ * (od=<0|1>, default the current mode). 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[] = { 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];
+ static const uint8_t ss[] = { 16, 20, 24, 28, 32, 36, 40, 44 };
+ enum { NA = sizeof(as), NB = sizeof(bs), NS = sizeof(ss) };
+ int sc[NA][NB][NS];
const char *a = strstr(args, "od=");
if (a) ch32_open_drain = a[3] == '1';
- ch32_t_sample = 14;
+ ch32_hi = 46;
for (int i = 0; i < NA; i++)
for (int j = 0; j < NB; j++)
- for (int k = 0; k < NH; k++)
+ for (int k = 0; k < NS; k++)
{
keybit_dock_pause_until = HAL_GetTick() + 5000;
ch32_t1 = as[i];
ch32_t0 = bs[j];
- ch32_hi = hs[k];
+ ch32_t_sample = ss[k];
uint32_t cfg, status, bw, br;
int bad, hr;
sc[i][j][k] = -1;
@@ -320,27 +320,27 @@ static void Handle_Line(proto_port_t *port, char *cmd)
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);
+ if (rd >= 240) Reply(port, "DOCKSCAN a=%u b=%u s=%u rd=%d/256 hr=%d", as[i], bs[j], ss[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);
+ if (rd + wr >= 480) Reply(port, "DOCKSCAN a=%u b=%u s=%u rd=%d/256 wr=%d/256", as[i], bs[j], ss[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++)
+ for (int k = 0; k < NS; 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);
+ (k > 0 && sc[i][j][k - 1] >= 500) + (k < NS - 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];
+ ch32_t_sample = ss[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);
+ Reply(port, "DOCKSCAN best a=%u b=%u s=%u score=%d/512 nb=%d", as[bi], bs[bj], ss[bk], best, bn);
}
else if (strcmp(cmd, "DOCKID") == 0)
{
diff --git a/firmware/keyword/Drivers/CH32V003/ch32v003_swio.c b/firmware/keyword/Drivers/CH32V003/ch32v003_swio.c
index 1020e3b..3bb66c7 100644
--- a/firmware/keyword/Drivers/CH32V003/ch32v003_swio.c
+++ b/firmware/keyword/Drivers/CH32V003/ch32v003_swio.c
@@ -93,7 +93,9 @@ static void refresh_moder(ch32_swio_t *s)
* 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.
+ * still change them live. Push-pull by default: drive the bits, then for a
+ * read just let go onto the internal pull-up (no kick, that fights the
+ * target) and sample late, ~500 ns, so no external pull-up is needed.
* 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
@@ -105,10 +107,10 @@ 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_t_kick = 0; /* push-pull read: cycles driven high after the low, before letting go; any kick breaks reads */
+uint32_t ch32_t_sample = 30; /* cycles from release to sampling, plus ~3; ~500 ns, late enough for the internal pull-up */
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 */
+uint8_t ch32_open_drain = 0; /* 1 needs the external ~1k pull-up; push-pull works with or without it */
/* Pulse lengths in cycles for one frame, worked out before interrupts go
* off so no multiply or divide sits between edges. */
@@ -306,10 +308,18 @@ 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 */
- /* One garbled read shouldn't fail the halt, so give it a few. */
+ /* A haltreq or a DMSTATUS read can get lost, so ask again a couple of times. */
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;
+ for (int t = 0; t < 3; t++) {
+ if (t) {
+ ch32_write_reg(s, CH32_DMCONTROL, 0x80000001);
+ ch32_write_reg(s, CH32_DMCONTROL, 0x80000001);
+ HAL_Delay(1);
+ }
+ 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 && (st & (1u << 9))) break;
+ }
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 */
@@ -653,10 +663,15 @@ ch32_status_t ch32_program(ch32_swio_t *s, const uint8_t *image, uint32_t len)
}
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;
+ /* 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));