eLua platform interface - SPI

Overview

This part of the platform interface groups functions related to the SPI interface(s) of the MCU.

Data structures, constants and types

// SPI mode
#define PLATFORM_SPI_MASTER                   1
#define PLATFORM_SPI_SLAVE                    0 

Constants used to select/deselect the SPI SS pin (if applicable).

// SS values
#define PLATFORM_SPI_SELECT_ON                1
#define PLATFORM_SPI_SELECT_OFF               0

Constants used to select/deselect the SPI SS pin (if applicable).

typedef u32 spi_data_type;

This is the type of a SPI data word, thus limiting the maximum size of a SPI data work to 32 bits (which should be enough for all practical purposes).

Functions

int platform_spi_exists( unsigned id );

Checks if the platform has the hardware SPI specified as argument. Implemented in src/common.c, it uses the NUM_SPI macro that must be defined in the platform's platform_conf.h file (see here for details). For example:

#define NUM_SPI   1      // The platform has 1 SPI interface

Arguments: id - SPI interface ID

Returns: 1 if the SPI interface exists, 0 otherwise

u32 platform_spi_setup( unsigned id, int mode, u32 clock, unsigned cpol, unsigned cpha, unsigned databits );

This function is used to initialize the parameters of the SPI interface. NOTE: currently, only master SPI mode is implemented in eLua.

Arguments:

Returns: the actual clock set for the SPI interface. Depending on the hardware, this may have a different value than the clock argument.

spi_data_type platform_spi_send_recv( unsigned id, spi_data_type data );

Executes a SPI read/write cycle

Arguments:

  • id - SPI interface ID
  • data - data to be sent to the SPI interface

Returns: data read from the SPI interface