Modern Apps git

← Modern-Apps.git

findfamily: wire Qorvo FiRa stack into tracker firmware as Zephyr module

Commit
c46d54d2f3f14d640bbdd448a8d292308f9c6bd0
Author
vayun-mathur <[email protected]>
When
2026-08-21 21:39 UTC (3 months ago)
Parents
02a43f8

Diff

firmware/findfamily-tracker/Kconfig
diff --git a/firmware/findfamily-tracker/Kconfig b/firmware/findfamily-tracker/Kconfigindex 12a0e31d27..d74d296c74 100644--- a/firmware/findfamily-tracker/Kconfig+++ b/firmware/findfamily-tracker/Kconfig@@ -6,11 +6,13 @@ menu "FindFamily tracker" config FF_TRACKER_UWB 	bool "Build the UWB (FiRa responder) path" 	default n+	select QORVO_UWB 	help 	  Compile src/tracker_sts.c and src/tracker_uwb.c, which run a FiRa 	  responder/controlee session on the DW3110. Off by default because it needs-	  the licensed Qorvo DW3xxx driver, which is not vendored in this tree — the-	  BLE crowd-finding image builds and flashes without it. See README.md.+	  the licensed Qorvo DW3xxx SDK, which is not vendored in this repo — the BLE+	  crowd-finding image builds and flashes without it. Point QORVO_SDK_DIR at an+	  extracted copy to enable this. See README.md.  config FF_TRACKER_BEACON_INTERVAL_MS 	int "Beacon advertising interval (ms)"
firmware/findfamily-tracker/README.md
diff --git a/firmware/findfamily-tracker/README.md b/firmware/findfamily-tracker/README.mdindex 2865788af5..597ce84bb8 100644--- a/firmware/findfamily-tracker/README.md+++ b/firmware/findfamily-tracker/README.md@@ -134,18 +134,68 @@ otherwise look identical.  ## UWB precision finding -`src/tracker_sts.c` and `src/tracker_uwb.c` are **guarded by `CONFIG_FF_TRACKER_UWB`,-default `n`, and have never been compiled.** They call into Qorvo's DW3xxx driver, which-is licensed and not vendored here. To enable:--1. Obtain the Qorvo DW3xxx driver / FiRa stack and add it as a Zephyr module (a `west.yml`-   entry or `ZEPHYR_EXTRA_MODULES`), so `deca_device_api.h` is on the include path.-2. Add a devicetree overlay binding the driver to `spi3` and the module's IRQ/RST/WAKEUP-   lines. The board's `spi3` node already exists for the DW3000.-3. Build with `-DCONFIG_FF_TRACKER_UWB=y`.--Expect this to need iteration. The uncertain part isn't the key derivation — that has-fixed vectors on both sides — it's matching Android's `CONFIG_UNICAST_DS_TWR` against a-hand-configured DW3110 responder: STS mode, RFRAME/packet format, and ranging-round and-block timing all have to line up. That is exactly why it is staged behind BLE rather than-blocking it.+`src/tracker_sts.c` and `src/tracker_uwb.c` build under `CONFIG_FF_TRACKER_UWB`, which+needs the licensed Qorvo **DW3_QM33 SDK**. It is not vendored here; only the glue is, in+`firmware/qorvo-uwb/` (a Zephyr module). Point two environment variables at an extracted+copy and build with the `uwb.conf` fragment:++```powershell+$env:QORVO_SDK_DIR   = "C:/qorvo"        # contains Libs/uwb-stack+$env:QORVO_DRIVER_DIR = "<...>/Drivers/API/Shared/dwt_uwb_driver"++cd C:\ncs\v3.4.0+nrfutil toolchain-manager launch --ncs-version v3.4.0 -- `+  west build -b decawave_dwm3001cdk/nrf52833 -d C:/ncs/ff-uwb-build `+  <repo>/firmware/findfamily-tracker --pristine `+  -- "-DEXTRA_CONF_FILE=uwb.conf" `+     "-DZEPHYR_EXTRA_MODULES=<repo>/firmware/qorvo-uwb"+```++Quote the `-D` arguments: PowerShell otherwise splits `uwb.conf` at the dot.++### State: builds, links, runs — but the stack does not come up yet++Verified on hardware:+- the image builds and links clean (58% flash, 47% RAM) and BLE crowd-finding is+  unaffected — the beacon keeps working in the UWB image;+- the DW3110 answers a direct device-id read over SPI with **`0xdeca0302`**, logged at+  startup. That clears the wiring, the `dw3110` devicetree node, chip-select handling and+  the SPI mode.++Not working: `qplatform_init()` returns `QERR_EADDRNOTAVAIL` (-1), so Qorvo's driver+rejects the part even though the bus is demonstrably fine. The fault is somewhere in the+glue below the driver rather than in the hardware — most likely `qspi_transceive` in+`firmware/qorvo-uwb/src/qspi_zephyr.c` (chip-select framing across the driver's+header-then-body transfers is the first thing to check) or the reset sequence in+`qhal_extras_zephyr.c`. **No ranging has been attempted**, and nothing here should be+taken as evidence that FiRa interop with the phone works.++### What the glue replaces, and why++Qorvo ships Zephyr backends but they cannot be used as they are:++| Vendor file | Problem | Replacement |+|---|---|---|+| `qhal/src/zephyr/qspi.c` | every function returns `QERR_ENOTSUP` — a placeholder | `src/qspi_zephyr.c` |+| `qhal/src/zephyr/qgpio.c` | defines `gpio_is_ready_dt`/`gpio_add_callback_dt`, which Zephyr now provides | `src/qgpio_zephyr.c` |+| `qplatform/qm33_qhal_zephyr/qplatform.c` | treats `spi_config.cs` as a pointer (by value since Zephyr 3.5); requires a `dw35720` node label | `src/qplatform_zephyr.c` |+| `qhal/src/qm33/qpwr.c`, `qhal/src/qrtc.c` | same, or route through `persistent_time`, which is nrfx-only | `src/qhal_extras_zephyr.c` |+| `qosal/src/zephyr/qmalloc.c` | uses libc malloc, which collides with picolibc's in this build | `src/qmalloc_zephyr.c` (k_malloc) |+| `Src/UWB/mcps_crypto.c` | built on Nordic's nrf_crypto, a second crypto provider | `src/mcps_crypto_psa.c` (PSA) |++`include-compat/` additionally papers over qosal being written against Zephyr 3.1: the+pre-3.0 unprefixed include paths, and the internal `log_msg2` logging symbols renamed in+Zephyr 3.2. That directory is the most likely thing to break on an NCS upgrade.++The vendor's DWM3001CDK calibration code (`platform_l1_config.c`) is compiled as-is — it+reads this module's factory antenna delays and crystal trim out of the DW3110's OTP, and+those values are what make ranging accurate.++### Interop is still the open risk++Even once the stack initialises, matching Android's `CONFIG_UNICAST_DS_TWR` against a+hand-configured responder means aligning STS, RFRAME format, and ranging-round and block+timing. Under static STS the timing parameters are configured identically on both ends+rather than negotiated, so `SLOT_DURATION_RSTU`, `BLOCK_DURATION_MS` and+`ROUND_DURATION_SLOTS` in `src/tracker_uwb.c` are the first things to vary if the phone+will not range.
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.overlaynew file mode 100644index 0000000000..97798b7aa0--- /dev/null+++ b/firmware/findfamily-tracker/boards/decawave_dwm3001cdk_nrf52833.overlay@@ -0,0 +1,31 @@+/*+ * DWM3001CDK: expose the on-module DW3110 as an SPI child so the Qorvo stack can+ * find its bus, chip select, IRQ and reset lines from the devicetree.+ *+ * Pin assignments are the DWM3001CDK's, taken from the vendor SDK's board config+ * (Projects/FreeRTOS/<app>/DWM3001CDK/ProjectDefinition/uwb_stack_llhw.cmake):+ *   SPI3  SCK P0.03, MOSI P0.08, MISO P0.29, CS P1.06   (already in the board dts)+ *   IRQ   P1.02+ *   RSTn  P0.25   open-drain: the DW3000 must never be driven high, only pulled low+ *   WAKEUP P1.19  unused for now; the part is kept out of deep sleep+ *+ * `qorvo,dw3110` has no Zephyr driver behind it: the binding+ * (firmware/qorvo-uwb/dts/bindings) exists only so DT_BUS()/GPIO_DT_SPEC_GET() resolve.+ * The Qorvo stack reaches the bus itself, see firmware/qorvo-uwb/src/qplatform_zephyr.c.+ */++&spi3 {+	status = "okay";++	dw3110: dw3110@0 {+		compatible = "qorvo,dw3110";+		reg = <0>;+		/* 8 MHz is the DW3000's safe rate until it leaves INIT state; the driver+		 * 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)>;+		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 4ef382669a..17c1ef1899 100644--- a/firmware/findfamily-tracker/src/tracker_uwb.c+++ b/firmware/findfamily-tracker/src/tracker_uwb.c@@ -1,24 +1,21 @@ /*- * FiRa responder/controlee session on the DW3110.+ * FiRa responder/controlee session on the DW3110, via Qorvo's uwbstack.  *- * NOT COMPILED BY DEFAULT — see CONFIG_FF_TRACKER_UWB in Kconfig and the "UWB" section- * of README.md. The DW3xxx driver is licensed and not vendored here, so the calls into- * it below are written against Qorvo's documented API but have never been built or run.- * Treat every dwt_* call as unverified.+ * The phone is the FiRa controller/initiator: it mints the session id, channel,+ * preamble and its own MAC, and drives the ranging rounds. This device is the+ * controlee/responder. Session material is derived, not received — see tracker_sts.c+ * and TrackerUwbKeys.kt — so the 8 bytes written over GATT per find are only address,+ * session id, channel and preamble.  *- * Roles: the phone is the controller/initiator (it mints session id, channel, preamble- * and its own address, and starts ranging); this device is the controlee/responder. The- * phone's stack uses UwbRangingParams.CONFIG_UNICAST_DS_TWR with static STS, which is- * what the configuration below aims at:+ * Built only when CONFIG_FF_TRACKER_UWB=y, which needs the licensed Qorvo SDK; see+ * README.md and firmware/qorvo-uwb/.  *- *   - double-sided two-way ranging, one initiator and one responder- *   - static STS, key from ff_sts_key() (FiRa reads those 8 bytes as- *     [2B VendorID][6B STATIC_STS_IV])- *   - channel 9, preamble index 10 (UwbController.DEFAULT_CHANNEL / DEFAULT_PREAMBLE)- *- * Aligning the ranging-round and block timing with what Android's stack picks is the- * genuinely uncertain part of this and should be expected to need iteration on real- * hardware.+ * Interop caveat: the parameters below are FiRa/Qorvo defaults, which is the best+ * available guess at what Android's `UwbRangingParams.CONFIG_UNICAST_DS_TWR` asks for.+ * Slot duration, block duration and round length are session parameters under static+ * STS — they are configured identically on both ends rather than negotiated — so if+ * they disagree with what the phone picked, ranging simply never converges. Expect to+ * iterate here against a real phone.  */ #include "ff_tracker.h" @@ -26,47 +23,261 @@ #include <zephyr/logging/log.h> #include <zephyr/sys/byteorder.h> -/* Provided by the Qorvo DW3xxx driver; see README.md for dropping it in. */-#include <deca_device_api.h>-#include <deca_regs.h>+/* Qorvo uwbstack. */+#include <l1_config.h>+#include <llhw.h>+#include <net/fira_region_params.h>+#include <qplatform.h>+#include <quwbs/fbs/defs.h>+#include <uwbmac/fira_helper.h>+#include <uwbmac/uwbmac.h>  #include <string.h>  LOG_MODULE_REGISTER(ff_uwb, LOG_LEVEL_INF); -#define STS_KEY_LEN 8+/* The stack calls back from MAC context, so ranging results are only logged here. */+#define UWB_THREAD_STACK_SIZE 4096+#define UWB_THREAD_PRIORITY 5++/*+ * Ranging timing. These are the FiRa defaults (the vendor SDK's fira_default_params.h+ * carries the same values, but that header ships with its sample apps rather than the+ * library, so they are spelled out here).+ *+ * Under static STS these are *not* negotiated: the controller and controlee each+ * configure them and they have to agree, so these three constants are the most likely+ * thing to need changing if the phone won't range. 2400 RSTU is 2 ms; 25 slots of that+ * inside a 200 ms block leaves the radio idle most of the time.+ */+#define SLOT_DURATION_RSTU 2400+#define BLOCK_DURATION_MS 200+#define ROUND_DURATION_SLOTS 25 +static struct uwbmac_context *uwbmac_ctx;+static struct fira_context fira_ctx;+static uint32_t session_handle;+static bool stack_ready; static bool session_active; +static K_THREAD_STACK_DEFINE(uwb_thread_stack, UWB_THREAD_STACK_SIZE);+static struct k_thread uwb_thread;+static atomic_t poll_running;++/*+ * Board calibration hook, from the vendor's DWM3001CDK support (built by+ * firmware/qorvo-uwb). It loads this module's factory calibration — antenna delays,+ * crystal trim, TX power — out of the DW3110's OTP. l1_config_init() returns+ * QERR_ENOTSUP without it.+ */+extern struct l1_config_platform_ops l1_config_platform_ops;++/*+ * Bring-up diagnostic from firmware/qorvo-uwb: reads DEV_ID straight over Zephyr's SPI,+ * bypassing the Qorvo driver. A DW3110 answers 0xDECA0302.+ */+uint32_t ff_qorvo_read_dev_id(void);++static void on_fira_notification(enum fira_helper_cb_type cb_type, const void *content,+				 void *user_data)+{+	ARG_UNUSED(user_data);++	switch (cb_type) {+	case FIRA_HELPER_CB_TYPE_TWR_RANGE_NTF: {+		const struct fira_twr_ranging_results *res = content;++		for (int i = 0; i < res->n_measurements; i++) {+			const struct fira_twr_measurements *m = &res->measurements[i];++			/*+			 * The phone computes the arrow from its own multi-antenna array;+			 * the tracker is single-port and has no AoA to report. Logging the+			 * distance is purely so the bench can see the session converge.+			 */+			LOG_INF("range: peer=%04x status=%u distance=%dcm rssi=%u",+				m->short_addr, m->status, m->distance_cm, m->rssi);+		}+		break;+	}+	case FIRA_HELPER_CB_TYPE_SESSION_STATUS_NTF: {+		const struct fbs_helper_session_status_ntf *ntf = content;++		LOG_INF("session %u state=%d reason=%d", ntf->session_handle, ntf->state,+			ntf->reason_code);+		break;+	}+	default:+		LOG_DBG("fira notification type %d", cb_type);+		break;+	}+}++static void uwb_poll_thread(void *a, void *b, void *c)+{+	ARG_UNUSED(a);+	ARG_UNUSED(b);+	ARG_UNUSED(c);++	while (atomic_get(&poll_running)) {+		/* Drives the MAC event loop; notifications are dispatched from here. */+		(void)uwbmac_poll_events(uwbmac_ctx, 100000);+	}+}+ int ff_uwb_init(void) {+	enum qerr r;++	if (stack_ready) {+		return 0;+	}+ 	/*-	 * Bring up the DW3110 over SPI. The devicetree overlay names the SPI bus, IRQ,-	 * reset and wakeup lines (see boards/ in this directory).+	 * Read the device id before handing the bus to Qorvo's driver: it tells a wiring+	 * or SPI-mode problem apart from the driver rejecting what it read, which+	 * qplatform_init's return code alone cannot. 	 */-	if (dwt_probe(NULL) != DWT_SUCCESS) {-		LOG_ERR("dwt_probe failed: DW3110 not responding on SPI");-		return -ENODEV;+	LOG_INF("DW3110 DEV_ID read over SPI: 0x%08x (expect 0xdeca0302)",+		ff_qorvo_read_dev_id());++	/*+	 * Order matters and is not obvious. qplatform_init() brings up SPI/GPIO and probes+	 * the DW3110, which has to happen first: l1_config_init()'s reset_to_default hook+	 * reads this module's calibration out of the transceiver's OTP, and without a+	 * probed driver that dereferences an unset interface pointer and bus-faults.+	 */+	r = qplatform_init();+	if (r != QERR_SUCCESS) {+		LOG_ERR("qplatform_init: %d (DW3110 not responding over SPI?)", r);+		return -EIO;+	}++	r = l1_config_init(&l1_config_platform_ops);+	if (r != QERR_SUCCESS) {+		LOG_ERR("l1_config_init: %d", r);+		qplatform_deinit();+		return -EIO;+	}+	r = llhw_init();+	if (r != QERR_SUCCESS) {+		LOG_ERR("llhw_init: %d", r);+		l1_config_deinit();+		qplatform_deinit();+		return -EIO; 	}-	if (dwt_initialise(DWT_DW_INIT) != DWT_SUCCESS) {-		LOG_ERR("dwt_initialise failed");+	r = uwbmac_init(&uwbmac_ctx);+	if (r != QERR_SUCCESS) {+		LOG_ERR("uwbmac_init: %d", r);+		llhw_deinit();+		l1_config_deinit();+		qplatform_deinit(); 		return -EIO; 	}-	LOG_INF("DW3110 initialised");++	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; } +/*+ * 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+ * SDK's fira_set_session_parameters does it.+ */+#define SET_PARAM(field)                                                                  \+	do {                                                                              \+		enum qerr _r = fira_helper_set_session_##field(&fira_ctx, session_handle, \+							       sp->field);                \+		if (_r != QERR_SUCCESS) {                                                 \+			LOG_ERR("set " #field " failed: %d", _r);                         \+			return -EIO;                                                      \+		}                                                                         \+	} while (0)++static int apply_session_params(const struct session_parameters *sp)+{+	enum qerr r;++	SET_PARAM(channel_number);+	SET_PARAM(preamble_code_index);+	SET_PARAM(sfd_id);+	SET_PARAM(phr_data_rate);+	SET_PARAM(prf_mode);+	SET_PARAM(device_type);+	SET_PARAM(device_role);+	SET_PARAM(multi_node_mode);+	SET_PARAM(rframe_config);+	SET_PARAM(slot_duration_rstu);+	SET_PARAM(block_duration_ms);+	SET_PARAM(round_duration_slots);+	SET_PARAM(ranging_round_usage);+	SET_PARAM(round_hopping);+	SET_PARAM(block_stride_length);+	SET_PARAM(schedule_mode);+	SET_PARAM(result_report_config);+	SET_PARAM(ranging_round_control);+	SET_PARAM(report_rssi);++	r = fira_helper_set_session_vupper64(&fira_ctx, session_handle,+					     (uint8_t *)sp->vupper64);+	if (r != QERR_SUCCESS) {+		LOG_ERR("set vupper64 failed: %d", r);+		return -EIO;+	}+	r = fira_helper_set_session_short_address(&fira_ctx, session_handle, sp->short_addr);+	if (r != QERR_SUCCESS) {+		LOG_ERR("set short_address failed: %d", r);+		return -EIO;+	}+	r = fira_helper_set_session_destination_short_addresses(+		&fira_ctx, session_handle, sp->n_destination_short_address,+		(uint16_t *)sp->destination_short_address);+	if (r != QERR_SUCCESS) {+		LOG_ERR("set destination_short_addresses failed: %d", r);+		return -EIO;+	}+	r = fira_helper_set_session_measurement_sequence(&fira_ctx, session_handle,+							 &sp->meas_seq);+	if (r != QERR_SUCCESS) {+		LOG_ERR("set measurement_sequence failed: %d", r);+		return -EIO;+	}+	return 0;+}++#undef SET_PARAM+ int ff_uwb_start(const struct ff_uwb_params *params, const uint8_t *secret) {-	uint8_t sts_key[STS_KEY_LEN];+	struct session_parameters sp = { 0 };+	struct fbs_session_init_rsp rsp;+	uint8_t vupper64[FIRA_VUPPER64_SIZE]; 	uint8_t own_address[2];+	uint16_t own_short_addr;+	uint16_t peer_short_addr;+	enum qerr r; 	int rc; +	if (!stack_ready) {+		return -EAGAIN;+	} 	if (session_active) { 		ff_uwb_stop(); 	} -	rc = ff_sts_key(secret, params->session_id, sts_key);+	/*+	 * vUpper64 is exactly [2B VendorID][6B STATIC_STS_IV], which is the same 8 bytes+	 * Android hands to UwbRangingParams.setSessionKeyInfo. So the derived STS key is+	 * used verbatim on both sides with no reinterpretation.+	 */+	rc = ff_sts_key(secret, params->session_id, vupper64); 	if (rc) { 		return rc; 	}@@ -74,52 +285,106 @@ int ff_uwb_start(const struct ff_uwb_params *params, const uint8_t *secret) 	if (rc) { 		return rc; 	}+	own_short_addr = sys_get_le16(own_address);+	peer_short_addr = sys_get_le16(params->peer_address); -	LOG_INF("starting FiRa responder: session=%08x ch=%u preamble=%u peer=%02x%02x own=%02x%02x",-		params->session_id, params->channel, params->preamble_index,-		params->peer_address[0], params->peer_address[1],-		own_address[0], own_address[1]);+	r = uwbmac_set_short_addr(uwbmac_ctx, own_short_addr);+	if (r != QERR_SUCCESS) {+		LOG_ERR("uwbmac_set_short_addr: %d", r);+		return -EIO;+	} -	/*-	 * Static STS: the 8 derived bytes are the vendor id + IV, and the FiRa layer-	 * expands them into the STS. Both ends must load identical bytes here — this is-	 * where a Kotlin/firmware HKDF mismatch would silently break ranging.-	 */-	dwt_configurestskey((dwt_sts_cp_key_t *)sts_key);-	dwt_configurestsiv((dwt_sts_cp_iv_t *)sts_key);-	dwt_configurestsloadiv();--	{-		dwt_config_t cfg = {-			.chan = params->channel,-			.txPreambLength = DWT_PLEN_128,-			.rxPAC = DWT_PAC8,-			.txCode = params->preamble_index,-			.rxCode = params->preamble_index,-			.sfdType = DWT_SFD_IEEE_4Z,-			.dataRate = DWT_BR_6M8,-			.phrMode = DWT_PHRMODE_STD,-			.phrRate = DWT_PHRRATE_STD,-			.sfdTO = (129 + 8 - 8),-			.stsMode = DWT_STS_MODE_1,-			.stsLength = DWT_STS_LEN_64,-			.pdoaMode = DWT_PDOA_M0,-		};--		if (dwt_configure(&cfg) != DWT_SUCCESS) {-			LOG_ERR("dwt_configure failed");-			return -EIO;-		}+	r = fira_helper_open(&fira_ctx, uwbmac_ctx, on_fira_notification, "endless", 0, NULL);+	if (r != QERR_SUCCESS) {+		LOG_ERR("fira_helper_open: %d", r);+		return -EIO;+	}+	/* Must happen while the MAC is stopped. */+	r = fira_helper_set_scheduler(&fira_ctx);+	if (r != QERR_SUCCESS) {+		LOG_ERR("fira_helper_set_scheduler: %d", r);+		goto close;+	}++	r = fira_helper_init_session(&fira_ctx, params->session_id,+				     QUWBS_FBS_SESSION_TYPE_RANGING_NO_IN_BAND_DATA, &rsp);+	if (r != QERR_SUCCESS) {+		LOG_ERR("fira_helper_init_session: %d", r);+		goto close;+	}+	session_handle = rsp.session_handle;++	/* Roles: the phone controls and initiates, we respond. */+	sp.device_type = QUWBS_FBS_DEVICE_TYPE_CONTROLEE;+	sp.device_role = QUWBS_FBS_DEVICE_ROLE_RESPONDER;++	/* From the phone, per find. */+	sp.channel_number = params->channel;+	sp.preamble_code_index = params->preamble_index;+	sp.short_addr = own_short_addr;+	sp.n_destination_short_address = 1;+	sp.destination_short_address[0] = peer_short_addr;+	memcpy(sp.vupper64, vupper64, sizeof(vupper64));++	/* One initiator, one responder, deferred DS-TWR: the FiRa shape of Android's+	 * CONFIG_UNICAST_DS_TWR. */+	sp.multi_node_mode = FIRA_MULTI_NODE_MODE_UNICAST;+	sp.ranging_round_usage = FIRA_RANGING_ROUND_USAGE_DSTWR_DEFERRED;+	sp.schedule_mode = FIRA_SCHEDULE_MODE_TIME_SCHEDULED;+	sp.rframe_config = FIRA_RFRAME_CONFIG_SP3;+	sp.sfd_id = FIRA_SFD_ID_2;+	sp.prf_mode = FIRA_PRF_MODE_BPRF;+	sp.phr_data_rate = FIRA_PRF_MODE_BPRF;+	sp.slot_duration_rstu = SLOT_DURATION_RSTU;+	sp.block_duration_ms = BLOCK_DURATION_MS;+	sp.round_duration_slots = ROUND_DURATION_SLOTS;+	sp.round_hopping = false;+	sp.block_stride_length = 0;+	sp.report_rssi = 1;+	sp.enable_diagnostics = false;++	/* Report time-of-flight; no AoA, the DW3110 has a single RF port. */+	sp.result_report_config = fira_helper_bool_to_result_report_config(true, false, false, false);+	sp.ranging_round_control = fira_helper_bool_to_ranging_round_control(true, false);++	/* Single range-only measurement step, all antenna sets left to the driver. */+	sp.meas_seq.n_steps = 1;+	sp.meas_seq.steps[0].type = FIRA_MEASUREMENT_TYPE_RANGE;+	sp.meas_seq.steps[0].n_measurements = 1;+	sp.meas_seq.steps[0].rx_ant_set_nonranging = 0xff;+	sp.meas_seq.steps[0].rx_ant_sets_ranging[0] = 0xff;+	sp.meas_seq.steps[0].rx_ant_sets_ranging[1] = 0xff;+	sp.meas_seq.steps[0].tx_ant_set_nonranging = 0xff;+	sp.meas_seq.steps[0].tx_ant_set_ranging = 0xff;++	rc = apply_session_params(&sp);+	if (rc) {+		goto deinit; 	} -	/* Responder: sit in receive and reply to the initiator's poll. The FiRa MAC-	 * timing (ranging round/block) is driven by the initiator. */-	dwt_setrxaftertxdelay(0);-	dwt_setrxtimeout(0);-	dwt_rxenable(DWT_START_RX_IMMEDIATE);+	r = uwbmac_start(uwbmac_ctx);+	if (r != QERR_SUCCESS) {+		LOG_ERR("uwbmac_start: %d", r);+		goto deinit;+	}+	r = fira_helper_start_session(&fira_ctx, session_handle);+	if (r != QERR_SUCCESS) {+		LOG_ERR("fira_helper_start_session: %d", r);+		uwbmac_stop(uwbmac_ctx);+		goto deinit;+	}  	session_active = true;+	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); 	return 0;++deinit:+	(void)fira_helper_deinit_session(&fira_ctx, session_handle);+close:+	fira_helper_close(&fira_ctx);+	return -EIO; }  void ff_uwb_stop(void)@@ -127,7 +392,10 @@ void ff_uwb_stop(void) 	if (!session_active) { 		return; 	}-	dwt_forcetrxoff();+	(void)fira_helper_stop_session(&fira_ctx, session_handle);+	(void)fira_helper_deinit_session(&fira_ctx, session_handle);+	(void)uwbmac_stop(uwbmac_ctx);+	fira_helper_close(&fira_ctx); 	session_active = false; 	LOG_INF("FiRa session stopped"); }
firmware/findfamily-tracker/uwb.conf
diff --git a/firmware/findfamily-tracker/uwb.conf b/firmware/findfamily-tracker/uwb.confnew file mode 100644index 0000000000..4d67913385--- /dev/null+++ b/firmware/findfamily-tracker/uwb.conf@@ -0,0 +1,17 @@+# Overlay config for the UWB (FiRa precision-finding) build. Not part of prj.conf so the+# BLE-only image is unaffected:+#+#   west build -b decawave_dwm3001cdk/nrf52833 firmware/findfamily-tracker \+#     -- -DEXTRA_CONF_FILE=uwb.conf \+#        -DZEPHYR_EXTRA_MODULES=<repo>/firmware/qorvo-uwb+#+# Requires the licensed Qorvo SDK via QORVO_SDK_DIR / QORVO_DRIVER_DIR. See README.md.++CONFIG_FF_TRACKER_UWB=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++# 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.txtnew file mode 100644index 0000000000..42ae10dae8--- /dev/null+++ b/firmware/qorvo-uwb/CMakeLists.txt@@ -0,0 +1,194 @@+# Zephyr module wrapping the licensed Qorvo DW3_QM33 SDK.+#+# The SDK is NOT vendored in this repo — it ships under a Qorvo licence. Point+# QORVO_SDK_DIR at an extracted copy (see README.md). Only the glue lives here:+# a Zephyr SPI backend and the platform instance definitions, because the versions+# the SDK ships are respectively a stub and written against an older Zephyr API.++if(NOT CONFIG_QORVO_UWB)+  return()+endif()++if(NOT QORVO_SDK_DIR)+  set(QORVO_SDK_DIR "$ENV{QORVO_SDK_DIR}")+endif()+if(NOT QORVO_SDK_DIR OR NOT EXISTS "${QORVO_SDK_DIR}/Libs/uwb-stack")+  message(FATAL_ERROR+    "CONFIG_QORVO_UWB=y but the Qorvo SDK was not found.\n"+    "Set -DQORVO_SDK_DIR=<path> or the QORVO_SDK_DIR environment variable to an\n"+    "extracted DW3_QM33_SDK (the directory containing Libs/uwb-stack).\n"+    "Got: '${QORVO_SDK_DIR}'\n"+    "See firmware/qorvo-uwb/README.md.")+endif()++set(UWB_STACK "${QORVO_SDK_DIR}/Libs/uwb-stack")+set(QOSAL "${UWB_STACK}/libs/qosal")+set(QHAL "${UWB_STACK}/libs/qhal")+set(QMATH "${UWB_STACK}/libs/qmath")+set(QPLATFORM "${UWB_STACK}/libs/qplatform")++# The driver ships in the Drivers package, which extracts alongside the SDK zip+# rather than inside it, so allow it to be pointed at separately.+if(NOT QORVO_DRIVER_DIR)+  set(QORVO_DRIVER_DIR "$ENV{QORVO_DRIVER_DIR}")+endif()+if(NOT QORVO_DRIVER_DIR)+  set(QORVO_DRIVER_DIR "${QORVO_SDK_DIR}/Libs/dwt_uwb_driver")+endif()+if(NOT EXISTS "${QORVO_DRIVER_DIR}/deca_device_api.h")+  message(FATAL_ERROR+    "DW3xxx driver not found at '${QORVO_DRIVER_DIR}'. Set -DQORVO_DRIVER_DIR=<path>\n"+    "to the dwt_uwb_driver directory (it contains deca_device_api.h).")+endif()++# Prebuilt FiRa bundle. nRF52833 is a Cortex-M4 with no FPU, hence soft_floating;+# picking the hard-float archive links but faults at the first float call.+file(GLOB FIRA_LIB+  "${QORVO_SDK_DIR}/Libs/uwbstack_libs/delivery/fira/Release/lib/arm-cortex-m4-soft_floating/uwbstack_bundle/libuwbstack_bundle_fira_*.a")+if(NOT FIRA_LIB)+  message(FATAL_ERROR "No arm-cortex-m4-soft_floating uwbstack bundle under ${QORVO_SDK_DIR}")+endif()+list(GET FIRA_LIB 0 FIRA_LIB)+message(STATUS "Qorvo FiRa bundle: ${FIRA_LIB}")++set(FIRA_INC+  "${QORVO_SDK_DIR}/Libs/uwbstack_libs/delivery/fira/Release/include/uwbstack_bundle")++zephyr_include_directories(+  ${FIRA_INC}+  ${QOSAL}/include+  ${QOSAL}/include/zephyr+  ${QHAL}/include+  ${QMATH}/include+  ${QPLATFORM}/include+  ${QORVO_DRIVER_DIR}++  # Qorvo's headers predate the <zephyr/...> include prefix (Zephyr 3.0) and still write+  # <kernel.h>, <device.h>, <logging/log.h>. These have to be global rather than scoped+  # to this library because the FindFamily firmware includes uwbmac/fira_helper.h, which+  # pulls the qosal headers in. include-compat/ supplies the two headers that were+  # removed outright rather than renamed.+  ${ZEPHYR_BASE}/include/zephyr+  ${CMAKE_CURRENT_LIST_DIR}/include-compat+)++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/qgpio.c redefines gpio_is_ready_dt/gpio_add_callback_dt, which+  #    Zephyr has since added itself.+  #  - 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+  #    persistent_time, which only has an nrfx implementation.+  # mcps_crypto_psa.c replaces Src/UWB/mcps_crypto.c so the image has one crypto+  # provider rather than PSA plus Nordic's nrf_crypto.+  src/qspi_zephyr.c+  src/qgpio_zephyr.c+  src/qplatform_zephyr.c+  src/qhal_extras_zephyr.c+  src/qmalloc_zephyr.c+  src/mcps_crypto_psa.c++  # qosal — Zephyr OS abstraction.+  ${QOSAL}/src/qerr_common.c+  ${QOSAL}/src/qmalloc.c+  ${QOSAL}/src/zephyr/qerr.c+  ${QOSAL}/src/zephyr/qirq.c+  ${QOSAL}/src/zephyr/qlog.c+  ${QOSAL}/src/zephyr/qmsg_queue.c+  ${QOSAL}/src/zephyr/qmutex.c+  ${QOSAL}/src/zephyr/qos.c+  ${QOSAL}/src/zephyr/qpm.c+  ${QOSAL}/src/zephyr/qprofiling.c+  ${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+  # OTP access, needed to read this module's factory calibration.+  ${QHAL}/src/qm33/qotp.c++  ${QMATH}/src/qmath.c++  # qplatform — ties the driver to qhal.+  ${QPLATFORM}/qm33_qhal_common/src/qplatform.c+  ${QPLATFORM}/qm33_qhal_common/src/qplatform_deca_impl.c++  # DW3xxx driver, DW3000 family (the DWM3001C carries a DW3110).+  ${QORVO_DRIVER_DIR}/deca_compat.c+  ${QORVO_DRIVER_DIR}/deca_interface.c+  ${QORVO_DRIVER_DIR}/deca_rsl.c+  ${QORVO_DRIVER_DIR}/dw3000/dw3000_device.c++  # Board calibration. l1_config_init() needs a reset_to_default hook that loads this+  # module's factory calibration — antenna delays, crystal trim, TX power — out of the+  # DW3110's OTP. Reusing the vendor's DWM3001CDK implementation rather than+  # reimplementing it, since those values are what make ranging accurate.+  ${QORVO_SDK_DIR}/Src/Boards/Src/Common/helper_platform_l1_config.c+  ${QORVO_SDK_DIR}/Src/Boards/Src/DWM3001CDK/platform_l1_config.c+)++zephyr_library_include_directories(+  ${QPLATFORM}/qm33_qhal_common/include+  ${QORVO_DRIVER_DIR}/dw3000+  ${QORVO_SDK_DIR}/Src/Boards/Src/Common+  ${QORVO_SDK_DIR}/Src/Boards/Src/Common_OTP+  ${QORVO_SDK_DIR}/Src/Boards/Inc+)++zephyr_library_compile_definitions(+  CONFIG_DW3000=1+  # Selects the idle-timer configuration in qm33_qhal_common/qplatform.c, which+  # #errors without a platform. The nRF branch is right for nRF52833; Zephyr owns+  # the timer instance via qhal's qtimer, so the "RTC1 is FreeRTOS" comment there+  # does not apply to us.+  CONFIG_QPLATFORM_IMPL_NRF=1+  CONFIG_UWB_SPI_INSTANCE=3+  CONFIG_UWB_SPI_FRAME_SIZE=8+  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}+  # 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+)++# Vendor code is not warning-clean under Zephyr's flags, and it is not ours to fix.+zephyr_library_compile_options(+  # See the header: fixes qos.c's missing qerr.h include, and maps qosal's Zephyr 3.1+  # log_msg2 internals onto their current names.+  -include ${CMAKE_CURRENT_LIST_DIR}/include-compat/ff_qorvo_compat.h+  -Wno-unused-function+  -Wno-unused-variable+  -Wno-unused-but-set-variable+  -Wno-sign-compare+  -Wno-implicit-fallthrough+  -Wno-missing-field-initializers+)++# Place the FiRa bundle's l1_config blob rather than leaving it an orphan section.+zephyr_linker_sources(RODATA l1_config_storage.ld)++zephyr_link_libraries(${FIRA_LIB})++# picolibc's malloc family (nano-malloc.o, nano-free.o) ends up on the link line+# alongside Zephyr's lib/libc/common allocator, and both define malloc/free. Neither is+# reached from our code — qosal's allocations go to k_malloc via src/qmalloc_zephyr.c —+# and only Zephyr's own picolibc glue references them, so one of picolibc's malloc-family+# objects is being pulled for an internal symbol rather than for malloc itself.+#+# Both implementations are functional and Zephyr's is encountered first, so the choice is+# deterministic. This is a blunt instrument though: it silences duplicate symbols for the+# whole image, so if a genuine symbol clash appears later this is the first thing to+# suspect. The real fix is to find what pulls nano-malloc.o (start with+# `-Wl,--print-map` and `-Wl,-y,__malloc_free_list`) and drop that reference.+zephyr_link_libraries(-Wl,--allow-multiple-definition)
firmware/qorvo-uwb/Kconfig
diff --git a/firmware/qorvo-uwb/Kconfig b/firmware/qorvo-uwb/Kconfignew file mode 100644index 0000000000..34ad0b635f--- /dev/null+++ b/firmware/qorvo-uwb/Kconfig@@ -0,0 +1,47 @@+# Copyright (c) 2026 Vayun Mathur+# SPDX-License-Identifier: Apache-2.0++config QORVO_UWB+	bool "Qorvo DW3xxx / QM33 UWB stack"+	select SPI+	select GPIO+	select POLL+	# The stack's frame protection (CCM*, CMAC key derivation, ECB header IEs) runs on+	# PSA via src/mcps_crypto_psa.c.+	select PSA_CRYPTO+	select PSA_WANT_KEY_TYPE_AES+	select PSA_WANT_ALG_CMAC+	select PSA_WANT_ALG_CCM+	select PSA_WANT_ALG_ECB_NO_PADDING+	select PSA_WANT_GENERATE_RANDOM+	help+	  Build the Qorvo DW3xxx driver, the Zephyr qosal/qhal backends and link the+	  prebuilt FiRa uwbstack bundle. Requires the licensed DW3_QM33 SDK, which is+	  not vendored here — point QORVO_SDK_DIR at an extracted copy. See+	  firmware/qorvo-uwb/README.md.++if QORVO_UWB++config QORVO_UWB_SPI_FREQ_SLOW_HZ+	int "SPI frequency used before the device is identified (Hz)"+	default 4000000+	help+	  The DW3000 requires <= 7 MHz until it has left INIT state, per Qorvo's+	  driver notes. 4 MHz is what the vendor SDK uses.++config QORVO_UWB_SPI_FREQ_FAST_HZ+	int "SPI frequency used once the device is up (Hz)"+	default 32000000+	help+	  Capped by the SPIM peripheral: nRF52833's SPIM3 does 32 MHz, other+	  instances only 8 MHz.++config QORVO_UWB_LOG_LEVEL+	int "Qorvo stack log level passed 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.++endif # QORVO_UWB
firmware/qorvo-uwb/dts/bindings/qorvo,dw3110.yaml
diff --git a/firmware/qorvo-uwb/dts/bindings/qorvo,dw3110.yaml b/firmware/qorvo-uwb/dts/bindings/qorvo,dw3110.yamlnew file mode 100644index 0000000000..c1f7d39e48--- /dev/null+++ b/firmware/qorvo-uwb/dts/bindings/qorvo,dw3110.yaml@@ -0,0 +1,35 @@+# Copyright (c) 2026 Vayun Mathur+# SPDX-License-Identifier: Apache-2.0++description: |+  Qorvo DW3110 UWB transceiver on SPI.++  No Zephyr driver binds to this node. It exists so the Qorvo uwbstack can resolve its+  bus, chip select, IRQ and reset lines from the devicetree rather than from compile+  definitions — see firmware/qorvo-uwb/src/qplatform_zephyr.c, which reads it with+  DT_NODELABEL(dw3110).++compatible: "qorvo,dw3110"++include: spi-device.yaml++properties:+  irq-gpios:+    type: phandle-array+    required: true+    description: |+      Transceiver IRQ line, driven high by the DW3110 on an event. Active high with a+      pull-down.++  rstn-gpios:+    type: phandle-array+    required: true+    description: |+      Active-low reset. Must be open-drain: the DW3110's RSTn is not to be driven high,+      only pulled low and released.++  wakeup-gpios:+    type: phandle-array+    required: false+    description: |+      Wake line, asserted to bring the transceiver out of deep sleep before SPI access.
firmware/qorvo-uwb/include-compat/ff_qorvo_compat.h
diff --git a/firmware/qorvo-uwb/include-compat/ff_qorvo_compat.h b/firmware/qorvo-uwb/include-compat/ff_qorvo_compat.hnew file mode 100644index 0000000000..ae35be8810--- /dev/null+++ b/firmware/qorvo-uwb/include-compat/ff_qorvo_compat.h@@ -0,0 +1,60 @@+/*+ * Compatibility shim for building Qorvo's uwb-stack against Zephyr 4.4 / NCS 3.4.+ * Force-included into the Qorvo library only (see CMakeLists.txt), never into the+ * FindFamily firmware.+ *+ * The SDK's Zephyr support was written against Zephyr ~3.1 and has two problems this+ * header fixes. This is the piece most likely to break on an NCS upgrade, because it+ * leans on Zephyr internals that carry no stability promise. If a future SDK release+ * ships a qosal built for a current Zephyr, delete this file.+ */+#ifndef FF_QORVO_COMPAT_H_+#define FF_QORVO_COMPAT_H_++/*+ * 1. qosal/src/zephyr/qos.c uses QERR_SUCCESS but only includes "qos.h", which does not+ *    pull in qerr.h. It happens to compile inside the vendor's own build because of+ *    their include ordering.+ */+#include <qerr.h>++/*+ * 2. qosal implements its own zero-copy log-message construction (see+ *    qosal/include/zephyr/qosal_impl.h) directly against Zephyr's internal logging+ *    symbols as they were named in 3.1: log_msg2, z_log_msg2_*, Z_LOG_MSG2_*. Zephyr+ *    3.2 dropped the "2" suffix throughout. The rename was mechanical — the structures+ *    and semantics are unchanged — so aliasing the old spellings onto the current ones+ *    is sufficient. Every name below was checked to exist in Zephyr 4.4's+ *    logging/log_msg.h.+ */+#ifdef CONFIG_LOG++#include <zephyr/logging/log_msg.h>++/* Message type and descriptor. */+#define log_msg2 log_msg+#define log_msg2_desc log_msg_desc++/* Packaging helpers. */+#define Z_LOG_MSG2_ALIGN_OFFSET Z_LOG_MSG_ALIGN_OFFSET+#define Z_LOG_MSG2_CBPRINTF_FLAGS Z_LOG_MSG_CBPRINTF_FLAGS+#define Z_LOG_MSG2_ALIGNED_WLEN Z_LOG_MSG_ALIGNED_WLEN++/* Allocation / finalisation. */+#define z_log_msg2_alloc z_log_msg_alloc+#define z_log_msg2_finalize z_log_msg_finalize+#define z_log_msg2_runtime_vcreate z_log_msg_runtime_vcreate+#define Z_LOG_MSG2_STACK_CREATE Z_LOG_MSG_STACK_CREATE+#define LOG_MSG2_DBG LOG_MSG_DBG++/*+ * Zephyr 3.1 had a Kconfig for the log domain id. It is now a plain constant, 0 meaning+ * the local domain (multi-domain logging gives remote domains non-zero ids).+ */+#ifndef CONFIG_LOG_DOMAIN_ID+#define CONFIG_LOG_DOMAIN_ID 0+#endif++#endif /* CONFIG_LOG */++#endif /* FF_QORVO_COMPAT_H_ */
firmware/qorvo-uwb/include-compat/random/rand32.h
diff --git a/firmware/qorvo-uwb/include-compat/random/rand32.h b/firmware/qorvo-uwb/include-compat/random/rand32.hnew file mode 100644index 0000000000..920d5884c5--- /dev/null+++ b/firmware/qorvo-uwb/include-compat/random/rand32.h@@ -0,0 +1,13 @@+/*+ * Compatibility shim: <random/rand32.h> was renamed to <zephyr/random/random.h> in+ * Zephyr 3.5 (sys_rand32_get and friends moved with it). Qorvo's qosal qrand.c still+ * includes the old path.+ *+ * See the note in ../zephyr.h about why this is scoped to the Qorvo library.+ */+#ifndef FF_COMPAT_RANDOM_RAND32_H_+#define FF_COMPAT_RANDOM_RAND32_H_++#include <zephyr/random/random.h>++#endif /* FF_COMPAT_RANDOM_RAND32_H_ */
firmware/qorvo-uwb/include-compat/zephyr.h
diff --git a/firmware/qorvo-uwb/include-compat/zephyr.h b/firmware/qorvo-uwb/include-compat/zephyr.hnew file mode 100644index 0000000000..8692012ee4--- /dev/null+++ b/firmware/qorvo-uwb/include-compat/zephyr.h@@ -0,0 +1,15 @@+/*+ * Compatibility shim: <zephyr.h> was the catch-all Zephyr header before the+ * <zephyr/...> prefix landed in Zephyr 3.0, and was removed in 3.2. Qorvo's qosal+ * Zephyr backend still includes it.+ *+ * Only reachable from the Qorvo sources — see firmware/qorvo-uwb/CMakeLists.txt, which+ * puts this directory on the include path for that library alone, so nothing in the+ * FindFamily firmware can accidentally depend on the old spelling.+ */+#ifndef FF_COMPAT_ZEPHYR_H_+#define FF_COMPAT_ZEPHYR_H_++#include <zephyr/kernel.h>++#endif /* FF_COMPAT_ZEPHYR_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.ldnew file mode 100644index 0000000000..e38ef36845--- /dev/null+++ b/firmware/qorvo-uwb/l1_config_storage.ld@@ -0,0 +1,21 @@+/*+ * Placement for the Qorvo l1_config persistent-configuration blob.+ *+ * 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.+ *+ * 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.+ */+. = ALIGN(4);+KEEP(*(.l1_config_persist_storage))+. = ALIGN(4);+KEEP(*(.l1_config_persist_storage_sha256))+. = ALIGN(4);
firmware/qorvo-uwb/src/mcps_crypto_psa.c
diff --git a/firmware/qorvo-uwb/src/mcps_crypto_psa.c b/firmware/qorvo-uwb/src/mcps_crypto_psa.cnew file mode 100644index 0000000000..3388183948--- /dev/null+++ b/firmware/qorvo-uwb/src/mcps_crypto_psa.c@@ -0,0 +1,367 @@+/*+ * MCPS crypto backend on PSA Crypto.+ *+ * The prebuilt FiRa bundle calls out to these for all 802.15.4z frame protection:+ * AES-CMAC key derivation, CCM* frame encryption/authentication, and AES-ECB for+ * header IEs. The SDK's own implementation (Src/UWB/mcps_crypto.c) forwards to a+ * HAL_crypto layer built on Nordic's old nrf_crypto, which would mean pulling a second+ * crypto provider into the image. This talks to PSA directly, which the FindFamily+ * firmware already uses for the epoch-id HMAC and the STS HKDF.+ *+ * Contexts are handed out from static pools: the stack creates a couple per session and+ * this avoids putting the heap on the ranging path.+ */+#include <qerr.h>++#include <zephyr/kernel.h>+#include <zephyr/logging/log.h>+#include <psa/crypto.h>++#include <stdbool.h>+#include <stdint.h>+#include <string.h>++LOG_MODULE_REGISTER(qorvo_crypto, CONFIG_QORVO_UWB_LOG_LEVEL);++/* 802.15.4 CCM* nonce is 13 bytes. */+#define CCM_NONCE_LEN 13+#define AES_BLOCK_LEN 16++#define MAX_CCM_CTX 4+#define MAX_ECB_CTX 4++struct key_ctx {+	psa_key_id_t key;+	bool used;+	bool encrypt; /* ECB only. */+};++static struct key_ctx ccm_ctxs[MAX_CCM_CTX];+static struct key_ctx ecb_ctxs[MAX_ECB_CTX];++static struct key_ctx *alloc_ctx(struct key_ctx *pool, size_t n)+{+	for (size_t i = 0; i < n; i++) {+		if (!pool[i].used) {+			pool[i].used = true;+			return &pool[i];+		}+	}+	return NULL;+}++static void free_ctx(struct key_ctx *ctx)+{+	if (ctx == NULL || !ctx->used) {+		return;+	}+	(void)psa_destroy_key(ctx->key);+	ctx->key = PSA_KEY_ID_NULL;+	ctx->used = false;+}++enum qerr mcps_crypto_init(void)+{+	/* Idempotent: the FindFamily firmware calls this from main() as well. */+	psa_status_t status = psa_crypto_init();++	if (status != PSA_SUCCESS) {+		LOG_ERR("psa_crypto_init: %d", (int)status);+		return QERR_EIO;+	}+	return QERR_SUCCESS;+}++void mcps_crypto_deinit(void)+{+	for (size_t i = 0; i < MAX_CCM_CTX; i++) {+		free_ctx(&ccm_ctxs[i]);+	}+	for (size_t i = 0; i < MAX_ECB_CTX; i++) {+		free_ctx(&ecb_ctxs[i]);+	}+}++enum qerr mcps_crypto_reinit(void)+{+	return QERR_SUCCESS;+}++uint32_t mcps_crypto_get_random(void)+{+	uint32_t v = 0;++	if (psa_generate_random((uint8_t *)&v, sizeof(v)) != PSA_SUCCESS) {+		/* Only reachable if the RNG driver is missing, which would already have+		 * failed psa_crypto_init. Zero is not a safe nonce, so say so loudly. */+		LOG_ERR("psa_generate_random failed");+	}+	return v;+}++/* ---- AES-CMAC ---------------------------------------------------------- */++static enum qerr cmac_digest(const uint8_t *key, size_t key_bits, const uint8_t *data,+			     unsigned int data_len, uint8_t *out)+{+	psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;+	psa_key_id_t key_id = PSA_KEY_ID_NULL;+	psa_status_t status;+	size_t out_len = 0;++	psa_set_key_usage_flags(&attr, PSA_KEY_USAGE_SIGN_MESSAGE);+	psa_set_key_algorithm(&attr, PSA_ALG_CMAC);+	psa_set_key_type(&attr, PSA_KEY_TYPE_AES);+	psa_set_key_bits(&attr, key_bits);++	status = psa_import_key(&attr, key, key_bits / 8, &key_id);+	if (status != PSA_SUCCESS) {+		LOG_ERR("cmac import_key: %d", (int)status);+		return QERR_EIO;+	}+	status = psa_mac_compute(key_id, PSA_ALG_CMAC, data, data_len, out, AES_BLOCK_LEN,+				 &out_len);+	(void)psa_destroy_key(key_id);+	if (status != PSA_SUCCESS) {+		LOG_ERR("psa_mac_compute: %d", (int)status);+		return QERR_EIO;+	}+	return QERR_SUCCESS;+}++enum qerr mcps_crypto_cmac_aes_128_digest(const uint8_t *key, const uint8_t *data,+					  unsigned int data_len, uint8_t *out)+{+	return cmac_digest(key, 128, data, data_len, out);+}++enum qerr mcps_crypto_cmac_aes_256_digest(const uint8_t *key, const uint8_t *data,+					  unsigned int data_len, uint8_t *out)+{+	return cmac_digest(key, 256, data, data_len, out);+}++/* ---- AES-CCM* ---------------------------------------------------------- */++int mcps_crypto_aead_aes_ccm_star_128_create(void **ccm_star_ctx, const uint8_t *key)+{+	psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;+	struct key_ctx *ctx;+	psa_status_t status;++	if (ccm_star_ctx == NULL || key == NULL) {+		return QERR_EINVAL;+	}+	ctx = alloc_ctx(ccm_ctxs, MAX_CCM_CTX);+	if (ctx == NULL) {+		LOG_ERR("out of CCM contexts");+		return QERR_ENOMEM;+	}++	psa_set_key_usage_flags(&attr, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);+	/* The MAC length varies per frame, so permit any tag from 4 bytes up rather than+	 * pinning one length in the key policy. */+	psa_set_key_algorithm(&attr, PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(PSA_ALG_CCM, 4));+	psa_set_key_type(&attr, PSA_KEY_TYPE_AES);+	psa_set_key_bits(&attr, 128);++	status = psa_import_key(&attr, key, 16, &ctx->key);+	if (status != PSA_SUCCESS) {+		LOG_ERR("ccm import_key: %d", (int)status);+		ctx->used = false;+		return QERR_EIO;+	}+	*ccm_star_ctx = ctx;+	return QERR_SUCCESS;+}++void mcps_crypto_aead_aes_ccm_star_128_destroy(void *ctx)+{+	free_ctx((struct key_ctx *)ctx);+}++/*+ * CCM* keeps the MAC separate from the ciphertext, whereas psa_aead_encrypt returns them+ * concatenated, so the multipart API is used to get the tag on its own.+ *+ * mac_len == 0 (CCM* security level "encryption only", where CCM degenerates to plain+ * AES-CTR) is not implemented: PSA's CCM requires a tag of at least 4 bytes. FiRa STS+ * frames use a 64-bit MIC, so this should not be reached — and an explicit error is+ * better than silently mis-encrypting a frame.+ */+static int ccm_crypt(void *ctx_, const uint8_t *nonce, const uint8_t *header,+		     unsigned int header_len, const uint8_t *in, unsigned int data_len,+		     uint8_t *out, uint8_t *mac, unsigned int mac_len, bool encrypt)+{+	struct key_ctx *ctx = ctx_;+	psa_aead_operation_t op = PSA_AEAD_OPERATION_INIT;+	psa_algorithm_t alg;+	psa_status_t status;+	size_t out_len = 0;+	size_t tag_len = 0;++	if (ctx == NULL || !ctx->used || nonce == NULL) {+		return QERR_EINVAL;+	}+	if (mac_len == 0) {+		LOG_ERR("CCM* with a zero-length MIC is not supported");+		return QERR_ENOTSUP;+	}++	alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, mac_len);+	status = encrypt ? psa_aead_encrypt_setup(&op, ctx->key, alg)+			 : psa_aead_decrypt_setup(&op, ctx->key, alg);+	if (status != PSA_SUCCESS) {+		goto err;+	}+	status = psa_aead_set_lengths(&op, header_len, data_len);+	if (status != PSA_SUCCESS) {+		goto err;+	}+	status = psa_aead_set_nonce(&op, nonce, CCM_NONCE_LEN);+	if (status != PSA_SUCCESS) {+		goto err;+	}+	if (header_len) {+		status = psa_aead_update_ad(&op, header, header_len);+		if (status != PSA_SUCCESS) {+			goto err;+		}+	}+	if (data_len) {+		status = psa_aead_update(&op, in, data_len, out, data_len, &out_len);+		if (status != PSA_SUCCESS) {+			goto err;+		}+	}+	if (encrypt) {+		status = psa_aead_finish(&op, NULL, 0, &out_len, mac, mac_len, &tag_len);+	} else {+		status = psa_aead_verify(&op, NULL, 0, &out_len, mac, mac_len);+	}+	if (status != PSA_SUCCESS) {+		goto err;+	}+	return QERR_SUCCESS;++err:+	/* A decrypt failure here is usually an authentication failure, which on the+	 * ranging path means the peer's STS key doesn't match ours. */+	LOG_ERR("CCM* %s failed: %d", encrypt ? "encrypt" : "decrypt", (int)status);+	(void)psa_aead_abort(&op);+	return QERR_EIO;+}++int mcps_crypto_aead_aes_ccm_star_128_encrypt(void *ctx, const uint8_t *nonce,+					      const uint8_t *header, unsigned int header_len,+					      uint8_t *data, unsigned int data_len, uint8_t *mac,+					      unsigned int mac_len)+{+	/* In place: PSA permits in == out for CCM. */+	return ccm_crypt(ctx, nonce, header, header_len, data, data_len, data, mac, mac_len,+			 true);+}++int mcps_crypto_aead_aes_ccm_star_128_encrypt_inout(void *ctx, const uint8_t *nonce,+						    const uint8_t *header,+						    unsigned int header_len, uint8_t *data,+						    unsigned int data_len, uint8_t *out,+						    uint8_t *mac, unsigned int mac_len)+{+	return ccm_crypt(ctx, nonce, header, header_len, data, data_len, out, mac, mac_len,+			 true);+}++int mcps_crypto_aead_aes_ccm_star_128_decrypt(void *ctx, const uint8_t *nonce,+					      const uint8_t *header, unsigned int header_len,+					      uint8_t *data, unsigned int data_len, uint8_t *mac,+					      unsigned int mac_len)+{+	return ccm_crypt(ctx, nonce, header, header_len, data, data_len, data, mac, mac_len,+			 false);+}++int mcps_crypto_aead_aes_ccm_star_128_decrypt_inout(void *ctx, const uint8_t *nonce,+						    const uint8_t *header,+						    unsigned int header_len, uint8_t *data,+						    unsigned int data_len, uint8_t *out,+						    uint8_t *mac, unsigned int mac_len)+{+	return ccm_crypt(ctx, nonce, header, header_len, data, data_len, out, mac, mac_len,+			 false);+}++/* ---- AES-ECB ----------------------------------------------------------- */++static int ecb_create(void **ecb_ctx, const uint8_t *key, bool encrypt)+{+	psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;+	struct key_ctx *ctx;+	psa_status_t status;++	if (ecb_ctx == NULL || key == NULL) {+		return QERR_EINVAL;+	}+	ctx = alloc_ctx(ecb_ctxs, MAX_ECB_CTX);+	if (ctx == NULL) {+		LOG_ERR("out of ECB contexts");+		return QERR_ENOMEM;+	}+	ctx->encrypt = encrypt;++	psa_set_key_usage_flags(&attr, encrypt ? PSA_KEY_USAGE_ENCRYPT : PSA_KEY_USAGE_DECRYPT);+	psa_set_key_algorithm(&attr, PSA_ALG_ECB_NO_PADDING);+	psa_set_key_type(&attr, PSA_KEY_TYPE_AES);+	psa_set_key_bits(&attr, 128);++	status = psa_import_key(&attr, key, 16, &ctx->key);+	if (status != PSA_SUCCESS) {+		LOG_ERR("ecb import_key: %d", (int)status);+		ctx->used = false;+		return QERR_EIO;+	}+	*ecb_ctx = ctx;+	return QERR_SUCCESS;+}++int mcps_crypto_aes_ecb_128_create_encrypt(void **ecb_ctx, const uint8_t *key)+{+	return ecb_create(ecb_ctx, key, true);+}++int mcps_crypto_aes_ecb_128_create_decrypt(void **ecb_ctx, const uint8_t *key)+{+	return ecb_create(ecb_ctx, key, false);+}++void mcps_crypto_aes_ecb_128_destroy(void *ctx)+{+	free_ctx((struct key_ctx *)ctx);+}++int mcps_crypto_aes_ecb_128_encrypt_decrypt(void *ctx_, const uint8_t *data,+					    unsigned int data_len, uint8_t *out)+{+	struct key_ctx *ctx = ctx_;+	psa_status_t status;+	size_t out_len = 0;++	if (ctx == NULL || !ctx->used || data == NULL || out == NULL) {+		return QERR_EINVAL;+	}+	if (data_len % AES_BLOCK_LEN) {+		return QERR_EINVAL;+	}+	/* ECB has no IV, so psa_cipher_* neither prepends nor consumes one and the+	 * output is the same length as the input. */+	status = ctx->encrypt ? psa_cipher_encrypt(ctx->key, PSA_ALG_ECB_NO_PADDING, data,+						   data_len, out, data_len, &out_len)+			      : psa_cipher_decrypt(ctx->key, PSA_ALG_ECB_NO_PADDING, data,+						   data_len, out, data_len, &out_len);+	if (status != PSA_SUCCESS) {+		LOG_ERR("psa_cipher_%s: %d", ctx->encrypt ? "encrypt" : "decrypt",+			(int)status);+		return QERR_EIO;+	}+	return QERR_SUCCESS;+}
firmware/qorvo-uwb/src/qgpio_zephyr.c
diff --git a/firmware/qorvo-uwb/src/qgpio_zephyr.c b/firmware/qorvo-uwb/src/qgpio_zephyr.cnew file mode 100644index 0000000000..2d1871dbfc--- /dev/null+++ b/firmware/qorvo-uwb/src/qgpio_zephyr.c@@ -0,0 +1,216 @@+/*+ * Zephyr GPIO backend for Qorvo's qhal.+ *+ * Replaces qhal/src/zephyr/qgpio.c, which cannot be compiled against Zephyr 4.4: it+ * defines its own `gpio_is_ready_dt` and `gpio_add_callback_dt` helpers, which Zephyr+ * has since added to gpio.h, so the two collide. Renaming them from the command line+ * doesn't help — the rename would hit Zephyr's definitions too.+ *+ * qplatform supplies each pin as a `gpio_dt_spec` in `qgpio.dev` (see+ * qplatform_zephyr.c); `port` and `pin_number` are unused on Zephyr.+ *+ * qgpio's flag bits are NOT the same as Zephyr's despite reading similarly — for+ * instance QGPIO_PULL_UP is BIT(2) where GPIO_PULL_UP is BIT(4) — so every flag is+ * translated explicitly rather than passed through.+ */+#include <qerr.h>+#include <qgpio.h>++#include <zephyr/device.h>+#include <zephyr/drivers/gpio.h>+#include <zephyr/kernel.h>+#include <zephyr/logging/log.h>++LOG_MODULE_REGISTER(qorvo_qgpio, CONFIG_QORVO_UWB_LOG_LEVEL);++/* The stack only ever registers a callback on the transceiver IRQ line. */+#define MAX_CALLBACKS 2++struct cb_entry {+	const struct gpio_dt_spec *spec;+	struct gpio_callback zephyr_cb;+	qgpio_irq_cb cb;+	void *arg;+	bool used;+};++static struct cb_entry callbacks[MAX_CALLBACKS];++static const struct gpio_dt_spec *spec_of(const struct qgpio *pin)+{+	if (pin == NULL || pin->dev == NULL) {+		return NULL;+	}+	return (const struct gpio_dt_spec *)pin->dev;+}++static gpio_flags_t translate_config(uint32_t flags)+{+	gpio_flags_t out = 0;++	if (flags & QGPIO_INPUT) {+		out |= GPIO_INPUT;+	}+	if (flags & INTERNAL_QGPIO_OUTPUT) {+		out |= GPIO_OUTPUT;+		/* Initial level, so a reset line never glitches to the wrong state. */+		if (flags & INTERNAL_QGPIO_OUTPUT_INIT_HIGH) {+			out |= GPIO_OUTPUT_INIT_HIGH;+		} else if (flags & INTERNAL_QGPIO_OUTPUT_INIT_LOW) {+			out |= GPIO_OUTPUT_INIT_LOW;+		}+	}+	if (flags & QGPIO_PULL_UP) {+		out |= GPIO_PULL_UP;+	}+	if (flags & QGPIO_PULL_DOWN) {+		out |= GPIO_PULL_DOWN;+	}+	if (flags & QGPIO_SINGLE_ENDED) {+		out |= GPIO_SINGLE_ENDED;+		/* The DW3110's RSTn is open drain: pulled low, released, never driven. */+		out |= (flags & QGPIO_LINE_OPEN_DRAIN) ? GPIO_LINE_OPEN_DRAIN+						       : GPIO_LINE_OPEN_SOURCE;+	}+	return out;+}++static gpio_flags_t translate_irq(uint32_t flags)+{+	if (flags & INTERNAL_QGPIO_INT_DISABLE) {+		return GPIO_INT_DISABLE;+	}+	if (!(flags & INTERNAL_QGPIO_INT_ENABLE)) {+		return GPIO_INT_DISABLE;+	}+	if (flags & INTERNAL_QGPIO_INT_EDGE) {+		const bool low = flags & INTERNAL_QGPIO_INT_LOW;+		const bool high = flags & INTERNAL_QGPIO_INT_HIGH;++		if (low && high) {+			return GPIO_INT_EDGE_BOTH;+		}+		return high ? GPIO_INT_EDGE_RISING : GPIO_INT_EDGE_FALLING;+	}+	return (flags & INTERNAL_QGPIO_INT_HIGH) ? GPIO_INT_LEVEL_HIGH : GPIO_INT_LEVEL_LOW;+}++enum qerr qgpio_pin_configure(const struct qgpio *qgpio_pin, uint32_t flags)+{+	const struct gpio_dt_spec *spec = spec_of(qgpio_pin);+	int rc;++	if (spec == NULL) {+		return QERR_EINVAL;+	}+	if (!gpio_is_ready_dt(spec)) {+		LOG_ERR("GPIO port not ready");+		return QERR_ENODEV;+	}+	rc = gpio_pin_configure_dt(spec, translate_config(flags));+	if (rc < 0) {+		LOG_ERR("gpio_pin_configure_dt: %d", rc);+		return QERR_EIO;+	}+	return QERR_SUCCESS;+}++enum qerr qgpio_pin_write(const struct qgpio *qgpio_pin, uint8_t value)+{+	const struct gpio_dt_spec *spec = spec_of(qgpio_pin);++	if (spec == NULL) {+		return QERR_EINVAL;+	}+	return gpio_pin_set_dt(spec, value ? 1 : 0) < 0 ? QERR_EIO : QERR_SUCCESS;+}++enum qerr qgpio_pin_read(const struct qgpio *qgpio_pin, uint8_t *value)+{+	const struct gpio_dt_spec *spec = spec_of(qgpio_pin);+	int rc;++	if (spec == NULL || value == NULL) {+		return QERR_EINVAL;+	}+	rc = gpio_pin_get_dt(spec);+	if (rc < 0) {+		return QERR_EIO;+	}+	*value = (uint8_t)rc;+	return QERR_SUCCESS;+}++enum qerr qgpio_pin_irq_configure(const struct qgpio *qgpio_pin, uint32_t flags)+{+	const struct gpio_dt_spec *spec = spec_of(qgpio_pin);+	int rc;++	if (spec == NULL) {+		return QERR_EINVAL;+	}+	rc = gpio_pin_interrupt_configure_dt(spec, translate_irq(flags));+	if (rc < 0) {+		LOG_ERR("gpio_pin_interrupt_configure_dt: %d", rc);+		return QERR_EIO;+	}+	return QERR_SUCCESS;+}++static void on_gpio_event(const struct device *port, struct gpio_callback *cb, uint32_t pins)+{+	struct cb_entry *entry = CONTAINER_OF(cb, struct cb_entry, zephyr_cb);++	ARG_UNUSED(port);+	ARG_UNUSED(pins);++	if (entry->cb) {+		entry->cb(entry->arg);+	}+}++enum qerr qgpio_pin_irq_set_callback(const struct qgpio *qgpio_pin, qgpio_irq_cb cb, void *arg)+{+	const struct gpio_dt_spec *spec = spec_of(qgpio_pin);+	struct cb_entry *entry = NULL;+	int rc;++	if (spec == NULL) {+		return QERR_EINVAL;+	}++	/* Re-registering the same pin replaces its handler rather than leaking a slot. */+	for (int i = 0; i < MAX_CALLBACKS; i++) {+		if (callbacks[i].used && callbacks[i].spec == spec) {+			entry = &callbacks[i];+			break;+		}+	}+	if (entry == NULL) {+		for (int i = 0; i < MAX_CALLBACKS; i++) {+			if (!callbacks[i].used) {+				entry = &callbacks[i];+				break;+			}+		}+	}+	if (entry == NULL) {+		LOG_ERR("no free GPIO callback slot");+		return QERR_ENOMEM;+	}++	if (entry->used) {+		(void)gpio_remove_callback_dt(spec, &entry->zephyr_cb);+	}+	entry->spec = spec;+	entry->cb = cb;+	entry->arg = arg;+	gpio_init_callback(&entry->zephyr_cb, on_gpio_event, BIT(spec->pin));+	rc = gpio_add_callback_dt(spec, &entry->zephyr_cb);+	if (rc < 0) {+		LOG_ERR("gpio_add_callback_dt: %d", rc);+		return QERR_EIO;+	}+	entry->used = true;+	return QERR_SUCCESS;+}
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.cnew file mode 100644index 0000000000..d1af800836--- /dev/null+++ b/firmware/qorvo-uwb/src/qhal_extras_zephyr.c@@ -0,0 +1,169 @@+/*+ * Remaining qhal pieces for Zephyr: transceiver power control, the RTC the stack times+ * ranging blocks against, and flash persistence.+ *+ * The SDK's versions can't be used here:+ *  - qhal/src/qm33/qpwr.c duplicates the SPI config and repeats the `dw35720` node-label+ *    requirement and the `spi_config.cs->gpio` pointer dereference (see+ *    qplatform_zephyr.c).+ *  - qhal/src/qrtc.c and qpwr.c delegate to persistent_time / persistent_config, which+ *    only have nrfx implementations. Those drive an nRF RTC instance directly, which on+ *    Zephyr would contend with the kernel's own timer, so the RTC is implemented on+ *    Zephyr's clock instead.+ */+#include <qerr.h>+#include <qgpio.h>+#include <qirq.h>+#include <qpwr.h>+#include <qrtc.h>+#include <qspi.h>+#include <qtime.h>++#include <deca_device_api.h>++#include <zephyr/drivers/gpio.h>+#include <zephyr/kernel.h>+#include <zephyr/logging/log.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. */+extern struct qspi_config qm33_qspi_config;++/* Time the DW3000 needs CS held low to wake, and then to become responsive. */+#define WAKEUP_CS_TOGGLE_US 400+#define WAKEUP_DELAY_US 500++/*+ * The DW3000 powers up asleep. dwt_probe() calls the wakeup path during probing, which+ * is what clears this.+ */+static bool uwb_sleeping = true;+static bool lpm_enabled;++/* ---- Transceiver power ------------------------------------------------- */++enum qerr qpwr_uwb_sleep(void)+{+	int key = qirq_lock();++	if (!uwb_sleeping) {+		dwt_entersleep(DWT_DW_IDLE_RC);+		uwb_sleeping = true;+	}+	qirq_unlock(key);+	return QERR_SUCCESS;+}++enum qerr qpwr_uwb_wakeup(void)+{+	int key = qirq_lock();++	if (uwb_sleeping) {+		/*+		 * Waking the DW3000 is a CS pulse, not an SPI transfer: hold CS low long+		 * enough and the part comes up. Zephyr's SPI driver owns this pin, so it+		 * is briefly driven by hand here and left deasserted afterwards.+		 *+		 * Driven through gpio_pin_set_dt rather than qgpio's OUTPUT_LOW/HIGH+		 * flags: the devicetree marks cs-gpios GPIO_ACTIVE_LOW, so Zephyr inverts+		 * logical levels for this pin and "output low" would idle it physically+		 * high — the opposite of the pulse the part needs. Level 1 here means+		 * asserted, i.e. physically low.+		 */+		const struct gpio_dt_spec *cs = qm33_qspi_config.cs_pin.dev;++		if (cs != NULL) {+			(void)gpio_pin_configure_dt(cs, GPIO_OUTPUT_ACTIVE);+			qtime_usleep(WAKEUP_CS_TOGGLE_US);+			(void)gpio_pin_set_dt(cs, 0);+			qtime_usleep(WAKEUP_DELAY_US);+		}+		uwb_sleeping = false;+	}+	qirq_unlock(key);+	return QERR_SUCCESS;+}++bool qpwr_uwb_is_sleeping(void)+{+	return uwb_sleeping;+}++void qpwr_enable_lpm(void)+{+	lpm_enabled = true;+}++void qpwr_disable_lpm(void)+{+	lpm_enabled = false;+}++bool qpwr_is_lpm_enabled(void)+{+	return lpm_enabled;+}++enum qerr qpwr_set_min_inactivity_s4(uint32_t time_ms)+{+	ARG_UNUSED(time_ms);+	return QERR_ENOTSUP;+}++enum qerr qpwr_get_min_inactivity_s4(uint32_t *time_ms)+{+	ARG_UNUSED(time_ms);+	return QERR_ENOTSUP;+}++/* ---- RTC -------------------------------------------------------------- */++/*+ * The stack uses this to convert between its own device time units and wall time when+ * scheduling ranging blocks, so it needs to be monotonic and reasonably fine-grained+ * rather than accurate in absolute terms. Zephyr's uptime is LFCLK-backed and keeps+ * counting through sleep, which is what matters.+ */+int64_t qrtc_get_us(void)+{+	return (int64_t)k_ticks_to_us_floor64(k_uptime_ticks());+}++void qrtc_resync_rtc_systime(int64_t *rtc_us, uint32_t *systime)+{+	if (rtc_us) {+		*rtc_us = qrtc_get_us();+	}+	if (systime) {+		/*+		 * "systime" is the DW3000's own 32-bit system-time counter. The driver+		 * reads it directly, so there is nothing to correlate here beyond+		 * reporting the current RTC; a deep-sleep-across-reset resync would need+		 * the persistent_time machinery this deliberately does not use.+		 */+		*systime = 0;+	}+}++void qrtc_update_rtc_systime(int64_t updated_rtc_us, uint32_t updated_systime)+{+	ARG_UNUSED(updated_rtc_us);+	ARG_UNUSED(updated_systime);+}++/* ---- 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);+	/*+	 * 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.+	 */+	return QERR_ENOTSUP;+}
firmware/qorvo-uwb/src/qmalloc_zephyr.c
diff --git a/firmware/qorvo-uwb/src/qmalloc_zephyr.c b/firmware/qorvo-uwb/src/qmalloc_zephyr.cnew file mode 100644index 0000000000..eddf4f0392--- /dev/null+++ b/firmware/qorvo-uwb/src/qmalloc_zephyr.c@@ -0,0 +1,52 @@+/*+ * qosal allocator backend on Zephyr's kernel heap.+ *+ * Replaces qosal/src/zephyr/qmalloc.c, which implements these on libc malloc. That+ * pulls a libc malloc provider onto the link line, and in a picolibc build two of them+ * offer one — picolibc's own nano-malloc and Zephyr's lib/libc/common — which collide.+ * Disabling Zephyr's instead leaves picolibc's wanting __heap_start/__heap_end, symbols+ * Zephyr only emits for its own allocator.+ *+ * Going to k_malloc sidesteps the whole question and puts UWB allocations in the+ * kernel heap (CONFIG_HEAP_MEM_POOL_SIZE), where they are sized and accounted with+ * everything else rather than in a second, invisible heap.+ */+#include "qmalloc.h"++#include <zephyr/kernel.h>++/*+ * Per-quota budgets consumed by qosal's wrappers in qosal/src/qmalloc.c. Index 0 is+ * MEM_QUOTA_ID_INFINITE. The optional CONFIG_MEM_QUOTA_ID* knobs are not set here, so+ * the stack runs with a single unbounded quota and the kernel heap is the real limit.+ */+uint32_t allocation_quotas[] = {+	~0U,+#ifdef CONFIG_MEM_QUOTA_ID1+	CONFIG_MEM_QUOTA_ID1,+#endif+#ifdef CONFIG_MEM_QUOTA_ID2+	CONFIG_MEM_QUOTA_ID2,+#endif+#ifdef CONFIG_MEM_QUOTA_ID3+	CONFIG_MEM_QUOTA_ID3,+#endif+#ifdef CONFIG_MEM_QUOTA_ID4+	CONFIG_MEM_QUOTA_ID4,+#endif+};++void *qmalloc_internal(size_t size)+{+	return k_malloc(size);+}++void *qrealloc_internal(void *ptr, size_t new_size)+{+	return k_realloc(ptr, new_size);+}++void qfree_internal(void *ptr)+{+	k_free(ptr);+}
firmware/qorvo-uwb/src/qplatform_zephyr.c
diff --git a/firmware/qorvo-uwb/src/qplatform_zephyr.c b/firmware/qorvo-uwb/src/qplatform_zephyr.cnew file mode 100644index 0000000000..8437ffea0e--- /dev/null+++ b/firmware/qorvo-uwb/src/qplatform_zephyr.c@@ -0,0 +1,100 @@+/*+ * Platform instance definitions for Qorvo's qplatform on Zephyr.+ *+ * Replaces qplatform/qm33_qhal_zephyr/src/qplatform.c, which cannot be used as+ * shipped: it dereferences `spi_config.cs` as a pointer (it has been a by-value+ * struct since Zephyr 3.5) and hard-#errors unless the devicetree happens to label+ * its UWB node `dw35720` — a DW3572, where this board carries a DW3110.+ *+ * The four externs below are what qm33_qhal_common/src/qplatform.c consumes; see+ * qplatform_internal.h. Everything comes from the devicetree so the pin mapping+ * lives in the board overlay rather than in compile definitions.+ */+#include "qplatform_internal.h"++#include <qgpio.h>+#include <qspi.h>++#include <zephyr/devicetree.h>+#include <zephyr/drivers/gpio.h>+#include <zephyr/drivers/spi.h>+#include <zephyr/sys/byteorder.h>++#define UWB_NODE DT_NODELABEL(dw3110)++#if !DT_NODE_HAS_STATUS(UWB_NODE, okay)+#error "Enable a 'dw3110' SPI node with irq-gpios and rstn-gpios in the board overlay. \+See firmware/findfamily-tracker/boards/decawave_dwm3001cdk_nrf52833.overlay."+#endif++static const struct gpio_dt_spec rstn_pin_spec = GPIO_DT_SPEC_GET(UWB_NODE, rstn_gpios);+static const struct gpio_dt_spec irq_pin_spec = GPIO_DT_SPEC_GET(UWB_NODE, irq_gpios);+static const struct gpio_dt_spec cs_pin_spec = GPIO_DT_SPEC_GET(DT_BUS(UWB_NODE), cs_gpios);++/*+ * qhal's Zephyr qgpio backend keys entirely off `dev` (a gpio_dt_spec); port and+ * pin_number are unused there and left zero.+ */+const struct qgpio qm33_rstn = {+	.dev = (void *)&rstn_pin_spec,+	.port = 0,+	.pin_number = 0,+};++const struct qgpio qm33_irq = {+	.dev = (void *)&irq_pin_spec,+	.port = 0,+	.pin_number = 0,+};++const struct qspi_instance qm33_qspi_instance = {+	.instance_number = CONFIG_UWB_SPI_INSTANCE,+	.dev = DEVICE_DT_GET(DT_BUS(UWB_NODE)),+};++struct qspi_config qm33_qspi_config = {+	/* SCK/MOSI/MISO are owned by Zephyr's pinctrl, so qhal needs no descriptors. */+	.sck_pin = { .port = 0, .pin_number = 0, .dev = NULL },+	.mosi_pin = { .port = 0, .pin_number = 0, .dev = NULL },+	.miso_pin = { .port = 0, .pin_number = 0, .dev = NULL },+	.cs_pin = { .port = 0, .pin_number = 0, .dev = (void *)&cs_pin_spec },+	/* Slow until the device is identified; the driver raises it afterwards. */+	.freq_hz = CONFIG_SPI_UWB_SLOW_RATE_FREQ,+	.irq_priority = CONFIG_SPI_UWB_IRQ_PRIORITY,+	/*+	 * SPI mode 0. Deliberately not the SDK's QSPI_OP_FLAGS, which sets QSPI_CPOL:+	 * the DW3000 datasheet specifies mode 0 (CPOL=0, CPHA=0), and mode 2 is what+	 * the nrfx backend's flag handling happens to paper over.+	 */+	.op_flags = QSPI_MASTER | QSPI_MSB_FIRST | QSPI_MISO_SINGLE |+		    QSPI_SET_FRAME_LEN(CONFIG_UWB_SPI_FRAME_SIZE),+};++/*+ * Bring-up diagnostic: read the transceiver's DEV_ID register straight through Zephyr's+ * SPI API, bypassing the Qorvo driver entirely. A DW3110 answers 0xDECA0302.+ *+ * This exists because "qplatform_init failed" is ambiguous — it cannot distinguish a+ * miswired bus or a wrong SPI mode from the driver being unhappy about something it read.+ * A correct id here means the bus is fine and the problem is above it.+ *+ * 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.+ */+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) };+	const struct spi_buf rx_buf = { .buf = rx, .len = sizeof(rx) };+	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) {+		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.cnew file mode 100644index 0000000000..3870b088cf--- /dev/null+++ b/firmware/qorvo-uwb/src/qspi_zephyr.c@@ -0,0 +1,159 @@+/*+ * Zephyr SPI backend for Qorvo's qhal.+ *+ * The SDK ships qhal/src/zephyr/qspi.c, but every function in it returns+ * QERR_ENOTSUP — it is a placeholder, not an implementation. This is the real one.+ *+ * The DW3000 is only ever driven full-duplex with equal-length buffers: the+ * qplatform layer concatenates the command header and the body into one buffer and+ * passes tx_size == rx_size (see qm33_qhal_common/src/qplatform.c,+ * qplatform_uwb_spi_read/write). So a single spi_transceive with one tx and one rx+ * buffer covers every transfer the stack makes.+ */+#include <qerr.h>+#include <qgpio.h>+#include <qspi.h>++#include <zephyr/device.h>+#include <zephyr/drivers/spi.h>+#include <zephyr/kernel.h>+#include <zephyr/logging/log.h>++#include <string.h>++LOG_MODULE_REGISTER(qorvo_qspi, CONFIG_QORVO_UWB_LOG_LEVEL);++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;+	 * we always transfer synchronously and invoke it on completion, which satisfies+	 * the contract (qspi.h: "If NULL, synchronous mode is used") without needing+	 * SPI_ASYNC and its extra buffering.+	 */+	qspi_xfer_cb done_cb;+	void *done_arg;+};++/* One UWB transceiver per board; a static instance avoids needing an allocator here. */+static struct qspi the_spi;++struct qspi *qspi_open(const struct qspi_instance *instance)+{+	const struct device *dev;++	if (instance == NULL || instance->dev == NULL) {+		LOG_ERR("qspi_open: no device in instance");+		return NULL;+	}+	if (the_spi.in_use) {+		LOG_ERR("qspi_open: already open");+		return NULL;+	}+	dev = (const struct device *)instance->dev;+	if (!device_is_ready(dev)) {+		LOG_ERR("qspi_open: SPI device %s not ready", dev->name);+		return NULL;+	}++	memset(&the_spi, 0, sizeof(the_spi));+	the_spi.dev = dev;+	the_spi.in_use = true;+	return &the_spi;+}++enum qerr qspi_close(struct qspi *spi)+{+	if (spi == NULL) {+		return QERR_EINVAL;+	}+	spi->in_use = false;+	spi->dev = NULL;+	return QERR_SUCCESS;+}++enum qerr qspi_configure(struct qspi *spi, const struct qspi_config *config)+{+	spi_operation_t op;++	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.+	 */+	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;+	}++	LOG_DBG("qspi_configure: %u Hz, op=0x%08x", config->freq_hz, (unsigned int)op);+	return QERR_SUCCESS;+}++enum qerr qspi_irq_set_callback(struct qspi *spi, qspi_xfer_cb handler, void *arg)+{+	if (spi == NULL) {+		return QERR_EINVAL;+	}+	spi->done_cb = handler;+	spi->done_arg = arg;+	return QERR_SUCCESS;+}++enum qerr qspi_transceive(struct qspi *spi, const struct qspi_transfer *xfer)+{+	struct spi_buf tx_buf;+	struct spi_buf rx_buf;+	struct spi_buf_set tx_set = { .buffers = &tx_buf, .count = 1 };+	struct spi_buf_set rx_set = { .buffers = &rx_buf, .count = 1 };+	int rc;++	if (spi == NULL || xfer == NULL || spi->dev == NULL) {+		return QERR_EINVAL;+	}++	tx_buf.buf = xfer->tx_buf;+	tx_buf.len = xfer->tx_size;+	rx_buf.buf = xfer->rx_buf;+	rx_buf.len = xfer->rx_size;++	rc = spi_transceive(spi->dev, &spi->cfg,+			    xfer->tx_buf ? &tx_set : NULL,+			    xfer->rx_buf ? &rx_set : NULL);+	if (rc < 0) {+		LOG_ERR("spi_transceive failed: %d", rc);+		return QERR_EIO;+	}++	if (spi->done_cb) {+		spi->done_cb(spi->done_arg);+	}+	return QERR_SUCCESS;+}
firmware/qorvo-uwb/zephyr/module.yml
diff --git a/firmware/qorvo-uwb/zephyr/module.yml b/firmware/qorvo-uwb/zephyr/module.ymlnew file mode 100644index 0000000000..b96ab73417--- /dev/null+++ b/firmware/qorvo-uwb/zephyr/module.yml@@ -0,0 +1,7 @@+name: qorvo-uwb+build:+  cmake: .+  kconfig: Kconfig+  settings:+    # Exposes dts/bindings/qorvo,dw3110.yaml to the devicetree compiler.+    dts_root: .