oveRTOS C API
Embedded RTOS framework — build system, configuration, and portable C API
Loading...
Searching...
No Matches
i2c.h
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,
78 const struct ove_i2c_cfg *cfg);
79
85void ove_i2c_deinit(ove_i2c_t i2c);
86
94#ifdef OVE_HEAP_I2C
95int ove_i2c_create(ove_i2c_t *i2c, const struct ove_i2c_cfg *cfg);
96
102void ove_i2c_destroy(ove_i2c_t i2c);
103#elif !defined(__ZIG_CIMPORT__)
104#define ove_i2c_create(pi2c, cfg) \
105 ({ static ove_i2c_storage_t _ove_stor_; \
106 ove_i2c_init((pi2c), &_ove_stor_, (cfg)); })
107#define ove_i2c_destroy(i2c) ove_i2c_deinit(i2c)
108#endif
109
110/* ── Operations ──────────────────────────────────────────────────── */
111
122int ove_i2c_write(ove_i2c_t i2c, uint16_t addr,
123 const void *data, size_t len, uint32_t timeout_ms);
124
135int ove_i2c_read(ove_i2c_t i2c, uint16_t addr,
136 void *buf, size_t len, uint32_t timeout_ms);
137
154int ove_i2c_write_read(ove_i2c_t i2c, uint16_t addr,
155 const void *tx, size_t tx_len,
156 void *rx, size_t rx_len,
157 uint32_t timeout_ms);
158
159/* ── Register convenience ────────────────────────────────────────── */
160
162#define OVE_I2C_REG_WRITE_MAX 32
163
180int ove_i2c_reg_write(ove_i2c_t i2c, uint16_t addr, uint8_t reg,
181 const void *data, size_t len,
182 uint32_t timeout_ms);
183
198int ove_i2c_reg_read(ove_i2c_t i2c, uint16_t addr, uint8_t reg,
199 void *buf, size_t len, uint32_t timeout_ms);
200
201/* ── Bus probe ───────────────────────────────────────────────────── */
202
215int ove_i2c_probe(ove_i2c_t i2c, uint16_t addr, uint32_t timeout_ms);
216
217#else /* !CONFIG_OVE_I2C */
218
219static inline int ove_i2c_create(ove_i2c_t *i, const struct ove_i2c_cfg *c) { (void)i; (void)c; return OVE_ERR_NOT_SUPPORTED; }
220static inline void ove_i2c_destroy(ove_i2c_t i) { (void)i; }
221static inline int ove_i2c_write(ove_i2c_t i, uint16_t a, const void *d, size_t l, uint32_t t) { (void)i; (void)a; (void)d; (void)l; (void)t; return OVE_ERR_NOT_SUPPORTED; }
222static inline int ove_i2c_read(ove_i2c_t i, uint16_t a, void *b, size_t l, uint32_t t) { (void)i; (void)a; (void)b; (void)l; (void)t; return OVE_ERR_NOT_SUPPORTED; }
223static inline int ove_i2c_write_read(ove_i2c_t i, uint16_t a, const void *tx, size_t tl, void *rx, size_t rl, uint32_t t) { (void)i; (void)a; (void)tx; (void)tl; (void)rx; (void)rl; (void)t; return OVE_ERR_NOT_SUPPORTED; }
224static inline int ove_i2c_reg_write(ove_i2c_t i, uint16_t a, uint8_t r, const void *d, size_t l, uint32_t t) { (void)i; (void)a; (void)r; (void)d; (void)l; (void)t; return OVE_ERR_NOT_SUPPORTED; }
225static inline int ove_i2c_reg_read(ove_i2c_t i, uint16_t a, uint8_t r, void *b, size_t l, uint32_t t) { (void)i; (void)a; (void)r; (void)b; (void)l; (void)t; return OVE_ERR_NOT_SUPPORTED; }
226static inline int ove_i2c_probe(ove_i2c_t i, uint16_t a, uint32_t t) { (void)i; (void)a; (void)t; return OVE_ERR_NOT_SUPPORTED; }
227
228#endif /* CONFIG_OVE_I2C */
229
230#ifdef __cplusplus
231}
232#endif
233
236#endif /* OVE_I2C_H */
ove_i2c_speed_t
I2C bus speed grade.
Definition i2c.h:49
@ 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
#define OVE_ERR_NOT_SUPPORTED
The requested feature is not supported by the active backend.
Definition types.h:38
struct ove_i2c * ove_i2c_t
Opaque handle for an I2C bus controller.
Definition types.h:142
I2C bus configuration descriptor.
Definition i2c.h:60
unsigned int instance
Definition i2c.h:61
ove_i2c_speed_t speed
Definition i2c.h:62