Skip to content

A truly modular keyboard

C

keyword: keep the side scan inside the watchdog, RST? reports the reset cause

Nathan119ea4d3ff83
diff --git a/firmware/keyword/Core/Inc/main.h b/firmware/keyword/Core/Inc/main.h
index d7ea058..46e1f3f 100644
--- a/firmware/keyword/Core/Inc/main.h
+++ b/firmware/keyword/Core/Inc/main.h
@@ -58,6 +58,7 @@ int Keybit_Dock_Reads(int n);
int Keybit_Dock_Score(int n, uint32_t *bw, uint32_t *br, int *bad, int *hr);
int Keybit_Dock_Regs(uint32_t *v, uint32_t delay_ms, int halt, int x, int *hr);
extern uint32_t keybit_dock_pause_until;
+extern uint32_t keyword_reset_csr; /* RCC->CSR at boot, before the reset flags were cleared */
void Keybit_Dock_Id(char *out, size_t len, int reset);
/* USER CODE END EFP */
diff --git a/firmware/keyword/Core/Src/keybit_bus.c b/firmware/keyword/Core/Src/keybit_bus.c
index 3cb279e..a2c4a43 100644
--- a/firmware/keyword/Core/Src/keybit_bus.c
+++ b/firmware/keyword/Core/Src/keybit_bus.c
@@ -79,7 +79,9 @@ int keybit_bus_jammed(void)
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;
+ /* Just the address byte (~0.5 ms at 20 kHz): a short timeout, or a
+ * misbehaving bus makes a full scan outlast the bootloader's ~8 s watchdog. */
+ return HAL_I2C_IsDeviceReady(i2c, (uint16_t)(addr << 1), 1, 3) == HAL_OK;
}
static int read_regs(I2C_HandleTypeDef *i2c, uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
@@ -134,6 +136,7 @@ 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++) {
+ IWDG->KR = 0xAAAA;
if (a == KEYBIT_DEFAULT_ADDR || a == LINK_ADDR || a == CHARGER_ADDR) continue;
if (!is_ready(i2c, a)) continue;
diff --git a/firmware/keyword/Core/Src/main.c b/firmware/keyword/Core/Src/main.c
index 4b11e2b..40cc043 100644
--- a/firmware/keyword/Core/Src/main.c
+++ b/firmware/keyword/Core/Src/main.c
@@ -63,7 +63,8 @@ PCD_HandleTypeDef hpcd_USB_FS;
/* USER CODE BEGIN PV */
static ch32_swio_t keybit_swio;
-uint32_t keybit_dock_pause_until; /* HAL tick; the dock poll stays off the line until then */
+uint32_t keybit_dock_pause_until;
+uint32_t keyword_reset_csr; /* HAL tick; the dock poll stays off the line until then */
/* 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
@@ -468,7 +469,7 @@ int main(void)
{
/* USER CODE BEGIN 1 */
-
+ keyword_reset_csr = RCC->CSR; /* why we last reset, for HELLO */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
@@ -487,6 +488,7 @@ int main(void)
PeriphCommonClock_Config();
/* USER CODE BEGIN SysInit */
+ RCC->CSR |= RCC_CSR_RMVF; /* after MX_APPE_Config has read them, so the next reset reports only its own cause */
/* USER CODE END SysInit */
diff --git a/firmware/keyword/Core/Src/proto.c b/firmware/keyword/Core/Src/proto.c
index 5ae1e76..5913d43 100644
--- a/firmware/keyword/Core/Src/proto.c
+++ b/firmware/keyword/Core/Src/proto.c
@@ -355,6 +355,15 @@ static void Handle_Line(proto_port_t *port, char *cmd)
for (size_t o = 0, l = strlen(buf); o < l || o == 0; o += 100)
Reply(port, "DOCKID%s %.100s", o ? "+" : "", buf + o);
}
+ else if (strcmp(cmd, "RST?") == 0)
+ {
+ /* Why the keyword last reset (RCC->CSR at boot). Several flags can be set. */
+ uint32_t c = keyword_reset_csr;
+ Reply(port, "RST csr=%08lx%s%s%s%s%s%s", (unsigned long)c, (c & RCC_CSR_IWDGRSTF) ? " iwdg" : "",
+ (c & RCC_CSR_WWDGRSTF) ? " wwdg" : "", (c & RCC_CSR_BORRSTF) ? " bor" : "",
+ (c & RCC_CSR_PINRSTF) ? " pin" : "", (c & RCC_CSR_SFTRSTF) ? " sw" : "",
+ (c & RCC_CSR_LPWRRSTF) ? " lpwr" : "");
+ }
else if (strcmp(cmd, "I2CSCAN") == 0)
{
/* Every 7-bit address that ACKs on the side bus, and the idle line levels. */