simplify spi api

duplex_mode configuration enables/disables receiving with spi.send()
spi.send_recv removed
This commit is contained in:
devsaurus 2015-10-27 23:30:33 +01:00
parent 0ba0370b87
commit 9444ae28df
5 changed files with 48 additions and 53 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 * Description : SPI master initial function for common byte units transmission
* Parameters : uint8 spi_no - SPI module number, Only "SPI" and "HSPI" are valid * Parameters : uint8 spi_no - SPI module number, Only "SPI" and "HSPI" are valid
*******************************************************************************/ *******************************************************************************/
void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, uint32_t clock_div, uint8 full_duplex) void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, uint32_t clock_div)
{ {
uint32 regvalue; uint32 regvalue;
@ -85,7 +85,7 @@ void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, uint32_t clock_
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, 2);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, 2);//configure io to spi mode
} }
SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_CS_SETUP|SPI_CS_HOLD|SPI_RD_BYTE_ORDER|SPI_WR_BYTE_ORDER); SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_CS_SETUP|SPI_CS_HOLD|SPI_RD_BYTE_ORDER|SPI_WR_BYTE_ORDER|SPI_DOUTDIN);
//set clock polarity //set clock polarity
// TODO: This doesn't work // TODO: This doesn't work
@ -103,8 +103,7 @@ void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, uint32_t clock_
CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_CK_OUT_EDGE|SPI_CK_I_EDGE); CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_CK_OUT_EDGE|SPI_CK_I_EDGE);
} }
CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_FLASH_MODE|SPI_USR_MISO| CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_FLASH_MODE|SPI_USR_MISO|SPI_USR_ADDR|SPI_USR_COMMAND|SPI_USR_DUMMY);
SPI_USR_ADDR|SPI_USR_COMMAND|SPI_USR_DUMMY|SPI_DOUTDIN);
//clear Dual 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); CLEAR_PERI_REG_MASK(SPI_CTRL(spi_no), SPI_QIO_MODE|SPI_DIO_MODE|SPI_DOUT_MODE|SPI_QOUT_MODE);
@ -124,11 +123,6 @@ void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, uint32_t clock_
((1&SPI_CLKCNT_N)<<SPI_CLKCNT_N_S)| ((1&SPI_CLKCNT_N)<<SPI_CLKCNT_N_S)|
((0&SPI_CLKCNT_H)<<SPI_CLKCNT_H_S)| ((0&SPI_CLKCNT_H)<<SPI_CLKCNT_H_S)|
((1&SPI_CLKCNT_L)<<SPI_CLKCNT_L_S)); //clear bit 31,set SPI clock div ((1&SPI_CLKCNT_L)<<SPI_CLKCNT_L_S)); //clear bit 31,set SPI clock div
if (full_duplex > 0)
{
SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_DOUTDIN);
}
} }
/****************************************************************************** /******************************************************************************
@ -250,20 +244,21 @@ uint32 spi_mast_get_miso(uint8 spi_no, uint8 offset, uint8 bitlen)
* uint16 cmd_data - Command data. * uint16 cmd_data - Command data.
* uint8 addr_bitlen - Valid number of bits in addr_data. * uint8 addr_bitlen - Valid number of bits in addr_data.
* uint32 addr_data - Address data. * uint32 addr_data - Address data.
* uint8 mosi_bitlen - Valid number of bits in MOSI buffer. * uint16 mosi_bitlen - Valid number of bits in MOSI buffer.
* uint8 dummy_bitlen - Number of dummy cycles. * uint8 dummy_bitlen - Number of dummy cycles.
* uint8 miso_bitlen - number of bits to be captured in MISO buffer. * sint16 miso_bitlen - number of bits to be captured in MISO buffer.
* negative value activates full-duplex mode.
*******************************************************************************/ *******************************************************************************/
void spi_mast_transaction(uint8 spi_no, uint8 cmd_bitlen, uint16 cmd_data, uint8 addr_bitlen, uint32 addr_data, 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) uint16 mosi_bitlen, uint8 dummy_bitlen, sint16 miso_bitlen)
{ {
if (spi_no > 1) if (spi_no > 1)
return; // handle invalid input number return; // handle invalid input number
while(READ_PERI_REG(SPI_CMD(spi_no)) & SPI_USR); while(READ_PERI_REG(SPI_CMD(spi_no)) & SPI_USR);
// default disable COMMAND, ADDR, MOSI, DUMMY, MISO // default disable COMMAND, ADDR, MOSI, DUMMY, MISO, and DOUTDIN (aka full-duplex)
CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_USR_COMMAND|SPI_USR_ADDR|SPI_USR_MOSI|SPI_USR_DUMMY|SPI_USR_MISO); CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_USR_COMMAND|SPI_USR_ADDR|SPI_USR_MOSI|SPI_USR_DUMMY|SPI_USR_MISO|SPI_DOUTDIN);
// default set bit lengths // default set bit lengths
WRITE_PERI_REG(SPI_USER1(spi_no), WRITE_PERI_REG(SPI_USER1(spi_no),
((addr_bitlen - 1) & SPI_USR_ADDR_BITLEN) << SPI_USR_ADDR_BITLEN_S | ((addr_bitlen - 1) & SPI_USR_ADDR_BITLEN) << SPI_USR_ADDR_BITLEN_S |
@ -298,6 +293,10 @@ void spi_mast_transaction(uint8 spi_no, uint8 cmd_bitlen, uint16 cmd_data, uint8
{ {
SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_USR_MISO); SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_USR_MISO);
} }
else if (miso_bitlen < 0)
{
SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_DOUTDIN);
}
// start transaction // start transaction
SET_PERI_REG_MASK(SPI_CMD(spi_no), SPI_USR); SET_PERI_REG_MASK(SPI_CMD(spi_no), SPI_USR);

View File

@ -18,14 +18,14 @@ void spi_lcd_mode_init(uint8 spi_no);
void spi_lcd_9bit_write(uint8 spi_no,uint8 high_bit,uint8 low_8bit); void spi_lcd_9bit_write(uint8 spi_no,uint8 high_bit,uint8 low_8bit);
//spi master init funtion //spi master init funtion
void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, uint32_t clock_div, uint8 full_duplex); void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, uint32_t clock_div);
// fill MOSI buffer // fill MOSI buffer
void spi_set_mosi(uint8 spi_no, uint8 offset, uint8 bitlen, uint32 data); void spi_set_mosi(uint8 spi_no, uint8 offset, uint8 bitlen, uint32 data);
// retrieve data from MISO buffer // retrieve data from MISO buffer
uint32 spi_get_miso(uint8 spi_no, uint8 offset, uint8 bitlen); uint32 spi_get_miso(uint8 spi_no, uint8 offset, uint8 bitlen);
// initiate SPI transaction // initiate SPI transaction
void spi_mast_transaction(uint8 spi_no, uint8 cmd_bitlen, uint16 cmd_data, uint8 addr_bitlen, uint32 addr_data, 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); uint16 mosi_bitlen, uint8 dummy_bitlen, sint16 miso_bitlen);
//transmit data to esp8266 slave buffer,which needs 16bit transmission , //transmit data to esp8266 slave buffer,which needs 16bit transmission ,
//first byte is master command 0x04, second byte is master data //first byte is master command 0x04, second byte is master data

View File

@ -7,9 +7,13 @@
#include "auxmods.h" #include "auxmods.h"
#include "lrotable.h" #include "lrotable.h"
static u8 spi_databits[NUM_SPI] = {0, 0}; #define SPI_HALFDUPLEX 0
#define SPI_FULLDUPLEX 1
// Lua: = spi.setup( id, mode, cpol, cpha, databits, clock_div, [full_duplex] ) static u8 spi_databits[NUM_SPI] = {0, 0};
static u8 spi_duplex[NUM_SPI] = {SPI_HALFDUPLEX, SPI_HALFDUPLEX};
// Lua: = spi.setup( id, mode, cpol, cpha, databits, clock_div, [duplex_mode] )
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 );
@ -18,7 +22,7 @@ static int spi_setup( lua_State *L )
int cpha = luaL_checkinteger( L, 4 ); int cpha = luaL_checkinteger( L, 4 );
int databits = luaL_checkinteger( L, 5 ); int databits = luaL_checkinteger( L, 5 );
u32 clock_div = luaL_checkinteger( L, 6 ); u32 clock_div = luaL_checkinteger( L, 6 );
int full_duplex = luaL_optinteger( L, 7, 1 ); int duplex_mode = luaL_optinteger( L, 7, SPI_HALFDUPLEX );
MOD_CHECK_ID( spi, id ); MOD_CHECK_ID( spi, id );
@ -43,18 +47,28 @@ static int spi_setup( lua_State *L )
clock_div = 8; clock_div = 8;
} }
if (full_duplex != 0 && full_duplex != 1) { if (duplex_mode == SPI_HALFDUPLEX || duplex_mode == SPI_FULLDUPLEX)
{
spi_duplex[id] = duplex_mode;
}
else
{
return luaL_error( L, "out of range" ); return luaL_error( L, "out of range" );
} }
spi_databits[id] = databits; spi_databits[id] = databits;
u32 res = platform_spi_setup(id, mode, cpol, cpha, clock_div, full_duplex); u32 res = platform_spi_setup(id, mode, cpol, cpha, clock_div);
lua_pushinteger( L, res ); lua_pushinteger( L, res );
return 1; return 1;
} }
static int spi_generic_send_recv( lua_State *L, u8 recv ) // 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 )
{ {
unsigned id = luaL_checkinteger( L, 1 ); unsigned id = luaL_checkinteger( L, 1 );
const char *pdata; const char *pdata;
@ -63,6 +77,7 @@ static int spi_generic_send_recv( lua_State *L, u8 recv )
u32 wrote = 0; u32 wrote = 0;
int pushed = 1; int pushed = 1;
unsigned argn, tos; unsigned argn, tos;
u8 recv = spi_duplex[id] == SPI_FULLDUPLEX ? 1 : 0;
MOD_CHECK_ID( spi, id ); MOD_CHECK_ID( spi, id );
if( (tos = lua_gettop( L )) < 2 ) if( (tos = lua_gettop( L )) < 2 )
@ -159,20 +174,6 @@ static int spi_generic_send_recv( lua_State *L, u8 recv )
return pushed; return pushed;
} }
// Lua: wrote = spi.send( id, data1, [data2], ..., [datan] )
// data can be either a string, a table or an 8-bit number
static int spi_send( lua_State *L )
{
return spi_generic_send_recv( L, 0 );
}
// 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 )
{
return spi_generic_send_recv( L, 1 );
}
// Lua: read = spi.recv( id, size, [default data] ) // Lua: read = spi.recv( id, size, [default data] )
static int spi_recv( lua_State *L ) static int spi_recv( lua_State *L )
{ {
@ -294,7 +295,7 @@ static int spi_transaction( lua_State *L )
return luaL_error( L, "dummy_bitlen out of range" ); return luaL_error( L, "dummy_bitlen out of range" );
} }
if (miso_bitlen < 0 || miso_bitlen > 511) { if (miso_bitlen < -512 || miso_bitlen > 511) {
return luaL_error( L, "miso_bitlen out of range" ); return luaL_error( L, "miso_bitlen out of range" );
} }
@ -313,8 +314,7 @@ static int spi_transaction( lua_State *L )
const LUA_REG_TYPE spi_map[] = const LUA_REG_TYPE spi_map[] =
{ {
{ LSTRKEY( "setup" ), LFUNCVAL( spi_setup ) }, { LSTRKEY( "setup" ), LFUNCVAL( spi_setup ) },
{ LSTRKEY( "send" ), LFUNCVAL( spi_send ) }, { LSTRKEY( "send" ), LFUNCVAL( spi_send_recv ) },
{ LSTRKEY( "send_recv" ), LFUNCVAL( spi_send_recv ) },
{ LSTRKEY( "recv" ), LFUNCVAL( spi_recv ) }, { LSTRKEY( "recv" ), LFUNCVAL( spi_recv ) },
{ LSTRKEY( "set_mosi" ), LFUNCVAL( spi_set_mosi ) }, { LSTRKEY( "set_mosi" ), LFUNCVAL( spi_set_mosi ) },
{ LSTRKEY( "get_miso" ), LFUNCVAL( spi_get_miso ) }, { LSTRKEY( "get_miso" ), LFUNCVAL( spi_get_miso ) },
@ -327,8 +327,8 @@ const LUA_REG_TYPE spi_map[] =
{ LSTRKEY( "CPOL_LOW" ), LNUMVAL( PLATFORM_SPI_CPOL_LOW) }, { LSTRKEY( "CPOL_LOW" ), LNUMVAL( PLATFORM_SPI_CPOL_LOW) },
{ LSTRKEY( "CPOL_HIGH" ), LNUMVAL( PLATFORM_SPI_CPOL_HIGH) }, { LSTRKEY( "CPOL_HIGH" ), LNUMVAL( PLATFORM_SPI_CPOL_HIGH) },
{ LSTRKEY( "DATABITS_8" ), LNUMVAL( 8 ) }, { LSTRKEY( "DATABITS_8" ), LNUMVAL( 8 ) },
{ LSTRKEY( "HALFDUPLEX" ), LNUMVAL( 0 ) }, { LSTRKEY( "HALFDUPLEX" ), LNUMVAL( SPI_HALFDUPLEX ) },
{ LSTRKEY( "FULLDUPLEX" ), LNUMVAL( 1 ) }, { LSTRKEY( "FULLDUPLEX" ), LNUMVAL( SPI_FULLDUPLEX ) },
#endif // #if LUA_OPTIMIZE_MEMORY > 0 #endif // #if LUA_OPTIMIZE_MEMORY > 0
{ LNILKEY, LNILVAL } { LNILKEY, LNILVAL }
}; };
@ -348,8 +348,8 @@ LUALIB_API int luaopen_spi( lua_State *L )
MOD_REG_NUMBER( L, "CPOL_LOW" , PLATFORM_SPI_CPOL_LOW); MOD_REG_NUMBER( L, "CPOL_LOW" , PLATFORM_SPI_CPOL_LOW);
MOD_REG_NUMBER( L, "CPOL_HIGH", PLATFORM_SPI_CPOL_HIGH); MOD_REG_NUMBER( L, "CPOL_HIGH", PLATFORM_SPI_CPOL_HIGH);
MOD_REG_NUMBER( L, "DATABITS_8", 8 ); MOD_REG_NUMBER( L, "DATABITS_8", 8 );
MOD_REG_NUMBER( L, "HALFDUPLEX", 0 ); MOD_REG_NUMBER( L, "HALFDUPLEX", SPI_HALFDUPLEX );
MOD_REG_NUMBER( L, "FULLDUPLEX", 1 ); MOD_REG_NUMBER( L, "FULLDUPLEX", SPI_FULLDUPLEX );
return 1; return 1;
#endif // #if LUA_OPTIMIZE_MEMORY > 0 #endif // #if LUA_OPTIMIZE_MEMORY > 0

