ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
spi.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 spi.h
6 * @brief The one owner of the shared SPI bus for the peripheral drivers.
7 *
8 * The sibling of i2c.h, for the drivers whose part is on SPI rather than I2C (the interface
9 * bridge, the W5500 Ethernet, the radio modules). They share one bus and bring it up through
10 * these verbs. The pins come from PROTOCORE_SPI_MOSI_PIN / PROTOCORE_SPI_MISO_PIN / PROTOCORE_SPI_SCLK_PIN (default
11 * -1 = the platform's default host pins). Re-begin is idempotent, so per-driver calls are
12 * harmless.
13 *
14 * SPI is symmetric: every clock shifts a bit out and a bit in, so one transfer verb covers the
15 * three shapes a driver needs. Pass a null @p rx to discard what comes back (a write), a null
16 * @p tx to clock zeros out (a read), or both to exchange.
17 *
18 * ::protocore_spi_txn runs at the configured clock, bit order and mode; ::protocore_spi_txn_at names its own,
19 * which is what a bus carrying parts with different timing needs.
20 *
21 * ::protocore_spi_txn_ext adds the framing a flash, a display controller or an ADC front end expects: a
22 * command, an address, dummy clocks, and a data phase one, two or four bits wide. The controller
23 * drives each phase, so the data buffer holds data alone.
24 *
25 * Chip select is the caller's: which pin selects a part is a board fact, and a driver often holds
26 * it across several transfers. ::protocore_spi_cs_idle / ::protocore_spi_cs_select / ::protocore_spi_cs_release drive
27 * it through the GPIO seam.
28 *
29 * The bodies compile wherever the platform states a bus.
30 *
31 * @author Douglas Quigg (dstroy0)
32 * @date 2026
33 */
34
35#ifndef PROTOCORE_SPI_H
36#define PROTOCORE_SPI_H
37
39
40#include "protocore_config.h"
41
42/** @brief Bus clock for the shared peripheral bus; 1 MHz is safe on every part on it. */
43#ifndef PROTOCORE_SPI_HZ
44#define PROTOCORE_SPI_HZ 1000000u
45#endif
46
47/** @brief Clock polarity and phase, as SPI mode 0..3. Mode 0 is what these parts use. */
48#ifndef PROTOCORE_SPI_MODE
49#define PROTOCORE_SPI_MODE 0u
50#endif
51
52/** @brief Controller the plain verbs drive. */
53#ifndef PROTOCORE_SPI_HOST
54#define PROTOCORE_SPI_HOST 0u
55#endif
56
57/** @brief Third and fourth data lines, for a quad-width bus; -1 leaves the bus single or dual. */
58#ifndef PROTOCORE_SPI_QUADWP_PIN
59#define PROTOCORE_SPI_QUADWP_PIN (-1)
60#endif
61#ifndef PROTOCORE_SPI_QUADHD_PIN
62#define PROTOCORE_SPI_QUADHD_PIN (-1)
63#endif
64
65EMBED_BEGIN_DECLS
66
67#if PROTOCORE_HAS_BUS
68
69/** @brief Bring up @p host on the given pins; -1 on quadwp / quadhd leaves the bus single or dual. */
70EMBED_INLINE embed_bool protocore_spi_begin_on(uint8_t host, int mosi, int miso, int sclk, int quadwp, int quadhd)
71{
72 return protocore_platform_spi_begin(host, mosi, miso, sclk, quadwp, quadhd) != 0;
73}
74
75/** @brief Bring up the shared SPI bus on the PROTOCORE_SPI_*_PIN pins (-1 = default). */
76EMBED_INLINE embed_bool protocore_spi_begin(void)
77{
80}
81
82/**
83 * @brief Clock @p len bytes on @p host at @p hz, @p bit_order and @p mode, shifting @p tx out and
84 * @p rx in. A null @p rx discards the inbound bits; a null @p tx clocks zeros out.
85 */
86EMBED_INLINE embed_bool protocore_spi_txn_on(uint8_t host, uint32_t hz, uint8_t bit_order, uint8_t mode,
87 const uint8_t *tx, uint8_t *rx, size_t len)
88{
89 return protocore_platform_spi_txn(host, hz, bit_order, mode, tx, rx, (uint32_t)len) != 0;
90}
91
92/** @brief Clock @p len bytes on the shared bus at @p hz, @p bit_order and @p mode. */
93EMBED_INLINE embed_bool protocore_spi_txn_at(uint32_t hz, uint8_t bit_order, uint8_t mode, const uint8_t *tx,
94 uint8_t *rx, size_t len)
95{
96 return protocore_spi_txn_on((uint8_t)PROTOCORE_SPI_HOST, hz, bit_order, mode, tx, rx, len);
97}
98
99/**
100 * @brief Clock @p len bytes, shifting @p tx out and @p rx in.
101 *
102 * A null @p rx discards the inbound bits; a null @p tx clocks zeros out.
103 */
104EMBED_INLINE embed_bool protocore_spi_txn(const uint8_t *tx, uint8_t *rx, size_t len)
105{
106 return protocore_spi_txn_at(PROTOCORE_SPI_HZ, PROTOCORE_SPI_MSBFIRST, (uint8_t)PROTOCORE_SPI_MODE, tx, rx, len);
107}
108
109/** @brief Clock @p len bytes out, discarding what shifts back. */
110EMBED_INLINE embed_bool protocore_spi_write(const uint8_t *tx, size_t len)
111{
112 return protocore_spi_txn(tx, NULL, len);
113}
114
115/** @brief Clock @p len zero bytes out, keeping what shifts back. */
116EMBED_INLINE embed_bool protocore_spi_read(uint8_t *rx, size_t len)
117{
118 return protocore_spi_txn(NULL, rx, len);
119}
120
121/**
122 * @brief A framed transfer on @p host: a @p cmd_bits command, an @p addr_bits address,
123 * @p dummy_bits idle clocks, then @p len data bytes at @p lanes bits per clock.
124 *
125 * A zero bit count omits that phase. @p lanes is PROTOCORE_SPI_LANES_1, _2 or _4.
126 */
127EMBED_INLINE embed_bool protocore_spi_txn_ext_on(uint8_t host, uint32_t hz, uint8_t bit_order, uint8_t mode,
128 uint16_t cmd, uint8_t cmd_bits, uint32_t addr, uint8_t addr_bits,
129 uint8_t dummy_bits, uint8_t lanes, const uint8_t *tx, uint8_t *rx,
130 size_t len)
131{
132 return protocore_platform_spi_txn_ext(host, hz, bit_order, mode, cmd, cmd_bits, addr, addr_bits, dummy_bits, lanes,
133 tx, rx, (uint32_t)len) != 0;
134}
135
136/** @brief A framed transfer on the shared bus at the configured clock, bit order and mode. */
137EMBED_INLINE embed_bool protocore_spi_txn_ext(uint16_t cmd, uint8_t cmd_bits, uint32_t addr, uint8_t addr_bits,
138 uint8_t dummy_bits, uint8_t lanes, const uint8_t *tx, uint8_t *rx,
139 size_t len)
140{
141 return protocore_spi_txn_ext_on((uint8_t)PROTOCORE_SPI_HOST, PROTOCORE_SPI_HZ, PROTOCORE_SPI_MSBFIRST,
142 (uint8_t)PROTOCORE_SPI_MODE, cmd, cmd_bits, addr, addr_bits, dummy_bits, lanes, tx,
143 rx, len);
144}
145
146/** @brief Drive @p pin as an output at the deselected level, which is how a part is left idle. */
147EMBED_INLINE void protocore_spi_cs_idle(uint8_t pin)
148{
149 protocore_platform_gpio_mode(pin, PROTOCORE_GPIO_OUT);
150 protocore_platform_gpio_write(pin, PROTOCORE_GPIO_HIGH);
151}
152
153/** @brief Pull @p pin low, selecting the part for the transfers that follow. */
154EMBED_INLINE void protocore_spi_cs_select(uint8_t pin)
155{
156 protocore_platform_gpio_write(pin, PROTOCORE_GPIO_LOW);
157}
158
159/** @brief Let @p pin back high, deselecting the part. */
160EMBED_INLINE void protocore_spi_cs_release(uint8_t pin)
161{
162 protocore_platform_gpio_write(pin, PROTOCORE_GPIO_HIGH);
163}
164
165#else // no bus seam
166
167EMBED_INLINE embed_bool protocore_spi_begin_on(uint8_t host, int mosi, int miso, int sclk, int quadwp, int quadhd)
168{
169 (void)host;
170 (void)mosi;
171 (void)miso;
172 (void)sclk;
173 (void)quadwp;
174 (void)quadhd;
175 return EMBED_TRUE;
176}
177
178EMBED_INLINE embed_bool protocore_spi_begin(void)
179{
180 return EMBED_TRUE;
181}
182
183EMBED_INLINE embed_bool protocore_spi_txn_on(uint8_t host, uint32_t hz, uint8_t bit_order, uint8_t mode,
184 const uint8_t *tx, uint8_t *rx, size_t len)
185{
186 (void)host;
187 (void)hz;
188 (void)bit_order;
189 (void)mode;
190 (void)tx;
191 (void)rx;
192 (void)len;
193 return EMBED_FALSE;
194}
195
196EMBED_INLINE embed_bool protocore_spi_txn_at(uint32_t hz, uint8_t bit_order, uint8_t mode, const uint8_t *tx,
197 uint8_t *rx, size_t len)
198{
199 return protocore_spi_txn_on((uint8_t)PROTOCORE_SPI_HOST, hz, bit_order, mode, tx, rx, len);
200}
201
202EMBED_INLINE embed_bool protocore_spi_txn(const uint8_t *tx, uint8_t *rx, size_t len)
203{
204 return protocore_spi_txn_at(PROTOCORE_SPI_HZ, PROTOCORE_SPI_MSBFIRST, (uint8_t)PROTOCORE_SPI_MODE, tx, rx, len);
205}
206
207EMBED_INLINE embed_bool protocore_spi_write(const uint8_t *tx, size_t len)
208{
209 return protocore_spi_txn(tx, NULL, len);
210}
211
212EMBED_INLINE embed_bool protocore_spi_read(uint8_t *rx, size_t len)
213{
214 return protocore_spi_txn(NULL, rx, len);
215}
216
217EMBED_INLINE embed_bool protocore_spi_txn_ext_on(uint8_t host, uint32_t hz, uint8_t bit_order, uint8_t mode,
218 uint16_t cmd, uint8_t cmd_bits, uint32_t addr, uint8_t addr_bits,
219 uint8_t dummy_bits, uint8_t lanes, const uint8_t *tx, uint8_t *rx,
220 size_t len)
221{
222 (void)host;
223 (void)hz;
224 (void)bit_order;
225 (void)mode;
226 (void)cmd;
227 (void)cmd_bits;
228 (void)addr;
229 (void)addr_bits;
230 (void)dummy_bits;
231 (void)lanes;
232 (void)tx;
233 (void)rx;
234 (void)len;
235 return EMBED_FALSE;
236}
237
238EMBED_INLINE embed_bool protocore_spi_txn_ext(uint16_t cmd, uint8_t cmd_bits, uint32_t addr, uint8_t addr_bits,
239 uint8_t dummy_bits, uint8_t lanes, const uint8_t *tx, uint8_t *rx,
240 size_t len)
241{
242 (void)cmd;
243 (void)cmd_bits;
244 (void)addr;
245 (void)addr_bits;
246 (void)dummy_bits;
247 (void)lanes;
248 (void)tx;
249 (void)rx;
250 (void)len;
251 return EMBED_FALSE;
252}
253
254EMBED_INLINE void protocore_spi_cs_idle(uint8_t pin)
255{
256 (void)pin;
257}
258
259EMBED_INLINE void protocore_spi_cs_select(uint8_t pin)
260{
261 (void)pin;
262}
263
264EMBED_INLINE void protocore_spi_cs_release(uint8_t pin)
265{
266 (void)pin;
267}
268
269#endif // PROTOCORE_HAS_BUS
270
271EMBED_END_DECLS
272
273#endif // PROTOCORE_SPI_H
#define PROTOCORE_SPI_MISO_PIN
#define PROTOCORE_SPI_SCLK_PIN
#define PROTOCORE_SPI_MOSI_PIN
Shared SPI bus pins for the peripheral drivers, the same way the I2C pins above are shared....
The platform contract: what the library asks of a target, in the library's own words.
EMBED_INLINE embed_bool protocore_spi_txn(const uint8_t *tx, uint8_t *rx, size_t len)
Definition spi.h:202
#define PROTOCORE_SPI_MODE
Clock polarity and phase, as SPI mode 0..3. Mode 0 is what these parts use.
Definition spi.h:49
EMBED_INLINE embed_bool protocore_spi_txn_ext(uint16_t cmd, uint8_t cmd_bits, uint32_t addr, uint8_t addr_bits, uint8_t dummy_bits, uint8_t lanes, const uint8_t *tx, uint8_t *rx, size_t len)
Definition spi.h:238
EMBED_INLINE embed_bool protocore_spi_txn_ext_on(uint8_t host, uint32_t hz, uint8_t bit_order, uint8_t mode, uint16_t cmd, uint8_t cmd_bits, uint32_t addr, uint8_t addr_bits, uint8_t dummy_bits, uint8_t lanes, const uint8_t *tx, uint8_t *rx, size_t len)
Definition spi.h:217
EMBED_INLINE embed_bool protocore_spi_txn_on(uint8_t host, uint32_t hz, uint8_t bit_order, uint8_t mode, const uint8_t *tx, uint8_t *rx, size_t len)
Definition spi.h:183
EMBED_INLINE embed_bool protocore_spi_write(const uint8_t *tx, size_t len)
Definition spi.h:207
#define PROTOCORE_SPI_QUADWP_PIN
Third and fourth data lines, for a quad-width bus; -1 leaves the bus single or dual.
Definition spi.h:59
#define PROTOCORE_SPI_QUADHD_PIN
Definition spi.h:62
EMBED_BEGIN_DECLS EMBED_INLINE embed_bool protocore_spi_begin_on(uint8_t host, int mosi, int miso, int sclk, int quadwp, int quadhd)
Definition spi.h:167
EMBED_INLINE void protocore_spi_cs_release(uint8_t pin)
Definition spi.h:264
#define PROTOCORE_SPI_HZ
Bus clock for the shared peripheral bus; 1 MHz is safe on every part on it.
Definition spi.h:44
EMBED_INLINE void protocore_spi_cs_select(uint8_t pin)
Definition spi.h:259
EMBED_INLINE embed_bool protocore_spi_begin(void)
Definition spi.h:178
EMBED_INLINE embed_bool protocore_spi_read(uint8_t *rx, size_t len)
Definition spi.h:212
#define PROTOCORE_SPI_HOST
Controller the plain verbs drive.
Definition spi.h:54
EMBED_INLINE void protocore_spi_cs_idle(uint8_t pin)
Definition spi.h:254
EMBED_INLINE embed_bool protocore_spi_txn_at(uint32_t hz, uint8_t bit_order, uint8_t mode, const uint8_t *tx, uint8_t *rx, size_t len)
Definition spi.h:196