spi - spi Operation Library#

Adapted Air780E/Air700E Air780EP Air601 Air101/Air103 Air105 ESP32C3 ESP32S3

Note

This page document is automatically generated by this file. If there is any error, please submit issue or help modify pr, thank you!

Tip

This library has its own demo,click this link to view the demo example of spi

Tip

There are also video tutorials in this library, click this link to view

Constant#

constant

type

explanation

spi.MSB

number

Big Endian Mode

spi.LSB

number

Little Endian Mode

spi.master

number

Host Mode

spi.slave

number

slave mode

spi.full

number

Full Duplex

spi.half

number

half duplex

spi.SPI_0

number

SPI0

spi.SPI_1

number

SPI1

spi.SPI_2

number

SPI2

spi.SPI_3

number

SPI3

spi.SPI_4

number

SPI4

spi.HSPI_0

number

High-speed SPI0, currently 105 dedicated

spi.setup(id, cs, CPHA, CPOL, dataw, bandrate, bitdict, ms, mode)#

Set up and enable SPI

Parameters

Incoming Value Type

Explanation

int

SPI No., for example 0

int

CS Foot selection, not available in w600, please fill in nil

int

CPHA Default 0, optional 0/1

int

CPOL Default 0, optional 0/1

int

Data width, default 8bit

int

baud rate, default 2M=2000000

int

Size side, default spi.MSB, optional spi.LSB

int

Master-slave setting, default master 1, optional slave 0. Generally, only host mode is supported

int

Operating mode, full duplex 1, half duplex 0, default full duplex

Return Value

return value type

explanation

int

Returns 0 on success, otherwise returns another value

Examples

-- Initialization spi
spi.setup(0,20,0,0,8,2000000,spi.MSB,1,1)

spi.createSoft(cs, mosi, miso, clk, CPHA, CPOL, dataw, bitdict, ms, mode)#

Set up and enable the software SPI

Parameters

Incoming Value Type

Explanation

int

cs Pin number, incoming nil means Lua controls cs pin

int

mosi Pin number

int

miso Pin number

int

clk Pin number

int

Default 0, optional 0/1

int

Default 0, optional 0/1

int

Data width, default 8bit

int

Size side, default spi.MSB, optional spi.LSB

int

Master-slave setting, default master 1, optional slave 0. Generally, only host mode is supported

int

Operating mode, full duplex 1, half duplex 0, default half duplex

Return Value

return value type

explanation

Software SPI object

can be used as the ID of the SPI

Examples

-- Initialize software spi
local softSpiDevice = spi.createSoft(0, 1, 2, 3, 0, 0, 8, spi.MSB, 1, 1)
local result = spi.send(softSpiDevice, string.char(0x9f))

spi.close(id)#

Close the specified SPI

Parameters

Incoming Value Type

Explanation

int

SPI No., for example 0

Return Value

return value type

explanation

int

Returns 0 on success, otherwise returns another value

Examples

-- Initialization spi
spi.close(0)

spi.transfer(id, send_data, send_len, recv_len)#

Transferring SPI Data

Parameters

Incoming Value Type

Explanation

int

SPI number (e. g. 0) or software SPI object

string/zbuff

The data to be sent. If it is zbuff data, it will be read from the pointer where the object is located.

int

Optional. The length of the data to be sent. The default value is the length of data.

int

Optional. The length of the read data. The default value is 1

Return Value

return value type

explanation

string

Read successfully returns the string, otherwise it returns nil

Examples

-- Initialization spi
spi.setup(0,nil,0,0,8,2000000,spi.MSB,1,1)
local recv = spi.transfer(0, "123")--Send 123 and read data

local buff = zbuff.create(1024, 0x33) --Create a memory area with all initial values of 0x 33
local recv = spi.transfer(0, buff)--Start zbuff data from the pointer, send it all out, and read the data

spi.recv(id, size, buff)#

Receives SPI data of a specified length

Parameters

Incoming Value Type

Explanation

int

SPI No., for example 0

int

Data length

userdata

zbuff object, optional, 2024.3.29 New

Return Value

return value type

explanation

string/int

The string returned after the read is successful. If zbuff is passed in, the read size is returned. If an error is returned nil

Examples

-- Initialization spi
spi.setup(0,nil,0,0,8,2000000,spi.MSB,1,1)
-- Receive data
local recv = spi.recv(0, 4)--Receive 4 bytes of data

-- When the zbuff parameter is passed in, the return value is different. 2024.3.29 New
-- After a successful read, the pointer moves back len bytes
-- The write position starts with the current used() position, make sure there is enough space to write len-length data
local len = spi.recv(0, 4, buff)

spi.send(id, data[, len])#

Send SPI Data

Parameters

Incoming Value Type

Explanation

int

SPI No., for example 0

string/zbuff

The data to be sent. If it is zbuff data, it will be read from the pointer where the object is located.

int

Optional. The length of the data to be sent. The default value is the length of data.

Return Value

return value type

explanation

int

Send Results

Examples

-- Initialization spi
spi.setup(0,nil,0,0,8,2000000,spi.MSB,1,1)
local result = spi.send(0, "123")--Send 123

local buff = zbuff.create(1024, 0x33) --Create a memory area with all initial values of 0x 33
local result = spi.send(0, buff)--Start the zbuff data from the pointer and send it all out.

spi.deviceSetup(id, cs, CPHA, CPOL, dataw, bandrate, bitdict, ms, mode)#

Set up and enable SPI (object way)

Parameters

Incoming Value Type

Explanation

int

SPI No., for example 0

int

CS Foot selection, not available in w600, please fill in nil

int

CPHA Default 0, optional 0/1

int

CPOL Default 0, optional 0/1

int

Data width, default 8bit

int

baud rate, default 20M=20000000

int

Size side, default spi.MSB, optional spi.LSB

int

Master-slave setting, default master 1, optional slave 0. Generally, only host mode is supported

int

Operating mode, full duplex 1, half duplex 0, default full duplex

Return Value

return value type

explanation

userdata

spi_device

Examples

-- Initialization spi
local spi_device = spi.deviceSetup(0,17,0,0,8,2000000,spi.MSB,1,1)

spi_device:close()#

Close the specified SPI (object way)

Parameters

Incoming Value Type

Explanation

userdata

spi_device

Return Value

return value type

explanation

int

Returns 0 on success, otherwise returns another value

Examples

-- Initialization spi
spi_device.close()

spi_device:transfer(send_data[, len])#

Transfer SPI Data (Object Way)

Parameters

Incoming Value Type

Explanation

userdata

spi_device

string/zbuff

The data to be sent. If it is zbuff data, it will be read from the pointer where the object is located.

int

Optional. The length of the data to be sent. The default value is the length of data.

int

Optional. The length of the read data. The default value is 1

Return Value

return value type

explanation

string

Read successfully returns the string, otherwise it returns nil

Examples

-- Initialization spi
local spi_device = spi.device_setup(0,17,0,0,8,2000000,spi.MSB,1,1)
local recv = spi_device:transfer("123")--Send 123 and read data

local buff = zbuff.create(1024, 0x33) --Create a memory area with all initial values of 0x 33
local recv = spi_device:transfer(buff)--Start zbuff data from the pointer, send it all out, and read the data

spi_device:send(data[, len])#

Send SPI Data (Object Way)

Parameters

Incoming Value Type

Explanation

userdata

spi_device

string/zbuff

The data to be sent. If it is zbuff data, it will be read from the pointer where the object is located.

Return Value

return value type

explanation

int

Send Results

Examples

-- Initialization spi
local spi_device = spi.device_setup(0,17,0,0,8,2000000,spi.MSB,1,1)
local result = spi_device:send("123")--Send 123

local buff = zbuff.create(1024, 0x33) --Create a memory area with all initial values of 0x 33
local result = spi_device:send(buff)--Start the zbuff data from the pointer and send it all out.

spi_device:recv(size)#

Receives SPI data of a specified length (object method)

Parameters

Incoming Value Type

Explanation

userdata

spi_device

int

Data length

Return Value

return value type

explanation

string

Read successfully returns the string, otherwise it returns nil

Examples

-- Initialization spi
local spi_device = spi.device_setup(0,17,0,0,8,2000000,spi.MSB,1,1)
local recv = spi_device:recv(4)--Receive 4 bytes of data

spi.xfer(id, txbuff, rxbuff, rx_len, transfer_done_topic)#

The non-blocking hardware SPI transfers SPI data in order to improve core utilization. The API directly returns whether to start the transmission or not. After the transmission is completed, the API is suitable for hardware SPI to transmit a large amount of data. SPI and software SPI occupied by peripheral functions (LCD SPI,W5500 SPI, etc.) cannot be used. Traditional blocking type is recommended for a small amount of data transmission.API

Parameters

Incoming Value Type

Explanation

userdata

or int spi_device Or spi_id, note that if it is spi_device, you need to manually pull it up after the transmission is completed.cs!!!!!!

zbuff

The data to be sent, if nil, only receives the data, because of the non-blocking model, in order to ensure the validity of the dynamic data, can only use zbuff, the data sent from.zbuff.addr

zbuff

Receive data, if it is nil, only send data, due to the non-blocking model, in order to ensure the validity of dynamic data, only zbuff can be used, and the received data is stored from zbuff.addr.

int

The length of the transmitted data, especially if it is half duplex, send before receive, such as spi flash operation, then the length = send byte receive byte, note that the above send and receive buff should leave enough data, subsequent receive data processing needs to skip the send data length byte

string

The callback after the transfer is complete topic

Return Value

return value type

explanation

boolean

true/false Whether this transmission is started correctly, true, start, false, and cannot be started with errors. Message transfer_done_topic and boolean results are published when the transfer is complete

Examples

local result = spi.xfer(spi.SPI_0, txbuff, rxbuff, 1024, "SPIDONE") if result then result, spi_id, succ, error_code = sys.waitUntil("SPIDONE") end if not result or not succ then log.info("spi fail, error code", error_code) else log.info("spi ok") end