3.24. spi — Serial Peripheral Interface

Source code: src/drivers/spi.h, src/drivers/spi.c


Defines

SPI_MODE_SLAVE 0
SPI_MODE_MASTER 1
SPI_SPEED_8MBPS SPI_PORT_SPEED_8MBPS
SPI_SPEED_4MBPS SPI_PORT_SPEED_4MBPS
SPI_SPEED_2MBPS SPI_PORT_SPEED_2MBPS
SPI_SPEED_1MBPS SPI_PORT_SPEED_1MBPS
SPI_SPEED_500KBPS SPI_PORT_SPEED_500KBPS
SPI_SPEED_250KBPS SPI_PORT_SPEED_250KBPS
SPI_SPEED_125KBPS SPI_PORT_SPEED_125KBPS

Functions

int spi_module_init(void)

Initialize SPI module. This function must be called before calling any other function in this module.

The module will only be initialized once even if this function is called multiple times.

Return
zero(0) or negative error code.

int spi_init(struct spi_driver_t *self_p, struct spi_device_t *dev_p, struct pin_device_t *ss_pin_p, int mode, int speed, int polarity, int phase)

Initialize driver object.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object to initialize.
  • dev_p: Device to use.
  • ss_pin_p: Slave select pin device.
  • mode: Master or slave mode.
  • speed: Speed in kbps.
  • polarity: Set to 0 or 1.
  • phase: Set to 0 or 1.

int spi_start(struct spi_driver_t *self_p)

Start given SPI driver. Configures the SPI hardware.

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized driver object.

int spi_stop(struct spi_driver_t *self_p)

Stop given SPI driver. Deconfigures the SPI hardware if given driver currently ownes the bus.

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized driver object.

int spi_take_bus(struct spi_driver_t *self_p)

In multi master application the driver must take ownership of the SPI bus before performing data transfers. Will re-configure the SPI hardware if configured by another driver.

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized driver object.

int spi_give_bus(struct spi_driver_t *self_p)

In multi master application the driver must give ownership of the SPI bus to let other masters take it.

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized driver object.

int spi_select(struct spi_driver_t *self_p)

Select the slave by asserting the slave select pin.

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized driver object.

int spi_deselect(struct spi_driver_t *self_p)

Deselect the slave by de-asserting the slave select pin.

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized driver object.

ssize_t spi_transfer(struct spi_driver_t *self_p, void *rxbuf_p, const void *txbuf_p, size_t size)

Simultaniuos read/write operation over the SPI bus.

Return
Number of transferred bytes or negative error code.
Parameters
  • self_p: Initialized driver object.
  • rxbuf_p: Buffer to read into.
  • txbuf_p: Buffer to write.
  • size: Number of bytes to transfer.

ssize_t spi_read(struct spi_driver_t *self_p, void *buf_p, size_t size)

Read data from the SPI bus.

Return
Number of read bytes or negative error code.
Parameters
  • self_p: Initialized driver object.
  • buf_p: Buffer to read into.
  • size: Number of bytes to receive.

ssize_t spi_write(struct spi_driver_t *self_p, const void *buf_p, size_t size)

Write data to the SPI bus.

Return
Number of written bytes or negative error code.
Parameters
  • self_p: Initialized driver object.
  • buf_p: Buffer to write.
  • size: Number of bytes to write.

ssize_t spi_get(struct spi_driver_t *self_p, uint8_t *data_p)

Get one byte of data from the SPI bus.

Return
Number of read bytes or negative error code.
Parameters
  • self_p: Initialized driver object.
  • data_p: Read data.

ssize_t spi_put(struct spi_driver_t *self_p, uint8_t data)

Put one byte of data to the SPI bus.

Return
Number of written bytes or negative error code.
Parameters
  • self_p: Initialized driver object.
  • data: data to write.

Variables

struct spi_device_t spi_device[SPI_DEVICE_MAX]