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_DeviceHandleRef_t spiHandle;

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);

Kernel module dependency

SPI Service has a build time dependency on the spisvc.ko kernel module for the subset of targets on which the feature is currently supported (currently, WP76, WP77, WP75, WP85). That means, spiService.adef file and default.sdef file both include kernelModules section that lists spisvc.mdef as a dependency.

Note
If the users is building their own configuration, extra attention has to be paid to preserve this logic, i.e. retain the build-time dependency and avoid duplicated inclusion of the spisvc.mdef (which may cause build errors).

Automatic installation/removal of the kernel modules

SPI service is started manually, i.e. it is not running by default. When the user starts spiService, the install.sh scripts takes care of loading both "spidev" and "spisvc" kernel modules. Conversely, when spiService is stopped, the remove.sh script uloads both kernel modules.