View File

@ -435,15 +435,11 @@ int platform_i2c_recv_byte( unsigned id, int ack ){
return r; return r;
} }
static uint8_t platform_spi_fdplx[NUM_SPI] = {0, 0};
// ***************************************************************************** // *****************************************************************************
// SPI platform interface // SPI platform interface
uint32_t platform_spi_setup( uint8_t id, int mode, unsigned cpol, unsigned cpha, uint32_t platform_spi_setup( uint8_t id, int mode, unsigned cpol, unsigned cpha, uint32_t clock_div)
uint32_t clock_div, uint8 full_duplex )
{ {
platform_spi_fdplx[id] = full_duplex; spi_master_init( id, cpol, cpha, clock_div );
spi_master_init( id, cpol, cpha, clock_div, full_duplex );
return 1; return 1;
} }
@ -462,7 +458,7 @@ spi_data_type platform_spi_send_recv( uint8_t id, uint8_t bitlen, spi_data_type
return 0; return 0;
spi_mast_set_mosi( id, 0, bitlen, data ); spi_mast_set_mosi( id, 0, bitlen, data );
spi_mast_transaction( id, 0, 0, 0, 0, bitlen, 0, platform_spi_fdplx[id] > 0 ? 0 : bitlen ); spi_mast_transaction( id, 0, 0, 0, 0, bitlen, 0, -1 );
return spi_mast_get_miso( id, 0, bitlen ); return spi_mast_get_miso( id, 0, bitlen );
} }
@ -486,7 +482,7 @@ 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, 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 addr_bitlen, spi_data_type addr_data,
uint8_t mosi_bitlen, uint8_t dummy_bitlen, uint8_t miso_bitlen ) uint16_t mosi_bitlen, uint8_t dummy_bitlen, int16_t miso_bitlen )
{ {
if ((cmd_bitlen > 16) || if ((cmd_bitlen > 16) ||
(addr_bitlen > 32) || (addr_bitlen > 32) ||

View File

@ -96,7 +96,7 @@ typedef uint32_t spi_data_type;
// The platform SPI functions // The platform SPI functions
int platform_spi_exists( unsigned id ); int platform_spi_exists( unsigned id );
uint32_t platform_spi_setup( uint8_t id, int mode, unsigned cpol, unsigned cpha, uint32_t clock_div, uint8_t full_duplex); uint32_t platform_spi_setup( uint8_t id, int mode, unsigned cpol, unsigned cpha, uint32_t clock_div);
int platform_spi_send( uint8_t id, uint8_t bitlen, spi_data_type data ); int platform_spi_send( uint8_t id, uint8_t bitlen, spi_data_type data );
spi_data_type platform_spi_send_recv( uint8_t id, uint8_t bitlen, spi_data_type data ); spi_data_type platform_spi_send_recv( uint8_t id, uint8_t bitlen, spi_data_type data );
void platform_spi_select( unsigned id, int is_select ); void platform_spi_select( unsigned id, int is_select );
@ -105,7 +105,7 @@ int platform_spi_set_mosi( uint8_t id, uint8_t offset, uint8_t bitlen, spi_data_
spi_data_type platform_spi_get_miso( uint8_t id, uint8_t offset, uint8_t bitlen ); 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, 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 addr_bitlen, spi_data_type addr_data,
uint8_t mosi_bitlen, uint8_t dummy_bitlen, uint8_t miso_bitlen ); uint16_t mosi_bitlen, uint8_t dummy_bitlen, int16_t miso_bitlen );
// ***************************************************************************** // *****************************************************************************