Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / firmware / qorvo-uwb / src / qhal_extras_zephyr.c
qhal_extras_zephyr.c213 lines · 6.9 KiBRaw
| 1 | /* |
| 2 | * Remaining qhal pieces for Zephyr: transceiver power control, the RTC the stack times |
| 3 | * ranging blocks against, and flash persistence. |
| 4 | * |
| 5 | * The SDK's versions can't be used here: |
| 6 | * - qhal/src/qm33/qpwr.c duplicates the SPI config and repeats the `dw35720` node-label |
| 7 | * requirement and the `spi_config.cs->gpio` pointer dereference (see |
| 8 | * qplatform_zephyr.c). |
| 9 | * - qhal/src/qrtc.c and qpwr.c delegate to persistent_time / persistent_config, which |
| 10 | * only have nrfx implementations. Those drive an nRF RTC instance directly, which on |
| 11 | * Zephyr would contend with the kernel's own timer, so the RTC is implemented on |
| 12 | * Zephyr's clock instead. |
| 13 | */ |
| 14 | #include <qerr.h> |
| 15 | #include <qgpio.h> |
| 16 | #include <qirq.h> |
| 17 | #include <qpwr.h> |
| 18 | #include <qrtc.h> |
| 19 | #include <qspi.h> |
| 20 | #include <qtime.h> |
| 21 | |
| 22 | #include <deca_device_api.h> |
| 23 | |
| 24 | #include <zephyr/drivers/gpio.h> |
| 25 | #include <zephyr/kernel.h> |
| 26 | #include <zephyr/logging/log.h> |
| 27 | |
| 28 | #include <string.h> |
| 29 | |
| 30 | LOG_MODULE_REGISTER(qorvo_qhal, CONFIG_QORVO_UWB_LOG_LEVEL); |
| 31 | |
| 32 | /* From qplatform_zephyr.c; carries the chip-select pin used for the wake pulse. */ |
| 33 | extern struct qspi_config qm33_qspi_config; |
| 34 | |
| 35 | /* Time the DW3000 needs CS held low to wake, and then to become responsive. */ |
| 36 | #define WAKEUP_CS_TOGGLE_US 400 |
| 37 | #define WAKEUP_DELAY_US 500 |
| 38 | |
| 39 | /* |
| 40 | * The DW3000 powers up asleep. dwt_probe() calls the wakeup path during probing, which |
| 41 | * is what clears this. |
| 42 | */ |
| 43 | static bool uwb_sleeping = true; |
| 44 | static bool lpm_enabled; |
| 45 | |
| 46 | /* ---- Transceiver power ------------------------------------------------- */ |
| 47 | |
| 48 | enum qerr qpwr_uwb_sleep(void) |
| 49 | { |
| 50 | int key = qirq_lock(); |
| 51 | |
| 52 | if (!uwb_sleeping) { |
| 53 | dwt_entersleep(DWT_DW_IDLE_RC); |
| 54 | uwb_sleeping = true; |
| 55 | } |
| 56 | qirq_unlock(key); |
| 57 | return QERR_SUCCESS; |
| 58 | } |
| 59 | |
| 60 | enum qerr qpwr_uwb_wakeup(void) |
| 61 | { |
| 62 | int key = qirq_lock(); |
| 63 | |
| 64 | if (uwb_sleeping) { |
| 65 | /* |
| 66 | * Waking the DW3000 is a CS pulse, not an SPI transfer: hold CS low long |
| 67 | * enough and the part comes up. Zephyr's SPI driver owns this pin, so it |
| 68 | * is briefly driven by hand here and left deasserted afterwards. |
| 69 | * |
| 70 | * Driven through gpio_pin_set_dt rather than qgpio's OUTPUT_LOW/HIGH |
| 71 | * flags: the devicetree marks cs-gpios GPIO_ACTIVE_LOW, so Zephyr inverts |
| 72 | * logical levels for this pin and "output low" would idle it physically |
| 73 | * high — the opposite of the pulse the part needs. Level 1 here means |
| 74 | * asserted, i.e. physically low. |
| 75 | */ |
| 76 | const struct gpio_dt_spec *cs = qm33_qspi_config.cs_pin.dev; |
| 77 | |
| 78 | if (cs != NULL) { |
| 79 | (void)gpio_pin_configure_dt(cs, GPIO_OUTPUT_ACTIVE); |
| 80 | qtime_usleep(WAKEUP_CS_TOGGLE_US); |
| 81 | (void)gpio_pin_set_dt(cs, 0); |
| 82 | qtime_usleep(WAKEUP_DELAY_US); |
| 83 | } |
| 84 | uwb_sleeping = false; |
| 85 | } |
| 86 | qirq_unlock(key); |
| 87 | return QERR_SUCCESS; |
| 88 | } |
| 89 | |
| 90 | bool qpwr_uwb_is_sleeping(void) |
| 91 | { |
| 92 | return uwb_sleeping; |
| 93 | } |
| 94 | |
| 95 | void qpwr_enable_lpm(void) |
| 96 | { |
| 97 | lpm_enabled = true; |
| 98 | } |
| 99 | |
| 100 | void qpwr_disable_lpm(void) |
| 101 | { |
| 102 | lpm_enabled = false; |
| 103 | } |
| 104 | |
| 105 | bool qpwr_is_lpm_enabled(void) |
| 106 | { |
| 107 | return lpm_enabled; |
| 108 | } |
| 109 | |
| 110 | enum qerr qpwr_set_min_inactivity_s4(uint32_t time_ms) |
| 111 | { |
| 112 | ARG_UNUSED(time_ms); |
| 113 | return QERR_ENOTSUP; |
| 114 | } |
| 115 | |
| 116 | enum qerr qpwr_get_min_inactivity_s4(uint32_t *time_ms) |
| 117 | { |
| 118 | ARG_UNUSED(time_ms); |
| 119 | return QERR_ENOTSUP; |
| 120 | } |
| 121 | |
| 122 | /* ---- RTC -------------------------------------------------------------- */ |
| 123 | |
| 124 | /* |
| 125 | * The stack uses this to convert between its own device time units and wall time when |
| 126 | * scheduling ranging blocks, so it needs to be monotonic and reasonably fine-grained |
| 127 | * rather than accurate in absolute terms. Zephyr's uptime is LFCLK-backed and keeps |
| 128 | * counting through sleep, which is what matters. |
| 129 | */ |
| 130 | int64_t qrtc_get_us(void) |
| 131 | { |
| 132 | return (int64_t)k_ticks_to_us_floor64(k_uptime_ticks()); |
| 133 | } |
| 134 | |
| 135 | void qrtc_resync_rtc_systime(int64_t *rtc_us, uint32_t *systime) |
| 136 | { |
| 137 | if (rtc_us) { |
| 138 | *rtc_us = qrtc_get_us(); |
| 139 | } |
| 140 | if (systime) { |
| 141 | /* |
| 142 | * llhw stores this as the anchor tying the DW3110's own timestamp counter to |
| 143 | * its DTU timeline (llhw_resync_dtu_systime keeps it, llhw_systime_to_dtu |
| 144 | * converts against it). Reporting a constant here does not mean "unused": it |
| 145 | * offsets every hardware timestamp by the whole real counter value, and since |
| 146 | * the session is time-scheduled, every RX window the MAC computes then refers |
| 147 | * to a fiction. The vendor reads the counter for exactly this reason |
| 148 | * (qhal/src/nrfx/persistent_time.c, persistent_time_resync_rtc_systime). |
| 149 | * |
| 150 | * Correlation quality. The two reads are adjacent but NOT atomic, and cannot be |
| 151 | * made so here: |
| 152 | * - qrtc_get_us() is tick-granular, so rtc_us alone carries up to one tick, |
| 153 | * 30.5 us at 32768 Hz. |
| 154 | * - this read is a blocking SPI transfer. The caller pends on the SPIM |
| 155 | * completion, so the thread YIELDS between the pair, and anything runnable |
| 156 | * then runs: BLE RX (cooperative -8), the transceiver dispatch thread (-1), |
| 157 | * log processing (3). The window is bounded by other work, not by the |
| 158 | * transfer. |
| 159 | * At 15.6 DTU/us the best case (tick + transfer, ~50 us) is ~800 DTU; with |
| 160 | * preemption it can reach milliseconds, so tens of thousands of DTU. |
| 161 | * |
| 162 | * Nothing available here closes that. irq_lock() deadlocks, since it masks the |
| 163 | * very SPIM completion this read waits on. k_sched_lock() does not help either: |
| 164 | * it prevents preemption but not the switch that blocking itself causes. Only a |
| 165 | * polling SPI path would, which is what the vendor's nrfx backend has (it |
| 166 | * busy-waits on a volatile flag) and ours deliberately does not. |
| 167 | * |
| 168 | * Tolerable because llhw re-anchors on every MCPS op and every wakeup, so a bad |
| 169 | * sample is replaced rather than accumulated, and because the error this |
| 170 | * replaces was the entire value of the counter. If ranging proves intermittent |
| 171 | * rather than broken, suspect this first. |
| 172 | */ |
| 173 | *systime = dwt_readsystimestamphi32(); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void qrtc_update_rtc_systime(int64_t updated_rtc_us, uint32_t updated_systime) |
| 178 | { |
| 179 | ARG_UNUSED(updated_rtc_us); |
| 180 | ARG_UNUSED(updated_systime); |
| 181 | } |
| 182 | |
| 183 | /* ---- "Flash" ---------------------------------------------------------- */ |
| 184 | |
| 185 | enum qerr qflash_write(uint32_t dst_addr, void *src_addr, uint32_t size) |
| 186 | { |
| 187 | const uint32_t ram_start = CONFIG_SRAM_BASE_ADDRESS; |
| 188 | const uint32_t ram_end = CONFIG_SRAM_BASE_ADDRESS + (CONFIG_SRAM_SIZE * 1024); |
| 189 | |
| 190 | if (src_addr == NULL || size == 0U) { |
| 191 | return QERR_EINVAL; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * l1_config is the only caller, storing its configuration and that config's |
| 196 | * SHA-256 to what it believes is persistent memory. Both live in .data here (see |
| 197 | * l1_config_storage.ld), so this is a plain copy and the store/load round-trip is |
| 198 | * consistent within a boot. It is deliberately not persistent: the calibration |
| 199 | * that matters is re-read from the DW3110's OTP by reset_to_default on every boot. |
| 200 | * |
| 201 | * The bounds check is the point of this function — if a future SDK moves those |
| 202 | * sections back into flash, this must fail loudly rather than write into the |
| 203 | * address space where the code lives. |
| 204 | */ |
| 205 | if (dst_addr < ram_start || (dst_addr + size) > ram_end) { |
| 206 | LOG_ERR("qflash_write to 0x%08x (%u bytes) is outside RAM; refusing", |
| 207 | dst_addr, size); |
| 208 | return QERR_ENOTSUP; |
| 209 | } |
| 210 | |
| 211 | memcpy((void *)dst_addr, src_addr, size); |
| 212 | return QERR_SUCCESS; |
| 213 | } |