From d6176c1f5e5556e25944ce52874ea2f2d82ccde5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20de=20Giessen?= Date: Thu, 29 Feb 2024 14:05:01 +0100 Subject: [PATCH] esp32: Add support for IDF version v5.2. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniƫl van de Giessen --- ports/esp32/README.md | 2 +- ports/esp32/machine_uart.c | 7 ++++++- ports/esp32/modnetwork_globals.h | 9 +++++++++ ports/esp32/mpthreadport.c | 14 ++++++++++---- ports/esp32/network_common.c | 4 +++- ports/esp32/network_wlan.c | 22 ++++++++++++++++++++++ ports/esp32/uart.c | 3 ++- 7 files changed, 53 insertions(+), 8 deletions(-) diff --git a/ports/esp32/README.md b/ports/esp32/README.md index deed413f25..2e03c55d3d 100644 --- a/ports/esp32/README.md +++ b/ports/esp32/README.md @@ -28,7 +28,7 @@ manage the ESP32 microcontroller, as well as a way to manage the required build environment and toolchains needed to build the firmware. The ESP-IDF changes quickly and MicroPython only supports certain versions. -Currently MicroPython supports v5.0.4, v5.0.5, v5.1.2. +Currently MicroPython supports v5.0.4, v5.0.5, v5.1.2, v5.2.0. To install the ESP-IDF the full instructions can be found at the [Espressif Getting Started guide](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/index.html#installation-step-by-step). diff --git a/ports/esp32/machine_uart.c b/ports/esp32/machine_uart.c index b7adbf81c7..b5c5c016a5 100644 --- a/ports/esp32/machine_uart.c +++ b/ports/esp32/machine_uart.c @@ -306,7 +306,12 @@ static void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args, } self->flowcontrol = args[ARG_flow].u_int; } - check_esp_err(uart_set_hw_flow_ctrl(self->uart_num, self->flowcontrol, UART_FIFO_LEN - UART_FIFO_LEN / 4)); + #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0) + uint8_t uart_fifo_len = UART_HW_FIFO_LEN(self->uart_num); + #else + uint8_t uart_fifo_len = UART_FIFO_LEN; + #endif + check_esp_err(uart_set_hw_flow_ctrl(self->uart_num, self->flowcontrol, uart_fifo_len - uart_fifo_len / 4)); } static mp_obj_t mp_machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { diff --git a/ports/esp32/modnetwork_globals.h b/ports/esp32/modnetwork_globals.h index 38c192f9a6..7dd2ee9e66 100644 --- a/ports/esp32/modnetwork_globals.h +++ b/ports/esp32/modnetwork_globals.h @@ -34,6 +34,10 @@ #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 5) && ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 0) || ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 2) { MP_ROM_QSTR(MP_QSTR_AUTH_WPA3_ENT_192), MP_ROM_INT(WIFI_AUTH_WPA3_ENT_192) }, #endif +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0) +{ MP_ROM_QSTR(MP_QSTR_AUTH_WPA3_EXT_PSK), MP_ROM_INT(WIFI_AUTH_WPA3_EXT_PSK) }, +{ MP_ROM_QSTR(MP_QSTR_AUTH_WPA3_EXT_PSK_MIXED_MODE), MP_ROM_INT(WIFI_AUTH_WPA3_EXT_PSK_MIXED_MODE) }, +#endif { MP_ROM_QSTR(MP_QSTR_AUTH_MAX), MP_ROM_INT(WIFI_AUTH_MAX) }, #endif @@ -69,6 +73,11 @@ { MP_ROM_QSTR(MP_QSTR_STAT_GOT_IP), MP_ROM_INT(STAT_GOT_IP)}, // Errors from the ESP-IDF { MP_ROM_QSTR(MP_QSTR_STAT_NO_AP_FOUND), MP_ROM_INT(WIFI_REASON_NO_AP_FOUND)}, +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0) +{ MP_ROM_QSTR(MP_QSTR_STAT_NO_AP_FOUND_IN_RSSI_THRESHOLD), MP_ROM_INT(WIFI_REASON_NO_AP_FOUND_IN_RSSI_THRESHOLD)}, +{ MP_ROM_QSTR(MP_QSTR_STAT_NO_AP_FOUND_IN_AUTHMODE_THRESHOLD), MP_ROM_INT(WIFI_REASON_NO_AP_FOUND_IN_AUTHMODE_THRESHOLD)}, +{ MP_ROM_QSTR(MP_QSTR_STAT_NO_AP_FOUND_W_COMPATIBLE_SECURITY), MP_ROM_INT(WIFI_REASON_NO_AP_FOUND_W_COMPATIBLE_SECURITY)}, +#endif { MP_ROM_QSTR(MP_QSTR_STAT_WRONG_PASSWORD), MP_ROM_INT(WIFI_REASON_AUTH_FAIL)}, { MP_ROM_QSTR(MP_QSTR_STAT_BEACON_TIMEOUT), MP_ROM_INT(WIFI_REASON_BEACON_TIMEOUT)}, { MP_ROM_QSTR(MP_QSTR_STAT_ASSOC_FAIL), MP_ROM_INT(WIFI_REASON_ASSOC_FAIL)}, diff --git a/ports/esp32/mpthreadport.c b/ports/esp32/mpthreadport.c index 1c13e928d9..12a6ede866 100644 --- a/ports/esp32/mpthreadport.c +++ b/ports/esp32/mpthreadport.c @@ -41,6 +41,12 @@ #define MP_THREAD_DEFAULT_STACK_SIZE (MP_THREAD_MIN_STACK_SIZE + 1024) #define MP_THREAD_PRIORITY (ESP_TASK_PRIO_MIN + 1) +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0) && !CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP +#define FREERTOS_TASK_DELETE_HOOK vTaskPreDeletionHook +#else +#define FREERTOS_TASK_DELETE_HOOK vPortCleanUpTCB +#endif + // this structure forms a linked list, one node per active thread typedef struct _mp_thread_t { TaskHandle_t id; // system id of thread @@ -70,7 +76,7 @@ void mp_thread_init(void *stack, uint32_t stack_len) { // memory barrier to ensure above data is committed __sync_synchronize(); - // vPortCleanUpTCB needs the thread ready after thread_mutex is ready + // FREERTOS_TASK_DELETE_HOOK needs the thread ready after thread_mutex is ready thread = &thread_entry0; } @@ -179,7 +185,7 @@ void mp_thread_finish(void) { // This is called from the FreeRTOS idle task and is not within Python context, // so MP_STATE_THREAD is not valid and it does not have the GIL. -void vPortCleanUpTCB(void *tcb) { +void FREERTOS_TASK_DELETE_HOOK(void *tcb) { if (thread == NULL) { // threading not yet initialised return; @@ -235,7 +241,7 @@ void mp_thread_deinit(void) { // No tasks left to delete break; } else { - // Call FreeRTOS to delete the task (it will call vPortCleanUpTCB) + // Call FreeRTOS to delete the task (it will call FREERTOS_TASK_DELETE_HOOK) vTaskDelete(id); } } @@ -243,7 +249,7 @@ void mp_thread_deinit(void) { #else -void vPortCleanUpTCB(void *tcb) { +void FREERTOS_TASK_DELETE_HOOK(void *tcb) { } #endif // MICROPY_PY_THREAD diff --git a/ports/esp32/network_common.c b/ports/esp32/network_common.c index a8c407b08c..223803a5d1 100644 --- a/ports/esp32/network_common.c +++ b/ports/esp32/network_common.c @@ -168,7 +168,9 @@ static mp_obj_t esp_phy_mode(size_t n_args, const mp_obj_t *args) { } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_network_phy_mode_obj, 0, 1, esp_phy_mode); -#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 5) && ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 0) || ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 2) +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0) +_Static_assert(WIFI_AUTH_MAX == 13, "Synchronize WIFI_AUTH_XXX constants with the ESP-IDF. Look at esp-idf/components/esp_wifi/include/esp_wifi_types.h"); +#elif ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 5) && ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 1, 0) || ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 2) _Static_assert(WIFI_AUTH_MAX == 11, "Synchronize WIFI_AUTH_XXX constants with the ESP-IDF. Look at esp-idf/components/esp_wifi/include/esp_wifi_types.h"); #else _Static_assert(WIFI_AUTH_MAX == 10, "Synchronize WIFI_AUTH_XXX constants with the ESP-IDF. Look at esp-idf/components/esp_wifi/include/esp_wifi_types.h"); diff --git a/ports/esp32/network_wlan.c b/ports/esp32/network_wlan.c index dfa3347c67..ece88e705f 100644 --- a/ports/esp32/network_wlan.c +++ b/ports/esp32/network_wlan.c @@ -113,6 +113,20 @@ static void network_wlan_wifi_event_handler(void *event_handler_arg, esp_event_b // AP may not exist, or it may have momentarily dropped out; try to reconnect. message = "no AP found"; break; + #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0) + case WIFI_REASON_NO_AP_FOUND_IN_RSSI_THRESHOLD: + // No AP with RSSI within given threshold exists, or it may have momentarily dropped out; try to reconnect. + message = "no AP with RSSI within threshold found"; + break; + case WIFI_REASON_NO_AP_FOUND_IN_AUTHMODE_THRESHOLD: + // No AP with authmode within given threshold exists, or it may have momentarily dropped out; try to reconnect. + message = "no AP with authmode within threshold found"; + break; + case WIFI_REASON_NO_AP_FOUND_W_COMPATIBLE_SECURITY: + // No AP with compatible security exists, or it may have momentarily dropped out; try to reconnect. + message = "no AP with compatible security found"; + break; + #endif case WIFI_REASON_AUTH_FAIL: // Password may be wrong, or it just failed to connect; try to reconnect. message = "authentication failed"; @@ -353,6 +367,14 @@ static mp_obj_t network_wlan_status(size_t n_args, const mp_obj_t *args) { return MP_OBJ_NEW_SMALL_INT(STAT_GOT_IP); } else if (wifi_sta_disconn_reason == WIFI_REASON_NO_AP_FOUND) { return MP_OBJ_NEW_SMALL_INT(WIFI_REASON_NO_AP_FOUND); + #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0) + } else if (wifi_sta_disconn_reason == WIFI_REASON_NO_AP_FOUND_IN_RSSI_THRESHOLD) { + return MP_OBJ_NEW_SMALL_INT(WIFI_REASON_NO_AP_FOUND_IN_RSSI_THRESHOLD); + } else if (wifi_sta_disconn_reason == WIFI_REASON_NO_AP_FOUND_IN_AUTHMODE_THRESHOLD) { + return MP_OBJ_NEW_SMALL_INT(WIFI_REASON_NO_AP_FOUND_IN_AUTHMODE_THRESHOLD); + } else if (wifi_sta_disconn_reason == WIFI_REASON_NO_AP_FOUND_W_COMPATIBLE_SECURITY) { + return MP_OBJ_NEW_SMALL_INT(WIFI_REASON_NO_AP_FOUND_W_COMPATIBLE_SECURITY); + #endif } else if ((wifi_sta_disconn_reason == WIFI_REASON_AUTH_FAIL) || (wifi_sta_disconn_reason == WIFI_REASON_CONNECTION_FAIL)) { // wrong password return MP_OBJ_NEW_SMALL_INT(WIFI_REASON_AUTH_FAIL); diff --git a/ports/esp32/uart.c b/ports/esp32/uart.c index 863301143d..8336268509 100644 --- a/ports/esp32/uart.c +++ b/ports/esp32/uart.c @@ -36,6 +36,7 @@ #include #include "driver/uart.h" // For uart_get_sclk_freq() #include "hal/uart_hal.h" +#include "soc/uart_periph.h" static void uart_irq_handler(void *arg); @@ -50,7 +51,7 @@ static void uart_irq_handler(void *arg); void uart_stdout_init(void) { uart_hal_context_t repl_hal = REPL_HAL_DEFN(); - #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0) + #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 2, 0) uart_sclk_t sclk; #else soc_module_clk_t sclk;