Since | Origin / Contributor | Maintainer | Source |
---|---|---|---|
2015-01-16 | Ibrahim Abd Elkader | Arnim Läuger | spi.c |
All transactions for sending and receiving are most-significant-bit first and least-significant last. For technical details of the underlying hardware refer to metalphreak’s ESP8266 HSPI articles.
The high level functions provide a send & receive API for half- and full-duplex mode. Sent and received data items are restricted to 1 - 32 bit length and each data item is surrounded by (H)SPI CS inactive.
Receive data from SPI.
spi.recv(id, size[, default_data])
id
SPI ID number: 0 for SPI, 1 for HSPIsize
number of data items to be readdefault_data
default data being sent on MOSI (all-1 if omitted)String containing the bytes read from SPI.
####See also spi.send()
Send data via SPI in half-duplex mode. Send & receive data in full-duplex mode.
HALFDUPLEX:
wrote = spi.send(id, data1[, data2[, ..., datan]])
FULLDUPLEX:
wrote[, rdata1[, ..., rdatan]] = spi.send(id, data1[, data2[, ..., datan]])
id
SPI ID number: 0 for SPI, 1 for HSPIdata
data can be either a string, a table or an integer number.databits
number of bits.wrote
number of written bytesrdata
received data when configured with spi.FULLDUPLEX
=spi.send(1, 0, 255, 255, 255)
4 255 192 32 0
x = {spi.send(1, 0, 255, 255, 255)}
=x[1]
4
=x[2]
255
=x[3]
192
=x[4]
32
=x[5]
0
=x[6]
nil
=#x
5
_, _, x = spi.send(1, 0, {255, 255, 255})
=x[1]
192
=x[2]
32
=x[3]
0
Set up the SPI configuration. Refer to Serial Peripheral Interface Bus for details regarding the clock polarity and phase definition.
spi.setup(id, mode, cpol, cpha, databits, clock_div[, duplex_mode])
id
SPI ID number: 0 for SPI, 1 for HSPImode
select master or slave mode
spi.MASTER
spi.SLAVE
- not supported currentlycpol
clock polarity selection
spi.CPOL_LOW
spi.CPOL_HIGH
cpha
clock phase selection
spi.CPHA_LOW
spi.CPHA_HIGH
databits
number of bits per data item 1 - 32clock_div
SPI clock divider, f(SPI) = f(CPU) / clock_div
duplex_mode
duplex mode
spi.HALFDUPLEX
(default when omitted)spi.FULLDUPLEX
Number: 1
The low level functions provide a hardware-centric API for application scenarios that need to excercise more complex SPI transactions. The programming model is built up around the HW send and receive buffers and SPI transactions are initiated with full control over the hardware features.
Extract data items from MISO buffer after spi.transaction()
.
data1[, data2[, ..., datan]] = spi.get_miso(id, offset, bitlen, num)
id
SPI ID number: 0 for SPI, 1 for HSPIoffset
bit offset into MISO buffer for first data itembitlen
bit length of a single data itemnum
number of data items to retrieve####Returns
num
data items
Insert data items into MOSI buffer for spi.transaction()
.
spi.set_mosi(id, offset, bitlen, data1[, data2[, ..., datan]])
####Parameters
id
SPI ID number: 0 for SPI, 1 for HSPIoffset
bit offset into MOSI buffer for inserting data1 and subsequent itemsbitlen
bit length of data1, data2, …data
data items where bitlen
number of bits are considered for the transaction.nil
Start an SPI transaction, consisting of up to 5 phases:
spi.transaction(id, cmd_bitlen, cmd_data, addr_bitlen, addr_data, mosi_bitlen, dummy_bitlen, miso_bitlen)
id
SPI ID number: 0 for SPI, 1 for HSPIcmd_bitlen
bit length of the command phase (0 - 16)cmd_data
data for command phaseaddr_bitlen
bit length for address phase (0 - 32)addr_data
data for command phasemosi_bitlen
bit length of the MOSI phase (0 - 512)dummy_bitlen
bit length of the dummy phase (0 - 256)miso_bitlen
bit length of the MISO phase (0 - 512) for half-duplex.####Returns
nil
####See also