Skip to content

A truly modular keyboard

C

firmware: flash Keybits over SWIO, Keybit firmware with soft I2C and relaying

Nathanad068d5e6e42
diff --git a/firmware/Core/Inc/keybit_bus.h b/firmware/Core/Inc/keybit_bus.h
new file mode 100644
index 0000000..4cf79fd
--- /dev/null
+++ b/firmware/Core/Inc/keybit_bus.h
@@ -0,0 +1,63 @@
+/*
+ * Keybits on the keyword's side bus (I2C3): hand out addresses and list
+ * who is there. See firmware/keybit/main.c for the Keybit's register map.
+ */
+#ifndef KEYBIT_BUS_H
+#define KEYBIT_BUS_H
+
+#include <stdint.h>
+#include "stm32wbxx_hal.h"
+
+#define KEYBIT_DEFAULT_ADDR 0x30
+#define KEYBIT_MAX 8
+#define KEYBIT_REMOTE_MAX 112
+#define KEYBIT_REMOTE_TIMEOUT_MS 3000
+
+typedef struct {
+ uint8_t addr;
+ uint8_t uid[12]; /* factory 96-bit ID, little endian */
+ uint8_t fw;
+ uint8_t key_down;
+ char keybit_side; /* the Keybit's own side touching us: N/E/S/W */
+ char at; /* where it is on the keyword: L, R or T, '?' if unknown */
+} keybit_info_t;
+
+/* A Keybit further out, known only through events relayed by its
+ * neighbours. Entries are never moved, so an index stays valid. */
+typedef struct {
+ uint32_t uid; /* low 32 bits of its UID */
+ uint8_t present; /* 0 once it has gone quiet for KEYBIT_REMOTE_TIMEOUT_MS */
+ uint8_t key_down;
+ uint8_t depth; /* hops from the keyword, 2 or more */
+ uint8_t via; /* address of the touching Keybit it came through */
+ uint8_t dirty; /* set on any change; the consumer clears it */
+ uint32_t last_seen; /* HAL_GetTick() */
+} keybit_remote_t;
+
+/* Give every Keybit still on the default address its own address. Returns
+ * how many were assigned. */
+int keybit_bus_assign(I2C_HandleTypeDef *i2c);
+
+/* Read every assigned Keybit into out. Returns the count. Probes all
+ * addresses, so it takes tens of ms; run it every few hundred ms. */
+int keybit_bus_scan(I2C_HandleTypeDef *i2c, keybit_info_t *out, int max);
+
+/* Fast path for typing: refresh key_down of the n Keybits from the last
+ * scan, one 1-byte read each (well under 1 ms apiece at 100 kHz). Returns a
+ * bitmask of entries whose key state changed. */
+uint32_t keybit_bus_poll_keys(I2C_HandleTypeDef *i2c, keybit_info_t *k, int n);
+
+/* Drain relayed events from the n touching Keybits into the remote table
+ * (r, with *rn entries in use) and age out silent ones. Run it with
+ * keybit_bus_poll_keys. Returns 1 if any entry changed. */
+int keybit_bus_poll_relay(I2C_HandleTypeDef *i2c, const keybit_info_t *k, int n,
+ keybit_remote_t *r, int *rn);
+
+/* Plain register access for other devices on I2C3, such as the BQ25895
+ * charger (0x6A, never handed to a Keybit). Everything on the bus runs from
+ * the main loop, so call these from there too, not from an interrupt.
+ * Return 1 on success. */
+int keybit_bus_read(I2C_HandleTypeDef *i2c, uint8_t addr7, uint8_t reg, uint8_t *buf, uint8_t len);
+int keybit_bus_write(I2C_HandleTypeDef *i2c, uint8_t addr7, uint8_t reg, const uint8_t *buf, uint8_t len);
+
+#endif
diff --git a/firmware/Core/Inc/keybit_image.h b/firmware/Core/Inc/keybit_image.h
new file mode 100644
index 0000000..e794282
--- /dev/null
+++ b/firmware/Core/Inc/keybit_image.h
@@ -0,0 +1,11 @@
+/* Keybit firmware image (firmware/keybit), flashed onto Keybits over SWIO. */
+#ifndef KEYBIT_IMAGE_H
+#define KEYBIT_IMAGE_H
+
+#include <stdint.h>
+
+extern const uint8_t keybit_image[];
+extern const uint32_t keybit_image_len;
+extern const uint8_t keybit_image_version; /* FW_VERSION in keybit/main.c */
+
+#endif
diff --git a/firmware/Core/Src/keybit_bus.c b/firmware/Core/Src/keybit_bus.c
new file mode 100644
index 0000000..dabe31b
--- /dev/null
+++ b/firmware/Core/Src/keybit_bus.c
@@ -0,0 +1,189 @@
+#include "keybit_bus.h"
+#include <string.h>
+
+#define I2C_TIMEOUT_MS 10
+#define ADDR_FIRST 0x08
+#define ADDR_LAST 0x77
+#define CHARGER_ADDR 0x6A /* BQ25895 shares I2C3 */
+#define LINK_ADDR 0x31 /* Keybit-to-Keybit links */
+
+#define REG_MAGIC 0x00
+#define REG_KEY 0x06
+#define REG_UID 0x10
+#define REG_ASSIGN 0x20
+#define REG_EV_COUNT 0x52
+#define REG_EV_DATA 0x53
+#define REG_EV_POP 0x7B
+#define EV_BATCH 8
+#define EV_PRESSED 0x01
+#define MAGIC 0x4B
+
+static int is_ready(I2C_HandleTypeDef *i2c, uint8_t addr)
+{
+ return HAL_I2C_IsDeviceReady(i2c, (uint16_t)(addr << 1), 1, I2C_TIMEOUT_MS) == HAL_OK;
+}
+
+static int read_regs(I2C_HandleTypeDef *i2c, uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
+{
+ return HAL_I2C_Mem_Read(i2c, (uint16_t)(addr << 1), reg, I2C_MEMADD_SIZE_8BIT,
+ buf, len, I2C_TIMEOUT_MS) == HAL_OK;
+}
+
+static uint8_t free_address(I2C_HandleTypeDef *i2c)
+{
+ for (uint8_t a = ADDR_FIRST; a <= ADDR_LAST; a++) {
+ if (a == KEYBIT_DEFAULT_ADDR || a == LINK_ADDR || a == CHARGER_ADDR) continue;
+ if (!is_ready(i2c, a)) return a;
+ }
+ return 0;
+}
+
+int keybit_bus_assign(I2C_HandleTypeDef *i2c)
+{
+ int assigned = 0;
+ /* Each round, all unassigned Keybits answer the UID read together and
+ * arbitrate down to the lowest UID; that one gets an address and leaves
+ * the default address, so the next round finds the next one. */
+ for (int round = 0; round < KEYBIT_MAX; round++) {
+ uint8_t msg[13];
+ if (!read_regs(i2c, KEYBIT_DEFAULT_ADDR, REG_UID, msg, 12)) break;
+ uint8_t addr = free_address(i2c);
+ if (!addr) break;
+ msg[12] = addr;
+ if (HAL_I2C_Mem_Write(i2c, KEYBIT_DEFAULT_ADDR << 1, REG_ASSIGN, I2C_MEMADD_SIZE_8BIT,
+ msg, sizeof(msg), I2C_TIMEOUT_MS) != HAL_OK)
+ break;
+ HAL_Delay(15); /* it saves the address to flash, about 6 ms */
+ assigned++;
+ }
+ return assigned;
+}
+
+/* The Keybit's side touching us gives its place on the keyword. It can only
+ * mate upright, so its east side means it sits on our left, and so on. */
+static char keyword_side(char keybit_side)
+{
+ switch (keybit_side) {
+ case 'E': return 'L';
+ case 'W': return 'R';
+ case 'S': return 'T';
+ default: return '?';
+ }
+}
+
+int keybit_bus_scan(I2C_HandleTypeDef *i2c, keybit_info_t *out, int max)
+{
+ int n = 0;
+ for (uint8_t a = ADDR_FIRST; a <= ADDR_LAST && n < max; a++) {
+ if (a == KEYBIT_DEFAULT_ADDR || a == LINK_ADDR || a == CHARGER_ADDR) continue;
+ if (!is_ready(i2c, a)) continue;
+
+ uint8_t head[9];
+ if (!read_regs(i2c, a, REG_MAGIC, head, sizeof(head)) || head[0] != MAGIC) continue;
+
+ keybit_info_t *k = &out[n];
+ /* Reading the UID also catches two Keybits on one address: the loser of
+ * the arbitration drops its address and turns up on the default one. */
+ if (!read_regs(i2c, a, REG_UID, k->uid, sizeof(k->uid))) continue;
+ k->addr = a;
+ k->fw = head[1];
+ k->key_down = head[6] & 1;
+ k->keybit_side = (char)head[7];
+ k->at = keyword_side(k->keybit_side);
+ n++;
+ }
+ return n;
+}
+
+uint32_t keybit_bus_poll_keys(I2C_HandleTypeDef *i2c, keybit_info_t *k, int n)
+{
+ uint32_t changed = 0;
+ for (int i = 0; i < n; i++) {
+ uint8_t v;
+ if (!read_regs(i2c, k[i].addr, REG_KEY, &v, 1)) continue; /* next scan drops it */
+ if ((v & 1) != k[i].key_down) {
+ k[i].key_down = v & 1;
+ changed |= 1u << i;
+ }
+ }
+ return changed;
+}
+
+static uint32_t uid32(const uint8_t *b)
+{
+ return (uint32_t)b[0] | (uint32_t)b[1] << 8 | (uint32_t)b[2] << 16 | (uint32_t)b[3] << 24;
+}
+
+static keybit_remote_t *remote_slot(keybit_remote_t *r, int *rn, uint32_t uid)
+{
+ keybit_remote_t *free_slot = NULL;
+ for (int i = 0; i < *rn; i++) {
+ if (r[i].uid == uid) return &r[i];
+ if (!r[i].present && !r[i].dirty && !free_slot) free_slot = &r[i];
+ }
+ if (*rn < KEYBIT_REMOTE_MAX) return &r[(*rn)++];
+ return free_slot; /* NULL when the table is full */
+}
+
+int keybit_bus_poll_relay(I2C_HandleTypeDef *i2c, const keybit_info_t *k, int n,
+ keybit_remote_t *r, int *rn)
+{
+ int changed = 0;
+ uint32_t now = HAL_GetTick();
+
+ for (int i = 0; i < n; i++) {
+ uint8_t count;
+ if (!read_regs(i2c, k[i].addr, REG_EV_COUNT, &count, 1) || count == 0) continue;
+ if (count > EV_BATCH) count = EV_BATCH;
+ uint8_t ev[EV_BATCH * 5];
+ if (!read_regs(i2c, k[i].addr, REG_EV_DATA, ev, (uint16_t)(count * 5))) continue;
+ HAL_I2C_Mem_Write(i2c, (uint16_t)(k[i].addr << 1), REG_EV_POP, I2C_MEMADD_SIZE_8BIT,
+ &count, 1, I2C_TIMEOUT_MS);
+
+ for (int e = 0; e < count; e++) {
+ uint32_t uid = uid32(&ev[e * 5]);
+ uint8_t flags = ev[e * 5 + 4];
+
+ /* Touching Keybits queue their own events too; we already read
+ * those directly. */
+ int direct = 0;
+ for (int j = 0; j < n; j++) if (uid32(k[j].uid) == uid) direct = 1;
+ if (direct) continue;
+
+ keybit_remote_t *x = remote_slot(r, rn, uid);
+ if (!x) continue;
+ uint8_t down = flags & EV_PRESSED;
+ if (x->uid != uid || !x->present || x->key_down != down) {
+ x->dirty = 1;
+ changed = 1;
+ }
+ x->uid = uid;
+ x->present = 1;
+ x->key_down = down;
+ x->depth = flags >> 4;
+ x->via = k[i].addr;
+ x->last_seen = now;
+ }
+ }
+
+ for (int i = 0; i < *rn; i++) {
+ if (r[i].present && now - r[i].last_seen > KEYBIT_REMOTE_TIMEOUT_MS) {
+ r[i].present = 0;
+ r[i].key_down = 0;
+ r[i].dirty = 1;
+ changed = 1;
+ }
+ }
+ return changed;
+}
+
+int keybit_bus_read(I2C_HandleTypeDef *i2c, uint8_t addr7, uint8_t reg, uint8_t *buf, uint8_t len)
+{
+ return read_regs(i2c, addr7, reg, buf, len);
+}
+
+int keybit_bus_write(I2C_HandleTypeDef *i2c, uint8_t addr7, uint8_t reg, const uint8_t *buf, uint8_t len)
+{
+ return HAL_I2C_Mem_Write(i2c, (uint16_t)(addr7 << 1), reg, I2C_MEMADD_SIZE_8BIT,
+ (uint8_t *)buf, len, I2C_TIMEOUT_MS) == HAL_OK;
+}
diff --git a/firmware/Core/Src/keybit_image.c b/firmware/Core/Src/keybit_image.c
new file mode 100644
index 0000000..8296b01
--- /dev/null
+++ b/firmware/Core/Src/keybit_image.c
@@ -0,0 +1,296 @@
+/* Generated from firmware/keybit by `make image` there. Do not edit. */
+#include "keybit_image.h"
+
+const uint32_t keybit_image_len = 4616;
+const uint8_t keybit_image_version = 3;
+const uint8_t keybit_image[] __attribute__((aligned(4))) = {
+ 0x6f, 0x00, 0x20, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x36, 0x08, 0x00, 0x00, 0x34, 0x08, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x34, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x34, 0x08, 0x00, 0x00, 0x34, 0x08, 0x00, 0x00, 0x34, 0x08, 0x00, 0x00, 0x34, 0x08, 0x00, 0x00,
+ 0x34, 0x08, 0x00, 0x00, 0x34, 0x08, 0x00, 0x00, 0x34, 0x08, 0x00, 0x00, 0x34, 0x08, 0x00, 0x00,
+ 0x34, 0x08, 0x00, 0x00, 0x34, 0x08, 0x00, 0x00, 0x34, 0x08, 0x00, 0x00, 0x34, 0x08, 0x00, 0x00,
+ 0x34, 0x08, 0x00, 0x00, 0x34, 0x08, 0x00, 0x00, 0x34, 0x08, 0x00, 0x00, 0x34, 0x08, 0x00, 0x00,
+ 0x34, 0x08, 0x00, 0x00, 0x34, 0x08, 0x00, 0x00, 0x34, 0x08, 0x00, 0x00, 0x34, 0x08, 0x00, 0x00,
+ 0x34, 0x08, 0x00, 0x00, 0x34, 0x08, 0x00, 0x00, 0x34, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x2a, 0x86, 0x01, 0x45, 0x93, 0xf6, 0x15, 0x00, 0x91, 0xc2, 0x32, 0x95, 0x85, 0x81, 0x06, 0x06,
+ 0xf5, 0xf9, 0x82, 0x80, 0x00, 0x00, 0x00, 0x00, 0x63, 0x4e, 0x05, 0x02, 0x63, 0xc3, 0x05, 0x04,
+ 0x2e, 0x86, 0xaa, 0x85, 0x7d, 0x55, 0x15, 0xc2, 0x85, 0x46, 0x63, 0x78, 0xb6, 0x00, 0x63, 0x56,
+ 0xc0, 0x00, 0x06, 0x06, 0x86, 0x06, 0xe3, 0x6c, 0xb6, 0xfe, 0x01, 0x45, 0x63, 0xe4, 0xc5, 0x00,
+ 0x91, 0x8d, 0x55, 0x8d, 0x85, 0x82, 0x05, 0x82, 0xf5, 0xfa, 0x82, 0x80, 0x86, 0x82, 0xc9, 0x3f,
+ 0x2e, 0x85, 0x82, 0x82, 0x33, 0x05, 0xa0, 0x40, 0x63, 0x47, 0xb0, 0x00, 0xb3, 0x05, 0xb0, 0x40,
+ 0xc1, 0xb7, 0xb3, 0x05, 0xb0, 0x40, 0x86, 0x82, 0x65, 0x3f, 0x33, 0x05, 0xa0, 0x40, 0x82, 0x82,
+ 0x86, 0x82, 0x63, 0xc7, 0x05, 0x00, 0x63, 0x49, 0x05, 0x00, 0x5d, 0x37, 0x2e, 0x85, 0x82, 0x82,
+ 0xb3, 0x05, 0xb0, 0x40, 0xe3, 0x5b, 0x05, 0xfe, 0x33, 0x05, 0xa0, 0x40, 0x51, 0x3f, 0x33, 0x05,
+ 0xb0, 0x40, 0x82, 0x82, 0x00, 0x00, 0xb7, 0xf7, 0x00, 0xe0, 0x9c, 0x47, 0x37, 0xf7, 0x00, 0xe0,
+ 0x3e, 0x95, 0x1c, 0x47, 0x89, 0x8f, 0xe3, 0xce, 0x07, 0xfe, 0x82, 0x80, 0x83, 0xc7, 0x71, 0xc3,
+ 0xbe, 0x86, 0x63, 0x53, 0xf5, 0x00, 0xaa, 0x86, 0x03, 0xc7, 0x61, 0xc3, 0x95, 0x8f, 0xa3, 0x8b,
+ 0xf1, 0xc2, 0x36, 0x97, 0x7d, 0x8b, 0x23, 0x8b, 0xe1, 0xc2, 0x82, 0x80, 0x41, 0x11, 0x26, 0xc2,
+ 0x03, 0xc7, 0x71, 0xc3, 0x06, 0xc6, 0x22, 0xc4, 0x93, 0x07, 0x00, 0x02, 0xaa, 0x85, 0x63, 0x16,
+ 0xf7, 0x00, 0x2a, 0xc0, 0x05, 0x45, 0xd9, 0x37, 0x82, 0x45, 0x03, 0xc4, 0x71, 0xc3, 0x83, 0xc7,
+ 0x61, 0xc3, 0x13, 0x85, 0x41, 0xcb, 0xa2, 0x97, 0xfd, 0x8b, 0x13, 0x97, 0x27, 0x00, 0xba, 0x97,
+ 0x15, 0x46, 0x3e, 0x95, 0x4d, 0x2d, 0x05, 0x04, 0xa3, 0x8b, 0x81, 0xc2, 0xb2, 0x40, 0x22, 0x44,
+ 0x92, 0x44, 0x41, 0x01, 0x82, 0x80, 0x83, 0xa7, 0x81, 0xca, 0x31, 0x11, 0x3e, 0xc0, 0x83, 0xc7,
+ 0x51, 0xc3, 0x83, 0xc6, 0xe1, 0xc1, 0x06, 0xc8, 0x3d, 0x46, 0x93, 0xf7, 0xf7, 0x0f, 0x3d, 0x47,
+ 0x63, 0x63, 0xd6, 0x00, 0x36, 0x87, 0x12, 0x07, 0xb3, 0x37, 0xf0, 0x00, 0xd9, 0x8f, 0x5d, 0x8d,
+ 0x23, 0x02, 0xa1, 0x00, 0x0a, 0x85, 0x59, 0x37, 0xc2, 0x40, 0x51, 0x01, 0x82, 0x80, 0x51, 0x11,
+ 0x93, 0x87, 0x41, 0xc0, 0x22, 0xc2, 0x2a, 0x84, 0x2e, 0x85, 0xb7, 0xd5, 0xcc, 0xcc, 0x1d, 0x8d,
+ 0x93, 0x85, 0xd5, 0xcc, 0x06, 0xc4, 0x69, 0x3d, 0x13, 0x07, 0x10, 0x03, 0x93, 0x86, 0x81, 0xc4,
+ 0x2a, 0x86, 0x93, 0x17, 0x15, 0x00, 0x63, 0x1d, 0xe4, 0x00, 0xaa, 0x97, 0x8e, 0x07, 0xb6, 0x97,
+ 0x03, 0xc5, 0x07, 0x01, 0x13, 0x35, 0x15, 0x00, 0xa2, 0x40, 0x12, 0x44, 0x31, 0x01, 0x82, 0x80,
+ 0x33, 0x87, 0xa7, 0x00, 0x0e, 0x07, 0x36, 0x97, 0x85, 0x45, 0x23, 0x08, 0xb7, 0x00, 0x03, 0xc7,
+ 0x41, 0xc3, 0x19, 0xcf, 0x01, 0x45, 0xe3, 0x11, 0x87, 0xfe, 0xb2, 0x97, 0x8e, 0x07, 0xb6, 0x97,
+ 0x23, 0x80, 0x07, 0x00, 0x37, 0xf7, 0x00, 0xe0, 0x18, 0x47, 0x05, 0x45, 0xd8, 0xc3, 0xe9, 0xb7,
+ 0x13, 0x07, 0x00, 0x03, 0x01, 0x45, 0xe3, 0x11, 0xe4, 0xfc, 0xc5, 0xb7, 0x93, 0x07, 0xe5, 0xff,
+ 0x93, 0xf7, 0xf7, 0x0f, 0x0d, 0x47, 0x63, 0x68, 0xf7, 0x00, 0x93, 0x87, 0x81, 0xca, 0xaa, 0x97,
+ 0x03, 0xc5, 0xe7, 0xff, 0x82, 0x80, 0x93, 0x07, 0x05, 0xff, 0x93, 0xf7, 0xf7, 0x0f, 0x2d, 0x47,
+ 0x63, 0x68, 0xf7, 0x00, 0x93, 0x87, 0x81, 0xca, 0xaa, 0x97, 0x03, 0xc5, 0x07, 0xff, 0x82, 0x80,
+ 0x51, 0x11, 0x26, 0xc0, 0x93, 0x04, 0xd5, 0xfa, 0x06, 0xc4, 0x22, 0xc2, 0x13, 0xf7, 0xf4, 0x0f,
+ 0x93, 0x07, 0x70, 0x02, 0x63, 0xe7, 0xe7, 0x04, 0x95, 0x45, 0x26, 0x85, 0xf5, 0x3b, 0x83, 0xc7,
+ 0x71, 0xc3, 0x2a, 0x84, 0x13, 0x05, 0xf0, 0x0f, 0x63, 0x55, 0xf4, 0x02, 0x95, 0x45, 0x26, 0x85,
+ 0x81, 0x35, 0x03, 0xc7, 0x61, 0xc3, 0x93, 0x87, 0x41, 0xcb, 0x22, 0x97, 0x7d, 0x8b, 0x93, 0x16,
+ 0x27, 0x00, 0xba, 0x96, 0x91, 0x45, 0xb6, 0x97, 0x63, 0x0a, 0xb5, 0x00, 0x3e, 0x95, 0x03, 0x45,
+ 0x05, 0x00, 0xa2, 0x40, 0x12, 0x44, 0x82, 0x44, 0x31, 0x01, 0x82, 0x80, 0x03, 0xc5, 0x47, 0x00,
+ 0xcd, 0xbf, 0xa1, 0x47, 0x63, 0xec, 0xa7, 0x00, 0x85, 0x67, 0x93, 0x87, 0x47, 0x1c, 0x0a, 0x05,
+ 0x3e, 0x95, 0x1c, 0x41, 0x82, 0x87, 0x13, 0x05, 0xb0, 0x04, 0xe1, 0xbf, 0x13, 0x07, 0x10, 0x05,
+ 0x63, 0x04, 0xe5, 0x02, 0x13, 0x07, 0x20, 0x05, 0x63, 0x03, 0xe5, 0x02, 0x13, 0x05, 0xf0, 0x0f,
+ 0xc9, 0xb7, 0x03, 0xc5, 0x51, 0xc3, 0x13, 0x75, 0xf5, 0x0f, 0x65, 0xbf, 0x03, 0xc5, 0x25, 0x00,
+ 0x4d, 0xbf, 0x03, 0xc5, 0x41, 0xc3, 0x75, 0xb7, 0x03, 0xc5, 0xe1, 0xc1, 0x5d, 0xb7, 0x03, 0xc7,
+ 0x71, 0xc3, 0x13, 0x75, 0xf7, 0x0f, 0xe3, 0xfe, 0xe7, 0xf8, 0x21, 0x45, 0x59, 0xbf, 0x0d, 0x45,
+ 0x49, 0xbf, 0x03, 0xc7, 0x41, 0xc3, 0x19, 0xc7, 0x63, 0x16, 0xa7, 0x00, 0x23, 0x8a, 0x01, 0xc2,
+ 0x23, 0x9e, 0x01, 0xc0, 0x82, 0x80, 0x83, 0x47, 0x15, 0x00, 0xb7, 0x16, 0x01, 0x40, 0x93, 0x86,
+ 0x06, 0x80, 0x13, 0xd7, 0x47, 0x00, 0x2a, 0x07, 0x13, 0xf6, 0xf7, 0x00, 0xe1, 0x67, 0x36, 0x97,
+ 0x93, 0x87, 0x07, 0x6a, 0x14, 0x47, 0xb3, 0xd6, 0xc6, 0x00, 0x85, 0x8a, 0x63, 0x86, 0xb6, 0x00,
+ 0xfd, 0x17, 0xed, 0xfb, 0x01, 0x45, 0x82, 0x80, 0x05, 0x45, 0x82, 0x80, 0x21, 0x11, 0x37, 0x13,
+ 0x01, 0x40, 0x22, 0xc8, 0x26, 0xc6, 0x06, 0xca, 0x2a, 0xc0, 0x2e, 0xc2, 0x21, 0x44, 0x81, 0x44,
+ 0x13, 0x03, 0x03, 0x80, 0x02, 0x45, 0x81, 0x45, 0x1a, 0xc4, 0x75, 0x37, 0x22, 0x43, 0x19, 0xe5,
+ 0x0d, 0x45, 0xd2, 0x40, 0x42, 0x44, 0xb2, 0x44, 0x61, 0x01, 0x82, 0x80, 0x02, 0x45, 0x85, 0x45,
+ 0x1a, 0xc4, 0x51, 0x3f, 0x75, 0xd5, 0x82, 0x47, 0x22, 0x43, 0x05, 0x45, 0x83, 0xc5, 0x07, 0x00,
+ 0x83, 0xc7, 0x17, 0x00, 0xe1, 0x63, 0x93, 0xd6, 0x45, 0x00, 0xaa, 0x06, 0x9a, 0x96, 0x98, 0x46,
+ 0x13, 0xd6, 0x47, 0x00, 0xbd, 0x89, 0x33, 0x57, 0xb7, 0x00, 0x2a, 0x06, 0xbd, 0x8b, 0x05, 0x8b,
+ 0x1a, 0x96, 0x33, 0x15, 0xf5, 0x00, 0x93, 0x83, 0x13, 0x6a, 0x1c, 0x46, 0xe9, 0x8f, 0x81, 0xef,
+ 0x86, 0x04, 0xd9, 0x8c, 0x7d, 0x14, 0x93, 0xf4, 0xf4, 0x0f, 0x49, 0xfc, 0x92, 0x47, 0x01, 0x45,
+ 0x23, 0x80, 0x97, 0x00, 0x79, 0xbf, 0x9c, 0x46, 0xb3, 0xd7, 0xb7, 0x00, 0x85, 0x8b, 0x63, 0x05,
+ 0xf7, 0x00, 0x13, 0x85, 0x17, 0x00, 0x71, 0xb7, 0xfd, 0x13, 0xe3, 0x98, 0x03, 0xfc, 0x49, 0xb7,
+ 0x41, 0x11, 0x26, 0xc2, 0x06, 0xc6, 0x22, 0xc4, 0x03, 0x44, 0x05, 0x00, 0x37, 0x17, 0x01, 0x40,
+ 0x13, 0x07, 0x07, 0x80, 0x93, 0x74, 0xf4, 0x00, 0x11, 0x80, 0x2a, 0x04, 0x3a, 0x94, 0x93, 0x86,
+ 0x04, 0x01, 0x05, 0x47, 0x33, 0x17, 0xd7, 0x00, 0x18, 0xc8, 0x85, 0x45, 0x2a, 0xc0, 0xe5, 0x3d,
+ 0x82, 0x47, 0x11, 0xc5, 0x81, 0x45, 0x3e, 0x85, 0xfd, 0x35, 0x33, 0x35, 0xa0, 0x00, 0x85, 0x47,
+ 0xb3, 0x97, 0x97, 0x00, 0xb2, 0x40, 0x1c, 0xc8, 0x22, 0x44, 0x92, 0x44, 0x41, 0x01, 0x82, 0x80,
+ 0x41, 0x11, 0x22, 0xc4, 0x26, 0xc2, 0x06, 0xc6, 0x2a, 0x84, 0x93, 0x04, 0x00, 0x04, 0x93, 0x05,
+ 0x31, 0x00, 0x22, 0x85, 0xe5, 0x3d, 0x13, 0x07, 0xe5, 0xff, 0x85, 0x47, 0x63, 0xf5, 0xe7, 0x00,
+ 0x01, 0xc9, 0xfd, 0x14, 0xed, 0xf4, 0xb2, 0x40, 0x22, 0x44, 0x92, 0x44, 0x41, 0x01, 0x82, 0x80,
+ 0x85, 0x45, 0x22, 0x85, 0x4d, 0x35, 0x65, 0xd9, 0x81, 0x45, 0x22, 0x85, 0x69, 0x3d, 0x75, 0xf1,
+ 0xdd, 0xb7, 0x03, 0x47, 0x15, 0x00, 0xb7, 0x16, 0x01, 0x40, 0x93, 0x86, 0x06, 0x80, 0x93, 0x57,
+ 0x47, 0x00, 0xaa, 0x07, 0xb6, 0x97, 0x3d, 0x8b, 0x85, 0x46, 0x33, 0x97, 0xe6, 0x00, 0x98, 0xcb,
+ 0x85, 0x45, 0x95, 0xbd, 0x31, 0x11, 0x06, 0xc8, 0x22, 0xc6, 0x26, 0xc4, 0x03, 0x44, 0x05, 0x00,
+ 0xb7, 0x16, 0x01, 0x40, 0x93, 0x86, 0x06, 0x80, 0x13, 0x57, 0x44, 0x00, 0x2a, 0x07, 0x36, 0x97,
+ 0x1c, 0x47, 0x3d, 0x88, 0xb3, 0xd7, 0x87, 0x00, 0x85, 0x8b, 0x99, 0xe7, 0x01, 0x45, 0xc2, 0x40,
+ 0x32, 0x44, 0xa2, 0x44, 0x51, 0x01, 0x82, 0x80, 0x83, 0x44, 0x15, 0x00, 0x3a, 0xc2, 0x93, 0xd7,
+ 0x44, 0x00, 0xaa, 0x07, 0xb6, 0x97, 0x94, 0x47, 0xbd, 0x88, 0x3e, 0xc0, 0xb3, 0xd6, 0x96, 0x00,
+ 0x85, 0x8a, 0xe9, 0xde, 0x79, 0x45, 0xc5, 0x36, 0x12, 0x47, 0x14, 0x47, 0xb3, 0xd6, 0x86, 0x00,
+ 0x85, 0x8a, 0xe9, 0xd6, 0x82, 0x47, 0x94, 0x47, 0xb3, 0xd6, 0x96, 0x00, 0x85, 0x8a, 0xdd, 0xde,
+ 0x85, 0x46, 0x41, 0x04, 0x33, 0x94, 0x86, 0x00, 0x00, 0xcb, 0x13, 0x05, 0x80, 0x07, 0x65, 0x3e,
+ 0x82, 0x47, 0xc1, 0x04, 0x85, 0x46, 0xb3, 0x96, 0x96, 0x00, 0x79, 0x45, 0x94, 0xcb, 0x65, 0x36,
+ 0x05, 0x45, 0x71, 0xbf, 0x31, 0x11, 0x06, 0xc8, 0x26, 0xc4, 0x22, 0xc6, 0x03, 0x44, 0x05, 0x00,
+ 0x37, 0x17, 0x01, 0x40, 0x13, 0x07, 0x07, 0x80, 0x93, 0x76, 0xf4, 0x00, 0x11, 0x80, 0x2a, 0x04,
+ 0x3a, 0x94, 0x85, 0x44, 0x13, 0x87, 0x06, 0x01, 0x33, 0x97, 0xe4, 0x00, 0x18, 0xc8, 0x2a, 0xc2,
+ 0x79, 0x45, 0x36, 0xc0, 0x8d, 0x3e, 0x92, 0x47, 0x3e, 0x85, 0x21, 0x3f, 0x79, 0x45, 0xa5, 0x36,
+ 0x82, 0x46, 0xc2, 0x40, 0x79, 0x45, 0xb3, 0x94, 0xd4, 0x00, 0x04, 0xc8, 0x32, 0x44, 0xa2, 0x44,
+ 0x51, 0x01, 0x91, 0xbe, 0x93, 0x57, 0x45, 0x00, 0xb7, 0x16, 0x01, 0x40, 0x93, 0x86, 0x06, 0x80,
+ 0x05, 0x47, 0xaa, 0x07, 0x3d, 0x89, 0xb6, 0x97, 0x33, 0x15, 0xa7, 0x00, 0x88, 0xcb, 0x93, 0xd7,
+ 0x45, 0x00, 0xaa, 0x07, 0xbd, 0x89, 0xb6, 0x97, 0x33, 0x17, 0xb7, 0x00, 0x98, 0xcb, 0x82, 0x80,
+ 0x21, 0x11, 0x22, 0xc8, 0x26, 0xc6, 0x06, 0xca, 0x03, 0x47, 0x05, 0x00, 0xb7, 0x17, 0x01, 0x40,
+ 0x93, 0x87, 0x07, 0x80, 0x93, 0x54, 0x47, 0x00, 0xaa, 0x04, 0x2a, 0x84, 0xbe, 0x94, 0x93, 0x76,
+ 0xf7, 0x00, 0x8d, 0xcd, 0x85, 0x47, 0xb3, 0x97, 0xd7, 0x00, 0x9c, 0xc8, 0x79, 0x45, 0x2e, 0xc4,
+ 0x36, 0xc2, 0x3a, 0xc0, 0xcd, 0x3c, 0x22, 0x85, 0x69, 0x3d, 0x02, 0x47, 0x92, 0x46, 0xa2, 0x45,
+ 0x05, 0xe5, 0x2a, 0xc0, 0x83, 0x45, 0x14, 0x00, 0x03, 0x45, 0x04, 0x00, 0x61, 0x37, 0x82, 0x47,
+ 0xd2, 0x40, 0x42, 0x44, 0xb2, 0x44, 0x3e, 0x85, 0x61, 0x01, 0x82, 0x80, 0x13, 0x86, 0x06, 0x01,
+ 0x85, 0x47, 0xb3, 0x97, 0xc7, 0x00, 0xd1, 0xb7, 0x81, 0xcd, 0x9c, 0x44, 0xb3, 0xd7, 0xd7, 0x00,
+ 0x85, 0x8b, 0x99, 0xe7, 0x83, 0x45, 0x14, 0x00, 0x3a, 0x85, 0xa9, 0x3f, 0xfd, 0x57, 0xc9, 0xbf,
+ 0x79, 0x45, 0x55, 0x34, 0x83, 0x47, 0x14, 0x00, 0xb7, 0x16, 0x01, 0x40, 0x93, 0x86, 0x06, 0x80,
+ 0x13, 0xd7, 0x47, 0x00, 0x2a, 0x07, 0xbd, 0x8b, 0x36, 0x97, 0xc1, 0x07, 0x85, 0x46, 0xb3, 0x97,
+ 0xf6, 0x00, 0x1c, 0xcb, 0x85, 0x47, 0x6d, 0xb7, 0x21, 0x11, 0x06, 0xca, 0x22, 0xc8, 0x26, 0xc6,
+ 0x83, 0x47, 0x05, 0x00, 0xb7, 0x14, 0x01, 0x40, 0x93, 0x84, 0x04, 0x80, 0x93, 0xf6, 0xf7, 0x00,
+ 0x91, 0x83, 0xaa, 0x07, 0x05, 0x47, 0xa6, 0x97, 0x33, 0x17, 0xd7, 0x00, 0x98, 0xcb, 0x2a, 0x84,
+ 0x79, 0x45, 0x2e, 0xc4, 0x36, 0xc2, 0x3e, 0xc0, 0xb9, 0x34, 0x22, 0x85, 0xdd, 0x3b, 0x82, 0x47,
+ 0x92, 0x46, 0xa2, 0x45, 0x11, 0xed, 0x2a, 0xc0, 0x83, 0x45, 0x14, 0x00, 0x03, 0x45, 0x04, 0x00,
+ 0xd5, 0x35, 0x02, 0x47, 0xd2, 0x40, 0x42, 0x44, 0xb2, 0x44, 0x3a, 0x85, 0x61, 0x01, 0x82, 0x80,
+ 0x9c, 0x47, 0x79, 0x45, 0xb3, 0xd7, 0xd7, 0x00, 0x85, 0x8b, 0x9c, 0xc1, 0x29, 0x3c, 0x83, 0x47,
+ 0x14, 0x00, 0x85, 0x46, 0x13, 0xd7, 0x47, 0x00, 0xbd, 0x8b, 0x2a, 0x07, 0xc1, 0x07, 0x26, 0x97,
+ 0xb3, 0x94, 0xf6, 0x00, 0x04, 0xcb, 0x05, 0x47, 0xf1, 0xb7, 0x31, 0x11, 0x22, 0xc6, 0x26, 0xc4,
+ 0x06, 0xc8, 0x2a, 0xc0, 0x2e, 0x84, 0x9d, 0x44, 0x02, 0x45, 0xb3, 0x55, 0x94, 0x40, 0x85, 0x89,
+ 0xc1, 0x35, 0x05, 0x47, 0x63, 0x1d, 0xe5, 0x00, 0xfd, 0x14, 0x7d, 0x57, 0xe3, 0x96, 0xe4, 0xfe,
+ 0x02, 0x45, 0x4c, 0x00, 0x91, 0x3f, 0x01, 0xc5, 0x12, 0x45, 0x13, 0x35, 0x15, 0x00, 0xc2, 0x40,
+ 0x32, 0x44, 0xa2, 0x44, 0x51, 0x01, 0x82, 0x80, 0x11, 0x11, 0x22, 0xca, 0x26, 0xc8, 0x06, 0xcc,
+ 0x2a, 0xc0, 0x2e, 0x84, 0x21, 0x47, 0x81, 0x44, 0x02, 0x45, 0x6c, 0x00, 0x32, 0xc4, 0x3a, 0xc2,
+ 0x25, 0x37, 0x1d, 0xc1, 0xb2, 0x46, 0x12, 0x47, 0x86, 0x04, 0xd5, 0x8c, 0x7d, 0x17, 0x22, 0x46,
+ 0x93, 0xf4, 0xf4, 0x0f, 0x75, 0xf3, 0x02, 0x45, 0x23, 0x00, 0x94, 0x00, 0x93, 0x45, 0x16, 0x00,
+ 0x85, 0x35, 0x7d, 0x15, 0x13, 0x35, 0x15, 0x00, 0xe2, 0x40, 0x52, 0x44, 0xc2, 0x44, 0x71, 0x01,
+ 0x82, 0x80, 0x97, 0x01, 0x00, 0x20, 0x93, 0x81, 0xa1, 0xc3, 0x13, 0x81, 0x41, 0x40, 0x09, 0x65,
+ 0x13, 0x05, 0x05, 0x88, 0x73, 0x10, 0x05, 0x30, 0x97, 0x06, 0x00, 0x00, 0x93, 0x86, 0x86, 0x82,
+ 0x93, 0xe6, 0x36, 0x00, 0x73, 0x90, 0x56, 0x30, 0x13, 0x85, 0x41, 0xc2, 0x93, 0x85, 0x41, 0xd5,
+ 0x01, 0x46, 0x63, 0x56, 0xb5, 0x00, 0x10, 0xc1, 0x11, 0x05, 0xe3, 0x4e, 0xb5, 0xfe, 0x17, 0x15,
+ 0x00, 0x00, 0x13, 0x05, 0xa5, 0x9e, 0x93, 0x85, 0x41, 0xc0, 0x13, 0x86, 0x41, 0xc2, 0x63, 0x88,
+ 0xc5, 0x00, 0x14, 0x41, 0x94, 0xc1, 0x11, 0x05, 0x91, 0x05, 0xe3, 0x9a, 0xc5, 0xfe, 0xb7, 0xf7,
+ 0x00, 0xe0, 0x05, 0x47, 0x98, 0xc3, 0x85, 0x67, 0x93, 0x87, 0x47, 0x99, 0x73, 0x90, 0x17, 0x34,
+ 0x73, 0x00, 0x20, 0x30, 0x01, 0xa0, 0x37, 0x17, 0x02, 0x40, 0x1c, 0x47, 0xb7, 0x06, 0x80, 0x00,
+ 0xd5, 0x8f, 0x1c, 0xc7, 0x2a, 0x96, 0xaa, 0x87, 0x63, 0x93, 0xc7, 0x00, 0x82, 0x80, 0x23, 0x80,
+ 0xb7, 0x00, 0x85, 0x07, 0xd5, 0xbf, 0x81, 0x47, 0x63, 0x13, 0xf6, 0x00, 0x82, 0x80, 0x33, 0x87,
+ 0xf5, 0x00, 0x83, 0x46, 0x07, 0x00, 0x33, 0x07, 0xf5, 0x00, 0x85, 0x07, 0x23, 0x00, 0xd7, 0x00,
+ 0xe5, 0xb7, 0x11, 0xce, 0x83, 0x47, 0x05, 0x00, 0x03, 0xc7, 0x05, 0x00, 0x63, 0x85, 0xe7, 0x00,
+ 0x33, 0x85, 0xe7, 0x40, 0x82, 0x80, 0x7d, 0x16, 0x05, 0x05, 0x85, 0x05, 0xdd, 0xb7, 0x01, 0x45,
+ 0x82, 0x80, 0x51, 0x11, 0x22, 0xc2, 0x06, 0xc4, 0x13, 0x07, 0x00, 0x05, 0x2e, 0x84, 0x63, 0x1d,
+ 0xe5, 0x02, 0x93, 0x87, 0x41, 0xc0, 0xb7, 0xd5, 0xcc, 0xcc, 0x33, 0x05, 0xf6, 0x40, 0x93, 0x85,
+ 0xd5, 0xcc, 0xef, 0xf0, 0xef, 0xfe, 0x13, 0x17, 0x15, 0x00, 0x2a, 0x97, 0x0e, 0x07, 0x93, 0x87,
+ 0x81, 0xc4, 0xba, 0x97, 0x23, 0x80, 0x87, 0x00, 0x37, 0xf7, 0x00, 0xe0, 0x18, 0x47, 0xd8, 0xc3,
+ 0xa2, 0x40, 0x12, 0x44, 0x31, 0x01, 0x82, 0x80, 0x13, 0x07, 0xb0, 0x07, 0xaa, 0x87, 0x63, 0x18,
+ 0xe5, 0x00, 0x12, 0x44, 0xa2, 0x40, 0x2e, 0x85, 0x31, 0x01, 0x6f, 0xf0, 0x3f, 0x86, 0x13, 0x07,
+ 0x05, 0xfe, 0x13, 0x77, 0xf7, 0x0f, 0xb1, 0x46, 0xe3, 0xec, 0xe6, 0xfc, 0x13, 0x87, 0x81, 0xc3,
+ 0xb3, 0x06, 0xf7, 0x00, 0x23, 0x80, 0xb6, 0xfe, 0x93, 0x06, 0xc0, 0x02, 0xe3, 0x92, 0xd7, 0xfc,
+ 0x31, 0x46, 0x93, 0x85, 0x81, 0xca, 0x13, 0x85, 0x81, 0xc3, 0x03, 0x44, 0xc7, 0x00, 0x91, 0x3f,
+ 0x45, 0xf9, 0x05, 0xc0, 0x93, 0x07, 0x84, 0xff, 0x93, 0xf7, 0xf7, 0x0f, 0x13, 0x07, 0xf0, 0x06,
+ 0xe3, 0x60, 0xf7, 0xfa, 0x93, 0x07, 0x04, 0xfd, 0x93, 0xf7, 0xf7, 0x0f, 0x05, 0x47, 0xe3, 0x79,
+ 0xf7, 0xf8, 0x23, 0x8a, 0x81, 0xc2, 0x23, 0x9e, 0x81, 0xc0, 0x59, 0xb7, 0xb7, 0x27, 0x02, 0x40,
+ 0x05, 0x47, 0x98, 0xc3, 0xb7, 0x17, 0x02, 0x40, 0x37, 0x07, 0x08, 0x01, 0x23, 0xa2, 0x07, 0x00,
+ 0x13, 0x07, 0x17, 0x08, 0x98, 0xc3, 0x37, 0x07, 0x9f, 0x00, 0x98, 0xc7, 0x37, 0x17, 0x02, 0x40,
+ 0x1c, 0x43, 0x93, 0x96, 0x67, 0x00, 0xe3, 0xdd, 0x06, 0xfe, 0x5c, 0x43, 0xb7, 0x16, 0x02, 0x40,
+ 0xf1, 0x9b, 0x93, 0xe7, 0x27, 0x00, 0x5c, 0xc3, 0x21, 0x47, 0xdc, 0x42, 0xb1, 0x8b, 0xe3, 0x9e,
+ 0xe7, 0xfe, 0x82, 0x80, 0x19, 0x71, 0x86, 0xde, 0xa2, 0xdc, 0xa6, 0xda, 0x45, 0x3f, 0x37, 0x17,
+ 0x02, 0x40, 0x1c, 0x4f, 0x93, 0xe7, 0x57, 0x03, 0x1c, 0xcf, 0xb7, 0xf7, 0xff, 0x1f, 0x93, 0x87,
+ 0x07, 0x7e, 0x8c, 0x47, 0xd0, 0x47, 0x98, 0x4b, 0x93, 0x87, 0x81, 0xca, 0x23, 0xa4, 0xb1, 0xca,
+ 0xd0, 0xc3, 0x98, 0xc7, 0xb7, 0x47, 0x00, 0x08, 0x03, 0xd6, 0x07, 0xfc, 0x15, 0x67, 0x93, 0x86,
+ 0x81, 0xca, 0x13, 0x07, 0x27, 0xb4, 0x36, 0xc8, 0x63, 0x1f, 0xe6, 0x02, 0x03, 0xc7, 0x27, 0xfc,
+ 0x03, 0xc6, 0x37, 0xfc, 0x93, 0x87, 0x07, 0xfc, 0x93, 0x46, 0xf7, 0xff, 0x93, 0xf7, 0xf6, 0x0f,
+ 0x63, 0x13, 0xf6, 0x02, 0x93, 0x07, 0x87, 0xff, 0x93, 0xf7, 0xf7, 0x0f, 0x93, 0x06, 0xf0, 0x06,
+ 0x63, 0xeb, 0xf6, 0x00, 0x93, 0x07, 0x07, 0xfd, 0x93, 0xf7, 0xf7, 0x0f, 0x85, 0x46, 0x63, 0xf4,
+ 0xf6, 0x00, 0x23, 0x8a, 0xe1, 0xc2, 0x93, 0x87, 0x81, 0xc4, 0x7d, 0x57, 0x23, 0x80, 0xe7, 0x00,
+ 0x23, 0x84, 0xe7, 0x00, 0x23, 0x80, 0xe7, 0x02, 0x23, 0x8c, 0xe7, 0x00, 0x23, 0x8c, 0xe7, 0x02,
+ 0x23, 0x88, 0xe7, 0x02, 0x23, 0x88, 0xe7, 0x04, 0x23, 0x84, 0xe7, 0x04, 0xb7, 0x17, 0x01, 0x40,
+ 0x93, 0x87, 0x07, 0x40, 0x98, 0x43, 0xb7, 0x06, 0x00, 0xf1, 0xfd, 0x16, 0x75, 0x8f, 0xb7, 0x06,
+ 0x00, 0x08, 0x55, 0x8f, 0x98, 0xc3, 0x13, 0x07, 0x00, 0x04, 0x98, 0xcb, 0xb7, 0x12, 0x01, 0x40,
+ 0x13, 0x87, 0x81, 0xc4, 0x93, 0x87, 0x41, 0xc0, 0x3a, 0xca, 0x93, 0x82, 0x02, 0x80, 0x05, 0x43,
+ 0x3d, 0x44, 0x95, 0x43, 0x83, 0xc6, 0x07, 0x00, 0x95, 0x07, 0x13, 0xf5, 0xf6, 0x00, 0x91, 0x82,
+ 0xaa, 0x06, 0x96, 0x96, 0x33, 0x17, 0xa3, 0x00, 0x98, 0xca, 0x03, 0xc7, 0xc7, 0xff, 0x0a, 0x05,
+ 0x13, 0x76, 0xf7, 0x00, 0x11, 0x83, 0x2a, 0x07, 0x16, 0x97, 0xb3, 0x15, 0xc3, 0x00, 0x0c, 0xcb,
+ 0x84, 0x42, 0xb3, 0x15, 0xa4, 0x00, 0x93, 0xc5, 0xf5, 0xff, 0xe5, 0x8d, 0x33, 0x95, 0xa3, 0x00,
+ 0xc9, 0x8d, 0x8c, 0xc2, 0x0c, 0x43, 0x0a, 0x06, 0xb3, 0x16, 0xc4, 0x00, 0x93, 0xc6, 0xf6, 0xff,
+ 0xed, 0x8e, 0x33, 0x96, 0xc3, 0x00, 0xd1, 0x8e, 0x14, 0xc3, 0x23, 0x8f, 0x67, 0xfe, 0xa3, 0x8f,
+ 0x67, 0xfe, 0x13, 0x87, 0x81, 0xc1, 0xe3, 0x1f, 0xf7, 0xf8, 0x13, 0x84, 0x41, 0xc0, 0x83, 0x46,
+ 0x04, 0x00, 0x37, 0x16, 0x01, 0x40, 0x13, 0x06, 0x06, 0x80, 0x93, 0xd7, 0x46, 0x00, 0xaa, 0x07,
+ 0xb2, 0x97, 0x98, 0x47, 0xbd, 0x8a, 0x02, 0xc0, 0x33, 0x57, 0xd7, 0x00, 0x83, 0x46, 0x14, 0x00,
+ 0x05, 0x8b, 0x93, 0xd7, 0x46, 0x00, 0xaa, 0x07, 0xb2, 0x97, 0x9c, 0x47, 0xbd, 0x8a, 0xb3, 0xd7,
+ 0xd7, 0x00, 0x83, 0x46, 0x44, 0x00, 0x85, 0x8b, 0x81, 0xca, 0x99, 0xc7, 0x83, 0x46, 0x34, 0x00,
+ 0x81, 0xc6, 0x93, 0x46, 0x17, 0x00, 0x36, 0xc0, 0x23, 0x02, 0xf4, 0x00, 0x82, 0x47, 0xa3, 0x01,
+ 0xe4, 0x00, 0x63, 0x81, 0x07, 0x0e, 0x0c, 0x18, 0x22, 0x85, 0xef, 0xf0, 0x3f, 0x87, 0x85, 0x47,
+ 0xe3, 0x0b, 0xf5, 0xfe, 0x51, 0xe9, 0x83, 0x44, 0x01, 0x03, 0xa2, 0x85, 0x93, 0xd7, 0x14, 0x00,
+ 0x3e, 0x85, 0x3e, 0xc4, 0xef, 0xf0, 0xaf, 0xe9, 0x09, 0xe5, 0x22, 0x85, 0xef, 0xf0, 0x5f, 0x94,
+ 0xa5, 0xa8, 0x22, 0x85, 0xef, 0xf0, 0xdf, 0x8e, 0x25, 0xc9, 0x85, 0x88, 0x63, 0x82, 0x04, 0x2e,
+ 0x03, 0xc5, 0x41, 0xc2, 0xa2, 0x85, 0x93, 0x07, 0x15, 0x00, 0x23, 0x82, 0xf1, 0xc2, 0xef, 0xf0,
+ 0xef, 0xee, 0x37, 0x16, 0x01, 0x40, 0x2a, 0xc6, 0x9d, 0x46, 0x13, 0x06, 0x06, 0x80, 0xb2, 0x47,
+ 0x81, 0x45, 0x22, 0x85, 0xb3, 0xd7, 0xd7, 0x40, 0x85, 0x8b, 0x32, 0xce, 0x36, 0xcc, 0x3e, 0xc2,
+ 0xef, 0xf0, 0x6f, 0xfd, 0x83, 0x47, 0x04, 0x00, 0xe2, 0x46, 0x72, 0x46, 0x63, 0x12, 0x05, 0x1e,
+ 0x13, 0xd7, 0x47, 0x00, 0xb7, 0x16, 0x01, 0x40, 0x93, 0x86, 0x06, 0x80, 0x2a, 0x07, 0x36, 0x97,
+ 0xbd, 0x8b, 0x85, 0x46, 0xb3, 0x97, 0xf6, 0x00, 0x1c, 0xcb, 0x0c, 0x18, 0x22, 0x85, 0xef, 0xf0,
+ 0xef, 0xfd, 0x85, 0x47, 0xe3, 0x01, 0xf5, 0xf6, 0x03, 0x47, 0x04, 0x00, 0xb7, 0x16, 0x01, 0x40,
+ 0x93, 0x86, 0x06, 0x80, 0x93, 0x57, 0x47, 0x00, 0xaa, 0x07, 0xb6, 0x97, 0x9c, 0x47, 0x3d, 0x8b,
+ 0xb3, 0xd7, 0xe7, 0x00, 0x03, 0x47, 0x14, 0x00, 0x85, 0x8b, 0xa3, 0x01, 0xf4, 0x00, 0x93, 0x57,
+ 0x47, 0x00, 0xaa, 0x07, 0xb6, 0x97, 0x9c, 0x47, 0x3d, 0x8b, 0xb3, 0xd7, 0xe7, 0x00, 0x85, 0x8b,
+ 0x23, 0x02, 0xf4, 0x00, 0x15, 0x04, 0x93, 0x87, 0x81, 0xc1, 0xe3, 0x92, 0x87, 0xec, 0xb7, 0x17,
+ 0x01, 0x40, 0x93, 0x87, 0x07, 0x40, 0x94, 0x47, 0x83, 0x97, 0x61, 0xc2, 0x93, 0xf6, 0x06, 0x04,
+ 0x63, 0x9c, 0x06, 0x24, 0x93, 0x06, 0x70, 0x0c, 0x63, 0xc5, 0xf6, 0x00, 0x85, 0x07, 0x23, 0x93,
+ 0xf1, 0xc2, 0x83, 0xc6, 0x51, 0xc3, 0x03, 0x97, 0x61, 0xc2, 0x13, 0x06, 0x80, 0x0c, 0x93, 0xf6,
+ 0xf6, 0x0f, 0x63, 0x1f, 0xc7, 0x22, 0x05, 0x47, 0xa3, 0x8a, 0xe1, 0xc2, 0x83, 0xc7, 0x51, 0xc3,
+ 0x63, 0x85, 0xf6, 0x00, 0x01, 0x45, 0xef, 0xf0, 0x0f, 0xd5, 0x83, 0xc6, 0xc1, 0xc2, 0x89, 0xea,
+ 0xb7, 0xf6, 0x00, 0xe0, 0x94, 0x46, 0x23, 0xa4, 0xd1, 0xc2, 0x85, 0x46, 0x23, 0x86, 0xd1, 0xc2,
+ 0xb7, 0xf7, 0x00, 0xe0, 0x9c, 0x47, 0x03, 0xa7, 0x81, 0xc2, 0xb7, 0xf6, 0x36, 0x00, 0x93, 0x86,
+ 0xf6, 0xe7, 0x99, 0x8f, 0x63, 0xe3, 0xf6, 0x20, 0x03, 0xc7, 0xe1, 0xc1, 0x93, 0x07, 0xf0, 0x0f,
+ 0x63, 0x16, 0xf7, 0x3c, 0x83, 0x97, 0xc1, 0xc1, 0xe3, 0xc9, 0x07, 0xe2, 0x37, 0x06, 0x67, 0x45,
+ 0x37, 0x27, 0x02, 0x40, 0x13, 0x06, 0x36, 0x12, 0xb7, 0x96, 0xef, 0xcd, 0x50, 0xc3, 0x93, 0x86,
+ 0xb6, 0x9a, 0x54, 0xc3, 0x50, 0xd3, 0x54, 0xd3, 0x14, 0x4b, 0x13, 0xd6, 0x76, 0x00, 0x13, 0x76,
+ 0x16, 0x10, 0x5d, 0xea, 0xb7, 0x06, 0x02, 0x00, 0x14, 0xcb, 0xb7, 0x46, 0x00, 0x08, 0x93, 0x86,
+ 0x06, 0xfc, 0x54, 0xcb, 0xb7, 0x06, 0x02, 0x00, 0x93, 0x86, 0x06, 0x04, 0x14, 0xcb, 0xb7, 0x24,
+ 0x02, 0x40, 0xd8, 0x44, 0x05, 0x8b, 0x75, 0xff, 0x93, 0xf7, 0xf7, 0x0f, 0xc9, 0xc3, 0x15, 0x67,
+ 0x13, 0x07, 0x27, 0xb4, 0x23, 0x09, 0xf1, 0x02, 0x13, 0x06, 0xc0, 0x03, 0x93, 0xc7, 0xf7, 0xff,
+ 0x93, 0x05, 0xf0, 0x0f, 0x28, 0x18, 0x23, 0x18, 0xe1, 0x02, 0xa3, 0x09, 0xf1, 0x02, 0x1d, 0x36,
+ 0xc2, 0x57, 0x37, 0x27, 0x02, 0x40, 0x3e, 0xda, 0xc1, 0x67, 0x9c, 0xc8, 0xb7, 0x07, 0x09, 0x00,
+ 0x9c, 0xc8, 0xb7, 0x47, 0x00, 0x08, 0x93, 0x87, 0x07, 0xfc, 0xdc, 0xc8, 0x5c, 0x47, 0x85, 0x8b,
+ 0xf5, 0xff, 0xb7, 0x47, 0x00, 0x08, 0x54, 0x18, 0x93, 0x87, 0x07, 0xfc, 0x37, 0x27, 0x02, 0x40,
+ 0x37, 0x05, 0x05, 0x00, 0xb7, 0x45, 0x00, 0x08, 0x90, 0x42, 0x90, 0xc3, 0x08, 0xcb, 0x50, 0x47,
+ 0x05, 0x8a, 0x75, 0xfe, 0x91, 0x07, 0x91, 0x06, 0xe3, 0x98, 0xb7, 0xfe, 0xc1, 0x67, 0x93, 0x87,
+ 0x07, 0x04, 0x1c, 0xcb, 0x37, 0x27, 0x02, 0x40, 0x5c, 0x47, 0x85, 0x8b, 0xf5, 0xff, 0xb7, 0x27,
+ 0x02, 0x40, 0x13, 0x07, 0x00, 0x08, 0x98, 0xcb, 0xfd, 0x57, 0x23, 0x9e, 0xf1, 0xc0, 0xb1, 0xb3,
+ 0x83, 0x45, 0x14, 0x00, 0x05, 0x45, 0x93, 0xf4, 0xf7, 0x00, 0x13, 0xf3, 0xf5, 0x00, 0x91, 0x81,
+ 0xaa, 0x05, 0x13, 0x07, 0x03, 0x01, 0x33, 0x17, 0xe5, 0x00, 0xb2, 0x95, 0x98, 0xc9, 0x12, 0x47,
+ 0x91, 0x83, 0xaa, 0x07, 0xb2, 0x97, 0x15, 0xcb, 0x33, 0x15, 0x95, 0x00, 0x85, 0x42, 0x88, 0xcb,
+ 0x33, 0x93, 0x62, 0x00, 0x23, 0xa8, 0x65, 0x00, 0x22, 0x85, 0x85, 0x45, 0x32, 0xd0, 0x3e, 0xce,
+ 0x36, 0xcc, 0xef, 0xf0, 0x4f, 0xda, 0xe2, 0x46, 0xf2, 0x47, 0x02, 0x56, 0x85, 0x42, 0x19, 0xe9,
+ 0xb3, 0x92, 0x92, 0x00, 0x23, 0xa8, 0x57, 0x00, 0xcd, 0xb3, 0x93, 0x82, 0x04, 0x01, 0x33, 0x15,
+ 0x55, 0x00, 0xe9, 0xb7, 0x12, 0x47, 0x19, 0xc7, 0x8c, 0x47, 0xb3, 0xd5, 0x95, 0x00, 0x85, 0x89,
+ 0x63, 0x8e, 0x05, 0x3a, 0xfd, 0x16, 0xfd, 0x55, 0xe3, 0x93, 0xb6, 0xd8, 0x81, 0x45, 0x22, 0x85,
+ 0x3e, 0xc2, 0xef, 0xf0, 0x4f, 0xd6, 0x92, 0x47, 0x85, 0x46, 0x09, 0xe5, 0x33, 0x97, 0x96, 0x00,
+ 0x98, 0xcb, 0x65, 0xb3, 0xb3, 0x96, 0x96, 0x00, 0x94, 0xcb, 0x85, 0x45, 0x22, 0x85, 0x3e, 0xc2,
+ 0xef, 0xf0, 0x6f, 0xd4, 0xe3, 0x0b, 0x05, 0xd8, 0x92, 0x47, 0x81, 0x45, 0x22, 0x85, 0x9c, 0x47,
+ 0xb3, 0xd4, 0x97, 0x00, 0x85, 0x88, 0xef, 0xf0, 0x0f, 0xd3, 0xe3, 0x83, 0x04, 0xd2, 0xb5, 0xbb,
+ 0x82, 0x44, 0x4c, 0x18, 0x22, 0x85, 0xef, 0xf0, 0x6f, 0xd5, 0xe3, 0x1c, 0x05, 0xd6, 0x83, 0x45,
+ 0x41, 0x03, 0x91, 0xc8, 0x23, 0x82, 0xb1, 0xc2, 0x22, 0x85, 0xef, 0xf0, 0x6f, 0xde, 0xe3, 0x05,
+ 0x05, 0xd6, 0x81, 0x44, 0xf9, 0xbf, 0x03, 0xc5, 0x41, 0xc2, 0x22, 0x86, 0x93, 0x07, 0x15, 0x00,
+ 0x23, 0x82, 0xf1, 0xc2, 0x39, 0x34, 0xcd, 0xb7, 0xe3, 0x5d, 0xf0, 0xda, 0xfd, 0x17, 0x45, 0xbb,
+ 0xe3, 0x16, 0x07, 0xdc, 0xa3, 0x8a, 0x01, 0xc2, 0xd1, 0xb3, 0xd2, 0x47, 0x13, 0x07, 0xf0, 0x0f,
+ 0x13, 0x05, 0xf0, 0x0f, 0x3e, 0xc2, 0x93, 0x87, 0x81, 0xc4, 0x37, 0xf3, 0x00, 0xe0, 0xfd, 0x52,
+ 0x83, 0xc5, 0x07, 0x00, 0x63, 0x8c, 0xa5, 0x00, 0x03, 0x26, 0x83, 0x00, 0x83, 0xa3, 0x47, 0x00,
+ 0x33, 0x06, 0x76, 0x40, 0x63, 0xfd, 0xc6, 0x1c, 0x23, 0x80, 0x57, 0x00, 0xe1, 0x07, 0x13, 0x86,
+ 0x81, 0xca, 0xe3, 0x1f, 0xf6, 0xfc, 0x93, 0x06, 0xd0, 0x0f, 0x63, 0xe9, 0xe6, 0x1c, 0x05, 0x07,
+ 0x13, 0x77, 0xf7, 0x0f, 0x23, 0x8f, 0xe1, 0xc0, 0x93, 0x87, 0x41, 0xc0, 0x3e, 0xc0, 0x9d, 0x67,
+ 0x93, 0x87, 0x07, 0x53, 0x3a, 0xc4, 0x3e, 0xcc, 0x92, 0x47, 0x83, 0xc7, 0x07, 0x01, 0x63, 0x9c,
+ 0x07, 0x14, 0x92, 0x47, 0x03, 0xc7, 0x07, 0x00, 0x93, 0x07, 0xf0, 0x0f, 0x63, 0x15, 0xf7, 0x14,
+ 0x92, 0x47, 0x83, 0xc7, 0x87, 0x00, 0x63, 0x81, 0xe7, 0x02, 0x22, 0x47, 0x63, 0x6e, 0xf7, 0x00,
+ 0xb7, 0xf7, 0x00, 0xe0, 0x98, 0x47, 0x92, 0x47, 0xdc, 0x47, 0x1d, 0x8f, 0xb7, 0x27, 0xb7, 0x00,
+ 0x93, 0x87, 0xf7, 0xaf, 0x63, 0xf1, 0xe7, 0x12, 0x92, 0x46, 0x37, 0xf7, 0x00, 0xe0, 0x1c, 0x47,
+ 0xd4, 0x4a, 0x95, 0x8f, 0x63, 0xc9, 0x07, 0x10, 0xc2, 0x47, 0x85, 0x65, 0x93, 0x85, 0x85, 0xbb,
+ 0x03, 0xc5, 0x07, 0x00, 0x00, 0x47, 0x0d, 0x89, 0xef, 0xf0, 0x8f, 0x94, 0xe2, 0x47, 0x23, 0x07,
+ 0x01, 0x02, 0xa3, 0x07, 0x01, 0x02, 0x3e, 0x94, 0x92, 0x47, 0x22, 0x95, 0xc8, 0xcb, 0x02, 0x45,
+ 0xef, 0xf0, 0x4f, 0xd9, 0x63, 0x0b, 0x05, 0x0c, 0x02, 0x45, 0x93, 0x05, 0x20, 0x06, 0xef, 0xf0,
+ 0xcf, 0xfb, 0x85, 0x47, 0xaa, 0x84, 0x63, 0x12, 0xf5, 0x06, 0x02, 0x45, 0x93, 0x05, 0x00, 0x05,
+ 0xef, 0xf0, 0xaf, 0xfa, 0x2a, 0x84, 0x63, 0x1a, 0x95, 0x04, 0xa2, 0x45, 0x02, 0x45, 0xef, 0xf0,
+ 0xcf, 0xf9, 0xaa, 0x84, 0x63, 0x13, 0x85, 0x04, 0x82, 0x47, 0xb7, 0x16, 0x01, 0x40, 0x93, 0x86,
+ 0x06, 0x80, 0x03, 0xc4, 0x07, 0x00, 0x36, 0xce, 0x93, 0x77, 0xf4, 0x00, 0x11, 0x80, 0x2a, 0x04,
+ 0x36, 0x94, 0x3e, 0xc6, 0xb3, 0x17, 0xf5, 0x00, 0x1c, 0xc8, 0x79, 0x45, 0xef, 0xf0, 0xaf, 0x96,
+ 0x02, 0x45, 0xef, 0xf0, 0x0f, 0xd1, 0xf2, 0x46, 0x63, 0x1e, 0x05, 0x0c, 0x82, 0x47, 0x83, 0xc5,
+ 0x17, 0x00, 0x03, 0xc5, 0x07, 0x00, 0xef, 0xf0, 0xef, 0xdf, 0x81, 0x44, 0x01, 0x44, 0x02, 0x45,
+ 0xef, 0xf0, 0x4f, 0xda, 0xb9, 0xc8, 0x83, 0x47, 0xe1, 0x02, 0x12, 0x47, 0x23, 0x04, 0xf7, 0x00,
+ 0xb7, 0xf7, 0x00, 0xe0, 0x9c, 0x47, 0x5c, 0xc7, 0x29, 0xc0, 0x48, 0x18, 0x81, 0x44, 0x63, 0xc0,
+ 0x84, 0x1a, 0x02, 0x45, 0xef, 0xf0, 0x0f, 0xcf, 0x0d, 0xc9, 0x02, 0x45, 0x93, 0x05, 0x20, 0x06,
+ 0xef, 0xf0, 0xaf, 0xf1, 0x85, 0x47, 0xaa, 0x84, 0x63, 0x1e, 0xf5, 0x00, 0x02, 0x45, 0x93, 0x05,
+ 0xb0, 0x07, 0xef, 0xf0, 0x8f, 0xf0, 0x63, 0x17, 0x95, 0x00, 0x02, 0x45, 0x93, 0x75, 0xf4, 0x0f,
+ 0xef, 0xf0, 0xaf, 0xef, 0x02, 0x45, 0xef, 0xf0, 0xef, 0xd4, 0x02, 0x47, 0x85, 0x47, 0x23, 0x02,
+ 0xf7, 0x00, 0xa3, 0x01, 0xf7, 0x00, 0x92, 0x47, 0xe1, 0x07, 0x3e, 0xc2, 0x82, 0x47, 0x12, 0x47,
+ 0x95, 0x07, 0x3e, 0xc0, 0x93, 0x87, 0x81, 0xca, 0xe3, 0x98, 0xe7, 0xe8, 0xc2, 0x47, 0x85, 0x65,
+ 0x93, 0x85, 0x05, 0x77, 0x03, 0xc5, 0x17, 0x00, 0xb7, 0xf4, 0x00, 0xe0, 0x80, 0x44, 0x13, 0x05,
+ 0x85, 0x3e, 0xef, 0xf0, 0xef, 0x81, 0x03, 0xa7, 0x01, 0xc3, 0x19, 0x8c, 0xe3, 0x6c, 0xa4, 0xc0,
+ 0x98, 0x44, 0x09, 0x45, 0x23, 0xa8, 0xe1, 0xc2, 0xef, 0xf0, 0xef, 0x91, 0x21, 0xb1, 0x3a, 0x86,
+ 0x63, 0xf3, 0xe5, 0x00, 0x2e, 0x86, 0x13, 0x77, 0xf6, 0x0f, 0x0d, 0xb5, 0x7d, 0x57, 0x23, 0x8f,
+ 0xe1, 0xc0, 0xcd, 0xbe, 0x79, 0x45, 0x36, 0xce, 0xef, 0xf0, 0xef, 0x87, 0xb2, 0x47, 0x79, 0x45,
+ 0xc1, 0x07, 0xb3, 0x97, 0xf4, 0x00, 0x1c, 0xc8, 0xef, 0xf0, 0xef, 0x86, 0x82, 0x47, 0xf2, 0x46,
+ 0x79, 0x45, 0x83, 0xc7, 0x17, 0x00, 0x13, 0xd7, 0x47, 0x00, 0xbd, 0x8b, 0x2a, 0x07, 0xc1, 0x07,
+ 0x36, 0x97, 0xb3, 0x97, 0xf4, 0x00, 0x1c, 0xcb, 0xef, 0xf0, 0xef, 0x84, 0x02, 0x45, 0x93, 0x05,
+ 0x30, 0x06, 0xef, 0xf0, 0x8f, 0xe4, 0x2a, 0xc6, 0xe3, 0x19, 0x95, 0xee, 0x02, 0x45, 0x05, 0x46,
+ 0x93, 0x05, 0xe1, 0x02, 0xef, 0xf0, 0x4f, 0xe7, 0xe3, 0x01, 0x05, 0xee, 0x02, 0x45, 0x05, 0x46,
+ 0x93, 0x05, 0xf1, 0x02, 0xef, 0xf0, 0x4f, 0xe6, 0xe3, 0x09, 0x05, 0xec, 0x03, 0xc7, 0x71, 0xc3,
+ 0x83, 0x46, 0xf1, 0x02, 0x93, 0x07, 0x00, 0x02, 0x99, 0x8f, 0x21, 0x47, 0x63, 0x74, 0xd7, 0x00,
+ 0xa3, 0x07, 0xe1, 0x02, 0x03, 0x44, 0xf1, 0x02, 0x63, 0xd3, 0x87, 0x00, 0x3e, 0x84, 0xb2, 0x44,
+ 0x54, 0x18, 0x01, 0x47, 0x13, 0x03, 0xf4, 0xff, 0x63, 0x5a, 0x87, 0x04, 0xe3, 0x81, 0x04, 0xea,
+ 0x81, 0x47, 0x32, 0x46, 0x63, 0x16, 0xe3, 0x00, 0x13, 0x86, 0xc7, 0xff, 0x33, 0x36, 0xc0, 0x00,
+ 0x02, 0x45, 0x0c, 0x18, 0x1a, 0xd4, 0x36, 0xd2, 0x3e, 0xd0, 0x3a, 0xce, 0xef, 0xf0, 0xcf, 0xe0,
+ 0x82, 0x57, 0x91, 0x45, 0x03, 0x46, 0x01, 0x03, 0x72, 0x47, 0x92, 0x56, 0x22, 0x53, 0xaa, 0x84,
+ 0x63, 0x8b, 0xb7, 0x00, 0xb3, 0x85, 0xf6, 0x00, 0x23, 0x80, 0xc5, 0x00, 0x85, 0x07, 0x71, 0xf1,
+ 0x05, 0x07, 0x95, 0x06, 0x55, 0xbf, 0x23, 0x82, 0xc6, 0x00, 0xdd, 0xbf, 0xe3, 0x19, 0x04, 0xe4,
+ 0x02, 0x45, 0x01, 0x46, 0x0c, 0x18, 0xef, 0xf0, 0x2f, 0xdd, 0xaa, 0x84, 0x89, 0xb5, 0x2a, 0xc6,
+ 0xef, 0xe0, 0xdf, 0xfb, 0x32, 0x45, 0x85, 0x04, 0x15, 0x05, 0x91, 0xbd, 0x22, 0x45, 0xef, 0xf0,
+ 0x4f, 0x9a, 0x61, 0xba, 0x16, 0x03, 0x00, 0x00, 0x5e, 0x03, 0x00, 0x00, 0x2c, 0x03, 0x00, 0x00,
+ 0x2c, 0x03, 0x00, 0x00, 0x2c, 0x03, 0x00, 0x00, 0x2c, 0x03, 0x00, 0x00, 0x32, 0x03, 0x00, 0x00,
+ 0x3c, 0x03, 0x00, 0x00, 0x42, 0x03, 0x00, 0x00, 0x24, 0x32, 0x53, 0x00, 0x00, 0x21, 0x22, 0x45,
+ 0x00, 0x00, 0x34, 0x35, 0x57, 0x00, 0x00, 0x01, 0x02, 0x4e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
+};
diff --git a/firmware/Core/Src/main.c b/firmware/Core/Src/main.c
index dba08ea..0f7714e 100644
--- a/firmware/Core/Src/main.c
+++ b/firmware/Core/Src/main.c
@@ -23,6 +23,9 @@
/* USER CODE BEGIN Includes */
#include "app_entry.h"
#include "usb_device.h"
+#include "ch32v003_swio.h"
+#include "keybit_image.h"
+#include "keybit_bus.h"
/* USER CODE END Includes */
@@ -35,6 +38,9 @@
/* USER CODE BEGIN PD */
/* U14 (XL-1608RGBC) is common anode to +3.3V, so a pin driven LOW lights its colour. */
#define LED_STEP_MS 500
+#define KEYBIT_POLL_MS 250
+#define KEYBIT_SCAN_MS 250
+#define KEYBIT_KEYS_MS 5
/* USER CODE END PD */
@@ -51,6 +57,17 @@ QSPI_HandleTypeDef hqspi;
PCD_HandleTypeDef hpcd_USB_FS;
/* USER CODE BEGIN PV */
+static ch32_swio_t keybit_swio;
+/* 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
+ * events clears the bits it has handled. */
+keybit_info_t keybits[KEYBIT_MAX];
+int keybit_count;
+volatile uint32_t keybit_changed;
+/* Keybits further out, from events relayed by their neighbours. */
+keybit_remote_t keybit_remotes[KEYBIT_REMOTE_MAX];
+int keybit_remote_count;
/* USER CODE END PV */
@@ -76,6 +93,80 @@ static void LED_Set(uint8_t r, uint8_t g, uint8_t b)
HAL_GPIO_WritePin(STATUS_B_GPIO_Port, STATUS_B_Pin, b ? GPIO_PIN_RESET : GPIO_PIN_SET);
}
+/* Status LED, highest priority first:
+ * red Keybit on the dock failed to flash (until it's taken off)
+ * white blinking: a Keybit just connected on a side
+ * blue Keybit on the dock (flashed or up to date)
+ * green idle */
+static uint8_t dock_present, dock_failed;
+static uint32_t white_until;
+
+#define WHITE_BLINK_MS 1200
+
+static void LED_Update(void)
+{
+ uint32_t now = HAL_GetTick();
+ if (dock_failed) LED_Set(1, 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 LED_Set(0, 1, 0);
+}
+
+/* Keybit programming dock: CN7/CN8/CN9 on the keyword's back are 3V3, SWIO
+ * and GND, lined up with a Keybit's TP1/TP2/TP4. Poll for a Keybit there
+ * and flash keybit_image onto it once each time one is placed. */
+static void Keybit_Dock_Poll(void)
+{
+ static uint32_t last_poll;
+ static uint8_t misses;
+
+ if (HAL_GetTick() - last_poll < KEYBIT_POLL_MS) 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. */
+ if (dock_present && ++misses >= 3) {
+ dock_present = 0;
+ dock_failed = 0;
+ }
+ return;
+ }
+ misses = 0;
+ if (dock_present) return;
+ dock_present = 1;
+ LED_Update(); /* blue while flashing */
+
+ HAL_Delay(50); /* let it finish powering up off CN7 */
+ ch32_status_t r = ch32_halt(&keybit_swio, 1);
+ if (r == CH32_OK && ch32_verify(&keybit_swio, CH32_FLASH_BASE, keybit_image, keybit_image_len) == CH32_OK) {
+ ch32_reboot(&keybit_swio); /* already up to date */
+ return;
+ }
+ dock_failed = ch32_program(&keybit_swio, keybit_image, keybit_image_len) != CH32_OK;
+}
+
+/* Hand addresses to new Keybits, then refresh the list of who is there. */
+static void Keybit_Bus_Poll(void)
+{
+ static uint32_t last_scan, last_keys;
+ if (HAL_GetTick() - last_keys >= KEYBIT_KEYS_MS) {
+ last_keys = HAL_GetTick();
+ keybit_changed |= keybit_bus_poll_keys(&hi2c3, keybits, keybit_count);
+ keybit_bus_poll_relay(&hi2c3, keybits, keybit_count, keybit_remotes, &keybit_remote_count);
+ }
+ if (HAL_GetTick() - last_scan < KEYBIT_SCAN_MS) return;
+ last_scan = HAL_GetTick();
+
+ int fresh = keybit_bus_assign(&hi2c3);
+ int before = keybit_count;
+ keybit_count = keybit_bus_scan(&hi2c3, keybits, KEYBIT_MAX);
+ if (fresh > 0 || keybit_count > before) white_until = HAL_GetTick() + WHITE_BLINK_MS;
+}
+
/* USER CODE END 0 */
/**
@@ -115,13 +206,9 @@ int main(void)
MX_QUADSPI_Init();
MX_RF_Init();
/* USER CODE BEGIN 2 */
- /* red, yellow, green, cyan, blue, magenta, white, off */
- static const uint8_t colours[][3] = {
- {1, 0, 0}, {1, 1, 0}, {0, 1, 0}, {0, 1, 1},
- {0, 0, 1}, {1, 0, 1}, {1, 1, 1}, {0, 0, 0},
- };
- uint32_t step = 0;
- LED_Set(0, 0, 0);
+
+ ch32_init(&keybit_swio, BB_SWIO_GPIO_Port, BB_SWIO_Pin);
+ LED_Set(0, 1, 0); // greeen
/* USER CODE END 2 */
@@ -133,9 +220,10 @@ int main(void)
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
- const uint8_t *c = colours[step++ % (sizeof(colours) / sizeof(colours[0]))];
- LED_Set(c[0], c[1], c[2]);
- HAL_Delay(LED_STEP_MS);
+ Keybit_Dock_Poll();
+ Keybit_Bus_Poll();
+ LED_Update();
+
}
/* USER CODE END 3 */
}
diff --git a/firmware/Drivers/CH32V003/ch32v003_swio.c b/firmware/Drivers/CH32V003/ch32v003_swio.c
new file mode 100644
index 0000000..bb1b35c
--- /dev/null
+++ b/firmware/Drivers/CH32V003/ch32v003_swio.c
@@ -0,0 +1,519 @@
+/*
+ * CH32V003 SWIO programmer, see ch32v003_swio.h.
+ *
+ * Three layers:
+ * 1. PHY: one bit at a time on the wire (send_bit / read_bit).
+ * 2. Frames: one debug-module register read or write (ch32_*_reg).
+ * 3. Memory and flash: small RISC-V programs loaded into the debug
+ * module's program buffer that do loads and stores for us.
+ */
+#include "ch32v003_swio.h"
+#include <string.h>
+
+/* CH32V003 flash controller */
+#define CH_FLASH_KEYR 0x40022004u
+#define CH_FLASH_OBKEYR 0x40022008u
+#define CH_FLASH_STATR 0x4002200Cu
+#define CH_FLASH_CTLR 0x40022010u
+#define CH_FLASH_ADDR 0x40022014u
+#define CH_FLASH_MODEKEYR 0x40022024u
+#define CH_FLASH_KEY1 0x45670123u
+#define CH_FLASH_KEY2 0xCDEF89ABu
+
+#define STATR_BSY 0x00000001u
+#define STATR_WRPRTERR 0x00000010u
+#define CTLR_MER 0x00000004u
+#define CTLR_STRT 0x00000040u
+#define CTLR_LOCKS 0x00008080u /* LOCK | FLOCK */
+#define CTLR_PAGE_PG 0x00010000u
+#define CTLR_PAGE_ER 0x00020000u
+#define CTLR_BUF_LOAD 0x00040000u
+#define CTLR_BUF_RST 0x00080000u
+
+/* On the V003 DATA0/DATA1 are memory mapped here (from DMHARTINFO). */
+#define V003_DATA0_ADDR 0xE00000F4u
+#define V003_CHIP_ID 0x1FFFF704u
+
+/* Unlock key for DMSHDWCFGR/DMCFGR, plus bit 10 = let the target drive the
+ * line (needed for any read). */
+#define DMCFG_OUTPUT_EN (0x5AA50000u | (1u << 10))
+
+enum { PROG_NONE, PROG_READ, PROG_WRITE };
+
+#define PHY __attribute__((optimize("O2")))
+
+/* ------------------------------------------------------------------------ */
+/* Timing */
+
+static inline uint32_t cyc(void) { return DWT->CYCCNT; }
+
+static inline void wait_from(uint32_t start, uint32_t cycles)
+{
+ while ((cyc() - start) < cycles) { }
+}
+
+static void delay_us(uint32_t us)
+{
+ uint32_t start = cyc();
+ wait_from(start, us * (SystemCoreClock / 1000000u));
+}
+
+/* ------------------------------------------------------------------------ */
+/* PHY */
+
+/* A bit is a low pulse; its length is the value. Every bit ends driven
+ * high so the line doesn't depend on the slow pull-ups. */
+static inline PHY void send_bit(ch32_swio_t *s, int one)
+{
+ GPIO_TypeDef *p = s->port;
+ uint32_t t = cyc();
+ p->BRR = s->pin;
+ wait_from(t, one ? CH32_SWIO_T_SHORT : CH32_SWIO_T_LONG);
+ t = cyc();
+ p->BSRR = s->pin;
+ wait_from(t, CH32_SWIO_T_SHORT);
+}
+
+/* Pulse low briefly, let go, and see whether the target keeps it low.
+ * Low = 0, high = 1, -1 = target never let go. After releasing we give the
+ * line a quick push high ("glitch") so a 1 doesn't depend on the pull-up's
+ * slow rise; the target easily overpowers that if it is sending a 0. */
+static inline PHY int read_bit(ch32_swio_t *s, uint32_t timeout_cycles)
+{
+ GPIO_TypeDef *p = s->port;
+ const uint32_t half = CH32_SWIO_T_SHORT / 2;
+ uint32_t t = cyc();
+ p->BRR = s->pin;
+ wait_from(t, CH32_SWIO_T_SHORT);
+ p->MODER = s->moder_in;
+ p->BSRR = s->pin;
+ t = cyc();
+ wait_from(t, half);
+ p->MODER = s->moder_out;
+ p->MODER = s->moder_in;
+ wait_from(t, CH32_SWIO_T_SHORT);
+ int bit = (p->IDR & s->pin) != 0;
+
+ if (!bit) {
+ t = cyc();
+ wait_from(t, CH32_SWIO_T_SHORT * 2);
+ p->MODER = s->moder_out;
+ p->MODER = s->moder_in;
+ }
+
+ t = cyc();
+ while (!(p->IDR & s->pin)) {
+ if ((cyc() - t) > timeout_cycles) {
+ p->MODER = s->moder_out;
+ return -1;
+ }
+ }
+ p->MODER = s->moder_out;
+ t = cyc();
+ wait_from(t, half);
+ return bit;
+}
+
+/* ------------------------------------------------------------------------ */
+/* Frames: start bit, 7-bit address MSB first, R/W bit (1 = write), then 32
+ * data bits MSB first. No parity in SWIO mode. */
+
+void ch32_init(ch32_swio_t *s, GPIO_TypeDef *port, uint16_t pin)
+{
+ memset(s, 0, sizeof(*s));
+ s->port = port;
+ s->pin = pin;
+
+ CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
+ DWT->CYCCNT = 0;
+ DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
+
+ port->BSRR = pin;
+ GPIO_InitTypeDef g = {0};
+ g.Pin = pin;
+ g.Mode = GPIO_MODE_OUTPUT_PP;
+ g.Pull = GPIO_PULLUP;
+ g.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
+ HAL_GPIO_Init(port, &g);
+
+ uint32_t n = __builtin_ctz(pin);
+ s->moder_in = port->MODER & ~(3u << (2 * n));
+ s->moder_out = s->moder_in | (1u << (2 * n));
+}
+
+PHY void ch32_write_reg(ch32_swio_t *s, uint8_t reg, uint32_t value)
+{
+ uint32_t primask = __get_PRIMASK();
+ __disable_irq();
+ 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);
+
+ __set_PRIMASK(primask);
+ delay_us(CH32_SWIO_FRAME_GAP_US);
+}
+
+PHY ch32_status_t ch32_read_reg(ch32_swio_t *s, uint8_t reg, uint32_t *value)
+{
+ uint32_t timeout = CH32_SWIO_READ_TIMEOUT_US * (SystemCoreClock / 1000000u);
+ uint32_t v = 0;
+ ch32_status_t ret = CH32_OK;
+
+ uint32_t primask = __get_PRIMASK();
+ __disable_irq();
+ 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);
+ for (int i = 0; i < 32; i++) {
+ int b = read_bit(s, timeout);
+ if (b < 0) { ret = CH32_ERR_TIMEOUT; break; }
+ v = (v << 1) | (uint32_t)b;
+ }
+
+ __set_PRIMASK(primask);
+ delay_us(CH32_SWIO_FRAME_GAP_US);
+ if (ret == CH32_OK) *value = v;
+ return ret;
+}
+
+/* ------------------------------------------------------------------------ */
+/* Debug module control */
+
+static void forget_state(ch32_swio_t *s)
+{
+ s->prog_state = PROG_NONE;
+ s->write_is_flash = 0;
+ s->autoincrement = 0;
+ s->flash_unlocked = 0;
+ s->chip_known = 0;
+}
+
+ch32_status_t ch32_detect(ch32_swio_t *s)
+{
+ /* Turn on the target's output, then read the config register back. With
+ * nothing on the line every bit reads 1, so the 0x5AA5 key can't appear. */
+ for (int i = 0; i < 2; i++) {
+ ch32_write_reg(s, CH32_DMSHDWCFGR, DMCFG_OUTPUT_EN);
+ ch32_write_reg(s, CH32_DMCFGR, DMCFG_OUTPUT_EN);
+ }
+ ch32_write_reg(s, CH32_DMCONTROL, 0x00000001); /* dmactive */
+ ch32_write_reg(s, CH32_DMCONTROL, 0x00000001);
+
+ uint32_t v = 0;
+ if (ch32_read_reg(s, CH32_DMCFGR, &v) != CH32_OK) return CH32_ERR_NO_TARGET;
+ if ((v & 0xFFFF0000u) != 0x5AA50000u) return CH32_ERR_NO_TARGET;
+ return CH32_OK;
+}
+
+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 */
+ ch32_write_reg(s, CH32_DMCONTROL, 0x80000001);
+ 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) 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;
+}
+
+void ch32_reboot(ch32_swio_t *s)
+{
+ ch32_write_reg(s, CH32_DMCONTROL, 0x80000001);
+ ch32_write_reg(s, CH32_DMCONTROL, 0x80000001);
+ ch32_write_reg(s, CH32_DMCONTROL, 0x80000003); /* reset */
+ ch32_write_reg(s, CH32_DMCONTROL, 0x40000001); /* resumereq */
+ forget_state(s);
+ HAL_Delay(10);
+}
+
+/* Wait for an abstract command (or program buffer run) to finish. */
+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 */
+
+ if ((cs >> 8) & 7) { /* cmderr */
+ ch32_write_reg(s, CH32_DMABSTRACTCS, 0x00000700);
+ return CH32_ERR_ABSTRACT;
+ }
+ return CH32_OK;
+}
+
+/* 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)
+{
+ 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;
+ if ((0xE0000000u | (hart & 0x7FF)) != V003_DATA0_ADDR) return CH32_ERR_NOT_V003;
+
+ 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);
+
+ /* 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 (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;
+}
+
+ch32_status_t ch32_read_chip_id(ch32_swio_t *s, uint32_t *id)
+{
+ ch32_status_t r = ensure_chip(s);
+ if (r == CH32_OK) *id = s->chip_id;
+ return r;
+}
+
+/* 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)
+{
+ 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);
+}
+
+/* ------------------------------------------------------------------------ */
+/* Memory access. With autoexec on, every access to DATA0 reruns the
+ * program buffer, so sequential words cost one frame each. */
+
+ch32_status_t ch32_read_word(ch32_swio_t *s, uint32_t addr, 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;
+ }
+ if (s->autoincrement) s->next_addr += 4;
+
+ r = wait_done(s);
+ if (r != 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;
+}
+
+/* ------------------------------------------------------------------------ */
+/* Flash */
+
+static ch32_status_t wait_flash(ch32_swio_t *s)
+{
+ uint32_t st = 0;
+ int timeout = 0;
+ ch32_status_t r;
+ do {
+ r = ch32_read_word(s, CH_FLASH_STATR, &st);
+ if (r != CH32_OK) return r;
+ } while ((st & STATR_BSY) && timeout++ < 2000);
+
+ ch32_write_word(s, CH_FLASH_STATR, 0);
+ if (st & (STATR_WRPRTERR | STATR_BSY)) return CH32_ERR_FLASH;
+ return CH32_OK;
+}
+
+ch32_status_t ch32_unlock_flash(ch32_swio_t *s)
+{
+ uint32_t ctlr = 0;
+ ch32_status_t r = ch32_read_word(s, CH_FLASH_CTLR, &ctlr);
+ if (r != CH32_OK) return r;
+
+ if (ctlr & CTLR_LOCKS) {
+ ch32_write_word(s, CH_FLASH_KEYR, CH_FLASH_KEY1);
+ ch32_write_word(s, CH_FLASH_KEYR, CH_FLASH_KEY2);
+ ch32_write_word(s, CH_FLASH_OBKEYR, CH_FLASH_KEY1);
+ ch32_write_word(s, CH_FLASH_OBKEYR, CH_FLASH_KEY2);
+ ch32_write_word(s, CH_FLASH_MODEKEYR, CH_FLASH_KEY1); /* unlocks 64-byte fast ops */
+ ch32_write_word(s, CH_FLASH_MODEKEYR, CH_FLASH_KEY2);
+ r = ch32_read_word(s, CH_FLASH_CTLR, &ctlr);
+ if (r != CH32_OK) return r;
+ if (ctlr & CTLR_LOCKS) return CH32_ERR_LOCKED;
+ }
+ s->flash_unlocked = 1;
+ return CH32_OK;
+}
+
+ch32_status_t ch32_erase_page(ch32_swio_t *s, uint32_t addr)
+{
+ ch32_status_t r;
+ if (!s->flash_unlocked && (r = ch32_unlock_flash(s)) != CH32_OK) return r;
+ if ((r = wait_flash(s)) != CH32_OK) return r;
+ ch32_write_word(s, CH_FLASH_CTLR, CTLR_PAGE_ER);
+ ch32_write_word(s, CH_FLASH_ADDR, addr);
+ ch32_write_word(s, CH_FLASH_CTLR, CTLR_PAGE_ER | CTLR_STRT);
+ if ((r = wait_flash(s)) != CH32_OK) return r;
+ ch32_write_word(s, CH_FLASH_CTLR, 0);
+ return CH32_OK;
+}
+
+ch32_status_t ch32_erase_all(ch32_swio_t *s)
+{
+ ch32_status_t r;
+ if (!s->flash_unlocked && (r = ch32_unlock_flash(s)) != CH32_OK) return r;
+ ch32_write_word(s, CH_FLASH_CTLR, 0);
+ ch32_write_word(s, CH_FLASH_CTLR, CTLR_MER);
+ ch32_write_word(s, CH_FLASH_CTLR, CTLR_MER | CTLR_STRT);
+ if ((r = wait_flash(s)) != CH32_OK) return r;
+ ch32_write_word(s, CH_FLASH_CTLR, 0);
+ return CH32_OK;
+}
+
+/* Erase one 64-byte page, fill the page buffer word by word, then commit. */
+static ch32_status_t write_block(ch32_swio_t *s, uint32_t addr, const uint8_t *data, uint32_t len)
+{
+ ch32_status_t r = ch32_erase_page(s, addr);
+ if (r != CH32_OK) return r;
+
+ ch32_write_word(s, CH_FLASH_CTLR, CTLR_PAGE_PG);
+ ch32_write_word(s, CH_FLASH_CTLR, CTLR_PAGE_PG | CTLR_BUF_RST);
+ if ((r = wait_flash(s)) != CH32_OK) return r;
+
+ for (uint32_t i = 0; i < CH32_FLASH_BLOCK; i += 4) {
+ uint32_t w = 0xFFFFFFFFu;
+ if (i < len) memcpy(&w, data + i, (len - i) < 4 ? (len - i) : 4);
+ if ((r = ch32_write_word(s, addr + i, w)) != CH32_OK) return r;
+ }
+
+ ch32_write_word(s, CH_FLASH_ADDR, addr);
+ ch32_write_word(s, CH_FLASH_CTLR, CTLR_PAGE_PG | CTLR_STRT);
+ return wait_flash(s);
+}
+
+ch32_status_t ch32_write_flash(ch32_swio_t *s, uint32_t addr, const uint8_t *data, uint32_t len)
+{
+ if ((addr & (CH32_FLASH_BLOCK - 1)) || addr < CH32_FLASH_BASE ||
+ addr + len > CH32_FLASH_BASE + CH32_FLASH_SIZE)
+ return CH32_ERR_ARG;
+
+ ch32_status_t r = ensure_chip(s);
+ if (r != CH32_OK) return r;
+
+ for (uint32_t off = 0; off < len; off += CH32_FLASH_BLOCK) {
+ uint32_t n = len - off < CH32_FLASH_BLOCK ? len - off : CH32_FLASH_BLOCK;
+ if ((r = write_block(s, addr + off, data + off, n)) != CH32_OK) return r;
+ }
+ ch32_write_word(s, CH_FLASH_CTLR, 0);
+ return CH32_OK;
+}
+
+ch32_status_t ch32_verify(ch32_swio_t *s, uint32_t addr, const uint8_t *data, uint32_t len)
+{
+ for (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;
+ if (got != want) return CH32_ERR_VERIFY;
+ }
+ return CH32_OK;
+}
+
+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_reboot(s);
+ return CH32_OK;
+}
diff --git a/firmware/Drivers/CH32V003/ch32v003_swio.h b/firmware/Drivers/CH32V003/ch32v003_swio.h
new file mode 100644
index 0000000..0b221fa
--- /dev/null
+++ b/firmware/Drivers/CH32V003/ch32v003_swio.h
@@ -0,0 +1,126 @@
+/*
+ * CH32V003 single-wire debug (SWIO) programmer for the STM32WB.
+ *
+ * Bit-bangs WCH's one-wire debug protocol on one GPIO so the keyword can
+ * detect a Keybit sitting on its programming studs and flash it.
+ *
+ * The protocol and flash sequences are ported from cnlohr's ch32v003
+ * programmer (github.com/cnlohr/esp32s2-cookbook, ch32v003_swio.h, MIT) and
+ * minichlink (github.com/cnlohr/ch32v003fun, MIT).
+ */
+#ifndef CH32V003_SWIO_H
+#define CH32V003_SWIO_H
+
+#include <stdint.h>
+#include "stm32wbxx_hal.h"
+
+/* Pulse timing in CPU cycles. Defaults are for a 64 MHz HCLK: a short low
+ * is about 125 ns (a 1), a long low is about 500 ns (a 0). Check on a scope
+ * if transfers fail; the window that works is fairly narrow. */
+#ifndef CH32_SWIO_T_SHORT
+#define CH32_SWIO_T_SHORT 8
+#endif
+#ifndef CH32_SWIO_T_LONG
+#define CH32_SWIO_T_LONG (CH32_SWIO_T_SHORT * 4)
+#endif
+/* Idle time after each frame. 2 us is sometimes too short. */
+#ifndef CH32_SWIO_FRAME_GAP_US
+#define CH32_SWIO_FRAME_GAP_US 8
+#endif
+/* How long to wait for the target to let the line go high again. */
+#ifndef CH32_SWIO_READ_TIMEOUT_US
+#define CH32_SWIO_READ_TIMEOUT_US 10
+#endif
+
+#define CH32_FLASH_BASE 0x08000000u
+#define CH32_FLASH_SIZE (16u * 1024u)
+#define CH32_FLASH_BLOCK 64u
+
+/* RISC-V debug module registers (DMI addresses). */
+#define CH32_DMDATA0 0x04
+#define CH32_DMDATA1 0x05
+#define CH32_DMCONTROL 0x10
+#define CH32_DMSTATUS 0x11
+#define CH32_DMHARTINFO 0x12
+#define CH32_DMABSTRACTCS 0x16
+#define CH32_DMCOMMAND 0x17
+#define CH32_DMABSTRACTAUTO 0x18
+#define CH32_DMPROGBUF0 0x20
+#define CH32_DMPROGBUF1 0x21
+#define CH32_DMPROGBUF2 0x22
+#define CH32_DMPROGBUF3 0x23
+#define CH32_DMPROGBUF4 0x24
+#define CH32_DMPROGBUF5 0x25
+#define CH32_DMCPBR 0x7C
+#define CH32_DMCFGR 0x7D
+#define CH32_DMSHDWCFGR 0x7E
+
+typedef enum {
+ CH32_OK = 0,
+ CH32_ERR_NO_TARGET = -1, /* nothing answered on the line */
+ CH32_ERR_TIMEOUT = -2, /* target held the line low too long */
+ CH32_ERR_NOT_V003 = -3, /* answered, but not a CH32V003 */
+ CH32_ERR_BUSY = -4, /* debug module command never finished */
+ CH32_ERR_ABSTRACT = -5, /* debug module reported a command error */
+ CH32_ERR_LOCKED = -6, /* flash would not unlock */
+ CH32_ERR_FLASH = -7, /* flash stayed busy or write-protected */
+ CH32_ERR_VERIFY = -8, /* read-back did not match */
+ CH32_ERR_ARG = -9, /* bad address or length */
+} ch32_status_t;
+
+typedef struct {
+ GPIO_TypeDef *port;
+ uint16_t pin; /* HAL pin mask, e.g. GPIO_PIN_5 */
+
+ /* Internal: cached MODER values and debug-module program state. */
+ uint32_t moder_in;
+ uint32_t moder_out;
+ uint8_t prog_state;
+ uint8_t write_is_flash;
+ uint8_t autoincrement;
+ uint8_t flash_unlocked;
+ uint8_t chip_known;
+ uint32_t next_addr;
+ uint32_t chip_id;
+} ch32_swio_t;
+
+/* Set the pin up (push-pull, very high speed, pull-up, idling high) and
+ * start the DWT cycle counter. Call once after MX_GPIO_Init(). */
+void ch32_init(ch32_swio_t *s, GPIO_TypeDef *port, uint16_t pin);
+
+/* Raw debug-module register access, one SWIO frame each. */
+void ch32_write_reg(ch32_swio_t *s, uint8_t reg, uint32_t value);
+ch32_status_t ch32_read_reg(ch32_swio_t *s, uint8_t reg, uint32_t *value);
+
+/* CH32_OK when a CH32 answers on the line. Does not halt it. */
+ch32_status_t ch32_detect(ch32_swio_t *s);
+
+/* Halt the core, optionally resetting it first so user code can't run. */
+ch32_status_t ch32_halt(ch32_swio_t *s, int reset);
+/* Reset and let the new firmware run. */
+void ch32_reboot(ch32_swio_t *s);
+
+/* Read the chip ID word (0x1FFFF704 on the V003). Needs a halted core. */
+ch32_status_t ch32_read_chip_id(ch32_swio_t *s, uint32_t *id);
+
+/* Memory access through the debug module. Needs a halted core. */
+ch32_status_t ch32_read_word(ch32_swio_t *s, uint32_t addr, uint32_t *data);
+ch32_status_t ch32_write_word(ch32_swio_t *s, uint32_t addr, uint32_t data);
+
+ch32_status_t ch32_unlock_flash(ch32_swio_t *s);
+ch32_status_t ch32_erase_page(ch32_swio_t *s, uint32_t addr);
+ch32_status_t ch32_erase_all(ch32_swio_t *s);
+
+/* Erase and program len bytes at addr (64-byte aligned). Short final blocks
+ * are padded with 0xFF. */
+ch32_status_t ch32_write_flash(ch32_swio_t *s, uint32_t addr,
+ const uint8_t *data, uint32_t len);
+/* CH32_OK when flash at addr matches data. */
+ch32_status_t ch32_verify(ch32_swio_t *s, uint32_t addr,
+ const uint8_t *data, uint32_t len);
+
+/* Whole job: detect, halt with reset, flash image at 0x08000000, verify,
+ * reboot. */
+ch32_status_t ch32_program(ch32_swio_t *s, const uint8_t *image, uint32_t len);
+
+#endif /* CH32V003_SWIO_H */
diff --git a/firmware/README.md b/firmware/README.md
index 133af4a..4a69357 100644
--- a/firmware/README.md
+++ b/firmware/README.md
@@ -54,6 +54,22 @@ STM32_Programmer_CLI -c port=SWD -ob displ # check: nSWBOOT0 = 0x0, nBOOT0 = 0
The same can be done in the STM32CubeProgrammer GUI under Option bytes > User
Configuration. This only needs doing once; it survives reflashing and mass erase.
+## Keybit dock and side bus
+
+The keyword flashes Keybits itself. CN7/CN8/CN9 (3V3, SWIO, GND) on the
+back line up with a Keybit's TP1/TP2/TP4. `Drivers/CH32V003` bit-bangs
+WCH's one-wire debug protocol on PB5 (BB_SWIO) to detect a Keybit there,
+halt it, and write `Core/Src/keybit_image.c` (built from `keybit/`) into its
+flash. If the flash already matches, it just resets it.
+
+On I2C3 (the side pogo pins) `Core/Src/keybit_bus.c` hands addresses to new
+Keybits and lists the ones present, with UID, key state and which side they
+are on. See `keybit/README.md` for the protocol.
+
+Status LED: green idle, blue while a Keybit sits on the dock, blinking
+white when a Keybit connects on a side, red if flashing the docked Keybit
+failed.
+
## Bluetooth
The keyword shows up as a BLE keyboard called **Keyword**. It runs ST's
diff --git a/firmware/STM32-for-VSCode.config.yaml b/firmware/STM32-for-VSCode.config.yaml
index d2aaf04..5ca2e4b 100644
--- a/firmware/STM32-for-VSCode.config.yaml
+++ b/firmware/STM32-for-VSCode.config.yaml
@@ -80,6 +80,7 @@ includeDirectories:
- Src/**
- Core/Src/**
- Core/Lib/**
+ - Drivers/CH32V003
- STM32_WPAN/App
- Middlewares/ST/STM32_WPAN
- Middlewares/ST/STM32_WPAN/ble
@@ -109,6 +110,7 @@ sourceFiles:
- Src/**
- Core/Src/**
- Core/Lib/**
+ - Drivers/CH32V003/**
- STM32_WPAN/**
- Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_hal_rtc.c
- Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_hal_rtc_ex.c
diff --git a/firmware/STM32Make.make b/firmware/STM32Make.make
index c8fd494..8a5d836 100644
--- a/firmware/STM32Make.make
+++ b/firmware/STM32Make.make
@@ -75,12 +75,15 @@ endif
######################################
# C sources
C_SOURCES = \
+Core/Src/keybit_bus.c \
+Core/Src/keybit_image.c \
Core/Src/main.c \
Core/Src/stm32wbxx_hal_msp.c \
Core/Src/stm32wbxx_it.c \
Core/Src/syscalls.c \
Core/Src/sysmem.c \
Core/Src/system_stm32wbxx.c \
+Drivers/CH32V003/ch32v003_swio.c \
Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_hal.c \
Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_hal_cortex.c \
Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_hal_dma.c \
@@ -237,6 +240,7 @@ AS_INCLUDES = \
# C includes
C_INCLUDES = \
-ICore/Inc \
+-IDrivers/CH32V003 \
-IDrivers/CMSIS/Device/ST/STM32WBxx/Include \
-IDrivers/CMSIS/Include \
-IDrivers/STM32WBxx_HAL_Driver/Inc \
diff --git a/firmware/keybit/.gitignore b/firmware/keybit/.gitignore
new file mode 100644
index 0000000..6af2148
--- /dev/null
+++ b/firmware/keybit/.gitignore
@@ -0,0 +1,6 @@
+ch32v003fun/
+*.elf
+*.bin
+*.hex
+*.lst
+*.map
diff --git a/firmware/keybit/Makefile b/firmware/keybit/Makefile
new file mode 100644
index 0000000..e043f37
--- /dev/null
+++ b/firmware/keybit/Makefile
@@ -0,0 +1,23 @@
+all : build
+
+TARGET := main
+TARGET_MCU ?= CH32V003
+ADDITIONAL_C_FILES := soft_i2c.c
+
+# ch32v003fun is fetched on first build (pinned) unless you point CH32V003FUN
+# at your own checkout.
+CH32V003FUN ?= ch32v003fun
+CH32V003FUN_REV := a800cbd1f1ca5e250c73da83c669f0965d2052e5
+ifeq ($(wildcard $(CH32V003FUN)/ch32fun/ch32fun.mk),)
+$(info Fetching ch32v003fun into $(CH32V003FUN))
+$(shell git clone --quiet https://github.com/cnlohr/ch32v003fun.git $(CH32V003FUN) && git -C $(CH32V003FUN) checkout --quiet $(CH32V003FUN_REV))
+endif
+CH32FUN := $(CH32V003FUN)/ch32fun
+include $(CH32FUN)/ch32fun.mk
+
+# Rebuild the image the keyword embeds and flashes onto Keybits.
+image : $(TARGET).bin
+ python3 tools/bin2c.py $(TARGET).bin ../Core/Src/keybit_image.c main.c
+
+flash : cv_flash
+clean : cv_clean
diff --git a/firmware/keybit/README.md b/firmware/keybit/README.md
new file mode 100644
index 0000000..31d1280
--- /dev/null
+++ b/firmware/keybit/README.md
@@ -0,0 +1,102 @@
+# keybit firmware
+
+Firmware for the Keybit's CH32V003F4U6, built with
+[ch32v003fun](https://github.com/cnlohr/ch32v003fun). The keyword carries a
+copy (`../Core/Src/keybit_image.c`) and flashes it onto any Keybit placed on
+its programming studs, so normally you never flash a Keybit by hand.
+
+## Build
+
+Needs a RISC-V GCC (`brew install riscv64-elf-gcc`, or
+`gcc-riscv64-unknown-elf` on Ubuntu). The first build clones ch32v003fun
+into `ch32v003fun/` at a pinned commit.
+
+```sh
+cd firmware/keybit
+make # main.bin
+make image # regenerate ../Core/Src/keybit_image.c, then rebuild the keyword
+make flash # optional: flash directly with a WCH-LinkE
+```
+
+## What it does
+
+Each side of a Keybit has its own SDA/SCL pair, so all four are bit-banged
+I2C targets (`soft_i2c.c`); the main loop polls them for a START and serves
+one transaction at a time, clock-stretching where it needs time.
+
+| Bus | SDA | SCL | Side |
+|-----|-----|-----|------|
+| 1 | PC4 | PD2 | S |
+| 2 | PC1 | PC2 | E |
+| 3 | PD4 | PD5 | W |
+| 4 | PA1 | PA2 | N |
+
+The button is on PD6 (to GND, internal pull-up). PD1 stays SWIO so the
+keyword can always reflash it.
+
+## Addresses
+
+The keyword hands out I2C addresses:
+
+1. A Keybit with no address answers on **0x30**.
+2. The keyword reads the 96-bit UID at 0x10. If several new Keybits answer
+ at once, they arbitrate bit by bit (open-drain, a 0 wins) and the
+ keyword gets the lowest UID cleanly; the others drop out.
+3. The keyword writes that UID plus a free address to 0x20..0x2C. Only the
+ matching Keybit takes it and saves it in the last flash page
+ (0x08003FC0), so it keeps the address after power-off and reflashing.
+4. Repeat until nobody answers on 0x30.
+
+If two Keybits ever end up with the same address, the next UID read
+arbitrates between them and the loser goes back to 0x30 for a new one.
+
+## Registers
+
+Reads and writes auto-increment from the pointer written first.
+
+| Reg | Meaning |
+|-----|---------|
+| 0x00 | 0x4B magic |
+| 0x01 | firmware version |
+| 0x02-0x05 | UID bits 0..31 |
+| 0x06 | key state, bit 0 = pressed |
+| 0x07 | side this request came in on, `N`/`E`/`S`/`W` |
+| 0x08 | current address, 0 if none |
+| 0x10-0x1B | 96-bit UID, little endian |
+| 0x20-0x2B | ASSIGN: UID to match (write) |
+| 0x2C | ASSIGN: new address, 0 = forget. Writing this applies it. |
+| 0x50 | (write) depth of the Keybit polling us over a link |
+| 0x51 | our depth, 0xFF if we can't reach the keyword |
+| 0x52 | events ready, at most 8 |
+| 0x53-0x7A | up to 8 events of 5 bytes: UID bits 0..31, flags |
+| 0x7B | (write) n: drop the first n events |
+
+Event flags: bit 0 pressed, bit 1 heartbeat, bits 4-7 the origin Keybit's
+depth.
+
+## Relaying
+
+Only Keybits touching the keyword are on its bus. Every other joint is a
+private link between two Keybits, so key state hops inward along a tree:
+
+- **Depth.** A Keybit the keyword talks to has depth 1. Any other Keybit
+ takes one more than the shallowest neighbour that polls it. Depth only
+ comes from being polled, so when a Keybit is pulled out everything behind
+ it drops to "no depth" within 600 ms, with no loops.
+- **Polling.** A Keybit with a depth acts as I2C controller (address 0x31,
+ about 100 kHz) on each other side every ~5 ms, unless that neighbour polls
+ it or is no deeper. A poll writes our depth to 0x50, reads 0x51 onwards
+ (the child's depth, event count and up to 8 events), then writes 0x7B to
+ drop the ones we kept.
+- **Events.** Each Keybit queues its own key changes plus a heartbeat about
+ once a second, and forwards its children's. The keyword drains the
+ touching Keybits the same way (`keybit_bus_poll_relay`) into
+ `keybit_remotes[]`, and drops a remote Keybit after 3 s without a
+ heartbeat.
+- **Safety.** A side where any address other than 0x31 shows up is the
+ keyword's shared bus; a Keybit never drives that side as controller. It
+ also listens for 600 ms after power-up before polling anything.
+
+Latency is roughly 5 ms per hop plus retries when a poll lands while the
+neighbour is busy on another side. Remote Keybits are identified by the low
+32 bits of their UID.
diff --git a/firmware/keybit/funconfig.h b/firmware/keybit/funconfig.h
new file mode 100644
index 0000000..b8517c3
--- /dev/null
+++ b/firmware/keybit/funconfig.h
@@ -0,0 +1,9 @@
+#ifndef _FUNCONFIG_H
+#define _FUNCONFIG_H
+
+// Full list of options in ch32fun/ch32fun.h. Defaults: 48 MHz from the HSI.
+
+// No debug printf: it would keep polling the debug module over SWIO.
+#define FUNCONF_USE_DEBUGPRINTF 0
+
+#endif
diff --git a/firmware/keybit/main.c b/firmware/keybit/main.c
new file mode 100644
index 0000000..c18a7df
--- /dev/null
+++ b/firmware/keybit/main.c
@@ -0,0 +1,420 @@
+/*
+ * Keybit firmware (CH32V003F4U6).
+ *
+ * Each of the four side buses is its own bit-banged I2C pair:
+ *
+ * bus 1 SDA PC4 / SCL PD2 south
+ * bus 2 SDA PC1 / SCL PC2 east
+ * bus 3 SDA PD4 / SCL PD5 west
+ * bus 4 SDA PA1 / SCL PA2 north
+ *
+ * (Sides as seen in keybit.kicad_pcb.)
+ *
+ * Addressing: the keyword hands addresses out. A Keybit with no address
+ * answers on KEYBIT_DEFAULT_ADDR. The keyword reads its 96-bit UID there
+ * (several new Keybits answering at once arbitrate down to one UID), then
+ * writes that UID plus a free address to the ASSIGN registers. Only the
+ * Keybit whose UID matches takes the address, and it keeps it in the last
+ * flash page across power cycles. If two Keybits ever share an address,
+ * the one that loses arbitration on a UID read drops its address and asks
+ * again.
+ *
+ * Relaying: only Keybits touching the keyword are on its bus. The rest sit
+ * behind private two-Keybit links, so key state hops inward:
+ * - Every Keybit has a depth: 1 when the keyword talks to it directly,
+ * otherwise one more than the shallowest neighbour that polls it.
+ * Depth only comes from being polled, so when a Keybit is pulled out
+ * everything behind it goes quiet within FRESH_MS instead of looping.
+ * - A Keybit with a depth polls each other side as I2C controller (at
+ * LINK_ADDR) unless the neighbour there is no deeper than it is. Each
+ * poll tells the child our depth, reads the child's depth, and takes
+ * up to 8 queued events.
+ * - Events are (UID low 32 bits, flags): key changes and a heartbeat
+ * about once a second. Each Keybit queues its own and forwards its
+ * children's, so they drain towards the keyword.
+ * A side where we ever see an address that isn't LINK_ADDR is the
+ * keyword's shared bus, and we never act as controller there.
+ *
+ * Registers (reads and writes auto-increment from the pointer):
+ * 0x00 0x4B ('K') magic
+ * 0x01 firmware version
+ * 0x02-0x05 UID bits 0..31 (same as 0x10-0x13)
+ * 0x06 key state, bit 0 = pressed
+ * 0x07 side this request came in on: 'N', 'E', 'S' or 'W'
+ * 0x08 our address, 0 if none yet
+ * 0x10-0x1B 96-bit UID, little endian
+ * 0x20-0x2B ASSIGN: UID to match (write only)
+ * 0x2C ASSIGN: new address, 0 = forget it. Writing this applies it.
+ * 0x50 (write) depth of the Keybit polling us over a link
+ * 0x51 our depth, 0xFF if we can't reach the keyword yet
+ * 0x52 events ready to read, at most 8
+ * 0x53-0x7A up to 8 events, 5 bytes each: UID bits 0..31, then flags
+ * (bit 0 pressed, bit 1 heartbeat, bits 4-7 origin depth)
+ * 0x7B (write) n: drop the first n events, they were received
+ *
+ * PD1 stays SWIO so the keyword can always reflash us. Don't remap it.
+ */
+#include "ch32fun.h"
+#include "soft_i2c.h"
+#include <string.h>
+
+#define FW_VERSION 3
+#define KEYBIT_DEFAULT_ADDR 0x30
+#define LINK_ADDR 0x31 /* Keybit-to-Keybit links; the keyword never assigns it */
+#define BTN_PIN PD6 /* MX switch to GND */
+#define DEBOUNCE 200 /* main loop passes */
+
+#define DEPTH_NONE 0xFF
+#define FRESH_MS 600 /* a neighbour's depth counts this long */
+#define POLL_MS 5 /* child poll interval per side */
+#define PEER_RECHECK_MS 2000
+#define HEARTBEAT_MS 1000
+#define QUEUE_LEN 32
+#define BATCH 8
+
+/* Last 64-byte page. The SWIO flasher only rewrites pages the image uses,
+ * so this survives reflashing. */
+#define CONFIG_PAGE 0x08003FC0u
+#define CONFIG_MAGIC 0x4B42u
+
+#define EV_PRESSED 0x01
+#define EV_HEARTBEAT 0x02
+
+typedef struct {
+ uint16_t magic;
+ uint8_t addr;
+ uint8_t addr_inv;
+} config_t;
+
+typedef struct {
+ uint8_t uid[4];
+ uint8_t flags;
+} event_t;
+
+typedef struct {
+ /* Learned when the neighbour polls us: it's a parent (or a peer). Only
+ * this counts towards our depth, so a child can never root us and no
+ * loop can keep itself alive after the real path is gone. */
+ uint8_t parent_depth; /* DEPTH_NONE if it isn't polling us */
+ uint32_t parent_seen;
+ /* Learned when we poll the neighbour. */
+ uint8_t child_depth; /* DEPTH_NONE if unknown */
+ uint32_t child_seen;
+ uint8_t keyword; /* this side is the keyword's shared bus */
+ uint32_t next_poll; /* tick of our next controller poll here */
+} side_t;
+
+static soft_i2c_bus_t buses[4] = {
+ { .sda = PC4, .scl = PD2, .side = 'S' },
+ { .sda = PC1, .scl = PC2, .side = 'E' },
+ { .sda = PD4, .scl = PD5, .side = 'W' },
+ { .sda = PA1, .scl = PA2, .side = 'N' },
+};
+static side_t sides[4];
+
+static uint8_t uid[12];
+static volatile uint8_t key_down;
+static uint8_t my_addr; /* 0 = unassigned */
+static uint8_t my_depth = DEPTH_NONE;
+static uint8_t assign_buf[13];
+static int16_t save_addr = -1; /* address to write to flash, -1 = none */
+
+static event_t queue[QUEUE_LEN];
+static uint8_t q_head, q_count;
+
+/* ------------------------------------------------------------------------ */
+/* Time: SysTick runs freely; compare ticks with unsigned subtraction. */
+
+static inline uint32_t ticks(void) { return SysTick->CNT; }
+#define MS(n) ((uint32_t)(n) * DELAY_MS_TIME)
+static inline int elapsed(uint32_t since, uint32_t span) { return ticks() - since >= span; }
+
+static int side_index(soft_i2c_bus_t *bus) { return (int)(bus - buses); }
+
+/* ------------------------------------------------------------------------ */
+/* Event queue */
+
+static event_t *queue_at(int i) { return &queue[(q_head + i) % QUEUE_LEN]; }
+
+static void queue_pop(int n)
+{
+ if (n > q_count) n = q_count;
+ q_head = (uint8_t)((q_head + n) % QUEUE_LEN);
+ q_count = (uint8_t)(q_count - n);
+}
+
+/* When full, the oldest event goes; heartbeats repair anything lost. */
+static void queue_push(const event_t *e)
+{
+ if (q_count == QUEUE_LEN) queue_pop(1);
+ *queue_at(q_count) = *e;
+ q_count++;
+}
+
+static void queue_own(uint8_t flags)
+{
+ event_t e;
+ memcpy(e.uid, uid, 4);
+ uint8_t d = my_depth > 15 ? 15 : my_depth;
+ e.flags = (uint8_t)(flags | (key_down ? EV_PRESSED : 0) | (d << 4));
+ queue_push(&e);
+}
+
+/* ------------------------------------------------------------------------ */
+/* Address storage */
+
+static void load_config(void)
+{
+ const config_t *c = (const config_t *)CONFIG_PAGE;
+ if (c->magic == CONFIG_MAGIC && (uint8_t)~c->addr == c->addr_inv &&
+ c->addr >= 0x08 && c->addr <= 0x77 &&
+ c->addr != KEYBIT_DEFAULT_ADDR && c->addr != LINK_ADDR)
+ my_addr = c->addr;
+}
+
+/* Erase the config page and write it back with addr. About 6 ms, so it runs
+ * from the main loop after the I2C transaction, not inside it. */
+static void store_config(uint8_t addr)
+{
+ FLASH->KEYR = FLASH_KEY1;
+ FLASH->KEYR = FLASH_KEY2;
+ FLASH->MODEKEYR = FLASH_KEY1;
+ FLASH->MODEKEYR = FLASH_KEY2;
+ if (FLASH->CTLR & 0x8080) return; /* still locked */
+
+ FLASH->CTLR = CR_PAGE_ER;
+ FLASH->ADDR = CONFIG_PAGE;
+ FLASH->CTLR = CR_STRT_Set | CR_PAGE_ER;
+ while (FLASH->STATR & FLASH_STATR_BSY) { }
+
+ if (addr) {
+ config_t c = { CONFIG_MAGIC, addr, (uint8_t)~addr };
+ uint32_t words[16];
+ memset(words, 0xFF, sizeof(words));
+ memcpy(words, &c, sizeof(c));
+
+ volatile uint32_t *dst = (volatile uint32_t *)CONFIG_PAGE;
+ FLASH->CTLR = CR_PAGE_PG;
+ FLASH->CTLR = CR_BUF_RST | CR_PAGE_PG;
+ FLASH->ADDR = CONFIG_PAGE;
+ while (FLASH->STATR & FLASH_STATR_BSY) { }
+ for (int i = 0; i < 16; i++) {
+ dst[i] = words[i];
+ FLASH->CTLR = CR_PAGE_PG | FLASH_CTLR_BUF_LOAD;
+ while (FLASH->STATR & FLASH_STATR_BSY) { }
+ }
+ FLASH->CTLR = CR_PAGE_PG | CR_STRT_Set;
+ while (FLASH->STATR & FLASH_STATR_BSY) { }
+ }
+ FLASH->CTLR = CR_LOCK_Set;
+}
+
+/* ------------------------------------------------------------------------ */
+/* I2C target callbacks */
+
+static void polled_by(int s, uint8_t depth)
+{
+ sides[s].parent_depth = depth;
+ sides[s].parent_seen = ticks();
+}
+
+static int match_addr(uint8_t addr, soft_i2c_bus_t *bus)
+{
+ int s = side_index(bus);
+ if (addr == LINK_ADDR) return !sides[s].keyword;
+
+ /* Anything else only happens on the keyword's bus. */
+ sides[s].keyword = 1;
+ int mine = my_addr ? addr == my_addr : addr == KEYBIT_DEFAULT_ADDR;
+ if (mine) polled_by(s, 0);
+ return mine;
+}
+
+static uint8_t read_reg(uint8_t reg, soft_i2c_bus_t *bus)
+{
+ if (reg >= 0x02 && reg <= 0x05) return uid[reg - 0x02];
+ if (reg >= 0x10 && reg <= 0x1B) return uid[reg - 0x10];
+ if (reg >= 0x53 && reg < 0x53 + BATCH * 5) {
+ int i = (reg - 0x53) / 5, b = (reg - 0x53) % 5;
+ if (i >= q_count) return 0xFF;
+ const event_t *e = queue_at(i);
+ return b < 4 ? e->uid[b] : e->flags;
+ }
+ switch (reg) {
+ case 0x00: return 0x4B;
+ case 0x01: return FW_VERSION;
+ case 0x06: return key_down;
+ case 0x07: return (uint8_t)bus->side;
+ case 0x08: return my_addr;
+ case 0x51: return my_depth;
+ case 0x52: return q_count < BATCH ? q_count : BATCH;
+ default: return 0xFF;
+ }
+}
+
+static void write_reg(uint8_t reg, uint8_t value, soft_i2c_bus_t *bus)
+{
+ if (reg == 0x50) { polled_by(side_index(bus), value); return; }
+ if (reg == 0x7B) { queue_pop(value); return; }
+ if (reg < 0x20 || reg > 0x2C) return;
+ assign_buf[reg - 0x20] = value;
+ if (reg != 0x2C) return;
+
+ uint8_t addr = assign_buf[12];
+ if (memcmp(assign_buf, uid, 12) != 0) return; /* meant for another Keybit */
+ if (addr && (addr < 0x08 || addr > 0x77 || addr == KEYBIT_DEFAULT_ADDR || addr == LINK_ADDR)) return;
+ my_addr = addr;
+ save_addr = addr;
+}
+
+/* Another Keybit answered on our address with a lower UID: we're the
+ * duplicate, so go back to the default address and get a new one. */
+static void lost_arbitration(uint8_t addr)
+{
+ if (my_addr && addr == my_addr) {
+ my_addr = 0;
+ save_addr = 0;
+ }
+}
+
+static const soft_i2c_target_t target = {
+ .match = match_addr,
+ .read = read_reg,
+ .write = write_reg,
+ .lost = lost_arbitration,
+};
+
+/* ------------------------------------------------------------------------ */
+/* Relay */
+
+static void update_depth(void)
+{
+ uint8_t best = DEPTH_NONE;
+ for (int s = 0; s < 4; s++) {
+ side_t *sd = &sides[s];
+ if (sd->parent_depth == DEPTH_NONE) continue;
+ if (elapsed(sd->parent_seen, MS(FRESH_MS))) { sd->parent_depth = DEPTH_NONE; continue; }
+ if (sd->parent_depth < best) best = sd->parent_depth;
+ }
+ my_depth = best == DEPTH_NONE || best >= 0xFE ? DEPTH_NONE : (uint8_t)(best + 1);
+}
+
+/* Poll the Keybit on side s: tell it our depth, read its depth and up to
+ * BATCH of its events, then tell it how many we kept. */
+static void poll_child(int s)
+{
+ soft_i2c_bus_t *b = &buses[s];
+ uint8_t depth = 0, count = 0;
+ event_t got[BATCH];
+ int n = 0, ok = 0;
+
+ if (!soft_i2c_start(b)) return;
+ if (soft_i2c_write(b, LINK_ADDR << 1) == 1 && soft_i2c_write(b, 0x50) == 1 &&
+ soft_i2c_write(b, my_depth) == 1 && soft_i2c_restart(b) &&
+ soft_i2c_write(b, (LINK_ADDR << 1) | 1) == 1 &&
+ soft_i2c_read(b, &depth, 1) && soft_i2c_read(b, &count, 1)) {
+ int room = QUEUE_LEN - q_count;
+ if (count > BATCH) count = BATCH;
+ n = count < room ? count : room;
+ ok = 1;
+ for (int i = 0; i < n && ok; i++)
+ for (int j = 0; j < 5 && ok; j++) {
+ uint8_t v;
+ ok = soft_i2c_read(b, &v, !(i == n - 1 && j == 4));
+ if (j < 4) got[i].uid[j] = v; else got[i].flags = v;
+ }
+ if (n == 0) { uint8_t dummy; ok = soft_i2c_read(b, &dummy, 0); }
+ }
+ soft_i2c_stop(b);
+ if (!ok) return;
+
+ sides[s].child_depth = depth;
+ sides[s].child_seen = ticks();
+ if (n == 0) return;
+ for (int i = 0; i < n; i++) queue_push(&got[i]);
+
+ if (soft_i2c_start(b)) {
+ if (soft_i2c_write(b, LINK_ADDR << 1) == 1 && soft_i2c_write(b, 0x7B) == 1)
+ soft_i2c_write(b, (uint8_t)n);
+ soft_i2c_stop(b);
+ }
+}
+
+static void relay(void)
+{
+ static uint32_t boot;
+ static uint8_t started;
+ if (!started) { boot = ticks(); started = 1; }
+ /* Listen first so the keyword's bus is recognised before we ever drive
+ * a side as controller. */
+ if (!elapsed(boot, MS(FRESH_MS))) return;
+
+ update_depth();
+ if (my_depth == DEPTH_NONE) return;
+
+ for (int s = 0; s < 4; s++) {
+ side_t *sd = &sides[s];
+ if (sd->keyword) continue;
+ if (sd->parent_depth != DEPTH_NONE) continue; /* it polls us */
+ /* A peer at the same depth or shallower isn't our child; look again
+ * now and then in case that changed. */
+ if (sd->child_depth != DEPTH_NONE && sd->child_depth <= my_depth &&
+ !elapsed(sd->child_seen, MS(PEER_RECHECK_MS))) continue;
+ if ((int32_t)(ticks() - sd->next_poll) < 0) continue;
+ /* A little UID-based jitter so two Keybits don't keep polling each
+ * other in lockstep. */
+ sd->next_poll = ticks() + MS(POLL_MS) + (uid[0] & 3) * DELAY_MS_TIME / 2;
+ poll_child(s);
+ buses[s].prev_sda = buses[s].prev_scl = 1;
+ }
+}
+
+/* ------------------------------------------------------------------------ */
+
+/* Counts towards the pin's level and only flips once it gets all the way. */
+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 void heartbeat(void)
+{
+ static uint32_t last;
+ if (my_depth == DEPTH_NONE || !elapsed(last, MS(HEARTBEAT_MS + uid[1]))) return;
+ last = ticks();
+ queue_own(EV_HEARTBEAT);
+}
+
+int main(void)
+{
+ SystemInit();
+ funGpioInitAll();
+
+ uint32_t ids[3] = { ESIG->UNIID1, ESIG->UNIID2, ESIG->UNIID3 };
+ memcpy(uid, ids, sizeof(uid));
+ load_config();
+ for (int s = 0; s < 4; s++) sides[s].parent_depth = sides[s].child_depth = DEPTH_NONE;
+
+ funPinMode(BTN_PIN, GPIO_CFGLR_IN_PUPD);
+ funDigitalWrite(BTN_PIN, FUN_HIGH); /* pull-up */
+
+ for (int i = 0; i < 4; i++) soft_i2c_init(&buses[i]);
+
+ for (;;) {
+ for (int i = 0; i < 4; i++) soft_i2c_poll(&buses[i], &target);
+ debounce_key();
+ relay();
+ heartbeat();
+ if (save_addr >= 0) {
+ store_config((uint8_t)save_addr);
+ save_addr = -1;
+ }
+ }
+}
diff --git a/firmware/keybit/soft_i2c.c b/firmware/keybit/soft_i2c.c
new file mode 100644
index 0000000..3c10086
--- /dev/null
+++ b/firmware/keybit/soft_i2c.c
@@ -0,0 +1,253 @@
+#include "ch32fun.h"
+#include "soft_i2c.h"
+
+/* Give up on a transaction if a line sits still this long (a few ms at
+ * 48 MHz), e.g. when the neighbour is pulled away mid-transfer. */
+#define TIMEOUT_LOOPS 100000
+
+enum { EV_BYTE, EV_START, EV_STOP, EV_TIMEOUT };
+
+#define SDA(b) funDigitalRead((b)->sda)
+#define SCL(b) funDigitalRead((b)->scl)
+#define SDA_LOW(b) funDigitalWrite((b)->sda, FUN_LOW)
+#define SDA_REL(b) funDigitalWrite((b)->sda, FUN_HIGH)
+#define SCL_LOW(b) funDigitalWrite((b)->scl, FUN_LOW)
+#define SCL_REL(b) funDigitalWrite((b)->scl, FUN_HIGH)
+
+void soft_i2c_init(soft_i2c_bus_t *bus)
+{
+ funDigitalWrite(bus->sda, FUN_HIGH);
+ funDigitalWrite(bus->scl, FUN_HIGH);
+ funPinMode(bus->sda, GPIO_CFGLR_OUT_10Mhz_OD);
+ funPinMode(bus->scl, GPIO_CFGLR_OUT_10Mhz_OD);
+ bus->prev_sda = 1;
+ bus->prev_scl = 1;
+}
+
+static int wait_scl(soft_i2c_bus_t *b, int level)
+{
+ for (int i = 0; i < TIMEOUT_LOOPS; i++)
+ if (SCL(b) == level) return 1;
+ return 0;
+}
+
+/* Clock in 8 bits. While SCL is high SDA must hold still; if it moves, the
+ * controller sent a START (falling) or STOP (rising) instead of data. */
+static int recv_byte(soft_i2c_bus_t *b, uint8_t *out)
+{
+ uint8_t v = 0;
+ for (int bit = 0; bit < 8; bit++) {
+ if (!wait_scl(b, 0) || !wait_scl(b, 1)) return EV_TIMEOUT;
+ int d = SDA(b);
+ int i = 0;
+ while (SCL(b)) {
+ int now = SDA(b);
+ if (now != d) return now ? EV_STOP : EV_START;
+ if (++i > TIMEOUT_LOOPS) return EV_TIMEOUT;
+ }
+ v = (uint8_t)((v << 1) | d);
+ }
+ *out = v;
+ return EV_BYTE;
+}
+
+/* Hold SDA low through the 9th clock. Called with SCL low. */
+static int send_ack(soft_i2c_bus_t *b)
+{
+ SDA_LOW(b);
+ int ok = wait_scl(b, 1) && wait_scl(b, 0);
+ SDA_REL(b);
+ return ok;
+}
+
+/* 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
+ * 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; }
+ 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 (one && !SDA(b)) return -1; /* someone else is sending a 0 */
+ }
+ if (!wait_scl(b, 0)) { SDA_REL(b); return 0; }
+ SDA_REL(b);
+ if (!wait_scl(b, 1)) return 0;
+ int ack = !SDA(b);
+ wait_scl(b, 0);
+ return ack;
+}
+
+/* Sit out the rest of a transaction that isn't for us. */
+static void 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 */
+ }
+}
+
+static uint8_t reg_ptr;
+
+static void serve(soft_i2c_bus_t *b, const soft_i2c_target_t *t)
+{
+ for (;;) { /* loops on repeated START */
+ uint8_t a;
+ int ev = recv_byte(b, &a);
+ if (ev == EV_START) continue;
+ if (ev != EV_BYTE) return;
+ uint8_t addr = a >> 1;
+ if (!t->match(addr, b)) { wait_bus_free(b); return; }
+ if (!send_ack(b)) return;
+
+ if (a & 1) { /* controller reads */
+ int r;
+ while ((r = send_byte(b, t->read(reg_ptr++, b))) == 1) { }
+ if (r < 0) {
+ if (t->lost) t->lost(addr);
+ wait_bus_free(b);
+ return;
+ }
+ /* NACK ends the read; next is STOP or a repeated START */
+ ev = recv_byte(b, &a);
+ if (ev == EV_START) continue;
+ return;
+ }
+
+ int first = 1;
+ for (;;) {
+ uint8_t d;
+ ev = recv_byte(b, &d);
+ if (ev != EV_BYTE) break;
+ if (first) { reg_ptr = d; first = 0; }
+ else t->write(reg_ptr++, d, b);
+ if (!send_ack(b)) return;
+ }
+ if (ev == EV_START) continue;
+ return;
+ }
+}
+
+int soft_i2c_poll(soft_i2c_bus_t *b, const soft_i2c_target_t *t)
+{
+ int sda = SDA(b), scl = SCL(b);
+ int start = b->prev_scl && scl && b->prev_sda && !sda;
+ b->prev_sda = sda;
+ b->prev_scl = scl;
+ if (!start) return 0;
+
+ serve(b, t);
+ b->prev_sda = SDA(b);
+ b->prev_scl = SCL(b);
+ return 1;
+}
+
+/* ------------------------------------------------------------------------ */
+/* Controller */
+
+/* Half a clock period. Keybit links run at about 100 kHz so the far side's
+ * polling loop can keep up. */
+#define HALF_US 5
+/* Hold START this long so a target busy elsewhere still sees it. */
+#define START_US 20
+
+static void release_bus(soft_i2c_bus_t *b)
+{
+ SDA_REL(b);
+ SCL_REL(b);
+}
+
+/* Let SCL go and wait for it, honouring clock stretching. */
+static int scl_high(soft_i2c_bus_t *b)
+{
+ SCL_REL(b);
+ return wait_scl(b, 1);
+}
+
+int soft_i2c_start(soft_i2c_bus_t *b)
+{
+ if (!SDA(b) || !SCL(b)) return 0;
+ Delay_Us(HALF_US);
+ if (!SDA(b) || !SCL(b)) return 0; /* someone else started */
+ SDA_LOW(b);
+ Delay_Us(START_US);
+ SCL_LOW(b);
+ Delay_Us(HALF_US);
+ return 1;
+}
+
+int soft_i2c_restart(soft_i2c_bus_t *b)
+{
+ SDA_REL(b);
+ Delay_Us(HALF_US);
+ if (!scl_high(b)) { release_bus(b); return 0; }
+ Delay_Us(HALF_US);
+ SDA_LOW(b);
+ Delay_Us(HALF_US);
+ SCL_LOW(b);
+ Delay_Us(HALF_US);
+ return 1;
+}
+
+void soft_i2c_stop(soft_i2c_bus_t *b)
+{
+ SDA_LOW(b);
+ Delay_Us(HALF_US);
+ scl_high(b);
+ Delay_Us(HALF_US);
+ SDA_REL(b);
+ Delay_Us(HALF_US);
+}
+
+/* One bit out with SCL low on entry and exit. */
+static int put_bit(soft_i2c_bus_t *b, int one)
+{
+ if (one) SDA_REL(b); else SDA_LOW(b);
+ Delay_Us(HALF_US);
+ if (!scl_high(b)) { release_bus(b); return 0; }
+ if (one && !SDA(b)) { release_bus(b); return -1; } /* lost arbitration */
+ Delay_Us(HALF_US);
+ SCL_LOW(b);
+ return 1;
+}
+
+static int get_bit(soft_i2c_bus_t *b, int *bit)
+{
+ SDA_REL(b);
+ Delay_Us(HALF_US);
+ if (!scl_high(b)) { release_bus(b); return 0; }
+ *bit = SDA(b);
+ Delay_Us(HALF_US);
+ SCL_LOW(b);
+ return 1;
+}
+
+int soft_i2c_write(soft_i2c_bus_t *b, uint8_t v)
+{
+ for (int i = 7; i >= 0; i--) {
+ int r = put_bit(b, (v >> i) & 1);
+ if (r != 1) return r;
+ }
+ int nack;
+ if (!get_bit(b, &nack)) return 0;
+ return !nack;
+}
+
+int soft_i2c_read(soft_i2c_bus_t *b, uint8_t *v, int ack)
+{
+ uint8_t x = 0;
+ for (int i = 0; i < 8; i++) {
+ int bit;
+ if (!get_bit(b, &bit)) return 0;
+ x = (uint8_t)((x << 1) | bit);
+ }
+ *v = x;
+ return put_bit(b, !ack) == 1;
+}
diff --git a/firmware/keybit/soft_i2c.h b/firmware/keybit/soft_i2c.h
new file mode 100644
index 0000000..b8b5ce9
--- /dev/null
+++ b/firmware/keybit/soft_i2c.h
@@ -0,0 +1,57 @@
+/*
+ * Bit-banged I2C target for the Keybit's four side buses.
+ *
+ * The CH32V003 has one hardware I2C, but every side of a Keybit has its own
+ * SDA/SCL pair, so all four are done in software. The main loop polls each
+ * bus for a START, then serves that whole transaction before going back to
+ * polling.
+ *
+ * Reads arbitrate like a multi-controller bus: while sending a 1 we check
+ * that SDA really is high. If another target sharing the address pulled it
+ * low, we lost, stop driving, and the transfer carries on with the winner's
+ * bytes. That is what lets several unaddressed Keybits answer one read of
+ * their UIDs at once.
+ */
+#ifndef SOFT_I2C_H
+#define SOFT_I2C_H
+
+#include <stdint.h>
+
+typedef struct {
+ uint8_t sda, scl; /* ch32fun pin numbers, e.g. PC4 */
+ char side; /* 'N', 'E', 'S' or 'W' */
+ uint8_t prev_sda, prev_scl;
+} soft_i2c_bus_t;
+
+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 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);
+} soft_i2c_target_t;
+
+/* Make both pins open-drain and released (high through the 10k pull-ups). */
+void soft_i2c_init(soft_i2c_bus_t *bus);
+
+/* Look once for a START. If there is one, serve the transaction and return
+ * 1. The first byte of a write sets the register pointer; reads and later
+ * writes auto-increment it. */
+int soft_i2c_poll(soft_i2c_bus_t *bus, const soft_i2c_target_t *t);
+
+/* Controller side, for polling the Keybit on the far side of a link.
+ * Every call returns 1 on success / ACK, 0 on NACK or timeout, and -1 if
+ * another controller won arbitration (then the bus is already released).
+ * Start fails (0) if the bus isn't idle. */
+int soft_i2c_start(soft_i2c_bus_t *bus);
+int soft_i2c_restart(soft_i2c_bus_t *bus);
+void soft_i2c_stop(soft_i2c_bus_t *bus);
+int soft_i2c_write(soft_i2c_bus_t *bus, uint8_t v);
+/* Reads one byte; ack = 1 to ask for more, 0 on the last byte. */
+int soft_i2c_read(soft_i2c_bus_t *bus, uint8_t *v, int ack);
+
+#endif
diff --git a/firmware/keybit/tools/bin2c.py b/firmware/keybit/tools/bin2c.py
new file mode 100644
index 0000000..5d83c00
--- /dev/null
+++ b/firmware/keybit/tools/bin2c.py
@@ -0,0 +1,25 @@
+#!/usr/bin/env python3
+"""Turn keybit.bin into a C file the keyword firmware links in."""
+import re
+import sys
+
+src, dst, main_c = sys.argv[1], sys.argv[2], sys.argv[3]
+data = open(src, "rb").read()
+version = int(re.search(r"#define\s+FW_VERSION\s+(\d+)", open(main_c).read()).group(1))
+
+# The last 64-byte page holds the Keybit's assigned I2C address.
+if len(data) > 16 * 1024 - 64:
+ sys.exit(f"keybit image is {len(data)} bytes and would overwrite the config page")
+
+lines = []
+for i in range(0, len(data), 16):
+ lines.append(" " + ", ".join(f"0x{b:02x}" for b in data[i:i + 16]) + ",")
+
+open(dst, "w").write(
+ "/* Generated from firmware/keybit by `make image` there. Do not edit. */\n"
+ '#include "keybit_image.h"\n\n'
+ f"const uint32_t keybit_image_len = {len(data)};\n"
+ f"const uint8_t keybit_image_version = {version};\n"
+ "const uint8_t keybit_image[] __attribute__((aligned(4))) = {\n"
+ + "\n".join(lines) + "\n};\n"
+)