spi(3)

πŸ’– spi β€” SPI access πŸ’–

okay bestie, so this is the spi(3) page from the Library Functions Manual ✨ and it's literally all about making your Raspberry Pi talk to external SPI devices. it's giving hardware-girlie energy, no cap πŸ¦‹


🌸 NAME 🌸

spi β€” SPI access πŸ’…


✨ SYNOPSIS ✨

πŸŽ€ Functions πŸŽ€

int bcm2835_spi_begin (void)
void bcm2835_spi_end (void)
void bcm2835_spi_setBitOrder (uint8_t order)
void bcm2835_spi_setClockDivider (uint16_t divider)
void bcm2835_spi_set_speed_hz (uint32_t speed_hz)
void bcm2835_spi_setDataMode (uint8_t mode)
void bcm2835_spi_chipSelect (uint8_t cs)
void bcm2835_spi_setChipSelectPolarity (uint8_t cs, uint8_t active)
uint8_t bcm2835_spi_transfer (uint8_t value)
void bcm2835_spi_transfernb (char *tbuf, char *rbuf, uint32_t len)
void bcm2835_spi_transfern (char *buf, uint32_t len)
void bcm2835_spi_writenb (const char *buf, uint32_t len)
void bcm2835_spi_write (uint16_t data)
int bcm2835_aux_spi_begin (void)
void bcm2835_aux_spi_end (void)
void bcm2835_aux_spi_setClockDivider (uint16_t divider)
uint16_t bcm2835_aux_spi_CalcClockDivider (uint32_t speed_hz)
void bcm2835_aux_spi_write (uint16_t data)
void bcm2835_aux_spi_writenb (const char *buf, uint32_t len)
void bcm2835_aux_spi_transfern (char *buf, uint32_t len)
void bcm2835_aux_spi_transfernb (const char *tbuf, char *rbuf, uint32_t
    len)
uint8_t bcm2835_aux_spi_transfer (uint8_t value)

πŸ¦‹ Detailed Description πŸ¦‹

ok so these functions let you use SPI0 (Serial Peripheral Interface) to interface with an external SPI device. that's the whole vibe. that's the whole point. ✨


πŸ’œ Function Documentation πŸ’œ

int bcm2835_aux_spi_begin (void) [extern]

Starts AUX SPI operations, bestie πŸ’« it forces the RPi AUX SPI pins P1-38 (MOSI), P1-38 (MISO), P1-40 (CLK) and P1-36 (CE2) to alternate function ALT4, which is what actually enables those pins for the SPI interface.

