diff --git a/app/driver/spi.c b/app/driver/spi.c index a45b2d64..12c32b54 100644 --- a/app/driver/spi.c +++ b/app/driver/spi.c @@ -64,12 +64,11 @@ 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) +void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, unsigned databits, uint32_t clock) { uint32 regvalue; if(spi_no>1) return; //handle invalid input number - if(spi_no==SPI){ WRITE_PERI_REG(PERIPHS_IO_MUX, 0x005); @@ -86,13 +85,33 @@ void spi_master_init(uint8 spi_no) 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_USR_COMMAND|SPI_USR_MOSI); - CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_FLASH_MODE); + SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_CS_SETUP|SPI_CS_HOLD|SPI_DOUTDIN|SPI_USR_MOSI); + + //set clock polarity + // TODO: This doesn't work + //if (cpol == 1) { + // SET_PERI_REG_MASK(SPI_CTRL2(spi_no), (SPI_CK_OUT_HIGH_MODE<1) return; //handle invalid input number - if(spi_no>1) 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); - CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_USR_MOSI|SPI_USR_MISO); + WRITE_PERI_REG(SPI_W0(HSPI), *data); - //SPI_FLASH_USER2 bit28-31 is cmd length,cmd bit length is value(0-15)+1, - // bit15-0 is cmd value. - WRITE_PERI_REG(SPI_USER2(spi_no), - ((7&SPI_USR_COMMAND_BITLEN)< 255 ) + return luaL_error( L, "wrong arg range" ); + platform_spi_send_recv( 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 || numdata > 255 ) + return luaL_error( L, "wrong arg range" ); + platform_spi_send_recv( id, numdata ); + } + wrote += i; + if( i < datalen ) + break; + } + else + { + pdata = luaL_checklstring( L, argn, &datalen ); + for( i = 0; i < datalen; i ++ ) + platform_spi_send_recv( id, pdata[ i ] ); + wrote += i; + if( i < datalen ) + break; + } + } + + lua_pushinteger( L, wrote ); + return 1; +} + +// Lua: read = spi.recv( id, size ) +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; + + MOD_CHECK_ID( spi, id ); + if (size == 0) { + return 0; + } + + luaL_buffinit( L, &b ); + for (i=0; i 0 + { LSTRKEY( "MASTER" ), LNUMVAL( PLATFORM_SPI_MASTER ) }, + { LSTRKEY( "SLAVE" ), LNUMVAL( PLATFORM_SPI_SLAVE) }, + { LSTRKEY( "CPHA_LOW" ), LNUMVAL( PLATFORM_SPI_CPHA_LOW) }, + { LSTRKEY( "CPHA_HIGH" ), LNUMVAL( PLATFORM_SPI_CPHA_HIGH) }, + { LSTRKEY( "CPOL_LOW" ), LNUMVAL( PLATFORM_SPI_CPOL_LOW) }, + { LSTRKEY( "CPOL_HIGH" ), LNUMVAL( PLATFORM_SPI_CPOL_HIGH) }, + { LSTRKEY( "DATABITS_8" ), LNUMVAL( PLATFORM_SPI_DATABITS_8) }, + { LSTRKEY( "DATABITS_16" ), LNUMVAL( PLATFORM_SPI_DATABITS_16) }, +#endif // #if LUA_OPTIMIZE_MEMORY > 0 + { LNILKEY, LNILVAL } +}; + +LUALIB_API int luaopen_spi( lua_State *L ) +{ +#if LUA_OPTIMIZE_MEMORY > 0 + return 0; +#else // #if LUA_OPTIMIZE_MEMORY > 0 + luaL_register( L, AUXLIB_SPI, spi_map ); + + // Add constants + MOD_REG_NUMBER( L, "MASTER", PLATFORM_SPI_MASTER); + MOD_REG_NUMBER( L, "SLAVE", PLATFORM_SPI_SLAVE); + MOD_REG_NUMBER( L, "CPHA_LOW" , PLATFORM_SPI_CPHA_LOW); + MOD_REG_NUMBER( L, "CPHA_HIGH", PLATFORM_SPI_CPHA_HIGH); + MOD_REG_NUMBER( L, "CPOL_LOW" , PLATFORM_SPI_CPOL_LOW); + MOD_REG_NUMBER( L, "CPOL_HIGH", PLATFORM_SPI_CPOL_HIGH); + MOD_REG_NUMBER( L, "DATABITS_8" , PLATFORM_SPI_DATABITS_8); + MOD_REG_NUMBER( L, "DATABITS_16" , PLATFORM_SPI_DATABITS_16); + + return 1; +#endif // #if LUA_OPTIMIZE_MEMORY > 0 +} + diff --git a/app/platform/cpu_esp8266.h b/app/platform/cpu_esp8266.h index 46689510..6e98e18a 100644 --- a/app/platform/cpu_esp8266.h +++ b/app/platform/cpu_esp8266.h @@ -8,7 +8,7 @@ #include "flash_api.h" // Number of resources (0 if not available/not implemented) #define NUM_GPIO GPIO_PIN_NUM -#define NUM_SPI 1 +#define NUM_SPI 2 #define NUM_UART 1 #define NUM_PWM GPIO_PIN_NUM #define NUM_ADC 1 diff --git a/app/platform/platform.c b/app/platform/platform.c index 9db3f9f6..c44fa013 100644 --- a/app/platform/platform.c +++ b/app/platform/platform.c @@ -435,6 +435,20 @@ int platform_i2c_recv_byte( unsigned id, int ack ){ return r; } +// ***************************************************************************** +// SPI platform interface +uint32_t platform_spi_setup( unsigned id, int mode, unsigned cpol, unsigned cpha, unsigned databits, uint32_t clock) +{ + spi_master_init(id, cpol, cpha, databits, clock); + return 1; +} + +spi_data_type platform_spi_send_recv( unsigned id, spi_data_type data ) +{ + spi_mast_byte_write(id, &data); + return data; +} + // **************************************************************************** // Flash access functions diff --git a/app/platform/platform.h b/app/platform/platform.h index 286dab2d..78452e3f 100644 --- a/app/platform/platform.h +++ b/app/platform/platform.h @@ -84,13 +84,23 @@ int platform_can_recv( unsigned id, uint32_t *canid, uint8_t *idtype, uint8_t *l // SPI enable/disable #define PLATFORM_SPI_ENABLE 1 #define PLATFORM_SPI_DISABLE 0 +// SPI clock phase +#define PLATFORM_SPI_CPHA_LOW 0 +#define PLATFORM_SPI_CPHA_HIGH 1 +// SPI clock polarity +#define PLATFORM_SPI_CPOL_LOW 0 +#define PLATFORM_SPI_CPOL_HIGH 1 +// SPI databits +#define PLATFORM_SPI_DATABITS_8 8 +#define PLATFORM_SPI_DATABITS_16 16 + // Data types 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, uint32_t clock, unsigned cpol, unsigned cpha, unsigned databits ); +uint32_t platform_spi_setup( unsigned id, int mode, unsigned cpol, unsigned cpha, unsigned databits, uint32_t clock); spi_data_type platform_spi_send_recv( unsigned id, spi_data_type data ); void platform_spi_select( unsigned id, int is_select );