Robot Control Library
SPI

Description

General purpose C interface to the Linux SPI driver.

<rc/spi.h>

It allows use of both dedicated hardware slave-select pins as well as GPIO pins for slave select. While this was developed on the BeagleBone platform, it should also work on Raspberry Pi and other embedded Linux systems.

For the Robotics Cape and BeagleBone blue, the SPI bus 1 is broken out on two JST SH 6-pin sockets labeled SPI1.1 and SPI1.2 These share clock and serial IO signals, but have independent slave select lines so two devices can share the same bus. Note that these ports labeled for slaves 1 and 2 correspond to slaves 0 and 1 in software. To make source code more clear, the macros RC_BB_SPI1_SS1 and RC_BB_SPI1_SS2 are provided in this header which are defined as 1,0 and 1,1. the GPIO channels corresponding to these slave select pins are also provided as macros in this header.For example:

pinout on Robotics Cape and BeagleBone Blue:

  1. GND
  2. 3.3V
  3. MOSI (P9_30)
  4. MISO (P9_29)
  5. SCK (P9_31)
  6. Slave Select

The slaves can be selected automatically by the SPI Linux driver or manually with rc_spi_select() function. On the Robotics Cape, slave 1 can be used in either mode, but slave 2 must be selected manually. On the BB Blue either slave can be used in manual or automatic modes. Only initialize once with either mode.

Author
James Strawson
Date
1/19/2018

Macros

#define RC_SPI_MAX_SPEED   24000000
 24mhz More...
 
#define RC_SPI_MIN_SPEED   1000
 1khz More...
 
#define RC_SPI_BITS_PER_WORD   8
 only allow for 8-bit words More...
 
#define RC_BB_SPI1_SS1   1,0
 bus and slave to use for Robotics Cape and BeagleBone Blue SPI1.1 port More...
 
#define RC_BB_SPI1_SS2   1,1
 bus and slave to use for Robotics Cape and BeagleBone Blue SPI1.2 port More...
 
#define RC_CAPE_SS1_GPIO   3,17
 Robotics Cape SPI1 SS1 gpio P9_28, normally AUTO mode. More...
 
#define RC_CAPE_SS2_GPIO   1,17
 Robotics Cape SPI1 SS2 gpio P9_23, normally MANUAL GPIO mode. More...
 
#define RC_BLUE_SS1_GPIO   0,29
 BeagleBone Blue SPI1 SS1 gpio 0_29 pin H18. More...
 
#define RC_BLUE_SS2_GPIO   0,7
 BeagleBone Blue SPI1 SS2 gpio 0_7 pin H18. More...
 

Functions

int rc_spi_init_auto_slave (int bus, int slave, int bus_mode, int speed_hz)
 Initializes an SPI bus. More...
 
int rc_spi_init_manual_slave (int bus, int slave, int bus_mode, int speed_hz, int chip, int pin)
 Initializes an SPI bus and GPIO pin for use as a manual SPI slave select pin. More...
 
int rc_spi_get_fd (int bus, int slave)
 fetches the file descriptor for a specified slave so the user can do more advanced IO operations than what's presented here More...
 
int rc_spi_close (int bus)
 Closes and cleans up the bus for specified slave. More...
 
int rc_spi_manual_select (int bus, int slave, int select)
 Manually selects or deselects a slave. More...
 
int rc_spi_transfer (int bus, int slave, uint8_t *tx_data, size_t tx_bytes, uint8_t *rx_data)
 Send any sequence of bytes and read the response. More...
 
int rc_spi_write (int bus, int slave, uint8_t *data, size_t bytes)
 Writes data to specified slave. More...
 
int rc_spi_read (int bus, int slave, uint8_t *data, size_t bytes)
 Reads data from a specified slave. More...
 

Macro Definition Documentation

◆ RC_SPI_MAX_SPEED

#define RC_SPI_MAX_SPEED   24000000

24mhz

◆ RC_SPI_MIN_SPEED

#define RC_SPI_MIN_SPEED   1000

1khz

◆ RC_SPI_BITS_PER_WORD

#define RC_SPI_BITS_PER_WORD   8

only allow for 8-bit words

◆ RC_BB_SPI1_SS1

#define RC_BB_SPI1_SS1   1,0

bus and slave to use for Robotics Cape and BeagleBone Blue SPI1.1 port

◆ RC_BB_SPI1_SS2

#define RC_BB_SPI1_SS2   1,1

bus and slave to use for Robotics Cape and BeagleBone Blue SPI1.2 port

◆ RC_CAPE_SS1_GPIO

#define RC_CAPE_SS1_GPIO   3,17

Robotics Cape SPI1 SS1 gpio P9_28, normally AUTO mode.