(yes, the original really does say P1-38 twice up there, i'm just the messenger πŸ™ˆ)

Returns

  • 1 if successful, 0 otherwise β€” perhaps because you are not running as root πŸ’€

uint16_t bcm2835_aux_spi_CalcClockDivider (uint32_t speed_hz) [extern]

Calculates the input for

See also

  • bcm2835_aux_spi_setClockDivider

Parameters

  • speed_hz β€” A value between

See also

  • BCM2835_AUX_SPI_CLOCK_MIN and
  • BCM2835_AUX_SPI_CLOCK_MAX

Returns

  • Input for

See also

  • bcm2835_aux_spi_setClockDivider
ngl this section is a lil bit of a cliffhanger, i'm just telling you what it says πŸ’€

void bcm2835_aux_spi_end (void) [extern]

Ends AUX SPI operations πŸŒ™ the SPI1 pins P1-38 (MOSI), P1-38 (MISO), P1-40 (CLK) and P1-36 (CE2) get returned to their default INPUT behaviour. so they go back to being regular lil pins πŸŽ€


void bcm2835_aux_spi_setClockDivider (uint16_t divider) [extern]

Sets the AUX SPI clock divider, which is basically the same thing as setting the AUX SPI clock speed πŸ”₯

Parameters

  • divider β€” The desired AUX SPI clock divider.

uint8_t bcm2835_aux_spi_transfer (uint8_t value) [extern]

Transfers one byte to and from the AUX SPI slave ✨ it clocks the 8 bit value out on MOSI, and simultaneously clocks in data from MISO. then it returns the read data byte from the slave. two-way street, main character energy 🫢

Parameters

  • value β€” The 8 bit data byte to write to MOSI

Returns

  • The 8 bit byte simultaneously read from MISO

See also

  • bcm2835_aux_spi_transfern()

void bcm2835_aux_spi_transfern (char * buf, uint32_t len) [extern]

Transfers any number of bytes to and from the AUX SPI slave, using bcm2835_aux_spi_transfernb under the hood. the returned data from the slave replaces the transmitted data in the buffer β€” so yeah, your buffer gets rewritten, that's the whole trick πŸ’…

Parameters

  • buf β€” Buffer of bytes to send. Received bytes will replace the contents
  • len β€” Number of bytes in the buffer, and the number of bytes to send/received

See also

  • bcm2835_aux_spi_transfer()

void bcm2835_aux_spi_transfernb (const char * tbuf, char * rbuf, uint32_t len) [extern]

Transfers any number of bytes to and from the AUX SPI slave πŸ’« it asserts the CE2 pin during the transfer. clocks the len 8 bit bytes out on MOSI, and simultaneously clocks in data from MISO. the data read from the slave gets placed into rbuf.

warning bestie β€” rbuf must be at least len bytes long or you're gonna have a bad time 😭

Parameters

  • tbuf β€” Buffer of bytes to send.
  • rbuf β€” Received bytes will by put in this buffer
  • len β€” Number of bytes in the tbuf buffer, and the number of bytes to send/received

void bcm2835_aux_spi_write (uint16_t data) [extern]

Transfers half-word to the AUX SPI slave 🌸 it asserts the currently selected CS pins during the transfer.

Parameters

  • data β€” The 8 bit data byte to write to MOSI

Returns

  • The 16 bit byte simultaneously read from MISO

See also

  • bcm2835_spi_transfern()

void bcm2835_aux_spi_writenb (const char * buf, uint32_t len) [extern]

Transfers any number of bytes to the AUX SPI slave (no reading back this time, it's a one-way moment πŸ’…). asserts the CE2 pin during the transfer.

Parameters

  • buf β€” Buffer of bytes to send.
  • len β€” Number of bytes in the tbuf buffer, and the number of bytes to send

int bcm2835_spi_begin (void) [extern]

Starts SPI operations, periodt ✨ it forces RPi SPI0 pins P1-19 (MOSI), P1-21 (MISO), P1-23 (CLK), P1-24 (CE0) and P1-26 (CE1) to alternate function ALT0, which enables those pins for the SPI interface.

you should call bcm2835_spi_end() when all SPI functions are complete, so the pins go back to their default functions. clean up after yourself, queen πŸ‘‘

See also

  • bcm2835_spi_end()

Returns

  • 1 if successful, 0 otherwise β€” perhaps because you are not running as root πŸ’€

void bcm2835_spi_chipSelect (uint8_t cs) [extern]

Sets the chip select pin(s) πŸŽ€ when a bcm2835_spi_transfer() is made, the selected pin(s) will be asserted during the transfer.

Parameters

  • cs β€” Specifies the CS pin(s) that are used to activate the desired slave. One of BCM2835_SPI_CS*, see bcm2835SPIChipSelect

void bcm2835_spi_end (void) [extern]

Ends SPI operations πŸŒ™ SPI0 pins P1-19 (MOSI), P1-21 (MISO), P1-23 (CLK), P1-24 (CE0) and P1-26 (CE1) are returned to their default INPUT behaviour.


void bcm2835_spi_set_speed_hz (uint32_t speed_hz) [extern]

Sets the SPI clock divider by converting the speed parameter into the equivalent SPI clock divider. ( see

See also

  • bcm2835_spi_setClockDivider

Parameters

  • speed_hz β€” The desired SPI clock speed in Hz

void bcm2835_spi_setBitOrder (uint8_t order) [extern]

Sets the SPI bit order πŸ” it sets the bit order used for transmit and receive. the bcm2835 SPI0 only supports BCM2835_SPI_BIT_ORDER_MSB, so if you pick BCM2835_SPI_BIT_ORDER_LSB, the bytes get reversed in software instead. the library defaults to BCM2835_SPI_BIT_ORDER_MSB.

Parameters

  • order β€” The desired bit order, one of BCM2835_SPI_BIT_ORDER_*, see bcm2835SPIBitOrder

void bcm2835_spi_setChipSelectPolarity (uint8_t cs, uint8_t active) [extern]

Sets the chip select pin polarity for a given pin ✨ when a bcm2835_spi_transfer() occurs, the currently selected chip select pin(s) will be asserted to the value given by active. when transfers are not happening, the chip select pin(s) return to the complement (inactive) value. so it flips back on its own, cute πŸŽ€

Parameters

  • cs β€” The chip select pin to affect
  • active β€” Whether the chip select pin is to be active HIGH

void bcm2835_spi_setClockDivider (uint16_t divider) [extern]

Sets the SPI clock divider, and therefore the SPI clock speed πŸ”₯

Parameters

  • divider β€” The desired SPI clock divider, one of BCM2835_SPI_CLOCK_DIVIDER_*, see bcm2835SPIClockDivider

void bcm2835_spi_setDataMode (uint8_t mode) [extern]

Sets the SPI data mode β€” that's the clock polarity and phase 🌈

Parameters

  • mode β€” The desired data mode, one of BCM2835_SPI_MODE*, see bcm2835SPIMode

uint8_t bcm2835_spi_transfer (uint8_t value) [extern]

Transfers one byte to and from the currently selected SPI slave πŸ’« it asserts the currently selected CS pins (as previously set by bcm2835_spi_chipSelect) during the transfer. clocks the 8 bit value out on MOSI, and simultaneously clocks in data from MISO. returns the read data byte from the slave. uses polled transfer as per section 10.6.1 of the BCM 2835 ARM Peripherls manual πŸ“–

Parameters

  • value β€” The 8 bit data byte to write to MOSI

Returns

  • The 8 bit byte simultaneously read from MISO

See also

  • bcm2835_spi_transfern()

void bcm2835_spi_transfern (char * buf, uint32_t len) [extern]

Transfers any number of bytes to and from the currently selected SPI slave, using bcm2835_spi_transfernb. the returned data from the slave replaces the transmitted data in the buffer 🫢

Parameters

  • buf β€” Buffer of bytes to send. Received bytes will replace the contents
  • len β€” Number of bytes in the buffer, and the number of bytes to send/received

See also

  • bcm2835_spi_transfer()

void bcm2835_spi_transfernb (char * tbuf, char * rbuf, uint32_t len) [extern]

Transfers any number of bytes to and from the currently selected SPI slave ✨ it asserts the currently selected CS pins (as previously set by bcm2835_spi_chipSelect) during the transfer. clocks the len 8 bit bytes out on MOSI, and simultaneously clocks in data from MISO. the data read from the slave gets placed into rbuf.

bestie listen β€” rbuf must be at least len bytes long 😭

uses polled transfer as per section 10.6.1 of the BCM 2835 ARM Peripherls manual πŸ“–

Parameters

  • tbuf β€” Buffer of bytes to send.
  • rbuf β€” Received bytes will by put in this buffer
  • len β€” Number of bytes in the tbuf buffer, and the number of bytes to send/received

See also

  • bcm2835_spi_transfer()

void bcm2835_spi_write (uint16_t data) [extern]

Transfers half-word to the currently selected SPI slave πŸ’… it asserts the currently selected CS pins (as previously set by bcm2835_spi_chipSelect) during the transfer. clocks the 8 bit value out on MOSI, and simultaneously clocks in data from MISO. uses polled transfer as per section 10.6.1 of the BCM 2835 ARM Peripherls manual πŸ“–

Parameters

  • data β€” The 8 bit data byte to write to MOSI

See also

  • bcm2835_spi_writenb()

void bcm2835_spi_writenb (const char * buf, uint32_t len) [extern]

Transfers any number of bytes to the currently selected SPI slave 🌸 it asserts the currently selected CS pins (as previously set by bcm2835_spi_chipSelect) during the transfer.

Parameters

  • buf β€” Buffer of bytes to send.
  • len β€” Number of bytes in the buf buffer, and the number of bytes to send

πŸ‘‘ Author πŸ‘‘

Generated automatically by Doxygen for bcm2835 from the source code. the robots wrote this one, not me πŸ’€βœ¨


and that's the whole page bestie, you ate it πŸ’… this was spi(3) from bcm2835, Version 1.75 πŸŽ€ go forth and clock some bits, ily 🫢🌸

πŸ“¦ where this page comes from