diff --git a/components/modules/spi_master.c b/components/modules/spi_master.c index e004f6c5..31628938 100644 --- a/components/modules/spi_master.c +++ b/components/modules/spi_master.c @@ -6,7 +6,7 @@ #include "lextra.h" #include "driver/spi_master.h" -#include "esp_heap_alloc_caps.h" +#include "esp_heap_caps.h" #include "esp_log.h" @@ -97,8 +97,8 @@ static int lspi_device_transfer( lua_State *L ) const char * const options[] = {"std", "dio", "qio"}; const uint32_t options_flags[] = {0, SPI_TRANS_MODE_DIO, SPI_TRANS_MODE_QIO}; - CONFIG_TRANS_FROM_FIELD(command); - CONFIG_TRANS_FROM_FIELD(address); + CONFIG_TRANS_FROM_FIELD(cmd); + CONFIG_TRANS_FROM_FIELD(addr); // lua_getfield( L, stack, "rxlen" ); rx_len = luaL_optint( L, -1, 0 ); @@ -128,7 +128,7 @@ static int lspi_device_transfer( lua_State *L ) } else { // use DMA'able buffer - if ((trans.tx_buffer = pvPortMallocCaps( data_len, MALLOC_CAP_DMA ))) { + if ((trans.tx_buffer = heap_caps_malloc( data_len, MALLOC_CAP_DMA ))) { memcpy( (void *)trans.tx_buffer, data, data_len ); } else { msg = "no memory"; @@ -147,7 +147,7 @@ static int lspi_device_transfer( lua_State *L ) } else { // use DMA'able buffer - if (!(trans.rx_buffer = pvPortMallocCaps( rx_len, MALLOC_CAP_DMA ))) { + if (!(trans.rx_buffer = heap_caps_malloc( rx_len, MALLOC_CAP_DMA ))) { msg = "no mem"; goto free_mem; } @@ -167,9 +167,9 @@ static int lspi_device_transfer( lua_State *L ) free_mem: if (!(trans.flags & SPI_TRANS_USE_TXDATA) && trans.tx_buffer) - free( (void *)trans.tx_buffer ); + heap_caps_free( (void *)trans.tx_buffer ); if (!(trans.flags & SPI_TRANS_USE_RXDATA) && trans.rx_buffer) - free( (void *)trans.rx_buffer ); + heap_caps_free( (void *)trans.rx_buffer ); if (msg) return luaL_error( L, msg ); @@ -237,9 +237,12 @@ int lspi_master( lua_State *L ) CONFIG_BUS_PIN_FROM_FIELD(miso); CONFIG_BUS_PIN_FROM_FIELD(quadwp); CONFIG_BUS_PIN_FROM_FIELD(quadhd); - lua_settop( L, stack ); - // - if (no_err( spi_bus_initialize( host, &config, 1 ) )) { + lua_pop( L, 5 ); + + int use_dma = luaL_optint( L, ++stack, 1 ); + luaL_argcheck( L, use_dma >= 0 && use_dma <= 2, stack, "out of range" ); + + if (no_err( spi_bus_initialize( host, &config, use_dma ) )) { lspi_host_t *ud = (lspi_host_t *)lua_newuserdata( L, sizeof( lspi_host_t ) ); luaL_getmetatable( L, UD_HOST_STR ); lua_setmetatable( L, -2 ); diff --git a/components/platform/u8x8_nodemcu_hal.c b/components/platform/u8x8_nodemcu_hal.c index fdf9b22b..6b16cd24 100644 --- a/components/platform/u8x8_nodemcu_hal.c +++ b/components/platform/u8x8_nodemcu_hal.c @@ -10,7 +10,7 @@ #include "u8x8_nodemcu_hal.h" -#include "esp_heap_alloc_caps.h" +#include "esp_heap_caps.h" uint8_t u8x8_gpio_and_delay_nodemcu(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) @@ -289,7 +289,7 @@ uint8_t u8x8_byte_nodemcu_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void * case U8X8_MSG_BYTE_START_TRANSFER: hal->buffer.size = 256; - if (!(hal->buffer.data = (uint8_t *)pvPortMallocCaps( hal->buffer.size, MALLOC_CAP_DMA ))) + if (!(hal->buffer.data = (uint8_t *)heap_caps_malloc( hal->buffer.size, MALLOC_CAP_DMA ))) return 0; hal->buffer.used = 0; @@ -300,12 +300,13 @@ uint8_t u8x8_byte_nodemcu_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void * while (hal->buffer.size - hal->buffer.used < arg_int) { hal->buffer.size *= 2; uint8_t *tmp; - if (!(tmp = (uint8_t *)pvPortMallocCaps( hal->buffer.size, MALLOC_CAP_DMA ))) { - free( hal->buffer.data ); + if (!(tmp = (uint8_t *)heap_caps_malloc( hal->buffer.size, MALLOC_CAP_DMA ))) { + heap_caps_free( hal->buffer.data ); + hal->buffer.data = NULL; return 0; } memcpy( tmp, hal->buffer.data, hal->buffer.used ); - free( hal->buffer.data ); + heap_caps_free( hal->buffer.data ); hal->buffer.data = tmp; } memcpy( hal->buffer.data + hal->buffer.used, arg_ptr, arg_int ); @@ -318,7 +319,7 @@ uint8_t u8x8_byte_nodemcu_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void * u8x8_gpio_SetCS( u8x8, u8x8->display_info->chip_disable_level ); if (hal->buffer.data) - free( hal->buffer.data ); + heap_caps_free( hal->buffer.data ); break; default: diff --git a/docs/en/modules/spi.md b/docs/en/modules/spi.md index 2a586bc3..af351f18 100644 --- a/docs/en/modules/spi.md +++ b/docs/en/modules/spi.md @@ -17,7 +17,7 @@ The host signals can be mapped to any suitable GPIO pins. Initializes a bus in master mode and returns a bus master object. #### Syntax -`spi.master(host, config)` +`spi.master(host, config[, dma])` #### Parameters - `host` id, one of @@ -30,6 +30,9 @@ Initializes a bus in master mode and returns a bus master object. - `miso` - `quadwp` - `quadhd` +- `dma` set DMA channel (1 or 2) or disable DMA (0), defaults to 1 if omitted. + Enabling DMA allows sending and receiving an unlimited amount of bytes but has restrictions in halfduplex mode (see [`spi.master:device()`](#spimasterdevice)). + Disabling DMA limits a transaction to 32 bytes max. #### Returns SPI bus master object @@ -61,6 +64,10 @@ none ## spi.master:device() Adds a device on the given master bus. Up to three devices per bus are supported. +!!! note + + Due to restrictions of the ESP IDF, halfduplex mode does not support DMA with both MOSI and MISO phases. Disable DMA during the call to [`spi.master()`](#spimaster) in this case and ensure that transaction data is not larger than 32 bytes. + #### Syntax `busmaster:device(config)` diff --git a/sdk/esp32-esp-idf b/sdk/esp32-esp-idf index 9b955f4c..3a1de7db 160000 --- a/sdk/esp32-esp-idf +++ b/sdk/esp32-esp-idf @@ -1 +1 @@ -Subproject commit 9b955f4c9f1b32652ea165d3e4cdaad01bba170e +Subproject commit 3a1de7dba3565424a12f795b71af45dc3c2072fc