A truly modular keyboard
C
keyword: keep the keymap in a log so SAVE works with Bluetooth connected
Nathanbd260a104ae3
diff --git a/firmware/keyword/Core/Src/keymap.c b/firmware/keyword/Core/Src/keymap.cindex 6737f16..1a0a3db 100644--- a/firmware/keyword/Core/Src/keymap.c+++ b/firmware/keyword/Core/Src/keymap.c@@ -68,6 +68,10 @@ const keybit_info_t *KEYMAP_Present(int *count)/* One erase or program, done when neither CPU is blocking flash access:* ST's sequence from BLE_RfWithFlash's flash_driver.c. CPU2 was told to* use semaphore 7 in APP_BLE_Init. Returns 0 when it ran. */+/* Why the last save failed (step 1 erase, 2 program, 3 read back, 4 the+ * page isn't the keymap's), how many tries it had, semaphore 7 then. */+uint32_t keymap_save_why[3];+static int Flash_Op(uint8_t erase, uint32_t addr_or_page, uint64_t data){for (int tries = 0; tries < 1000; tries++)@@ -99,43 +103,127 @@ static int Flash_Op(uint8_t erase, uint32_t addr_or_page, uint64_t data)}HAL_Delay(1); /* CPU2 is in a radio event, try again */}+ keymap_save_why[0] = erase ? 1 : 2;+ keymap_save_why[1] = 1000;+ keymap_save_why[2] = HSEM->R[CFG_HW_BLOCK_FLASH_REQ_BY_CPU2_SEMID]; /* R, not RLR: reading RLR locks it */return -1;}-int KEYMAP_Save(void)+/* The keymap page is a log: each SAVE writes the whole map after the last+ * one (header doubleword, entries, then MAP_COMMIT once all of it is in),+ * and loading takes the newest one with its commit. Writes are single+ * doublewords, which fit between CPU2's radio events; a page erase takes+ * ~22 ms and CPU2 never leaves a gap that long while a Bluetooth link is+ * up (it held semaphore 7 the whole time), so it only happens when the+ * page is full, in the background, once nobody is connected. */+#define MAP_COMMIT 0x54494D4D4F43504BULL /* "KPCOMMIT" */+#define PAGE_END(base) ((base) + FLASH_PAGE_SIZE)++static uint8_t erase_due; /* the page is full: erase it, then write the map */++/* The newest committed map in the page, and where the next one goes (0+ * if the page holds something we can't follow and needs erasing). */+static const map_image_t *Log_Scan(uint32_t *next){- keytable.magic = MAP_MAGIC;- keytable.version = MAP_VERSION;- uint32_t size = Map_Size(&keytable);- if (memcmp(Map_Flash(), &keytable, size) == 0) return 0; /* nothing changed */+ uint32_t base = (uint32_t)Map_Flash(), pos = base;+ const map_image_t *best = NULL;+ *next = 0;+ while (pos + 8 <= PAGE_END(base))+ {+ const map_image_t *m = (const map_image_t *)pos;+ if (*(const uint64_t *)pos == UINT64_MAX) { *next = pos; break; } /* erased: the end */+ if (m->magic != MAP_MAGIC || m->count > KEYMAP_MAX) break;+ uint32_t end = pos + Map_Size(m);+ if (end + 8 > PAGE_END(base)) break;+ if (m->version == MAP_VERSION && *(const uint64_t *)end == MAP_COMMIT) best = m;+ pos = end + 8; /* a map cut off before its commit is skipped, not reused */+ }+ return best;+}+/* Program the map at addr, commit last. */+static int Log_Write(uint32_t addr)+{+ uint32_t size = Map_Size(&keytable);+ const uint64_t *src = (const uint64_t *)&keytable;int err = 0;+ for (uint32_t i = 0; i < size / 8 && !err; i++) err |= Flash_Op(0, addr + 8 * i, src[i]);+ if (!err) err |= Flash_Op(0, addr + size, MAP_COMMIT);+ if (!err && memcmp((const void *)addr, &keytable, size) != 0) { keymap_save_why[0] = 3; err = -1; }+ return err;+}++static void Flash_Begin(void)+{while (LL_HSEM_1StepLock(HSEM, CFG_HW_FLASH_SEMID));HAL_FLASH_Unlock();__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);+}- /* Without the BLE stack running there is nobody to answer SHCI. */- if (APP_BLE_Ready()) SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_ON);- err |= Flash_Op(1, Map_Page(), 0);- if (APP_BLE_Ready()) SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_OFF);+static void Flash_End(void)+{+ HAL_FLASH_Lock();+ LL_HSEM_ReleaseLock(HSEM, CFG_HW_FLASH_SEMID, 0);+}- uint32_t addr = (uint32_t)Map_Flash();- const uint64_t *src = (const uint64_t *)&keytable;- for (uint32_t i = 0; i < size / 8 && !err; i++)+/* 0 saved, 1 queued until the page can be erased, -1 failed. */+int KEYMAP_Save(void)+{+ keytable.magic = MAP_MAGIC;+ keytable.version = MAP_VERSION;+ uint32_t size = Map_Size(&keytable), next;+ /* Only ever the page just under CPU2's, far above the app. */+ if (Map_Page() < 0x80 || Map_Page() >= 0xFF) { keymap_save_why[0] = 4; return -1; }++ const map_image_t *last = Log_Scan(&next);+ if (last && memcmp(last, &keytable, size) == 0) { erase_due = 0; return 0; } /* nothing changed */+ if (next && next + size + 8 <= PAGE_END((uint32_t)Map_Flash())){- err |= Flash_Op(0, addr + 8 * i, src[i]);+ Flash_Begin();+ int err = Log_Write(next);+ Flash_End();+ if (!err) erase_due = 0;+ return err ? -1 : 0;}+ erase_due = 1; /* KEYMAP_Service finishes it */+ return 1;+}- HAL_FLASH_Lock();- LL_HSEM_ReleaseLock(HSEM, CFG_HW_FLASH_SEMID, 0);- if (err || memcmp(Map_Flash(), &keytable, size) != 0) return -1;- return 0;+/* From the keys task: once the page is due an erase and nobody is+ * connected over Bluetooth, stop advertising, erase, write the map. With+ * a link up while USB does the typing, drop the link: the computer comes+ * back by itself. */+static void Keymap_Service(void)+{+ static uint32_t last;+ if (!erase_due || HAL_GetTick() - last < 1000) return;+ last = HAL_GetTick();+ if (APP_BLE_ConnHandle() != APP_BLE_NO_CONN)+ {+ extern USBD_HandleTypeDef hUsbDeviceFS;+ if (USBD_KW_Ready(&hUsbDeviceFS)) APP_BLE_Drop();+ return;+ }+ int paused = APP_BLE_PauseAdv();+ Flash_Begin();+ if (APP_BLE_Ready()) SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_ON);+ int err = Flash_Op(1, Map_Page(), 0);+ if (APP_BLE_Ready()) SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_OFF);+ if (!err) err = Log_Write((uint32_t)Map_Flash());+ Flash_End();+ if (paused) APP_BLE_ResumeAdv();+ if (!err)+ {+ erase_due = 0;+ PROTO_Send("OK SAVE (page erased)");+ }}static void Map_Load(void){- const map_image_t *f = Map_Flash();- if (f->magic == MAP_MAGIC && f->version == MAP_VERSION && f->count <= KEYMAP_MAX)+ uint32_t next;+ const map_image_t *f = Log_Scan(&next);+ if (f){memcpy(&keytable, f, Map_Size(f));}@@ -366,6 +454,7 @@ static void Remote_Keys(void)static void Keys_Task(void){keybit_changed = 0; /* only main-loop code sets it, no ISR */+ Keymap_Service();Direct_Keys();Remote_Keys();diff --git a/firmware/keyword/Core/Src/proto.c b/firmware/keyword/Core/Src/proto.cindex 5f978c7..3b0cf24 100644--- a/firmware/keyword/Core/Src/proto.c+++ b/firmware/keyword/Core/Src/proto.c@@ -603,7 +603,12 @@ static void Handle_Line(proto_port_t *port, char *cmd)Reply(port, "ERR no BLE stack yet, keymap not saved");return;}- if (KEYMAP_Save() != 0) Reply(port, "ERR flash write failed");+ extern uint32_t keymap_save_why[3];+ int r = KEYMAP_Save();+ if (r > 0) Reply(port, "OK SAVE queued, the page needs erasing first");+ else if (r < 0)+ Reply(port, "ERR flash write failed step=%lu tries=%lu sem7=%08lx", (unsigned long)keymap_save_why[0],+ (unsigned long)keymap_save_why[1], (unsigned long)keymap_save_why[2]);else Reply(port, "OK SAVE");}elsediff --git a/firmware/keyword/STM32_WPAN/App/app_ble.c b/firmware/keyword/STM32_WPAN/App/app_ble.cindex 2de749c..63207af 100644--- a/firmware/keyword/STM32_WPAN/App/app_ble.c+++ b/firmware/keyword/STM32_WPAN/App/app_ble.c@@ -301,6 +301,27 @@ static void Adv_Start(uint8_t fast)if (fast) HW_TS_Start(adv_timer_id, FAST_ADV_TIMEOUT);}+/* Keymap SAVE: CPU2 holds flash for its radio timing while it advertises,+ * leaving no gap for a page erase. Stop advertising for one, if nobody is+ * connected; start again after. */+int APP_BLE_PauseAdv(void)+{+ if (!APP_BLE_Ready() || APP_BLE_ConnHandle() != APP_BLE_NO_CONN) return 0;+ HW_TS_Stop(adv_timer_id);+ aci_gap_set_non_discoverable();+ return 1;+}++void APP_BLE_ResumeAdv(void)+{+ Adv_Start(0);+}++void APP_BLE_Drop(void)+{+ if (APP_BLE_ConnHandle() != APP_BLE_NO_CONN) aci_gap_terminate(APP_BLE_ConnHandle(), 0x13);+}+void hci_notify_asynch_evt(void *p_Data){UNUSED(p_Data);diff --git a/firmware/keyword/STM32_WPAN/App/app_ble.h b/firmware/keyword/STM32_WPAN/App/app_ble.hindex a52a987..7d9f8da 100644--- a/firmware/keyword/STM32_WPAN/App/app_ble.h+++ b/firmware/keyword/STM32_WPAN/App/app_ble.h@@ -15,6 +15,14 @@ void APP_BLE_Init(void);/* Handle of the current link, APP_BLE_NO_CONN when nobody is connected. */uint16_t APP_BLE_ConnHandle(void);+/* Stop advertising for a flash page erase (1 if it did: nobody connected),+ * and start again. */+int APP_BLE_PauseAdv(void);+void APP_BLE_ResumeAdv(void);++/* Drop the Bluetooth link, if any (the computer reconnects by itself). */+void APP_BLE_Drop(void);+/* 1 once the CPU2 stack is up and the services exist. */uint8_t APP_BLE_Ready(void);