SPI(9)
π SPI(9) β the Serial Peripheral Interface kernel interface, bestie edition π
okay so this is the SPI(9) page from the Kernel Developer's Manual, and it's literally your whole guide to talking to SPI devices from inside the kernel. no cap, it's a good one. let's get into it β¨
πΈ NAME πΈ
spi_configure, spi_transfer, spi_transfer_init, spi_chunk_init, spi_transfer_add, spi_wait, spi_done, spi_send, spi_recv, spi_send_recv β Serial Peripheral Interface (SPI) kernel interface π«Ά
β¨ SYNOPSIS β¨
like, here's the whole cast, in their natural habitat:
#include <dev/spi/spivar.h>
int
spi_configure(struct spi_handle *sh, int mode, int speed);
int
spi_transfer(struct spi_handle *sh, struct spi_transfer *st);
void
spi_transfer_init(struct spi_transfer *st);
void
spi_chunk_init(struct spi_chunk *chunk, int cnt, const uint8_t *wptr,
uint8_t *rptr);
void
spi_transfer_add(struct spi_transfer *st, struct spi_chunk *chunk);
void
spi_wait(struct spi_transfer *st);
void
spi_done(struct spi_transfer *st, int err);
int
spi_recv(struct spi_handle *sh, int cnt, uint8_t *data);
int
spi_send(struct spi_handle *sh, int cnt, const uint8_t *data);
int
spi_send_recv(struct spi_handle *sh, int scnt, const uint8_t *snd,
int rcnt, const uint8_t *rcv);
π¦ DESCRIPTION π¦
so, SPI? it's a 4-wire synchronous full-duplex serial bus π« and it's the moment for hooking up stuff like EEPROMs, displays, and honestly just, like, a whole aesthetic of integrated circuits. the spi interface is literally the way you communicate with SPI-connected devices. it's giving matchmaker, honestly. π₯
π FUNCTIONS π
these are the functions that make up the API you get, as a driver author, for SPI-connected devices. main character energy, all of them. π
the struct spi_handle that corresponds to your device gets passed in through the driver attachment. (iykyk π«Ά)
spi_configure(sh, mode, speed)β setsmodeandspeedfor all the communication that comes after this, with a SPI slave. set the vibe, then go. β¨spi_transfer(sh, st)β queues a transfer to the SPI controller. and if it couldn't be queued? it hands you back anerrnovalue, so you're not left guessing. πspi_transfer_init(st)β initializes a transfer structure. fresh start, blank slate, new era. πspi_chunk_init(chunk, cnt, wptr, rptr")β initializes a chunk structure. every chunk is a possibly bi-directional transfer, where the same amount of bytes gets shifted in and out. balanced. symmetric. iconic. βοΈspi_transfer_add(st, chunk)β appends a chunk onto a transfer structure. little chunk, big transfer, we love a team effort. π«Άspi_wait(st)β waits for a transfer to complete. and if the transfer failed for some reason? the fieldst->st_errnogets set to a non-zero value, so you will know. πspi_done(st, err)β this is called back by the machine-dependent backend to signal that a transfer completed. the backend is basically tapping you on the shoulder like "hey bestie, it's done". π
for simplicity there are also convenience functions that bundle up common operations for you. these return an errno value if the transfer failed. slay, honestly. π§
spi_recv(sh, cnt, data)β prepares a chunk for receiving data, queues a transfer, then waits for it to complete. the whole routine, no thinking required. πspi_send(sh, cnt, data)β prepares a chunk for sending data, queues a transfer, then waits for it to complete. same energy, opposite direction. πspi_send_recv(sh, scnt, snd, rcnt, rcv)β prepares two chunks: one for sending data first, then one for receiving an answer. queues a transfer and waits for it to complete. β
heads up bestie: this is not a full-duplex operation. it's send, then receive. do not confuse the two, i'm begging. π
π SEE ALSO π
π°οΈ HISTORY π°οΈ
the spi API first appeared in NetBSD 4.0. a legacy. a pioneer. we stan. π
and that's the page, bestie!! π mwah π β this little gem came to you courtesy of Debian, dated February 23, 2019, and yes i'm absolutely signing off with that because she deserves her credit. ππΈ
π¦ where this page comes from
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c) 2019 The NetBSD Foundation
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms