-
Notifications
You must be signed in to change notification settings - Fork 225
SPI Module
This module contains functions for accessing the CPU's SPI module.
SPI units are encoded into a byte and are platform-dependent. For this reason the SPI module define a numeric constant for each available SPI unit. For example in ESP32 SPI1 is defined by the constant spi.SPI1. Please refer to your platform or board documentation to know which SPI units are available. If you refer to an inexistent SPI, a nil value is returned.
SPI module can work in master or slave mode, each of then identified by spi.MASTER and spi.SLAVE constants. For now only spi.MASTER is allowed.
The Serial Peripheral Interface (SPI) bus is a synchronous serial communication interface specification used for short distance communication, primarily in embedded systems. The interface was developed by Motorola in the late eighties and has become a de facto standard.
SPI devices communicate in full duplex mode using a master-slave architecture with a single master. The master device originates the frame for reading and writing.
An SPI use 4 signals:
- CLK / SCK: Serial Clock (output from master).
- MOSI / SDO: Master Output, Slave Input (output from master).
- MISO / SDI: Master Input, Slave Output (output from slave).
- SS / CS : Slave Select (active low, output from master).
Multiple slave devices can share the same SPI bus (SCK, SDO and SDI lines), and each of them are selected through individual CS lines per device, when the master needs to communicate with one device; in addition each device can work at different speeds, clock polarity, etc ...
To use this module you must take into consideration the following:
-
Attach an SPI device, using the spi.attach function. The attach function returns an instance of the SPI device. You must store this instance into a variable for further operation with it.
device = spi.attach(.....)
-
Use the device instance for select the device. This selects the device trough the CS pin connected to the device and configures the bus (speed, polarity, etc ...):
device:select()
-
Use the device instance for read from / write to the device:
device:write(...)
-
Use the device instance for deselect the device. This deselects the device trough the CS pin connected to the device and disconnects the device from the bus:
instance:deselect()
Attach a SPI device.
Arguments:
-
id: SPI unit identifier. Use spi.SPIx defined for this purpose.
-
mode: SPI mode, can be either spi.MASTER or spi.SLAVE.
-
cs: device's chip select pin, for example gpio.GPIO5. If cs is 0 the default cs pin for this SPI unit is used.
-
speed: speed of the spi module, expressed in hertz.
-
data bits: can be either, 8, 16 or 32 bits.
-
mode number: an integer between 0 and 3, according the following table:
mode number polarity phase 0 0 0 1 0 1 2 1 0 3 1 1 -
flags (optional): a bit mask formed by the following constants
- spi.READ: attach the spi device as read only (only MISO is used)
- spi.WRITE: attach the spi device as write only (only MOSI is used)
- default value is spi.READ | spi.WRITE: attach the spi device as read / write (MISO and MOSI are used)
Returns: spi device instance, or an exception. You must store this instance into a variable for further operations with it.
-- Attach a SPI device using SPI2 as master, 1 Mhz speed, 8 bits, using GPIO5 as CS, number mode 0
device = spi.attach(spi.SPI2, spi.MASTER, pio.GPIO5, 1000000, 8, 0)
Select the SPI device.
Arguments: nothing. Returns: nothing, or an exception.
-- Attach a SPI device using SPI2 as master, 1 Mhz speed, 8 bits, using GPIO5 as CS, number mode 0
device = spi.attach(spi.SPI2, spi.MASTER, pio.GPIO5, 1000000, 8, 0)
-- Select
device:select()
-- Do something
...
...
Deselect the SPI device.
Arguments: nothing. Returns: nothing, or an exception.
-- Attach a SPI device using SPI2 as master, 1 Mhz speed, 8 bits, using GPIO5 as CS, number mode 0
device = spi.attach(spi.SPI2, spi.MASTER, pio.GPIO5, 1000000, 8, 0)
-- Select
device:select()
-- Do something
...
...
-- Deselect
device:deselect()
Write data to the spi device.
Arguments:
- data1 to datan: data to write, can be either a byte or a string.
Returns: nothing, or an exception.
-- Attach a SPI device using SPI2 as master, 1 Mhz speed, 8 bits, using GPIO5 as CS, number mode 0
device = spi.attach(spi.SPI2, spi.MASTER, pio.GPIO5, 1000000, 8, 0)
-- Select
device:select()
-- Do something
device:write("Hello, Lua RTOS!")
-- Deselect
device:deselect()
Write and read data to a SPI device.
Arguments:
- data1 to datan: data to write, can be either a byte or a string.
Returns: readed data, or an exception.
-- Attach a SPI device using SPI2 as master, 1 Mhz speed, 8 bits, using GPIO5 as CS, number mode 0
device = spi.attach(spi.SPI2, spi.MASTER, pio.GPIO5, 1000000, 8, 0)
-- Select
device:select()
-- Write and read
print(device:write("Hello, Lua RTOS!"))
-- Deselect
device:deselect()