A truly modular keyboard
C
keyword: side bus at 20 kHz, clock a stuck bus free, I2CSPEED command
Nathanf933efe05fdf
diff --git a/firmware/keyword/Core/Inc/keybit_bus.h b/firmware/keyword/Core/Inc/keybit_bus.hindex 7c3391c..5fdc013 100644--- a/firmware/keyword/Core/Inc/keybit_bus.h+++ b/firmware/keyword/Core/Inc/keybit_bus.h@@ -46,6 +46,11 @@ typedef struct {int keybit_bus_held(void);int keybit_bus_jammed(void);+/* SCL low with the bus idle for over 5 ms means something stopped+ * mid-transfer and is waiting on a clock. Clock it free: I2C3 off, 9 SCL+ * pulses and a STOP by hand, I2C3 back on. Returns 1 when it did that. */+int keybit_bus_unstick(I2C_HandleTypeDef *i2c);+/* Give every Keybit still on the default address its own address. Returns* how many were assigned. */int keybit_bus_assign(I2C_HandleTypeDef *i2c);diff --git a/firmware/keyword/Core/Src/keybit_bus.c b/firmware/keyword/Core/Src/keybit_bus.cindex fbef2fb..4f4fa9c 100644--- a/firmware/keyword/Core/Src/keybit_bus.c+++ b/firmware/keyword/Core/Src/keybit_bus.c@@ -31,6 +31,38 @@ int keybit_bus_held(void)HAL_GPIO_ReadPin(BUS_PORT, BUS_SCL) == GPIO_PIN_SET;}+int keybit_bus_unstick(I2C_HandleTypeDef *i2c)+{+ static uint32_t low_since;+ uint32_t now = HAL_GetTick();+ if (HAL_GPIO_ReadPin(BUS_PORT, BUS_SCL) == GPIO_PIN_SET || HAL_I2C_GetState(i2c) != HAL_I2C_STATE_READY) {+ low_since = now;+ return 0;+ }+ if (now - low_since < 5) return 0;++ HAL_I2C_DeInit(i2c);+ GPIO_InitTypeDef g = {0};+ g.Pin = BUS_SCL | BUS_SDA;+ g.Mode = GPIO_MODE_OUTPUT_OD;+ g.Pull = GPIO_NOPULL;+ g.Speed = GPIO_SPEED_FREQ_LOW;+ HAL_GPIO_WritePin(BUS_PORT, BUS_SCL | BUS_SDA, GPIO_PIN_SET);+ HAL_GPIO_Init(BUS_PORT, &g);+ for (int i = 0; i < 9; i++) { /* 9 clocks finish any byte and its ACK */+ HAL_GPIO_WritePin(BUS_PORT, BUS_SCL, GPIO_PIN_RESET);+ HAL_Delay(1);+ HAL_GPIO_WritePin(BUS_PORT, BUS_SCL, GPIO_PIN_SET);+ HAL_Delay(1);+ }+ HAL_GPIO_WritePin(BUS_PORT, BUS_SDA, GPIO_PIN_RESET); /* STOP: SDA rises while SCL is high */+ HAL_Delay(1);+ HAL_GPIO_WritePin(BUS_PORT, BUS_SDA, GPIO_PIN_SET);+ HAL_I2C_Init(i2c); /* MspInit puts the pins back on I2C3 */+ low_since = HAL_GetTick();+ return 1;+}+int keybit_bus_jammed(void){static uint32_t last_try;diff --git a/firmware/keyword/Core/Src/main.c b/firmware/keyword/Core/Src/main.cindex 8b550ba..ea64cae 100644--- a/firmware/keyword/Core/Src/main.c+++ b/firmware/keyword/Core/Src/main.c@@ -417,6 +417,7 @@ static void Keybit_Bus_Poll(void)bus_held = 1;PROTO_Send("ERR bus SDA held low, a Keybit on the Keyword may be turned the wrong way");}+ if (keybit_bus_unstick(&hi2c3)) PROTO_Send("OK BUS recovered");if (keybit_bus_jammed()) return;if (HAL_GetTick() - last_keys >= KEYBIT_KEYS_MS) {last_keys = HAL_GetTick();@@ -597,7 +598,7 @@ static void MX_I2C3_Init(void)/* USER CODE END I2C3_Init 1 */hi2c3.Instance = I2C3;- hi2c3.Init.Timing = 0x10B17DB5;+ hi2c3.Init.Timing = 0xF0426363; /* ~20 kHz: Keybits run their I2C target in software and miss bits at 100 kHz */hi2c3.Init.OwnAddress1 = 0;hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;diff --git a/firmware/keyword/Core/Src/proto.c b/firmware/keyword/Core/Src/proto.cindex 44257e0..5ae1e76 100644--- a/firmware/keyword/Core/Src/proto.c+++ b/firmware/keyword/Core/Src/proto.c@@ -367,6 +367,25 @@ static void Handle_Line(proto_port_t *port, char *cmd)Reply(port, "I2CSCAN sda=%d scl=%d acks:%s", HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_14) == GPIO_PIN_SET,HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_13) == GPIO_PIN_SET, n ? list : " none");}+ else if (strcmp(cmd, "I2CSPEED") == 0)+ {+ /* I2CSPEED <khz>: retime the side bus. I2C3 runs off PCLK1 (64 MHz);+ * with PRESC=15 one tick is 0.25 us, so SCL low and high max out at+ * 256 ticks each and the slowest it goes is about 7.8 kHz. */+ unsigned khz = (unsigned)atoi(args);+ if (khz < 1 || khz > 400)+ Reply(port, "ERR usage: I2CSPEED <khz 1-400>");+ else {+ uint32_t half = 2000u / khz; /* ticks per half period */+ if (half > 256) half = 256;+ if (half < 2) half = 2;+ uint32_t t = (15u << 28) | (4u << 20) | (2u << 16) | ((half - 1) << 8) | (half - 1);+ __HAL_I2C_DISABLE(&hi2c3);+ hi2c3.Instance->TIMINGR = t;+ __HAL_I2C_ENABLE(&hi2c3);+ Reply(port, "I2CSPEED timing=%08lx ~%lu Hz", (unsigned long)t, (unsigned long)(2000000u / half));+ }+ }else if (strcmp(cmd, "I2CRD") == 0){/* I2CRD <addr hex> <reg hex> <n>: raw register read on the side bus. */