findfamily: bring up the Qorvo UWB stack on the DWM3001CDK
- Commit
98f593bdd00631b5924b33e960c4e00e0b835e78- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-22 00:10 UTC (3 months ago)
- Parents
79139a6
Diff
firmware/findfamily-tracker/boards/decawave_dwm3001cdk_nrf52833.overlay
diff --git a/firmware/findfamily-tracker/boards/decawave_dwm3001cdk_nrf52833.overlay b/firmware/findfamily-tracker/boards/decawave_dwm3001cdk_nrf52833.overlayindex 97798b7aa0..7441d924e1 100644--- a/firmware/findfamily-tracker/boards/decawave_dwm3001cdk_nrf52833.overlay+++ b/firmware/findfamily-tracker/boards/decawave_dwm3001cdk_nrf52833.overlay@@ -24,7 +24,15 @@ * raises it to CONFIG_QORVO_UWB_SPI_FREQ_FAST_HZ once identified. */ spi-max-frequency = <8000000>; irq-gpios = <&gpio1 2 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>;- rstn-gpios = <&gpio0 25 (GPIO_ACTIVE_LOW | GPIO_OPEN_DRAIN)>;+ /*+ * Open drain, but deliberately NOT GPIO_ACTIVE_LOW. qplatform_uwb_reset()+ * asserts reset with qgpio's QGPIO_OUTPUT_LOW, which maps to Zephyr's+ * GPIO_OUTPUT_INIT_LOW — a *logical* level. Marking the pin active-low+ * would invert that and release reset instead of asserting it. Active-high+ * here makes logical and physical levels agree, so "output low" really+ * pulls RSTn down; open drain still means it is never driven high.+ */+ rstn-gpios = <&gpio0 25 GPIO_OPEN_DRAIN>; wakeup-gpios = <&gpio1 19 GPIO_ACTIVE_HIGH>; status = "okay"; };
firmware/findfamily-tracker/src/tracker_uwb.c
diff --git a/firmware/findfamily-tracker/src/tracker_uwb.c b/firmware/findfamily-tracker/src/tracker_uwb.cindex 17c1ef1899..0505e391ea 100644--- a/firmware/findfamily-tracker/src/tracker_uwb.c+++ b/firmware/findfamily-tracker/src/tracker_uwb.c@@ -24,6 +24,7 @@ #include <zephyr/sys/byteorder.h> /* Qorvo uwbstack. */+#include <ff_qorvo_l1_config.h> #include <l1_config.h> #include <llhw.h> #include <net/fira_region_params.h>@@ -36,8 +37,14 @@ LOG_MODULE_REGISTER(ff_uwb, LOG_LEVEL_INF); -/* The stack calls back from MAC context, so ranging results are only logged here. */-#define UWB_THREAD_STACK_SIZE 4096+/*+ * The stack calls back from MAC context, so ranging results are only logged here.+ *+ * 8 KB because uwbmac_poll_events runs the whole MAC event loop on this stack — frame+ * assembly, crypto and region callbacks all nest below it. 4 KB tripped the MPU stack+ * guard immediately on the first poll.+ */+#define UWB_THREAD_STACK_SIZE 8192 #define UWB_THREAD_PRIORITY 5 /*@@ -149,10 +156,22 @@ int ff_uwb_init(void) */ r = qplatform_init(); if (r != QERR_SUCCESS) {- LOG_ERR("qplatform_init: %d (DW3110 not responding over SPI?)", r);+ LOG_ERR("qplatform_init: %d", r);+ /*+ * Re-read the id on the raw bus. It worked before qplatform_init, so if it+ * reads back zero now, the transceiver is being held in reset rather than+ * failing to talk — which points at the rstn pin handling, not the SPI.+ */+ LOG_ERR("DEV_ID after failed init: 0x%08x", ff_qorvo_read_dev_id()); return -EIO; } + /*+ * Must precede l1_config_init, which stores its defaults back to this storage.+ * As shipped it points into the image, where the store would target flash.+ */+ ff_qorvo_l1_config_use_ram_storage();+ r = l1_config_init(&l1_config_platform_ops); if (r != QERR_SUCCESS) { LOG_ERR("l1_config_init: %d", r);@@ -177,15 +196,29 @@ int ff_uwb_init(void) stack_ready = true; LOG_INF("Qorvo uwbstack up; DW3110 identified");-- atomic_set(&poll_running, 1);- k_thread_create(&uwb_thread, uwb_thread_stack, UWB_THREAD_STACK_SIZE,- uwb_poll_thread, NULL, NULL, NULL, UWB_THREAD_PRIORITY, 0,- K_NO_WAIT);- k_thread_name_set(&uwb_thread, "ff_uwb"); return 0; } +/*+ * The MAC event loop is only driven while a session exists. Starting it at init time+ * faulted with a corrupted return address: there is nothing for uwbmac_poll_events to+ * dispatch before uwbmac_start(), and polling an unstarted MAC is not a supported state.+ */+static void start_polling(void)+{+ if (atomic_cas(&poll_running, 0, 1)) {+ k_thread_create(&uwb_thread, uwb_thread_stack, UWB_THREAD_STACK_SIZE,+ uwb_poll_thread, NULL, NULL, NULL, UWB_THREAD_PRIORITY, 0,+ K_NO_WAIT);+ k_thread_name_set(&uwb_thread, "ff_uwb");+ }+}++static void stop_polling(void)+{+ atomic_set(&poll_running, 0);+}+ /* * Push a filled session_parameters into the session. The stack has no bulk setter — * only a bulk *getter* — so each field goes in individually, the same way the vendor@@ -375,6 +408,7 @@ int ff_uwb_start(const struct ff_uwb_params *params, const uint8_t *secret) } session_active = true;+ start_polling(); LOG_INF("FiRa responder started: session=%08x ch=%u preamble=%u own=%04x peer=%04x", params->session_id, params->channel, params->preamble_index, own_short_addr, peer_short_addr);@@ -392,6 +426,7 @@ void ff_uwb_stop(void) if (!session_active) { return; }+ stop_polling(); (void)fira_helper_stop_session(&fira_ctx, session_handle); (void)fira_helper_deinit_session(&fira_ctx, session_handle); (void)uwbmac_stop(uwbmac_ctx);
firmware/findfamily-tracker/uwb.conf
diff --git a/firmware/findfamily-tracker/uwb.conf b/firmware/findfamily-tracker/uwb.confindex 4d67913385..8f68ca0eaa 100644--- a/firmware/findfamily-tracker/uwb.conf+++ b/firmware/findfamily-tracker/uwb.conf@@ -9,9 +9,29 @@ CONFIG_FF_TRACKER_UWB=y +# Zephyr log level for firmware/qorvo-uwb's own sources (3 = INF), so the SPI probe trace+# and the DW3110 init messages are visible. Note this is Zephyr's scale, where 2 would be+# warning — distinct from CONFIG_QORVO_UWB_QLOG_LEVEL, which is Qorvo's.+CONFIG_QORVO_UWB_LOG_LEVEL_INF=y++# Immediate rather than deferred logging for this build. Deferred output is flushed by a+# log thread, so an early crash loses everything queued — which presents as the boot banner+# (a direct printk) followed by silence, and hides the actual failure.+CONFIG_LOG_MODE_DEFERRED=n+CONFIG_LOG_MODE_IMMEDIATE=y+ # qosal allocates session state and frame buffers; firmware/qorvo-uwb routes those to-# k_malloc, so this pool has to cover them as well as normal kernel use.-CONFIG_HEAP_MEM_POOL_SIZE=16384+# k_malloc, so this pool has to cover them as well as normal kernel use — and now the MAC+# thread stacks too, since those are allocated from it.+CONFIG_HEAP_MEM_POOL_SIZE=24576++# Thread stacks for the Qorvo MAC are allocated at runtime. Its qmalloc returns pointers+# offset past a quota prefix, so the buffers it passes to qthread_create are byte-misaligned+# and unusable as Zephyr stacks; firmware/qorvo-uwb/src/qthread_zephyr.c ignores them and+# allocates properly instead, which needs these.+CONFIG_DYNAMIC_THREAD=y+CONFIG_DYNAMIC_THREAD_ALLOC=y+CONFIG_DYNAMIC_THREAD_PREFER_ALLOC=y # The Qorvo MAC runs its own threads and workqueue. CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
firmware/qorvo-uwb/CMakeLists.txt
diff --git a/firmware/qorvo-uwb/CMakeLists.txt b/firmware/qorvo-uwb/CMakeLists.txtindex 42ae10dae8..c95420a119 100644--- a/firmware/qorvo-uwb/CMakeLists.txt+++ b/firmware/qorvo-uwb/CMakeLists.txt@@ -56,6 +56,7 @@ set(FIRA_INC zephyr_include_directories( ${FIRA_INC}+ ${CMAKE_CURRENT_LIST_DIR}/include ${QOSAL}/include ${QOSAL}/include/zephyr ${QHAL}/include@@ -77,8 +78,12 @@ zephyr_library_named(qorvo_uwb) zephyr_library_sources( # Our glue. Three of the SDK's Zephyr backends are unusable as shipped: # - qhal/src/zephyr/qspi.c returns QERR_ENOTSUP from every function (a placeholder).+ # - qhal/src/zephyr/qtimer.c likewise, which alone makes llhw_init fail with EIO. # - qhal/src/zephyr/qgpio.c redefines gpio_is_ready_dt/gpio_add_callback_dt, which # Zephyr has since added itself.+ src/qspi_zephyr.c+ src/qtimer_zephyr.c+ src/qgpio_zephyr.c # - qplatform/qm33_qhal_zephyr/qplatform.c dereferences spi_config.cs as a pointer # (by value since Zephyr 3.5) and hard-requires a "dw35720" node label. # - qhal/src/qm33/qpwr.c and qhal/src/qrtc.c likewise, or route through@@ -89,7 +94,9 @@ zephyr_library_sources( src/qgpio_zephyr.c src/qplatform_zephyr.c src/qhal_extras_zephyr.c+ src/l1_config_storage_zephyr.c src/qmalloc_zephyr.c+ src/qthread_zephyr.c src/mcps_crypto_psa.c # qosal — Zephyr OS abstraction.@@ -106,13 +113,11 @@ zephyr_library_sources( ${QOSAL}/src/zephyr/qrand.c ${QOSAL}/src/zephyr/qsemaphore.c ${QOSAL}/src/zephyr/qsignal.c- ${QOSAL}/src/zephyr/qthread.c ${QOSAL}/src/zephyr/qtime.c ${QOSAL}/src/zephyr/qtracing.c ${QOSAL}/src/zephyr/qworkqueue.c - # qhal — Zephyr hardware abstraction (qspi.c and qgpio.c replaced above).- ${QHAL}/src/zephyr/qtimer.c+ # qhal — Zephyr hardware abstraction (qspi.c, qgpio.c and qtimer.c replaced above). # OTP access, needed to read this module's factory calibration. ${QHAL}/src/qm33/qotp.c @@ -138,6 +143,7 @@ zephyr_library_sources( zephyr_library_include_directories( ${QPLATFORM}/qm33_qhal_common/include+ ${CMAKE_CURRENT_LIST_DIR}/src ${QORVO_DRIVER_DIR}/dw3000 ${QORVO_SDK_DIR}/Src/Boards/Src/Common ${QORVO_SDK_DIR}/Src/Boards/Src/Common_OTP@@ -156,7 +162,7 @@ zephyr_library_compile_definitions( CONFIG_SPI_UWB_IRQ_PRIORITY=3 CONFIG_SPI_UWB_SLOW_RATE_FREQ=${CONFIG_QORVO_UWB_SPI_FREQ_SLOW_HZ} CONFIG_SPI_UWB_FAST_RATE_FREQ=${CONFIG_QORVO_UWB_SPI_FREQ_FAST_HZ}- CONFIG_QLOG_LEVEL=${CONFIG_QORVO_UWB_LOG_LEVEL}+ CONFIG_QLOG_LEVEL=${CONFIG_QORVO_UWB_QLOG_LEVEL} # Number of GPIO callback slots in our qgpio backend's static table; the stack only # registers one, on the transceiver IRQ. CONFIG_QHAL_MAX_GPIO_CALLBACKS=2@@ -175,7 +181,8 @@ zephyr_library_compile_options( -Wno-missing-field-initializers ) -# Place the FiRa bundle's l1_config blob rather than leaving it an orphan section.+# The FiRa bundle's l1_config sections are placed in rodata purely to avoid orphan-section+# warnings; src/l1_config_storage_zephyr.c redirects the accessors to a RAM buffer. zephyr_linker_sources(RODATA l1_config_storage.ld) zephyr_link_libraries(${FIRA_LIB})
firmware/qorvo-uwb/Kconfig
diff --git a/firmware/qorvo-uwb/Kconfig b/firmware/qorvo-uwb/Kconfigindex 34ad0b635f..6cdca6bacb 100644--- a/firmware/qorvo-uwb/Kconfig+++ b/firmware/qorvo-uwb/Kconfig@@ -31,17 +31,25 @@ config QORVO_UWB_SPI_FREQ_SLOW_HZ config QORVO_UWB_SPI_FREQ_FAST_HZ int "SPI frequency used once the device is up (Hz)"- default 32000000+ default 8000000 help- Capped by the SPIM peripheral: nRF52833's SPIM3 does 32 MHz, other- instances only 8 MHz.+ The DW3000 tolerates up to 38 MHz, but this is also capped by the dw3110 node's+ spi-max-frequency, and 32 MHz only works at all on nRF52833's SPIM3. 8 MHz is+ plenty — nothing about ranging is bottlenecked on register access — and it avoids+ a class of signal-integrity problems that only appear once traffic gets real. -config QORVO_UWB_LOG_LEVEL- int "Qorvo stack log level passed as CONFIG_QLOG_LEVEL"+config QORVO_UWB_QLOG_LEVEL+ int "Log level passed to the Qorvo stack as CONFIG_QLOG_LEVEL" default 2 range 0 4 help- 0=off 1=error 2=info 3=debug 4=trace. The Qorvo stack filters its own logs- with this and routes them through the Zephyr logging backend.+ Qorvo's own scale, which is NOT Zephyr's: 0=off 1=error 2=info 3=debug 4=trace.+ The stack filters its own logs with this and routes them through the Zephyr+ logging backend. The Zephyr-side level for this module's own sources is+ QORVO_UWB_LOG_LEVEL below, where 2 means warning rather than info.++module = QORVO_UWB+module-str = Qorvo UWB+source "subsys/logging/Kconfig.template.log_config" endif # QORVO_UWB
firmware/qorvo-uwb/include/ff_qorvo_l1_config.h
diff --git a/firmware/qorvo-uwb/include/ff_qorvo_l1_config.h b/firmware/qorvo-uwb/include/ff_qorvo_l1_config.hnew file mode 100644index 0000000000..6f43351b1d--- /dev/null+++ b/firmware/qorvo-uwb/include/ff_qorvo_l1_config.h@@ -0,0 +1,22 @@+/*+ * Copyright (c) 2026 Vayun Mathur+ * SPDX-License-Identifier: Apache-2.0+ */+#ifndef FF_QORVO_L1_CONFIG_H_+#define FF_QORVO_L1_CONFIG_H_++/**+ * Point l1_config's storage at RAM buffers instead of the read-only sections the prebuilt+ * bundle places in the image.+ *+ * Must be called before l1_config_init(), which writes to that storage: on a device whose+ * stored configuration doesn't match its checksum — always the case here, since the+ * shipped section is uninitialised — it resets to defaults and stores the result. Left+ * pointing at the image, that store targets flash and fails.+ *+ * See src/l1_config_storage_zephyr.c for why this is done at runtime rather than in the+ * linker script.+ */+void ff_qorvo_l1_config_use_ram_storage(void);++#endif /* FF_QORVO_L1_CONFIG_H_ */
firmware/qorvo-uwb/l1_config_storage.ld
diff --git a/firmware/qorvo-uwb/l1_config_storage.ld b/firmware/qorvo-uwb/l1_config_storage.ldindex e38ef36845..f9bf4572d3 100644--- a/firmware/qorvo-uwb/l1_config_storage.ld+++ b/firmware/qorvo-uwb/l1_config_storage.ld@@ -1,18 +1,16 @@ /*- * Placement for the Qorvo l1_config persistent-configuration blob.+ * Placement for the Qorvo l1_config sections. *- * The prebuilt FiRa bundle emits .l1_config_persist_storage (1004 bytes of default L1- * configuration) and .l1_config_persist_storage_sha256 (its checksum) as their own- * sections, expecting the platform's linker script to place them. Without this they land- * as orphan sections, which the linker warns about and which leaves- * l1_config_get_persistent_memory() pointing somewhere unintended.+ * The prebuilt FiRa bundle emits .l1_config_persist_storage and+ * .l1_config_persist_storage_sha256 as their own sections and expects the platform's+ * linker script to place them. Left unplaced they become orphan sections, which the+ * linker warns about. *- * NOTE: this puts the blob in rodata, alongside code. That is correct for *reading* the- * compiled-in defaults, which is all this firmware needs — per-unit calibration is read- * from the DW3110's own OTP at init, and qflash_write() is deliberately unimplemented- * (see qhal_extras_zephyr.c). If persisting a modified L1 config is ever needed, this- * has to move to a dedicated flash partition first: erasing a page here would erase- * code with it.+ * They are placed in rodata and then go unused: src/l1_config_storage_zephyr.c overrides+ * the accessors l1_config reads them through, pointing them at a writable RAM buffer+ * instead. See that file for why RAM placement via the linker was abandoned — every+ * variant of it disturbed a RAM layout the kernel depends on. This snippet exists purely+ * to keep the build free of orphan-section warnings. */ . = ALIGN(4); KEEP(*(.l1_config_persist_storage))
firmware/qorvo-uwb/src/l1_config_storage_zephyr.c
diff --git a/firmware/qorvo-uwb/src/l1_config_storage_zephyr.c b/firmware/qorvo-uwb/src/l1_config_storage_zephyr.cnew file mode 100644index 0000000000..f3dedc2cde--- /dev/null+++ b/firmware/qorvo-uwb/src/l1_config_storage_zephyr.c@@ -0,0 +1,63 @@+/*+ * RAM-backed storage for the Qorvo l1_config "persistent" configuration.+ *+ * l1_config's storage has to be writable: `internal_l1_config` has no initializer, so the+ * stored hash never matches on a fresh device and l1_config_init() always calls+ * reset_to_default() and then stores the result. As shipped, the storage is two sections+ * the bundle places in the image, and on this target that lands in flash — where a store+ * would mean erasing a page shared with code.+ *+ * Making the linker put those sections in RAM was tried four ways and abandoned: rodata+ * boots but the store fails ENOTSUP; .data faults before logging starts; .noinit pushed a+ * kernel variable (slice_expired) into an MPU stack-guard region so the kernel faulted+ * writing its own state; and a dedicated NOLOAD section hung the image before main().+ * Overriding the accessors doesn't work either, because l1_config calls them from inside+ * the same translation unit, so those calls are bound locally and never reach us.+ *+ * What does work: the accessors return two ordinary global pointers, so repointing them at+ * buffers of our own before l1_config_init() runs puts the storage in RAM with no linker+ * surgery and no symbol games. See ff_qorvo_l1_config_use_ram_storage().+ *+ * Not persisting across reset is fine: what matters is this module's antenna delays and+ * crystal trim, and reset_to_default re-reads those from the DW3110's OTP every boot.+ */+#include "ff_qorvo_l1_config.h"++#include <zephyr/kernel.h>+#include <zephyr/logging/log.h>+#include <zephyr/toolchain.h>++#include <stdint.h>++LOG_MODULE_REGISTER(qorvo_l1cfg, CONFIG_QORVO_UWB_LOG_LEVEL);++/*+ * Defined by the bundle's l1_config_custom.c, which initialises them to point at its own+ * in-image sections and returns them from l1_config_get_persistent_memory() and+ * l1_config_get_persistent_memory_hash_addr().+ */+extern const uint32_t *persistent_ram_config;+extern const uint32_t *persistent_ram_config_hash;++/*+ * sizeof(struct l1_config) is 1004 bytes in this delivery (the size of the+ * .l1_config_persist_storage section the archive emits), and the struct is only declared in+ * the SDK's internal headers. Sized past that so a larger struct in a future SDK cannot+ * overrun it, since l1_config copies sizeof(struct l1_config) in and out of here.+ */+#define L1_CONFIG_STORAGE_SIZE 2048+/* store_to_persistent_memory writes a full SHA-256 to the hash address. */+#define L1_CONFIG_HASH_SIZE 32++static uint8_t l1_config_storage[L1_CONFIG_STORAGE_SIZE] __aligned(8);+static uint8_t l1_config_hash_storage[L1_CONFIG_HASH_SIZE] __aligned(8);++void ff_qorvo_l1_config_use_ram_storage(void)+{+ persistent_ram_config = (const uint32_t *)l1_config_storage;+ persistent_ram_config_hash = (const uint32_t *)l1_config_hash_storage;++ LOG_INF("l1_config storage redirected to RAM: config %p (%u bytes), hash %p (%u bytes)",+ (void *)l1_config_storage, (unsigned int)sizeof(l1_config_storage),+ (void *)l1_config_hash_storage, (unsigned int)sizeof(l1_config_hash_storage));+}
firmware/qorvo-uwb/src/qhal_extras_zephyr.c
diff --git a/firmware/qorvo-uwb/src/qhal_extras_zephyr.c b/firmware/qorvo-uwb/src/qhal_extras_zephyr.cindex d1af800836..fd3e5f13eb 100644--- a/firmware/qorvo-uwb/src/qhal_extras_zephyr.c+++ b/firmware/qorvo-uwb/src/qhal_extras_zephyr.c@@ -25,6 +25,8 @@ #include <zephyr/kernel.h> #include <zephyr/logging/log.h> +#include <string.h>+ LOG_MODULE_REGISTER(qorvo_qhal, CONFIG_QORVO_UWB_LOG_LEVEL); /* From qplatform_zephyr.c; carries the chip-select pin used for the wake pulse. */@@ -152,18 +154,34 @@ void qrtc_update_rtc_systime(int64_t updated_rtc_us, uint32_t updated_systime) ARG_UNUSED(updated_systime); } -/* ---- Flash ------------------------------------------------------------ */+/* ---- "Flash" ---------------------------------------------------------- */ enum qerr qflash_write(uint32_t dst_addr, void *src_addr, uint32_t size) {- ARG_UNUSED(dst_addr);- ARG_UNUSED(src_addr);- ARG_UNUSED(size);+ const uint32_t ram_start = CONFIG_SRAM_BASE_ADDRESS;+ const uint32_t ram_end = CONFIG_SRAM_BASE_ADDRESS + (CONFIG_SRAM_SIZE * 1024);++ if (src_addr == NULL || size == 0U) {+ return QERR_EINVAL;+ }+ /*- * Only reached by l1_config's "store calibration to persistent memory" path.- * Per-unit antenna-delay calibration is not done here — the DWM3001CDK module- * ships calibrated and l1_config_init(NULL) uses defaults — so there is nothing- * to write back.+ * l1_config is the only caller, storing its configuration and that config's+ * SHA-256 to what it believes is persistent memory. Both live in .data here (see+ * l1_config_storage.ld), so this is a plain copy and the store/load round-trip is+ * consistent within a boot. It is deliberately not persistent: the calibration+ * that matters is re-read from the DW3110's OTP by reset_to_default on every boot.+ *+ * The bounds check is the point of this function — if a future SDK moves those+ * sections back into flash, this must fail loudly rather than write into the+ * address space where the code lives. */- return QERR_ENOTSUP;+ if (dst_addr < ram_start || (dst_addr + size) > ram_end) {+ LOG_ERR("qflash_write to 0x%08x (%u bytes) is outside RAM; refusing",+ dst_addr, size);+ return QERR_ENOTSUP;+ }++ memcpy((void *)dst_addr, src_addr, size);+ return QERR_SUCCESS; }
firmware/qorvo-uwb/src/qmalloc_zephyr.c
diff --git a/firmware/qorvo-uwb/src/qmalloc_zephyr.c b/firmware/qorvo-uwb/src/qmalloc_zephyr.cindex eddf4f0392..10189053ef 100644--- a/firmware/qorvo-uwb/src/qmalloc_zephyr.c+++ b/firmware/qorvo-uwb/src/qmalloc_zephyr.c@@ -14,6 +14,14 @@ #include "qmalloc.h" #include <zephyr/kernel.h>+#include <zephyr/sys/util.h>++/*+ * Thread stacks come out of here (see qmalloc_internal), and Zephyr assumes a stack buffer+ * meets ARCH_STACK_PTR_ALIGN. 32 bytes covers that on Cortex-M and is also the MPU region+ * granularity the stack guard wants.+ */+#define QMALLOC_ALIGN 32 /* * Per-quota budgets consumed by qosal's wrappers in qosal/src/qmalloc.c. Index 0 is@@ -38,11 +46,25 @@ uint32_t allocation_quotas[] = { void *qmalloc_internal(size_t size) {- return k_malloc(size);+ /*+ * Aligned, not plain k_malloc: qthread_create() requires its caller to supply the+ * thread stack, and the prebuilt uwbmac allocates those through here. Zephyr builds+ * a thread's initial stack frame assuming the buffer meets ARCH_STACK_PTR_ALIGN, and+ * k_malloc only guarantees pointer alignment — so a MAC thread came up with a+ * garbage return address and faulted with "Illegal load of EXC_RETURN into PC" the+ * moment it was scheduled.+ */+ return k_aligned_alloc(QMALLOC_ALIGN, ROUND_UP(size, QMALLOC_ALIGN)); } void *qrealloc_internal(void *ptr, size_t new_size) {+ /*+ * Plain k_realloc, which drops back to pointer alignment. That is fine: the+ * alignment above matters only for thread stacks, and those are allocated once by+ * qthread_create's caller and never resized. Growing an aligned block by hand here+ * would mean tracking the old size ourselves just to copy it.+ */ return k_realloc(ptr, new_size); }
firmware/qorvo-uwb/src/qplatform_zephyr.c
diff --git a/firmware/qorvo-uwb/src/qplatform_zephyr.c b/firmware/qorvo-uwb/src/qplatform_zephyr.cindex 8437ffea0e..d93e53edca 100644--- a/firmware/qorvo-uwb/src/qplatform_zephyr.c+++ b/firmware/qorvo-uwb/src/qplatform_zephyr.c@@ -81,11 +81,24 @@ struct qspi_config qm33_qspi_config = { * The transaction is a DW3000 short-addressed read: one header byte (bit 7 clear = read, * register file 0 offset 0) followed by four bytes clocked out to shift the value in. */+static const struct spi_dt_spec uwb_bus =+ SPI_DT_SPEC_GET(UWB_NODE, SPI_WORD_SET(8) | SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB, 0);++/*+ * The devicetree-derived bus spec, shared with the qspi backend.+ *+ * Hand-assembling an equivalent spi_config produced transfers that clocked out correctly+ * but read back all zeros, while this spec worked on the same bus with what logged as an+ * identical frequency, operation word and chip-select. Rather than keep guessing at the+ * discrepancy, the backend uses Zephyr's own DT plumbing and only overrides the clock.+ */+const struct spi_dt_spec *ff_qorvo_spi_spec(void)+{+ return &uwb_bus;+}+ uint32_t ff_qorvo_read_dev_id(void) {- static const struct spi_dt_spec bus =- SPI_DT_SPEC_GET(UWB_NODE,- SPI_WORD_SET(8) | SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB, 0); uint8_t tx[5] = { 0x00, 0x00, 0x00, 0x00, 0x00 }; uint8_t rx[5] = { 0 }; const struct spi_buf tx_buf = { .buf = tx, .len = sizeof(tx) };@@ -93,7 +106,7 @@ uint32_t ff_qorvo_read_dev_id(void) const struct spi_buf_set tx_set = { .buffers = &tx_buf, .count = 1 }; const struct spi_buf_set rx_set = { .buffers = &rx_buf, .count = 1 }; - if (spi_transceive_dt(&bus, &tx_set, &rx_set) < 0) {+ if (spi_transceive_dt(&uwb_bus, &tx_set, &rx_set) < 0) { return 0; } return sys_get_le32(&rx[1]);
firmware/qorvo-uwb/src/qspi_zephyr.c
diff --git a/firmware/qorvo-uwb/src/qspi_zephyr.c b/firmware/qorvo-uwb/src/qspi_zephyr.cindex 3870b088cf..2e9d8f3433 100644--- a/firmware/qorvo-uwb/src/qspi_zephyr.c+++ b/firmware/qorvo-uwb/src/qspi_zephyr.c@@ -23,10 +23,12 @@ LOG_MODULE_REGISTER(qorvo_qspi, CONFIG_QORVO_UWB_LOG_LEVEL); +/* From qplatform_zephyr.c: the devicetree-derived bus spec for the dw3110 node. */+const struct spi_dt_spec *ff_qorvo_spi_spec(void);+ struct qspi { const struct device *dev; struct spi_config cfg;- struct spi_cs_control cs; bool in_use; /* * Async completion callback. The stack sets this to opt into async transfers;@@ -41,6 +43,10 @@ struct qspi { /* One UWB transceiver per board; a static instance avoids needing an allocator here. */ static struct qspi the_spi; +/* Number of opening transfers to hexdump, which is enough to cover the device-id probe. */+#define TRACE_XFERS 4+static unsigned int traced;+ struct qspi *qspi_open(const struct qspi_instance *instance) { const struct device *dev;@@ -77,43 +83,32 @@ enum qerr qspi_close(struct qspi *spi) enum qerr qspi_configure(struct qspi *spi, const struct qspi_config *config) {- spi_operation_t op;+ const struct spi_dt_spec *spec = ff_qorvo_spi_spec(); if (spi == NULL || config == NULL || spi->dev == NULL) { return QERR_EINVAL; } - op = SPI_WORD_SET(8) | SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB;- if (config->op_flags & QSPI_CPOL) {- op |= SPI_MODE_CPOL;- }- if (config->op_flags & QSPI_CPHA) {- op |= SPI_MODE_CPHA;- }- if (config->op_flags & QSPI_LSB_FIRST) {- op &= ~(spi_operation_t)SPI_TRANSFER_MSB;- op |= SPI_TRANSFER_LSB;- }- if (config->op_flags & QSPI_LOOP) {- op |= SPI_MODE_LOOP;- }-- spi->cfg.frequency = config->freq_hz;- spi->cfg.operation = op;- /*- * qplatform hands us the chip select as a gpio_dt_spec in cs_pin.dev, taken- * from the devicetree. Zephyr's SPI driver drives CS itself when given this,- * which matters for the DW3000: it needs CS to frame the whole- * header-plus-body transfer, not each byte.+ * Start from the devicetree-derived config rather than assembling one by hand.+ * A hand-built equivalent clocked out the right bytes but read back all zeros,+ * even though its frequency, operation word and chip-select all logged identically+ * to this spec's. Only the clock is taken from qspi_config, because the driver+ * raises it from the cold-start rate once the device is identified.+ *+ * Clamped to the node's spi-max-frequency: the driver asks for its fast rate+ * unconditionally, and honouring a rate above what the board declares is how you+ * get a bus that enumerates fine and then fails once traffic gets real. */- if (config->cs_pin.dev != NULL) {- spi->cs.gpio = *(const struct gpio_dt_spec *)config->cs_pin.dev;- spi->cs.delay = 0;- spi->cfg.cs = spi->cs;- }+ spi->cfg = spec->config;+ spi->cfg.frequency = MIN(config->freq_hz, spec->config.frequency); - LOG_DBG("qspi_configure: %u Hz, op=0x%08x", config->freq_hz, (unsigned int)op);+ if (config->freq_hz > spec->config.frequency) {+ LOG_INF("qspi_configure: capping %u Hz to the node's %u Hz",+ config->freq_hz, spec->config.frequency);+ }+ LOG_INF("qspi_configure: %u Hz, op=0x%08x (from devicetree spec)",+ spi->cfg.frequency, (unsigned int)spi->cfg.operation); return QERR_SUCCESS; } @@ -152,6 +147,17 @@ enum qerr qspi_transceive(struct qspi *spi, const struct qspi_transfer *xfer) return QERR_EIO; } + /*+ * The first few transfers are the driver probing the device id. Logging them makes+ * a probe failure diagnosable: dwt_probe() only reports DWT_ERROR, which cannot+ * distinguish "the bus returned nothing" from "the id matched no known driver".+ */+ if (traced < TRACE_XFERS) {+ traced++;+ LOG_HEXDUMP_INF(xfer->tx_buf, MIN(xfer->tx_size, 8U), "spi tx");+ LOG_HEXDUMP_INF(xfer->rx_buf, MIN(xfer->rx_size, 8U), "spi rx");+ }+ if (spi->done_cb) { spi->done_cb(spi->done_arg); }
firmware/qorvo-uwb/src/qthread_zephyr.c
diff --git a/firmware/qorvo-uwb/src/qthread_zephyr.c b/firmware/qorvo-uwb/src/qthread_zephyr.cnew file mode 100644index 0000000000..8f7bb91e31--- /dev/null+++ b/firmware/qorvo-uwb/src/qthread_zephyr.c@@ -0,0 +1,130 @@+/*+ * Zephyr thread backend for Qorvo's qosal.+ *+ * Replaces qosal/src/zephyr/qthread.c, which hands the caller's buffer and size straight to+ * k_thread_create(). The MAC threads created through it faulted the instant they were+ * scheduled with "Illegal load of EXC_RETURN into PC" and an all-zero exception frame — the+ * signature of a thread whose stack is unusable, either misaligned or too small for+ * Zephyr's frame plus MPU guard.+ *+ * Rather than trust the supplied buffer, this allocates the stack with+ * k_thread_stack_alloc(), which is the only allocator that guarantees what Zephyr's thread+ * setup assumes about alignment and size on an MPU target. The requested size is honoured+ * as a floor, with a minimum applied because a stack sized for FreeRTOS (whose sizes are in+ * words, not bytes) would be a quarter of what it needs to be here.+ *+ * The requested parameters are logged so a stack that is being under-provisioned by the+ * caller is visible rather than presenting as a corrupt frame.+ */+#include "qthread.h"++#include <zephyr/kernel.h>+#include <zephyr/logging/log.h>+#include <zephyr/sys/util.h>++#include <string.h>++LOG_MODULE_REGISTER(qorvo_qthread, CONFIG_QORVO_UWB_LOG_LEVEL);++/* The MAC creates a couple of threads; a small fixed pool avoids a second allocator. */+#define MAX_THREADS 4++/*+ * Floor for a MAC thread. Frame assembly, crypto and region callbacks all nest on these,+ * and anything the caller asks for below this is treated as a FreeRTOS-style word count+ * rather than a byte count.+ */+#define MIN_STACK_SIZE 2048++struct qthread {+ struct k_thread thread;+ k_thread_stack_t *stack;+ bool used;+};++static struct qthread threads[MAX_THREADS];++static void zephyr_thread_entry(void *p1, void *p2, void *p3)+{+ qthread_func func = p1;++ ARG_UNUSED(p3);+ func(p2);+}++struct qthread *qthread_create(qthread_func thread, void *arg, const char *name, void *stack,+ uint32_t stack_size, enum qthread_priority prio)+{+ struct qthread *th = NULL;+ size_t size;++ if (thread == NULL || name == NULL) {+ return NULL;+ }+ if (prio >= QTHREAD_PRIORITY_MAX) {+ return NULL;+ }++ size = MAX(stack_size, MIN_STACK_SIZE);+ LOG_INF("qthread_create('%s'): caller stack %p size %u prio %d -> allocating %u bytes",+ name, stack, stack_size, (int)prio, (unsigned int)size);++ for (int i = 0; i < MAX_THREADS; i++) {+ if (!threads[i].used) {+ th = &threads[i];+ break;+ }+ }+ if (th == NULL) {+ LOG_ERR("out of qthread slots");+ return NULL;+ }++ /*+ * The caller's buffer is deliberately ignored. k_thread_stack_alloc is the only way+ * to get memory Zephyr will accept as a thread stack on an MPU target; a plain heap+ * buffer is not interchangeable with one, however well aligned.+ */+ th->stack = k_thread_stack_alloc(size, 0);+ if (th->stack == NULL) {+ LOG_ERR("k_thread_stack_alloc(%u) failed for '%s'", (unsigned int)size, name);+ return NULL;+ }++ th->used = true;+ k_thread_create(&th->thread, th->stack, size, zephyr_thread_entry, thread, arg, NULL,+ K_PRIO_PREEMPT((int)prio), 0, K_NO_WAIT);+ k_thread_name_set(&th->thread, name);+ return th;+}++static void release(struct qthread *thread)+{+ if (thread->stack != NULL) {+ (void)k_thread_stack_free(thread->stack);+ thread->stack = NULL;+ }+ thread->used = false;+}++enum qerr qthread_join(struct qthread *thread)+{+ if (thread == NULL || !thread->used) {+ return QERR_EINVAL;+ }+ if (k_thread_join(&thread->thread, K_FOREVER)) {+ return QERR_EIO;+ }+ release(thread);+ return QERR_SUCCESS;+}++enum qerr qthread_delete(struct qthread *thread)+{+ if (thread == NULL || !thread->used) {+ return QERR_EINVAL;+ }+ k_thread_abort(&thread->thread);+ release(thread);+ return QERR_SUCCESS;+}
firmware/qorvo-uwb/src/qtimer_zephyr.c
diff --git a/firmware/qorvo-uwb/src/qtimer_zephyr.c b/firmware/qorvo-uwb/src/qtimer_zephyr.cnew file mode 100644index 0000000000..823d48df71--- /dev/null+++ b/firmware/qorvo-uwb/src/qtimer_zephyr.c@@ -0,0 +1,107 @@+/*+ * Zephyr timer backend for Qorvo's qhal.+ *+ * Replaces qhal/src/zephyr/qtimer.c, which like its qspi.c sibling is a placeholder:+ * qtimer_init() returns NULL and every other entry point returns QERR_ENOTSUP. llhw_init()+ * sets up an idle timer through this, so with the stub in place the whole UWB stack fails+ * to initialise with QERR_EIO.+ *+ * Built on k_timer rather than the counter API. The stack uses this to schedule wakeups+ * around ranging blocks, which are milliseconds apart, so tick granularity (30.5 us at the+ * default 32768 Hz) is far finer than needed and avoids depending on a particular hardware+ * timer instance being free — the nRF's are already claimed by the kernel and the+ * Bluetooth controller.+ *+ * `qtimer_id`, and the requested frequency and width in qtimer_config, are therefore+ * ignored: they describe a hardware timer instance this implementation does not allocate.+ */+#include <qerr.h>+#include <qtimer.h>++#include <zephyr/kernel.h>+#include <zephyr/logging/log.h>++LOG_MODULE_REGISTER(qorvo_qtimer, CONFIG_QORVO_UWB_LOG_LEVEL);++/* The stack allocates one idle timer; a second slot leaves room without a heap. */+#define MAX_TIMERS 2++struct qtimer {+ struct k_timer timer;+ qtimer_cb cb;+ void *arg;+ int64_t started_ticks;+ bool used;+};++static struct qtimer timers[MAX_TIMERS];++static void on_expiry(struct k_timer *t)+{+ struct qtimer *q = CONTAINER_OF(t, struct qtimer, timer);++ if (q->cb) {+ q->cb(q->arg);+ }+}++struct qtimer *qtimer_init(uint8_t qtimer_id, const struct qtimer_config *config,+ qtimer_cb handler, void *arg)+{+ ARG_UNUSED(qtimer_id);+ ARG_UNUSED(config);++ for (int i = 0; i < MAX_TIMERS; i++) {+ if (timers[i].used) {+ continue;+ }+ timers[i].cb = handler;+ timers[i].arg = arg;+ timers[i].started_ticks = 0;+ timers[i].used = true;+ k_timer_init(&timers[i].timer, on_expiry, NULL);+ return &timers[i];+ }+ LOG_ERR("out of qtimer slots");+ return NULL;+}++enum qerr qtimer_start(const struct qtimer *timer, uint32_t us, bool periodic)+{+ /* The interface is const, but starting a timer necessarily mutates it. */+ struct qtimer *q = (struct qtimer *)timer;++ if (q == NULL || !q->used) {+ return QERR_EINVAL;+ }+ q->started_ticks = k_uptime_ticks();+ /*+ * Note the inverted sense in qtimer.h: it documents `periodic` as "true for a+ * one-shot timer, false for a cyclic timer". Going by the parameter name rather+ * than that comment, since the name is what callers read — periodic means repeat.+ */+ k_timer_start(&q->timer, K_USEC(us), periodic ? K_USEC(us) : K_NO_WAIT);+ return QERR_SUCCESS;+}++enum qerr qtimer_stop(const struct qtimer *timer)+{+ struct qtimer *q = (struct qtimer *)timer;++ if (q == NULL || !q->used) {+ return QERR_EINVAL;+ }+ k_timer_stop(&q->timer);+ return QERR_SUCCESS;+}++enum qerr qtimer_read(const struct qtimer *timer, uint32_t *us)+{+ struct qtimer *q = (struct qtimer *)timer;++ if (q == NULL || !q->used || us == NULL) {+ return QERR_EINVAL;+ }+ *us = (uint32_t)k_ticks_to_us_floor64(k_uptime_ticks() - q->started_ticks);+ return QERR_SUCCESS;+}