SPI

API Reference
How To Setup SPI
yoctoOutofTreeKernelModule


Overview

This API is used by apps to control a Serial Peripheral Interface (SPI).

The SPI API is configured as a four wire interface:

  • Clock - Serial Clock
  • MOSI - Master Output
  • MISO - Master Input
  • SS or CS - Chip select

Usage

The sample code in this section shows how to use the SPI API for a user space app.

Handle for passing to related functions to access the SPI device:

le_spi_Open() opens the spi file handle:

res = le_spi_Open("spidev0.0", &spiHandle);
LE_FATAL_IF(res != LE_OK, "le_spi_Open failed with result=%s", LE_RESULT_TXT(res));

le_spi_Configure() initializes all the parameters on the master side based on the implementation below uses mode = 0, 8 bit data, 960kbps and MSB first:

le_spi_Configure(spiHandle, 0, 8, 960000, 0);

le_spi_WriteHD() writes data to slave device in half-duplex mode: write_buffer_tx is an array. Example defined below:

uinti8_t write_buffer_tx[] ={0xC7, 0x94, 0x80, 0x9A};
res = le_spi_WriteHD(spiHandle, write_buffer_tx, NUM_ARRAY_MEMBERS(write_buffer_tx));
LE_FATAL_IF(res != LE_OK, "le_spi_WriteHD failed with result=%s", LE_RESULT_TXT(res));

le_spi_WriteReadHD() writes and reads to/from slave device in half-duplex mode:

le_spi_WriteReadHD(spiHandle, read_ID_tx, NUM_ARRAY_MEMBERS(read_ID_tx), ID_rx,
&readBufferSize);
LE_FATAL_IF(res != LE_OK, "le_spi_WriteReadHD failed with result=%s", LE_RESULT_TXT(res));

read_ID_tx is an array that is transmitted to the device. ID_rx is a buffer reserved for data received from the device.

le_spi_WriteReadFD() writes and reads to/from slave in full-duplex mode:

res = le_spi_WriteReadHD(spiHandle, read_buffer_tx, read_rx, NUM_ARRAY_MEMBERS(read_buffer_tx);
LE_FATAL_IF(res != LE_OK, "le_spi_WriteReadHD failed with result=%s", LE_RESULT_TXT(res));

read_buffer_tx is an array transmitted to the device. read_rx is a buffer reserved for data received from the device. Buffer size for tx and rx must be the same.

le_spi_Close() closes the spi handle:

le_spi_Close(spiHandle);