Skip to content

Commit 4236168

Browse files
authored
Merge pull request #1598 from UltimateHackingKeyboard/reclaim_memory
Reclaim memory
2 parents c85dea8 + 35a3dbc commit 4236168

9 files changed

Lines changed: 250 additions & 150 deletions

File tree

CLAUDE.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,23 @@ In `key_action.h`:
9494
## Code Review Philosophy
9595
9696
Focus on functional aspects, not nitpicks. Only flag magic constants if they're used in multiple places and may need future changes. Don't require comments unless truly necessary.
97+
98+
## UHK80 / Zephyr Dev Environment — Findings
99+
100+
Key gotchas discovered while setting up:
101+
102+
- **Re-run `./build.sh update` after any branch/manifest switch.** The west modules (zephyr, nrf, …)
103+
are pinned per `west_nrfsdk.yml`; if they don't match the current checkout, CMake configure fails
104+
with `Error finding board: uhk-80-right ... Malformed "build" section ... SchemaError`. This is a
105+
Zephyr version mismatch, not a `board.yml` bug — `./build.sh update` (west update + patch) fixes it.
106+
- **Single-device builds run in the foreground** (`./build.sh right build`), which is easiest for
107+
testing; multi-device (`left right dongle`) fans out into tmux `buildsession` panes.
108+
- **Dongle link depends on `keyboard/` sources.** `right/src/slave_drivers/kboot_driver.c` is compiled
109+
for all UHK80 targets, but `device/src/CMakeLists.txt` only compiles `keyboard/*.c` (which includes
110+
`uart_modules.c`) for left/right, **not** the dongle. So any symbol `kboot_driver.c` pulls from
111+
`uart_modules.c` (e.g. the `KbootUart_*` UART-transport functions) must be stubbed/guarded for the
112+
dongle or the dongle link fails with `undefined reference`.
113+
- **Agent npm shadow.** `lib/agent` is nested inside this repo; the firmware root's `node_modules/npm`
114+
(11.8.0, pinned by the root lockfile) shadows nvm's npm during the Agent build and trips its
115+
`check-node-version` (>=11.13.0). Fix: `npm install npm@11.13.0 --no-save --no-package-lock` in the
116+
firmware root. Details in the reference doc above.

device/prj.conf.overlays/nrf_shared.conf

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ CONFIG_INIT_STACKS=y
88
CONFIG_LOG=y
99
CONFIG_LOG_CMDS=y
1010
CONFIG_LOG_MODE_DEFERRED=y
11-
CONFIG_LOG_BUFFER_SIZE=2048
1211
CONFIG_LOG_BACKEND_SHOW_COLOR=y
1312
CONFIG_LOG_RUNTIME_FILTERING=y
1413

@@ -38,19 +37,31 @@ CONFIG_BT_FILTER_ACCEPT_LIST=y
3837
CONFIG_BT_CTLR_SDC_MAX_CONN_EVENT_LEN_DEFAULT=2500
3938
CONFIG_BT_CTLR_SDC_CENTRAL_ACL_EVENT_SPACING_DEFAULT=2500
4039

41-
# increase these to make multiple connections more reliable
42-
# this is a generic ai advice.
43-
CONFIG_BT_ATT_TX_COUNT=10
44-
CONFIG_BT_CONN_TX_MAX=6
45-
CONFIG_BT_L2CAP_TX_BUF_COUNT=12
46-
47-
# negotiate larger MTU for NUS
40+
# BLE buffer counts, sized for ~2 active links (L=2) with double buffering
41+
# (D=2), no HCI fragmentation (LL unit == L2CAP MTU, so frags/PDU F=1). Each
42+
# line notes the constraint it must satisfy. BT_BUF_ACL_TX_COUNT (the HCI TX
43+
# credit window) is left at its default 3: it is capped by the controller's
44+
# BT_CTLR_SDC_TX_PACKET_COUNT and 3 credits already cover 2 links.
45+
# ATT TX PDUs in flight: >= L*D (freed on HCI handoff, not air-ack)
46+
CONFIG_BT_ATT_TX_COUNT=5
47+
# L2CAP signalling/SMP only (no CoC): a handful
48+
CONFIG_BT_L2CAP_TX_BUF_COUNT=4
49+
# HCI event RX: > BT_BUF_ACL_TX_COUNT (build assert)
50+
CONFIG_BT_BUF_EVT_RX_COUNT=6
51+
# ACL RX reassembly: 1+EXTRA >= L+1 (one/link + driver spare)
52+
CONFIG_BT_BUF_ACL_RX_COUNT_EXTRA=2
53+
54+
# NUS packet sizing. We cap the link packet at MAX_LINK_PACKET_LENGTH=128
55+
# (link_protocol.h) rather than the DLE maximum, to save RAM and cut BLE latency.
56+
# ACL buffer = MAX_LINK_PACKET_LENGTH + L2CAP(4) + ATT(3) = 135
57+
# L2CAP MTU = MAX_LINK_PACKET_LENGTH + ATT(3) = 131
58+
# bt_conn.c caps the negotiated DLE tx length to CONFIG_BT_BUF_ACL_TX_SIZE.
4859
CONFIG_BT_USER_DATA_LEN_UPDATE=y
49-
CONFIG_BT_BUF_ACL_RX_SIZE=251
50-
CONFIG_BT_BUF_ACL_TX_SIZE=251
51-
CONFIG_BT_L2CAP_TX_MTU=247
60+
CONFIG_BT_BUF_ACL_RX_SIZE=135
61+
CONFIG_BT_BUF_ACL_TX_SIZE=135
62+
CONFIG_BT_L2CAP_TX_MTU=131
5263

