Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / firmware / qorvo-uwb / src / qtimer_zephyr.c
qtimer_zephyr.c166 lines · 5.0 KiBRaw
| 1 | /* |
| 2 | * Zephyr timer backend for Qorvo's qhal. |
| 3 | * |
| 4 | * Replaces qhal/src/zephyr/qtimer.c, which like its qspi.c sibling is a placeholder: |
| 5 | * qtimer_init() returns NULL and every other entry point returns QERR_ENOTSUP. llhw_init() |
| 6 | * sets up an idle timer through this, so with the stub in place the whole UWB stack fails |
| 7 | * to initialise with QERR_EIO. |
| 8 | * |
| 9 | * Built on k_timer rather than the counter API. The stack uses this to schedule wakeups |
| 10 | * around ranging blocks, which are milliseconds apart, so tick granularity (30.5 us at the |
| 11 | * default 32768 Hz) is far finer than needed and avoids depending on a particular hardware |
| 12 | * timer instance being free — the nRF's are already claimed by the kernel and the |
| 13 | * Bluetooth controller. |
| 14 | * |
| 15 | * `qtimer_id`, and the requested frequency and width in qtimer_config, are therefore |
| 16 | * ignored: they describe a hardware timer instance this implementation does not allocate. |
| 17 | */ |
| 18 | #include <qerr.h> |
| 19 | #include <qtimer.h> |
| 20 | |
| 21 | #include <zephyr/kernel.h> |
| 22 | #include <zephyr/logging/log.h> |
| 23 | #include <zephyr/sys/atomic.h> |
| 24 | |
| 25 | LOG_MODULE_REGISTER(qorvo_qtimer, CONFIG_QORVO_UWB_LOG_LEVEL); |
| 26 | |
| 27 | /* The stack allocates one idle timer; a second slot leaves room without a heap. */ |
| 28 | #define MAX_TIMERS 2 |
| 29 | |
| 30 | struct qtimer { |
| 31 | struct k_timer timer; |
| 32 | qtimer_cb cb; |
| 33 | void *arg; |
| 34 | int64_t started_ticks; |
| 35 | bool used; |
| 36 | }; |
| 37 | |
| 38 | static struct qtimer timers[MAX_TIMERS]; |
| 39 | |
| 40 | /* |
| 41 | * Bring-up instrumentation. llhw drives its ranging schedule off this timer, so the wait |
| 42 | * duration it asks for is a number we can read: if the MAC arms for something absurd, the |
| 43 | * fault is in the time base rather than the radio. |
| 44 | * |
| 45 | * Counters are unbounded but the log trace is capped, because a working schedule would arm |
| 46 | * this every block and flood the capture. Capping alone is not enough though - last time a |
| 47 | * cap hid exactly the calls that mattered - so the counters and the last requested interval |
| 48 | * are reported separately and survive past the cap. |
| 49 | */ |
| 50 | #define QTIMER_TRACE_MAX 16 |
| 51 | static unsigned int call_traced; |
| 52 | static unsigned int expiry_traced; |
| 53 | static atomic_t starts; |
| 54 | static atomic_t stops; |
| 55 | static atomic_t expiries; |
| 56 | /* Single aligned 32-bit accesses; diagnostic only, deliberately not synchronised. */ |
| 57 | static volatile uint32_t last_us; |
| 58 | static volatile bool last_periodic; |
| 59 | |
| 60 | void ff_qorvo_timer_stats(uint32_t *starts_out, uint32_t *stops_out, uint32_t *expiries_out, |
| 61 | uint32_t *last_us_out) |
| 62 | { |
| 63 | if (starts_out != NULL) { |
| 64 | *starts_out = (uint32_t)atomic_get(&starts); |
| 65 | } |
| 66 | if (stops_out != NULL) { |
| 67 | *stops_out = (uint32_t)atomic_get(&stops); |
| 68 | } |
| 69 | if (expiries_out != NULL) { |
| 70 | *expiries_out = (uint32_t)atomic_get(&expiries); |
| 71 | } |
| 72 | if (last_us_out != NULL) { |
| 73 | *last_us_out = last_us; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | static void on_expiry(struct k_timer *t) |
| 78 | { |
| 79 | struct qtimer *q = CONTAINER_OF(t, struct qtimer, timer); |
| 80 | |
| 81 | atomic_inc(&expiries); |
| 82 | if (expiry_traced < QTIMER_TRACE_MAX) { |
| 83 | expiry_traced++; |
| 84 | LOG_INF("qtimer expiry"); |
| 85 | } |
| 86 | if (q->cb) { |
| 87 | q->cb(q->arg); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | struct qtimer *qtimer_init(uint8_t qtimer_id, const struct qtimer_config *config, |
| 92 | qtimer_cb handler, void *arg) |
| 93 | { |
| 94 | ARG_UNUSED(qtimer_id); |
| 95 | ARG_UNUSED(config); |
| 96 | |
| 97 | for (int i = 0; i < MAX_TIMERS; i++) { |
| 98 | if (timers[i].used) { |
| 99 | continue; |
| 100 | } |
| 101 | timers[i].cb = handler; |
| 102 | timers[i].arg = arg; |
| 103 | timers[i].started_ticks = 0; |
| 104 | timers[i].used = true; |
| 105 | k_timer_init(&timers[i].timer, on_expiry, NULL); |
| 106 | return &timers[i]; |
| 107 | } |
| 108 | LOG_ERR("out of qtimer slots"); |
| 109 | return NULL; |
| 110 | } |
| 111 | |
| 112 | enum qerr qtimer_start(const struct qtimer *timer, uint32_t us, bool periodic) |
| 113 | { |
| 114 | /* The interface is const, but starting a timer necessarily mutates it. */ |
| 115 | struct qtimer *q = (struct qtimer *)timer; |
| 116 | |
| 117 | if (q == NULL || !q->used) { |
| 118 | return QERR_EINVAL; |
| 119 | } |
| 120 | q->started_ticks = k_uptime_ticks(); |
| 121 | atomic_inc(&starts); |
| 122 | last_us = us; |
| 123 | last_periodic = periodic; |
| 124 | if (call_traced < QTIMER_TRACE_MAX) { |
| 125 | call_traced++; |
| 126 | LOG_INF("qtimer_start: us=%u (%u ms), periodic=%d", us, us / 1000U, |
| 127 | (int)periodic); |
| 128 | } |
| 129 | /* |
| 130 | * Note the inverted sense in qtimer.h: it documents `periodic` as "true for a |
| 131 | * one-shot timer, false for a cyclic timer". Going by the parameter name rather |
| 132 | * than that comment, since the name is what callers read - periodic means repeat. |
| 133 | * Settled since: the vendor's own nrfx backend re-arms when the flag is true and |
| 134 | * disables when it is false (qhal/src/nrfx/qrtc_share.c), so the comment is wrong |
| 135 | * and the name is right. llhw passes false, i.e. one-shot, and re-arms each interval. |
| 136 | */ |
| 137 | k_timer_start(&q->timer, K_USEC(us), periodic ? K_USEC(us) : K_NO_WAIT); |
| 138 | return QERR_SUCCESS; |
| 139 | } |
| 140 | |
| 141 | enum qerr qtimer_stop(const struct qtimer *timer) |
| 142 | { |
| 143 | struct qtimer *q = (struct qtimer *)timer; |
| 144 | |
| 145 | if (q == NULL || !q->used) { |
| 146 | return QERR_EINVAL; |
| 147 | } |
| 148 | atomic_inc(&stops); |
| 149 | if (call_traced < QTIMER_TRACE_MAX) { |
| 150 | call_traced++; |
| 151 | LOG_INF("qtimer_stop"); |
| 152 | } |
| 153 | k_timer_stop(&q->timer); |
| 154 | return QERR_SUCCESS; |
| 155 | } |
| 156 | |
| 157 | enum qerr qtimer_read(const struct qtimer *timer, uint32_t *us) |
| 158 | { |
| 159 | struct qtimer *q = (struct qtimer *)timer; |
| 160 | |
| 161 | if (q == NULL || !q->used || us == NULL) { |
| 162 | return QERR_EINVAL; |
| 163 | } |
| 164 | *us = (uint32_t)k_ticks_to_us_floor64(k_uptime_ticks() - q->started_ticks); |
| 165 | return QERR_SUCCESS; |
| 166 | } |