Merge remote-tracking branch 'origin/dev-esp32' into ble

This commit is contained in:
Philip Gladstone 2024-01-31 01:24:47 +00:00
commit 8cafd558e4
51 changed files with 328 additions and 645 deletions

3
.gitignore vendored
View File

@ -10,6 +10,9 @@ components/*/.output/
tools/toolchains tools/toolchains
.ccache .ccache
bin bin
version.txt
managed_components/
dependencies.lock
#ignore Eclipse project files #ignore Eclipse project files
.cproject .cproject

View File

@ -1,15 +1,19 @@
# .readthedocs.yaml # Read the Docs configuration file for MkDocs projects
# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details # See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
# Required # Required
version: 2 version: 2
# Set the version of Python and other tools you might need
build:
os: ubuntu-22.04
tools:
python: "3.12"
mkdocs: mkdocs:
configuration: mkdocs.yml configuration: mkdocs.yml
# Optionally set the version of Python and requirements required to build your docs # Optionally declare the Python requirements required to build your docs
python: python:
version: "3.7"
install: install:
- requirements: docs/requirements.txt - requirements: docs/requirements.txt

View File

@ -2,6 +2,6 @@ idf_component_register(
SRCS "ip_fmt.c" "user_main.c" SRCS "ip_fmt.c" "user_main.c"
INCLUDE_DIRS "include" INCLUDE_DIRS "include"
REQUIRES "lua" REQUIRES "lua"
PRIV_REQUIRES "nvs_flash" "spiffs" PRIV_REQUIRES "nvs_flash" "spiffs" "esp_netif" "driver"
LDFRAGMENTS "nodemcu.lf" LDFRAGMENTS "nodemcu.lf"
) )

View File

@ -17,8 +17,8 @@
#include "esp_system.h" #include "esp_system.h"
#include "esp_event.h" #include "esp_event.h"
#include "esp_spiffs.h" #include "esp_spiffs.h"
#include "esp_netif.h"
#include "nvs_flash.h" #include "nvs_flash.h"
#include "flash_api.h"
#include "task/task.h" #include "task/task.h"
#include "sections.h" #include "sections.h"
@ -64,7 +64,7 @@ static void relay_default_loop_events(
if (task_post_medium(relayed_event_task, (intptr_t)&event)) if (task_post_medium(relayed_event_task, (intptr_t)&event))
xSemaphoreTake(relayed_event_handled, portMAX_DELAY); xSemaphoreTake(relayed_event_handled, portMAX_DELAY);
else else
printf("ERROR: failed to forward esp event %s/%d", base, id); printf("ERROR: failed to forward esp event %s/%ld", base, id);
} }

View File

@ -32,6 +32,9 @@
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/queue.h" #include "freertos/queue.h"
#include "soc/gpio_sig_map.h"
#include "rom/gpio.h"
#include "esp_intr_alloc.h" #include "esp_intr_alloc.h"
#include "soc/dport_reg.h" #include "soc/dport_reg.h"
#include <math.h> #include <math.h>

View File

@ -3,6 +3,7 @@ if(IDF_TARGET STREQUAL "esp32")
idf_component_register( idf_component_register(
SRCS "CAN.c" SRCS "CAN.c"
INCLUDE_DIRS "include" INCLUDE_DIRS "include"
PRIV_REQUIRES "driver" "esp_rom"
) )
endif() endif()

View File

@ -1,5 +1,6 @@
idf_component_register( idf_component_register(
SRCS "i2c_sw_master.c" SRCS "i2c_sw_master.c"
INCLUDE_DIRS "include" INCLUDE_DIRS "include"
REQUIRES "esp32" REQUIRES "esp_rom"
PRIV_REQUIRES "driver"
) )

View File

@ -1,14 +1,14 @@
#ifndef __I2C_SW_MASTER_H__ #ifndef __I2C_SW_MASTER_H__
#define __I2C_SW_MASTER_H__ #define __I2C_SW_MASTER_H__
#include "rom/ets_sys.h" #include "esp_rom_sys.h"
#define I2C_NUM_MAX 1 #define I2C_NUM_MAX 1
void i2c_sw_master_gpio_init(uint8_t sda, uint8_t scl); void i2c_sw_master_gpio_init(uint8_t sda, uint8_t scl);
void i2c_sw_master_init(void); void i2c_sw_master_init(void);
#define i2c_sw_master_wait ets_delay_us #define i2c_sw_master_wait esp_rom_delay_us
void i2c_sw_master_stop(void); void i2c_sw_master_stop(void);
void i2c_sw_master_start(void); void i2c_sw_master_start(void);
void i2c_sw_master_setAck(uint8_t level); void i2c_sw_master_setAck(uint8_t level);

View File

@ -2,7 +2,7 @@ menu "Lua configuration"
choice LUA_VERSION choice LUA_VERSION
prompt "Lua version" prompt "Lua version"
default LUA_VERSION_51 default LUA_VERSION_53
help help
The version of Lua interpreter to use. The version of Lua interpreter to use.

View File

@ -20,9 +20,7 @@
#include "lstate.h" #include "lstate.h"
#include "lstring.h" #include "lstring.h"
#include "lvm.h" #include "lvm.h"
#ifndef LUA_CROSS_COMPILER #ifdef LUA_CROSS_COMPILER
#include "flash_api.h"
#else
#include <limits.h> #include <limits.h>
#endif #endif
const TValue luaO_nilobject_ = {LUA_TVALUE_NIL}; const TValue luaO_nilobject_ = {LUA_TVALUE_NIL};

View File

@ -177,7 +177,7 @@ static void platform_flash_erase_sector(lu_int32 i) {
lockFlashWrite(); lockFlashWrite();
} }
static void platform_s_flash_write(const void *from, lu_int32 to, lu_int32 len) { static void platform_flash_write(const void *from, lu_int32 to, lu_int32 len) {
lua_assert(to >= LFSbase && to + len < LFSbase + LFS_SIZE); /* DEBUG */ lua_assert(to >= LFSbase && to + len < LFSbase + LFS_SIZE); /* DEBUG */
unlockFlashWrite(); unlockFlashWrite();
memcpy(byteptr(LFSregion) + (to-LFSbase), from, len); memcpy(byteptr(LFSregion) + (to-LFSbase), from, len);
@ -449,7 +449,7 @@ static const char *readF (lua_State *L, void *ud, size_t *size) {
static void eraseLFS(LFSflashState *F) { static void eraseLFS(LFSflashState *F) {
lu_int32 i; lu_int32 i;
#ifdef LUA_USE_ESP #ifdef LUA_USE_ESP
printf("\nErasing LFS from flash addr 0x%06x", F->addrPhys); printf("\nErasing LFS from flash addr 0x%06lx", F->addrPhys);
#endif #endif
unlockFlashWrite(); unlockFlashWrite();
for (i = 0; i < F->size; i += FLASH_PAGE_SIZE) { for (i = 0; i < F->size; i += FLASH_PAGE_SIZE) {
@ -464,7 +464,7 @@ static void eraseLFS(LFSflashState *F) {
platform_flash_erase_sector(s); platform_flash_erase_sector(s);
} }
#ifdef LUA_USE_ESP #ifdef LUA_USE_ESP
printf(" to 0x%06x\n", F->addrPhys + F->size-1); printf(" to 0x%06lx\n", F->addrPhys + F->size-1);
#endif #endif
flush_icache(F); flush_icache(F);
lockFlashWrite(); lockFlashWrite();
@ -483,7 +483,7 @@ LUAI_FUNC void luaN_flushFlash(void *vF) {
lu_int32 size = F->oNdx * WORDSIZE; lu_int32 size = F->oNdx * WORDSIZE;
lua_assert(start + size < F->addrPhys + F->size); /* is write in bounds? */ lua_assert(start + size < F->addrPhys + F->size); /* is write in bounds? */
//printf("Flush Buf: %6x (%u)\n", F->oNdx, size); //DEBUG //printf("Flush Buf: %6x (%u)\n", F->oNdx, size); //DEBUG
platform_s_flash_write(F->oBuff, start, size); platform_flash_write(F->oBuff, start, size);
F->oChunkNdx += F->oNdx; F->oChunkNdx += F->oNdx;
F->oNdx = 0; F->oNdx = 0;
} }

View File

@ -60,13 +60,14 @@ if(IDF_TARGET STREQUAL "esp32")
elseif(IDF_TARGET STREQUAL "esp32s2") elseif(IDF_TARGET STREQUAL "esp32s2")
list(APPEND module_srcs list(APPEND module_srcs
"dac.c" "dac.c"
"rmt.c"
"pulsecnt.c" "pulsecnt.c"
"rmt.c"
) )
elseif(IDF_TARGET STREQUAL "esp32s3") elseif(IDF_TARGET STREQUAL "esp32s3")
list(APPEND module_srcs list(APPEND module_srcs
"rmt.c"
"pulsecnt.c" "pulsecnt.c"
"rmt.c"
"sdmmc.c"
) )
elseif(IDF_TARGET STREQUAL "esp32c3") elseif(IDF_TARGET STREQUAL "esp32c3")
list(APPEND module_srcs list(APPEND module_srcs
@ -82,9 +83,11 @@ idf_component_register(
"base_nodemcu" "base_nodemcu"
"bt" "bt"
"driver_can" "driver_can"
"esp_eth"
"esp_http_client" "esp_http_client"
"fatfs"
"esp_http_server" "esp_http_server"
"esp_hw_support"
"fatfs"
"libsodium" "libsodium"
"lua" "lua"
"mbedtls" "mbedtls"
@ -92,10 +95,12 @@ idf_component_register(
"platform" "platform"
"qrcodegen" "qrcodegen"
"sdmmc" "sdmmc"
"spi_flash"
"sjson" "sjson"
"soc" "soc"
"u8g2" "u8g2"
"ucg" "ucg"
"vfs"
) )
# Match up all the module source files with their corresponding Kconfig # Match up all the module source files with their corresponding Kconfig

View File

@ -54,9 +54,9 @@ static int adc_read( lua_State *L )
// Lua: read_hall_sensor( ) // Lua: read_hall_sensor( )
static int read_hall_sensor( lua_State *L ) static int read_hall_sensor( lua_State *L )
{ {
int sample = platform_adc_read_hall_sensor( ); const char *msg = "ADC hall sensor no longer supported in IDF, sorry";
lua_pushinteger( L, ( lua_Integer ) sample ); platform_print_deprecation_note("msg", "IDFv5");
return 1; return luaL_error(L, msg);
} }
// Module function map // Module function map

View File

@ -29,7 +29,7 @@ CAN_device_t CAN_cfg = {
static task_handle_t can_data_task_id; static task_handle_t can_data_task_id;
static int can_on_received = LUA_NOREF; static int can_on_received = LUA_NOREF;
static xTaskHandle xCanTaskHandle = NULL; static TaskHandle_t xCanTaskHandle = NULL;
// LUA // LUA
static void can_data_task( task_param_t param, task_prio_t prio ) { static void can_data_task( task_param_t param, task_prio_t prio ) {

View File

@ -14,6 +14,7 @@
#include "esp_eth.h" #include "esp_eth.h"
#include "esp_eth_phy.h" #include "esp_eth_phy.h"
#include "driver/gpio.h" #include "driver/gpio.h"
#include "esp_mac.h"
typedef esp_eth_phy_t *(*new_eth_phy_fn)(const eth_phy_config_t *config); typedef esp_eth_phy_t *(*new_eth_phy_fn)(const eth_phy_config_t *config);
@ -21,9 +22,8 @@ typedef esp_eth_phy_t *(*new_eth_phy_fn)(const eth_phy_config_t *config);
typedef enum { typedef enum {
ETH_PHY_DP83848, ETH_PHY_DP83848,
ETH_PHY_IP101, ETH_PHY_IP101,
ETH_PHY_KSZ8041, ETH_PHY_KSZ80XX,
ETH_PHY_KSZ8081, ETH_PHY_LAN87XX,
ETH_PHY_LAN8720,
ETH_PHY_RTL8201, ETH_PHY_RTL8201,
ETH_PHY_MAX ETH_PHY_MAX
} eth_phy_t; } eth_phy_t;
@ -31,9 +31,8 @@ typedef enum {
static const new_eth_phy_fn new_eth_phy[] = { static const new_eth_phy_fn new_eth_phy[] = {
[ETH_PHY_DP83848] = esp_eth_phy_new_dp83848, [ETH_PHY_DP83848] = esp_eth_phy_new_dp83848,
[ETH_PHY_IP101] = esp_eth_phy_new_ip101, [ETH_PHY_IP101] = esp_eth_phy_new_ip101,
[ETH_PHY_KSZ8041] = esp_eth_phy_new_ksz8041, [ETH_PHY_KSZ80XX] = esp_eth_phy_new_ksz80xx,
[ETH_PHY_KSZ8081] = esp_eth_phy_new_ksz8081, [ETH_PHY_LAN87XX] = esp_eth_phy_new_lan87xx,
[ETH_PHY_LAN8720] = esp_eth_phy_new_lan8720,
[ETH_PHY_RTL8201] = esp_eth_phy_new_rtl8201, [ETH_PHY_RTL8201] = esp_eth_phy_new_rtl8201,
}; };
_Static_assert(sizeof(new_eth_phy) == (sizeof(new_eth_phy[0]) * ETH_PHY_MAX), _Static_assert(sizeof(new_eth_phy) == (sizeof(new_eth_phy[0]) * ETH_PHY_MAX),
@ -273,13 +272,15 @@ static int leth_init( lua_State *L )
// temporarily copy option table to top of stack for opt_ functions // temporarily copy option table to top of stack for opt_ functions
lua_pushvalue( L, stack ); lua_pushvalue( L, stack );
eth_mac_config_t mac_cfg = ETH_MAC_DEFAULT_CONFIG(); eth_esp32_emac_config_t emac_cfg = ETH_ESP32_EMAC_DEFAULT_CONFIG();
mac_cfg.smi_mdc_gpio_num = emac_cfg.smi_mdc_gpio_num =
opt_checkint_range( L, "mdc", -1, GPIO_NUM_0, GPIO_NUM_MAX-1 ); opt_checkint_range( L, "mdc", -1, GPIO_NUM_0, GPIO_NUM_MAX-1 );
mac_cfg.smi_mdio_gpio_num = emac_cfg.smi_mdio_gpio_num =
opt_checkint_range( L, "mdio", -1, GPIO_NUM_0, GPIO_NUM_MAX-1 ); opt_checkint_range( L, "mdio", -1, GPIO_NUM_0, GPIO_NUM_MAX-1 );
eth_mac_config_t mac_cfg = ETH_MAC_DEFAULT_CONFIG();
eth_phy_config_t phy_cfg = ETH_PHY_DEFAULT_CONFIG(); eth_phy_config_t phy_cfg = ETH_PHY_DEFAULT_CONFIG();
phy_cfg.phy_addr = opt_checkint_range( L, "addr", -1, -1, 31 ); phy_cfg.phy_addr = opt_checkint_range( L, "addr", -1, -1, 31 );
@ -290,7 +291,7 @@ static int leth_init( lua_State *L )
lua_settop( L, top ); lua_settop( L, top );
esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_cfg); esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&emac_cfg, &mac_cfg);
esp_eth_phy_t *phy = new_eth_phy[phy_type](&phy_cfg); esp_eth_phy_t *phy = new_eth_phy[phy_type](&phy_cfg);
esp_eth_config_t eth_cfg = ETH_DEFAULT_CONFIG(mac, phy); esp_eth_config_t eth_cfg = ETH_DEFAULT_CONFIG(mac, phy);
@ -302,10 +303,6 @@ static int leth_init( lua_State *L )
esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH(); esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_ETH();
esp_netif_t *new_eth = esp_netif_new(&netif_cfg); esp_netif_t *new_eth = esp_netif_new(&netif_cfg);
err = esp_eth_set_default_handlers(new_eth);
if (err != ESP_OK)
goto cleanup_netif;
void *glue = esp_eth_new_netif_glue(eth_handle); void *glue = esp_eth_new_netif_glue(eth_handle);
err = esp_netif_attach(new_eth, glue); err = esp_netif_attach(new_eth, glue);
if (err != ESP_OK) if (err != ESP_OK)
@ -320,8 +317,6 @@ static int leth_init( lua_State *L )
cleanup_glue: cleanup_glue:
esp_eth_del_netif_glue(glue); esp_eth_del_netif_glue(glue);
cleanup_netif:
esp_netif_destroy(new_eth); esp_netif_destroy(new_eth);
cleanup_mac_phy: cleanup_mac_phy:
@ -354,9 +349,11 @@ LROT_BEGIN(eth, NULL, 0)
LROT_NUMENTRY( PHY_DP83848, ETH_PHY_DP83848 ) LROT_NUMENTRY( PHY_DP83848, ETH_PHY_DP83848 )
LROT_NUMENTRY( PHY_IP101, ETH_PHY_IP101 ) LROT_NUMENTRY( PHY_IP101, ETH_PHY_IP101 )
LROT_NUMENTRY( PHY_KSZ8041, ETH_PHY_KSZ8041 ) LROT_NUMENTRY( PHY_KSZ80XX, ETH_PHY_KSZ80XX )
LROT_NUMENTRY( PHY_KSZ8081, ETH_PHY_KSZ8081 ) LROT_NUMENTRY( PHY_KSZ8041, ETH_PHY_KSZ80XX ) // deprecated in favour of 80XX
LROT_NUMENTRY( PHY_LAN8720, ETH_PHY_LAN8720 ) LROT_NUMENTRY( PHY_KSZ8081, ETH_PHY_KSZ80XX ) // deprecated in favour of 80XX
LROT_NUMENTRY( PHY_LAN87XX, ETH_PHY_LAN87XX )
LROT_NUMENTRY( PHY_LAN8720, ETH_PHY_LAN87XX ) // deprecated in favour of 87XX
LROT_NUMENTRY( PHY_RTL8201, ETH_PHY_RTL8201 ) LROT_NUMENTRY( PHY_RTL8201, ETH_PHY_RTL8201 )
LROT_END(eth, NULL, 0) LROT_END(eth, NULL, 0)

View File

@ -1,7 +1,10 @@
#include "module.h" #include "module.h"
#include "lauxlib.h" #include "lauxlib.h"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcpp"
#include "esp_heap_trace.h" #include "esp_heap_trace.h"
#pragma GCC diagnostic pop
static heap_trace_record_t *buffer = NULL; static heap_trace_record_t *buffer = NULL;

View File

@ -1,10 +1,11 @@
#include "module.h" #include "module.h"
#include "lauxlib.h" #include "lauxlib.h"
#include "lmem.h" #include "lmem.h"
#include "platform.h"
#include "driver/i2c.h" #include "driver/i2c.h"
#include "soc/i2c_reg.h" #include "soc/i2c_reg.h"
#include "hal/i2c_ll.h" #include "hal/i2c_ll.h"
#include "rom/gpio.h"
#include "i2c_common.h" #include "i2c_common.h"
@ -73,7 +74,7 @@ static void vTransferTask( void *pvParameters )
xQueueReceive( xQueue, job, portMAX_DELAY ); xQueueReceive( xQueue, job, portMAX_DELAY );
job->err = i2c_master_cmd_begin( job->port, job->cmd, job->err = i2c_master_cmd_begin( job->port, job->cmd,
job->to_ms > 0 ? job->to_ms / portTICK_RATE_MS : portMAX_DELAY ); job->to_ms > 0 ? job->to_ms / portTICK_PERIOD_MS : portMAX_DELAY );
task_post_medium( i2c_transfer_task_id, (task_param_t)job ); task_post_medium( i2c_transfer_task_id, (task_param_t)job );
} }
@ -202,11 +203,11 @@ int li2c_hw_master_setup( lua_State *L, unsigned id, unsigned sda, unsigned scl,
memset( &cfg, 0, sizeof( cfg ) ); memset( &cfg, 0, sizeof( cfg ) );
cfg.mode = I2C_MODE_MASTER; cfg.mode = I2C_MODE_MASTER;
luaL_argcheck( L, GPIO_IS_VALID_OUTPUT_GPIO(sda), 2, "invalid sda pin" ); luaL_argcheck( L, platform_gpio_output_exists(sda), 2, "invalid sda pin" );
cfg.sda_io_num = sda; cfg.sda_io_num = sda;
cfg.sda_pullup_en = GPIO_PULLUP_ENABLE; cfg.sda_pullup_en = GPIO_PULLUP_ENABLE;
luaL_argcheck( L, GPIO_IS_VALID_OUTPUT_GPIO(scl), 3, "invalid scl pin" ); luaL_argcheck( L, platform_gpio_output_exists(scl), 3, "invalid scl pin" );
cfg.scl_io_num = scl; cfg.scl_io_num = scl;
cfg.scl_pullup_en = GPIO_PULLUP_ENABLE; cfg.scl_pullup_en = GPIO_PULLUP_ENABLE;
@ -357,7 +358,7 @@ int li2c_hw_master_transfer( lua_State *L )
// note that i2c_master_cmd_begin() implements mutual exclusive access // note that i2c_master_cmd_begin() implements mutual exclusive access
// if it is currently in progress from the transfer task, it will block here until // if it is currently in progress from the transfer task, it will block here until
esp_err_t err = i2c_master_cmd_begin( job->port, job->cmd, esp_err_t err = i2c_master_cmd_begin( job->port, job->cmd,
job->to_ms > 0 ? job->to_ms / portTICK_RATE_MS : portMAX_DELAY ); job->to_ms > 0 ? job->to_ms / portTICK_PERIOD_MS : portMAX_DELAY );
switch (err) { switch (err) {
case ESP_OK: case ESP_OK:

View File

@ -30,11 +30,11 @@ typedef struct {
typedef struct { typedef struct {
struct { struct {
xTaskHandle taskHandle; TaskHandle_t taskHandle;
QueueHandle_t queue; QueueHandle_t queue;
} tx; } tx;
struct { struct {
xTaskHandle taskHandle; TaskHandle_t taskHandle;
QueueHandle_t queue; QueueHandle_t queue;
} rx; } rx;
int cb; int cb;
@ -152,8 +152,8 @@ static int node_i2s_start( lua_State *L )
// //
i2s_config.channel_format = opt_checkint(L, "channel", I2S_CHANNEL_FMT_RIGHT_LEFT); i2s_config.channel_format = opt_checkint(L, "channel", I2S_CHANNEL_FMT_RIGHT_LEFT);
i2s_config.communication_format = opt_checkint(L, "format", I2S_COMM_FORMAT_STAND_I2S); i2s_config.communication_format = opt_checkint(L, "format", I2S_COMM_FORMAT_STAND_I2S);
i2s_config.dma_buf_count = opt_checkint_range(L, "buffer_count", 2, 2, 128); i2s_config.dma_desc_num = opt_checkint_range(L, "buffer_count", 2, 2, 128);
i2s_config.dma_buf_len = opt_checkint_range(L, "buffer_len", i2s_config.sample_rate / 100, 8, 1024); i2s_config.dma_frame_num = opt_checkint_range(L, "buffer_len", i2s_config.sample_rate / 100, 8, 1024);
i2s_config.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1; i2s_config.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1;
// //
pin_config.bck_io_num = opt_checkint(L, "bck_pin", I2S_PIN_NO_CHANGE); pin_config.bck_io_num = opt_checkint(L, "bck_pin", I2S_PIN_NO_CHANGE);
@ -176,9 +176,9 @@ static int node_i2s_start( lua_State *L )
esp_err_t err; esp_err_t err;
if (i2s_config.mode & I2S_MODE_RX) { if (i2s_config.mode & I2S_MODE_RX) {
err = i2s_driver_install(i2s_id, &i2s_config, i2s_config.dma_buf_count, &is->rx.queue); err = i2s_driver_install(i2s_id, &i2s_config, i2s_config.dma_desc_num, &is->rx.queue);
} else { } else {
err = i2s_driver_install(i2s_id, &i2s_config, i2s_config.dma_buf_count, NULL); err = i2s_driver_install(i2s_id, &i2s_config, i2s_config.dma_desc_num, NULL);
} }
if (err != ESP_OK) if (err != ESP_OK)
luaL_error( L, "i2s can not start" ); luaL_error( L, "i2s can not start" );
@ -265,7 +265,7 @@ static int node_i2s_read( lua_State *L )
int wait_ms = luaL_optint(L, 3, 0); int wait_ms = luaL_optint(L, 3, 0);
char * data = luaM_malloc( L, bytes ); char * data = luaM_malloc( L, bytes );
size_t read; size_t read;
if (i2s_read(i2s_id, data, bytes, &read, wait_ms / portTICK_RATE_MS) != ESP_OK) if (i2s_read(i2s_id, data, bytes, &read, wait_ms / portTICK_PERIOD_MS) != ESP_OK)
return luaL_error( L, "I2S driver error" ); return luaL_error( L, "I2S driver error" );
lua_pushlstring(L, data, read); lua_pushlstring(L, data, read);

View File

@ -0,0 +1,7 @@
## IDF Component Manager Manifest File
dependencies:
## Required IDF version
idf:
version: ">=5.0.0"
# Component dependencies
libsodium: "~1.0.20"

View File

@ -50,6 +50,13 @@ typedef struct {
}; };
} mqtt_context_t; } mqtt_context_t;
typedef struct {
mqtt_context_t *ctx;
esp_mqtt_event_t event;
} event_info_t;
// event_handler_t is the function signature for all events // event_handler_t is the function signature for all events
typedef void (*event_handler_t)(lua_State* L, mqtt_context_t* mqtt_context, esp_mqtt_event_handle_t event); typedef void (*event_handler_t)(lua_State* L, mqtt_context_t* mqtt_context, esp_mqtt_event_handle_t event);
@ -64,10 +71,12 @@ task_handle_t event_handler_task_id = 0;
// event_clone makes a copy of the mqtt event received so we can pass it on // event_clone makes a copy of the mqtt event received so we can pass it on
// and the mqtt library can discard it. // and the mqtt library can discard it.
static esp_mqtt_event_handle_t event_clone(esp_mqtt_event_handle_t ev) { static event_info_t *event_clone(esp_mqtt_event_handle_t ev, mqtt_context_t *ctx) {
// allocate memory for the copy // allocate memory for the copy
esp_mqtt_event_handle_t ev1 = (esp_mqtt_event_handle_t)malloc(sizeof(esp_mqtt_event_t)); event_info_t *clone = (event_info_t *)malloc(sizeof(event_info_t));
ESP_LOGD(TAG, "event_clone(): event %p, event id %d, msg %d", ev, ev->event_id, ev->msg_id); ESP_LOGD(TAG, "event_clone(): event %p, event id %d, msg %d", ev, ev->event_id, ev->msg_id);
clone->ctx = ctx;
esp_mqtt_event_handle_t ev1 = &clone->event;
// make a shallow copy: // make a shallow copy:
*ev1 = *ev; *ev1 = *ev;
@ -95,11 +104,12 @@ static esp_mqtt_event_handle_t event_clone(esp_mqtt_event_handle_t ev) {
ev1->topic = NULL; ev1->topic = NULL;
} }
} }
return ev1; return clone;
} }
// event_free deallocates all the memory associated with a cloned event // event_free deallocates all the memory associated with a cloned event
static void event_free(esp_mqtt_event_handle_t ev) { static void event_free(event_info_t *clone) {
esp_mqtt_event_handle_t ev = &clone->event;
if (ev->data != NULL) { if (ev->data != NULL) {
ESP_LOGD(TAG, "event_free():free: event %p, msg %d, data %p", ev, ev->msg_id, ev->data); ESP_LOGD(TAG, "event_free():free: event %p, msg %d, data %p", ev, ev->msg_id, ev->data);
free(ev->data); free(ev->data);
@ -108,7 +118,7 @@ static void event_free(esp_mqtt_event_handle_t ev) {
ESP_LOGD(TAG, "event_free():free: event %p, msg %d, topic %p", ev, ev->msg_id, ev->topic); ESP_LOGD(TAG, "event_free():free: event %p, msg %d, topic %p", ev, ev->msg_id, ev->topic);
free(ev->topic); free(ev->topic);
} }
free(ev); free(clone);
} }
// event_connected is run when the mqtt client connected // event_connected is run when the mqtt client connected
@ -235,10 +245,9 @@ static void event_data_received(lua_State* L, mqtt_context_t* mqtt_context, esp_
// event_task_handler takes a nodemcu task message and dispatches it to the appropriate event_xxx callback above. // event_task_handler takes a nodemcu task message and dispatches it to the appropriate event_xxx callback above.
static void event_task_handler(task_param_t param, task_prio_t prio) { static void event_task_handler(task_param_t param, task_prio_t prio) {
// extract the event data out of the task param // extract the event data out of the task param
esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t)param; event_info_t *info = (event_info_t *)param;
esp_mqtt_event_handle_t event = &info->event;
// recover the mqtt context from the event user_context field: mqtt_context_t* mqtt_context = info->ctx;
mqtt_context_t* mqtt_context = (mqtt_context_t*)event->user_context;
// Check if this event is about an object that is in the process of garbage collection: // Check if this event is about an object that is in the process of garbage collection:
if (!luaX_valid_ref(mqtt_context->self)) { if (!luaX_valid_ref(mqtt_context->self)) {
@ -290,14 +299,17 @@ static void event_task_handler(task_param_t param, task_prio_t prio) {
lua_settop(L, top); // leave the stack as it was lua_settop(L, top); // leave the stack as it was
task_handler_end: task_handler_end:
event_free(event); // free the event copy memory event_free(info); // free the cloned event info
} }
// mqtt_event_handler receives all events from the esp mqtt library and converts them // mqtt_event_handler receives all events from the esp mqtt library and converts them
// to a task message // to a task message
static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) { static void mqtt_event_handler(void *handler_arg, esp_event_base_t base, int32_t event_id, void *event_data) {
task_post_medium(event_handler_task_id, (task_param_t)event_clone(event)); (void)base;
return ESP_OK; (void)event_id;
event_info_t *clone = event_clone(event_data, handler_arg);
if (!task_post_medium(event_handler_task_id, (task_param_t)clone))
event_free(clone);
} }
// Lua: on() // Lua: on()
@ -330,7 +342,15 @@ static int mqtt_connect(lua_State* L) {
memset(&config, 0, sizeof(esp_mqtt_client_config_t)); memset(&config, 0, sizeof(esp_mqtt_client_config_t));
// process function parameters populating the mqtt config structure // process function parameters populating the mqtt config structure
config.host = luaL_checkstring(L, 2); const char *host = luaL_checkstring(L, 2);
bool is_mqtt_uri = strncmp(host, "mqtt://", 7) == 0;
bool is_mqtts_uri = strncmp(host, "mqtts://", 8) == 0;
bool is_ws_uri = strncmp(host, "ws://", 5) == 0;
bool is_wss_uri = strncmp(host, "wss://", 6) == 0;
if (is_mqtt_uri || is_mqtts_uri || is_ws_uri || is_wss_uri)
config.broker.address.uri = host;
else
config.broker.address.hostname = host;
// set defaults: // set defaults:
int secure = 0; int secure = 0;
@ -340,34 +360,44 @@ static int mqtt_connect(lua_State* L) {
const char * cert_pem = NULL; const char * cert_pem = NULL;
const char * client_cert_pem = NULL; const char * client_cert_pem = NULL;
const char * client_key_pem = NULL; const char * client_key_pem = NULL;
size_t cert_pem_len = 0;
size_t client_cert_pem_len = 0;
size_t client_key_pem_len = 0;
if (lua_isnumber(L, n)) { if (lua_isnumber(L, n)) {
if (is_mqtt_uri || is_mqtts_uri)
return luaL_error(L, "port arg must be nil if giving full uri");
port = luaL_checknumber(L, n); port = luaL_checknumber(L, n);
n++; n++;
} else if (lua_type(L, n) == LUA_TNIL) {
n++;
} }
if (lua_isnumber(L, n)) { if (lua_isnumber(L, n)) {
if (is_mqtt_uri || is_mqtts_uri)
return luaL_error(L, "secure on/off determined by uri");
secure = !!luaL_checkinteger(L, n); secure = !!luaL_checkinteger(L, n);
n++; n++;
} else if (lua_type(L, n) == LUA_TNIL) {
n++;
} else { } else {
if (lua_istable(L, n)) { if (lua_istable(L, n)) {
secure = true; secure = true;
lua_getfield(L, n, "ca_cert"); lua_getfield(L, n, "ca_cert");
if ((cert_pem = luaL_optstring(L, -1, NULL)) != NULL) { if ((cert_pem = luaL_optlstring(L, -1, NULL, &cert_pem_len)) != NULL) {
luaX_set_ref(L, -1, &mqtt_context->cert_pem); luaX_set_ref(L, -1, &mqtt_context->cert_pem);
} }
lua_pop(L, 1); lua_pop(L, 1);
// //
lua_getfield(L, n, "client_cert"); lua_getfield(L, n, "client_cert");
if ((client_cert_pem = luaL_optstring(L, -1, NULL)) != NULL) { if ((client_cert_pem = luaL_optlstring(L, -1, NULL, &client_cert_pem_len)) != NULL) {
luaX_set_ref(L, -1, &mqtt_context->client_cert_pem); luaX_set_ref(L, -1, &mqtt_context->client_cert_pem);
} }
lua_pop(L, 1); lua_pop(L, 1);
// //
lua_getfield(L, n, "client_key"); lua_getfield(L, n, "client_key");
if ((client_key_pem = luaL_optstring(L, -1, NULL)) != NULL) { if ((client_key_pem = luaL_optlstring(L, -1, NULL, &client_key_pem_len)) != NULL) {
luaX_set_ref(L, -1, &mqtt_context->client_key_pem); luaX_set_ref(L, -1, &mqtt_context->client_key_pem);
} }
lua_pop(L, 1); lua_pop(L, 1);
@ -393,24 +423,26 @@ static int mqtt_connect(lua_State* L) {
ESP_LOGD(TAG, "connect: mqtt_context*: %p", mqtt_context); ESP_LOGD(TAG, "connect: mqtt_context*: %p", mqtt_context);
config.user_context = mqtt_context; // store a pointer to our context in the mqtt client user context field if (config.broker.address.uri == NULL)
// this will be useful to identify to which instance events belong to {
config.event_handle = mqtt_event_handler; // set the function that will be called by the mqtt client everytime something config.broker.address.port = port;
// happens config.broker.address.transport =
secure ? MQTT_TRANSPORT_OVER_SSL : MQTT_TRANSPORT_OVER_TCP;
config.client_id = mqtt_context->client_id; }
config.lwt_msg = mqtt_context->lwt_msg; config.broker.verification.certificate = cert_pem;
config.lwt_topic = mqtt_context->lwt_topic; config.broker.verification.certificate_len = cert_pem_len;
config.username = mqtt_context->username; config.credentials.authentication.certificate = client_cert_pem;
config.password = mqtt_context->password; config.credentials.authentication.certificate_len = client_cert_pem_len;
config.keepalive = mqtt_context->keepalive; config.credentials.authentication.key = client_key_pem;
config.disable_clean_session = mqtt_context->disable_clean_session; config.credentials.authentication.key_len = client_key_pem_len;
config.port = port; config.credentials.authentication.password = mqtt_context->password;
config.disable_auto_reconnect = (reconnect == 0); config.credentials.client_id = mqtt_context->client_id;
config.transport = secure ? MQTT_TRANSPORT_OVER_SSL : MQTT_TRANSPORT_OVER_TCP; config.credentials.username = mqtt_context->username;
config.cert_pem = cert_pem; config.network.disable_auto_reconnect = (reconnect == 0);
config.client_cert_pem = client_cert_pem; config.session.disable_clean_session = mqtt_context->disable_clean_session;
config.client_key_pem = client_key_pem; config.session.keepalive = mqtt_context->keepalive;
config.session.last_will.msg = mqtt_context->lwt_msg;
config.session.last_will.topic = mqtt_context->lwt_topic;
// create a mqtt client instance // create a mqtt client instance
mqtt_context->client = esp_mqtt_client_init(&config); mqtt_context->client = esp_mqtt_client_init(&config);
@ -419,6 +451,10 @@ static int mqtt_connect(lua_State* L) {
return 0; return 0;
} }
// register the event handler with mqtt_context as the handler arg
esp_mqtt_client_register_event(
mqtt_context->client, ESP_EVENT_ANY_ID, mqtt_event_handler, mqtt_context);
// actually start the mqtt client and connect // actually start the mqtt client and connect
esp_err_t err = esp_mqtt_client_start(mqtt_context->client); esp_err_t err = esp_mqtt_client_start(mqtt_context->client);
if (err != ESP_OK) { if (err != ESP_OK) {

View File

@ -5,6 +5,7 @@
#include "platform.h" #include "platform.h"
#include "task/task.h" #include "task/task.h"
#include "esp_system.h" #include "esp_system.h"
#include "esp_timer.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_sleep.h" #include "esp_sleep.h"
#include "driver/rtc_io.h" #include "driver/rtc_io.h"

View File

@ -37,6 +37,7 @@
#include "esp_system.h" #include "esp_system.h"
#include "esp_ota_ops.h" #include "esp_ota_ops.h"
#include "esp_partition.h" #include "esp_partition.h"
#include "spi_flash_mmap.h"
static esp_ota_handle_t ota_handle; static esp_ota_handle_t ota_handle;
static const esp_partition_t *next; static const esp_partition_t *next;

View File

@ -114,7 +114,7 @@ static void pulsecnt_task(task_param_t param, task_prio_t prio)
(void)prio; (void)prio;
// we bit packed the unit number and status into 1 int in the IRAM interrupt so need to unpack here // we bit packed the unit number and status into 1 int in the IRAM interrupt so need to unpack here
uint32_t unit = (uint32_t)param & 0xffu; uint8_t unit = (uint8_t)param & 0xffu;
int status = ((uint32_t)param >> 8); int status = ((uint32_t)param >> 8);
// int16_t cur_count, evt_count = 0; // int16_t cur_count, evt_count = 0;
@ -217,7 +217,7 @@ static void pulsecnt_task(task_param_t param, task_prio_t prio)
// lua_pushinteger (L, status); // lua_pushinteger (L, status);
luaL_pcallx (L, 6, 0); luaL_pcallx (L, 6, 0);
} else { } else {
if (pc->is_debug) ESP_LOGI("pulsecnt", "Could not find cb for unit %d with ptr %d", unit, pc->cb_ref); if (pc->is_debug) ESP_LOGI("pulsecnt", "Could not find cb for unit %d with ptr %ld", unit, pc->cb_ref);
} }
} }
@ -599,7 +599,7 @@ static int pulsecnt_create( lua_State *L ) {
pc->is_debug = true; pc->is_debug = true;
} }
if (pc->is_debug) ESP_LOGI("pulsecnt", "Created obj for unit %d with callback ref of %d", pc->unit, pc->cb_ref ); if (pc->is_debug) ESP_LOGI("pulsecnt", "Created obj for unit %d with callback ref of %ld", pc->unit, pc->cb_ref );
return 1; return 1;
} }

View File

@ -13,6 +13,7 @@
#include "driver/sdspi_host.h" #include "driver/sdspi_host.h"
#include "common.h" #include "common.h"
#include "platform.h"
// We're limiting ourselves to the number of FAT volumes configured. // We're limiting ourselves to the number of FAT volumes configured.
#define NUM_CARDS FF_VOLUMES #define NUM_CARDS FF_VOLUMES
@ -20,8 +21,12 @@ sdmmc_card_t *lsdmmc_card[NUM_CARDS];
// local definition for SDSPI host // local definition for SDSPI host
#define LSDMMC_HOST_SDSPI 100 #define LSDMMC_HOST_SDSPI 100
#ifdef CONFIG_IDF_TARGET_ESP32
#define LSDMMC_HOST_HSPI (LSDMMC_HOST_SDSPI + HSPI_HOST) #define LSDMMC_HOST_HSPI (LSDMMC_HOST_SDSPI + HSPI_HOST)
#define LSDMMC_HOST_VSPI (LSDMMC_HOST_SDSPI + VSPI_HOST) #define LSDMMC_HOST_VSPI (LSDMMC_HOST_SDSPI + VSPI_HOST)
#endif
#define LSDMMC_HOST_SPI2 (LSDMMC_HOST_SDSPI + SPI2_HOST)
#define LSDMMC_HOST_SPI3 (LSDMMC_HOST_SDSPI + SPI3_HOST)
typedef struct { typedef struct {
sdmmc_card_t *card; sdmmc_card_t *card;
@ -31,25 +36,21 @@ typedef struct {
} lsdmmc_ud_t; } lsdmmc_ud_t;
static void choose_partition(uint8_t pdrv, uint8_t part) static bool is_field_present(lua_State *L, int idx, const char *field)
{ {
// Update the volume->partition mapping in FatFS lua_getfield(L, idx, field);
for (unsigned i = 0; i < FF_VOLUMES; ++i) bool present = !lua_isnoneornil(L, -1);
{ lua_pop(L, 1);
if (VolToPart[i].pd == pdrv) return present;
VolToPart[i].pt = part;
}
} }
static esp_err_t sdmmc_mount_fat(lsdmmc_ud_t *ud, const char *base_path, uint8_t partition) static esp_err_t sdmmc_mount_fat(lsdmmc_ud_t *ud, const char *base_path)
{ {
esp_err_t err = ff_diskio_get_drive(&ud->pdrv); esp_err_t err = ff_diskio_get_drive(&ud->pdrv);
if (err != ESP_OK) if (err != ESP_OK)
return err; return err;
choose_partition(ud->pdrv, partition);
ff_diskio_register_sdmmc(ud->pdrv, ud->card); ff_diskio_register_sdmmc(ud->pdrv, ud->card);
char drv[3] = { (char)('0' + ud->pdrv), ':', 0 }; char drv[3] = { (char)('0' + ud->pdrv), ':', 0 };
@ -73,7 +74,6 @@ fail:
ud->fs = NULL; ud->fs = NULL;
} }
esp_vfs_fat_unregister_path(base_path); esp_vfs_fat_unregister_path(base_path);
choose_partition(ud->pdrv, 0);
ff_diskio_unregister(ud->pdrv); ff_diskio_unregister(ud->pdrv);
return err; return err;
@ -101,7 +101,10 @@ static int lsdmmc_init( lua_State *L )
int slot = luaL_checkint( L, ++stack ); int slot = luaL_checkint( L, ++stack );
luaL_argcheck( L, slot == SDMMC_HOST_SLOT_0 || slot == SDMMC_HOST_SLOT_1 || luaL_argcheck( L, slot == SDMMC_HOST_SLOT_0 || slot == SDMMC_HOST_SLOT_1 ||
slot == LSDMMC_HOST_HSPI || slot == LSDMMC_HOST_VSPI, #ifdef CONFIG_IDF_TARGET_ESP32
slot == LSDMMC_HOST_HSPI || slot == LSDMMC_HOST_VSPI ||
#endif
slot == LSDMMC_HOST_SPI2 || slot == LSDMMC_HOST_SPI3,
stack, "invalid slot" ); stack, "invalid slot" );
bool is_sdspi = false; bool is_sdspi = false;
@ -115,10 +118,7 @@ static int lsdmmc_init( lua_State *L )
int wp_pin = SDMMC_SLOT_NO_WP; int wp_pin = SDMMC_SLOT_NO_WP;
int freq_khz = SDMMC_FREQ_DEFAULT; int freq_khz = SDMMC_FREQ_DEFAULT;
int width = SDMMC_HOST_FLAG_1BIT; int width = SDMMC_HOST_FLAG_1BIT;
// additional entries for SDSPI configuration // additional cs for SDSPI configuration
int sck_pin = -1;
int mosi_pin = -1;
int miso_pin = -1;
int cs_pin = -1; int cs_pin = -1;
if (lua_type( L, ++stack ) == LUA_TTABLE) { if (lua_type( L, ++stack ) == LUA_TTABLE) {
@ -131,9 +131,12 @@ static int lsdmmc_init( lua_State *L )
// mandatory entries for SDSPI configuration // mandatory entries for SDSPI configuration
if (is_sdspi) { if (is_sdspi) {
sck_pin = opt_checkint_range( L, "sck_pin", -1, 0, GPIO_NUM_MAX ); if (is_field_present(L, -1, "sck_pin") ||
mosi_pin = opt_checkint_range( L, "mosi_pin", -1, 0, GPIO_NUM_MAX ); is_field_present(L, -1, "mosi_pin") ||
miso_pin = opt_checkint_range( L, "miso_pin", -1, 0, GPIO_NUM_MAX ); is_field_present(L, -1, "miso_pin"))
{
platform_print_deprecation_note("SCK/MOSI/MISO ignored; please configure via spimaster instead", "IDFv5");
}
cs_pin = opt_checkint_range( L, "cs_pin", -1, 0, GPIO_NUM_MAX ); cs_pin = opt_checkint_range( L, "cs_pin", -1, 0, GPIO_NUM_MAX );
} }
@ -166,16 +169,16 @@ static int lsdmmc_init( lua_State *L )
} }
if (res == ESP_OK || res == ESP_ERR_INVALID_STATE) { if (res == ESP_OK || res == ESP_ERR_INVALID_STATE) {
sdmmc_host_t sdspi_host_config = SDSPI_HOST_DEFAULT();
sdmmc_host_t sdmmc_host_config = SDMMC_HOST_DEFAULT();
if (is_sdspi) { if (is_sdspi) {
// configure SDSPI slot // configure SDSPI slot
sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT(); sdspi_device_config_t dev_config = SDSPI_DEVICE_CONFIG_DEFAULT();
slot_config.gpio_miso = miso_pin; dev_config.gpio_cs = cs_pin;
slot_config.gpio_mosi = mosi_pin; dev_config.gpio_cd = cd_pin;
slot_config.gpio_sck = sck_pin; dev_config.gpio_wp = wp_pin;
slot_config.gpio_cs = cs_pin; res = sdspi_host_init_device(&dev_config, &sdspi_host_config.slot);
slot_config.gpio_cd = cd_pin;
slot_config.gpio_wp = wp_pin;
res = sdspi_host_init_slot( slot, &slot_config );
} else { } else {
// configure SDMMC slot // configure SDMMC slot
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT(); sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
@ -184,10 +187,7 @@ static int lsdmmc_init( lua_State *L )
res = sdmmc_host_init_slot( slot, &slot_config ); res = sdmmc_host_init_slot( slot, &slot_config );
} }
if (res == ESP_OK) { if (res == ESP_OK) {
// initialize card // initialize card
sdmmc_host_t sdspi_host_config = SDSPI_HOST_DEFAULT();
sdmmc_host_t sdmmc_host_config = SDMMC_HOST_DEFAULT();
sdmmc_host_t *host_config = is_sdspi ? &sdspi_host_config : &sdmmc_host_config; sdmmc_host_t *host_config = is_sdspi ? &sdspi_host_config : &sdmmc_host_config;
host_config->slot = slot; host_config->slot = slot;
host_config->flags &= ~SDMMC_HOST_FLAG_8BIT; host_config->flags &= ~SDMMC_HOST_FLAG_8BIT;
@ -352,13 +352,21 @@ static int lsdmmc_mount( lua_State *L )
(void)card; (void)card;
const char *ldrv = luaL_checkstring(L, 2); const char *ldrv = luaL_checkstring(L, 2);
int part = luaL_optint(L, 3, 0); if (!lua_isnoneornil(L, 3))
{
// Warn that this feature isn't around
platform_print_deprecation_note(
"partition selection not supported", "IDFv5");
// ...and error if used to select something we can no longer do
if (luaL_optint(L, 3, 0) > 1)
return luaL_error(L, "only first partition supported since IDFv5");
}
lua_settop(L, 2); lua_settop(L, 2);
if (ud->fs == NULL) if (ud->fs == NULL)
{ {
esp_err_t err = sdmmc_mount_fat(ud, ldrv, part); esp_err_t err = sdmmc_mount_fat(ud, ldrv);
if (err == ESP_OK) if (err == ESP_OK)
{ {
// We need this when we unmount // We need this when we unmount
@ -408,8 +416,12 @@ LROT_BEGIN(sdmmc, NULL, 0)
LROT_FUNCENTRY( init, lsdmmc_init ) LROT_FUNCENTRY( init, lsdmmc_init )
LROT_NUMENTRY( HS1, SDMMC_HOST_SLOT_0 ) LROT_NUMENTRY( HS1, SDMMC_HOST_SLOT_0 )
LROT_NUMENTRY( HS2, SDMMC_HOST_SLOT_1 ) LROT_NUMENTRY( HS2, SDMMC_HOST_SLOT_1 )
#ifdef CONFIG_IDF_TARGET_ESP32
LROT_NUMENTRY( HSPI, LSDMMC_HOST_HSPI ) LROT_NUMENTRY( HSPI, LSDMMC_HOST_HSPI )
LROT_NUMENTRY( VSPI, LSDMMC_HOST_VSPI ) LROT_NUMENTRY( VSPI, LSDMMC_HOST_VSPI )
#endif
LROT_NUMENTRY( SPI2, LSDMMC_HOST_SPI2 )
LROT_NUMENTRY( SPI3, LSDMMC_HOST_SPI3 )
LROT_NUMENTRY( W1BIT, SDMMC_HOST_FLAG_1BIT ) LROT_NUMENTRY( W1BIT, SDMMC_HOST_FLAG_1BIT )
LROT_NUMENTRY( W4BIT, SDMMC_HOST_FLAG_1BIT | LROT_NUMENTRY( W4BIT, SDMMC_HOST_FLAG_1BIT |
SDMMC_HOST_FLAG_4BIT ) SDMMC_HOST_FLAG_4BIT )

View File

@ -17,7 +17,9 @@ LROT_BEGIN(lspi, NULL, 0)
#endif #endif
LROT_NUMENTRY( SPI1, SPI1_HOST ) LROT_NUMENTRY( SPI1, SPI1_HOST )
LROT_NUMENTRY( SPI2, SPI2_HOST ) LROT_NUMENTRY( SPI2, SPI2_HOST )
#ifdef SPI3_HOST
LROT_NUMENTRY( SPI3, SPI3_HOST ) LROT_NUMENTRY( SPI3, SPI3_HOST )
#endif
LROT_END(lspi, NULL, 0) LROT_END(lspi, NULL, 0)
int luaopen_spi( lua_State *L ) { int luaopen_spi( lua_State *L ) {

View File

@ -32,8 +32,8 @@ static int time_get(lua_State *L)
{ {
struct timeval tv; struct timeval tv;
gettimeofday (&tv, NULL); gettimeofday (&tv, NULL);
lua_pushnumber (L, tv.tv_sec); lua_pushinteger (L, tv.tv_sec);
lua_pushnumber (L, tv.tv_usec); lua_pushinteger (L, tv.tv_usec);
return 2; return 2;
} }

View File

@ -190,7 +190,7 @@ static int touch_create( lua_State *L ) {
tp->is_intr = opt_checkbool(L, "intrInitAtStart", true); tp->is_intr = opt_checkbool(L, "intrInitAtStart", true);
tp->thresTrigger = opt_checkint_range(L, "thresTrigger", TOUCH_TRIGGER_BELOW, TOUCH_TRIGGER_BELOW, TOUCH_TRIGGER_MAX); tp->thresTrigger = opt_checkint_range(L, "thresTrigger", TOUCH_TRIGGER_BELOW, TOUCH_TRIGGER_BELOW, TOUCH_TRIGGER_MAX);
if (tp->is_debug) ESP_LOGI(TAG, "isDebug: %d, filterMs: %d, lvolt: %d, hvolt: %d, atten: %d, slope: %d, intrInitAtStart: %d, thresTrigger: %d", if (tp->is_debug) ESP_LOGI(TAG, "isDebug: %d, filterMs: %lu, lvolt: %d, hvolt: %d, atten: %d, slope: %d, intrInitAtStart: %d, thresTrigger: %d",
tp->is_debug, tp->filterMs, tp->lvolt, tp->hvolt, tp->atten, tp->slope, tp->is_intr, tp->thresTrigger); tp->is_debug, tp->filterMs, tp->lvolt, tp->hvolt, tp->atten, tp->slope, tp->is_intr, tp->thresTrigger);
// get the field pad. this can be passed in as int or table of ints. pad = 0 || {0,1,2,3,4,5,6,7,8,9} // get the field pad. this can be passed in as int or table of ints. pad = 0 || {0,1,2,3,4,5,6,7,8,9}
@ -277,7 +277,7 @@ static int touch_create( lua_State *L ) {
// Initialize and start a software filter to detect slight change of capacitance. // Initialize and start a software filter to detect slight change of capacitance.
if (tp->filterMs > 0) { if (tp->filterMs > 0) {
touch_pad_filter_start(tp->filterMs); touch_pad_filter_start(tp->filterMs);
if (tp->is_debug) ESP_LOGI(TAG, "Set filter period to %d ms", tp->filterMs ); if (tp->is_debug) ESP_LOGI(TAG, "Set filter period to %lu ms", tp->filterMs );
} }
// Register touch interrupt ISR // Register touch interrupt ISR
@ -320,7 +320,7 @@ static int touch_create( lua_State *L ) {
// prevent false triggering when detecting slight change of capacitance. Need to call // prevent false triggering when detecting slight change of capacitance. Need to call
// touch_pad_filter_start before all touch filter APIs // touch_pad_filter_start before all touch filter APIs
if (tp->filterMs > 0) { if (tp->filterMs > 0) {
if (tp->is_debug) ESP_LOGI(TAG, "You provided a filter so turning on filter mode. filterMs: %d", tp->filterMs); if (tp->is_debug) ESP_LOGI(TAG, "You provided a filter so turning on filter mode. filterMs: %lu", tp->filterMs);
esp_err_t err = touch_pad_filter_start(tp->filterMs); esp_err_t err = touch_pad_filter_start(tp->filterMs);
if (err == ESP_ERR_INVALID_ARG) { if (err == ESP_ERR_INVALID_ARG) {
ESP_LOGI(TAG, "Filter start parameter error"); ESP_LOGI(TAG, "Filter start parameter error");

View File

@ -1,8 +1,27 @@
idf_component_register( idf_component_register(
SRCS "dht.c" "flash_api.c" "onewire.c" "platform.c" SRCS
"platform_flash.c" "platform_partition.c" "platform_rmt.c" "dht.c"
"u8x8_nodemcu_hal.c" "ucg_nodemcu_hal.c" "wdt.c" "ws2812.c" "onewire.c"
"platform.c"
"platform_flash.c"
"platform_partition.c"
"platform_rmt.c"
"u8x8_nodemcu_hal.c"
"ucg_nodemcu_hal.c"
"wdt.c"
"ws2812.c"
INCLUDE_DIRS "include" INCLUDE_DIRS "include"
REQUIRES "spiffs" "u8g2" "ucg" "driver_i2c" "task" REQUIRES
PRIV_REQUIRES "bootloader_support" "lua" "esp32" "driver"
"driver_i2c"
"spi_flash"
"spiffs"
"task"
"u8g2"
"ucg"
PRIV_REQUIRES
"bootloader_support"
"esp_rom"
"lua"
"soc"
) )

View File

@ -1,179 +0,0 @@
/******************************************************************************
* Flash api for NodeMCU
* NodeMCU Team
* 2014-12-31
*******************************************************************************/
#include "flash_api.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "rom/spi_flash.h"
#include "platform_wdt.h"
#include "esp_image_format.h"
#include "esp_flash_partitions.h"
#define FLASH_HDR_ADDR 0x1000
static inline esp_image_header_t flash_load_rom_header (void)
{
esp_image_header_t hdr;
if (ESP_OK !=
spi_flash_read (FLASH_HDR_ADDR, (uint32_t *)&hdr, sizeof (hdr)))
{
NODE_ERR("Failed to load flash header block!\n");
abort();
}
return hdr;
}
#define IRAM_SECTION __attribute__((section(".iram1")))
static void IRAM_SECTION update_flash_chip_size (uint32_t sz)
{
esp_rom_spiflash_config_param (
g_rom_flashchip.device_id,
sz,
g_rom_flashchip.block_size,
g_rom_flashchip.sector_size,
g_rom_flashchip.page_size,
g_rom_flashchip.status_mask);
}
static uint32_t __attribute__((section(".iram1"))) flash_detect_size_byte(void)
{
#define DETECT_SZ 32
uint32_t detected_size = FLASH_SIZE_1MBYTE;
uint8_t data_orig[DETECT_SZ] PLATFORM_ALIGNMENT = {0};
uint8_t data_new[DETECT_SZ] PLATFORM_ALIGNMENT = {0};
// Ensure we read something which isn't just 0xff...
const uint32_t offs = ESP_PARTITION_TABLE_OFFSET;
// Detect read failure or wrap-around on flash read to find end of flash
if (ESP_OK == spi_flash_read (offs, (uint32_t *)data_orig, DETECT_SZ))
{
update_flash_chip_size (FLASH_SIZE_16MBYTE);
while ((detected_size < FLASH_SIZE_16MBYTE) &&
(ESP_OK == spi_flash_read (
detected_size + offs, (uint32_t *)data_new, DETECT_SZ)) &&
(0 != memcmp(data_orig, data_new, DETECT_SZ)))
{
detected_size *= 2;
}
update_flash_chip_size (detected_size);
};
return detected_size;
#undef FLASH_BUFFER_SIZE_DETECT
}
uint32_t flash_safe_get_size_byte(void)
{
static uint32_t flash_size = 0;
if (flash_size == 0)
{
flash_size = flash_detect_size_byte();
}
return flash_size;
}
uint16_t flash_safe_get_sec_num(void)
{
return (flash_safe_get_size_byte() / (SPI_FLASH_SEC_SIZE));
}
uint32_t flash_rom_get_size_byte(void)
{
static uint32_t flash_size = 0;
if (flash_size == 0)
{
switch (flash_load_rom_header ().spi_size)
{
default: // Unknown flash size, fall back mode.
case ESP_IMAGE_FLASH_SIZE_1MB: flash_size = FLASH_SIZE_1MBYTE; break;
case ESP_IMAGE_FLASH_SIZE_2MB: flash_size = FLASH_SIZE_2MBYTE; break;
case ESP_IMAGE_FLASH_SIZE_4MB: flash_size = FLASH_SIZE_4MBYTE; break;
case ESP_IMAGE_FLASH_SIZE_8MB: flash_size = FLASH_SIZE_8MBYTE; break;
case ESP_IMAGE_FLASH_SIZE_16MB: flash_size = FLASH_SIZE_16MBYTE; break;
}
}
return flash_size;
}
static bool flash_rom_set_size_type(uint8_t size_code)
{
// Dangerous, here are dinosaur infested!!!!!
// Reboot required!!!
// If you don't know what you're doing, your nodemcu may turn into stone ...
NODE_DBG("\nBEGIN SET FLASH HEADER\n");
esp_image_header_t *hdr = (esp_image_header_t *)malloc (SPI_FLASH_SEC_SIZE);
if (!hdr)
return false;
if (ESP_OK == spi_flash_read (FLASH_HDR_ADDR, (uint32_t *)hdr, SPI_FLASH_SEC_SIZE))
{
hdr->spi_size = size_code;
if (ESP_OK == spi_flash_erase_sector (FLASH_HDR_ADDR / SPI_FLASH_SEC_SIZE))
{
NODE_DBG("\nERASE SUCCESS\n");
}
if (ESP_OK == spi_flash_write(FLASH_HDR_ADDR, (uint32_t *)hdr, SPI_FLASH_SEC_SIZE))
{
NODE_DBG("\nWRITE SUCCESS, %u\n", size_code);
}
}
free (hdr);
NODE_DBG("\nEND SET FLASH HEADER\n");
return true;
}
bool flash_rom_set_size_byte(uint32_t size)
{
// Dangerous, here are dinosaur infested!!!!!
// Reboot required!!!
bool ok = true;
uint8_t size_code = 0;
switch (size)
{
case FLASH_SIZE_1MBYTE: size_code = ESP_IMAGE_FLASH_SIZE_1MB; break;
case FLASH_SIZE_2MBYTE: size_code = ESP_IMAGE_FLASH_SIZE_2MB; break;
case FLASH_SIZE_4MBYTE: size_code = ESP_IMAGE_FLASH_SIZE_4MB; break;
case FLASH_SIZE_8MBYTE: size_code = ESP_IMAGE_FLASH_SIZE_8MB; break;
case FLASH_SIZE_16MBYTE: size_code = ESP_IMAGE_FLASH_SIZE_16MB; break;
default: ok = false; break;
}
if (ok)
ok = flash_rom_set_size_type (size_code);
return ok;
}
uint16_t flash_rom_get_sec_num(void)
{
return ( flash_rom_get_size_byte() / (SPI_FLASH_SEC_SIZE) );
}
uint8_t flash_rom_get_mode(void)
{
return flash_load_rom_header ().spi_mode;
}
uint32_t flash_rom_get_speed(void)
{
switch (flash_load_rom_header ().spi_speed)
{
case ESP_IMAGE_SPI_SPEED_40M: return 40000000;
case ESP_IMAGE_SPI_SPEED_26M: return 26700000; // TODO: verify 26.7MHz
case ESP_IMAGE_SPI_SPEED_20M: return 20000000;
case ESP_IMAGE_SPI_SPEED_80M: return 80000000;
default: break;
}
return 0;
}
esp_err_t flash_erase(size_t sector)
{
platform_wdt_feed();
return spi_flash_erase_sector(sector);
}

View File

@ -2,7 +2,7 @@
#define _CPU_ESP32_H_ #define _CPU_ESP32_H_
#include "sdkconfig.h" #include "sdkconfig.h"
#include "esp_spi_flash.h" #include "spi_flash_mmap.h"
#define NUM_UART SOC_UART_NUM #define NUM_UART SOC_UART_NUM
@ -10,12 +10,4 @@
#define INTERNAL_FLASH_WRITE_UNIT_SIZE 4 #define INTERNAL_FLASH_WRITE_UNIT_SIZE 4
#define INTERNAL_FLASH_READ_UNIT_SIZE 4 #define INTERNAL_FLASH_READ_UNIT_SIZE 4
#define FLASH_SEC_NUM (flash_safe_get_sec_num())
// Determine whether an address is in the flash-cache range
static inline bool is_cache_flash_addr (uint32_t addr)
{
return addr >= 0x3F400000 && addr < 0x3FC00000;
}
#endif #endif

View File

@ -1,25 +0,0 @@
#ifndef __FLASH_API_H__
#define __FLASH_API_H__
#include "platform.h"
#include "esp_spi_flash.h"
uint32_t flash_safe_get_size_byte(void);
uint16_t flash_safe_get_sec_num(void);
uint32_t flash_rom_get_size_byte(void);
bool flash_rom_set_size_byte(uint32_t);
uint16_t flash_rom_get_sec_num(void);
uint8_t flash_rom_get_mode(void);
uint32_t flash_rom_get_speed(void);
#define FLASH_SIZE_1MBYTE ( 1 * 1024 * 1024)
#define FLASH_SIZE_2MBYTE ( 2 * 1024 * 1024)
#define FLASH_SIZE_4MBYTE ( 4 * 1024 * 1024)
#define FLASH_SIZE_8MBYTE ( 8 * 1024 * 1024)
#define FLASH_SIZE_16MBYTE (16 * 1024 * 1024)
#define flash_write spi_flash_write
esp_err_t flash_erase(size_t sector);
#define flash_read spi_flash_read
#endif // __FLASH_API_H__

View File

@ -90,7 +90,7 @@ typedef struct {
typedef struct { typedef struct {
QueueHandle_t queue; QueueHandle_t queue;
xTaskHandle taskHandle; TaskHandle_t taskHandle;
int receive_rf; int receive_rf;
int error_rf; int error_rf;
char *line_buffer; char *line_buffer;
@ -137,7 +137,6 @@ int platform_adc_channel_exists( uint8_t adc, uint8_t channel );
uint8_t platform_adc_set_width( uint8_t adc, int bits ); uint8_t platform_adc_set_width( uint8_t adc, int bits );
uint8_t platform_adc_setup( uint8_t adc, uint8_t channel, uint8_t attn ); uint8_t platform_adc_setup( uint8_t adc, uint8_t channel, uint8_t attn );
int platform_adc_read( uint8_t adc, uint8_t channel ); int platform_adc_read( uint8_t adc, uint8_t channel );
int platform_adc_read_hall_sensor( );
enum { enum {
PLATFORM_ADC_ATTEN_0db = 0, PLATFORM_ADC_ATTEN_0db = 0,
PLATFORM_ADC_ATTEN_2_5db = 1, PLATFORM_ADC_ATTEN_2_5db = 1,
@ -220,9 +219,6 @@ int platform_ws2812_send( void );
uint32_t platform_flash_get_sector_of_address( uint32_t addr ); uint32_t platform_flash_get_sector_of_address( uint32_t addr );
uint32_t platform_flash_write( const void *from, uint32_t toaddr, uint32_t size ); uint32_t platform_flash_write( const void *from, uint32_t toaddr, uint32_t size );
uint32_t platform_flash_read( void *to, uint32_t fromaddr, uint32_t size ); uint32_t platform_flash_read( void *to, uint32_t fromaddr, uint32_t size );
uint32_t platform_s_flash_write( const void *from, uint32_t toaddr, uint32_t size );
uint32_t platform_s_flash_read( void *to, uint32_t fromaddr, uint32_t size );
uint32_t platform_flash_get_num_sectors(void);
int platform_flash_erase_sector( uint32_t sector_id ); int platform_flash_erase_sector( uint32_t sector_id );

View File

@ -63,6 +63,8 @@ sample code bearing this copyright.
#include "driver/rmt.h" #include "driver/rmt.h"
#include "driver/gpio.h" #include "driver/gpio.h"
#include "rom/gpio.h" // for gpio_matrix_out()
#include "soc/gpio_periph.h"
#include "esp_log.h" #include "esp_log.h"
#define TRUE (1==1) #define TRUE (1==1)
@ -108,7 +110,7 @@ static const uint8_t owDefaultPower = 0;
static int onewire_rmt_init( uint8_t gpio_num ) static int onewire_rmt_init( uint8_t gpio_num )
{ {
if(!GPIO_IS_VALID_GPIO(gpio_num)) { if(!platform_gpio_exists(gpio_num)) {
return PLATFORM_ERR; return PLATFORM_ERR;
} }
@ -187,7 +189,7 @@ static void onewire_flush_rmt_rx_buf( void )
// check rmt TX&RX channel assignment and eventually attach them to the requested pin // check rmt TX&RX channel assignment and eventually attach them to the requested pin
static int onewire_rmt_attach_pin( uint8_t gpio_num ) static int onewire_rmt_attach_pin( uint8_t gpio_num )
{ {
if(!GPIO_IS_VALID_GPIO(gpio_num)) { if(!platform_gpio_exists(gpio_num)) {
return PLATFORM_ERR; return PLATFORM_ERR;
} }

View File

@ -5,7 +5,9 @@
#include "soc/uart_reg.h" #include "soc/uart_reg.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <freertos/semphr.h> #include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "lua.h" #include "lua.h"
#include "rom/uart.h" #include "rom/uart.h"
#include "esp_log.h" #include "esp_log.h"
@ -22,8 +24,24 @@ int platform_init (void)
// ***************************************************************************** // *****************************************************************************
// GPIO subsection // GPIO subsection
int platform_gpio_exists( unsigned gpio ) { return GPIO_IS_VALID_GPIO(gpio); } int platform_gpio_exists(unsigned gpio)
int platform_gpio_output_exists( unsigned gpio ) { return GPIO_IS_VALID_OUTPUT_GPIO(gpio); } {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtype-limits"
// Suppress ">= is always true" due to unsigned type here
return GPIO_IS_VALID_GPIO(gpio);
#pragma GCC diagnostic pop
}
int platform_gpio_output_exists(unsigned gpio)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtype-limits"
// Suppress ">= is always true" due to unsigned type here
return GPIO_IS_VALID_OUTPUT_GPIO(gpio);
#pragma GCC diagnostic pop
}
// **************************************************************************** // ****************************************************************************
@ -136,7 +154,7 @@ static void task_uart( void *pvParameters ){
uart_event_t event; uart_event_t event;
for(;;) { for(;;) {
if(xQueueReceive(uart_status[id].queue, (void * )&event, (portTickType)portMAX_DELAY)) { if(xQueueReceive(uart_status[id].queue, (void * )&event, (TickType_t)portMAX_DELAY)) {
switch(event.type) { switch(event.type) {
case UART_DATA: { case UART_DATA: {
// Attempt to coalesce received bytes to reduce risk of overrunning // Attempt to coalesce received bytes to reduce risk of overrunning
@ -222,6 +240,7 @@ uint32_t platform_uart_setup( unsigned id, uint32_t baud, int databits, int pari
.baud_rate = baud, .baud_rate = baud,
.flow_ctrl = flow_control, .flow_ctrl = flow_control,
.rx_flow_ctrl_thresh = UART_FIFO_LEN - 16, .rx_flow_ctrl_thresh = UART_FIFO_LEN - 16,
.source_clk = UART_SCLK_DEFAULT,
}; };
switch (databits) switch (databits)
@ -523,14 +542,6 @@ int platform_adc_read( uint8_t adc, uint8_t channel ) {
return value; return value;
} }
int platform_adc_read_hall_sensor( ) {
#if defined(CONFIG_IDF_TARGET_ESP32)
int value = hall_sensor_read( );
return value;
#else
return -1;
#endif
}
// ***************************************************************************** // *****************************************************************************
// I2C platform interface // I2C platform interface

View File

@ -1,5 +1,6 @@
#include "platform.h" #include "platform.h"
#include "flash_api.h" #include "platform_wdt.h"
#include "esp_flash.h"
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -21,183 +22,43 @@ static uint32_t flashh_find_sector( uint32_t address, uint32_t *pstart, uint32_t
return sect_id; return sect_id;
} }
uint32_t platform_flash_get_sector_of_address( uint32_t addr ) uint32_t platform_flash_get_sector_of_address( uint32_t addr )
{ {
return flashh_find_sector( addr, NULL, NULL ); return flashh_find_sector( addr, NULL, NULL );
} }
uint32_t platform_flash_get_num_sectors(void)
{
return flash_safe_get_sec_num ();
}
uint32_t platform_flash_write( const void *from, uint32_t toaddr, uint32_t size ) uint32_t platform_flash_write( const void *from, uint32_t toaddr, uint32_t size )
{ {
#ifndef INTERNAL_FLASH_WRITE_UNIT_SIZE esp_err_t err = esp_flash_write(NULL, from, toaddr, size);
return platform_s_flash_write( from, toaddr, size ); if (err != ESP_OK)
#else // #ifindef INTERNAL_FLASH_WRITE_UNIT_SIZE return 0;
uint32_t temp, rest, ssize = size; else
unsigned i; return size;
char tmpdata[ INTERNAL_FLASH_WRITE_UNIT_SIZE ];
const uint8_t *pfrom = ( const uint8_t* )from;
const uint32_t blksize = INTERNAL_FLASH_WRITE_UNIT_SIZE;
const uint32_t blkmask = INTERNAL_FLASH_WRITE_UNIT_SIZE - 1;
// Align the start
if( toaddr & blkmask )
{
rest = toaddr & blkmask;
temp = toaddr & ~blkmask; // this is the actual aligned address
// memcpy( tmpdata, ( const void* )temp, blksize );
platform_s_flash_read( tmpdata, temp, blksize );
for( i = rest; size && ( i < blksize ); i ++, size --, pfrom ++ )
tmpdata[ i ] = *pfrom;
platform_s_flash_write( tmpdata, temp, blksize );
if( size == 0 )
return ssize;
toaddr = temp + blksize;
}
// The start address is now a multiple of blksize
// Compute how many bytes we can write as multiples of blksize
rest = size & blkmask;
temp = size & ~blkmask;
// Program the blocks now
if( temp )
{
platform_s_flash_write( pfrom, toaddr, temp );
toaddr += temp;
pfrom += temp;
}
// And the final part of a block if needed
if( rest )
{
// memcpy( tmpdata, ( const void* )toaddr, blksize );
platform_s_flash_read( tmpdata, toaddr, blksize );
for( i = 0; size && ( i < rest ); i ++, size --, pfrom ++ )
tmpdata[ i ] = *pfrom;
platform_s_flash_write( tmpdata, toaddr, blksize );
}
return ssize;
#endif // #ifndef INTERNAL_FLASH_WRITE_UNIT_SIZE
} }
uint32_t platform_flash_read( void *to, uint32_t fromaddr, uint32_t size ) uint32_t platform_flash_read( void *to, uint32_t fromaddr, uint32_t size )
{ {
#ifndef INTERNAL_FLASH_READ_UNIT_SIZE esp_err_t err = esp_flash_write(NULL, to, fromaddr, size);
return platform_s_flash_read( to, fromaddr, size ); if (err != ESP_OK)
#else // #ifindef INTERNAL_FLASH_READ_UNIT_SIZE
uint32_t temp, rest, ssize = size;
unsigned i;
char tmpdata[ INTERNAL_FLASH_READ_UNIT_SIZE ] __attribute__ ((aligned(INTERNAL_FLASH_READ_UNIT_SIZE)));
uint8_t *pto = ( uint8_t* )to;
const uint32_t blksize = INTERNAL_FLASH_READ_UNIT_SIZE;
const uint32_t blkmask = INTERNAL_FLASH_READ_UNIT_SIZE - 1;
// Align the start
if( fromaddr & blkmask )
{
rest = fromaddr & blkmask;
temp = fromaddr & ~blkmask; // this is the actual aligned address
platform_s_flash_read( tmpdata, temp, blksize );
for( i = rest; size && ( i < blksize ); i ++, size --, pto ++ )
*pto = tmpdata[ i ];
if( size == 0 )
return ssize;
fromaddr = temp + blksize;
}
// The start address is now a multiple of blksize
// Compute how many bytes we can read as multiples of blksize
rest = size & blkmask;
temp = size & ~blkmask;
// Program the blocks now
if( temp )
{
platform_s_flash_read( pto, fromaddr, temp );
fromaddr += temp;
pto += temp;
}
// And the final part of a block if needed
if( rest )
{
platform_s_flash_read( tmpdata, fromaddr, blksize );
for( i = 0; size && ( i < rest ); i ++, size --, pto ++ )
*pto = tmpdata[ i ];
}
return ssize;
#endif // #ifndef INTERNAL_FLASH_READ_UNIT_SIZE
}
/*
* Assumptions:
* > toaddr is INTERNAL_FLASH_WRITE_UNIT_SIZE aligned
* > size is a multiple of INTERNAL_FLASH_WRITE_UNIT_SIZE
*/
uint32_t platform_s_flash_write( const void *from, uint32_t toaddr, uint32_t size )
{
esp_err_t r;
const uint32_t blkmask = INTERNAL_FLASH_WRITE_UNIT_SIZE - 1;
uint32_t *apbuf = NULL;
uint32_t fromaddr = (uint32_t)from;
if( (fromaddr & blkmask ) || is_cache_flash_addr(fromaddr)) {
apbuf = (uint32_t *)malloc(size);
if(!apbuf)
return 0; return 0;
memcpy(apbuf, from, size);
}
r = flash_write(toaddr, apbuf?(uint32_t *)apbuf:(uint32_t *)from, size);
if(apbuf)
free(apbuf);
if(ESP_OK == r)
return size;
else{
NODE_ERR( "ERROR in flash_write: r=%d at %08X\n", ( int )r, ( unsigned )toaddr);
return 0;
}
}
/*
* Assumptions:
* > fromaddr is INTERNAL_FLASH_READ_UNIT_SIZE aligned
* > size is a multiple of INTERNAL_FLASH_READ_UNIT_SIZE
*/
uint32_t platform_s_flash_read( void *to, uint32_t fromaddr, uint32_t size )
{
if (size==0)
return 0;
esp_err_t r;
const uint32_t blkmask = (INTERNAL_FLASH_READ_UNIT_SIZE - 1);
if( ((uint32_t)to) & blkmask )
{
uint32_t size2=size-INTERNAL_FLASH_READ_UNIT_SIZE;
uint32_t* to2=(uint32_t*)((((uint32_t)to)&(~blkmask))+INTERNAL_FLASH_READ_UNIT_SIZE);
r = flash_read(fromaddr, to2, size2);
if(ESP_OK == r)
{
memmove(to,to2,size2);
char back[ INTERNAL_FLASH_READ_UNIT_SIZE ] __attribute__ ((aligned(INTERNAL_FLASH_READ_UNIT_SIZE)));
r=flash_read(fromaddr+size2,(uint32_t*)back,INTERNAL_FLASH_READ_UNIT_SIZE);
memcpy((uint8_t*)to+size2,back,INTERNAL_FLASH_READ_UNIT_SIZE);
}
}
else else
r = flash_read(fromaddr, (uint32_t *)to, size);
if(ESP_OK == r)
return size; return size;
else{
NODE_ERR( "ERROR in flash_read: r=%d at %08X sz %x\n", ( int )r, ( unsigned )fromaddr, size);
return 0;
}
} }
int platform_flash_erase_sector( uint32_t sector_id ) int platform_flash_erase_sector( uint32_t sector_id )
{ {
return flash_erase( sector_id ) == ESP_OK ? PLATFORM_OK : PLATFORM_ERR; platform_wdt_feed();
uint32_t addr = sector_id * INTERNAL_FLASH_SECTOR_SIZE;
esp_err_t err =
esp_flash_erase_region(NULL, addr, INTERNAL_FLASH_SECTOR_SIZE);
if (err == ESP_OK)
return PLATFORM_OK;
else
return PLATFORM_ERR;
} }

View File

@ -36,7 +36,7 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include "esp_flash_partitions.h" #include "esp_flash_partitions.h"
#include "esp_spi_flash.h" #include "esp_flash.h"
static inline bool possible_idx (uint8_t idx) static inline bool possible_idx (uint8_t idx)
{ {
@ -50,8 +50,12 @@ bool platform_partition_info (uint8_t idx, platform_partition_t *info)
return false; return false;
esp_partition_info_t pi; esp_partition_info_t pi;
esp_err_t err = spi_flash_read ( esp_err_t err = esp_flash_read(
ESP_PARTITION_TABLE_OFFSET + idx * sizeof(pi), (uint32_t *)&pi, sizeof (pi)); NULL,
(uint32_t *)&pi,
ESP_PARTITION_TABLE_OFFSET + idx * sizeof(pi),
sizeof (pi));
if (err != ESP_OK) if (err != ESP_OK)
return false; return false;
@ -66,42 +70,3 @@ bool platform_partition_info (uint8_t idx, platform_partition_t *info)
return true; return true;
} }
bool platform_partition_add (const platform_partition_t *info)
{
esp_partition_info_t *part_table =
(esp_partition_info_t *)malloc(SPI_FLASH_SEC_SIZE);
if (!part_table)
return false;
esp_err_t err = spi_flash_read (
ESP_PARTITION_TABLE_OFFSET, (uint32_t *)part_table, SPI_FLASH_SEC_SIZE);
if (err != ESP_OK)
goto out;
uint8_t idx = 0;
for (; possible_idx (idx); ++idx)
if (part_table[idx].magic != ESP_PARTITION_MAGIC)
break;
if (possible_idx (idx))
{
esp_partition_info_t *slot = &part_table[idx];
slot->magic = ESP_PARTITION_MAGIC;
slot->type = info->type;
slot->subtype = info->subtype;
slot->pos.offset = info->offs;
slot->pos.size = info->size;
memcpy (slot->label, info->label, sizeof (slot->label));
slot->flags = 0;
err = spi_flash_erase_sector (
ESP_PARTITION_TABLE_OFFSET / SPI_FLASH_SEC_SIZE);
if (err == ESP_OK)
err = spi_flash_write (
ESP_PARTITION_TABLE_OFFSET, (uint32_t *)part_table, SPI_FLASH_SEC_SIZE);
}
out:
free (part_table);
return err == ESP_OK;
}

View File

@ -1,4 +1,3 @@
#include <string.h> #include <string.h>
#include "platform_rmt.h" #include "platform_rmt.h"

View File

@ -3,7 +3,7 @@
#include "driver/gpio.h" #include "driver/gpio.h"
#include "driver/i2c.h" #include "driver/i2c.h"
#include "driver/spi_master.h" #include "driver/spi_master.h"
#include "rom/ets_sys.h" #include "esp_rom_sys.h"
#include "esp_heap_caps.h" #include "esp_heap_caps.h"
#include <string.h> #include <string.h>
@ -38,27 +38,27 @@ uint8_t u8x8_gpio_and_delay_nodemcu(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int,
break; break;
case U8X8_MSG_DELAY_NANO: // delay arg_int * 1 nano second case U8X8_MSG_DELAY_NANO: // delay arg_int * 1 nano second
ets_delay_us( 1 ); esp_rom_delay_us( 1 );
break; break;
case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds case U8X8_MSG_DELAY_100NANO: // delay arg_int * 100 nano seconds
temp = arg_int * 100; temp = arg_int * 100;
temp /= 1000; temp /= 1000;
ets_delay_us( temp > 0 ? temp : 1 ); esp_rom_delay_us( temp > 0 ? temp : 1 );
break; break;
case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro seconds case U8X8_MSG_DELAY_10MICRO: // delay arg_int * 10 micro seconds
ets_delay_us( arg_int * 10 ); esp_rom_delay_us( arg_int * 10 );
break; break;
case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli second case U8X8_MSG_DELAY_MILLI: // delay arg_int * 1 milli second
ets_delay_us( arg_int * 1000 ); esp_rom_delay_us( arg_int * 1000 );
break; break;
case U8X8_MSG_DELAY_I2C: // arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHz case U8X8_MSG_DELAY_I2C: // arg_int is the I2C speed in 100KHz, e.g. 4 = 400 KHz
temp = 5000 / arg_int; // arg_int=1: delay by 5us, arg_int = 4: delay by 1.25us temp = 5000 / arg_int; // arg_int=1: delay by 5us, arg_int = 4: delay by 1.25us
temp /= 1000; temp /= 1000;
ets_delay_us( temp > 0 ? temp : 1 ); esp_rom_delay_us( temp > 0 ? temp : 1 );
break; break;
case U8X8_MSG_GPIO_D0: // D0 or SPI clock pin: Output level in arg_int case U8X8_MSG_GPIO_D0: // D0 or SPI clock pin: Output level in arg_int

View File

@ -87,7 +87,7 @@ int16_t ucg_com_nodemcu_hw_spi(ucg_t *ucg, int16_t msg, uint16_t arg, uint8_t *d
break; break;
case UCG_COM_MSG_DELAY: case UCG_COM_MSG_DELAY:
ets_delay_us(arg); esp_rom_delay_us(arg);
break; break;
case UCG_COM_MSG_CHANGE_RESET_LINE: case UCG_COM_MSG_CHANGE_RESET_LINE:

View File

@ -7,23 +7,20 @@
#include "platform.h" #include "platform.h"
#include "esp_task_wdt.h" #include "esp_task_wdt.h"
#ifdef CONFIG_TASK_WDT
static uint32_t task_wdt_timeout = CONFIG_TASK_WDT_TIMEOUT_S;
static bool task_wdt_panic =
#ifdef CONFIG_TASK_WDT_PANIC
true;
#else
false;
#endif
#endif
int platform_wdt_feed( void ) int platform_wdt_feed( void )
{ {
#ifdef CONFIG_TASK_WDT #ifdef CONFIG_TASK_WDT
return esp_task_wdt_init(task_wdt_timeout, task_wdt_panic) == ESP_OK ? PLATFORM_OK : PLATFORM_ERR; esp_task_wdt_config_t cfg = {
.timeout_ms = CONFIG_TASK_WDT_TIMEOUT_S * 1000,
.idle_core_mask = (uint32_t)-1,
.trigger_panic =
#ifdef CONFIG_TASK_WDT_PANIC
true,
#else
false,
#endif
};
return esp_task_wdt_init(&cfg) == ESP_OK ? PLATFORM_OK : PLATFORM_ERR;
#else #else
return PLATFORM_OK; return PLATFORM_OK;
#endif #endif

View File

@ -30,6 +30,8 @@
#include "driver/gpio.h" #include "driver/gpio.h"
#include "esp_log.h" #include "esp_log.h"
#include "soc/periph_defs.h" #include "soc/periph_defs.h"
#include "rom/gpio.h" // for gpio_matrix_out()
#include "soc/gpio_periph.h"
#undef WS2812_DEBUG #undef WS2812_DEBUG

View File

@ -13,7 +13,7 @@ extern struct {
void rtos_dbg_task_print (const char *file, uint32_t line) void rtos_dbg_task_print (const char *file, uint32_t line)
{ {
printf(">>dbg: %s:%d in RTOS task \"%s\": prio %d\n", printf(">>dbg: %s:%lu in RTOS task \"%s\": prio %u\n",
file, file,
line, line,
pxCurrentTCB->pcTaskName, pxCurrentTCB->pcTaskName,
@ -30,6 +30,6 @@ void rtos_dbg_stack_print (const char *file, uint32_t line)
for (;p < pxCurrentTCB->pxTopOfStack && *p == fill; ++p) for (;p < pxCurrentTCB->pxTopOfStack && *p == fill; ++p)
++nwords; ++nwords;
printf(">>dbg: %s:%d in RTOS task \"%s\": %u stack untouched\n", printf(">>dbg: %s:%lu in RTOS task \"%s\": %lu stack untouched\n",
file, line, pxCurrentTCB->pcTaskName, nwords * 4); file, line, pxCurrentTCB->pcTaskName, nwords * 4);
} }

View File

@ -29,11 +29,11 @@ typedef struct
/* /*
* Private arrays to hold the 3 event task queues and the dispatch callbacks * Private arrays to hold the 3 event task queues and the dispatch callbacks
*/ */
static xQueueHandle task_Q[TASK_PRIORITY_COUNT]; static QueueHandle_t task_Q[TASK_PRIORITY_COUNT];
/* Rather than using a QueueSet (which requires queues to be empty when created) /* Rather than using a QueueSet (which requires queues to be empty when created)
* we use a binary semaphore to unblock the pump whenever something is posted */ * we use a binary semaphore to unblock the pump whenever something is posted */
static xSemaphoreHandle pending; static SemaphoreHandle_t pending;
static task_callback_t *task_func; static task_callback_t *task_func;
static int task_count; static int task_count;

View File

@ -60,22 +60,3 @@ the sampled value (number)
```lua ```lua
val = adc.read(adc.ADC1, 0) val = adc.read(adc.ADC1, 0)
``` ```
## adc.read_hall_sensor()
Read Hall sensor (GPIO36, GPIO39). We recommend using 12-bits width on ADC1.
#### Syntax
`adc.read_hall_sensor()`
#### Parameters
none
#### Returns
the sampled value (number)
#### Example
```lua
val = adc.read_hall_sensor()
```

View File

@ -8,8 +8,10 @@ The eth module provides access to the ethernet PHY chip configuration.
Your board must contain one of the PHY chips that are supported by ESP-IDF: Your board must contain one of the PHY chips that are supported by ESP-IDF:
- IP101 - IP101
- LAN8720 - RTL8201
- TLK110 - LAN8720 (possibly others in the LAN87xx family)
- DP83848
- KSZ8001 / KSZ8021 / KSZ8031 / KSZ8041 / KSZ8051 / KSZ8061 / KSZ8081 / KSZ8091
## eth.get_mac() ## eth.get_mac()
Get MAC address. Get MAC address.
@ -67,9 +69,11 @@ eth.init(cfg)
- `phy` PHY chip model, one of - `phy` PHY chip model, one of
- `PHY_DP83848` - `PHY_DP83848`
- `PHY_IP101` - `PHY_IP101`
- `PHY_KSZ8041` - `PHY_KSZ80XX`
- `PHY_KSZ8081` - `PHY_KSZ8041` (deprecated, use `PHY_KSZ80XX` instead)
- `PHY_LAN8720` - `PHY_KSZ8081` (deprecated, use `PHY_KSZ80XX` instead)
- `PHY_LAN87XX`
- `PHY_LAN8720` (deprecated, use `PHY_LAN87XX` instead)
- `PHY_RTL8201` - `PHY_RTL8201`
- `power` power enable pin, optional - `power` power enable pin, optional

View File

@ -86,7 +86,7 @@ Closes connection to the broker.
none none
#### Returns #### Returns
`true` on success, `false` otherwise `nil`
## mqtt.client:connect() ## mqtt.client:connect()
@ -139,22 +139,6 @@ This is the description of how the `autoreconnect` functionality may (or may not
> is considered a success if the client connects to a server and gets back a good response packet in response to its MQTT connection request. > is considered a success if the client connects to a server and gets back a good response packet in response to its MQTT connection request.
> This implies (for example) that the username and password are correct. > This implies (for example) that the username and password are correct.
#### Connection failure callback reason codes:
| Constant | Value | Description |
|----------|-------|-------------|
|`mqtt.CONN_FAIL_SERVER_NOT_FOUND`|-5|There is no broker listening at the specified IP Address and Port|
|`mqtt.CONN_FAIL_NOT_A_CONNACK_MSG`|-4|The response from the broker was not a CONNACK as required by the protocol|
|`mqtt.CONN_FAIL_DNS`|-3|DNS Lookup failed|
|`mqtt.CONN_FAIL_TIMEOUT_RECEIVING`|-2|Timeout waiting for a CONNACK from the broker|
|`mqtt.CONN_FAIL_TIMEOUT_SENDING`|-1|Timeout trying to send the Connect message|
|`mqtt.CONNACK_ACCEPTED`|0|No errors. _Note: This will not trigger a failure callback._|
|`mqtt.CONNACK_REFUSED_PROTOCOL_VER`|1|The broker is not a 3.1.1 MQTT broker.|
|`mqtt.CONNACK_REFUSED_ID_REJECTED`|2|The specified ClientID was rejected by the broker. (See `mqtt.Client()`)|
|`mqtt.CONNACK_REFUSED_SERVER_UNAVAILABLE`|3|The server is unavailable.|
|`mqtt.CONNACK_REFUSED_BAD_USER_OR_PASS`|4|The broker refused the specified username or password.|
|`mqtt.CONNACK_REFUSED_NOT_AUTHORIZED`|5|The username is not authorized.|
## mqtt.client:lwt() ## mqtt.client:lwt()
Setup [Last Will and Testament](http://www.hivemq.com/blog/mqtt-essentials-part-9-last-will-and-testament) (optional). A broker will publish a message with qos = 0, retain = 0, data = "offline" to topic "/lwt" if client does not send keepalive packet. Setup [Last Will and Testament](http://www.hivemq.com/blog/mqtt-essentials-part-9-last-will-and-testament) (optional). A broker will publish a message with qos = 0, retain = 0, data = "offline" to topic "/lwt" if client does not send keepalive packet.

View File

@ -77,7 +77,7 @@ Initialize the SDMMC and probe the attached SD card.
- `sdmmc.W8BIT`, not supported yet - `sdmmc.W8BIT`, not supported yet
#### Parameters SD SPI Mode #### Parameters SD SPI Mode
- `slot` SD SPI slot, one of `sdmmc.HSPI` or `sdmmc.VSPI` - `slot` SD SPI slot, one of `sdmmc.SPI2` or `sdmmc.SPI3` (on ESP32 the names`sdmmc.HSPI` or `sdmmc.VSPI` are still available, but deprecated)
- `cfg` mandatory table containing slot configuration: - `cfg` mandatory table containing slot configuration:
- `sck_pin` SPI SCK pin, mandatory - `sck_pin` SPI SCK pin, mandatory
- `mosi_pin`, SPI MOSI pin, mandatory - `mosi_pin`, SPI MOSI pin, mandatory
@ -128,12 +128,10 @@ Table containing the card's OCR, CID, CSD, SCR, and RCA with elements:
Mount filesystem on SD card. Mount filesystem on SD card.
#### Syntax #### Syntax
`card:mount(ldrv[, partition])` `card:mount(ldrv)`
#### Parameters #### Parameters
- `ldrv` name of logical drive, "/SD0", "/SD1", etc. - `ldrv` name of logical drive, "/SD0", "/SD1", etc.
- `partition` the partition number, default is 0, meaning the first available
FAT partition.
#### Returns #### Returns
`true` if successful, `false` otherwise `true` if successful, `false` otherwise

View File

@ -6,6 +6,8 @@
# SPI Bus # SPI Bus
The ESP32 contains 4 SPI bus hosts called `SPI`, `SPI1`, `HSPI`, and `VSPI`. `SPI` is locked to flash communication and is not available for the application. `SPI1` is currently also tied to flash support, but might be available in the future. Applications can currently only use the `HSPI` and `VSPI` hosts. The ESP32 contains 4 SPI bus hosts called `SPI`, `SPI1`, `HSPI`, and `VSPI`. `SPI` is locked to flash communication and is not available for the application. `SPI1` is currently also tied to flash support, but might be available in the future. Applications can currently only use the `HSPI` and `VSPI` hosts.
On later models in the ESP32 series, the SPI hosts are simply referred to by number, and are available here as `spi.SPI1`, `spi.SPI2` and (if present) `spi.SPI3`.
The host signals can be mapped to any suitable GPIO pins. The host signals can be mapped to any suitable GPIO pins.
!!! note !!! note

View File

@ -102,7 +102,7 @@ uart.setup(0, 9600, 8, uart.PARITY_NONE, uart.STOPBITS_1, 1)
``` ```
```lua ```lua
uart.setup(2, 115200, 8, uart.PARITY_NONE, uart.STOPBITS_1, {tx = 16, rx = 17}) uart.setup(2, 115200, 8, uart.PARITY_NONE, uart.STOPBITS_1, {tx = 17, rx = 16})
``` ```
## uart.getconfig() ## uart.getconfig()

View File

@ -1,2 +1 @@
ziglang>=0.8.0 ziglang>=0.8.0
cryptography>=2.1.4,<35.0.0

@ -1 +1 @@
Subproject commit 6407ecb3f8d2cc07c4c230e7e64f2046af5c86f7 Subproject commit 5181de8ac5ec5e18f04f634da8ce173b7ef5ab73