2015-01-16 21:41:34 +01:00
|
|
|
// Module for interfacing with the SPI interface
|
|
|
|
|
2015-12-16 06:04:58 +01:00
|
|
|
#include "module.h"
|
2015-01-16 21:41:34 +01:00
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "platform.h"
|
|
|
|
|
2017-02-06 13:55:26 +01:00
|
|
|
#include "driver/spi.h"
|
|
|
|
|
2015-10-27 23:30:33 +01:00
|
|
|
#define SPI_HALFDUPLEX 0
|
|
|
|
#define SPI_FULLDUPLEX 1
|
|
|
|
|
2015-10-11 16:03:58 +02:00
|
|
|
static u8 spi_databits[NUM_SPI] = {0, 0};
|
2015-10-27 23:30:33 +01:00
|
|
|
static u8 spi_duplex[NUM_SPI] = {SPI_HALFDUPLEX, SPI_HALFDUPLEX};
|
2015-10-11 16:03:58 +02:00
|
|
|
|
2015-10-27 23:30:33 +01:00
|
|
|
// Lua: = spi.setup( id, mode, cpol, cpha, databits, clock_div, [duplex_mode] )
|
2015-01-16 21:41:34 +01:00
|
|
|
static int spi_setup( lua_State *L )
|
|
|
|
{
|
2015-10-25 22:58:06 +01:00
|
|
|
int id = luaL_checkinteger( L, 1 );
|
|
|
|
int mode = luaL_checkinteger( L, 2 );
|
|
|
|
int cpol = luaL_checkinteger( L, 3 );
|
|
|
|
int cpha = luaL_checkinteger( L, 4 );
|
|
|
|
int databits = luaL_checkinteger( L, 5 );
|
|
|
|
u32 clock_div = luaL_checkinteger( L, 6 );
|
2015-10-27 23:30:33 +01:00
|
|
|
int duplex_mode = luaL_optinteger( L, 7, SPI_HALFDUPLEX );
|
2015-01-16 21:41:34 +01:00
|
|
|
|
|
|
|
MOD_CHECK_ID( spi, id );
|
|
|
|
|
|
|
|
if (mode != PLATFORM_SPI_SLAVE && mode != PLATFORM_SPI_MASTER) {
|
|
|
|
return luaL_error( L, "wrong arg type" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cpol != PLATFORM_SPI_CPOL_LOW && cpol != PLATFORM_SPI_CPOL_HIGH) {
|
|
|
|
return luaL_error( L, "wrong arg type" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cpha != PLATFORM_SPI_CPHA_LOW && cpha != PLATFORM_SPI_CPHA_HIGH) {
|
|
|
|
return luaL_error( L, "wrong arg type" );
|
|
|
|
}
|
|
|
|
|
2015-10-11 16:03:58 +02:00
|
|
|
if (databits < 0 || databits > 32) {
|
|
|
|
return luaL_error( L, "out of range" );
|
|
|
|
}
|
|
|
|
|
2016-06-15 21:01:52 +02:00
|
|
|
if (clock_div == 0) {
|
|
|
|
// defaulting to 8 for backward compatibility
|
2015-10-18 11:37:17 +02:00
|
|
|
clock_div = 8;
|
2015-10-04 00:40:21 +02:00
|
|
|
}
|
|
|
|
|
2015-10-27 23:30:33 +01:00
|
|
|
if (duplex_mode == SPI_HALFDUPLEX || duplex_mode == SPI_FULLDUPLEX)
|
|
|
|
{
|
|
|
|
spi_duplex[id] = duplex_mode;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-10-25 22:58:06 +01:00
|
|
|
return luaL_error( L, "out of range" );
|
|
|
|
}
|
|
|
|
|
2015-10-11 16:03:58 +02:00
|
|
|
spi_databits[id] = databits;
|
|
|
|
|
2015-10-27 23:30:33 +01:00
|
|
|
u32 res = platform_spi_setup(id, mode, cpol, cpha, clock_div);
|
2015-01-16 21:41:34 +01:00
|
|
|
lua_pushinteger( L, res );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-10-27 23:30:33 +01:00
|
|
|
// Half-duplex mode:
|
|
|
|
// Lua: wrote = spi.send( id, data1, [data2], ..., [datan] )
|
|
|
|
// Full-duplex mode:
|
|
|
|
// Lua: wrote, [data1], ..., [datan] = spi.send_recv( id, data1, [data2], ..., [datan] )
|
|
|
|
// data can be either a string, a table or an 8-bit number
|
|
|
|
static int spi_send_recv( lua_State *L )
|
2015-01-16 21:41:34 +01:00
|
|
|
{
|
2015-10-11 16:03:58 +02:00
|
|
|
unsigned id = luaL_checkinteger( L, 1 );
|
|
|
|
const char *pdata;
|
|
|
|
size_t datalen, i;
|
2015-10-25 22:58:06 +01:00
|
|
|
u32 numdata;
|
2015-01-16 21:41:34 +01:00
|
|
|
u32 wrote = 0;
|
2015-10-25 22:58:06 +01:00
|
|
|
int pushed = 1;
|
|
|
|
unsigned argn, tos;
|
2015-10-27 23:30:33 +01:00
|
|
|
u8 recv = spi_duplex[id] == SPI_FULLDUPLEX ? 1 : 0;
|
2015-01-16 21:41:34 +01:00
|
|
|
|
|
|
|
MOD_CHECK_ID( spi, id );
|
2015-10-25 22:58:06 +01:00
|
|
|
if( (tos = lua_gettop( L )) < 2 )
|
2015-10-11 16:03:58 +02:00
|
|
|
return luaL_error( L, "wrong arg type" );
|
2015-01-16 21:41:34 +01:00
|
|
|
|
2015-10-25 22:58:06 +01:00
|
|
|
// prepare first returned item 'wrote' - value is yet unknown
|
|
|
|
// position on stack is tos+1
|
|
|
|
lua_pushinteger( L, 0 );
|
|
|
|
|
|
|
|
for( argn = 2; argn <= tos; argn ++ )
|
2015-01-16 21:41:34 +01:00
|
|
|
{
|
2015-10-25 22:58:06 +01:00
|
|
|
// *** Send integer value and return received data as integer ***
|
2015-10-11 16:03:58 +02:00
|
|
|
// lua_isnumber() would silently convert a string of digits to an integer
|
|
|
|
// whereas here strings are handled separately.
|
|
|
|
if( lua_type( L, argn ) == LUA_TNUMBER )
|
|
|
|
{
|
2015-10-25 22:58:06 +01:00
|
|
|
numdata = luaL_checkinteger( L, argn );
|
|
|
|
if (recv > 0)
|
|
|
|
{
|
|
|
|
lua_pushinteger( L, platform_spi_send_recv( id, spi_databits[id], numdata ) );
|
|
|
|
pushed ++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
platform_spi_send( id, spi_databits[id], numdata );
|
|
|
|
}
|
2015-10-11 16:03:58 +02:00
|
|
|
wrote ++;
|
|
|
|
}
|
2015-10-25 22:58:06 +01:00
|
|
|
|
|
|
|
// *** Send table elements and return received data items as a table ***
|
2015-10-11 16:03:58 +02:00
|
|
|
else if( lua_istable( L, argn ) )
|
|
|
|
{
|
|
|
|
datalen = lua_objlen( L, argn );
|
2015-10-25 22:58:06 +01:00
|
|
|
|
|
|
|
if (recv > 0 && datalen > 0) {
|
|
|
|
// create a table for the received data
|
|
|
|
lua_createtable( L, datalen, 0 );
|
|
|
|
pushed ++;
|
|
|
|
}
|
|
|
|
|
2015-10-11 16:03:58 +02:00
|
|
|
for( i = 0; i < datalen; i ++ )
|
|
|
|
{
|
|
|
|
lua_rawgeti( L, argn, i + 1 );
|
2015-10-25 22:58:06 +01:00
|
|
|
numdata = luaL_checkinteger( L, -1 );
|
2015-10-11 16:03:58 +02:00
|
|
|
lua_pop( L, 1 );
|
2015-10-25 22:58:06 +01:00
|
|
|
if (recv > 0) {
|
|
|
|
lua_pushinteger( L, platform_spi_send_recv( id, spi_databits[id], numdata ) );
|
2015-10-26 22:40:11 +01:00
|
|
|
lua_rawseti( L, -2, i + 1 );
|
2015-10-25 22:58:06 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
platform_spi_send( id, spi_databits[id], numdata );
|
|
|
|
}
|
2015-10-11 16:03:58 +02:00
|
|
|
}
|
|
|
|
wrote += i;
|
|
|
|
if( i < datalen )
|
|
|
|
break;
|
|
|
|
}
|
2015-10-25 22:58:06 +01:00
|
|
|
|
|
|
|
// *** Send characters of a string and return received data items as string ***
|
2015-10-11 16:03:58 +02:00
|
|
|
else
|
|
|
|
{
|
2015-10-25 22:58:06 +01:00
|
|
|
luaL_Buffer b;
|
|
|
|
|
2015-10-11 16:03:58 +02:00
|
|
|
pdata = luaL_checklstring( L, argn, &datalen );
|
2015-10-25 22:58:06 +01:00
|
|
|
if (recv > 0) {
|
|
|
|
luaL_buffinit( L, &b );
|
|
|
|
}
|
|
|
|
|
2015-10-11 16:03:58 +02:00
|
|
|
for( i = 0; i < datalen; i ++ )
|
2015-10-25 22:58:06 +01:00
|
|
|
{
|
|
|
|
if (recv > 0)
|
|
|
|
{
|
|
|
|
luaL_addchar( &b, (char)platform_spi_send_recv( id, spi_databits[id], pdata[ i ] ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
platform_spi_send( id, spi_databits[id], pdata[ i ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (recv > 0 && datalen > 0) {
|
|
|
|
luaL_pushresult( &b );
|
|
|
|
pushed ++;
|
|
|
|
}
|
2015-10-11 16:03:58 +02:00
|
|
|
wrote += i;
|
|
|
|
if( i < datalen )
|
|
|
|
break;
|
2015-01-16 21:41:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-25 22:58:06 +01:00
|
|
|
// update item 'wrote' on stack
|
2015-01-16 21:41:34 +01:00
|
|
|
lua_pushinteger( L, wrote );
|
2015-10-25 22:58:06 +01:00
|
|
|
lua_replace( L, tos+1 );
|
|
|
|
return pushed;
|
|
|
|
}
|
|
|
|
|
2015-10-26 22:39:33 +01:00
|
|
|
// Lua: read = spi.recv( id, size, [default data] )
|
2015-01-16 21:41:34 +01:00
|
|
|
static int spi_recv( lua_State *L )
|
|
|
|
{
|
2015-10-11 16:03:58 +02:00
|
|
|
int id = luaL_checkinteger( L, 1 );
|
|
|
|
int size = luaL_checkinteger( L, 2 ), i;
|
2015-10-26 22:39:33 +01:00
|
|
|
int def = luaL_optinteger( L, 3, 0xffffffff );
|
2015-10-11 16:03:58 +02:00
|
|
|
|
|
|
|
luaL_Buffer b;
|
2015-01-16 21:41:34 +01:00
|
|
|
|
|
|
|
MOD_CHECK_ID( spi, id );
|
2015-10-11 16:03:58 +02:00
|
|
|
if (size == 0) {
|
|
|
|
return 0;
|
2015-01-16 21:41:34 +01:00
|
|
|
}
|
|
|
|
|
2015-10-11 16:03:58 +02:00
|
|
|
luaL_buffinit( L, &b );
|
|
|
|
for (i=0; i<size; i++)
|
2015-10-10 22:34:36 +02:00
|
|
|
{
|
2015-10-26 22:39:33 +01:00
|
|
|
luaL_addchar( &b, ( char )platform_spi_send_recv( id, spi_databits[id], def ) );
|
2015-01-16 21:41:34 +01:00
|
|
|
}
|
|
|
|
|
2015-10-11 16:03:58 +02:00
|
|
|
luaL_pushresult( &b );
|
|
|
|
return 1;
|
2015-01-16 21:41:34 +01:00
|
|
|
}
|
|
|
|
|
2015-10-08 23:13:31 +02:00
|
|
|
// Lua: spi.set_mosi( id, offset, bitlen, data1, [data2], ..., [datan] )
|
2017-02-06 13:55:26 +01:00
|
|
|
// Lua: spi.set_mosi( id, string )
|
2015-10-08 23:13:31 +02:00
|
|
|
static int spi_set_mosi( lua_State *L )
|
|
|
|
{
|
2017-02-06 13:55:26 +01:00
|
|
|
int id = luaL_checkinteger( L, 1 );
|
2015-10-08 23:13:31 +02:00
|
|
|
|
|
|
|
MOD_CHECK_ID( spi, id );
|
|
|
|
|
2017-02-06 13:55:26 +01:00
|
|
|
if (lua_type( L, 2 ) == LUA_TSTRING) {
|
|
|
|
size_t len;
|
|
|
|
const char *data = luaL_checklstring( L, 2, &len );
|
|
|
|
luaL_argcheck( L, 2, len <= 64, "out of range" );
|
2015-10-10 22:34:36 +02:00
|
|
|
|
2017-02-06 13:55:26 +01:00
|
|
|
spi_mast_blkset( id, len * 8, data );
|
2015-10-08 23:13:31 +02:00
|
|
|
|
2017-02-06 13:55:26 +01:00
|
|
|
} else {
|
|
|
|
int offset = luaL_checkinteger( L, 2 );
|
|
|
|
int bitlen = luaL_checkinteger( L, 3 );
|
2015-10-08 23:13:31 +02:00
|
|
|
|
2017-02-06 13:55:26 +01:00
|
|
|
luaL_argcheck( L, 2, offset >= 0 && offset <= 511, "out of range" );
|
|
|
|
luaL_argcheck( L, 3, bitlen >= 1 && bitlen <= 32, "out of range" );
|
2015-10-08 23:13:31 +02:00
|
|
|
|
2017-02-06 13:55:26 +01:00
|
|
|
for (int argn = 4; argn <= lua_gettop( L ); argn++, offset += bitlen ) {
|
|
|
|
u32 data = ( u32 )luaL_checkinteger(L, argn );
|
|
|
|
|
|
|
|
if (offset + bitlen > 512) {
|
|
|
|
return luaL_error( L, "data range exceeded > 512 bits" );
|
|
|
|
}
|
2015-10-08 23:13:31 +02:00
|
|
|
|
2017-02-06 13:55:26 +01:00
|
|
|
spi_mast_set_mosi( id, offset, bitlen, data );
|
2015-10-10 22:34:36 +02:00
|
|
|
}
|
2015-10-08 23:13:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-10 22:34:36 +02:00
|
|
|
// Lua: data = spi.get_miso( id, offset, bitlen, num )
|
2017-02-06 13:55:26 +01:00
|
|
|
// Lua: string = spi.get_miso( id, len )
|
2015-10-10 22:34:36 +02:00
|
|
|
static int spi_get_miso( lua_State *L )
|
|
|
|
{
|
2017-02-06 13:55:26 +01:00
|
|
|
int id = luaL_checkinteger( L, 1 );
|
2015-10-10 22:34:36 +02:00
|
|
|
|
|
|
|
MOD_CHECK_ID( spi, id );
|
|
|
|
|
2017-02-06 13:55:26 +01:00
|
|
|
if (lua_gettop( L ) == 2) {
|
|
|
|
uint8_t data[64];
|
|
|
|
int len = luaL_checkinteger( L, 2 );
|
2015-10-10 22:34:36 +02:00
|
|
|
|
2017-02-06 13:55:26 +01:00
|
|
|
luaL_argcheck( L, 2, len >= 1 && len <= 64, "out of range" );
|
2015-10-10 22:34:36 +02:00
|
|
|
|
2017-02-06 13:55:26 +01:00
|
|
|
spi_mast_blkget( id, len * 8, data );
|
2015-10-10 22:34:36 +02:00
|
|
|
|
2017-02-06 13:55:26 +01:00
|
|
|
lua_pushlstring( L, data, len );
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
int offset = luaL_checkinteger( L, 2 );
|
|
|
|
int bitlen = luaL_checkinteger( L, 3 );
|
|
|
|
int num = luaL_checkinteger( L, 4 ), i;
|
|
|
|
|
|
|
|
luaL_argcheck( L, 2, offset >= 0 && offset <= 511, "out of range" );
|
|
|
|
luaL_argcheck( L, 3, bitlen >= 1 && bitlen <= 32, "out of range" );
|
|
|
|
|
|
|
|
if (offset + bitlen * num > 512) {
|
|
|
|
return luaL_error( L, "out of range" );
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
lua_pushinteger( L, spi_mast_get_miso( id, offset + (bitlen * i), bitlen ) );
|
|
|
|
}
|
|
|
|
return num;
|
2015-10-10 22:34:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 23:13:31 +02:00
|
|
|
// Lua: spi.transaction( id, cmd_bitlen, cmd_data, addr_bitlen, addr_data, mosi_bitlen, dummy_bitlen, miso_bitlen )
|
|
|
|
static int spi_transaction( lua_State *L )
|
|
|
|
{
|
2017-02-06 13:55:26 +01:00
|
|
|
int id = luaL_checkinteger( L, 1 );
|
2015-10-08 23:13:31 +02:00
|
|
|
|
|
|
|
MOD_CHECK_ID( spi, id );
|
|
|
|
|
2017-02-06 13:55:26 +01:00
|
|
|
int cmd_bitlen = luaL_checkinteger( L, 2 );
|
|
|
|
u16 cmd_data = ( u16 )luaL_checkinteger( L, 3 );
|
|
|
|
luaL_argcheck( L, 2, cmd_bitlen >= 0 && cmd_bitlen <= 16, "out of range" );
|
2015-10-10 22:34:36 +02:00
|
|
|
|
2017-02-06 13:55:26 +01:00
|
|
|
int addr_bitlen = luaL_checkinteger( L, 4 );
|
|
|
|
u32 addr_data = ( u32 )luaL_checkinteger( L, 5 );
|
|
|
|
luaL_argcheck( L, 4, addr_bitlen >= 0 && addr_bitlen <= 32, "out of range" );
|
2015-10-10 22:34:36 +02:00
|
|
|
|
2017-02-06 13:55:26 +01:00
|
|
|
int mosi_bitlen = luaL_checkinteger( L, 6 );
|
|
|
|
luaL_argcheck( L, 6, mosi_bitlen >= 0 && mosi_bitlen <= 512, "out of range" );
|
2015-10-10 22:34:36 +02:00
|
|
|
|
2017-02-06 13:55:26 +01:00
|
|
|
int dummy_bitlen = luaL_checkinteger( L, 7 );
|
|
|
|
luaL_argcheck( L, 7, dummy_bitlen >= 0 && dummy_bitlen <= 256, "out of range" );
|
2015-10-10 22:34:36 +02:00
|
|
|
|
2017-02-06 13:55:26 +01:00
|
|
|
int miso_bitlen = luaL_checkinteger( L, 8 );
|
|
|
|
luaL_argcheck( L, 8, miso_bitlen >= -512 && miso_bitlen <= 512, "out of range" );
|
2015-10-08 23:13:31 +02:00
|
|
|
|
2017-02-06 13:55:26 +01:00
|
|
|
spi_mast_transaction( id, cmd_bitlen, cmd_data, addr_bitlen, addr_data,
|
|
|
|
mosi_bitlen, dummy_bitlen, miso_bitlen );
|
2015-10-08 23:13:31 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-16 12:07:20 +01:00
|
|
|
// Lua: old_div = spi.set_clock_div( id, new_div )
|
|
|
|
static int spi_set_clock_div( lua_State *L )
|
|
|
|
{
|
|
|
|
int id = luaL_checkinteger( L, 1 );
|
|
|
|
|
|
|
|
MOD_CHECK_ID( spi, id );
|
|
|
|
|
|
|
|
u32 clk_div = luaL_checkinteger( L, 2 );
|
|
|
|
|
|
|
|
u32 old_div = spi_set_clkdiv(id, clk_div);
|
|
|
|
|
|
|
|
lua_pushinteger( L, old_div );
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-10-08 23:13:31 +02:00
|
|
|
|
2015-01-16 21:41:34 +01:00
|
|
|
// Module function map
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_BEGIN(spi, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_FUNCENTRY( setup, spi_setup )
|
|
|
|
LROT_FUNCENTRY( send, spi_send_recv )
|
|
|
|
LROT_FUNCENTRY( recv, spi_recv )
|
|
|
|
LROT_FUNCENTRY( set_mosi, spi_set_mosi )
|
|
|
|
LROT_FUNCENTRY( get_miso, spi_get_miso )
|
|
|
|
LROT_FUNCENTRY( transaction, spi_transaction )
|
|
|
|
LROT_FUNCENTRY( set_clock_div, spi_set_clock_div )
|
|
|
|
LROT_NUMENTRY( MASTER, PLATFORM_SPI_MASTER )
|
|
|
|
LROT_NUMENTRY( SLAVE, PLATFORM_SPI_SLAVE )
|
|
|
|
LROT_NUMENTRY( CPHA_LOW, PLATFORM_SPI_CPHA_LOW )
|
|
|
|
LROT_NUMENTRY( CPHA_HIGH, PLATFORM_SPI_CPHA_HIGH )
|
|
|
|
LROT_NUMENTRY( CPOL_LOW, PLATFORM_SPI_CPOL_LOW )
|
|
|
|
LROT_NUMENTRY( CPOL_HIGH, PLATFORM_SPI_CPOL_HIGH )
|
|
|
|
LROT_NUMENTRY( DATABITS_8, 8 )
|
|
|
|
LROT_NUMENTRY( HALFDUPLEX, SPI_HALFDUPLEX )
|
|
|
|
LROT_NUMENTRY( FULLDUPLEX, SPI_FULLDUPLEX )
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_END(spi, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
NODEMCU_MODULE(SPI, "spi", spi, NULL);
|