diff --git a/components/modules/CMakeLists.txt b/components/modules/CMakeLists.txt index aff349f9..80455ef9 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" @@ -58,12 +59,17 @@ if(IDF_TARGET STREQUAL "esp32") elseif(IDF_TARGET STREQUAL "esp32s2") list(APPEND module_srcs "dac.c" + "rmt.c" + "pulsecnt.c" ) elseif(IDF_TARGET STREQUAL "esp32s3") list(APPEND module_srcs + "rmt.c" + "pulsecnt.c" ) elseif(IDF_TARGET STREQUAL "esp32c3") list(APPEND module_srcs + "rmt.c" ) endif() 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/components/modules/uart.c b/components/modules/uart.c index 118be37d..107e2884 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; @@ -128,23 +132,24 @@ 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; + memset(&pins, 0, sizeof(pins)); id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( uart, id ); baud = luaL_checkinteger( L, 2 ); 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 (!lua_isnoneornil(L, 6)) { + if(id == CONFIG_ESP_CONSOLE_UART_NUM){ + input_echo = luaL_checkinteger(L, 6) > 0; + } else { + luaL_checktable(L, 6); + lua_getfield (L, 6, "tx"); pins.tx_pin = luaL_checkint(L, -1); lua_getfield (L, 6, "rx"); @@ -165,9 +170,12 @@ 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; } 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/components/platform/platform.c b/components/platform/platform.c index 179e2369..30a9d6e4 100644 --- a/components/platform/platform.c +++ b/components/platform/platform.c @@ -89,16 +89,22 @@ static uart_status_t usbcdc_status; SemaphoreHandle_t usbcdc_sem = NULL; #endif +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); static void handle_uart_data(unsigned int id, uart_event_post_t *post, uart_status_t *us) { - 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; - } else { + } + } + 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++; @@ -106,13 +112,13 @@ static void handle_uart_data(unsigned int id, uart_event_post_t *post, uart_stat uint16_t need_len = us->need_len; int16_t end_char = us->end_char; size_t max_wanted = - (end_char >= 0 && need_len == 0) ? LUA_MAXINPUT : need_len; + (end_char >= 0 && need_len == 0) ? LUA_MAXINPUT : need_len; bool at_end = (us->line_position >= max_wanted); bool end_char_found = - (end_char >= 0 && (uint8_t)ch == (uint8_t)end_char); + (end_char >= 0 && (uint8_t)ch == (uint8_t)end_char); if (at_end || end_char_found) { - uart_on_data_cb(id, us->line_buffer, us->line_position); - us->line_position = 0; + uart_on_data_cb(id, us->line_buffer, us->line_position); + us->line_position = 0; } ++i; } @@ -292,9 +298,11 @@ uint32_t platform_uart_setup( unsigned id, uint32_t baud, int databits, int pari { ADJUST_IDR(id, baud); 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, 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) 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.