Skip to content

A truly modular keyboard

C

keybit v12: sit out other Keybits' transfers by watching for STOP, events with their place go whole in the status stream

Nathana20ac5b4d99c
diff --git a/firmware/keybit/main.c b/firmware/keybit/main.c
index 671e2a6..8731ea5 100644
--- a/firmware/keybit/main.c
+++ b/firmware/keybit/main.c
@@ -63,10 +63,10 @@
* 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.
+ * backwards (bit 1), the event count, then the events: 5 bytes
+ * (UID, flags as at 0x53), or all 11 as at 0x80 when flags bit
+ * 3 says the place is in it. A read that ends normally drops
+ * the events it covered whole.
*
* PD1 stays SWIO so the keyword can always reflash us. Don't remap it.
*/
@@ -107,7 +107,7 @@
#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 */
+#define STREAM_EV 5 /* uid32, flags; LINK_EV_SIZE with EV_WHERE */
typedef struct {
uint16_t magic;
@@ -332,23 +332,20 @@ 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 int stream_size(const event_t *e) { return e->flags & EV_WHERE ? LINK_EV_SIZE : STREAM_EV; }
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;
+ int pos = 2;
+ for (int i = 0; i < q_count && i < BATCH; i++) {
+ const event_t *e = queue_at(i);
+ if (idx < pos + stream_size(e)) return ((const uint8_t *)e)[idx - pos];
+ pos += stream_size(e);
+ }
+ return 0xFF;
}
if (reg >= 0x02 && reg <= 0x05) return uid[reg - 0x02];
if (reg >= 0x10 && reg <= 0x1B) return uid[reg - 0x10];
@@ -380,8 +377,10 @@ static uint8_t read_reg(uint8_t reg, uint8_t idx, 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));
+ if (first != REG_STREAM) return;
+ int pos = 2, k = 0;
+ while (k < q_count && k < BATCH && pos + stream_size(queue_at(k)) <= n) pos += stream_size(queue_at(k++));
+ queue_pop(k);
}
static void write_reg(uint8_t reg, uint8_t value, soft_i2c_bus_t *bus)
diff --git a/firmware/keybit/soft_i2c.c b/firmware/keybit/soft_i2c.c
index 748d310..711e0a6 100644
--- a/firmware/keybit/soft_i2c.c
+++ b/firmware/keybit/soft_i2c.c
@@ -85,14 +85,24 @@ static int send_byte(soft_i2c_bus_t *b, uint8_t v)
return ack;
}
-/* Sit out the rest of a transaction that isn't for us. */
-static void wait_bus_free(soft_i2c_bus_t *b)
+/* Sit out the rest of a transaction that isn't for us. It's called right
+ * after the address byte, before its 9th clock, so counting 9-bit bytes
+ * from here was one bit off: at the end it could be left waiting through
+ * TIMEOUT for an SCL fall after the STOP, deaf to the next transfer.
+ * Instead just watch for SDA moving while SCL stays high. Returns 1 on a
+ * repeated START (the caller reads its address), 0 on a STOP or when the
+ * lines sit still for TIMEOUT. */
+static int wait_bus_free(soft_i2c_bus_t *b)
{
- uint8_t dummy;
- for (int n = 0; n < 64; n++) {
- int ev = recv_byte(b, &dummy);
- if (ev == EV_STOP || ev == EV_TIMEOUT) return;
- if (ev == EV_BYTE) { if (!wait_scl(b, 1) || !wait_scl(b, 0)) return; } /* 9th clock */
+ int scl = SCL(b), sda = SDA(b);
+ uint32_t t0 = SysTick->CNT;
+ for (;;) {
+ int c = SCL(b), d = SDA(b);
+ if (c && scl && d != sda) return !d; /* SDA fell: START, rose: STOP */
+ if (c != scl || d != sda) t0 = SysTick->CNT;
+ else if (SysTick->CNT - t0 >= TIMEOUT) return 0;
+ scl = c;
+ sda = d;
}
}
@@ -108,7 +118,10 @@ static int serve(soft_i2c_bus_t *b, const soft_i2c_target_t *t)
if (ev == EV_START) continue;
if (ev != EV_BYTE) return addressed;
uint8_t addr = a >> 1;
- if (!t->match(addr, b)) { wait_bus_free(b); return addressed; }
+ if (!t->match(addr, b)) {
+ if (wait_bus_free(b)) continue;
+ return addressed;
+ }
addressed = 1;
if (!send_ack(b)) return addressed;