Soft SPI

Software implemented Serial Peripheral Interface bus.

This module provides a software implemented Serial Peripheral Interface bus. It is intended to be used in situation where hardware spi is not available. The signatures of the functions are similar to the functions declared in spi.h The clock speed is approximated by using xtimer_nanosleep. Currently only the use of MOSI in master mode is implemented. Therefore receiving data from a slave is currently not possible.

enum @158
SOFT_SPI_OK =  0
everything went as planned
SOFT_SPI_NODEV = -1
invalid SPI bus specified
SOFT_SPI_NOCS = -2
invalid chip select line specified
SOFT_SPI_NOMODE = -3
selected mode is not supported
SOFT_SPI_NOCLK = -4
selected clock value is not supported
enum soft_spi_mode_t
SOFT_SPI_MODE_0 = 0
CPOL=0, CPHA=0.
SOFT_SPI_MODE_1
CPOL=0, CPHA=1.
SOFT_SPI_MODE_2
CPOL=1, CPHA=0.
SOFT_SPI_MODE_3
CPOL=1, CPHA=1.
enum soft_spi_clk_t
SOFT_SPI_CLK_100KHZ = 5000
drive the SPI bus with less than 100kHz
SOFT_SPI_CLK_400KHZ = 1250
drive the SPI bus with less than 400kHz
SOFT_SPI_CLK_DEFAULT = 0
drive the SPI bus with maximum speed possible
unsigned int soft_spi_t

Default type for SPI devices.

gpio.h::gpio_t soft_spi_cs_t

Chip select pin type overlaps with gpio_t so it can be casted to this.

void soft_spi_init(soft_spi.h::soft_spi_t bus)

Basic initialization of the given SPI bus.

This function does the basic initialization including pin configuration for MISO, MOSI, and CLK pins.

Errors (e.g. invalid bus parameter) are not signaled through a return value, but should be signaled using the assert.h::assert function internally.

Note

This function MUST not be called more than once per bus!

Parameters

bus:SPI device to initialize

void soft_spi_init_pins(soft_spi.h::soft_spi_t bus)

Initialize the used SPI bus pins, i.e.

MISO, MOSI, and CLK

After calling soft_spi_init, the pins must be initialized. In normal cases, this function will not be used.

The pins used are configured in the board’s periph_conf.h.

Parameters

bus:SPI device the pins are configure for

int soft_spi_init_cs(soft_spi.h::soft_spi_t bus, soft_spi.h::soft_spi_cs_t cs)

Initialize the given chip select pin.

The chip select must be any generic GPIO pin (e.g. gpio.h::GPIO_PIN). It must be called once before the use of the chip select pin in transaction.

Parameters

bus:SPI device that is used with the given CS line
cs:chip select pin to initialize

Return values

  • SOFT_SPI_OK on success
  • SOFT_SPI_NODEV on invalid device
  • SOFT_SPI_NOCS on invalid CS pin/line
int soft_spi_acquire(soft_spi.h::soft_spi_t bus, soft_spi.h::soft_spi_cs_t cs, soft_spi.h::soft_spi_mode_t mode, soft_spi.h::soft_spi_clk_t clk)

Start a new SPI transaction.

Starting a new SPI transaction will get exclusive access to the SPI bus and configure it according to the given values. If another SPI transaction is active when this function is called, this function will block until the other transaction is complete (soft_spi_relase was called).

Note

This function expects the bus and the cs parameters to be valid (they are checked in soft_spi_init and soft_spi_init_cs before)

Parameters

bus:SPI device to access
cs:chip select pin/line to use
mode:mode to use for the new transaction
clk:bus clock speed to use for the transaction

Return values

  • SOFT_SPI_OK on success
  • SOFT_SPI_NOMODE if given mode is not supported
  • SOFT_SPI_NOCLK if given clock speed is not supported
void soft_spi_release(soft_spi.h::soft_spi_t bus)

Finish an ongoing SPI transaction by releasing the given SPI bus.

After release, the given SPI bus should be fully powered down until acquired again.

Parameters

bus:SPI device to release

uint8_t soft_spi_transfer_byte(soft_spi.h::soft_spi_t bus, soft_spi.h::soft_spi_cs_t cs, bool cont, uint8_t out)

Transfer one byte on the given SPI bus Currently only the use of MOSI in master mode is implemented.

Therefore receiving data from a slave is currently not possible.

Parameters

bus:SPI device to use
cs:chip select pin/line to use
cont:if true, keep device selected after transfer
out:byte to send out, set NULL if only receiving

Return values

  • the received byte
void soft_spi_transfer_bytes(soft_spi.h::soft_spi_t bus, soft_spi.h::soft_spi_cs_t cs, bool cont, const void * out, void * in, msp430_types.h::size_t len)

Transfer a number bytes using the given SPI bus.

Parameters

bus:SPI device to use
cs:chip select pin/line to use
cont:if true, keep device selected after transfer
out:buffer to send data from, set NULL if only receiving
in:buffer to read into, set NULL if only sending
len:number of bytes to transfer

uint8_t soft_spi_transfer_reg(soft_spi.h::soft_spi_t bus, soft_spi.h::soft_spi_cs_t cs, uint8_t reg, uint8_t out)

Transfer one byte to/from a given register address.

This function is a shortcut function for easier handling of SPI devices that implement a register based access scheme.

Parameters

bus:SPI device to use
cs:chip select pin/line to use
reg:register address to transfer data to/from
out:byte to send, set NULL if only receiving data

Return values

  • value that was read from the given register address
void soft_spi_transfer_regs(soft_spi.h::soft_spi_t bus, soft_spi.h::soft_spi_cs_t cs, uint8_t reg, const void * out, void * in, msp430_types.h::size_t len)

Transfer a number of bytes to/from a given register address.

This function is a shortcut function for easier handling of SPI devices that implement a register based access scheme.

Parameters

bus:SPI device to use
cs:chip select pin/line to use
reg:register address to transfer data to/from
out:buffer to send data from, set NULL if only receiving
in:buffer to read into, set NULL if only sending
len:number of bytes to transfer

SOFT_SPI_DEV( x)

Default SPI device access macro.

1
(x)
SOFT_SPI_UNDEF

Define global value for undefined SPI device.

1
(UINT_MAX)
SOFT_SPI_CS_UNDEF

Define value for unused CS line.

1
(GPIO_UNDEF)
struct soft_spi_conf_t

Software SPI port descriptor.

gpio.h::gpio_t miso_pin

MOSI pin.

gpio.h::gpio_t mosi_pin

MOSI pin.

gpio.h::gpio_t clk_pin

CLK pin.

soft_spi.h::soft_spi_mode_t soft_spi_mode

data and clock polarity

soft_spi.h::soft_spi_clk_t soft_spi_clk

clock speed