MySensors Library & Examples  2.3.2-62-ge298769
Functions
SPI access

Detailed Description

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

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)
 

Function Documentation

◆ bcm2835_aux_spi_begin()

int bcm2835_aux_spi_begin ( void  )

Start AUX SPI operations. Forces RPi AUX SPI pins P1-36 (MOSI), P1-38 (MISO), P1-40 (CLK) and P1-36 (CE2) to alternate function ALT4, which enables those pins for SPI interface.

Returns
1 if successful, 0 otherwise (perhaps because you are not running as root)

◆ bcm2835_aux_spi_CalcClockDivider()

uint16_t bcm2835_aux_spi_CalcClockDivider ( uint32_t  speed_hz)

Calculates the input for

See also
bcm2835_aux_spi_setClockDivider
Parameters
speed_hzA value between
See also
BCM2835_AUX_SPI_CLOCK_MIN and
BCM2835_AUX_SPI_CLOCK_MAX
Returns
Input for
See also
bcm2835_aux_spi_setClockDivider

◆ bcm2835_aux_spi_end()

void bcm2835_aux_spi_end ( void  )

End AUX SPI operations. SPI1 pins P1-36 (MOSI), P1-38 (MISO), P1-40 (CLK) and P1-36 (CE2) are returned to their default INPUT behaviour.

◆ bcm2835_aux_spi_setClockDivider()

void bcm2835_aux_spi_setClockDivider ( uint16_t  divider)

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

Parameters
[in]dividerThe desired AUX SPI clock divider.

◆ bcm2835_aux_spi_transfern()

void bcm2835_aux_spi_transfern ( char *  buf,
uint32_t  len 
)

Transfers any number of bytes to and from the AUX SPI slave using bcm2835_aux_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_aux_spi_transfer()

◆ bcm2835_aux_spi_transfernb()

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

Transfers any number of bytes to and from the AUX SPI slave. 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 read from the slave is placed into rbuf. rbuf must be at least len bytes long

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

◆ bcm2835_aux_spi_write()

void bcm2835_aux_spi_write ( uint16_t  data)

Transfers half-word to and from the AUX SPI slave. Asserts the currently selected CS pins during the transfer.

Parameters
[in]dataThe 8 bit data byte to write to MOSI
Returns
The 8 bit byte simultaneously read from MISO
See also
bcm2835_spi_transfern()

◆ bcm2835_aux_spi_writenb()

void bcm2835_aux_spi_writenb ( const char *  buf,
uint32_t  len 
)

Transfers any number of bytes to the AUX SPI slave. Asserts the CE2 pin 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

◆ bcm2835_spi_begin()

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)

◆ bcm2835_spi_chipSelect()

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

◆ bcm2835_spi_end()

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.

◆ bcm2835_spi_set_speed_hz()

void bcm2835_spi_set_speed_hz ( uint32_t  speed_hz)

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

See also
bcm2835_spi_setClockDivider)
Parameters
[in]speed_hzThe desired SPI clock speed in Hz

◆ bcm2835_spi_setBitOrder()

void bcm2835_spi_setBitOrder ( uint8_t  order)

Sets the SPI bit order Set the bit order to be used for transmit and receive. The bcm2835 SPI0 only supports BCM2835_SPI_BIT_ORDER_MSB, so if you select BCM2835_SPI_BIT_ORDER_LSB, the bytes will be reversed in software. The library defaults to BCM2835_SPI_BIT_ORDER_MSB.

Parameters
[in]orderThe desired bit order, one of BCM2835_SPI_BIT_ORDER_*, see bcm2835SPIBitOrder

◆ bcm2835_spi_setChipSelectPolarity()

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

◆ bcm2835_spi_setClockDivider()

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

◆ bcm2835_spi_setDataMode()

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

◆ bcm2835_spi_transfer()

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()

◆ 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()

◆ bcm2835_spi_transfernb()

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()

◆ bcm2835_spi_write()

void bcm2835_spi_write ( uint16_t  data)

Transfers half-word 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]dataThe 8 bit data byte to write to MOSI
See also
bcm2835_spi_writenb()

◆ bcm2835_spi_writenb()

void bcm2835_spi_writenb ( const 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 buf buffer, and the number of bytes to send