From 01b94f31dc4cc2e0c57bd1f7e1ccb800778e0105 Mon Sep 17 00:00:00 2001 From: Joo Aun Saw Date: Mon, 29 Aug 2022 16:19:46 +1000 Subject: [PATCH 01/12] uart: feed rx data to both lua console and on-data callback --- components/modules/uart.c | 4 ++++ components/platform/platform.c | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/components/modules/uart.c b/components/modules/uart.c index 118be37d..298a6770 100644 --- a/components/modules/uart.c +++ b/components/modules/uart.c @@ -10,6 +10,10 @@ static lua_State *gL = NULL; +bool uart_has_on_data_cb(unsigned id){ + return uart_status[id].receive_rf != LUA_NOREF; +} + bool uart_on_data_cb(unsigned id, const char *buf, size_t len){ if(!buf || len==0) return false; diff --git a/components/platform/platform.c b/components/platform/platform.c index 59228b82..09fbe757 100644 --- a/components/platform/platform.c +++ b/components/platform/platform.c @@ -65,6 +65,7 @@ uart_status_t uart_status[NUM_UART]; task_handle_t uart_event_task_id = 0; SemaphoreHandle_t sem = NULL; +extern bool uart_has_on_data_cb(unsigned id); extern bool uart_on_data_cb(unsigned id, const char *buf, size_t len); extern bool uart_on_error_cb(unsigned id, const char *buf, size_t len); @@ -81,7 +82,7 @@ void uart_event_task( task_param_t param, task_prio_t prio ) { unsigned used = feed_lua_input(post->data + i, post->size - i); i += used; } - else { + if (uart_has_on_data_cb(id)) { char ch = post->data[i]; us->line_buffer[us->line_position] = ch; us->line_position++; From ca0a2b3f476fac98b72bd4b3a702885400d71974 Mon Sep 17 00:00:00 2001 From: Joo Aun Saw Date: Mon, 29 Aug 2022 16:36:35 +1000 Subject: [PATCH 02/12] platform: platform_uart_setup handles null ptr --- components/platform/platform.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/components/platform/platform.c b/components/platform/platform.c index 09fbe757..979984cf 100644 --- a/components/platform/platform.c +++ b/components/platform/platform.c @@ -209,9 +209,11 @@ static void task_uart( void *pvParameters ){ uint32_t platform_uart_setup( unsigned id, uint32_t baud, int databits, int parity, int stopbits, uart_pins_t* pins ) { int flow_control = UART_HW_FLOWCTRL_DISABLE; - if(pins->flow_control & PLATFORM_UART_FLOW_CTS) flow_control |= UART_HW_FLOWCTRL_CTS; - if(pins->flow_control & PLATFORM_UART_FLOW_RTS) flow_control |= UART_HW_FLOWCTRL_RTS; - + if (pins != NULL) { + if(pins->flow_control & PLATFORM_UART_FLOW_CTS) flow_control |= UART_HW_FLOWCTRL_CTS; + if(pins->flow_control & PLATFORM_UART_FLOW_RTS) flow_control |= UART_HW_FLOWCTRL_RTS; + } + uart_config_t cfg = { .baud_rate = baud, .flow_ctrl = flow_control, From ddc770a6f7ccf46928cac176f00754b32aa549be Mon Sep 17 00:00:00 2001 From: Joo Aun Saw Date: Mon, 29 Aug 2022 16:45:36 +1000 Subject: [PATCH 03/12] uart: do not use uninitialized uart pins config --- components/modules/uart.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/components/modules/uart.c b/components/modules/uart.c index 298a6770..fa3def34 100644 --- a/components/modules/uart.c +++ b/components/modules/uart.c @@ -135,7 +135,9 @@ static int uart_setup( lua_State* L ) unsigned id, databits, parity, stopbits, echo = 1; uint32_t baud, res; uart_pins_t pins; + uart_pins_t* pins_to_use = NULL; + memset(&pins, 0, sizeof(pins)); id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( uart, id ); baud = luaL_checkinteger( L, 2 ); @@ -169,9 +171,11 @@ static int uart_setup( lua_State* L ) lua_getfield (L, 6, "flow_control"); pins.flow_control = luaL_optint(L, -1, PLATFORM_UART_FLOW_NONE); + + pins_to_use = &pins; } - res = platform_uart_setup( id, baud, databits, parity, stopbits, &pins ); + res = platform_uart_setup( id, baud, databits, parity, stopbits, pins_to_use ); lua_pushinteger( L, res ); return 1; } From 5e6cb4d1fa344f4deb77c9a5bc3753353a8a99e5 Mon Sep 17 00:00:00 2001 From: Joo Aun Saw Date: Mon, 29 Aug 2022 17:35:38 +1000 Subject: [PATCH 04/12] uart: slight clean up of uart.setup --- components/modules/uart.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/components/modules/uart.c b/components/modules/uart.c index fa3def34..e5c4fc22 100644 --- a/components/modules/uart.c +++ b/components/modules/uart.c @@ -132,7 +132,7 @@ static int uart_on( lua_State* L ) // Lua: actualbaud = setup( id, baud, databits, parity, stopbits, echo ) static int uart_setup( lua_State* L ) { - unsigned id, databits, parity, stopbits, echo = 1; + unsigned id, databits, parity, stopbits; uint32_t baud, res; uart_pins_t pins; uart_pins_t* pins_to_use = NULL; @@ -144,13 +144,14 @@ static int uart_setup( lua_State* L ) databits = luaL_checkinteger( L, 3 ); parity = luaL_checkinteger( L, 4 ); stopbits = luaL_checkinteger( L, 5 ); - if(id == CONFIG_ESP_CONSOLE_UART_NUM && lua_isnumber(L,6)){ - echo = lua_tointeger(L,6); - if(echo!=0) - input_echo = true; - else - input_echo = false; - } else if(id != CONFIG_ESP_CONSOLE_UART_NUM && lua_istable( L, 6 )) { + if(id == CONFIG_ESP_CONSOLE_UART_NUM){ + if (!lua_isnoneornil(L, 6)) { + input_echo = luaL_checkinteger(L, 6) > 0; + } + } else { + if (!lua_isnoneornil(L, 6)) { + luaL_checktable(L, 6); + lua_getfield (L, 6, "tx"); pins.tx_pin = luaL_checkint(L, -1); lua_getfield (L, 6, "rx"); @@ -173,6 +174,7 @@ static int uart_setup( lua_State* L ) pins.flow_control = luaL_optint(L, -1, PLATFORM_UART_FLOW_NONE); pins_to_use = &pins; + } } res = platform_uart_setup( id, baud, databits, parity, stopbits, pins_to_use ); From 0e71189c113bf803e34181375336288c3f17e904 Mon Sep 17 00:00:00 2001 From: Joo Aun Saw Date: Tue, 30 Aug 2022 09:53:39 +1000 Subject: [PATCH 05/12] uart: further clean up --- components/modules/uart.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/components/modules/uart.c b/components/modules/uart.c index e5c4fc22..107e2884 100644 --- a/components/modules/uart.c +++ b/components/modules/uart.c @@ -144,12 +144,10 @@ static int uart_setup( lua_State* L ) databits = luaL_checkinteger( L, 3 ); parity = luaL_checkinteger( L, 4 ); stopbits = luaL_checkinteger( L, 5 ); - if(id == CONFIG_ESP_CONSOLE_UART_NUM){ - if (!lua_isnoneornil(L, 6)) { + if (!lua_isnoneornil(L, 6)) { + if(id == CONFIG_ESP_CONSOLE_UART_NUM){ input_echo = luaL_checkinteger(L, 6) > 0; - } - } else { - if (!lua_isnoneornil(L, 6)) { + } else { luaL_checktable(L, 6); lua_getfield (L, 6, "tx"); From d24f2ae459e46133424b787eab17b3ae48df6664 Mon Sep 17 00:00:00 2001 From: Joo Aun Saw Date: Tue, 30 Aug 2022 16:53:05 +1000 Subject: [PATCH 06/12] platform uart: really feed uart data to both console and on-data cb --- components/platform/platform.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/components/platform/platform.c b/components/platform/platform.c index 979984cf..5fc404d4 100644 --- a/components/platform/platform.c +++ b/components/platform/platform.c @@ -75,14 +75,18 @@ void uart_event_task( task_param_t param, task_prio_t prio ) { uart_status_t *us = &uart_status[id]; xSemaphoreGive(sem); if(post->type == PLATFORM_UART_EVENT_DATA) { - size_t i = 0; - while (i < post->size) - { - if (id == CONFIG_ESP_CONSOLE_UART_NUM && run_input) { + if (id == CONFIG_ESP_CONSOLE_UART_NUM && run_input) { + size_t i = 0; + while (i < post->size) + { unsigned used = feed_lua_input(post->data + i, post->size - i); i += used; } - if (uart_has_on_data_cb(id)) { + } + if (uart_has_on_data_cb(id)) { + size_t i = 0; + while (i < post->size) + { char ch = post->data[i]; us->line_buffer[us->line_position] = ch; us->line_position++; From 99656356948704e4bbc7ff64a3f06bf04c0b32ea Mon Sep 17 00:00:00 2001 From: Philip Gladstone Date: Thu, 29 Sep 2022 21:37:34 -0400 Subject: [PATCH 07/12] Ported the rtcmem over to ESP32 (#3544) * Ported the rtcmem over to ESP32 * Apply review comments. * Add the rtcmem string to the config option --- components/modules/CMakeLists.txt | 1 + components/modules/Kconfig | 7 ++++ components/modules/rtcmem.c | 52 ++++++++++++++++++++++++ docs/modules/rtcmem.md | 66 +++++++++++++++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 components/modules/rtcmem.c create mode 100644 docs/modules/rtcmem.md diff --git a/components/modules/CMakeLists.txt b/components/modules/CMakeLists.txt index aff349f9..5b960e2c 100644 --- a/components/modules/CMakeLists.txt +++ b/components/modules/CMakeLists.txt @@ -23,6 +23,7 @@ set(module_srcs "otaupgrade.c" "ow.c" "pipe.c" + "rtcmem.c" "qrcodegen.c" "sigma_delta.c" "sjson.c" diff --git a/components/modules/Kconfig b/components/modules/Kconfig index 3758bc59..7741e37b 100644 --- a/components/modules/Kconfig +++ b/components/modules/Kconfig @@ -225,6 +225,13 @@ menu "NodeMCU modules" Includes the rmt module to use the ESP32's built-in remote control hardware. + config NODEMCU_CMODULE_RTCMEM + bool "Access to a limited amount of battery backed memory (rtcmem)" + default "n" + help + Includes the rtcmem module to allow access to + the battery backed memory. + config NODEMCU_CMODULE_SDMMC depends on IDF_TARGET_ESP32 bool "SD-MMC module" diff --git a/components/modules/rtcmem.c b/components/modules/rtcmem.c new file mode 100644 index 00000000..34f03ec2 --- /dev/null +++ b/components/modules/rtcmem.c @@ -0,0 +1,52 @@ +// Module for RTC user memory access + +#include "module.h" +#include "lauxlib.h" +#include "esp_attr.h" + +#define RTC_USER_MEM_NUM_DWORDS 128 + +RTC_NOINIT_ATTR uint32_t rtc_memory[RTC_USER_MEM_NUM_DWORDS]; + +static int rtcmem_read32 (lua_State *L) +{ + int idx = luaL_checkinteger (L, 1); + int n = (lua_gettop(L) < 2) ? 1 : lua_tointeger (L, 2); + if (n == 0 || !lua_checkstack (L, n)) { + return 0; + } + + int ret = 0; + while (n > 0 && idx >= 0 && idx < RTC_USER_MEM_NUM_DWORDS) + { + lua_pushinteger (L, rtc_memory[idx++]); + --n; + ++ret; + } + return ret; +} + + +static int rtcmem_write32 (lua_State *L) +{ + int idx = luaL_checkinteger (L, 1); + int n = lua_gettop (L) - 1; + luaL_argcheck ( + L, idx + n <= RTC_USER_MEM_NUM_DWORDS, 1, "RTC mem would overrun"); + int src = 2; + while (n-- > 0) + { + rtc_memory[idx++] = (uint32_t) lua_tointeger(L, src++); + } + return 0; +} + + +// Module function map +LROT_BEGIN(rtcmem, NULL, 0) + LROT_FUNCENTRY( read32, rtcmem_read32 ) + LROT_FUNCENTRY( write32, rtcmem_write32 ) +LROT_END(rtcmem, NULL, 0) + + +NODEMCU_MODULE(RTCMEM, "rtcmem", rtcmem, NULL); diff --git a/docs/modules/rtcmem.md b/docs/modules/rtcmem.md new file mode 100644 index 00000000..bd4020dd --- /dev/null +++ b/docs/modules/rtcmem.md @@ -0,0 +1,66 @@ +# RTC User Memory Module +| Since | Origin / Contributor | Maintainer | Source | +| :----- | :-------------------- | :---------- | :------ | +| 2015-06-25 | [DiUS](https://github.com/DiUS), [Johny Mattsson](https://github.com/jmattsson) | [PJSG](https://github.com/pjsg) | [rtcmem.c](../../app/modules/rtcmem.c)| + +The rtcmem module provides basic access to the RTC memory. + +This memory is preserved while power is applied, making them highly useful for keeping state across sleep cycles. Some of this memory is reserved for system use, +and, for compatibility with NodeMCU on the ESP8266, 128 slots (each 32bit wide) of RTC memory are reserved by this module. +This module then provides read and write access to these slots. + +This module is 100% compatible with the ESP8266 version, and this means that, there is no mechanism for arbitrating use of particular slots. It is up to the end user to be aware of which memory is used for what, and avoid conflicts. Unlike the ESP8266 version, no other NodeMCU module uses any of these slots. + +Note that this memory is not necessary preserved across reflashing the firmware. It is the responsibility of the +developer to deal with getting inconsistent data. + +## rtcmem.read32() + +Reads one or more 32bit values from RTC user memory. + +#### Syntax +`rtcmem.read32(idx [, num])` + +#### Parameters + - `idx` zero-based index to start reading from + - `num` number of slots to read (default 1) + +#### Returns +The value(s) read from RTC user memory. + +If `idx` is outside the valid range [0,127] this function returns nothing. + +If `num` results in overstepping the end of available memory, the function only returns the data from the valid slots. + +#### Example +```lua +val = rtcmem.read32(0) -- Read the value in slot 0 +val1, val2 = rtcmem.read32(42, 2) -- Read the values in slots 42 and 43 +``` +#### See also +[`rtcmem.write32()`](#rtcmemwrite32) + +## rtcmem.write32() + +Writes one or more values to RTC user memory, starting at index `idx`. + +Writing to indices outside the valid range [0,127] has no effect. + +#### Syntax +`rtcmem.write32(idx, val [, val2, ...])` + +#### Parameters + - `idx` zero-based index to start writing to. Auto-increments if multiple values are given. + - `val` value to store (32bit) + - `val2...` additional values to store (optional) + +#### Returns +`nil` + +#### Example +```lua +rtcmem.write32(0, 53) -- Store the value 53 in slot 0 +rtcmem.write32(42, 2, 5, 7) -- Store the values 2, 5 and 7 into slots 42, 43 and 44, respectively. +``` +#### See also +[`rtcmem.read32()`](#rtcmemread32) From c7cab0aba4493c9a3b930a1f069425241d53c04f Mon Sep 17 00:00:00 2001 From: Philip Gladstone Date: Thu, 29 Sep 2022 21:38:32 -0400 Subject: [PATCH 08/12] Adds support for settxpower (#3535) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Adds support for settxpower * Update docs/modules/wifi.md Co-authored-by: Marcel Stör * Update docs/modules/wifi.md Co-authored-by: Marcel Stör Co-authored-by: Marcel Stör --- components/modules/wifi_sta.c | 20 ++++++++++++++++++-- docs/modules/wifi.md | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/components/modules/wifi_sta.c b/components/modules/wifi_sta.c index acf40b76..e75a6388 100644 --- a/components/modules/wifi_sta.c +++ b/components/modules/wifi_sta.c @@ -40,6 +40,7 @@ #include "nodemcu_esp_event.h" #include #include "esp_netif.h" +#include static esp_netif_t *wifi_sta = NULL; static int scan_cb_ref = LUA_NOREF; @@ -207,6 +208,20 @@ static int wifi_sta_setip(lua_State *L) return 0; } +static int wifi_sta_settxpower(lua_State *L) +{ + lua_Number max_power = luaL_checknumber(L, 1); + + esp_err_t err = esp_wifi_set_max_tx_power(floor(max_power * 4 + 0.5)); + + if (err != ESP_OK) + return luaL_error(L, "failed to set transmit power, code %d", err); + + lua_pushboolean(L, err == ESP_OK); + + return 1; +} + static int wifi_sta_sethostname(lua_State *L) { size_t l; @@ -440,8 +455,9 @@ static int wifi_sta_scan (lua_State *L) LROT_BEGIN(wifi_sta, NULL, 0) LROT_FUNCENTRY( setip, wifi_sta_setip ) - LROT_FUNCENTRY( sethostname, wifi_sta_sethostname ) - LROT_FUNCENTRY( config, wifi_sta_config ) + LROT_FUNCENTRY( sethostname, wifi_sta_sethostname) + LROT_FUNCENTRY( settxpower, wifi_sta_settxpower) + LROT_FUNCENTRY( config, wifi_sta_config) LROT_FUNCENTRY( connect, wifi_sta_connect ) LROT_FUNCENTRY( disconnect, wifi_sta_disconnect ) LROT_FUNCENTRY( getconfig, wifi_sta_getconfig ) diff --git a/docs/modules/wifi.md b/docs/modules/wifi.md index 70860feb..b84f826e 100644 --- a/docs/modules/wifi.md +++ b/docs/modules/wifi.md @@ -233,6 +233,27 @@ Disconnects from AP in station mode. - [`wifi.sta.connect()`](#wifistaconnect) +## wifi.sta.settxpower + +Allows adjusting the maximum TX power for the WiFi. This is (unfortunately) needed for some boards which +have a badly matched antenna. + +#### Syntax +`wifi.sta.settxpower(power)` + +#### Parameters +- `power` The maximum transmit power in dBm. This must have the range 2dBm - 20dBm. This value is a float. + +#### Returns +A `boolean` where `true` is OK. + +#### Example + +``` +# Needed for the WEMOS C3 Mini +wifi.sta.settxpower(8.5) +``` + ## wifi.sta.on() Registers callbacks for WiFi station status events. From f15470e44f6f4fe11cc598ada7cae18f7a9e4b9d Mon Sep 17 00:00:00 2001 From: Philip Gladstone Date: Sun, 2 Oct 2022 22:29:57 +0000 Subject: [PATCH 09/12] Include the rmt device for the esp32-c3 (and other missing devices) --- components/modules/CMakeLists.txt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/components/modules/CMakeLists.txt b/components/modules/CMakeLists.txt index 5b960e2c..88439660 100644 --- a/components/modules/CMakeLists.txt +++ b/components/modules/CMakeLists.txt @@ -59,12 +59,22 @@ if(IDF_TARGET STREQUAL "esp32") elseif(IDF_TARGET STREQUAL "esp32s2") list(APPEND module_srcs "dac.c" + "i2s.c" + "touch.c" + "rmt.c" + "pulsecnt.c" ) elseif(IDF_TARGET STREQUAL "esp32s3") list(APPEND module_srcs + "i2s.c" + "touch.c" + "rmt.c" + "pulsecnt.c" + "sdmmc.c" ) elseif(IDF_TARGET STREQUAL "esp32c3") list(APPEND module_srcs + "rmt.c" ) endif() From b9d6e2722e62393ec3847460585a4c62ec4eff09 Mon Sep 17 00:00:00 2001 From: Philip Gladstone Date: Sun, 2 Oct 2022 22:38:23 +0000 Subject: [PATCH 10/12] The i2s module doesn't appear to build --- components/modules/CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/components/modules/CMakeLists.txt b/components/modules/CMakeLists.txt index 88439660..fa21e262 100644 --- a/components/modules/CMakeLists.txt +++ b/components/modules/CMakeLists.txt @@ -59,14 +59,12 @@ if(IDF_TARGET STREQUAL "esp32") elseif(IDF_TARGET STREQUAL "esp32s2") list(APPEND module_srcs "dac.c" - "i2s.c" "touch.c" "rmt.c" "pulsecnt.c" ) elseif(IDF_TARGET STREQUAL "esp32s3") list(APPEND module_srcs - "i2s.c" "touch.c" "rmt.c" "pulsecnt.c" From fef57344db4d4cbd117164b928b90afed28f3859 Mon Sep 17 00:00:00 2001 From: Philip Gladstone Date: Sun, 2 Oct 2022 22:44:56 +0000 Subject: [PATCH 11/12] Remove the touch driver as well --- components/modules/CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/components/modules/CMakeLists.txt b/components/modules/CMakeLists.txt index fa21e262..2301e73d 100644 --- a/components/modules/CMakeLists.txt +++ b/components/modules/CMakeLists.txt @@ -59,13 +59,11 @@ if(IDF_TARGET STREQUAL "esp32") elseif(IDF_TARGET STREQUAL "esp32s2") list(APPEND module_srcs "dac.c" - "touch.c" "rmt.c" "pulsecnt.c" ) elseif(IDF_TARGET STREQUAL "esp32s3") list(APPEND module_srcs - "touch.c" "rmt.c" "pulsecnt.c" "sdmmc.c" From d8f07ddf90a278abb77a5247d4ce34201de9e56d Mon Sep 17 00:00:00 2001 From: Philip Gladstone Date: Sun, 2 Oct 2022 22:52:16 +0000 Subject: [PATCH 12/12] Remove another not-compiling module --- components/modules/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/components/modules/CMakeLists.txt b/components/modules/CMakeLists.txt index 2301e73d..80455ef9 100644 --- a/components/modules/CMakeLists.txt +++ b/components/modules/CMakeLists.txt @@ -66,7 +66,6 @@ elseif(IDF_TARGET STREQUAL "esp32s3") list(APPEND module_srcs "rmt.c" "pulsecnt.c" - "sdmmc.c" ) elseif(IDF_TARGET STREQUAL "esp32c3") list(APPEND module_srcs