oveRTOS C API
Embedded RTOS framework — build system, configuration, and portable C API
Loading...
Searching...
No Matches
spi.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 Kamil Lulko <kamil.lulko@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 *
6 * This file is part of oveRTOS.
7 */
8
9#ifndef OVE_SPI_H
10#define OVE_SPI_H
11
30#include "ove/types.h"
31#include "ove_config.h"
32#include "ove/storage.h"
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38/* ── Enums ───────────────────────────────────────────────────────── */
39
49
57
58/* ── Configuration ───────────────────────────────────────────────── */
59
70
78struct ove_spi_cs {
79 unsigned int gpio_port;
80 unsigned int gpio_pin;
82};
83
91 const void *tx;
92 void *rx;
93 size_t len;
94};
95
96#ifdef CONFIG_OVE_SPI
97
98/* ── Lifecycle ───────────────────────────────────────────────────── */
99
107int ove_spi_init(ove_spi_t *spi, ove_spi_storage_t *storage, const struct ove_spi_cfg *cfg);
110
111#ifdef OVE_HEAP_SPI
118int ove_spi_create(ove_spi_t *spi, const struct ove_spi_cfg *cfg);
121#endif /* OVE_HEAP_SPI */
122
123/* ── Operations ──────────────────────────────────────────────────── */
124
140int ove_spi_transfer(ove_spi_t spi, const struct ove_spi_cs *cs, const void *tx, void *rx,
141 size_t len, uint64_t timeout_ns);
142
153int ove_spi_write(ove_spi_t spi, const struct ove_spi_cs *cs, const void *data, size_t len,
154 uint64_t timeout_ns);
155
166int ove_spi_read(ove_spi_t spi, const struct ove_spi_cs *cs, void *buf, size_t len,
167 uint64_t timeout_ns);
168
183int ove_spi_transfer_seq(ove_spi_t spi, const struct ove_spi_cs *cs,
184 const struct ove_spi_xfer *xfers, unsigned int num_xfers,
185 uint64_t timeout_ns);
186
218int ove_spi_transfer_async(ove_spi_t spi, const struct ove_spi_cs *cs, const void *tx, void *rx,
219 size_t len, ove_dma_complete_cb cb, void *user_data);
220
221#else /* !CONFIG_OVE_SPI */
222
223/* No _init/_deinit stubs: OVE_SPI_DEFINE_STATIC is itself gated by
224 * #ifdef CONFIG_OVE_SPI in storage.h. */
225static inline int ove_spi_create(ove_spi_t *s, const struct ove_spi_cfg *c)
226{
227 (void)s;
228 (void)c;
230}
231static inline void ove_spi_destroy(ove_spi_t s)
232{
233 (void)s;
234}
235static inline int ove_spi_transfer_async(ove_spi_t s, const struct ove_spi_cs *cs, const void *tx,
236 void *rx, size_t l, ove_dma_complete_cb cb, void *ud)
237{
238 (void)s;
239 (void)cs;
240 (void)tx;
241 (void)rx;
242 (void)l;
243 (void)cb;
244 (void)ud;
246}
247static inline int ove_spi_transfer(ove_spi_t s, const struct ove_spi_cs *cs, const void *tx,
248 void *rx, size_t l, uint64_t t)
249{
250 (void)s;
251 (void)cs;
252 (void)tx;
253 (void)rx;
254 (void)l;
255 (void)t;
257}
258static inline int ove_spi_write(ove_spi_t s, const struct ove_spi_cs *cs, const void *d, size_t l,
259 uint64_t t)
260{
261 (void)s;
262 (void)cs;
263 (void)d;
264 (void)l;
265 (void)t;
267}
268static inline int ove_spi_read(ove_spi_t s, const struct ove_spi_cs *cs, void *b, size_t l,
269 uint64_t t)
270{
271 (void)s;
272 (void)cs;
273 (void)b;
274 (void)l;
275 (void)t;
277}
278static inline int ove_spi_transfer_seq(ove_spi_t s, const struct ove_spi_cs *cs,
279 const struct ove_spi_xfer *x, unsigned int n, uint64_t t)
280{
281 (void)s;
282 (void)cs;
283 (void)x;
284 (void)n;
285 (void)t;
287}
288
289#endif /* CONFIG_OVE_SPI */
290
291#ifdef __cplusplus
292}
293#endif
294
297#endif /* OVE_SPI_H */
int ove_spi_transfer_seq(ove_spi_t spi, const struct ove_spi_cs *cs, const struct ove_spi_xfer *xfers, unsigned int num_xfers, uint64_t timeout_ns)
Multi-segment SPI transfer under a single CS assertion.
int ove_spi_create(ove_spi_t *spi, const struct ove_spi_cfg *cfg)
Heap-mode counterpart of ove_spi_init() — allocates storage internally.
int ove_spi_init(ove_spi_t *spi, ove_spi_storage_t *storage, const struct ove_spi_cfg *cfg)
Initialise an SPI bus controller with caller-provided storage.
ove_spi_bit_order_t
SPI bit order.
Definition spi.h:53
void ove_spi_deinit(ove_spi_t spi)
Release an SPI bus handle previously created with ove_spi_init.
int ove_spi_transfer(ove_spi_t spi, const struct ove_spi_cs *cs, const void *tx, void *rx, size_t len, uint64_t timeout_ns)
Full-duplex SPI transfer.
ove_spi_mode_t
SPI clock polarity / phase mode.
Definition spi.h:43
int ove_spi_read(ove_spi_t spi, const struct ove_spi_cs *cs, void *buf, size_t len, uint64_t timeout_ns)
Read-only SPI transfer (clock out zeros, capture RX).
int ove_spi_transfer_async(ove_spi_t spi, const struct ove_spi_cs *cs, const void *tx, void *rx, size_t len, ove_dma_complete_cb cb, void *user_data)
Submit an SPI transfer asynchronously and return immediately.
int ove_spi_write(ove_spi_t spi, const struct ove_spi_cs *cs, const void *data, size_t len, uint64_t timeout_ns)
Write-only SPI transfer (TX only, ignore RX).
void ove_spi_destroy(ove_spi_t spi)
Destroy an SPI bus handle previously created with ove_spi_create.
@ OVE_SPI_MSB_FIRST
Definition spi.h:54
@ OVE_SPI_LSB_FIRST
Definition spi.h:55
@ OVE_SPI_MODE_3
Definition spi.h:47
@ OVE_SPI_MODE_0
Definition spi.h:44
@ OVE_SPI_MODE_2
Definition spi.h:46
@ OVE_SPI_MODE_1
Definition spi.h:45
struct ove_spi * ove_spi_t
Opaque handle for an SPI bus controller.
Definition types.h:265
void(* ove_dma_complete_cb)(int result, void *user_data)
DMA / async transfer completion callback signature.
Definition types.h:312
@ OVE_ERR_NOT_SUPPORTED
Definition types.h:98
SPI bus configuration descriptor.
Definition spi.h:63
unsigned int instance
Definition spi.h:64
uint8_t word_size
Definition spi.h:68
ove_spi_mode_t mode
Definition spi.h:66
uint32_t clock_hz
Definition spi.h:65
ove_spi_bit_order_t bit_order
Definition spi.h:67
SPI chip-select descriptor.
Definition spi.h:78
unsigned int gpio_port
Definition spi.h:79
unsigned int gpio_pin
Definition spi.h:80
int active_low
Definition spi.h:81
SPI transfer segment for multi-segment transactions.
Definition spi.h:90
size_t len
Definition spi.h:93
void * rx
Definition spi.h:92
const void * tx
Definition spi.h:91