rework of (H)SPI API

This commit is contained in:
devsaurus 2015-10-10 22:34:36 +02:00
parent 9cde0bbb83
commit f238673772
5 changed files with 158 additions and 93 deletions

View File

@ -64,7 +64,7 @@ void spi_lcd_9bit_write(uint8 spi_no,uint8 high_bit,uint8 low_8bit)
* Description : SPI master initial function for common byte units transmission
* Parameters : uint8 spi_no - SPI module number, Only "SPI" and "HSPI" are valid
*******************************************************************************/
void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, unsigned databits, uint32_t clock_div)
void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, uint32_t clock_div)
{
uint32 regvalue;
@ -107,7 +107,7 @@ void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, unsigned databi
CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_FLASH_MODE|SPI_USR_MISO|
SPI_USR_ADDR|SPI_USR_COMMAND|SPI_USR_DUMMY);
//clear Daul or Quad lines transmission mode
//clear Dual or Quad lines transmission mode
CLEAR_PERI_REG_MASK(SPI_CTRL(spi_no), SPI_QIO_MODE|SPI_DIO_MODE|SPI_DOUT_MODE|SPI_QOUT_MODE);
// SPI clock = CPU clock / clock_div
@ -208,6 +208,57 @@ void spi_mast_set_mosi(uint8 spi_no, uint8 offset, uint8 bitlen, uint32 data)
} while (bitlen > 0);
}
/******************************************************************************
* FunctionName : spi_mast_get_miso
* Description : Retrieve data from MISO buffer.
* The data is regarded as a sequence of bits with length 'bitlen'.
* It will be read starting left-aligned from position 'offset'.
* Parameters : uint8 spi_no - SPI module number, Only "SPI" and "HSPI" are valid
* uint8 offset - offset into MISO buffer (number of bits)
* uint8 bitlen - requested number of bits in data
*******************************************************************************/
uint32 spi_mast_get_miso(uint8 spi_no, uint8 offset, uint8 bitlen)
{
uint8 wn, wn_offset, wn_bitlen;
uint32 wn_data = 0;
if (spi_no > 1)
return 0; // handle invalid input number
// determine which SPI_Wn register is addressed
wn = offset >> 5;
if (wn > 15)
return 0; // out of range
wn_offset = offset & 0x1f;
if (bitlen > (32 - wn_offset))
{
// splitting required
wn_bitlen = 32 - wn_offset;
}
else
{
wn_bitlen = bitlen;
}
do
{
wn_data |= (READ_PERI_REG(REG_SPI_BASE(spi_no) +0x40 + wn*4) >> (32 - (wn_offset + wn_bitlen))) & (BIT(wn_bitlen) - 1);
// prepare reading of dangling data part
wn_data <<= bitlen - wn_bitlen;
wn += 1;
wn_offset = 0;
if (wn <= 15)
bitlen -= wn_bitlen;
else
bitlen = 0; // force abort
wn_bitlen = bitlen;
} while (bitlen > 0);
return wn_data;
}
/******************************************************************************
* FunctionName : spi_mast_transaction
* Description : Start a transaction and wait for completion.

View File

@ -18,11 +18,12 @@ void spi_lcd_mode_init(uint8 spi_no);
void spi_lcd_9bit_write(uint8 spi_no,uint8 high_bit,uint8 low_8bit);
//spi master init funtion
void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, unsigned databits, uint32_t clock_div);
void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, uint32_t clock_div);
//use spi send 8bit data
void spi_mast_byte_write(uint8 spi_no,uint8 *data);
void spi_set_mosi(uint8 spi_no, uint8 offset, uint8 bitlen, uint32 data);
uint32 spi_get_miso(uint8 spi_no, uint8 offset, uint8 bitlen);
void spi_mast_transaction(uint8 spi_no, uint8 cmd_bitlen, uint16 cmd_data, uint8 addr_bitlen, uint32 addr_data,
uint8 mosi_bitlen, uint8 dummy_bitlen, uint8 miso_bitlen);

View File

@ -7,15 +7,14 @@
#include "auxmods.h"
#include "lrotable.h"
// Lua: = spi.setup( id, mode, cpol, cpha, databits, clock_div )
// Lua: = spi.setup( id, mode, cpol, cpha, clock_div )
static int spi_setup( lua_State *L )
{
unsigned id = luaL_checkinteger( L, 1 );
unsigned mode = luaL_checkinteger( L, 2 );
unsigned cpol = luaL_checkinteger( L, 3 );
unsigned cpha = luaL_checkinteger( L, 4 );
unsigned databits = luaL_checkinteger( L, 5 );
uint32_t clock_div = luaL_checkinteger( L, 6 );
int id = luaL_checkinteger( L, 1 );
int mode = luaL_checkinteger( L, 2 );
int cpol = luaL_checkinteger( L, 3 );
int cpha = luaL_checkinteger( L, 4 );
u32 clock_div = luaL_checkinteger( L, 5 );
MOD_CHECK_ID( spi, id );
@ -31,114 +30,82 @@ static int spi_setup( lua_State *L )
return luaL_error( L, "wrong arg type" );
}
if (databits != PLATFORM_SPI_DATABITS_8 && databits != PLATFORM_SPI_DATABITS_16) {
return luaL_error( L, "wrong arg type" );
}
if (clock_div < 4) {
return luaL_error( L, "invalid clock divider" );
}
u32 res = platform_spi_setup(id, mode, cpol, cpha, databits, clock_div);
u32 res = platform_spi_setup(id, mode, cpol, cpha, clock_div);
lua_pushinteger( L, res );
return 1;
}
// Lua: wrote = spi.send( id, data1, [data2], ..., [datan] )
// Lua: wrote = spi.send( id, bitlen, data1, [data2], ..., [datan] )
// data can be either a string, a table or an 8-bit number
static int spi_send( lua_State *L )
{
unsigned id = luaL_checkinteger( L, 1 );
const char *pdata;
size_t datalen, i;
int numdata;
int id = luaL_checkinteger( L, 1 );
int bitlen = luaL_checkinteger( L, 2 );
size_t i;
u32 numdata;
u32 wrote = 0;
unsigned argn;
int argn;
MOD_CHECK_ID( spi, id );
if( lua_gettop( L ) < 2 )
if( lua_gettop( L ) < 3 ) {
return luaL_error( L, "too few args" );
}
for( argn = 2; argn <= lua_gettop( L ); argn ++ )
for( argn = 3; argn <= lua_gettop( L ); argn ++ )
{
// 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 )
{
numdata = ( int )luaL_checkinteger( L, argn );
if( numdata < 0 || numdata > 255 )
return luaL_error( L, "wrong arg range" );
platform_spi_transaction( id, 8, numdata, 0, 0, 0, 0, 0 );
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 || numdata > 255 )
return luaL_error( L, "wrong arg range" );
platform_spi_transaction( id, 8, numdata, 0, 0, 0, 0, 0 );
}
wrote += i;
if( i < datalen )
break;
}
else
{
pdata = luaL_checklstring( L, argn, &datalen );
for( i = 0; i < datalen; i ++ )
platform_spi_transaction( id, 8, pdata[ i ], 0, 0, 0, 0, 0 );
wrote += i;
if( i < datalen )
break;
numdata = ( u32 )luaL_checkinteger( L, argn );
if (PLATFORM_OK != platform_spi_transaction( id, 0, 0, bitlen, numdata, 0, 0, 0 )) {
return luaL_error( L, "failed" );
}
wrote ++;
}
lua_pushinteger( L, wrote );
return 1;
}
// Lua: read = spi.recv( id, size )
// Lua: read = spi.recv( id, bitlen, num )
static int spi_recv( lua_State *L )
{
unsigned id = luaL_checkinteger( L, 1 );
u32 size = ( u32 )luaL_checkinteger( L, 2 ), i;
luaL_Buffer b;
spi_data_type data;
int id = luaL_checkinteger( L, 1 );
int bitlen = luaL_checkinteger( L, 2 );
int num = luaL_checkinteger( L, 3 ), i;
MOD_CHECK_ID( spi, id );
if (size == 0) {
return 0;
if (bitlen < 1 || bitlen > 32) {
return luaL_error( L, "bitlen out of range" );
}
if (num < 0) num = 0;
for (i=0; i<num; i++)
{
if (PLATFORM_OK != platform_spi_transaction( id, 0, 0, 0, 0, 0, 0, bitlen )) {
return luaL_error( L, "failed" );
}
lua_pushinteger( L, platform_spi_get_miso( id, 0, bitlen ) );
}
luaL_buffinit( L, &b );
for (i=0; i<size; i++) {
data = platform_spi_send_recv(id, 0xFF);
luaL_addchar( &b, ( char )data);
}
luaL_pushresult( &b );
return 1;
return num;
}
// Lua: spi.set_mosi( id, offset, bitlen, data1, [data2], ..., [datan] )
static int spi_set_mosi( lua_State *L )
{
unsigned id = luaL_checkinteger( L, 1 );
unsigned offset = luaL_checkinteger( L, 2 );
unsigned bitlen = luaL_checkinteger( L, 3 );
unsigned argn;
int id = luaL_checkinteger( L, 1 );
int offset = luaL_checkinteger( L, 2 );
int bitlen = luaL_checkinteger( L, 3 );
int argn;
MOD_CHECK_ID( spi, id );
if (offset < 0 || offset > 511) {
return luaL_error( L, "offset out of range" );
}
if (bitlen < 1 || bitlen > 32) {
return luaL_error( L, "bitlen out of range" );
}
@ -149,51 +116,87 @@ static int spi_set_mosi( lua_State *L )
for (argn = 4; argn <= lua_gettop( L ); argn++, offset += bitlen )
{
u32 data = luaL_checkinteger(L, argn );
u32 data = ( u32 )luaL_checkinteger(L, argn );
if (offset + bitlen > 512)
if (offset + bitlen > 512) {
return luaL_error( L, "data range exceeded > 512 bits" );
}
if (platform_spi_set_mosi( id, offset, bitlen, data ) != PLATFORM_OK)
if (PLATFORM_OK != platform_spi_set_mosi( id, offset, bitlen, data )) {
return luaL_error( L, "failed" );
}
}
return 0;
}
// Lua: data = spi.get_miso( id, offset, bitlen, num )
static int spi_get_miso( lua_State *L )
{
int id = luaL_checkinteger( L, 1 );
int offset = luaL_checkinteger( L, 2 );
int bitlen = luaL_checkinteger( L, 3 );
int num = luaL_checkinteger( L, 4 ), i;
MOD_CHECK_ID( spi, id );
if (offset < 0 || offset > 511) {
return luaL_error( L, "out of range" );
}
if (bitlen < 1 || bitlen > 32) {
return luaL_error( L, "bitlen out of range" );
}
if (offset + bitlen * num > 512) {
return luaL_error( L, "out of range" );
}
for (i = 0; i < num; i++)
{
lua_pushinteger( L, platform_spi_get_miso( id, offset + (bitlen * i), bitlen ) );
}
return num;
}
// 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 )
{
unsigned id = luaL_checkinteger( L, 1 );
unsigned cmd_bitlen = luaL_checkinteger( L, 2 );
u32 cmd_data = luaL_checkinteger( L, 3 );
unsigned addr_bitlen = luaL_checkinteger( L, 4 );
u32 addr_data = luaL_checkinteger( L, 5 );
unsigned mosi_bitlen = luaL_checkinteger( L, 6 );
unsigned dummy_bitlen = luaL_checkinteger( L, 7 );
unsigned miso_bitlen = luaL_checkinteger( L, 8 );
int id = luaL_checkinteger( L, 1 );
int cmd_bitlen = luaL_checkinteger( L, 2 );
u16 cmd_data = ( u16 )luaL_checkinteger( L, 3 );
int addr_bitlen = luaL_checkinteger( L, 4 );
u32 addr_data = ( u32 )luaL_checkinteger( L, 5 );
int mosi_bitlen = luaL_checkinteger( L, 6 );
int dummy_bitlen = luaL_checkinteger( L, 7 );
int miso_bitlen = luaL_checkinteger( L, 8 );
MOD_CHECK_ID( spi, id );
if (cmd_bitlen < 0 || cmd_bitlen > 16) {
return luaL_error( L, "cmd_bitlen out of range" );
}
if (addr_bitlen < 0 || addr_bitlen > 32) {
return luaL_error( L, "addr_bitlen out of range" );
}
if (mosi_bitlen < 0 || mosi_bitlen > 512) {
return luaL_error( L, "mosi_bitlen out of range" );
}
if (dummy_bitlen < 0 || dummy_bitlen > 256) {
return luaL_error( L, "dummy_bitlen out of range" );
}
if (miso_bitlen < 0 || miso_bitlen > 511) {
return luaL_error( L, "miso_bitlen out of range" );
}
if (platform_spi_transaction( id, cmd_bitlen, cmd_data, addr_bitlen, addr_data,
mosi_bitlen, dummy_bitlen, miso_bitlen) != PLATFORM_OK)
if (PLATFORM_OK != platform_spi_transaction( id, cmd_bitlen, cmd_data, addr_bitlen, addr_data,
mosi_bitlen, dummy_bitlen, miso_bitlen) ) {
return luaL_error( L, "failed" );
}
return 0;
}
@ -208,6 +211,7 @@ const LUA_REG_TYPE spi_map[] =
{ LSTRKEY( "send" ), LFUNCVAL( spi_send ) },
{ LSTRKEY( "recv" ), LFUNCVAL( spi_recv ) },
{ LSTRKEY( "set_mosi" ), LFUNCVAL( spi_set_mosi ) },
{ LSTRKEY( "get_miso" ), LFUNCVAL( spi_get_miso ) },
{ LSTRKEY( "transaction" ), LFUNCVAL( spi_transaction ) },
#if LUA_OPTIMIZE_MEMORY > 0
{ LSTRKEY( "MASTER" ), LNUMVAL( PLATFORM_SPI_MASTER ) },

View File

@ -437,9 +437,9 @@ int platform_i2c_recv_byte( unsigned id, int ack ){
// *****************************************************************************
// SPI platform interface
uint32_t platform_spi_setup( unsigned id, int mode, unsigned cpol, unsigned cpha, unsigned databits, uint32_t clock_div)
uint32_t platform_spi_setup( unsigned id, int mode, unsigned cpol, unsigned cpha, uint32_t clock_div)
{
spi_master_init(id, cpol, cpha, databits, clock_div);
spi_master_init(id, cpol, cpha, clock_div);
return 1;
}
@ -459,6 +459,14 @@ int platform_spi_set_mosi( uint8_t id, uint8_t offset, uint8_t bitlen, spi_data_
return PLATFORM_OK;
}
spi_data_type platform_spi_get_miso( uint8_t id, uint8_t offset, uint8_t bitlen )
{
if (offset + bitlen > 512)
return 0;
return spi_mast_get_miso( id, offset, bitlen );
}
int platform_spi_transaction( uint8_t id, uint8_t cmd_bitlen, spi_data_type cmd_data,
uint8_t addr_bitlen, spi_data_type addr_data,
uint8_t mosi_bitlen, uint8_t dummy_bitlen, uint8_t miso_bitlen )

View File

@ -99,11 +99,12 @@ typedef uint32_t spi_data_type;
// The platform SPI functions
int platform_spi_exists( unsigned id );
uint32_t platform_spi_setup( unsigned id, int mode, unsigned cpol, unsigned cpha, unsigned databits, uint32_t clock_div);
uint32_t platform_spi_setup( unsigned id, int mode, unsigned cpol, unsigned cpha, uint32_t clock_div);
spi_data_type platform_spi_send_recv( unsigned id, spi_data_type data );
void platform_spi_select( unsigned id, int is_select );
int platform_spi_set_mosi( uint8_t id, uint8_t offset, uint8_t bitlen, spi_data_type data );
spi_data_type platform_spi_get_miso( uint8_t id, uint8_t offset, uint8_t bitlen );
int platform_spi_transaction( uint8_t id, uint8_t cmd_bitlen, spi_data_type cmd_data,
uint8_t addr_bitlen, spi_data_type addr_data,
uint8_t mosi_bitlen, uint8_t dummy_bitlen, uint8_t miso_bitlen );