From 7abda5c9e07d49a0648619384975bc93e052712a Mon Sep 17 00:00:00 2001 From: Johny Mattsson Date: Fri, 30 Dec 2016 19:20:01 +1100 Subject: [PATCH] Updated to latest IDF. - Switched hardcoded interrupts to new IDF interrupt allocation framework. - gpio module switched to the IDF's per-pin interrupt callback service. - Improved NodeMCU linker script since it broke with the IDF upgrade. - Various compatibility updates. --- components/base_nodemcu/ld/nodemcu_core.ld | 5 +-- components/driver_console/console.c | 13 +++--- components/modules/gpio.c | 49 +++++++--------------- components/modules/wifi_ap.c | 4 +- components/modules/wifi_sta.c | 12 +++--- components/platform/platform_partition.c | 2 +- sdk/esp32-esp-idf | 2 +- 7 files changed, 33 insertions(+), 54 deletions(-) diff --git a/components/base_nodemcu/ld/nodemcu_core.ld b/components/base_nodemcu/ld/nodemcu_core.ld index 2a5ed197..303d44b6 100644 --- a/components/base_nodemcu/ld/nodemcu_core.ld +++ b/components/base_nodemcu/ld/nodemcu_core.ld @@ -1,9 +1,8 @@ SECTIONS { - .lua_lib : ALIGN(4) + .flash.rodata : ALIGN(4) { /* Link-time arrays containing the defs for the included modules */ lua_libs = ABSOLUTE(.); - /* Allow either empty define or defined-to-1 to include the module */ KEEP(*(.lua_libs)) LONG(0) LONG(0) /* Null-terminate the array */ lua_rotable = ABSOLUTE(.); @@ -14,4 +13,4 @@ SECTIONS { LONG(0) LONG(0) /* Null-terminate the array */ } } -INSERT AFTER .flash.text +INSERT BEFORE .flash.text diff --git a/components/driver_console/console.c b/components/driver_console/console.c index 013f705e..3a5202b7 100644 --- a/components/driver_console/console.c +++ b/components/driver_console/console.c @@ -33,6 +33,7 @@ #include "driver/console.h" #include "esp_intr.h" +#include "esp_intr_alloc.h" #include "soc/soc.h" #include "soc/uart_reg.h" #include "soc/dport_reg.h" @@ -44,7 +45,6 @@ #include "sys/reent.h" #define UART_INPUT_QUEUE_SZ 0x100 -#define MAP_CONSOLE_UART_PRO_INT_NO 9 // PRO interrupt used by this driver for uart0 // These used to be available in soc/uart_register.h: #define UART_GET_RXFIFO_RD_BYTE(i) GET_PERI_REG_BITS2(UART_FIFO_REG(i) , UART_RXFIFO_RD_BYTE_V, UART_RXFIFO_RD_BYTE_S) @@ -66,7 +66,7 @@ static _read_r_fn _read_r_pro, _read_r_app; static xQueueHandle uart0Q; static task_handle_t input_task = 0; - +static intr_handle_t intr_handle; // --- Syscall support for reading from STDIN_FILENO --------------- @@ -159,9 +159,9 @@ void console_init (const ConsoleSetup_t *cfg, task_handle_t tsk) console_setup (cfg); - ESP_INTR_DISABLE(MAP_CONSOLE_UART_PRO_INT_NO); - - xt_set_interrupt_handler (MAP_CONSOLE_UART_PRO_INT_NO, uart0_rx_intr_handler, NULL); + esp_intr_alloc (ETS_UART0_INTR_SOURCE, + ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_INTRDISABLED, + uart0_rx_intr_handler, NULL, &intr_handle); UART_SET_RX_TOUT_EN(CONSOLE_UART, true); UART_SET_RX_TOUT_THRHD(CONSOLE_UART, 2); @@ -172,8 +172,7 @@ void console_init (const ConsoleSetup_t *cfg, task_handle_t tsk) UART_RXFIFO_FULL_INT_ENA | UART_FRM_ERR_INT_ENA); - WRITE_PERI_REG(DPORT_PRO_UART_INTR_MAP_REG, MAP_CONSOLE_UART_PRO_INT_NO); - ESP_INTR_ENABLE(MAP_CONSOLE_UART_PRO_INT_NO); + esp_intr_enable (intr_handle); // Register our console_read_r_xxx functions to support stdin input _read_r_pro = syscall_table_ptr_pro->_read_r; diff --git a/components/modules/gpio.c b/components/modules/gpio.c index 55c32bf6..d04f26ea 100644 --- a/components/modules/gpio.c +++ b/components/modules/gpio.c @@ -42,9 +42,6 @@ #define PULL_UP 1 #define PULL_DOWN 2 -// TODO: change this to dynamically allocated once that framework is available! -#define MAP_GPIO_PRO_INT_NO 12 - static int *gpio_cb_refs = NULL; // Lazy init static task_handle_t cb_task; @@ -53,11 +50,21 @@ static int check_err (lua_State *L, esp_err_t err) switch (err) { case ESP_ERR_INVALID_ARG: luaL_error (L, "invalid argument"); + case ESP_ERR_INVALID_STATE: luaL_error (L, "internal logic error"); case ESP_OK: break; } return 0; } +// TODO: can/should we attempt to guard against task q overflow? +_Static_assert(GPIO_PIN_COUNT<256, "task post encoding assumes < 256 gpios"); +static void IRAM_ATTR single_pin_isr (void *p) +{ + gpio_num_t gpio_num = (gpio_num_t)p; + gpio_intr_disable (gpio_num); + task_post_low (cb_task, (gpio_num) | (gpio_get_level (gpio_num) << 8)); +} + /* Lua: gpio.config({ * gpio= x || { x, y ... } @@ -159,10 +166,14 @@ static int lgpio_trig (lua_State *L) check_err (L, gpio_intr_disable (gpio)); if (gpio_cb_refs[gpio] == LUA_NOREF) + { check_err (L, gpio_set_intr_type (gpio, GPIO_INTR_DISABLE)); + check_err (L, gpio_isr_handler_remove (gpio)); + } else { check_err (L, gpio_set_intr_type (gpio, intr_type)); + check_err (L, gpio_isr_handler_add (gpio, single_pin_isr, (void *)gpio)); check_err (L, gpio_intr_enable (gpio)); } return 0; @@ -192,36 +203,6 @@ static int lgpio_write (lua_State *L) } -// TODO: move this to the platform layer so it can be shared -// TODO: can/should we attempt to guard against task q overflow? -_Static_assert(GPIO_PIN_COUNT<256, "task post encoding assumes < 256 gpios"); -static void IRAM_ATTR nodemcu_gpio_isr (void *p) -{ - uint32_t intrs = READ_PERI_REG(GPIO_STATUS_REG); - uint32_t intrs_rtc = READ_PERI_REG(GPIO_STATUS1_REG); - - #define handle_gpio_intr(gpio_num) do { \ - gpio_intr_disable (gpio_num); \ - task_post_low (cb_task, (gpio_num) | (gpio_get_level (gpio_num) << 8)); \ - } while (0) - - // Regular gpios - for (uint32_t gpio = 0; intrs && gpio < 32; ++gpio) - { - if (intrs & BIT(gpio)) - handle_gpio_intr (gpio); - } - // RTC gpios - for (uint32_t gpio = 0; intrs_rtc && gpio < (GPIO_PIN_COUNT - 32); ++gpio) - { - if (intrs_rtc & BIT(gpio)) - handle_gpio_intr (gpio + 32); - } - - SET_PERI_REG_MASK(GPIO_STATUS_W1TC_REG, intrs); - SET_PERI_REG_MASK(GPIO_STATUS1_W1TC_REG, intrs_rtc); -} - static void nodemcu_gpio_callback_task (task_param_t param, task_prio_t prio) { @@ -245,7 +226,7 @@ static int nodemcu_gpio_init (lua_State *L) { cb_task = task_get_id (nodemcu_gpio_callback_task); check_err (L, - gpio_isr_register (MAP_GPIO_PRO_INT_NO, nodemcu_gpio_isr, NULL)); + gpio_install_isr_service (ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM)); return 0; } diff --git a/components/modules/wifi_ap.c b/components/modules/wifi_ap.c index 3a9ca31d..cb1a4495 100644 --- a/components/modules/wifi_ap.c +++ b/components/modules/wifi_ap.c @@ -134,14 +134,14 @@ static int wifi_ap_config (lua_State *L) const char *str = luaL_checklstring (L, -1, &len); if (len > sizeof (cfg.ap.ssid)) len = sizeof (cfg.ap.ssid); - strncpy (cfg.ap.ssid, str, len); + strncpy ((char *)cfg.ap.ssid, str, len); cfg.ap.ssid_len = len; lua_getfield (L, 1, "pwd"); str = luaL_optlstring (L, -1, "", &len); if (len > sizeof (cfg.ap.password)) len = sizeof (cfg.ap.password); - strncpy (cfg.ap.password, str, len); + strncpy ((char *)cfg.ap.password, str, len); lua_getfield (L, 1, "auth"); int authmode = luaL_optint (L, -1, WIFI_AUTH_WPA2_PSK); diff --git a/components/modules/wifi_sta.c b/components/modules/wifi_sta.c index cee81cdb..f7dce694 100644 --- a/components/modules/wifi_sta.c +++ b/components/modules/wifi_sta.c @@ -176,13 +176,13 @@ static int wifi_sta_config (lua_State *L) const char *str = luaL_checklstring (L, -1, &len); if (len > sizeof (cfg.sta.ssid)) len = sizeof (cfg.sta.ssid); - strncpy (cfg.sta.ssid, str, len); + strncpy ((char *)cfg.sta.ssid, str, len); lua_getfield (L, 1, "pwd"); str = luaL_optlstring (L, -1, "", &len); if (len > sizeof (cfg.sta.password)) len = sizeof (cfg.sta.password); - strncpy (cfg.sta.password, str, len); + strncpy ((char *)cfg.sta.password, str, len); lua_getfield (L, 1, "bssid"); cfg.sta.bssid_set = false; @@ -251,12 +251,12 @@ static int wifi_sta_getconfig (lua_State *L) return luaL_error (L, "failed to get config, code %d", err); lua_createtable (L, 0, 3); - size_t ssid_len = strnlen (cfg.sta.ssid, sizeof (cfg.sta.ssid)); - lua_pushlstring (L, cfg.sta.ssid, ssid_len); + size_t ssid_len = strnlen ((char *)cfg.sta.ssid, sizeof (cfg.sta.ssid)); + lua_pushlstring (L, (char *)cfg.sta.ssid, ssid_len); lua_setfield (L, -2, "ssid"); - size_t pwd_len = strnlen (cfg.sta.password, sizeof (cfg.sta.password)); - lua_pushlstring (L, cfg.sta.password, pwd_len); + size_t pwd_len = strnlen ((char *)cfg.sta.password, sizeof (cfg.sta.password)); + lua_pushlstring (L, (char *)cfg.sta.password, pwd_len); lua_setfield (L, -2, "pwd"); if (cfg.sta.bssid_set) diff --git a/components/platform/platform_partition.c b/components/platform/platform_partition.c index e3445f0c..10fddd30 100644 --- a/components/platform/platform_partition.c +++ b/components/platform/platform_partition.c @@ -93,7 +93,7 @@ bool platform_partition_add (const platform_partition_t *info) slot->pos.offset = info->offs; slot->pos.size = info->size; memcpy (slot->label, info->label, sizeof (slot->label)); - memset (slot->reserved, 0xff, sizeof (slot->reserved)); + slot->flags = 0; err = spi_flash_erase_sector ( ESP_PARTITION_TABLE_ADDR / SPI_FLASH_SEC_SIZE); if (err == ESP_OK) diff --git a/sdk/esp32-esp-idf b/sdk/esp32-esp-idf index c8685c20..8bcd341f 160000 --- a/sdk/esp32-esp-idf +++ b/sdk/esp32-esp-idf @@ -1 +1 @@ -Subproject commit c8685c200276abda4418f1367743f650d2aeacb2 +Subproject commit 8bcd341fcae23c534ea98f47d1ba33e1694a9e48