diff --git a/app/driver/spi.c b/app/driver/spi.c index 286bc744..f458e2ef 100644 --- a/app/driver/spi.c +++ b/app/driver/spi.c @@ -132,26 +132,6 @@ void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, uint32_t clock_ ((7&SPI_USR_MISO_BITLEN)<1) return; //handle invalid input number - - while(READ_PERI_REG(SPI_CMD(spi_no))&SPI_USR); - - WRITE_PERI_REG(SPI_W0(spi_no), *data); - - SET_PERI_REG_MASK(SPI_CMD(spi_no), SPI_USR); - while(READ_PERI_REG(SPI_CMD(spi_no))&SPI_USR); - - *data = (uint8)(READ_PERI_REG(SPI_W0(spi_no))&0xff); -} - /****************************************************************************** * FunctionName : spi_mast_set_mosi * Description : Enter provided data into MOSI buffer. diff --git a/app/include/driver/spi.h b/app/include/driver/spi.h index 7bbf0d8c..5eb8faa5 100644 --- a/app/include/driver/spi.h +++ b/app/include/driver/spi.h @@ -19,11 +19,11 @@ 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, uint32_t clock_div); -//use spi send 8bit data -void spi_mast_byte_write(uint8 spi_no,uint8 *data); - +// fill MOSI buffer void spi_set_mosi(uint8 spi_no, uint8 offset, uint8 bitlen, uint32 data); +// retrieve data from MISO buffer uint32 spi_get_miso(uint8 spi_no, uint8 offset, uint8 bitlen); +// initiate SPI transaction 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); diff --git a/app/modules/spi.c b/app/modules/spi.c index ab94d801..d6bbc672 100644 --- a/app/modules/spi.c +++ b/app/modules/spi.c @@ -58,7 +58,7 @@ static int spi_send( lua_State *L ) for( argn = 3; argn <= lua_gettop( L ); argn ++ ) { numdata = ( u32 )luaL_checkinteger( L, argn ); - if (PLATFORM_OK != platform_spi_transaction( id, 0, 0, bitlen, numdata, 0, 0, 0 )) { + if (PLATFORM_OK != platform_spi_send( id, bitlen, numdata )) { return luaL_error( L, "failed" ); } wrote ++; diff --git a/app/modules/u8g.c b/app/modules/u8g.c index 7747a9de..0397e23a 100644 --- a/app/modules/u8g.c +++ b/app/modules/u8g.c @@ -861,7 +861,7 @@ uint8_t u8g_com_esp8266_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void break; case U8G_COM_MSG_WRITE_BYTE: - platform_spi_send_recv( 1, arg_val ); + platform_spi_send( 1, 8, arg_val ); break; case U8G_COM_MSG_WRITE_SEQ: @@ -870,7 +870,7 @@ uint8_t u8g_com_esp8266_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void register uint8_t *ptr = arg_ptr; while( arg_val > 0 ) { - platform_spi_send_recv( 1, *ptr++ ); + platform_spi_send( 1, 8, *ptr++ ); arg_val--; } } diff --git a/app/modules/ucg.c b/app/modules/ucg.c index 94e66218..3d51265f 100644 --- a/app/modules/ucg.c +++ b/app/modules/ucg.c @@ -752,36 +752,36 @@ static int16_t ucg_com_esp8266_hw_spi(ucg_t *ucg, int16_t msg, uint16_t arg, uin break; case UCG_COM_MSG_SEND_BYTE: - platform_spi_send_recv( 1, arg ); + platform_spi_send( 1, 8, arg ); break; case UCG_COM_MSG_REPEAT_1_BYTE: while( arg > 0 ) { - platform_spi_send_recv( 1, data[0] ); + platform_spi_send( 1, 8, data[0] ); arg--; } break; case UCG_COM_MSG_REPEAT_2_BYTES: while( arg > 0 ) { - platform_spi_send_recv( 1, data[0] ); - platform_spi_send_recv( 1, data[1] ); + platform_spi_send( 1, 8, data[0] ); + platform_spi_send( 1, 8, data[1] ); arg--; } break; case UCG_COM_MSG_REPEAT_3_BYTES: while( arg > 0 ) { - platform_spi_send_recv( 1, data[0] ); - platform_spi_send_recv( 1, data[1] ); - platform_spi_send_recv( 1, data[2] ); + platform_spi_send( 1, 8, data[0] ); + platform_spi_send( 1, 8, data[1] ); + platform_spi_send( 1, 8, data[2] ); arg--; } break; case UCG_COM_MSG_SEND_STR: while( arg > 0 ) { - platform_spi_send_recv( 1, *data++ ); + platform_spi_send( 1, 8, *data++ ); arg--; } break; @@ -802,7 +802,7 @@ static int16_t ucg_com_esp8266_hw_spi(ucg_t *ucg, int16_t msg, uint16_t arg, uin } } data++; - platform_spi_send_recv( 1, *data ); + platform_spi_send( 1, 8, *data ); data++; arg--; } diff --git a/app/platform/platform.c b/app/platform/platform.c index 314e7815..aa7fdc85 100644 --- a/app/platform/platform.c +++ b/app/platform/platform.c @@ -437,16 +437,20 @@ 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, uint32_t clock_div) +uint32_t platform_spi_setup( uint8_t id, int mode, unsigned cpol, unsigned cpha, uint32_t clock_div) { spi_master_init(id, cpol, cpha, clock_div); return 1; } -spi_data_type platform_spi_send_recv( unsigned id, spi_data_type data ) +int platform_spi_send( uint8_t id, uint8_t bitlen, spi_data_type data ) { - spi_mast_byte_write(id, &data); - return data; + if (bitlen > 32) + return PLATFORM_ERR; + + spi_mast_transaction( id, 0, 0, bitlen, data, 0, 0, 0 ); + + return PLATFORM_OK; } int platform_spi_set_mosi( uint8_t id, uint8_t offset, uint8_t bitlen, spi_data_type data ) diff --git a/app/platform/platform.h b/app/platform/platform.h index 64f7c668..42db1bfa 100644 --- a/app/platform/platform.h +++ b/app/platform/platform.h @@ -96,8 +96,8 @@ 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, uint32_t clock_div); -spi_data_type platform_spi_send_recv( unsigned id, spi_data_type data ); +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 ); 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 );