ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
i2c.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file i2c.h
6 * @brief The one owner of the shared I2C bus for the peripheral drivers.
7 *
8 * The sensor / peripheral drivers (RTC, SHT3x, MPR121, ADS1115, INA219, PCA9685, VL53L0X,
9 * LDC1614, FDC2214) share one bus and bring it up through these verbs. The pins come from
10 * PROTOCORE_I2C_SDA_PIN / PROTOCORE_I2C_SCL_PIN (default -1 = the platform default GPIO 21 / 22). Re-begin
11 * is idempotent, so per-driver calls are harmless.
12 *
13 * This covers the master half of the protocol. The three transfer shapes a driver reaches for are
14 * a write, a read, and a register read, which is a write and a read joined by a repeated start.
15 * The rest of what the bus can express is here too: a bus scan, an address probe, the general
16 * call, 10-bit addressing, a per-transfer clock, and recovery of a bus a device is holding low.
17 *
18 * Every verb has a plain form on the default bus (PROTOCORE_I2C_BUS) and an `_on` form naming a
19 * controller, so a board with two I2C controllers drives both through one owner.
20 *
21 * An address is 7-bit unless it is wrapped in ::PROTOCORE_I2C_ADDR10, which selects the 10-bit form.
22 *
23 * The bodies compile wherever the platform states a bus.
24 *
25 * @author Douglas Quigg (dstroy0)
26 * @date 2026
27 */
28
29#ifndef PROTOCORE_I2C_H
30#define PROTOCORE_I2C_H
31
33
34#include "protocore_config.h"
35
36/** @brief Bus clock for the shared peripheral bus; 100 kHz standard mode. */
37#ifndef PROTOCORE_I2C_HZ
38#define PROTOCORE_I2C_HZ 100000u
39#endif
40
41/** @brief Per-transfer timeout in milliseconds. */
42#ifndef PROTOCORE_I2C_TIMEOUT_MS
43#define PROTOCORE_I2C_TIMEOUT_MS 50u
44#endif
45
46/** @brief Controller the plain verbs drive. */
47#ifndef PROTOCORE_I2C_BUS
48#define PROTOCORE_I2C_BUS 0u
49#endif
50
51/** @brief Standard-mode, fast-mode and fast-mode-plus bus clocks. */
52#define PROTOCORE_I2C_HZ_STANDARD 100000u
53#define PROTOCORE_I2C_HZ_FAST 400000u
54#define PROTOCORE_I2C_HZ_FAST_PLUS 1000000u
55
56/** @brief Wrap a 10-bit address so a transfer verb puts the two-byte form on the wire. */
57#define PROTOCORE_I2C_ADDR10(a) ((uint16_t)(PROTOCORE_I2C_ADDR_10BIT | ((a) & PROTOCORE_I2C_ADDR_MASK)))
58
59/** @brief Lowest and highest 7-bit addresses a scan reports; 0x00 - 0x07 and 0x78 - 0x7F are reserved. */
60#define PROTOCORE_I2C_SCAN_FIRST 0x08u
61#define PROTOCORE_I2C_SCAN_LAST 0x77u
62
63EMBED_BEGIN_DECLS
64
65#if PROTOCORE_HAS_BUS
66
67/** @brief Bring up @p bus on @p sda / @p scl at @p hz (-1 on a pin = the platform default). */
68EMBED_INLINE embed_bool protocore_i2c_begin_on(uint8_t bus, int sda, int scl, uint32_t hz)
69{
70 return protocore_platform_i2c_begin(bus, sda, scl, hz) != 0;
71}
72
73/** @brief Bring up the shared I2C bus on PROTOCORE_I2C_SDA_PIN / PROTOCORE_I2C_SCL_PIN (-1 = default). */
74EMBED_INLINE embed_bool protocore_i2c_begin(void)
75{
78}
79
80/** @brief Write @p len bytes to @p addr on @p bus, closing with a stop. */
81EMBED_INLINE embed_bool protocore_i2c_write_on(uint8_t bus, uint16_t addr, const uint8_t *buf, size_t len)
82{
83 return protocore_platform_i2c_write(bus, addr, buf, (uint32_t)len, PROTOCORE_I2C_TIMEOUT_MS) != 0;
84}
85
86/** @brief Write @p len bytes to @p addr, closing with a stop. */
87EMBED_INLINE embed_bool protocore_i2c_write(uint16_t addr, const uint8_t *buf, size_t len)
88{
89 return protocore_i2c_write_on((uint8_t)PROTOCORE_I2C_BUS, addr, buf, len);
90}
91
92/** @brief Read @p len bytes from @p addr on @p bus. */
93EMBED_INLINE embed_bool protocore_i2c_read_on(uint8_t bus, uint16_t addr, uint8_t *buf, size_t len)
94{
95 return protocore_platform_i2c_read(bus, addr, buf, (uint32_t)len, PROTOCORE_I2C_TIMEOUT_MS) != 0;
96}
97
98/** @brief Read @p len bytes from @p addr. */
99EMBED_INLINE embed_bool protocore_i2c_read(uint16_t addr, uint8_t *buf, size_t len)
100{
101 return protocore_i2c_read_on((uint8_t)PROTOCORE_I2C_BUS, addr, buf, len);
102}
103
104/** @brief Write @p wlen bytes to @p addr on @p bus, then read @p rlen back through a repeated start. */
105EMBED_INLINE embed_bool protocore_i2c_write_read_on(uint8_t bus, uint16_t addr, const uint8_t *w, size_t wlen,
106 uint8_t *r, size_t rlen)
107{
108 return protocore_platform_i2c_write_read(bus, addr, w, (uint32_t)wlen, r, (uint32_t)rlen,
110}
111
112/** @brief Write @p wlen bytes, then read @p rlen back in the same transaction (repeated start). */
113EMBED_INLINE embed_bool protocore_i2c_write_read(uint16_t addr, const uint8_t *w, size_t wlen, uint8_t *r, size_t rlen)
114{
115 return protocore_i2c_write_read_on((uint8_t)PROTOCORE_I2C_BUS, addr, w, wlen, r, rlen);
116}
117
118/** @brief Run @p bus at @p hz from here on. */
119EMBED_INLINE embed_bool protocore_i2c_set_clock_on(uint8_t bus, uint32_t hz)
120{
121 return protocore_platform_i2c_set_clock(bus, hz) != 0;
122}
123
124/** @brief Run the shared bus at @p hz from here on; a part slower than the bus needs this. */
125EMBED_INLINE embed_bool protocore_i2c_set_clock(uint32_t hz)
126{
128}
129
130/** @brief Address @p addr on @p bus and stop. @return true if a device drove ACK. */
131EMBED_INLINE embed_bool protocore_i2c_probe_on(uint8_t bus, uint16_t addr)
132{
133 return protocore_platform_i2c_probe(bus, addr, PROTOCORE_I2C_TIMEOUT_MS) != 0;
134}
135
136/** @brief Address @p addr and stop. @return true if a device drove ACK. */
137EMBED_INLINE embed_bool protocore_i2c_probe(uint16_t addr)
138{
139 return protocore_i2c_probe_on((uint8_t)PROTOCORE_I2C_BUS, addr);
140}
141
142/**
143 * @brief Probe every non-reserved 7-bit address on @p bus, writing those that answered into
144 * @p out (caller-owned, @p cap entries).
145 * @return how many answered, which is how many entries of @p out were written.
146 */
147EMBED_INLINE size_t protocore_i2c_scan_on(uint8_t bus, uint8_t *out, size_t cap)
148{
149 size_t n = 0;
150 for (uint16_t a = PROTOCORE_I2C_SCAN_FIRST; a <= PROTOCORE_I2C_SCAN_LAST && n < cap; a++)
151 {
152 if (protocore_i2c_probe_on(bus, a))
153 {
154 out[n] = (uint8_t)a;
155 n++;
156 }
157 }
158 return n;
159}
160
161/** @brief Probe every non-reserved 7-bit address on the shared bus into @p out. */
162EMBED_INLINE size_t protocore_i2c_scan(uint8_t *out, size_t cap)
163{
164 return protocore_i2c_scan_on((uint8_t)PROTOCORE_I2C_BUS, out, cap);
165}
166
167/** @brief Write @p len bytes to address 0x00 on @p bus, which every device answers. */
168EMBED_INLINE embed_bool protocore_i2c_general_call_on(uint8_t bus, const uint8_t *buf, size_t len)
169{
170 return protocore_i2c_write_on(bus, PROTOCORE_I2C_GENERAL_CALL, buf, len);
171}
172
173/** @brief Write @p len bytes to the general call address, which every device on the bus answers. */
174EMBED_INLINE embed_bool protocore_i2c_general_call(const uint8_t *buf, size_t len)
175{
176 return protocore_i2c_general_call_on((uint8_t)PROTOCORE_I2C_BUS, buf, len);
177}
178
179/** @brief Clock @p bus until the device holding SDA low releases it, then drive a stop. */
180EMBED_INLINE embed_bool protocore_i2c_recover_on(uint8_t bus, int sda, int scl)
181{
182 return protocore_platform_i2c_recover(bus, sda, scl) != 0;
183}
184
185/** @brief Free the shared bus from a device holding SDA low. @return true if SDA came back high. */
186EMBED_INLINE embed_bool protocore_i2c_recover(void)
187{
189}
190
191#else // no bus seam on this build
192
193EMBED_INLINE embed_bool protocore_i2c_begin_on(uint8_t bus, int sda, int scl, uint32_t hz)
194{
195 (void)bus;
196 (void)sda;
197 (void)scl;
198 (void)hz;
199 return EMBED_TRUE;
200}
201
202EMBED_INLINE embed_bool protocore_i2c_begin(void)
203{
204 return EMBED_TRUE;
205}
206
207EMBED_INLINE embed_bool protocore_i2c_write_on(uint8_t bus, uint16_t addr, const uint8_t *buf, size_t len)
208{
209 (void)bus;
210 (void)addr;
211 (void)buf;
212 (void)len;
213 return EMBED_FALSE;
214}
215
216EMBED_INLINE embed_bool protocore_i2c_write(uint16_t addr, const uint8_t *buf, size_t len)
217{
218 return protocore_i2c_write_on((uint8_t)PROTOCORE_I2C_BUS, addr, buf, len);
219}
220
221EMBED_INLINE embed_bool protocore_i2c_read_on(uint8_t bus, uint16_t addr, uint8_t *buf, size_t len)
222{
223 (void)bus;
224 (void)addr;
225 (void)buf;
226 (void)len;
227 return EMBED_FALSE;
228}
229
230EMBED_INLINE embed_bool protocore_i2c_read(uint16_t addr, uint8_t *buf, size_t len)
231{
232 return protocore_i2c_read_on((uint8_t)PROTOCORE_I2C_BUS, addr, buf, len);
233}
234
235EMBED_INLINE embed_bool protocore_i2c_write_read_on(uint8_t bus, uint16_t addr, const uint8_t *w, size_t wlen,
236 uint8_t *r, size_t rlen)
237{
238 (void)bus;
239 (void)addr;
240 (void)w;
241 (void)wlen;
242 (void)r;
243 (void)rlen;
244 return EMBED_FALSE;
245}
246
247EMBED_INLINE embed_bool protocore_i2c_write_read(uint16_t addr, const uint8_t *w, size_t wlen, uint8_t *r, size_t rlen)
248{
249 return protocore_i2c_write_read_on((uint8_t)PROTOCORE_I2C_BUS, addr, w, wlen, r, rlen);
250}
251
252EMBED_INLINE embed_bool protocore_i2c_set_clock_on(uint8_t bus, uint32_t hz)
253{
254 (void)bus;
255 (void)hz;
256 return EMBED_TRUE;
257}
258
259EMBED_INLINE embed_bool protocore_i2c_set_clock(uint32_t hz)
260{
261 (void)hz;
262 return EMBED_TRUE;
263}
264
265EMBED_INLINE embed_bool protocore_i2c_probe_on(uint8_t bus, uint16_t addr)
266{
267 (void)bus;
268 (void)addr;
269 return EMBED_FALSE;
270}
271
272EMBED_INLINE embed_bool protocore_i2c_probe(uint16_t addr)
273{
274 return protocore_i2c_probe_on((uint8_t)PROTOCORE_I2C_BUS, addr);
275}
276
277EMBED_INLINE size_t protocore_i2c_scan_on(uint8_t bus, uint8_t *out, size_t cap)
278{
279 size_t n = 0;
280 for (uint16_t a = PROTOCORE_I2C_SCAN_FIRST; a <= PROTOCORE_I2C_SCAN_LAST && n < cap; a++)
281 {
282 if (protocore_i2c_probe_on(bus, a))
283 {
284 out[n] = (uint8_t)a;
285 n++;
286 }
287 }
288 return n;
289}
290
291EMBED_INLINE size_t protocore_i2c_scan(uint8_t *out, size_t cap)
292{
293 return protocore_i2c_scan_on((uint8_t)PROTOCORE_I2C_BUS, out, cap);
294}
295
296EMBED_INLINE embed_bool protocore_i2c_general_call_on(uint8_t bus, const uint8_t *buf, size_t len)
297{
298 return protocore_i2c_write_on(bus, PROTOCORE_I2C_GENERAL_CALL, buf, len);
299}
300
301EMBED_INLINE embed_bool protocore_i2c_general_call(const uint8_t *buf, size_t len)
302{
303 return protocore_i2c_general_call_on((uint8_t)PROTOCORE_I2C_BUS, buf, len);
304}
305
306EMBED_INLINE embed_bool protocore_i2c_recover_on(uint8_t bus, int sda, int scl)
307{
308 (void)bus;
309 (void)sda;
310 (void)scl;
311 return EMBED_FALSE;
312}
313
314EMBED_INLINE embed_bool protocore_i2c_recover(void)
315{
316 return EMBED_FALSE;
317}
318
319#endif // PROTOCORE_HAS_BUS
320
321EMBED_END_DECLS
322
323#endif // PROTOCORE_I2C_H
#define PROTOCORE_I2C_SDA_PIN
Shared I2C bus pins for the sensor / peripheral drivers (RTC, SHT3x, MPR121, ADS1115,...
#define PROTOCORE_I2C_SCL_PIN
EMBED_INLINE embed_bool protocore_i2c_write_read(uint16_t addr, const uint8_t *w, size_t wlen, uint8_t *r, size_t rlen)
Definition i2c.h:247
EMBED_INLINE embed_bool protocore_i2c_write_on(uint8_t bus, uint16_t addr, const uint8_t *buf, size_t len)
Definition i2c.h:207
#define PROTOCORE_I2C_TIMEOUT_MS
Per-transfer timeout in milliseconds.
Definition i2c.h:43
EMBED_INLINE embed_bool protocore_i2c_recover_on(uint8_t bus, int sda, int scl)
Definition i2c.h:306
EMBED_INLINE embed_bool protocore_i2c_general_call_on(uint8_t bus, const uint8_t *buf, size_t len)
Definition i2c.h:296
EMBED_INLINE size_t protocore_i2c_scan_on(uint8_t bus, uint8_t *out, size_t cap)
Definition i2c.h:277
EMBED_INLINE size_t protocore_i2c_scan(uint8_t *out, size_t cap)
Definition i2c.h:291
EMBED_INLINE embed_bool protocore_i2c_general_call(const uint8_t *buf, size_t len)
Definition i2c.h:301
#define PROTOCORE_I2C_HZ
Bus clock for the shared peripheral bus; 100 kHz standard mode.
Definition i2c.h:38
#define PROTOCORE_I2C_SCAN_FIRST
Lowest and highest 7-bit addresses a scan reports; 0x00 - 0x07 and 0x78 - 0x7F are reserved.
Definition i2c.h:60
EMBED_INLINE embed_bool protocore_i2c_recover(void)
Definition i2c.h:314
#define PROTOCORE_I2C_SCAN_LAST
Definition i2c.h:61
EMBED_INLINE embed_bool protocore_i2c_probe(uint16_t addr)
Definition i2c.h:272
EMBED_INLINE embed_bool protocore_i2c_write(uint16_t addr, const uint8_t *buf, size_t len)
Definition i2c.h:216
EMBED_INLINE embed_bool protocore_i2c_read(uint16_t addr, uint8_t *buf, size_t len)
Definition i2c.h:230
EMBED_BEGIN_DECLS EMBED_INLINE embed_bool protocore_i2c_begin_on(uint8_t bus, int sda, int scl, uint32_t hz)
Definition i2c.h:193
EMBED_INLINE embed_bool protocore_i2c_read_on(uint8_t bus, uint16_t addr, uint8_t *buf, size_t len)
Definition i2c.h:221
EMBED_INLINE embed_bool protocore_i2c_write_read_on(uint8_t bus, uint16_t addr, const uint8_t *w, size_t wlen, uint8_t *r, size_t rlen)
Definition i2c.h:235
EMBED_INLINE embed_bool protocore_i2c_set_clock_on(uint8_t bus, uint32_t hz)
Definition i2c.h:252
#define PROTOCORE_I2C_BUS
Controller the plain verbs drive.
Definition i2c.h:48
EMBED_INLINE embed_bool protocore_i2c_set_clock(uint32_t hz)
Definition i2c.h:259
EMBED_INLINE embed_bool protocore_i2c_begin(void)
Definition i2c.h:202
EMBED_INLINE embed_bool protocore_i2c_probe_on(uint8_t bus, uint16_t addr)
Definition i2c.h:265
The platform contract: what the library asks of a target, in the library's own words.