◆ RC_CAPE_SS2_GPIO

#define RC_CAPE_SS2_GPIO   1,17

Robotics Cape SPI1 SS2 gpio P9_23, normally MANUAL GPIO mode.

◆ RC_BLUE_SS1_GPIO

#define RC_BLUE_SS1_GPIO   0,29

BeagleBone Blue SPI1 SS1 gpio 0_29 pin H18.

◆ RC_BLUE_SS2_GPIO

#define RC_BLUE_SS2_GPIO   0,7

BeagleBone Blue SPI1 SS2 gpio 0_7 pin H18.

Function Documentation

◆ rc_spi_init_auto_slave()

int rc_spi_init_auto_slave ( int  bus,
int  slave,
int  bus_mode,
int  speed_hz 
)

Initializes an SPI bus.

For description of the 4 SPI modes, see https://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Mode_numbers

Parameters
[in]busThe bus
[in]slaveThe slave
[in]bus_modeSPI_MODE_0, SPI_MODE_1, SPI_MODE_2, or SPI_MODE_3
[in]speed_hzThe speed hz
Returns
0 on succcess or -1 on failure
Examples:
rc_spi_loopback.c.

◆ rc_spi_init_manual_slave()

int rc_spi_init_manual_slave ( int  bus,
int  slave,
int  bus_mode,
int  speed_hz,
int  chip,
int  pin 
)

Initializes an SPI bus and GPIO pin for use as a manual SPI slave select pin.

The provided gpio chip/pin will then be remembered and tied to the provided slave number and bus. Note that on the BeagleBone and probably other platforms, there are only two files provided by the driver for interfacing to the bus, /dev/spi1.0 and /dev/spi1.1. When using a slave in manual mode, the first interface (slave 0 in software) will be used to talk to the manual slaves. Therefore this slave can not be used as an automatic slave.

For the BeagleBone Blue and RoboticsCape, this will also ensure that the pinmux is set correctly for that pin. The available manual slave select pins for these two boards are defined in this header for convenience. If using other boards it's up to the user to make sure the pin they are using is set up correctly in the device tree.

Parameters
[in]busThe spi bus
[in]slaveThe slave identifier (up to 16)
[in]bus_modeThe bus mode
[in]speed_hzThe speed hz
[in]chipThe gpio chip
[in]pinThe gpio pin
Returns
0 on succcess or -1 on failure

◆ rc_spi_get_fd()

int rc_spi_get_fd ( int  bus,
int  slave 
)

fetches the file descriptor for a specified slave so the user can do more advanced IO operations than what's presented here

Parameters
[in]busThe bus
[in]slave0 or 1
Returns
fd or -1 on failure

◆ rc_spi_close()

int rc_spi_close ( int  bus)

Closes and cleans up the bus for specified slave.

Parameters
[in]busSPI bus to close
Returns
0 on succcess or -1 on failure
Examples:
rc_spi_loopback.c.

◆ rc_spi_manual_select()

int rc_spi_manual_select ( int  bus,
int  slave,
int  select 
)

Manually selects or deselects a slave.

Only works if slave was initialized with SPI_SLAVE_MODE_MANUAL. If SPI_SLAVE_MODE_AUTO was selected then the SPI driver will handle this automatically when reading or writing.

Parameters
[in]busSPI bus to use
[in]slaveslave id
[in]select0 to deselect, otherwise selects
Returns
0 on succcess or -1 on failure

◆ rc_spi_transfer()

int rc_spi_transfer ( int  bus,
int  slave,
uint8_t *  tx_data,
size_t  tx_bytes,
uint8_t *  rx_data 
)

Send any sequence of bytes and read the response.

This is a wrapper for the ioctl spi transfer function and is generally what you will use for reading/writing device registers.

Parameters
[in]busSPI bus to use
[in]slaveslave id
[in]tx_datapointer to data to send
[in]tx_bytesnumber of bytes to send
rx_datapointer to put response data
Returns
number of bytes received or -1 on failure
Examples:
rc_spi_loopback.c.

◆ rc_spi_write()

int rc_spi_write ( int  bus,
int  slave,
uint8_t *  data,
size_t  bytes 
)

Writes data to specified slave.

Parameters
[in]busSPI bus to use
[in]slaveslave id
datadata pointer
[in]bytesnumber of bytes to send
Returns
returns number of bytes written or -1 on failure

◆ rc_spi_read()

int rc_spi_read ( int  bus,
int  slave,
uint8_t *  data,
size_t  bytes 
)

Reads data from a specified slave.

Parameters
[in]busSPI bus to use
[in]slaveslave id
datadata poitner
[in]bytesnumber of bytes to read
Returns
number of bytes read or -1 on failure