SPI(9)

πŸ“¦ version 11.0, in nixpkgs unstable

πŸ’… 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) β€” sets mode and speed for 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 an errno value, 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 field st->st_errno gets 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