Standard Library: SoftSPI

Overview

The Software SPI (SoftSPI) library allows the creation of any number of software based (bit-banged) SPI ports. Serial Peripheral Interface (SPI) is a four wire synchronous serial interface used by many integrated circuits and electronic devices. SPI devices can operate as either master devices or as slave device. The four SPI signals are generally referred to as Slave Select (SS), Master Out, Slave In (MOSI), Master In, Slave Out (MISO), and Serial Clock (SCK). A master device generates SS, MOSI and SCK, and receives MISO. A slave device receives SS, MOSI, and SCK and transmits MISO. The SS signal is used to enable the slave device, and this signal is only significant for slave devices. A master device can use any general purpose I/O pin to generate SS to enable a slave.

The SoftSPI library only supports operation as an SPI master device.

An SPI transaction begins with the master device bringing SS low. When the slave ses SS go low it becomes enabled and waits for the master to send data. The master shifts data out on MOSI and simultaneously shifts data in on MISO. The slave device receives data from the master on its MOSI pin and simultaneously sends data to the master on its MISO pin. Each time the master sends a byte to the slave, it simultaneously receives a byte from the slave. The master generates the clock signal (SCK) that is used to control the shifting of the data in both the master and the slave. SPI devices can operate using one of four data transfer modes, and one of two shift directions. The transfer modes specify the idle state of the clock signal (idles high or idles low) and the phase relationship between the active edge of the clock signal and the data. The modes are generally called mode 0 through mode 3. The shift direction specifies whether the data is shifted most significant bit first (shift left), or least significant bit first (shift right). The SoftSPI library header file defines symbols used to specify the transfer mode and shift direction. Refer to documentation for the SPI slave device being used to determine the transfer mode and shift direction to use. Most SPI devices use mode 0 with shift left. The SoftSPI library defines an object class (SoftSPI) that is used to create a separate object for each desired SPI port. Each instance of the SoftSPI object class uses four specified digital pins for the SPI signals. The SoftSPI object class supports all four transfer modes, both shift directions, and setting the frequency of the clock signal.

To use the SoftSPI library, an object instance variable of the SoftSPI object class must be created. The object instance is initialized using functions to set mode, shift direction and clock speed. Data can then be transferred to a slave device by calling the various data transfer functions.

Defined Interface Symbols

The following symbols are defined in the SoftSPI header file (SoftSPI.h) and can be used with the various configuration functions:

Transfer Mode

  • SSPI_MODE0
  • SSPI_MODE1
  • SSPI_MODE2
  • SSPI_MODE3

These correspond to the four standard SPI transfer modes: 0, 1, 2, and 3. More detailed information about this can generally be found in the manufacturer’s data sheet for the SPI device being used.

Shift Direction

  • SSPI_SHIFT_LEFT
  • SSPI_SHIFT_RIGHT

Default Clock Speed

  • SSPI_SPEED_DEFAULT – select the default SPI clock speed, 500Khz.

SoftSPI Functions

The following are functions defined by the SoftSPI library:

Initialization and Setup Functions

Data Transfer Functions