bcm2835  1.52
Functions
SPI access

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_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 (char *buf, uint32_t len)
 

Detailed Description

These functions let you use SPI0 (Serial Peripheral Interface) to interface with an external SPI device.

Function Documentation

int bcm2835_spi_begin ( void  )

Start SPI operations. 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 SPI interface. You should call bcm2835_spi_end() when all SPI funcitons are complete to return the pins to their default functions.

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)

Sets the chip select pin(s) When an bcm2835_spi_transfer() is made, the selected pin(s) will be asserted during the transfer.

Parameters
[in]csSpecifies the CS pins(s) that are used to activate the desired slave. One of BCM2835_SPI_CS*, see bcm2835SPIChipSelect
void bcm2835_spi_end ( void  )

End 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_setBitOrder ( uint8_t  order)

Sets the SPI bit order NOTE: has no effect. Not supported by SPI0. Defaults to

Parameters
[in]orderThe desired bit order, one of BCM2835_SPI_BIT_ORDER_*, see bcm2835SPIBitOrder
Examples:
spi.c, and spin.c.
void bcm2835_spi_setChipSelectPolarity ( uint8_t  cs,
uint8_t  active 
)

Sets the chip select pin polarity for a given pin When an 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.

Parameters
[in]csThe chip select pin to affect
[in]activeWhether the chip select pin is to be active HIGH
void bcm2835_spi_setClockDivider ( uint16_t  divider)

Sets the SPI clock divider and therefore the SPI clock speed.

Parameters
[in]dividerThe desired SPI clock divider, one of BCM2835_SPI_CLOCK_DIVIDER_*, see bcm2835SPIClockDivider
void bcm2835_spi_setDataMode ( uint8_t  mode)

Sets the SPI data mode Sets the clock polariy and phase

Parameters
[in]modeThe desired data mode, one of BCM2835_SPI_MODE*, see bcm2835SPIMode
uint8_t bcm2835_spi_transfer ( uint8_t  value)

Transfers one byte to and from the currently selected SPI slave. 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
[in]valueThe 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 
)

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
[in,out]bufBuffer of bytes to send. Received bytes will replace the contents
[in]lenNumber of bytes int eh 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 
)

Transfers any number of bytes to and from the currently selected SPI slave. 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 read from the slave is placed into rbuf. 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
[in]tbufBuffer of bytes to send.
[out]rbufReceived bytes will by put in this buffer
[in]lenNumber of bytes in the tbuf buffer, and the number of bytes to send/received
See also
bcm2835_spi_transfer()
void bcm2835_spi_writenb ( char *  buf,
uint32_t  len 
)

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

Parameters
[in]bufBuffer of bytes to send.
[in]lenNumber of bytes in the tbuf buffer, and the number of bytes to send