A truly modular keyboard
C
keybit v12: report a press within a few reads, watch the keyword's bus between every step, status stream at 0xE0
Nathan66c981e9a08f
diff --git a/firmware/keybit/link.c b/firmware/keybit/link.cindex 9a662f8..845b0bd 100644--- a/firmware/keybit/link.c+++ b/firmware/keybit/link.c@@ -87,10 +87,11 @@ int link_poll(const link_pins_t *p, int doorbell, uint8_t my_depth, const uint8_if (!doorbell && !DT(p)) return LINK_SKIPPED;CK_LOW(p);- /* A ringing child already holds DT low, so wait for it to let go- * before taking DT low as the ack. */- int ok = !doorbell ? 1 : wait_pin(p->dt, 1, ACK_US, idle);- if (ok > 0) ok = wait_pin(p->dt, 0, ACK_US, idle);+ /* A ringing child already holds DT low: it acks by letting go. (It used+ * to let go for 3 us and pull it low again, and a parent busy serving+ * I2C between looks missed that pulse and timed out.) Otherwise the ack+ * is DT low. */+ int ok = wait_pin(p->dt, doorbell ? 1 : 0, ACK_US, idle);if (ok <= 0) { CK_REL(p); return ok < 0 ? LINK_ABORTED : LINK_NO_ACK; }DelaySysTick(HALF);CK_REL(p);@@ -190,11 +191,9 @@ int link_serve(const link_pins_t *p, int ringing, const link_reply_t *reply,for (int i = 0; i < len; i++) crc = crc8(crc, out[i]);out[len++] = crc;- if (ringing) {- DT_REL(p);- Delay_Us(3); /* long enough for the parent to see DT high */- }- DT_LOW(p);+ /* Ack: let go of the doorbell, or pull DT low; then wait for CK high. */+ if (ringing) DT_REL(p);+ else DT_LOW(p);int ok = spin(&f, 1);DT_REL(p);if (!ok) return 0;diff --git a/firmware/keybit/link.h b/firmware/keybit/link.hindex e9b6ca0..5fbb5fe 100644--- a/firmware/keybit/link.h+++ b/firmware/keybit/link.h@@ -9,15 +9,15 @@* Request The parent pulls CK low with DT high.* Doorbell A child with events pulls DT low with CK high, so its parent* polls it now instead of at the next periodic poll.- * Ack The child pulls DT low while CK is low (if it was ringing the- * doorbell, it lets DT go high first so the edge is visible).- * The parent then releases CK, the child releases DT, and both- * lines high again marks the start of the frame.+ * Ack The child pulls DT low while CK is low; if it was ringing the+ * doorbell (DT already low), it lets DT go instead. The parent+ * then releases CK, the child releases DT, and both lines high+ * again marks the start of the frame.* Bits The parent clocks every bit. Each half period is* LINK_HALF_US. The parent's bits are sampled on CK rising;* the child sets its bits on CK falling and the parent reads* them just before CK falls again. MSB first.- * Frame parent -> child: 0xA6, parent depth, parent uid32 (4),+ * Frame parent -> child: 0xA7, parent depth, parent uid32 (4),* the parent's side this is (N/E/S/W)* child -> parent: child depth, n (<= 8), n events of 11* bytes (uid32, flags, parent uid32, parent@@ -33,7 +33,7 @@#include <stdint.h>-#define LINK_MAGIC 0xA6 /* v11 frames; v10 used 0xA5 and a different layout */+#define LINK_MAGIC 0xA7 /* v12; v11 used 0xA6 (doorbell ack was a pulse), v10 0xA5 */#define LINK_EVENTS 8#define LINK_EV_SIZE 11 /* uid32, flags, parent uid32, parent side, face */#define LINK_ME_SIZE 5 /* what a parent tells a child about itself: uid32, side */diff --git a/firmware/keybit/main.c b/firmware/keybit/main.cindex 56c6cab..671e2a6 100644--- a/firmware/keybit/main.c+++ b/firmware/keybit/main.c@@ -59,8 +59,14 @@* and its own side facing it* 0x53-0x7A the same events, 5 bytes each (UID, flags), for older keywords* (bit 0 pressed, bit 1 heartbeat, bit 2 a neighbour is on- * backwards, bits 4-7 origin depth)+ * backwards, bit 3 its place is new or due again, bits 4-7+ * origin depth)* 0x7B (write) n: drop the first n events, they were received+ * 0xE0 status stream, read from here only: key state (bit 0) and+ * backwards (bit 1), the event count, then events 5 bytes each+ * as at 0x53. A read that ends normally drops the events it+ * covered, up to the first one with bit 3 set: read that one's+ * place at 0x80 and drop it with 0x7B.** PD1 stays SWIO so the keyword can always reflash us. Don't remap it.*/@@ -69,11 +75,12 @@#include "link.h"#include <string.h>-#define FW_VERSION 11+#define FW_VERSION 12#define KEYBIT_DEFAULT_ADDR 0x30#define LINK_ADDR 0x31 /* reserved: the keyword never assigns it */#define BTN_PIN PD6 /* MX switch to GND */-#define DEBOUNCE 200 /* main loop passes */+#define KEY_AGREE 3 /* reads in a row that must agree on a change */+#define KEY_LOCK_MS 5 /* after a change, contact bounce: ignore the pin */#define DEPTH_NONE 0xFF#define FRESH_MS 600 /* a neighbour's depth counts this long */@@ -83,6 +90,7 @@#define LINK_FRESH_MS 1000 /* a side counts as a link this long after a frame */#define LINK_REQ_US 50 /* CK held low this long on an unknown side = link request */#define HEARTBEAT_MS 1000+#define WHERE_EVERY 4 /* heartbeats; our place goes along on every 4th */#define BACKWARDS_MS 200 /* DT low, CK high this long = neighbour on backwards */#define QUEUE_LEN 32#define BATCH 8@@ -97,6 +105,9 @@#define EV_PRESSED 0x01#define EV_HEARTBEAT 0x02#define EV_BACKWARDS 0x04+#define EV_WHERE 0x08 /* parent, side and face in this event are news */+#define REG_STREAM 0xE0+#define STREAM_EV 5 /* uid32, flags */typedef struct {uint16_t magic;@@ -146,11 +157,11 @@ static side_t sides[4];/* Link diagnostics, read over I2C at 0x30 + 8 * side (S, E, W, N): link* requests we sent, ones that came back ok, no ack, called off for keyword* traffic, bad frames, requests we answered as a child, ones we failed to- * answer, and probes skipped because the lines weren't idle. Each counts up- * to 255 and stays there. */+ * answer, and probes skipped because the lines weren't idle. Each wraps at+ * 256, so two reads a while apart give rates. */enum { D_REQ, D_OK, D_NO_ACK, D_ABORT, D_BAD, D_SERVED, D_SERVE_FAIL, D_SKIPPED, D_N };static uint8_t diag[4][D_N];-static void diag_count(int s, int what) { if (diag[s][what] < 255) diag[s][what]++; }+static void diag_count(int s, int what) { diag[s][what]++; }static uint8_t backwards; /* reg 0x09: sides with a Keybit on backwards */static uint8_t uid[12];@@ -214,6 +225,7 @@ static void queue_push(const event_t *e)/* The side our depth comes from, so we can say where we hang off. */static int8_t up_side = -1;+/* flags may include EV_WHERE when our place is news. */static void queue_own(uint8_t flags){event_t e;@@ -291,6 +303,10 @@ static void polled_by(int s, uint8_t depth)sides[s].parent_seen = ticks();}+static int8_t kw_side = -1; /* the side the keyword has addressed us on */+static const soft_i2c_target_t target;+static void listen(void);+static int match_addr(uint8_t addr, soft_i2c_bus_t *bus){int s = side_index(bus);@@ -303,12 +319,37 @@ static int match_addr(uint8_t addr, soft_i2c_bus_t *bus)sides[s].keyword = 1;sides[s].link = 0;polled_by(s, 0);+ kw_side = (int8_t)s;}return mine;}-static uint8_t read_reg(uint8_t reg, soft_i2c_bus_t *bus)+/* The keyword's START only lasts one SCL high period, and a main loop pass+ * takes longer than that, so look at its bus between every step, not once+ * a pass. */+static void listen(void)+{+ if (kw_side >= 0) soft_i2c_poll(&buses[kw_side], &target);+}++/* The first n events in the queue, up to the first one with EV_WHERE. */+static int plain_events(int n)+{+ int i = 0;+ while (i < n && i < q_count && !(queue_at(i)->flags & EV_WHERE)) i++;+ return i;+}++static uint8_t read_reg(uint8_t reg, uint8_t idx, soft_i2c_bus_t *bus){+ if ((uint8_t)(reg - idx) == REG_STREAM) {+ if (idx == 0) return (uint8_t)(key_down | (backwards ? 2 : 0));+ if (idx == 1) return q_count < BATCH ? q_count : BATCH;+ int i = (idx - 2) / STREAM_EV, b = (idx - 2) % STREAM_EV;+ if (i >= q_count || i >= BATCH) return 0xFF;+ const event_t *e = queue_at(i);+ return b < 4 ? e->uid[b] : e->flags;+ }if (reg >= 0x02 && reg <= 0x05) return uid[reg - 0x02];if (reg >= 0x10 && reg <= 0x1B) return uid[reg - 0x10];if (reg >= 0x30 && reg < 0x30 + sizeof(diag)) return diag[(reg - 0x30) / D_N][(reg - 0x30) % D_N];@@ -336,6 +377,13 @@ static uint8_t read_reg(uint8_t reg, soft_i2c_bus_t *bus)}}+/* A status stream read went through: drop the events it covered whole. */+static void read_done(uint8_t first, uint8_t n)+{+ if (first != REG_STREAM || n < 2 + STREAM_EV) return;+ queue_pop(plain_events((n - 2) / STREAM_EV));+}+static void write_reg(uint8_t reg, uint8_t value, soft_i2c_bus_t *bus){if (reg == 0x7B) { queue_pop(value); return; }@@ -365,6 +413,7 @@ static const soft_i2c_target_t target = {.read = read_reg,.write = write_reg,.lost = lost_arbitration,+ .read_done = read_done,};/* ------------------------------------------------------------------------ */@@ -384,7 +433,7 @@ static void update_depth(void)if (my_depth == DEPTH_NONE) from = -1;if (from != up_side) {up_side = from;- if (from >= 0) queue_own(0); /* tell the keyword where we hang off now */+ if (from >= 0) queue_own(EV_WHERE); /* tell the keyword where we hang off now */}}@@ -544,22 +593,27 @@ static void relay(int listening)sd->polled_at = ticks();sd->poll_wait = MS((child ? CHILD_POLL_MS : PROBE_MS) + jitter());poll_child(s, rang);+ listen();}}/* ------------------------------------------------------------------------ */-/* Counts towards the pin's level and only flips once it gets all the way. */+/* A change counts as soon as KEY_AGREE reads in a row see it, so a press+ * goes out within a few passes; then the pin is left alone for KEY_LOCK_MS+ * while the switch contacts bounce. */static void debounce_key(void){- static int16_t count;- if (!funDigitalRead(BTN_PIN)) { if (count < DEBOUNCE) count++; }- else if (count > 0) count--;-- uint8_t was = key_down;- if (count == DEBOUNCE) key_down = 1;- else if (count == 0) key_down = 0;- if (key_down != was) queue_own(0);+ static uint8_t agree;+ static uint32_t changed_at;+ if (!elapsed(changed_at, MS(KEY_LOCK_MS))) return;+ uint8_t down = !funDigitalRead(BTN_PIN);+ if (down == key_down) { agree = 0; return; }+ if (++agree < KEY_AGREE) return;+ agree = 0;+ key_down = down;+ changed_at = ticks();+ queue_own(0);}/* A Keybit turned the wrong way round puts its GND on our DT and its 3V3@@ -588,9 +642,10 @@ static void watch_backwards(int listening)static void heartbeat(void){static uint32_t last;+ static uint8_t beats;if (my_depth == DEPTH_NONE || !elapsed(last, MS(HEARTBEAT_MS + uid[1]))) return;last = ticks();- queue_own(EV_HEARTBEAT);+ queue_own(EV_HEARTBEAT | (++beats % WHERE_EVERY == 0 && up_side >= 0 ? EV_WHERE : 0));}int main(void)@@ -617,11 +672,16 @@ int main(void)for (int i = 0; i < 4; i++) {if (!is_link(i)) soft_i2c_poll(&buses[i], &target);answer_link(i, listening);+ listen();}debounce_key();+ listen();relay(listening);+ listen();doorbell();+ listen();watch_backwards(listening);+ listen();heartbeat();if (save_addr >= 0) {store_config((uint8_t)save_addr);diff --git a/firmware/keybit/soft_i2c.c b/firmware/keybit/soft_i2c.cindex 860a248..748d310 100644--- a/firmware/keybit/soft_i2c.c+++ b/firmware/keybit/soft_i2c.c@@ -63,23 +63,23 @@ static int send_ack(soft_i2c_bus_t *b)}/* Clock out 8 bits, then read the controller's ACK. Returns 1 on ACK, 0 on- * NACK or timeout, -1 if we lost arbitration. We hold SCL low (clock+ * NACK, -1 if we lost arbitration, -2 on a timeout. We hold SCL low (clock* stretching) while setting up each bit, so slow polling can't miss the* setup time. */static int send_byte(soft_i2c_bus_t *b, uint8_t v){for (int bit = 7; bit >= 0; bit--) {int one = (v >> bit) & 1;- if (!wait_scl(b, 0)) { SDA_REL(b); return 0; }+ if (!wait_scl(b, 0)) { SDA_REL(b); return -2; }SCL_LOW(b);if (one) SDA_REL(b); else SDA_LOW(b);SCL_REL(b);- if (!wait_scl(b, 1)) { SDA_REL(b); return 0; }+ if (!wait_scl(b, 1)) { SDA_REL(b); return -2; }if (one && !SDA(b)) return -1; /* someone else is sending a 0 */}- if (!wait_scl(b, 0)) { SDA_REL(b); return 0; }+ if (!wait_scl(b, 0)) { SDA_REL(b); return -2; }SDA_REL(b);- if (!wait_scl(b, 1)) return 0;+ if (!wait_scl(b, 1)) return -2;int ack = !SDA(b);wait_scl(b, 0);return ack;@@ -114,12 +114,16 @@ static int serve(soft_i2c_bus_t *b, const soft_i2c_target_t *t)if (a & 1) { /* controller reads */int r;- while ((r = send_byte(b, t->read(reg_ptr++, b))) == 1) { }- if (r < 0) {+ uint8_t first = reg_ptr, n = 0;+ while ((r = send_byte(b, t->read(reg_ptr++, n, b))) == 1) n++;+ n++;+ if (r == 0 && t->read_done) t->read_done(first, n);+ if (r == -1) {if (t->lost) t->lost(addr);wait_bus_free(b);return addressed;}+ if (r < 0) return addressed;/* NACK ends the read; next is STOP or a repeated START */ev = recv_byte(b, &a);if (ev == EV_START) continue;diff --git a/firmware/keybit/soft_i2c.h b/firmware/keybit/soft_i2c.hindex 549aa10..136bfec 100644--- a/firmware/keybit/soft_i2c.h+++ b/firmware/keybit/soft_i2c.h@@ -27,12 +27,16 @@ typedef struct {/* Return 1 if we answer to this 7-bit address on this bus. Called for* every address seen, so it also tells us who else uses the bus. */int (*match)(uint8_t addr, soft_i2c_bus_t *bus);- /* A byte the controller reads, from register reg. */- uint8_t (*read)(uint8_t reg, soft_i2c_bus_t *bus);+ /* A byte the controller reads, from register reg, the idx-th of this read+ * (so reg - idx is where the read started). */+ uint8_t (*read)(uint8_t reg, uint8_t idx, soft_i2c_bus_t *bus);/* A byte the controller wrote to register reg. */void (*write)(uint8_t reg, uint8_t value, soft_i2c_bus_t *bus);/* We lost arbitration while answering on addr. */void (*lost)(uint8_t addr);+ /* A read that started at register first ended the normal way, the+ * controller NACKing the last of the n bytes we sent: it has them all. */+ void (*read_done)(uint8_t first, uint8_t n);} soft_i2c_target_t;/* Make both pins open-drain and released (high through the 10k pull-ups). */