53-
# allow BLE buffering
64+
# inbound long-write (config transfer) queue depth
5465
CONFIG_BT_ATT_PREPARE_COUNT=4
5566

5667
# store BLE link keys in flash
@@ -61,9 +72,12 @@ CONFIG_FLASH_MAP=y
6172
CONFIG_SETTINGS=y
6273
CONFIG_NVS=y
6374

64-
# prevent dropped messages
65-
CONFIG_LOG_BUFFER_SIZE=4096
66-
CONFIG_SEGGER_RTT_BUFFER_SIZE_UP=4096
75+
# Log/RTT buffers kept small to save RAM (we are tight on RAM). If these small
76+
# buffers drop messages under bursty logging, raise the logging thread priority
77+
# at runtime with the "uhk log priority high" shell command rather than enlarging
78+
# the buffers.
79+
CONFIG_LOG_BUFFER_SIZE=1024
80+
CONFIG_SEGGER_RTT_BUFFER_SIZE_UP=512
6781

6882
CONFIG_SERIAL=y
6983

device/src/bt_conn.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,13 @@ static void enableDataLengthExtension(struct bt_conn *conn) {
291291
data_len = BT_LE_DATA_LEN_PARAM_MAX;
292292

293293
/**
294-
* This configures actual transmission length.
294+
* Configures actual transmission length. Cap the payload to our reduced ACL
295+
* buffer (CONFIG_BT_BUF_ACL_TX_SIZE) rather than the DLE max of 251: we never
296+
* send larger packets and shorter PDUs mean lower latency.
295297
*
296-
* We don't want it too high in order to prevent scheduling conflicts between multiple links.
298+
* tx_max_time kept low to avoid scheduling conflicts between multiple links.
297299
* */
300+
data_len->tx_max_len = CONFIG_BT_BUF_ACL_TX_SIZE;
298301
data_len->tx_max_time = 2500;
299302

300303
int err = bt_conn_le_data_len_update(conn, data_len);

device/src/link_protocol.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@
88

99
// Macros:
1010

11-
// 251 = maximum BLE packet length with data length extension
11+
// Maximum deserialized message length (header + payload) that fits in a single
12+
// BLE notification / UART bridge frame. We deliberately keep this well below the
13+
// DLE maximum (244 with a 251-byte ACL buffer) to save RAM (messenger-queue
14+
// regions, nus/uart buffers) and to shorten BLE packets for lower latency.
1215
// 4 bytes reserved for L2CAP header
1316
// 3 bytes reserved for ATT header
1417
// https://devzone.nordicsemi.com/f/nordic-q-a/111900/maximum-nus-packet-payload-with-ble-data-length-extensio
15-
// Should equal `CONFIG_BT_BUF_ACL_RX_SIZE - L2CAP_HEADER_SIZE - ATT_HEADER_SIZE`, otherwise something is wrong
16-
#define MAX_LINK_PACKET_LENGTH 244
18+
// Should equal `CONFIG_BT_BUF_ACL_RX_SIZE - L2CAP_HEADER_SIZE - ATT_HEADER_SIZE`
19+
// (i.e. 135 - 4 - 3), otherwise something is wrong.
20+
#define MAX_LINK_PACKET_LENGTH 128
1721

1822
// Typedefs:
1923

0 commit comments

Comments
 (0)