oveRTOS C API
Embedded RTOS framework — build system, configuration, and portable C API
Loading...
Searching...
No Matches
i2c.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_I2C_H
10#define OVE_I2C_H
11
36#include "ove/types.h"
37#include "ove_config.h"
38#include "ove/storage.h"
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44/* ── Enums ───────────────────────────────────────────────────────── */
45
54
55/* ── Configuration ───────────────────────────────────────────────── */
56
61 unsigned int instance;
63};
64
65#ifdef CONFIG_OVE_I2C
66
67/* ── Lifecycle ───────────────────────────────────────────────────── */
68
77int ove_i2c_init(ove_i2c_t *i2c, ove_i2c_storage_t *storage, const struct ove_i2c_cfg *cfg);
78
85
93#ifdef OVE_HEAP_I2C
94int ove_i2c_create(ove_i2c_t *i2c, const struct ove_i2c_cfg *cfg);
95
102#endif /* OVE_HEAP_I2C */
103
104/* ── Operations ──────────────────────────────────────────────────── */
105
116int ove_i2c_write(ove_i2c_t i2c, uint16_t addr, const void *data, size_t len, uint64_t timeout_ns);
117
128int ove_i2c_read(ove_i2c_t i2c, uint16_t addr, void *buf, size_t len, uint64_t timeout_ns);
129
146int ove_i2c_write_read(ove_i2c_t i2c, uint16_t addr, const void *tx, size_t tx_len, void *rx,
147 size_t rx_len, uint64_t timeout_ns);
148
149/* ── Register convenience ────────────────────────────────────────── */
150
152#define OVE_I2C_REG_WRITE_MAX 32
153
170int ove_i2c_reg_write(ove_i2c_t i2c, uint16_t addr, uint8_t reg, const void *data, size_t len,
171 uint64_t timeout_ns);
172
187int ove_i2c_reg_read(ove_i2c_t i2c, uint16_t addr, uint8_t reg, void *buf, size_t len,
188 uint64_t timeout_ns);
189
190/* ── Bus probe ───────────────────────────────────────────────────── */
191
204int ove_i2c_probe(ove_i2c_t i2c, uint16_t addr, uint64_t timeout_ns);
205
233int ove_i2c_write_read_async(ove_i2c_t i2c, uint16_t addr, const void *tx, size_t tx_len, void *rx,
234 size_t rx_len, ove_dma_complete_cb cb, void *user_data);
235
236#else /* !CONFIG_OVE_I2C */
237
238/* No _init/_deinit stubs: OVE_I2C_DEFINE_STATIC is itself gated by
239 * #ifdef CONFIG_OVE_I2C in storage.h, so the link-failure path doesn't
240 * exist when the subsystem is off. */
241static inline int ove_i2c_create(ove_i2c_t *i, const struct ove_i2c_cfg *c)
242{
243 (void)i;
244 (void)c;
246}
247static inline void ove_i2c_destroy(ove_i2c_t i)
248{
249 (void)i;
250}
251static inline int ove_i2c_write(ove_i2c_t i, uint16_t a, const void *d, size_t l, uint64_t t)
252{
253 (void)i;
254 (void)a;
255 (void)d;
256 (void)l;
257 (void)t;
259}
260static inline int ove_i2c_read(ove_i2c_t i, uint16_t a, void *b, size_t l, uint64_t t)
261{
262 (void)i;
263 (void)a;
264 (void)b;
265 (void)l;
266 (void)t;
268}
269static inline int ove_i2c_write_read(ove_i2c_t i, uint16_t a, const void *tx, size_t tl, void *rx,
270 size_t rl, uint64_t t)
271{
272 (void)i;
273 (void)a;
274 (void)tx;
275 (void)tl;
276 (void)rx;
277 (void)rl;
278 (void)t;
280}
281static inline int ove_i2c_reg_write(ove_i2c_t i, uint16_t a, uint8_t r, const void *d, size_t l,
282 uint64_t t)
283{
284 (void)i;
285 (void)a;
286 (void)r;
287 (void)d;
288 (void)l;
289 (void)t;
291}
292static inline int ove_i2c_reg_read(ove_i2c_t i, uint16_t a, uint8_t r, void *b, size_t l,
293 uint64_t t)
294{
295 (void)i;
296 (void)a;
297 (void)r;
298 (void)b;
299 (void)l;
300 (void)t;
302}
303static inline int ove_i2c_probe(ove_i2c_t i, uint16_t a, uint64_t t)
304{
305 (void)i;
306 (void)a;
307 (void)t;
309}
310static inline int ove_i2c_write_read_async(ove_i2c_t i, uint16_t a, const void *tx, size_t tl,
311 void *rx, size_t rl, ove_dma_complete_cb cb, void *ud)
312{
313 (void)i;
314 (void)a;
315 (void)tx;
316 (void)tl;
317 (void)rx;
318 (void)rl;
319 (void)cb;
320 (void)ud;
322}
323
324#endif /* CONFIG_OVE_I2C */
325
326#ifdef __cplusplus
327}
328#endif
329
332#endif /* OVE_I2C_H */
int ove_i2c_read(ove_i2c_t i2c, uint16_t addr, void *buf, size_t len, uint64_t timeout_ns)
Read data from an I2C device.
int ove_i2c_reg_read(ove_i2c_t i2c, uint16_t addr, uint8_t reg, void *buf, size_t len, uint64_t timeout_ns)
Read from a single-byte-addressed register.
ove_i2c_speed_t
I2C bus speed grade.
Definition i2c.h:49
int ove_i2c_write_read(ove_i2c_t i2c, uint16_t addr, const void *tx, size_t tx_len, void *rx, size_t rx_len, uint64_t timeout_ns)
Combined write-then-read with I2C repeated start.
void ove_i2c_destroy(ove_i2c_t i2c)
Destroy a heap-allocated I2C bus controller.
void ove_i2c_deinit(ove_i2c_t i2c)
Deinitialise a statically-allocated I2C bus.
int ove_i2c_write(ove_i2c_t i2c, uint16_t addr, const void *data, size_t len, uint64_t timeout_ns)
Write data to an I2C device.
int ove_i2c_create(ove_i2c_t *i2c, const struct ove_i2c_cfg *cfg)
Create a heap-allocated I2C bus controller.
int ove_i2c_reg_write(ove_i2c_t i2c, uint16_t addr, uint8_t reg, const void *data, size_t len, uint64_t timeout_ns)
Write to a single-byte-addressed register.
int ove_i2c_init(ove_i2c_t *i2c, ove_i2c_storage_t *storage, const struct ove_i2c_cfg *cfg)
Initialise an I2C bus using caller-provided static storage.
int ove_i2c_write_read_async(ove_i2c_t i2c, uint16_t addr, const void *tx, size_t tx_len, void *rx, size_t rx_len, ove_dma_complete_cb cb, void *user_data)
Submit an I2C write-then-read transaction asynchronously.
int ove_i2c_probe(ove_i2c_t i2c, uint16_t addr, uint64_t timeout_ns)
Probe for a device at the given address.
@ OVE_I2C_SPEED_FAST
Definition i2c.h:51
@ OVE_I2C_SPEED_STANDARD
Definition i2c.h:50
@ OVE_I2C_SPEED_FAST_PLUS
Definition i2c.h:52
void(* ove_dma_complete_cb)(int result, void *user_data)
DMA / async transfer completion callback signature.
Definition types.h:312
struct ove_i2c * ove_i2c_t
Opaque handle for an I2C bus controller.
Definition types.h:268
@ OVE_ERR_NOT_SUPPORTED
Definition types.h:98
I2C bus configuration descriptor.
Definition i2c.h:60
unsigned int instance
Definition i2c.h:61
ove_i2c_speed_t speed
Definition i2c.h:62