openaliro
Aliro reader: UWB/CCC core and ESP32-S3/C5/C6 port
Loading...
Searching...
No Matches
woz_port.h
Go to the documentation of this file.
1
6/*
7 * Copyright (c) 2026 asxeem
8 * SPDX-License-Identifier: ISC
9 * woz_port.h - the platform contract for the UWB engine and the Aliro reader.
10 *
11 * This header IS the port specification. Everything the ranging engine needs
12 * from an operating system is the eight functions below; a new target is a new
13 * branch here plus a DW3000 SPI/GPIO backend. Nothing else.
14 *
15 * woz_mutex_* is the one addition beyond the ranging engine's needs: the Aliro
16 * reader in modules/woz_aliro guards its trust store against the BLE-host and
17 * REPL tasks, and a second competing port contract for one mutex would be worse
18 * than widening this one. It stays a plain blocking lock — no try-lock, no
19 * timeout — so every backend is three lines.
20 *
21 * Deliberately absent: work queues, timers, and init hooks. Those are used only
22 * by uwb_rxdiag.c, uwb_selftest.c, woz_logfmt.c, woz_logquiet.c and
23 * aliro_shell.c, which are Zephyr-only by design and are not in any port's
24 * source list. Admitting them here would multiply the port surface for code
25 * that never runs on the ranging path.
26 *
27 * Also absent: the k_work / k_sem / k_poll surface used by dw3000_spi.c and
28 * dw3000_hw.c. Every port supplies its own SPI/GPIO backend for those two
29 * files, so they never constrain the contract.
30 *
31 * woz_malloc/woz_calloc/woz_free heap
32 * woz_uptime_us monotonic microseconds since boot
33 * woz_uptime_ms monotonic milliseconds since boot
34 * woz_sleep_ms relinquish the CPU for at least ms
35 * woz_sleep_us short busy-wait, microseconds (deca_sleep)
36 * woz_cycle_get_32 free-running counter, RX-arm latency probe
37 * woz_mutex_init/lock/unlock blocking mutex (Aliro reader trust store)
38 */
39#ifndef WOZ_PORT_H
40#define WOZ_PORT_H
41
42#include <stddef.h>
43#include <stdint.h>
44
45#if defined(__ZEPHYR__)
46
47#include <zephyr/kernel.h>
48
49static inline void *woz_malloc(size_t size)
50{
51 return k_malloc(size);
52}
53static inline void *woz_calloc(size_t n, size_t size)
54{
55 return k_calloc(n, size);
56}
57static inline void woz_free(void *ptr)
58{
59 k_free(ptr);
60}
61static inline int64_t woz_uptime_us(void)
62{
63 return (int64_t)k_ticks_to_us_floor64(k_uptime_ticks());
64}
65static inline int64_t woz_uptime_ms(void)
66{
67 return k_uptime_get();
68}
69static inline void woz_sleep_ms(int32_t ms)
70{
71 k_msleep(ms);
72}
73static inline void woz_sleep_us(int64_t us)
74{
75 k_usleep((int32_t)us);
76}
77static inline uint32_t woz_cycle_get_32(void)
78{
79 return k_cycle_get_32();
80}
81typedef struct k_mutex woz_mutex_t;
82static inline void woz_mutex_init(woz_mutex_t *m)
83{
84 k_mutex_init(m);
85}
86static inline void woz_mutex_lock(woz_mutex_t *m)
87{
88 k_mutex_lock(m, K_FOREVER);
89}
90static inline void woz_mutex_unlock(woz_mutex_t *m)
91{
92 k_mutex_unlock(m);
93}
94
95#elif defined(ESP_PLATFORM)
96
97#include "freertos/FreeRTOS.h"
98#include "freertos/task.h"
99#include "freertos/semphr.h"
100#include <stdlib.h>
101
102#include "esp_cpu.h"
103#include "esp_rom_sys.h"
104#include "esp_timer.h"
105
106static inline void *woz_malloc(size_t size)
107{
108 return malloc(size);
109}
110static inline void *woz_calloc(size_t n, size_t size)
111{
112 return calloc(n, size);
113}
114static inline void woz_free(void *ptr)
115{
116 free(ptr);
117}
118static inline int64_t woz_uptime_us(void)
119{
120 return esp_timer_get_time();
121}
122static inline int64_t woz_uptime_ms(void)
123{
124 return esp_timer_get_time() / 1000;
125}
126static inline void woz_sleep_ms(int32_t ms)
127{
128 if (ms > 0) {
129 vTaskDelay(pdMS_TO_TICKS(ms));
130 }
131}
132static inline void woz_sleep_us(int64_t us)
133{
134 esp_rom_delay_us((uint32_t)us);
135}
136static inline uint32_t woz_cycle_get_32(void)
137{
138 return esp_cpu_get_cycle_count();
139}
140typedef struct {
141 StaticSemaphore_t buf;
142 SemaphoreHandle_t h;
143} woz_mutex_t;
144static inline void woz_mutex_init(woz_mutex_t *m)
145{
146 m->h = xSemaphoreCreateMutexStatic(&m->buf);
147}
148static inline void woz_mutex_lock(woz_mutex_t *m)
149{
150 xSemaphoreTake(m->h, portMAX_DELAY);
151}
152static inline void woz_mutex_unlock(woz_mutex_t *m)
153{
154 xSemaphoreGive(m->h);
155}
156
157#elif defined(WOZ_PORT_HOST)
158
159#include <stdlib.h>
160#include <time.h>
161
167static inline void *woz_malloc(size_t size)
168{
169 return malloc(size);
170}
177static inline void *woz_calloc(size_t n, size_t size)
178{
179 return calloc(n, size);
180}
185static inline void woz_free(void *ptr)
186{
187 free(ptr);
188}
193static inline int64_t woz_uptime_us(void)
194{
195 struct timespec ts;
196
197 clock_gettime(CLOCK_MONOTONIC, &ts);
198 return (int64_t)ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
199}
204static inline int64_t woz_uptime_ms(void)
205{
206 return woz_uptime_us() / 1000;
207}
212static inline void woz_sleep_ms(int32_t ms)
213{
214 (void)ms; /* host tests are deterministic; nothing to wait for */
215}
220static inline void woz_sleep_us(int64_t us)
221{
222 (void)us;
223}
228static inline uint32_t woz_cycle_get_32(void)
229{
230 return (uint32_t)woz_uptime_us(); /* us resolution is plenty for the probe */
231}
235typedef int woz_mutex_t;
240static inline void woz_mutex_init(woz_mutex_t *m)
241{
242 (void)m;
243}
248static inline void woz_mutex_lock(woz_mutex_t *m)
249{
250 (void)m;
251}
256static inline void woz_mutex_unlock(woz_mutex_t *m)
257{
258 (void)m;
259}
260
261#else
262#error "woz_port.h: no platform backend. Define WOZ_PORT_HOST, or build under Zephyr/ESP-IDF."
263#endif
264
265#endif /* WOZ_PORT_H */