Commit 26f4bc79 authored by devsaurus's avatar devsaurus
Browse files

ensure backwards compatibility of exising api functions

parent 19092712
...@@ -7,14 +7,17 @@ ...@@ -7,14 +7,17 @@
#include "auxmods.h" #include "auxmods.h"
#include "lrotable.h" #include "lrotable.h"
// Lua: = spi.setup( id, mode, cpol, cpha, clock_div ) static u8 spi_databits[NUM_SPI] = {0, 0};
// Lua: = spi.setup( id, mode, cpol, cpha, databits, clock_div )
static int spi_setup( lua_State *L ) static int spi_setup( lua_State *L )
{ {
int id = luaL_checkinteger( L, 1 ); int id = luaL_checkinteger( L, 1 );
int mode = luaL_checkinteger( L, 2 ); int mode = luaL_checkinteger( L, 2 );
int cpol = luaL_checkinteger( L, 3 ); int cpol = luaL_checkinteger( L, 3 );
int cpha = luaL_checkinteger( L, 4 ); int cpha = luaL_checkinteger( L, 4 );
u32 clock_div = luaL_checkinteger( L, 5 ); int databits = luaL_checkinteger( L, 5 );
u32 clock_div = luaL_checkinteger( L, 6 );
MOD_CHECK_ID( spi, id ); MOD_CHECK_ID( spi, id );
...@@ -30,66 +33,103 @@ static int spi_setup( lua_State *L ) ...@@ -30,66 +33,103 @@ static int spi_setup( lua_State *L )
return luaL_error( L, "wrong arg type" ); return luaL_error( L, "wrong arg type" );
} }
if (databits < 0 || databits > 32) {
return luaL_error( L, "out of range" );
}
if (clock_div < 4) { if (clock_div < 4) {
return luaL_error( L, "invalid clock divider" ); return luaL_error( L, "invalid clock divider" );
} }
spi_databits[id] = databits;
u32 res = platform_spi_setup(id, mode, cpol, cpha, clock_div); u32 res = platform_spi_setup(id, mode, cpol, cpha, clock_div);
lua_pushinteger( L, res ); lua_pushinteger( L, res );
return 1; return 1;
} }
// Lua: wrote = spi.send( id, bitlen, data1, [data2], ..., [datan] ) // Lua: wrote = spi.send( id, data1, [data2], ..., [datan] )
// data can be either a string, a table or an 8-bit number // data can be either a string, a table or an 8-bit number
static int spi_send( lua_State *L ) static int spi_send( lua_State *L )
{ {
int id = luaL_checkinteger( L, 1 ); unsigned id = luaL_checkinteger( L, 1 );
int bitlen = luaL_checkinteger( L, 2 ); const char *pdata;
size_t i; size_t datalen, i;
u32 numdata; int numdata;
u32 wrote = 0; u32 wrote = 0;
int argn; unsigned argn;
MOD_CHECK_ID( spi, id ); MOD_CHECK_ID( spi, id );
if( lua_gettop( L ) < 3 ) { if( lua_gettop( L ) < 2 )
return luaL_error( L, "too few args" ); return luaL_error( L, "wrong arg type" );
}
for( argn = 3; argn <= lua_gettop( L ); argn ++ ) for( argn = 2; argn <= lua_gettop( L ); argn ++ )
{ {
numdata = ( u32 )luaL_checkinteger( L, argn ); // lua_isnumber() would silently convert a string of digits to an integer
if (PLATFORM_OK != platform_spi_send( id, bitlen, numdata )) { // whereas here strings are handled separately.
return luaL_error( L, "failed" ); if( lua_type( L, argn ) == LUA_TNUMBER )
{
numdata = ( int )luaL_checkinteger( L, argn );
if( numdata < 0 )
return luaL_error( L, "wrong arg range" );
platform_spi_send( id, spi_databits[id], numdata );
wrote ++;
}
else if( lua_istable( L, argn ) )
{
datalen = lua_objlen( L, argn );
for( i = 0; i < datalen; i ++ )
{
lua_rawgeti( L, argn, i + 1 );
numdata = ( int )luaL_checkinteger( L, -1 );
lua_pop( L, 1 );
if( numdata < 0 )
return luaL_error( L, "wrong arg range" );
platform_spi_send( id, spi_databits[id], numdata );
}
wrote += i;
if( i < datalen )
break;
}
else
{
pdata = luaL_checklstring( L, argn, &datalen );
for( i = 0; i < datalen; i ++ )
platform_spi_send( id, spi_databits[id], pdata[ i ] );
wrote += i;
if( i < datalen )
break;
} }
wrote ++;
} }
lua_pushinteger( L, wrote ); lua_pushinteger( L, wrote );
return 1; return 1;
} }
// Lua: read = spi.recv( id, bitlen, num ) // Lua: read = spi.recv( id, size )
static int spi_recv( lua_State *L ) static int spi_recv( lua_State *L )
{ {
int id = luaL_checkinteger( L, 1 ); int id = luaL_checkinteger( L, 1 );
int bitlen = luaL_checkinteger( L, 2 ); int size = luaL_checkinteger( L, 2 ), i;
int num = luaL_checkinteger( L, 3 ), i;
luaL_Buffer b;
MOD_CHECK_ID( spi, id ); MOD_CHECK_ID( spi, id );
if (bitlen < 1 || bitlen > 32) { if (size == 0) {
return luaL_error( L, "bitlen out of range" ); return 0;
} }
if (num < 0) num = 0;
for (i=0; i<num; i++) luaL_buffinit( L, &b );
for (i=0; i<size; i++)
{ {
if (PLATFORM_OK != platform_spi_transaction( id, 0, 0, 0, 0, 0, 0, bitlen )) { if (PLATFORM_OK != platform_spi_transaction( id, 0, 0, 0, 0, 0, 0, spi_databits[id] )) {
return luaL_error( L, "failed" ); return luaL_error( L, "failed" );
} }
lua_pushinteger( L, platform_spi_get_miso( id, 0, bitlen ) ); luaL_addchar( &b, ( char )platform_spi_get_miso( id, 0, spi_databits[id] ) );
} }
return num; luaL_pushresult( &b );
return 1;
} }
// Lua: spi.set_mosi( id, offset, bitlen, data1, [data2], ..., [datan] ) // Lua: spi.set_mosi( id, offset, bitlen, data1, [data2], ..., [datan] )
......
...@@ -30,7 +30,7 @@ function init_spi_display() ...@@ -30,7 +30,7 @@ function init_spi_display()
local dc = 4 -- GPIO2 local dc = 4 -- GPIO2
local res = 0 -- GPIO16 local res = 0 -- GPIO16
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8) spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
disp = u8g.ssd1306_128x64_hw_spi(cs, dc, res) disp = u8g.ssd1306_128x64_hw_spi(cs, dc, res)
end end
......
...@@ -29,7 +29,7 @@ function init_spi_display() ...@@ -29,7 +29,7 @@ function init_spi_display()
local dc = 4 -- GPIO2 local dc = 4 -- GPIO2
local res = 0 -- GPIO16 local res = 0 -- GPIO16
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8) spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
disp = u8g.ssd1306_128x64_hw_spi(cs, dc, res) disp = u8g.ssd1306_128x64_hw_spi(cs, dc, res)
end end
......
...@@ -30,7 +30,7 @@ function init_spi_display() ...@@ -30,7 +30,7 @@ function init_spi_display()
local dc = 4 -- GPIO2 local dc = 4 -- GPIO2
local res = 0 -- GPIO16 local res = 0 -- GPIO16
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8) spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
disp = u8g.ssd1306_128x64_hw_spi(cs, dc, res) disp = u8g.ssd1306_128x64_hw_spi(cs, dc, res)
end end
......
...@@ -8,7 +8,7 @@ function init_spi_display() ...@@ -8,7 +8,7 @@ function init_spi_display()
local dc = 4 -- GPIO2 local dc = 4 -- GPIO2
local res = 0 -- GPIO16 local res = 0 -- GPIO16
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8) spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
-- initialize the matching driver for your display -- initialize the matching driver for your display
-- see app/include/ucg_config.h -- see app/include/ucg_config.h
......
...@@ -8,7 +8,7 @@ function init_spi_display() ...@@ -8,7 +8,7 @@ function init_spi_display()
local dc = 4 -- GPIO2 local dc = 4 -- GPIO2
local res = 0 -- GPIO16 local res = 0 -- GPIO16
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8) spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
disp = ucg.ili9341_18x240x320_hw_spi(cs, dc, res) disp = ucg.ili9341_18x240x320_hw_spi(cs, dc, res)
end end
......
...@@ -8,8 +8,8 @@ function init_spi_display() ...@@ -8,8 +8,8 @@ function init_spi_display()
local dc = 4 -- GPIO2 local dc = 4 -- GPIO2
local res = 0 -- GPIO16 local res = 0 -- GPIO16
spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8) spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
disp = ucg.ili9341_18x240x320_hw_spi(cs, dc, res) disp = ucg.st7735_18x128x160_hw_spi(cs, dc, res)
end end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment