From e7ae3ad92d7540cbe349cc05f5d62e0f63fce59b Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 9 May 2023 09:52:16 +1000 Subject: [PATCH 01/12] extmod: Update to support mbedtls 3.x. Signed-off-by: Damien George --- extmod/modhashlib.c | 4 ++-- extmod/modssl_mbedtls.c | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/extmod/modhashlib.c b/extmod/modhashlib.c index 3fafe6316def6..4cdc23fddc6c6 100644 --- a/extmod/modhashlib.c +++ b/extmod/modhashlib.c @@ -75,7 +75,7 @@ STATIC mp_obj_t hashlib_sha256_update(mp_obj_t self_in, mp_obj_t arg); #if MICROPY_SSL_MBEDTLS -#if MBEDTLS_VERSION_NUMBER < 0x02070000 +#if MBEDTLS_VERSION_NUMBER < 0x02070000 || MBEDTLS_VERSION_NUMBER >= 0x03000000 #define mbedtls_sha256_starts_ret mbedtls_sha256_starts #define mbedtls_sha256_update_ret mbedtls_sha256_update #define mbedtls_sha256_finish_ret mbedtls_sha256_finish @@ -203,7 +203,7 @@ STATIC mp_obj_t hashlib_sha1_digest(mp_obj_t self_in) { #if MICROPY_SSL_MBEDTLS -#if MBEDTLS_VERSION_NUMBER < 0x02070000 +#if MBEDTLS_VERSION_NUMBER < 0x02070000 || MBEDTLS_VERSION_NUMBER >= 0x03000000 #define mbedtls_sha1_starts_ret mbedtls_sha1_starts #define mbedtls_sha1_update_ret mbedtls_sha1_update #define mbedtls_sha1_finish_ret mbedtls_sha1_finish diff --git a/extmod/modssl_mbedtls.c b/extmod/modssl_mbedtls.c index c230b1eaa2416..e346f986f77f2 100644 --- a/extmod/modssl_mbedtls.c +++ b/extmod/modssl_mbedtls.c @@ -225,7 +225,11 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) { size_t key_len; const byte *key = (const byte *)mp_obj_str_get_data(args->key.u_obj, &key_len); // len should include terminating null + #if MBEDTLS_VERSION_NUMBER >= 0x03000000 + ret = mbedtls_pk_parse_key(&o->pkey, key, key_len + 1, NULL, 0, mbedtls_ctr_drbg_random, &o->ctr_drbg); + #else ret = mbedtls_pk_parse_key(&o->pkey, key, key_len + 1, NULL, 0); + #endif if (ret != 0) { ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA; // use general error for all key errors goto cleanup; From 402fdc40fb5ca98b3987052cc264e8344d928a69 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 21 Jun 2023 14:40:33 +1000 Subject: [PATCH 02/12] extmod/modplatform: Set MICROPY_PLATFORM_ARCH on riscv platforms. Signed-off-by: Damien George --- extmod/modplatform.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/extmod/modplatform.h b/extmod/modplatform.h index 95bf073fbd90f..56a50e53c5445 100644 --- a/extmod/modplatform.h +++ b/extmod/modplatform.h @@ -43,6 +43,8 @@ #define MICROPY_PLATFORM_ARCH "x86" #elif defined(__xtensa__) #define MICROPY_PLATFORM_ARCH "xtensa" +#elif defined(__riscv) +#define MICROPY_PLATFORM_ARCH "riscv" #else #define MICROPY_PLATFORM_ARCH "" #endif From 18caf49a7fab84f55fdf170eb5ca1c27206ccc76 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 22 Jun 2023 11:18:01 +1000 Subject: [PATCH 03/12] extmod/modbtree: Undefine queue macros before including berkeley-db. To prevent warnings when building with ESP IDF v5. Signed-off-by: Damien George --- extmod/modbtree.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/extmod/modbtree.c b/extmod/modbtree.c index e9c760a389717..9b2d184a1ee79 100644 --- a/extmod/modbtree.c +++ b/extmod/modbtree.c @@ -32,6 +32,31 @@ #include #include // for declaration of global errno variable #include + +// Undefine queue macros that will be defined in berkeley-db-1.xx headers +// below, in case they clash with system ones defined in headers above. +#undef LIST_HEAD +#undef LIST_ENTRY +#undef LIST_INIT +#undef LIST_INSERT_AFTER +#undef LIST_INSERT_HEAD +#undef LIST_REMOVE +#undef TAILQ_HEAD +#undef TAILQ_ENTRY +#undef TAILQ_INIT +#undef TAILQ_INSERT_HEAD +#undef TAILQ_INSERT_TAIL +#undef TAILQ_INSERT_AFTER +#undef TAILQ_REMOVE +#undef CIRCLEQ_HEAD +#undef CIRCLEQ_ENTRY +#undef CIRCLEQ_INIT +#undef CIRCLEQ_INSERT_AFTER +#undef CIRCLEQ_INSERT_BEFORE +#undef CIRCLEQ_INSERT_HEAD +#undef CIRCLEQ_INSERT_TAIL +#undef CIRCLEQ_REMOVE + #include #include <../../btree/btree.h> From 7c929d4478fbb247c330eb182e9d07440b962cfc Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Tue, 9 May 2023 15:46:01 +1000 Subject: [PATCH 04/12] esp32: Switch from UART driver to UART HAL. Allows registering UART interrupt again. Signed-off-by: Angus Gratton --- ports/esp32/uart.c | 104 +++++++++++++++++++++++++-------------------- ports/esp32/uart.h | 2 +- 2 files changed, 59 insertions(+), 47 deletions(-) diff --git a/ports/esp32/uart.c b/ports/esp32/uart.c index e871ca1d7a330..358d434709951 100644 --- a/ports/esp32/uart.c +++ b/ports/esp32/uart.c @@ -28,81 +28,93 @@ #include -#include "driver/uart.h" -#include "soc/uart_periph.h" +#include "hal/uart_hal.h" #include "py/runtime.h" #include "py/mphal.h" #include "uart.h" +// Backwards compatibility for when MICROPY_HW_UART_REPL was a ESP-IDF UART +// driver enum. Only UART_NUM_0 was supported with that version of the driver. +#define UART_NUM_0 0 + STATIC void uart_irq_handler(void *arg); +// Declaring the HAL structure on the stack saves a tiny amount of static RAM +#define REPL_HAL_DEFN() { .dev = UART_LL_GET_HW(MICROPY_HW_UART_REPL) } + +// RXFIFO Full interrupt threshold. Set the same as the ESP-IDF UART driver +#define RXFIFO_FULL_THR (SOC_UART_FIFO_LEN - 8) + +// RXFIFO RX timeout threshold. This is in bit periods, so 10==one byte. Same as ESP-IDF UART driver. +#define RXFIFO_RX_TIMEOUT (10) + void uart_stdout_init(void) { - uart_config_t uartcfg = { - .baud_rate = MICROPY_HW_UART_REPL_BAUD, - .data_bits = UART_DATA_8_BITS, - .parity = UART_PARITY_DISABLE, - .stop_bits = UART_STOP_BITS_1, - .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, - .rx_flow_ctrl_thresh = 0 - }; - #if SOC_UART_SUPPORT_XTAL_CLK - // works independently of APB frequency - uartcfg.source_clk = UART_SCLK_XTAL; // ESP32C3, ESP32S3 + uart_hal_context_t repl_hal = REPL_HAL_DEFN(); + uint32_t sclk_freq; + + #if UART_SCLK_DEFAULT == SOC_MOD_CLK_APB + sclk_freq = APB_CLK_FREQ; // Assumes no frequency scaling + #else + // ESP32-H2 and ESP32-C2, I think + #error "This SoC uses a different default UART SCLK source, code needs updating." #endif - uart_param_config(MICROPY_HW_UART_REPL, &uartcfg); - const uint32_t rxbuf = 129; // IDF requires > 128 min - const uint32_t txbuf = 0; + uart_hal_init(&repl_hal, MICROPY_HW_UART_REPL); // Sets defaults: 8n1, no flow control + uart_hal_set_baudrate(&repl_hal, MICROPY_HW_UART_REPL_BAUD, sclk_freq); + uart_hal_rxfifo_rst(&repl_hal); + uart_hal_txfifo_rst(&repl_hal); - uart_driver_install(MICROPY_HW_UART_REPL, rxbuf, txbuf, 0, NULL, 0); + ESP_ERROR_CHECK( + esp_intr_alloc(uart_periph_signal[MICROPY_HW_UART_REPL].irq, + ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM, + uart_irq_handler, + NULL, + NULL) + ); - uart_isr_handle_t handle; - uart_isr_free(MICROPY_HW_UART_REPL); - uart_isr_register(MICROPY_HW_UART_REPL, uart_irq_handler, NULL, ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM, &handle); - uart_enable_rx_intr(MICROPY_HW_UART_REPL); + // Enable RX interrupts + uart_hal_set_rxfifo_full_thr(&repl_hal, RXFIFO_FULL_THR); + uart_hal_set_rx_timeout(&repl_hal, RXFIFO_RX_TIMEOUT); + uart_hal_ena_intr_mask(&repl_hal, UART_INTR_RXFIFO_FULL | UART_INTR_RXFIFO_TOUT); } int uart_stdout_tx_strn(const char *str, size_t len) { + uart_hal_context_t repl_hal = REPL_HAL_DEFN(); size_t remaining = len; + uint32_t written = 0; // TODO add a timeout for (;;) { - int ret = uart_tx_chars(MICROPY_HW_UART_REPL, str, remaining); - if (ret == -1) { - return -1; - } - remaining -= ret; - if (remaining <= 0) { + uart_hal_write_txfifo(&repl_hal, (const void *)str, remaining, &written); + + if (written >= remaining) { break; } - str += ret; + remaining -= written; + str += written; ulTaskNotifyTake(pdFALSE, 1); } - return len - remaining; + return len; } // all code executed in ISR must be in IRAM, and any const data must be in DRAM STATIC void IRAM_ATTR uart_irq_handler(void *arg) { - volatile uart_dev_t *uart = &UART0; - #if CONFIG_IDF_TARGET_ESP32S3 - uart->int_clr.rxfifo_full_int_clr = 1; - uart->int_clr.rxfifo_tout_int_clr = 1; - #else - uart->int_clr.rxfifo_full = 1; - uart->int_clr.rxfifo_tout = 1; - uart->int_clr.frm_err = 1; - #endif - while (uart->status.rxfifo_cnt) { - #if CONFIG_IDF_TARGET_ESP32 - uint8_t c = uart->fifo.rw_byte; - #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 - uint8_t c = READ_PERI_REG(UART_FIFO_AHB_REG(0)); // UART0 - #endif - if (c == mp_interrupt_char) { + uint8_t rbuf[SOC_UART_FIFO_LEN]; + int len; + uart_hal_context_t repl_hal = REPL_HAL_DEFN(); + + uart_hal_clr_intsts_mask(&repl_hal, UART_INTR_RXFIFO_FULL | UART_INTR_RXFIFO_TOUT | UART_INTR_FRAM_ERR); + + len = uart_hal_get_rxfifo_len(&repl_hal); + + uart_hal_read_rxfifo(&repl_hal, rbuf, &len); + + for (int i = 0; i < len; i++) { + if (rbuf[i] == mp_interrupt_char) { mp_sched_keyboard_interrupt(); } else { // this is an inline function so will be in IRAM - ringbuf_put(&stdin_ringbuf, c); + ringbuf_put(&stdin_ringbuf, rbuf[i]); } } } diff --git a/ports/esp32/uart.h b/ports/esp32/uart.h index e3c7482e7b485..14ad30ccd84ea 100644 --- a/ports/esp32/uart.h +++ b/ports/esp32/uart.h @@ -34,7 +34,7 @@ #endif #ifndef MICROPY_HW_UART_REPL -#define MICROPY_HW_UART_REPL (UART_NUM_0) +#define MICROPY_HW_UART_REPL (0) #endif #ifndef MICROPY_HW_UART_REPL_BAUD From 1db40ed29542ff85c8d1d1b58707122436b39cf4 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 21 Jun 2023 15:09:26 +1000 Subject: [PATCH 05/12] esp32/ppp_set_auth: Add pppapi_set_auth from ESP-IDF. This function was made private/static in IDF commit c67f4c2b4c2bb4b7740f988fc0f8a3e911e56afe, so it add back here. Signed-off-by: Damien George --- LICENSE | 2 ++ ports/esp32/main/CMakeLists.txt | 1 + ports/esp32/ppp_set_auth.c | 35 +++++++++++++++++++++++++++++++++ ports/esp32/ppp_set_auth.h | 22 +++++++++++++++++++++ 4 files changed, 60 insertions(+) create mode 100644 ports/esp32/ppp_set_auth.c create mode 100644 ports/esp32/ppp_set_auth.h diff --git a/LICENSE b/LICENSE index 31eb9813f2d6a..07c88b0a03231 100644 --- a/LICENSE +++ b/LICENSE @@ -67,6 +67,8 @@ used during the build process and is not part of the compiled source code. /hal (BSD-3-clause) /simplelink (BSD-3-clause) /FreeRTOS (GPL-2.0 with FreeRTOS exception) + /esp32 + /ppp_set_auth.* (Apache-2.0) /stm32 /usbd*.c (MCD-ST Liberty SW License Agreement V2) /stm32_it.* (MIT + BSD-3-clause) diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/main/CMakeLists.txt index 425d70fdf047c..4311af801c0f3 100644 --- a/ports/esp32/main/CMakeLists.txt +++ b/ports/esp32/main/CMakeLists.txt @@ -53,6 +53,7 @@ set(MICROPY_SOURCE_DRIVERS set(MICROPY_SOURCE_PORT main.c + ppp_set_auth.c uart.c usb.c usb_serial_jtag.c diff --git a/ports/esp32/ppp_set_auth.c b/ports/esp32/ppp_set_auth.c new file mode 100644 index 0000000000000..88ab668d48d3a --- /dev/null +++ b/ports/esp32/ppp_set_auth.c @@ -0,0 +1,35 @@ +/* + * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +// The pppapi_set_auth function was made static in the ESP-IDF, so it's re-added here. +// See ESP-IDF commit c67f4c2b4c2bb4b7740f988fc0f8a3e911e56afe + +#include "ppp_set_auth.h" + +#ifdef CONFIG_ESP_NETIF_TCPIP_LWIP + +#include "netif/ppp/pppapi.h" + +typedef struct { + struct tcpip_api_call_data call; + ppp_pcb *ppp; + u8_t authtype; + const char *user; + const char *passwd; +} set_auth_msg_t; + +static err_t pppapi_do_ppp_set_auth(struct tcpip_api_call_data *m) { + set_auth_msg_t *msg = (set_auth_msg_t *)m; + ppp_set_auth(msg->ppp, msg->authtype, msg->user, msg->passwd); + return ERR_OK; +} + +void pppapi_set_auth(ppp_pcb *pcb, u8_t authtype, const char *user, const char *passwd) { + set_auth_msg_t msg = { .ppp = pcb, .authtype = authtype, .user = user, .passwd = passwd}; + tcpip_api_call(pppapi_do_ppp_set_auth, &msg.call); +} + +#endif diff --git a/ports/esp32/ppp_set_auth.h b/ports/esp32/ppp_set_auth.h new file mode 100644 index 0000000000000..67676ef4d6c76 --- /dev/null +++ b/ports/esp32/ppp_set_auth.h @@ -0,0 +1,22 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +// The pppapi_set_auth function was made static in the ESP-IDF, so it's re-added here. +// See ESP-IDF commit c67f4c2b4c2bb4b7740f988fc0f8a3e911e56afe + +#pragma once + +#include "esp_netif.h" + +#ifdef CONFIG_ESP_NETIF_TCPIP_LWIP + +#include "lwip/netif.h" + +typedef struct ppp_pcb_s ppp_pcb; + +void pppapi_set_auth(ppp_pcb *pcb, u8_t authtype, const char *user, const char *passwd); + +#endif From 2af229c3cc55ceefc4cfba0f50092af725c3bfa9 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 21 Jun 2023 17:04:56 +1000 Subject: [PATCH 06/12] esp32/modesp32: Remove esp32.hall_sensor function. The hall sensor is no longer supported by IDF v5. Signed-off-by: Damien George --- ports/esp32/modesp32.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/ports/esp32/modesp32.c b/ports/esp32/modesp32.c index 7a436bf77cefb..a18ca4c1375ac 100644 --- a/ports/esp32/modesp32.c +++ b/ports/esp32/modesp32.c @@ -170,12 +170,6 @@ STATIC mp_obj_t esp32_raw_temperature(void) { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32_raw_temperature_obj, esp32_raw_temperature); -STATIC mp_obj_t esp32_hall_sensor(void) { - adc1_config_width(ADC_WIDTH_12Bit); - return MP_OBJ_NEW_SMALL_INT(hall_sensor_read()); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp32_hall_sensor_obj, esp32_hall_sensor); - #endif STATIC mp_obj_t esp32_idf_heap_info(const mp_obj_t cap_in) { @@ -210,7 +204,6 @@ STATIC const mp_rom_map_elem_t esp32_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_gpio_deep_sleep_hold), MP_ROM_PTR(&esp32_gpio_deep_sleep_hold_obj) }, #if CONFIG_IDF_TARGET_ESP32 { MP_ROM_QSTR(MP_QSTR_raw_temperature), MP_ROM_PTR(&esp32_raw_temperature_obj) }, - { MP_ROM_QSTR(MP_QSTR_hall_sensor), MP_ROM_PTR(&esp32_hall_sensor_obj) }, #endif { MP_ROM_QSTR(MP_QSTR_idf_heap_info), MP_ROM_PTR(&esp32_idf_heap_info_obj) }, From e4650125b88a35f074097f16d84a8f49bd22ac06 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 9 May 2023 09:52:54 +1000 Subject: [PATCH 07/12] esp32: Update port to support IDF v5.0.2. This commit updates the esp32 port to work exclusively with ESP-IDF v5. IDF v5 is needed for some of the newer ESP32 SoCs to work, and it also cleans up a lot of the inconsistencies between existing SoCs (eg S2, S3, and C3). Support for IDF v4 is dropped because it's a lot of effort to maintain both versions at the same time. The following components have been verified to work on the various SoCs: ESP32 ESP32-S2 ESP32-S3 ESP32-C3 build pass pass pass pass SPIRAM pass pass pass N/A REPL (UART) pass pass pass pass REPL (USB) N/A pass pass N/A filesystem pass pass pass pass GPIO pass pass pass pass SPI pass pass pass pass I2C pass pass pass pass PWM pass pass pass pass ADC pass pass pass pass WiFi STA pass pass pass pass WiFi AP pass pass pass pass BLE pass N/A pass pass ETH pass -- -- -- PPP pass pass pass -- sockets pass pass pass pass SSL pass ENOMEM pass pass RMT pass pass pass pass NeoPixel pass pass pass pass I2S pass pass pass N/A ESPNow pass pass pass pass ULP-FSM pass pass pass N/A SDCard pass N/A N/A pass WDT pass pass pass pass Signed-off-by: Damien George Signed-off-by: Jim Mussared --- ports/esp32/.gitignore | 2 + ports/esp32/CMakeLists.txt | 20 ++- ports/esp32/README.md | 15 +- .../boards/ESP32_S2_WROVER/sdkconfig.board | 1 - .../boards/GENERIC_C3_USB/sdkconfig.board | 2 - .../esp32/boards/GENERIC_D2WD/sdkconfig.board | 1 + ports/esp32/boards/GENERIC_S3/sdkconfig.board | 3 - .../boards/GENERIC_S3_SPIRAM/sdkconfig.board | 3 - .../GENERIC_S3_SPIRAM_OCT/sdkconfig.board | 3 - .../boards/GENERIC_SPIRAM/mpconfigboard.cmake | 1 - .../boards/GENERIC_SPIRAM/sdkconfig.board | 2 - .../boards/LOLIN_C3_MINI/sdkconfig.board | 2 - .../esp32/boards/UM_FEATHERS2/sdkconfig.board | 4 - .../boards/UM_FEATHERS2NEO/sdkconfig.board | 2 - .../esp32/boards/UM_FEATHERS3/sdkconfig.board | 7 +- ports/esp32/boards/UM_PROS3/sdkconfig.board | 5 - .../esp32/boards/UM_TINYPICO/sdkconfig.board | 3 - ports/esp32/boards/UM_TINYS2/sdkconfig.board | 2 - ports/esp32/boards/UM_TINYS3/sdkconfig.board | 5 - ports/esp32/boards/sdkconfig.240mhz | 9 +- ports/esp32/boards/sdkconfig.base | 34 +++-- ports/esp32/boards/sdkconfig.ble | 15 +- ports/esp32/boards/sdkconfig.nimble_core0 | 6 - ports/esp32/boards/sdkconfig.nimble_core1 | 6 - ports/esp32/boards/sdkconfig.spiram | 9 +- ports/esp32/boards/sdkconfig.spiram_sx | 7 +- ports/esp32/boards/sdkconfig.usb | 8 +- .../CMakeLists.txt => esp32_common.cmake} | 75 +++------- ports/esp32/esp32_rmt.c | 4 - ports/esp32/esp32_ulp.c | 9 +- ports/esp32/gccollect.c | 3 +- ports/esp32/machine_adcblock.h | 2 + ports/esp32/machine_bitstream.c | 35 +---- ports/esp32/machine_hw_spi.c | 34 +++-- ports/esp32/machine_i2c.c | 8 +- ports/esp32/machine_i2s.c | 30 +--- ports/esp32/machine_pin.c | 25 ++-- ports/esp32/machine_pwm.c | 19 +-- ports/esp32/machine_sdcard.c | 137 ++++++++++++------ ports/esp32/machine_timer.c | 32 +--- ports/esp32/machine_uart.c | 9 -- ports/esp32/machine_wdt.c | 10 +- ports/esp32/main.c | 67 ++------- ports/esp32/main_esp32/CMakeLists.txt | 11 ++ ports/esp32/main_esp32c3/CMakeLists.txt | 14 ++ ports/esp32/main_esp32s2/CMakeLists.txt | 11 ++ ports/esp32/main_esp32s2/idf_component.yml | 5 + ports/esp32/main_esp32s3/CMakeLists.txt | 11 ++ ports/esp32/main_esp32s3/idf_component.yml | 5 + ports/esp32/modesp.c | 24 +-- ports/esp32/modesp32.c | 6 +- ports/esp32/modespnow.c | 14 +- ports/esp32/modmachine.c | 16 +- ports/esp32/modnetwork.h | 10 +- ports/esp32/modnetwork_globals.h | 9 +- ports/esp32/modsocket.c | 7 +- ports/esp32/mpconfigport.h | 14 +- ports/esp32/mphalport.c | 3 +- ports/esp32/mphalport.h | 12 +- ports/esp32/mpnimbleport.c | 4 +- ports/esp32/network_common.c | 107 +++----------- ports/esp32/network_lan.c | 52 +++---- ports/esp32/network_ppp.c | 1 + ports/esp32/network_wlan.c | 129 ++++++++++------- ports/esp32/uart.h | 2 +- ports/esp32/usb.c | 11 +- ports/esp32/usb.h | 2 +- ports/esp32/usb_serial_jtag.c | 4 +- 68 files changed, 485 insertions(+), 675 deletions(-) create mode 100644 ports/esp32/.gitignore delete mode 100644 ports/esp32/boards/GENERIC_SPIRAM/sdkconfig.board delete mode 100644 ports/esp32/boards/sdkconfig.nimble_core0 delete mode 100644 ports/esp32/boards/sdkconfig.nimble_core1 rename ports/esp32/{main/CMakeLists.txt => esp32_common.cmake} (69%) create mode 100644 ports/esp32/main_esp32/CMakeLists.txt create mode 100644 ports/esp32/main_esp32c3/CMakeLists.txt create mode 100644 ports/esp32/main_esp32s2/CMakeLists.txt create mode 100644 ports/esp32/main_esp32s2/idf_component.yml create mode 100644 ports/esp32/main_esp32s3/CMakeLists.txt create mode 100644 ports/esp32/main_esp32s3/idf_component.yml diff --git a/ports/esp32/.gitignore b/ports/esp32/.gitignore new file mode 100644 index 0000000000000..a78ecd0191c9f --- /dev/null +++ b/ports/esp32/.gitignore @@ -0,0 +1,2 @@ +dependencies.lock +managed_components/ diff --git a/ports/esp32/CMakeLists.txt b/ports/esp32/CMakeLists.txt index 6992737c9b4d6..a4970c1a1b0b2 100644 --- a/ports/esp32/CMakeLists.txt +++ b/ports/esp32/CMakeLists.txt @@ -15,16 +15,15 @@ if(NOT EXISTS ${MICROPY_BOARD_DIR}/mpconfigboard.cmake) message(FATAL_ERROR "Invalid MICROPY_BOARD specified: ${MICROPY_BOARD}") endif() -# Include main IDF cmake file. -include($ENV{IDF_PATH}/tools/cmake/project.cmake) - # Define the output sdkconfig so it goes in the build directory. set(SDKCONFIG ${CMAKE_BINARY_DIR}/sdkconfig) # Save the manifest file set from the cmake command line. set(MICROPY_USER_FROZEN_MANIFEST ${MICROPY_FROZEN_MANIFEST}) -# Include board config; this is expected to set SDKCONFIG_DEFAULTS (among other options). +# Include board config; this is expected to set (among other options): +# - SDKCONFIG_DEFAULTS +# - IDF_TARGET include(${MICROPY_BOARD_DIR}/mpconfigboard.cmake) # Set the frozen manifest file. Note if MICROPY_FROZEN_MANIFEST is set from the cmake @@ -35,13 +34,6 @@ elseif (NOT MICROPY_FROZEN_MANIFEST) set(MICROPY_FROZEN_MANIFEST ${CMAKE_CURRENT_LIST_DIR}/boards/manifest.py) endif() -# Add sdkconfig fragments that depend on the IDF version. -if(IDF_VERSION_MAJOR EQUAL 4 AND IDF_VERSION_MINOR LESS 2) - set(SDKCONFIG_DEFAULTS ${SDKCONFIG_DEFAULTS} boards/sdkconfig.nimble_core0) -else() - set(SDKCONFIG_DEFAULTS ${SDKCONFIG_DEFAULTS} boards/sdkconfig.nimble_core1) -endif() - # Concatenate all sdkconfig files into a combined one for the IDF to use. file(WRITE ${CMAKE_BINARY_DIR}/sdkconfig.combined.in "") foreach(SDKCONFIG_DEFAULT ${SDKCONFIG_DEFAULTS}) @@ -51,5 +43,11 @@ endforeach() configure_file(${CMAKE_BINARY_DIR}/sdkconfig.combined.in ${CMAKE_BINARY_DIR}/sdkconfig.combined COPYONLY) set(SDKCONFIG_DEFAULTS ${CMAKE_BINARY_DIR}/sdkconfig.combined) +# Include main IDF cmake file. +include($ENV{IDF_PATH}/tools/cmake/project.cmake) + +# Set the location of the main component for the project (one per target). +set(EXTRA_COMPONENT_DIRS main_${IDF_TARGET}) + # Define the project. project(micropython) diff --git a/ports/esp32/README.md b/ports/esp32/README.md index c37213b303109..dc9e9f8b723ac 100644 --- a/ports/esp32/README.md +++ b/ports/esp32/README.md @@ -22,13 +22,13 @@ Initial development of this ESP32 port was sponsored in part by Microbric Pty Lt Setting up ESP-IDF and the build environment -------------------------------------------- -MicroPython on ESP32 requires the Espressif IDF version 4 (IoT development +MicroPython on ESP32 requires the Espressif IDF version 5 (IoT development framework, aka SDK). The ESP-IDF includes the libraries and RTOS needed to 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 v4.0.2, v4.1.1, v4.2.2, v4.3.2 and v4.4, +Currently MicroPython supports v5.0.2, although other IDF v4 versions may also work. To install the ESP-IDF the full instructions can be found at the @@ -47,10 +47,10 @@ The steps to take are summarised below. To check out a copy of the IDF use git clone: ```bash -$ git clone -b v4.0.2 --recursive https://github.com/espressif/esp-idf.git +$ git clone -b v5.0.2 --recursive https://github.com/espressif/esp-idf.git ``` -You can replace `v4.0.2` with `v4.2.2` or `v4.4` or any other supported version. +You can replace `v5.0.2` with any other supported version. (You don't need a full recursive clone; see the `ci_esp32_setup` function in `tools/ci.sh` in this repository for more detailed set-up commands.) @@ -59,7 +59,7 @@ MicroPython and update the submodules using: ```bash $ cd esp-idf -$ git checkout v4.2 +$ git checkout v5.0.2 $ git submodule update --init --recursive ``` @@ -75,11 +75,6 @@ $ source export.sh # (or export.bat on Windows) The `install.sh` step only needs to be done once. You will need to source `export.sh` for every new session. -**Note:** If you are building MicroPython for the ESP32-S2, ESP32-C3 or ESP32-S3, -please ensure you are using the following required IDF versions: -- ESP32-S3 currently requires `v4.4` or later. -- ESP32-S2 and ESP32-C3 require `v4.3.1` or later. - Building the firmware --------------------- diff --git a/ports/esp32/boards/ESP32_S2_WROVER/sdkconfig.board b/ports/esp32/boards/ESP32_S2_WROVER/sdkconfig.board index 9373a522324db..ea263cad9f4af 100644 --- a/ports/esp32/boards/ESP32_S2_WROVER/sdkconfig.board +++ b/ports/esp32/boards/ESP32_S2_WROVER/sdkconfig.board @@ -1,6 +1,5 @@ CONFIG_FLASHMODE_QIO=y CONFIG_ESPTOOLPY_FLASHFREQ_80M=y -CONFIG_ESPTOOLPY_FLASHSIZE_DETECT=y CONFIG_ESPTOOLPY_AFTER_NORESET=y CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y diff --git a/ports/esp32/boards/GENERIC_C3_USB/sdkconfig.board b/ports/esp32/boards/GENERIC_C3_USB/sdkconfig.board index f0cbad00e4617..d9e7c7f61f620 100644 --- a/ports/esp32/boards/GENERIC_C3_USB/sdkconfig.board +++ b/ports/esp32/boards/GENERIC_C3_USB/sdkconfig.board @@ -1,9 +1,7 @@ CONFIG_ESP32C3_REV_MIN_3=y -CONFIG_ESP32C3_REV_MIN=3 CONFIG_ESP32C3_BROWNOUT_DET=y CONFIG_ESP32C3_BROWNOUT_DET_LVL_SEL_7= CONFIG_ESP32C3_BROWNOUT_DET_LVL_SEL_4=y CONFIG_ESP32C3_BROWNOUT_DET_LVL=4 CONFIG_ESP_CONSOLE_UART_DEFAULT= -CONFIG_ESP_CONSOLE_USB_CDC= CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG=y diff --git a/ports/esp32/boards/GENERIC_D2WD/sdkconfig.board b/ports/esp32/boards/GENERIC_D2WD/sdkconfig.board index 07e208a09ae7b..7b4313b7deef1 100644 --- a/ports/esp32/boards/GENERIC_D2WD/sdkconfig.board +++ b/ports/esp32/boards/GENERIC_D2WD/sdkconfig.board @@ -1,6 +1,7 @@ # Optimise using -Os to reduce size CONFIG_COMPILER_OPTIMIZATION_SIZE=y CONFIG_COMPILER_OPTIMIZATION_PERF=n +CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT=y CONFIG_ESPTOOLPY_FLASHMODE_DIO=y CONFIG_ESPTOOLPY_FLASHFREQ_40M=y diff --git a/ports/esp32/boards/GENERIC_S3/sdkconfig.board b/ports/esp32/boards/GENERIC_S3/sdkconfig.board index c9726d4232ed4..7b9d932503e55 100644 --- a/ports/esp32/boards/GENERIC_S3/sdkconfig.board +++ b/ports/esp32/boards/GENERIC_S3/sdkconfig.board @@ -1,10 +1,7 @@ CONFIG_FLASHMODE_QIO=y CONFIG_ESPTOOLPY_FLASHFREQ_80M=y -CONFIG_ESPTOOLPY_FLASHSIZE_DETECT=y CONFIG_ESPTOOLPY_AFTER_NORESET=y -CONFIG_SPIRAM_MEMTEST= - CONFIG_ESPTOOLPY_FLASHSIZE_4MB= CONFIG_ESPTOOLPY_FLASHSIZE_8MB=y CONFIG_ESPTOOLPY_FLASHSIZE_16MB= diff --git a/ports/esp32/boards/GENERIC_S3_SPIRAM/sdkconfig.board b/ports/esp32/boards/GENERIC_S3_SPIRAM/sdkconfig.board index c9726d4232ed4..7b9d932503e55 100644 --- a/ports/esp32/boards/GENERIC_S3_SPIRAM/sdkconfig.board +++ b/ports/esp32/boards/GENERIC_S3_SPIRAM/sdkconfig.board @@ -1,10 +1,7 @@ CONFIG_FLASHMODE_QIO=y CONFIG_ESPTOOLPY_FLASHFREQ_80M=y -CONFIG_ESPTOOLPY_FLASHSIZE_DETECT=y CONFIG_ESPTOOLPY_AFTER_NORESET=y -CONFIG_SPIRAM_MEMTEST= - CONFIG_ESPTOOLPY_FLASHSIZE_4MB= CONFIG_ESPTOOLPY_FLASHSIZE_8MB=y CONFIG_ESPTOOLPY_FLASHSIZE_16MB= diff --git a/ports/esp32/boards/GENERIC_S3_SPIRAM_OCT/sdkconfig.board b/ports/esp32/boards/GENERIC_S3_SPIRAM_OCT/sdkconfig.board index c9726d4232ed4..7b9d932503e55 100644 --- a/ports/esp32/boards/GENERIC_S3_SPIRAM_OCT/sdkconfig.board +++ b/ports/esp32/boards/GENERIC_S3_SPIRAM_OCT/sdkconfig.board @@ -1,10 +1,7 @@ CONFIG_FLASHMODE_QIO=y CONFIG_ESPTOOLPY_FLASHFREQ_80M=y -CONFIG_ESPTOOLPY_FLASHSIZE_DETECT=y CONFIG_ESPTOOLPY_AFTER_NORESET=y -CONFIG_SPIRAM_MEMTEST= - CONFIG_ESPTOOLPY_FLASHSIZE_4MB= CONFIG_ESPTOOLPY_FLASHSIZE_8MB=y CONFIG_ESPTOOLPY_FLASHSIZE_16MB= diff --git a/ports/esp32/boards/GENERIC_SPIRAM/mpconfigboard.cmake b/ports/esp32/boards/GENERIC_SPIRAM/mpconfigboard.cmake index dcffe5f01e7a0..2e1d799b93b1c 100644 --- a/ports/esp32/boards/GENERIC_SPIRAM/mpconfigboard.cmake +++ b/ports/esp32/boards/GENERIC_SPIRAM/mpconfigboard.cmake @@ -2,5 +2,4 @@ set(SDKCONFIG_DEFAULTS boards/sdkconfig.base boards/sdkconfig.ble boards/sdkconfig.spiram - boards/GENERIC_SPIRAM/sdkconfig.board ) diff --git a/ports/esp32/boards/GENERIC_SPIRAM/sdkconfig.board b/ports/esp32/boards/GENERIC_SPIRAM/sdkconfig.board deleted file mode 100644 index a2859acb5f906..0000000000000 --- a/ports/esp32/boards/GENERIC_SPIRAM/sdkconfig.board +++ /dev/null @@ -1,2 +0,0 @@ -# SPIRAM increases the size of the firmware, use -Os to reduce it again to fit in iram -CONFIG_COMPILER_OPTIMIZATION_SIZE=y diff --git a/ports/esp32/boards/LOLIN_C3_MINI/sdkconfig.board b/ports/esp32/boards/LOLIN_C3_MINI/sdkconfig.board index f0cbad00e4617..d9e7c7f61f620 100644 --- a/ports/esp32/boards/LOLIN_C3_MINI/sdkconfig.board +++ b/ports/esp32/boards/LOLIN_C3_MINI/sdkconfig.board @@ -1,9 +1,7 @@ CONFIG_ESP32C3_REV_MIN_3=y -CONFIG_ESP32C3_REV_MIN=3 CONFIG_ESP32C3_BROWNOUT_DET=y CONFIG_ESP32C3_BROWNOUT_DET_LVL_SEL_7= CONFIG_ESP32C3_BROWNOUT_DET_LVL_SEL_4=y CONFIG_ESP32C3_BROWNOUT_DET_LVL=4 CONFIG_ESP_CONSOLE_UART_DEFAULT= -CONFIG_ESP_CONSOLE_USB_CDC= CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG=y diff --git a/ports/esp32/boards/UM_FEATHERS2/sdkconfig.board b/ports/esp32/boards/UM_FEATHERS2/sdkconfig.board index ccda7bff68129..88bc9e72aca76 100644 --- a/ports/esp32/boards/UM_FEATHERS2/sdkconfig.board +++ b/ports/esp32/boards/UM_FEATHERS2/sdkconfig.board @@ -1,15 +1,11 @@ CONFIG_FLASHMODE_QIO=y CONFIG_ESPTOOLPY_FLASHFREQ_80M=y -CONFIG_ESPTOOLPY_FLASHSIZE_DETECT=y CONFIG_ESPTOOLPY_AFTER_NORESET=y -CONFIG_SPIRAM_MEMTEST= - CONFIG_ESPTOOLPY_FLASHSIZE_4MB= CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y CONFIG_PARTITION_TABLE_CUSTOM=y CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions-16MiB.csv" -#CONFIG_USB_AND_UART=y # LWIP CONFIG_LWIP_LOCAL_HOSTNAME="UMFeatherS2" diff --git a/ports/esp32/boards/UM_FEATHERS2NEO/sdkconfig.board b/ports/esp32/boards/UM_FEATHERS2NEO/sdkconfig.board index 556de1f8b4227..87a92892d07e1 100644 --- a/ports/esp32/boards/UM_FEATHERS2NEO/sdkconfig.board +++ b/ports/esp32/boards/UM_FEATHERS2NEO/sdkconfig.board @@ -3,8 +3,6 @@ CONFIG_ESPTOOLPY_FLASHFREQ_80M=y CONFIG_USB_AND_UART=y CONFIG_ESPTOOLPY_AFTER_NORESET=y -CONFIG_SPIRAM_MEMTEST= - # LWIP CONFIG_LWIP_LOCAL_HOSTNAME="UMFeatherS2Neo" # end of LWIP diff --git a/ports/esp32/boards/UM_FEATHERS3/sdkconfig.board b/ports/esp32/boards/UM_FEATHERS3/sdkconfig.board index 5e20045125463..a4a167de8a748 100644 --- a/ports/esp32/boards/UM_FEATHERS3/sdkconfig.board +++ b/ports/esp32/boards/UM_FEATHERS3/sdkconfig.board @@ -1,10 +1,7 @@ CONFIG_FLASHMODE_QIO=y CONFIG_ESPTOOLPY_FLASHFREQ_80M=y -CONFIG_ESPTOOLPY_FLASHSIZE_DETECT=y CONFIG_ESPTOOLPY_AFTER_NORESET=y -CONFIG_SPIRAM_MEMTEST= - CONFIG_ESPTOOLPY_FLASHSIZE_4MB= CONFIG_ESPTOOLPY_FLASHSIZE_8MB= CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y @@ -13,11 +10,9 @@ CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions-16MiB.csv" CONFIG_LWIP_LOCAL_HOSTNAME="UMFeatherS3" -# CONFIG_TINYUSB_DESC_USE_ESPRESSIF_VID is not set CONFIG_TINYUSB_DESC_CUSTOM_VID=0x303A -# CONFIG_TINYUSB_DESC_USE_DEFAULT_PID is not set CONFIG_TINYUSB_DESC_CUSTOM_PID=0x80D7 CONFIG_TINYUSB_DESC_BCD_DEVICE=0x0100 CONFIG_TINYUSB_DESC_MANUFACTURER_STRING="Unexpected Maker" CONFIG_TINYUSB_DESC_PRODUCT_STRING="FeatherS3" -CONFIG_TINYUSB_DESC_SERIAL_STRING="_fs3_" \ No newline at end of file +CONFIG_TINYUSB_DESC_SERIAL_STRING="_fs3_" diff --git a/ports/esp32/boards/UM_PROS3/sdkconfig.board b/ports/esp32/boards/UM_PROS3/sdkconfig.board index 06b3a00a1c5a4..74ca6df8f6686 100644 --- a/ports/esp32/boards/UM_PROS3/sdkconfig.board +++ b/ports/esp32/boards/UM_PROS3/sdkconfig.board @@ -1,10 +1,7 @@ CONFIG_FLASHMODE_QIO=y CONFIG_ESPTOOLPY_FLASHFREQ_80M=y -CONFIG_ESPTOOLPY_FLASHSIZE_DETECT=y CONFIG_ESPTOOLPY_AFTER_NORESET=y -CONFIG_SPIRAM_MEMTEST= - CONFIG_ESPTOOLPY_FLASHSIZE_4MB= CONFIG_ESPTOOLPY_FLASHSIZE_8MB= CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y @@ -13,9 +10,7 @@ CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions-16MiB.csv" CONFIG_LWIP_LOCAL_HOSTNAME="UMProS3" -# CONFIG_TINYUSB_DESC_USE_ESPRESSIF_VID is not set CONFIG_TINYUSB_DESC_CUSTOM_VID=0x303A -# CONFIG_TINYUSB_DESC_USE_DEFAULT_PID is not set CONFIG_TINYUSB_DESC_CUSTOM_PID=0x80D4 CONFIG_TINYUSB_DESC_BCD_DEVICE=0x0100 CONFIG_TINYUSB_DESC_MANUFACTURER_STRING="Unexpected Maker" diff --git a/ports/esp32/boards/UM_TINYPICO/sdkconfig.board b/ports/esp32/boards/UM_TINYPICO/sdkconfig.board index 8ed083e62d1a4..766419c7f81a3 100644 --- a/ports/esp32/boards/UM_TINYPICO/sdkconfig.board +++ b/ports/esp32/boards/UM_TINYPICO/sdkconfig.board @@ -3,6 +3,3 @@ CONFIG_ESPTOOLPY_FLASHFREQ_80M=y CONFIG_SPIRAM_SPEED_80M=y CONFIG_ESP32_REV_MIN_1=y CONFIG_LWIP_LOCAL_HOSTNAME="UMTinyPICO" - -# SPIRAM increases the size of the firmware, use -Os to reduce it again to fit in iram -CONFIG_COMPILER_OPTIMIZATION_SIZE=y diff --git a/ports/esp32/boards/UM_TINYS2/sdkconfig.board b/ports/esp32/boards/UM_TINYS2/sdkconfig.board index 39a2a293acc8c..5e129e4d8e33f 100644 --- a/ports/esp32/boards/UM_TINYS2/sdkconfig.board +++ b/ports/esp32/boards/UM_TINYS2/sdkconfig.board @@ -3,8 +3,6 @@ CONFIG_ESPTOOLPY_FLASHFREQ_80M=y CONFIG_USB_AND_UART=y CONFIG_ESPTOOLPY_AFTER_NORESET=y -CONFIG_SPIRAM_MEMTEST= - # LWIP CONFIG_LWIP_LOCAL_HOSTNAME="UMTinyS2" # end of LWIP diff --git a/ports/esp32/boards/UM_TINYS3/sdkconfig.board b/ports/esp32/boards/UM_TINYS3/sdkconfig.board index 2b9ddbebe7b5d..0ac3c1f4cb157 100644 --- a/ports/esp32/boards/UM_TINYS3/sdkconfig.board +++ b/ports/esp32/boards/UM_TINYS3/sdkconfig.board @@ -1,10 +1,7 @@ CONFIG_FLASHMODE_QIO=y CONFIG_ESPTOOLPY_FLASHFREQ_80M=y -CONFIG_ESPTOOLPY_FLASHSIZE_DETECT=y CONFIG_ESPTOOLPY_AFTER_NORESET=y -CONFIG_SPIRAM_MEMTEST= - CONFIG_ESPTOOLPY_FLASHSIZE_4MB= CONFIG_ESPTOOLPY_FLASHSIZE_8MB=y CONFIG_ESPTOOLPY_FLASHSIZE_16MB= @@ -13,9 +10,7 @@ CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions-8MiB.csv" CONFIG_LWIP_LOCAL_HOSTNAME="UMTinyS3" -# CONFIG_TINYUSB_DESC_USE_ESPRESSIF_VID is not set CONFIG_TINYUSB_DESC_CUSTOM_VID=0x303A -# CONFIG_TINYUSB_DESC_USE_DEFAULT_PID is not set CONFIG_TINYUSB_DESC_CUSTOM_PID=0x80D1 CONFIG_TINYUSB_DESC_BCD_DEVICE=0x0100 CONFIG_TINYUSB_DESC_MANUFACTURER_STRING="Unexpected Maker" diff --git a/ports/esp32/boards/sdkconfig.240mhz b/ports/esp32/boards/sdkconfig.240mhz index e36884009d1ed..c89a1ed251899 100644 --- a/ports/esp32/boards/sdkconfig.240mhz +++ b/ports/esp32/boards/sdkconfig.240mhz @@ -1,5 +1,6 @@ # MicroPython on ESP32, ESP IDF configuration with 240MHz CPU -CONFIG_ESP32_DEFAULT_CPU_FREQ_80= -CONFIG_ESP32_DEFAULT_CPU_FREQ_160= -CONFIG_ESP32_DEFAULT_CPU_FREQ_240=y -CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ=240 +CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_40= +CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_80= +CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_160= +CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ_240=y +CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ=240 diff --git a/ports/esp32/boards/sdkconfig.base b/ports/esp32/boards/sdkconfig.base index 138de095b45f2..c74a19c0cf2aa 100644 --- a/ports/esp32/boards/sdkconfig.base +++ b/ports/esp32/boards/sdkconfig.base @@ -4,8 +4,6 @@ CONFIG_IDF_FIRMWARE_CHIP_ID=0x0000 # Compiler options: use -O2 and disable assertions to improve performance -# (CONFIG_COMPILER_OPTIMIZATION_LEVEL_RELEASE is for IDF 4.0.2) -CONFIG_COMPILER_OPTIMIZATION_LEVEL_RELEASE=y CONFIG_COMPILER_OPTIMIZATION_PERF=y CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE=y @@ -21,10 +19,14 @@ CONFIG_LOG_DEFAULT_LEVEL_INFO=n CONFIG_LOG_DEFAULT_LEVEL_ERROR=y CONFIG_LOG_DEFAULT_LEVEL=1 -# ESP32-specific +# Main XTAL Config +# Only on: ESP32 +CONFIG_XTAL_FREQ_AUTO=y + +# ESP System Settings +# Only on: ESP32, ESP32S3 CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0=n CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1=n -CONFIG_ESP32_XTAL_FREQ_AUTO=y # Power Management CONFIG_PM_ENABLE=y @@ -44,16 +46,14 @@ CONFIG_LWIP_PPP_PAP_SUPPORT=y CONFIG_LWIP_PPP_CHAP_SUPPORT=y # SSL -# Use 4kiB output buffer instead of default 16kiB (because IDF heap is fragmented in 4.0) +# Use 4kiB output buffer instead of default 16kiB CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN=y # ULP coprocessor support -CONFIG_ESP32_ULP_COPROC_ENABLED=y -CONFIG_ESP32_ULP_COPROC_RESERVE_MEM=2040 -CONFIG_ESP32S2_ULP_COPROC_ENABLED=y -CONFIG_ESP32S2_ULP_COPROC_RESERVE_MEM=2040 -CONFIG_ESP32S3_ULP_COPROC_ENABLED=y -CONFIG_ESP32S3_ULP_COPROC_RESERVE_MEM=2040 +# Only on: ESP32, ESP32S2, ESP32S3 +CONFIG_ULP_COPROC_ENABLED=y +CONFIG_ULP_COPROC_TYPE_FSM=y +CONFIG_ULP_COPROC_RESERVE_MEM=2040 # For cmake build CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y @@ -64,7 +64,17 @@ CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv" CONFIG_ESP32_WIFI_IRAM_OPT=n CONFIG_ESP32_WIFI_RX_IRAM_OPT=n -# ADC calibration +# Legacy ADC Calibration Configuration +# Only on: ESP32 CONFIG_ADC_CAL_EFUSE_TP_ENABLE=y CONFIG_ADC_CAL_EFUSE_VREF_ENABLE=y CONFIG_ADC_CAL_LUT_ENABLE=y + +# UART Configuration +CONFIG_UART_ISR_IN_IRAM=y + +# IDF 5 deprecated +CONFIG_ADC_SUPPRESS_DEPRECATE_WARN=y +CONFIG_GPTIMER_SUPPRESS_DEPRECATE_WARN=y +CONFIG_RMT_SUPPRESS_DEPRECATE_WARN=y +CONFIG_I2S_SUPPRESS_DEPRECATE_WARN=y diff --git a/ports/esp32/boards/sdkconfig.ble b/ports/esp32/boards/sdkconfig.ble index 08d5e481f4993..91ac3240ebee7 100644 --- a/ports/esp32/boards/sdkconfig.ble +++ b/ports/esp32/boards/sdkconfig.ble @@ -1,9 +1,14 @@ -# Note this requires building with IDF 4.x +CONFIG_BT_NIMBLE_LOG_LEVEL_ERROR=y CONFIG_BT_ENABLED=y -CONFIG_BTDM_CTRL_MODE_BLE_ONLY=y -CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY= -CONFIG_BTDM_CTRL_MODE_BTDM= - CONFIG_BT_NIMBLE_ENABLED=y +CONFIG_BT_CONTROLLER_ENABLED=y CONFIG_BT_NIMBLE_MAX_CONNECTIONS=4 + +# Put NimBLE on core 1, and for synchronisation +# with the ringbuffer and scheduler MP needs to be on the same core. +# MP on core 1 prevents interference with WiFi for time sensitive operations. +# Only on: ESP32, ESP32S2, ESP32S3 +CONFIG_BT_NIMBLE_PINNED_TO_CORE_0=n +CONFIG_BT_NIMBLE_PINNED_TO_CORE_1=y +CONFIG_BT_NIMBLE_PINNED_TO_CORE=1 diff --git a/ports/esp32/boards/sdkconfig.nimble_core0 b/ports/esp32/boards/sdkconfig.nimble_core0 deleted file mode 100644 index cacaff1197cb3..0000000000000 --- a/ports/esp32/boards/sdkconfig.nimble_core0 +++ /dev/null @@ -1,6 +0,0 @@ -# For IDF <4.2, we need NimBLE on core 0, and for synchronisation -# with the ringbuffer and scheduler MP needs to be on the same core. -# See https://github.com/micropython/micropython/issues/5489 -CONFIG_BT_NIMBLE_PINNED_TO_CORE_0=y -CONFIG_BT_NIMBLE_PINNED_TO_CORE_1=n -CONFIG_BT_NIMBLE_PINNED_TO_CORE=0 diff --git a/ports/esp32/boards/sdkconfig.nimble_core1 b/ports/esp32/boards/sdkconfig.nimble_core1 deleted file mode 100644 index 33653cc4b164c..0000000000000 --- a/ports/esp32/boards/sdkconfig.nimble_core1 +++ /dev/null @@ -1,6 +0,0 @@ -# For IDF >=4.2, we are able to put NimBLE on core 1, and for synchronisation -# with the ringbuffer and scheduler MP needs to be on the same core. -# MP on core 1 prevents interference with WiFi for time sensitive operations. -CONFIG_BT_NIMBLE_PINNED_TO_CORE_0=n -CONFIG_BT_NIMBLE_PINNED_TO_CORE_1=y -CONFIG_BT_NIMBLE_PINNED_TO_CORE=1 diff --git a/ports/esp32/boards/sdkconfig.spiram b/ports/esp32/boards/sdkconfig.spiram index 5b4ce118b890e..74d35f7b4ad4a 100644 --- a/ports/esp32/boards/sdkconfig.spiram +++ b/ports/esp32/boards/sdkconfig.spiram @@ -1,6 +1,11 @@ # MicroPython on ESP32, ESP IDF configuration with SPIRAM support -CONFIG_ESP32_SPIRAM_SUPPORT=y +CONFIG_SPIRAM=y CONFIG_SPIRAM_CACHE_WORKAROUND=y CONFIG_SPIRAM_IGNORE_NOTFOUND=y -CONFIG_SPIRAM_USE_MEMMAP=y +CONFIG_SPIRAM_USE_CAPS_ALLOC=y + +# SPIRAM increases the size of the firmware and overflows iram0_0_seg, due +# to PSRAM bug workarounds. Apply some options to reduce the firmware size. +CONFIG_COMPILER_OPTIMIZATION_SIZE=y +CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH=y diff --git a/ports/esp32/boards/sdkconfig.spiram_sx b/ports/esp32/boards/sdkconfig.spiram_sx index ef24e90829e16..fe388467800e2 100644 --- a/ports/esp32/boards/sdkconfig.spiram_sx +++ b/ports/esp32/boards/sdkconfig.spiram_sx @@ -1,6 +1,4 @@ # MicroPython on ESP32-S2 and ESP32-PAD1_subscript_3, ESP IDF configuration with SPIRAM support -CONFIG_ESP32S2_SPIRAM_SUPPORT=y -CONFIG_ESP32S3_SPIRAM_SUPPORT=y CONFIG_SPIRAM_MODE_QUAD=y CONFIG_SPIRAM_TYPE_AUTO=y CONFIG_DEFAULT_PSRAM_CLK_IO=30 @@ -9,7 +7,4 @@ CONFIG_SPIRAM_SPEED_80M=y CONFIG_SPIRAM=y CONFIG_SPIRAM_BOOT_INIT=y CONFIG_SPIRAM_IGNORE_NOTFOUND=y -CONFIG_SPIRAM_USE_MEMMAP=y -CONFIG_SPIRAM_MEMTEST=y -CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=16384 -CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL=32768 +CONFIG_SPIRAM_USE_CAPS_ALLOC=y diff --git a/ports/esp32/boards/sdkconfig.usb b/ports/esp32/boards/sdkconfig.usb index 657edbc58059b..4090c710e62e6 100644 --- a/ports/esp32/boards/sdkconfig.usb +++ b/ports/esp32/boards/sdkconfig.usb @@ -1,4 +1,4 @@ -CONFIG_USB_ENABLED=y -CONFIG_USB_CDC_ENABLED=y -CONFIG_USB_CDC_RX_BUFSIZE=256 -CONFIG_USB_CDC_TX_BUFSIZE=256 +CONFIG_USB_OTG_SUPPORTED=y +CONFIG_TINYUSB_CDC_ENABLED=y +CONFIG_TINYUSB_CDC_RX_BUFSIZE=256 +CONFIG_TINYUSB_CDC_TX_BUFSIZE=256 diff --git a/ports/esp32/main/CMakeLists.txt b/ports/esp32/esp32_common.cmake similarity index 69% rename from ports/esp32/main/CMakeLists.txt rename to ports/esp32/esp32_common.cmake index 4311af801c0f3..d55dd38134fb8 100644 --- a/ports/esp32/main/CMakeLists.txt +++ b/ports/esp32/esp32_common.cmake @@ -1,8 +1,9 @@ # Set location of base MicroPython directory. if(NOT MICROPY_DIR) - get_filename_component(MICROPY_DIR ${CMAKE_CURRENT_LIST_DIR}/../../.. ABSOLUTE) + get_filename_component(MICROPY_DIR ${CMAKE_CURRENT_LIST_DIR}/../.. ABSOLUTE) endif() +# Set location of the ESP32 port directory. if(NOT MICROPY_PORT_DIR) get_filename_component(MICROPY_PORT_DIR ${MICROPY_DIR}/ports/esp32 ABSOLUTE) endif() @@ -19,11 +20,11 @@ if(NOT CMAKE_BUILD_EARLY_EXPANSION) include(${MICROPY_DIR}/extmod/extmod.cmake) endif() -set(MICROPY_QSTRDEFS_PORT +list(APPEND MICROPY_QSTRDEFS_PORT ${MICROPY_PORT_DIR}/qstrdefsport.h ) -set(MICROPY_SOURCE_SHARED +list(APPEND MICROPY_SOURCE_SHARED ${MICROPY_DIR}/shared/readline/readline.c ${MICROPY_DIR}/shared/netutils/netutils.c ${MICROPY_DIR}/shared/timeutils/timeutils.c @@ -33,25 +34,22 @@ set(MICROPY_SOURCE_SHARED ${MICROPY_DIR}/shared/runtime/pyexec.c ) -set(MICROPY_SOURCE_LIB +list(APPEND MICROPY_SOURCE_LIB ${MICROPY_DIR}/lib/littlefs/lfs1.c ${MICROPY_DIR}/lib/littlefs/lfs1_util.c ${MICROPY_DIR}/lib/littlefs/lfs2.c ${MICROPY_DIR}/lib/littlefs/lfs2_util.c - ${MICROPY_DIR}/lib/mbedtls_errors/esp32_mbedtls_errors.c + #${MICROPY_DIR}/lib/mbedtls_errors/esp32_mbedtls_errors.c ${MICROPY_DIR}/lib/oofatfs/ff.c ${MICROPY_DIR}/lib/oofatfs/ffunicode.c ) -if(IDF_TARGET STREQUAL "esp32c3") - list(APPEND MICROPY_SOURCE_LIB ${MICROPY_DIR}/shared/runtime/gchelper_generic.c) -endif() -set(MICROPY_SOURCE_DRIVERS +list(APPEND MICROPY_SOURCE_DRIVERS ${MICROPY_DIR}/drivers/bus/softspi.c ${MICROPY_DIR}/drivers/dht/dht.c ) -set(MICROPY_SOURCE_PORT +list(APPEND MICROPY_SOURCE_PORT main.c ppp_set_auth.c uart.c @@ -93,7 +91,7 @@ set(MICROPY_SOURCE_PORT ) list(TRANSFORM MICROPY_SOURCE_PORT PREPEND ${MICROPY_PORT_DIR}/) -set(MICROPY_SOURCE_QSTR +list(APPEND MICROPY_SOURCE_QSTR ${MICROPY_SOURCE_PY} ${MICROPY_SOURCE_EXTMOD} ${MICROPY_SOURCE_USERMOD} @@ -103,63 +101,42 @@ set(MICROPY_SOURCE_QSTR ${MICROPY_SOURCE_BOARD} ) -set(IDF_COMPONENTS +list(APPEND IDF_COMPONENTS app_update bootloader_support bt driver - esp_adc_cal + esp_adc + esp_app_format esp_common esp_eth esp_event + esp_hw_support + esp_netif + esp_partition + esp_pm + esp_psram esp_ringbuf esp_rom + esp_system + esp_timer esp_wifi freertos + hal heap log lwip mbedtls - mdns newlib nvs_flash sdmmc soc spi_flash - tcpip_adapter ulp vfs xtensa ) -if(IDF_VERSION_MINOR GREATER_EQUAL 1 OR IDF_VERSION_MAJOR GREATER_EQUAL 5) - list(APPEND IDF_COMPONENTS esp_netif) -endif() - -if(IDF_VERSION_MINOR GREATER_EQUAL 2 OR IDF_VERSION_MAJOR GREATER_EQUAL 5) - list(APPEND IDF_COMPONENTS esp_system) - list(APPEND IDF_COMPONENTS esp_timer) -endif() - -if(IDF_VERSION_MINOR GREATER_EQUAL 3 OR IDF_VERSION_MAJOR GREATER_EQUAL 5) - list(APPEND IDF_COMPONENTS esp_hw_support) - list(APPEND IDF_COMPONENTS esp_pm) - list(APPEND IDF_COMPONENTS hal) -endif() - -if(IDF_TARGET STREQUAL "esp32") - list(APPEND IDF_COMPONENTS esp32) -elseif(IDF_TARGET STREQUAL "esp32c3") - list(APPEND IDF_COMPONENTS esp32c3) - list(APPEND IDF_COMPONENTS riscv) -elseif(IDF_TARGET STREQUAL "esp32s2") - list(APPEND IDF_COMPONENTS esp32s2) - list(APPEND IDF_COMPONENTS tinyusb) -elseif(IDF_TARGET STREQUAL "esp32s3") - list(APPEND IDF_COMPONENTS esp32s3) - list(APPEND IDF_COMPONENTS tinyusb) -endif() - # Register the main IDF component. idf_component_register( SRCS @@ -216,18 +193,8 @@ target_link_libraries(${MICROPY_TARGET} usermod) # Collect all of the include directories and compile definitions for the IDF components. foreach(comp ${IDF_COMPONENTS}) micropy_gather_target_properties(__idf_${comp}) + micropy_gather_target_properties(${comp}) endforeach() -if(IDF_VERSION_MINOR GREATER_EQUAL 2 OR IDF_VERSION_MAJOR GREATER_EQUAL 5) - # These paths cannot currently be found by the IDF_COMPONENTS search loop above, - # so add them explicitly. - list(APPEND MICROPY_CPP_INC_EXTRA ${IDF_PATH}/components/soc/soc/${IDF_TARGET}/include) - list(APPEND MICROPY_CPP_INC_EXTRA ${IDF_PATH}/components/soc/soc/include) - if(IDF_VERSION_MINOR GREATER_EQUAL 3) - list(APPEND MICROPY_CPP_INC_EXTRA ${IDF_PATH}/components/tinyusb/additions/include) - list(APPEND MICROPY_CPP_INC_EXTRA ${IDF_PATH}/components/tinyusb/tinyusb/src) - endif() -endif() - # Include the main MicroPython cmake rules. include(${MICROPY_DIR}/py/mkrules.cmake) diff --git a/ports/esp32/esp32_rmt.c b/ports/esp32/esp32_rmt.c index 3cd43e8474e66..f92af636f55a2 100644 --- a/ports/esp32/esp32_rmt.c +++ b/ports/esp32/esp32_rmt.c @@ -48,11 +48,7 @@ // and carrier output. // Last available RMT channel that can transmit. -#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 4, 0) -#define RMT_LAST_TX_CHANNEL (RMT_CHANNEL_MAX - 1) -#else #define RMT_LAST_TX_CHANNEL (SOC_RMT_TX_CANDIDATES_PER_GROUP - 1) -#endif // Forward declaration extern const mp_obj_type_t esp32_rmt_type; diff --git a/ports/esp32/esp32_ulp.c b/ports/esp32/esp32_ulp.c index 439ee3283683d..97041c60bd6be 100644 --- a/ports/esp32/esp32_ulp.c +++ b/ports/esp32/esp32_ulp.c @@ -35,7 +35,6 @@ #elif CONFIG_IDF_TARGET_ESP32S3 #include "esp32s3/ulp.h" #endif -#include "esp_err.h" typedef struct _esp32_ulp_obj_t { mp_obj_base_t base; @@ -93,13 +92,7 @@ STATIC const mp_rom_map_elem_t esp32_ulp_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_set_wakeup_period), MP_ROM_PTR(&esp32_ulp_set_wakeup_period_obj) }, { MP_ROM_QSTR(MP_QSTR_load_binary), MP_ROM_PTR(&esp32_ulp_load_binary_obj) }, { MP_ROM_QSTR(MP_QSTR_run), MP_ROM_PTR(&esp32_ulp_run_obj) }, - #if CONFIG_IDF_TARGET_ESP32 - { MP_ROM_QSTR(MP_QSTR_RESERVE_MEM), MP_ROM_INT(CONFIG_ESP32_ULP_COPROC_RESERVE_MEM) }, - #elif CONFIG_IDF_TARGET_ESP32S2 - { MP_ROM_QSTR(MP_QSTR_RESERVE_MEM), MP_ROM_INT(CONFIG_ESP32S2_ULP_COPROC_RESERVE_MEM) }, - #elif CONFIG_IDF_TARGET_ESP32S3 - { MP_ROM_QSTR(MP_QSTR_RESERVE_MEM), MP_ROM_INT(CONFIG_ESP32S3_ULP_COPROC_RESERVE_MEM) }, - #endif + { MP_ROM_QSTR(MP_QSTR_RESERVE_MEM), MP_ROM_INT(CONFIG_ULP_COPROC_RESERVE_MEM) }, }; STATIC MP_DEFINE_CONST_DICT(esp32_ulp_locals_dict, esp32_ulp_locals_dict_table); diff --git a/ports/esp32/gccollect.c b/ports/esp32/gccollect.c index 403a3c7dfa0fb..6fa287de28c0c 100644 --- a/ports/esp32/gccollect.c +++ b/ports/esp32/gccollect.c @@ -34,7 +34,6 @@ #include "py/gc.h" #include "py/mpthread.h" #include "gccollect.h" -#include "soc/cpu.h" #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 @@ -50,7 +49,7 @@ static void gc_collect_inner(int level) { if (level == XCHAL_NUM_AREGS / 8) { // get the sp - volatile uint32_t sp = (uint32_t)get_sp(); + volatile uint32_t sp = (uint32_t)esp_cpu_get_sp(); gc_collect_root((void **)sp, ((mp_uint_t)MP_STATE_THREAD(stack_top) - sp) / sizeof(uint32_t)); return; } diff --git a/ports/esp32/machine_adcblock.h b/ports/esp32/machine_adcblock.h index 0500726d71791..7c9249b072c18 100644 --- a/ports/esp32/machine_adcblock.h +++ b/ports/esp32/machine_adcblock.h @@ -3,6 +3,8 @@ #include "esp_adc_cal.h" +#define ADC_ATTEN_MAX SOC_ADC_ATTEN_NUM + typedef struct _madcblock_obj_t { mp_obj_base_t base; adc_unit_t unit_id; diff --git a/ports/esp32/machine_bitstream.c b/ports/esp32/machine_bitstream.c index 4284b5f8baf77..87a5ae53cfe54 100644 --- a/ports/esp32/machine_bitstream.c +++ b/ports/esp32/machine_bitstream.c @@ -28,6 +28,10 @@ #include "py/mphal.h" #include "modesp32.h" +#include "rom/gpio.h" +#include "soc/gpio_reg.h" +#include "soc/gpio_sig_map.h" + #if MICROPY_PY_MACHINE_BITSTREAM /******************************************************************************/ @@ -52,7 +56,7 @@ STATIC void IRAM_ATTR machine_bitstream_high_low_bitbang(mp_hal_pin_obj_t pin, u } // Convert ns to cpu ticks [high_time_0, period_0, high_time_1, period_1]. - uint32_t fcpu_mhz = ets_get_cpu_frequency(); + uint32_t fcpu_mhz = esp_rom_get_cpu_ticks_per_us(); for (size_t i = 0; i < 4; ++i) { timing_ns[i] = fcpu_mhz * timing_ns[i] / 1000; if (timing_ns[i] > NS_TICKS_OVERHEAD) { @@ -91,27 +95,6 @@ STATIC void IRAM_ATTR machine_bitstream_high_low_bitbang(mp_hal_pin_obj_t pin, u #include "driver/rmt.h" -#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 1, 0) -// This convenience macro was not available in earlier IDF versions. -#define RMT_DEFAULT_CONFIG_TX(gpio, channel_id) \ - { \ - .rmt_mode = RMT_MODE_TX, \ - .channel = channel_id, \ - .clk_div = 80, \ - .gpio_num = gpio, \ - .mem_block_num = 1, \ - .tx_config = { \ - .loop_en = false, \ - .carrier_freq_hz = 38000, \ - .carrier_duty_percent = 33, \ - .carrier_level = RMT_CARRIER_LEVEL_HIGH, \ - .carrier_en = false, \ - .idle_level = RMT_IDLE_LEVEL_LOW, \ - .idle_output_en = true, \ - } \ - } -#endif - // Logical 0 and 1 values (encoded as a rmt_item32_t). // The duration fields will be set later. STATIC rmt_item32_t bitstream_high_low_0 = {{{ 0, 1, 0, 0 }}}; @@ -163,13 +146,7 @@ STATIC void machine_bitstream_high_low_rmt(mp_hal_pin_obj_t pin, uint32_t *timin // Get the tick rate in kHz (this will likely be 40000). uint32_t counter_clk_khz = 0; - #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 1, 0) - uint8_t div_cnt; - check_esp_err(rmt_get_clk_div(config.channel, &div_cnt)); - counter_clk_khz = APB_CLK_FREQ / div_cnt; - #else check_esp_err(rmt_get_counter_clock(config.channel, &counter_clk_khz)); - #endif counter_clk_khz /= 1000; @@ -193,7 +170,7 @@ STATIC void machine_bitstream_high_low_rmt(mp_hal_pin_obj_t pin, uint32_t *timin check_esp_err(rmt_driver_uninstall(config.channel)); // Cancel RMT output to GPIO pin. - gpio_matrix_out(pin, SIG_GPIO_OUT_IDX, false, false); + esp_rom_gpio_connect_out_signal(pin, SIG_GPIO_OUT_IDX, false, false); } /******************************************************************************/ diff --git a/ports/esp32/machine_hw_spi.c b/ports/esp32/machine_hw_spi.c index 36f8d3f06956a..662d0e599419b 100644 --- a/ports/esp32/machine_hw_spi.c +++ b/ports/esp32/machine_hw_spi.c @@ -35,6 +35,8 @@ #include "modmachine.h" #include "driver/spi_master.h" +#include "soc/gpio_sig_map.h" +#include "soc/spi_pins.h" // SPI mappings by device, naming used by IDF old/new // upython | ESP32 | ESP32S2 | ESP32S3 | ESP32C3 @@ -57,9 +59,9 @@ #define MICROPY_HW_SPI1_MOSI FSPI_IOMUX_PIN_NUM_MOSI #define MICROPY_HW_SPI1_MISO FSPI_IOMUX_PIN_NUM_MISO #else -#define MICROPY_HW_SPI1_SCK HSPI_IOMUX_PIN_NUM_CLK -#define MICROPY_HW_SPI1_MOSI HSPI_IOMUX_PIN_NUM_MOSI -#define MICROPY_HW_SPI1_MISO HSPI_IOMUX_PIN_NUM_MISO +#define MICROPY_HW_SPI1_SCK SPI2_IOMUX_PIN_NUM_CLK +#define MICROPY_HW_SPI1_MOSI SPI2_IOMUX_PIN_NUM_MOSI +#define MICROPY_HW_SPI1_MISO SPI2_IOMUX_PIN_NUM_MISO #endif #endif @@ -67,9 +69,9 @@ #ifndef MICROPY_HW_SPI2_SCK #if CONFIG_IDF_TARGET_ESP32 // ESP32 has IO_MUX pins for VSPI/SPI3 lines, use them as defaults -#define MICROPY_HW_SPI2_SCK VSPI_IOMUX_PIN_NUM_CLK // pin 18 -#define MICROPY_HW_SPI2_MOSI VSPI_IOMUX_PIN_NUM_MOSI // pin 23 -#define MICROPY_HW_SPI2_MISO VSPI_IOMUX_PIN_NUM_MISO // pin 19 +#define MICROPY_HW_SPI2_SCK SPI3_IOMUX_PIN_NUM_CLK // pin 18 +#define MICROPY_HW_SPI2_MOSI SPI3_IOMUX_PIN_NUM_MOSI // pin 23 +#define MICROPY_HW_SPI2_MISO SPI3_IOMUX_PIN_NUM_MISO // pin 19 #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 // ESP32S2 and S3 uses GPIO matrix for SPI3 pins, no IO_MUX possible // Set defaults to the pins used by SPI2 in Octal mode @@ -83,9 +85,9 @@ #define MP_HW_SPI_MAX_XFER_BITS (MP_HW_SPI_MAX_XFER_BYTES * 8) // Has to be an even multiple of 8 #if CONFIG_IDF_TARGET_ESP32C3 -#define HSPI_HOST SPI2_HOST +#define SPI2_HOST SPI2_HOST #elif CONFIG_IDF_TARGET_ESP32S3 -#define HSPI_HOST SPI3_HOST +#define SPI2_HOST SPI3_HOST #define FSPI_HOST SPI2_HOST #endif @@ -122,7 +124,7 @@ STATIC const machine_hw_spi_default_pins_t machine_hw_spi_default_pins[2] = { #endif }; -// Static objects mapping to HSPI and VSPI hardware peripherals +// Static objects mapping to SPI2 and SPI3 hardware peripherals STATIC machine_hw_spi_obj_t machine_hw_spi_obj[2]; STATIC void machine_hw_spi_deinit_internal(machine_hw_spi_obj_t *self) { @@ -150,8 +152,8 @@ STATIC void machine_hw_spi_deinit_internal(machine_hw_spi_obj_t *self) { for (int i = 0; i < 3; i++) { if (pins[i] != -1) { - gpio_pad_select_gpio(pins[i]); - gpio_matrix_out(pins[i], SIG_GPIO_OUT_IDX, false, false); + esp_rom_gpio_pad_select_gpio(pins[i]); + esp_rom_gpio_connect_out_signal(pins[i], SIG_GPIO_OUT_IDX, false, false); gpio_set_direction(pins[i], GPIO_MODE_INPUT); } } @@ -226,12 +228,12 @@ STATIC void machine_hw_spi_init_internal( changed = true; } - if (self->host != HSPI_HOST + if (self->host != SPI2_HOST #ifdef FSPI_HOST && self->host != FSPI_HOST #endif - #ifdef VSPI_HOST - && self->host != VSPI_HOST + #ifdef SPI3_HOST + && self->host != SPI3_HOST #endif ) { mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) doesn't exist"), self->host); @@ -270,7 +272,7 @@ STATIC void machine_hw_spi_init_internal( #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 dma_chan = SPI_DMA_CH_AUTO; #else - if (self->host == HSPI_HOST) { + if (self->host == SPI2_HOST) { dma_chan = 1; } else { dma_chan = 2; @@ -483,7 +485,7 @@ mp_obj_t machine_hw_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_ machine_hw_spi_obj_t *self; const machine_hw_spi_default_pins_t *default_pins; - if (args[ARG_id].u_int == 1) { // SPI2_HOST which is FSPI_HOST on ESP32Sx, HSPI_HOST on others + if (args[ARG_id].u_int == 1) { // SPI2_HOST which is FSPI_HOST on ESP32Sx, SPI2_HOST on others self = &machine_hw_spi_obj[0]; default_pins = &machine_hw_spi_default_pins[0]; } else { diff --git a/ports/esp32/machine_i2c.c b/ports/esp32/machine_i2c.c index 9ec39e5649668..e3b76477902f9 100644 --- a/ports/esp32/machine_i2c.c +++ b/ports/esp32/machine_i2c.c @@ -31,13 +31,7 @@ #include "modmachine.h" #include "driver/i2c.h" - -#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0) #include "hal/i2c_ll.h" -#else -#include "soc/i2c_reg.h" -#define I2C_LL_MAX_TIMEOUT I2C_TIME_OUT_REG_V -#endif #ifndef MICROPY_HW_I2C0_SCL #define MICROPY_HW_I2C0_SCL (GPIO_NUM_18) @@ -125,7 +119,7 @@ int machine_hw_i2c_transfer(mp_obj_base_t *self_in, uint16_t addr, size_t n, mp_ } // TODO proper timeout - esp_err_t err = i2c_master_cmd_begin(self->port, cmd, 100 * (1 + data_len) / portTICK_RATE_MS); + esp_err_t err = i2c_master_cmd_begin(self->port, cmd, 100 * (1 + data_len) / portTICK_PERIOD_MS); i2c_cmd_link_delete(cmd); if (err == ESP_FAIL) { diff --git a/ports/esp32/machine_i2s.c b/ports/esp32/machine_i2s.c index f0163486cd73e..59b24ae70c3af 100644 --- a/ports/esp32/machine_i2s.c +++ b/ports/esp32/machine_i2s.c @@ -148,7 +148,7 @@ STATIC const int8_t i2s_frame_map[NUM_I2S_USER_FORMATS][I2S_RX_FRAME_SIZE_IN_BYT }; void machine_i2s_init0() { - for (i2s_port_t p = 0; p < I2S_NUM_MAX; p++) { + for (i2s_port_t p = 0; p < I2S_NUM_AUTO; p++) { MP_STATE_PORT(machine_i2s_obj)[p] = NULL; } } @@ -269,15 +269,6 @@ STATIC uint32_t fill_appbuf_from_dma(machine_i2s_obj_t *self, mp_buffer_info_t * num_bytes_requested_from_dma, &num_bytes_received_from_dma, delay); - - // the following is a workaround for a bug in ESP-IDF v4.4 - // https://github.com/espressif/esp-idf/issues/8121 - #if (ESP_IDF_VERSION_MAJOR == 4) && (ESP_IDF_VERSION_MINOR >= 4) - if ((delay != portMAX_DELAY) && (ret == ESP_ERR_TIMEOUT)) { - ret = ESP_OK; - } - #endif - check_esp_err(ret); // process the transform buffer one frame at a time. @@ -334,15 +325,6 @@ STATIC size_t copy_appbuf_to_dma(machine_i2s_obj_t *self, mp_buffer_info_t *appb } esp_err_t ret = i2s_write(self->port, appbuf->buf, appbuf->len, &num_bytes_written, delay); - - // the following is a workaround for a bug in ESP-IDF v4.4 - // https://github.com/espressif/esp-idf/issues/8121 - #if (ESP_IDF_VERSION_MAJOR == 4) && (ESP_IDF_VERSION_MINOR >= 4) - if ((delay != portMAX_DELAY) && (ret == ESP_ERR_TIMEOUT)) { - ret = ESP_OK; - } - #endif - check_esp_err(ret); if ((self->io_mode == ASYNCIO) && (num_bytes_written < appbuf->len)) { @@ -467,10 +449,8 @@ STATIC void machine_i2s_init_helper(machine_i2s_obj_t *self, size_t n_pos_args, i2s_config.use_apll = false; i2s_config.tx_desc_auto_clear = true; i2s_config.fixed_mclk = 0; - #if (ESP_IDF_VERSION_MAJOR == 4) && (ESP_IDF_VERSION_MINOR >= 4) - i2s_config.mclk_multiple = I2S_MCLK_MULTIPLE_DEFAULT; + i2s_config.mclk_multiple = I2S_MCLK_MULTIPLE_256; i2s_config.bits_per_chan = 0; - #endif // I2S queue size equals the number of DMA buffers check_esp_err(i2s_driver_install(self->port, &i2s_config, i2s_config.dma_buf_count, &self->i2s_event_queue)); @@ -487,9 +467,7 @@ STATIC void machine_i2s_init_helper(machine_i2s_obj_t *self, size_t n_pos_args, #endif i2s_pin_config_t pin_config; - #if (ESP_IDF_VERSION_MAJOR == 4) && (ESP_IDF_VERSION_MINOR >= 4) pin_config.mck_io_num = I2S_PIN_NO_CHANGE; - #endif pin_config.bck_io_num = self->sck; pin_config.ws_io_num = self->ws; @@ -527,7 +505,7 @@ STATIC mp_obj_t machine_i2s_make_new(const mp_obj_type_t *type, size_t n_pos_arg mp_arg_check_num(n_pos_args, n_kw_args, 1, MP_OBJ_FUN_ARGS_MAX, true); i2s_port_t port = mp_obj_get_int(args[0]); - if (port < 0 || port >= I2S_NUM_MAX) { + if (port < 0 || port >= I2S_NUM_AUTO) { mp_raise_ValueError(MP_ERROR_TEXT("invalid id")); } @@ -841,6 +819,6 @@ MP_DEFINE_CONST_OBJ_TYPE( locals_dict, &machine_i2s_locals_dict ); -MP_REGISTER_ROOT_POINTER(struct _machine_i2s_obj_t *machine_i2s_obj[I2S_NUM_MAX]); +MP_REGISTER_ROOT_POINTER(struct _machine_i2s_obj_t *machine_i2s_obj[I2S_NUM_AUTO]); #endif // MICROPY_PY_MACHINE_I2S diff --git a/ports/esp32/machine_pin.c b/ports/esp32/machine_pin.c index f103b96963ce4..5ea41701afe3c 100644 --- a/ports/esp32/machine_pin.c +++ b/ports/esp32/machine_pin.c @@ -31,6 +31,7 @@ #include "driver/gpio.h" #include "driver/rtc_io.h" +#include "hal/gpio_ll.h" #include "py/runtime.h" #include "py/mphal.h" @@ -92,11 +93,7 @@ STATIC const machine_pin_obj_t machine_pin_obj[GPIO_NUM_MAX] = { #endif {{&machine_pin_type}, GPIO_NUM_18}, {{&machine_pin_type}, GPIO_NUM_19}, - #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 3, 2) {{&machine_pin_type}, GPIO_NUM_20}, - #else - {{NULL}, -1}, - #endif {{&machine_pin_type}, GPIO_NUM_21}, {{&machine_pin_type}, GPIO_NUM_22}, {{&machine_pin_type}, GPIO_NUM_23}, @@ -295,7 +292,7 @@ STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_ #endif // configure the pin for gpio - gpio_pad_select_gpio(self->id); + esp_rom_gpio_pad_select_gpio(self->id); // set initial value (do this before configuring mode/pull) if (args[ARG_value].u_obj != MP_OBJ_NULL) { @@ -423,7 +420,7 @@ STATIC mp_obj_t machine_pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_ enum { ARG_handler, ARG_trigger, ARG_wake }; static const mp_arg_t allowed_args[] = { { MP_QSTR_handler, MP_ARG_OBJ, {.u_obj = mp_const_none} }, - { MP_QSTR_trigger, MP_ARG_INT, {.u_int = GPIO_PIN_INTR_POSEDGE | GPIO_PIN_INTR_NEGEDGE} }, + { MP_QSTR_trigger, MP_ARG_INT, {.u_int = GPIO_INTR_POSEDGE | GPIO_INTR_NEGEDGE} }, { MP_QSTR_wake, MP_ARG_OBJ, {.u_obj = mp_const_none} }, }; machine_pin_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); @@ -436,7 +433,7 @@ STATIC mp_obj_t machine_pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_ uint32_t trigger = args[ARG_trigger].u_int; mp_obj_t wake_obj = args[ARG_wake].u_obj; - if ((trigger == GPIO_PIN_INTR_LOLEVEL || trigger == GPIO_PIN_INTR_HILEVEL) && wake_obj != mp_const_none) { + if ((trigger == GPIO_INTR_LOW_LEVEL || trigger == GPIO_INTR_HIGH_LEVEL) && wake_obj != mp_const_none) { mp_int_t wake; if (mp_obj_get_int_maybe(wake_obj, &wake)) { if (wake < 2 || wake > 7) { @@ -460,7 +457,7 @@ STATIC mp_obj_t machine_pin_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_ mp_raise_ValueError(MP_ERROR_TEXT("no resources")); } - machine_rtc_config.ext0_level = trigger == GPIO_PIN_INTR_LOLEVEL ? 0 : 1; + machine_rtc_config.ext0_level = trigger == GPIO_INTR_LOW_LEVEL ? 0 : 1; machine_rtc_config.ext0_wake_types = wake; } else { if (machine_rtc_config.ext0_pin == self->id) { @@ -497,10 +494,10 @@ STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_OPEN_DRAIN), MP_ROM_INT(GPIO_MODE_INPUT_OUTPUT_OD) }, { MP_ROM_QSTR(MP_QSTR_PULL_UP), MP_ROM_INT(GPIO_PULL_UP) }, { MP_ROM_QSTR(MP_QSTR_PULL_DOWN), MP_ROM_INT(GPIO_PULL_DOWN) }, - { MP_ROM_QSTR(MP_QSTR_IRQ_RISING), MP_ROM_INT(GPIO_PIN_INTR_POSEDGE) }, - { MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(GPIO_PIN_INTR_NEGEDGE) }, - { MP_ROM_QSTR(MP_QSTR_WAKE_LOW), MP_ROM_INT(GPIO_PIN_INTR_LOLEVEL) }, - { MP_ROM_QSTR(MP_QSTR_WAKE_HIGH), MP_ROM_INT(GPIO_PIN_INTR_HILEVEL) }, + { MP_ROM_QSTR(MP_QSTR_IRQ_RISING), MP_ROM_INT(GPIO_INTR_POSEDGE) }, + { MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(GPIO_INTR_NEGEDGE) }, + { MP_ROM_QSTR(MP_QSTR_WAKE_LOW), MP_ROM_INT(GPIO_INTR_LOW_LEVEL) }, + { MP_ROM_QSTR(MP_QSTR_WAKE_HIGH), MP_ROM_INT(GPIO_INTR_HIGH_LEVEL) }, { MP_ROM_QSTR(MP_QSTR_DRIVE_0), MP_ROM_INT(GPIO_DRIVE_CAP_0) }, { MP_ROM_QSTR(MP_QSTR_DRIVE_1), MP_ROM_INT(GPIO_DRIVE_CAP_1) }, { MP_ROM_QSTR(MP_QSTR_DRIVE_2), MP_ROM_INT(GPIO_DRIVE_CAP_2) }, @@ -573,11 +570,7 @@ STATIC const machine_pin_irq_obj_t machine_pin_irq_object[GPIO_NUM_MAX] = { #endif {{&machine_pin_irq_type}, GPIO_NUM_18}, {{&machine_pin_irq_type}, GPIO_NUM_19}, - #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 3, 2) {{&machine_pin_irq_type}, GPIO_NUM_20}, - #else - {{NULL}, -1}, - #endif {{&machine_pin_irq_type}, GPIO_NUM_21}, {{&machine_pin_irq_type}, GPIO_NUM_22}, {{&machine_pin_irq_type}, GPIO_NUM_23}, diff --git a/ports/esp32/machine_pwm.c b/ports/esp32/machine_pwm.c index 445ac80fb218c..462d0fa79c740 100644 --- a/ports/esp32/machine_pwm.c +++ b/ports/esp32/machine_pwm.c @@ -34,6 +34,7 @@ #include "driver/ledc.h" #include "esp_err.h" +#include "soc/gpio_sig_map.h" #define PWM_DBG(...) // #define PWM_DBG(...) mp_printf(&mp_plat_print, __VA_ARGS__); mp_printf(&mp_plat_print, "\n"); @@ -164,13 +165,13 @@ STATIC void pwm_deinit(int channel_idx) { // Mark it unused, and tell the hardware to stop routing check_esp_err(ledc_stop(mode, channel, 0)); // Disable ledc signal for the pin - // gpio_matrix_out(pin, SIG_GPIO_OUT_IDX, false, false); + // esp_rom_gpio_connect_out_signal(pin, SIG_GPIO_OUT_IDX, false, false); if (mode == LEDC_LOW_SPEED_MODE) { - gpio_matrix_out(pin, LEDC_LS_SIG_OUT0_IDX + channel, false, true); + esp_rom_gpio_connect_out_signal(pin, LEDC_LS_SIG_OUT0_IDX + channel, false, true); } else { #if LEDC_SPEED_MODE_MAX > 1 #if CONFIG_IDF_TARGET_ESP32 - gpio_matrix_out(pin, LEDC_HS_SIG_OUT0_IDX + channel, false, true); + esp_rom_gpio_connect_out_signal(pin, LEDC_HS_SIG_OUT0_IDX + channel, false, true); #else #error Add supported CONFIG_IDF_TARGET_ESP32_xxx #endif @@ -209,18 +210,13 @@ STATIC void configure_channel(machine_pwm_obj_t *self) { STATIC void set_freq(machine_pwm_obj_t *self, unsigned int freq, ledc_timer_config_t *timer) { if (freq != timer->freq_hz) { // Find the highest bit resolution for the requested frequency - unsigned int i = LEDC_APB_CLK_HZ; // 80 MHz + unsigned int i = APB_CLK_FREQ; // 80 MHz #if SOC_LEDC_SUPPORT_REF_TICK if (freq < EMPIRIC_FREQ) { - i = LEDC_REF_CLK_HZ; // 1 MHz + i = REF_CLK_FREQ; // 1 MHz } #endif - #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) - // original code - i /= freq; - #else - // See https://github.com/espressif/esp-idf/issues/7722 int divider = (i + freq / 2) / freq; // rounded if (divider == 0) { divider = 1; @@ -230,7 +226,6 @@ STATIC void set_freq(machine_pwm_obj_t *self, unsigned int freq, ledc_timer_conf f = 1.0; } i = (unsigned int)roundf((float)i / f); - #endif unsigned int res = 0; for (; i > 1; i >>= 1) { @@ -354,7 +349,7 @@ STATIC void set_duty_u16(machine_pwm_obj_t *self, int duty) { // See https://github.com/espressif/esp-idf/issues/7288 if (duty != get_duty_u16(self)) { PWM_DBG("set_duty_u16(%u), get_duty_u16():%u, channel_duty:%d, duty_resolution:%d, freq_hz:%d", duty, get_duty_u16(self), channel_duty, timer.duty_resolution, timer.freq_hz); - ets_delay_us(2 * 1000000 / timer.freq_hz); + esp_rom_delay_us(2 * 1000000 / timer.freq_hz); if (duty != get_duty_u16(self)) { PWM_DBG("set_duty_u16(%u), get_duty_u16():%u, channel_duty:%d, duty_resolution:%d, freq_hz:%d", duty, get_duty_u16(self), channel_duty, timer.duty_resolution, timer.freq_hz); } diff --git a/ports/esp32/machine_sdcard.c b/ports/esp32/machine_sdcard.c index 5b495f8494e77..a2d133442f10b 100644 --- a/ports/esp32/machine_sdcard.c +++ b/ports/esp32/machine_sdcard.c @@ -69,6 +69,68 @@ typedef struct _sdcard_obj_t { #define _SECTOR_SIZE(self) (self->card.csd.sector_size) +// SPI bus default bus and device configuration. + +static const spi_bus_config_t spi_bus_defaults[2] = { + { + #if CONFIG_IDF_TARGET_ESP32 + .miso_io_num = GPIO_NUM_19, + .mosi_io_num = GPIO_NUM_23, + .sclk_io_num = GPIO_NUM_18, + #else + .miso_io_num = GPIO_NUM_36, + .mosi_io_num = GPIO_NUM_35, + .sclk_io_num = GPIO_NUM_37, + #endif + .data2_io_num = GPIO_NUM_NC, + .data3_io_num = GPIO_NUM_NC, + .data4_io_num = GPIO_NUM_NC, + .data5_io_num = GPIO_NUM_NC, + .data6_io_num = GPIO_NUM_NC, + .data7_io_num = GPIO_NUM_NC, + .max_transfer_sz = 4000, + .flags = SPICOMMON_BUSFLAG_MASTER | SPICOMMON_BUSFLAG_SCLK | SPICOMMON_BUSFLAG_MISO | SPICOMMON_BUSFLAG_MOSI, + .intr_flags = 0, + }, + { + .miso_io_num = GPIO_NUM_2, + .mosi_io_num = GPIO_NUM_15, + .sclk_io_num = GPIO_NUM_14, + .data2_io_num = GPIO_NUM_NC, + .data3_io_num = GPIO_NUM_NC, + .data4_io_num = GPIO_NUM_NC, + .data5_io_num = GPIO_NUM_NC, + .data6_io_num = GPIO_NUM_NC, + .data7_io_num = GPIO_NUM_NC, + .max_transfer_sz = 4000, + .flags = SPICOMMON_BUSFLAG_MASTER | SPICOMMON_BUSFLAG_SCLK | SPICOMMON_BUSFLAG_MISO | SPICOMMON_BUSFLAG_MOSI, + .intr_flags = 0, + }, +}; + +#if CONFIG_IDF_TARGET_ESP32 +static const uint8_t spi_dma_channel_defaults[2] = { + 2, + 1, +}; +#endif + +static const sdspi_device_config_t spi_dev_defaults[2] = { + { + #if CONFIG_IDF_TARGET_ESP32 + .host_id = VSPI_HOST, + .gpio_cs = GPIO_NUM_5, + #else + .host_id = SPI3_HOST, + .gpio_cs = GPIO_NUM_34, + #endif + .gpio_cd = SDSPI_SLOT_NO_CD, + .gpio_wp = SDSPI_SLOT_NO_WP, + .gpio_int = SDSPI_SLOT_NO_INT, + }, + SDSPI_DEVICE_CONFIG_DEFAULT(), // HSPI (ESP32) / SPI2 (ESP32S3) +}; + STATIC gpio_num_t pin_or_int(const mp_obj_t arg) { if (mp_obj_is_small_int(arg)) { return MP_OBJ_SMALL_INT_VALUE(arg); @@ -188,10 +250,11 @@ STATIC mp_obj_t machine_sdcard_make_new(const mp_obj_type_t *type, size_t n_args } if (is_spi) { - #if CONFIG_IDF_TARGET_ESP32S3 - self->host.slot = slot_num ? SPI3_HOST : SPI2_HOST; - #else + // Needs to match spi_dev_defaults above. + #if CONFIG_IDF_TARGET_ESP32 self->host.slot = slot_num ? HSPI_HOST : VSPI_HOST; + #else + self->host.slot = slot_num ? SPI2_HOST : SPI3_HOST; #endif } @@ -202,46 +265,39 @@ STATIC mp_obj_t machine_sdcard_make_new(const mp_obj_type_t *type, size_t n_args if (is_spi) { // SPI interface - #if CONFIG_IDF_TARGET_ESP32S3 - STATIC const sdspi_slot_config_t slot_defaults[2] = { - { - .gpio_miso = GPIO_NUM_36, - .gpio_mosi = GPIO_NUM_35, - .gpio_sck = GPIO_NUM_37, - .gpio_cs = GPIO_NUM_34, - .gpio_cd = SDSPI_SLOT_NO_CD, - .gpio_wp = SDSPI_SLOT_NO_WP, - .dma_channel = SPI_DMA_CH_AUTO - }, - SDSPI_SLOT_CONFIG_DEFAULT() - }; + DEBUG_printf(" Setting up SPI slot configuration"); + spi_host_device_t spi_host_id = self->host.slot; + spi_bus_config_t bus_config = spi_bus_defaults[slot_num]; + #if CONFIG_IDF_TARGET_ESP32 + spi_dma_chan_t dma_channel = spi_dma_channel_defaults[slot_num]; #else - STATIC const sdspi_slot_config_t slot_defaults[2] = { - { - .gpio_miso = GPIO_NUM_19, - .gpio_mosi = GPIO_NUM_23, - .gpio_sck = GPIO_NUM_18, - .gpio_cs = GPIO_NUM_5, - .gpio_cd = SDSPI_SLOT_NO_CD, - .gpio_wp = SDSPI_SLOT_NO_WP, - .dma_channel = 2 - }, - SDSPI_SLOT_CONFIG_DEFAULT() - }; + spi_dma_chan_t dma_channel = SPI_DMA_CH_AUTO; #endif + sdspi_device_config_t dev_config = spi_dev_defaults[slot_num]; - DEBUG_printf(" Setting up SPI slot configuration"); - sdspi_slot_config_t slot_config = slot_defaults[slot_num]; + SET_CONFIG_PIN(bus_config, miso_io_num, ARG_miso); + SET_CONFIG_PIN(bus_config, mosi_io_num, ARG_mosi); + SET_CONFIG_PIN(bus_config, sclk_io_num, ARG_sck); - SET_CONFIG_PIN(slot_config, gpio_cd, ARG_cd); - SET_CONFIG_PIN(slot_config, gpio_wp, ARG_wp); - SET_CONFIG_PIN(slot_config, gpio_miso, ARG_miso); - SET_CONFIG_PIN(slot_config, gpio_mosi, ARG_mosi); - SET_CONFIG_PIN(slot_config, gpio_sck, ARG_sck); - SET_CONFIG_PIN(slot_config, gpio_cs, ARG_cs); + SET_CONFIG_PIN(dev_config, gpio_cs, ARG_cs); + SET_CONFIG_PIN(dev_config, gpio_cd, ARG_cd); + SET_CONFIG_PIN(dev_config, gpio_wp, ARG_wp); - DEBUG_printf(" Calling init_slot()"); - check_esp_err(sdspi_host_init_slot(self->host.slot, &slot_config)); + DEBUG_printf(" Calling spi_bus_initialize()"); + check_esp_err(spi_bus_initialize(spi_host_id, &bus_config, dma_channel)); + + DEBUG_printf(" Calling sdspi_host_init_device()"); + sdspi_dev_handle_t sdspi_handle; + esp_err_t ret = sdspi_host_init_device(&dev_config, &sdspi_handle); + if (ret != ESP_OK) { + spi_bus_free(spi_host_id); + check_esp_err(ret); + } + if (self->host.slot != sdspi_handle) { + // MicroPython restriction: the SPI bus must be exclusively for the SD card. + spi_bus_free(spi_host_id); + mp_raise_ValueError(MP_ERROR_TEXT("SPI bus already in use")); + } } else { // SD/MMC interface DEBUG_printf(" Setting up SDMMC slot configuration"); @@ -275,12 +331,9 @@ STATIC mp_obj_t sd_deinit(mp_obj_t self_in) { DEBUG_printf("De-init host\n"); if (self->flags & SDCARD_CARD_FLAGS_HOST_INIT_DONE) { - #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 2, 0) if (self->host.flags & SDMMC_HOST_FLAG_DEINIT_ARG) { self->host.deinit_p(self->host.slot); - } else - #endif - { + } else { self->host.deinit(); } if (self->host.flags & SDMMC_HOST_FLAG_SPI) { diff --git a/ports/esp32/machine_timer.c b/ports/esp32/machine_timer.c index c66cb2a48d903..5855cfb3e0b09 100644 --- a/ports/esp32/machine_timer.c +++ b/ports/esp32/machine_timer.c @@ -36,16 +36,13 @@ #include "mphalport.h" #include "driver/timer.h" -#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 1, 1) #include "hal/timer_ll.h" -#define HAVE_TIMER_LL (1) -#endif #define TIMER_INTR_SEL TIMER_INTR_LEVEL #define TIMER_DIVIDER 8 // TIMER_BASE_CLK is normally 80MHz. TIMER_DIVIDER ought to divide this exactly -#define TIMER_SCALE (TIMER_BASE_CLK / TIMER_DIVIDER) +#define TIMER_SCALE (APB_CLK_FREQ / TIMER_DIVIDER) #define TIMER_FLAGS 0 @@ -143,39 +140,14 @@ STATIC void machine_timer_isr(void *self_in) { machine_timer_obj_t *self = self_in; timg_dev_t *device = self->group ? &(TIMERG1) : &(TIMERG0); - #if HAVE_TIMER_LL - - #if CONFIG_IDF_TARGET_ESP32 && ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) - device->hw_timer[self->index].update = 1; - #else - #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0) #if CONFIG_IDF_TARGET_ESP32S3 device->hw_timer[self->index].update.tn_update = 1; #else device->hw_timer[self->index].update.tx_update = 1; #endif - #else - device->hw_timer[self->index].update.update = 1; - #endif - #endif + timer_ll_clear_intr_status(device, self->index); - #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) - timer_ll_set_alarm_enable(device, self->index, self->repeat); - #else timer_ll_set_alarm_value(device, self->index, self->repeat); - #endif - - #else - - device->hw_timer[self->index].update = 1; - if (self->index) { - device->int_clr_timers.t1 = 1; - } else { - device->int_clr_timers.t0 = 1; - } - device->hw_timer[self->index].config.alarm_en = self->repeat; - - #endif mp_sched_schedule(self->callback, self); mp_hal_wake_main_task_from_isr(); diff --git a/ports/esp32/machine_uart.c b/ports/esp32/machine_uart.c index 8e6e5788d074e..d9b845047b6b3 100644 --- a/ports/esp32/machine_uart.c +++ b/ports/esp32/machine_uart.c @@ -37,17 +37,10 @@ #include "modmachine.h" #include "uart.h" -#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 1, 0) -#define UART_INV_TX UART_INVERSE_TXD -#define UART_INV_RX UART_INVERSE_RXD -#define UART_INV_RTS UART_INVERSE_RTS -#define UART_INV_CTS UART_INVERSE_CTS -#else #define UART_INV_TX UART_SIGNAL_TXD_INV #define UART_INV_RX UART_SIGNAL_RXD_INV #define UART_INV_RTS UART_SIGNAL_RTS_INV #define UART_INV_CTS UART_SIGNAL_CTS_INV -#endif #define UART_INV_MASK (UART_INV_TX | UART_INV_RX | UART_INV_RTS | UART_INV_CTS) @@ -273,9 +266,7 @@ STATIC void machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args, co uint32_t char_time_ms = 12000 / baudrate + 1; uint32_t rx_timeout = self->timeout_char / char_time_ms; if (rx_timeout < 1) { - #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 1, 0) uart_set_rx_full_threshold(self->uart_num, 1); - #endif uart_set_rx_timeout(self->uart_num, 1); } else { uart_set_rx_timeout(self->uart_num, rx_timeout); diff --git a/ports/esp32/machine_wdt.c b/ports/esp32/machine_wdt.c index 4ccf417b60999..2cb6c518175eb 100644 --- a/ports/esp32/machine_wdt.c +++ b/ports/esp32/machine_wdt.c @@ -54,14 +54,16 @@ STATIC mp_obj_t machine_wdt_make_new(const mp_obj_type_t *type_in, size_t n_args mp_raise_ValueError(NULL); } - // Convert milliseconds to seconds (esp_task_wdt_init needs seconds) - args[ARG_timeout].u_int /= 1000; - if (args[ARG_timeout].u_int <= 0) { mp_raise_ValueError(MP_ERROR_TEXT("WDT timeout too short")); } - mp_int_t rs_code = esp_task_wdt_init(args[ARG_timeout].u_int, true); + esp_task_wdt_config_t config = { + .timeout_ms = args[ARG_timeout].u_int, + .idle_core_mask = 0, + .trigger_panic = true, + }; + mp_int_t rs_code = esp_task_wdt_reconfigure(&config); if (rs_code != ESP_OK) { mp_raise_OSError(rs_code); } diff --git a/ports/esp32/main.c b/ports/esp32/main.c index 3e7c9ee162d79..b8ba03e513fff 100644 --- a/ports/esp32/main.c +++ b/ports/esp32/main.c @@ -35,16 +35,9 @@ #include "esp_system.h" #include "nvs_flash.h" #include "esp_task.h" -#include "soc/cpu.h" +#include "esp_event.h" #include "esp_log.h" - -#if CONFIG_IDF_TARGET_ESP32 -#include "esp32/spiram.h" -#elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/spiram.h" -#elif CONFIG_IDF_TARGET_ESP32S3 -#include "esp32s3/spiram.h" -#endif +#include "esp_psram.h" #include "py/stackctrl.h" #include "py/nlr.h" @@ -88,11 +81,11 @@ int vprintf_null(const char *format, va_list ap) { } void mp_task(void *pvParameter) { - volatile uint32_t sp = (uint32_t)get_sp(); + volatile uint32_t sp = (uint32_t)esp_cpu_get_sp(); #if MICROPY_PY_THREAD mp_thread_init(pxTaskGetStackStart(NULL), MP_TASK_STACK_SIZE / sizeof(uintptr_t)); #endif - #if CONFIG_USB_ENABLED + #if CONFIG_USB_OTG_SUPPORTED usb_init(); #elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG usb_serial_jtag_init(); @@ -102,50 +95,18 @@ void mp_task(void *pvParameter) { #endif machine_init(); - size_t mp_task_heap_size; - void *mp_task_heap = NULL; - - #if CONFIG_SPIRAM_USE_MALLOC - // SPIRAM is issued using MALLOC, fallback to normal allocation rules - mp_task_heap = NULL; - #elif CONFIG_ESP32_SPIRAM_SUPPORT - // Try to use the entire external SPIRAM directly for the heap - mp_task_heap = (void *)SOC_EXTRAM_DATA_LOW; - switch (esp_spiram_get_chip_size()) { - case ESP_SPIRAM_SIZE_16MBITS: - mp_task_heap_size = 2 * 1024 * 1024; - break; - case ESP_SPIRAM_SIZE_32MBITS: - case ESP_SPIRAM_SIZE_64MBITS: - mp_task_heap_size = 4 * 1024 * 1024; - break; - default: - // No SPIRAM, fallback to normal allocation - mp_task_heap = NULL; - break; - } - #elif CONFIG_ESP32S2_SPIRAM_SUPPORT || CONFIG_ESP32S3_SPIRAM_SUPPORT - // Try to use the entire external SPIRAM directly for the heap - size_t esp_spiram_size = esp_spiram_get_size(); - if (esp_spiram_size > 0) { - mp_task_heap = (void *)SOC_EXTRAM_DATA_HIGH - esp_spiram_size; - mp_task_heap_size = esp_spiram_size; + esp_err_t err = esp_event_loop_create_default(); + if (err != ESP_OK) { + ESP_LOGE("esp_init", "can't create event loop: 0x%x\n", err); } - #endif - if (mp_task_heap == NULL) { - // Allocate the uPy heap using malloc and get the largest available region, - // limiting to 1/2 total available memory to leave memory for the OS. - #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 1, 0) - size_t heap_total = heap_caps_get_total_size(MALLOC_CAP_8BIT); - #else - multi_heap_info_t info; - heap_caps_get_info(&info, MALLOC_CAP_8BIT); - size_t heap_total = info.total_free_bytes + info.total_allocated_bytes; - #endif - mp_task_heap_size = MIN(heap_caps_get_largest_free_block(MALLOC_CAP_8BIT), heap_total / 2); - mp_task_heap = malloc(mp_task_heap_size); - } + // Allocate the uPy heap using malloc and get the largest available region, + // limiting to 1/2 total available memory to leave memory for the OS. + // When SPIRAM is enabled, this will allocate from SPIRAM. + uint32_t caps = MALLOC_CAP_8BIT; + size_t heap_total = heap_caps_get_total_size(caps); + size_t mp_task_heap_size = MIN(heap_caps_get_largest_free_block(caps), heap_total / 2); + void *mp_task_heap = heap_caps_malloc(mp_task_heap_size, caps); soft_reset: // initialise the stack pointer for the main thread diff --git a/ports/esp32/main_esp32/CMakeLists.txt b/ports/esp32/main_esp32/CMakeLists.txt new file mode 100644 index 0000000000000..40188abff8e57 --- /dev/null +++ b/ports/esp32/main_esp32/CMakeLists.txt @@ -0,0 +1,11 @@ +# Set location of base MicroPython directory. +if(NOT MICROPY_DIR) + get_filename_component(MICROPY_DIR ${CMAKE_CURRENT_LIST_DIR}/../../.. ABSOLUTE) +endif() + +# Set location of the ESP32 port directory. +if(NOT MICROPY_PORT_DIR) + get_filename_component(MICROPY_PORT_DIR ${MICROPY_DIR}/ports/esp32 ABSOLUTE) +endif() + +include(${MICROPY_PORT_DIR}/esp32_common.cmake) diff --git a/ports/esp32/main_esp32c3/CMakeLists.txt b/ports/esp32/main_esp32c3/CMakeLists.txt new file mode 100644 index 0000000000000..307c0f32183a9 --- /dev/null +++ b/ports/esp32/main_esp32c3/CMakeLists.txt @@ -0,0 +1,14 @@ +# Set location of base MicroPython directory. +if(NOT MICROPY_DIR) + get_filename_component(MICROPY_DIR ${CMAKE_CURRENT_LIST_DIR}/../../.. ABSOLUTE) +endif() + +# Set location of the ESP32 port directory. +if(NOT MICROPY_PORT_DIR) + get_filename_component(MICROPY_PORT_DIR ${MICROPY_DIR}/ports/esp32 ABSOLUTE) +endif() + +list(APPEND MICROPY_SOURCE_LIB ${MICROPY_DIR}/shared/runtime/gchelper_generic.c) +list(APPEND IDF_COMPONENTS riscv) + +include(${MICROPY_PORT_DIR}/esp32_common.cmake) diff --git a/ports/esp32/main_esp32s2/CMakeLists.txt b/ports/esp32/main_esp32s2/CMakeLists.txt new file mode 100644 index 0000000000000..40188abff8e57 --- /dev/null +++ b/ports/esp32/main_esp32s2/CMakeLists.txt @@ -0,0 +1,11 @@ +# Set location of base MicroPython directory. +if(NOT MICROPY_DIR) + get_filename_component(MICROPY_DIR ${CMAKE_CURRENT_LIST_DIR}/../../.. ABSOLUTE) +endif() + +# Set location of the ESP32 port directory. +if(NOT MICROPY_PORT_DIR) + get_filename_component(MICROPY_PORT_DIR ${MICROPY_DIR}/ports/esp32 ABSOLUTE) +endif() + +include(${MICROPY_PORT_DIR}/esp32_common.cmake) diff --git a/ports/esp32/main_esp32s2/idf_component.yml b/ports/esp32/main_esp32s2/idf_component.yml new file mode 100644 index 0000000000000..fe3213bd26af2 --- /dev/null +++ b/ports/esp32/main_esp32s2/idf_component.yml @@ -0,0 +1,5 @@ +## IDF Component Manager Manifest File +dependencies: + idf: + version: ">=5.0.2" + espressif/esp_tinyusb: "~1.0.0" diff --git a/ports/esp32/main_esp32s3/CMakeLists.txt b/ports/esp32/main_esp32s3/CMakeLists.txt new file mode 100644 index 0000000000000..40188abff8e57 --- /dev/null +++ b/ports/esp32/main_esp32s3/CMakeLists.txt @@ -0,0 +1,11 @@ +# Set location of base MicroPython directory. +if(NOT MICROPY_DIR) + get_filename_component(MICROPY_DIR ${CMAKE_CURRENT_LIST_DIR}/../../.. ABSOLUTE) +endif() + +# Set location of the ESP32 port directory. +if(NOT MICROPY_PORT_DIR) + get_filename_component(MICROPY_PORT_DIR ${MICROPY_DIR}/ports/esp32 ABSOLUTE) +endif() + +include(${MICROPY_PORT_DIR}/esp32_common.cmake) diff --git a/ports/esp32/main_esp32s3/idf_component.yml b/ports/esp32/main_esp32s3/idf_component.yml new file mode 100644 index 0000000000000..fe3213bd26af2 --- /dev/null +++ b/ports/esp32/main_esp32s3/idf_component.yml @@ -0,0 +1,5 @@ +## IDF Component Manager Manifest File +dependencies: + idf: + version: ">=5.0.2" + espressif/esp_tinyusb: "~1.0.0" diff --git a/ports/esp32/modesp.c b/ports/esp32/modesp.c index f125b614bda95..4726ce5874209 100644 --- a/ports/esp32/modesp.c +++ b/ports/esp32/modesp.c @@ -29,8 +29,8 @@ #include +#include "esp_flash.h" #include "esp_log.h" -#include "esp_spi_flash.h" #include "py/runtime.h" #include "py/mperrno.h" @@ -53,33 +53,33 @@ STATIC mp_obj_t esp_osdebug(size_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_osdebug_obj, 1, 2, esp_osdebug); -STATIC mp_obj_t esp_flash_read(mp_obj_t offset_in, mp_obj_t buf_in) { +STATIC mp_obj_t esp_flash_read_(mp_obj_t offset_in, mp_obj_t buf_in) { mp_int_t offset = mp_obj_get_int(offset_in); mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE); - esp_err_t res = spi_flash_read(offset, bufinfo.buf, bufinfo.len); + esp_err_t res = esp_flash_read(NULL, bufinfo.buf, offset, bufinfo.len); if (res != ESP_OK) { mp_raise_OSError(MP_EIO); } return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp_flash_read_obj, esp_flash_read); +STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp_flash_read_obj, esp_flash_read_); -STATIC mp_obj_t esp_flash_write(mp_obj_t offset_in, mp_obj_t buf_in) { +STATIC mp_obj_t esp_flash_write_(mp_obj_t offset_in, mp_obj_t buf_in) { mp_int_t offset = mp_obj_get_int(offset_in); mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ); - esp_err_t res = spi_flash_write(offset, bufinfo.buf, bufinfo.len); + esp_err_t res = esp_flash_write(NULL, bufinfo.buf, offset, bufinfo.len); if (res != ESP_OK) { mp_raise_OSError(MP_EIO); } return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp_flash_write_obj, esp_flash_write); +STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp_flash_write_obj, esp_flash_write_); STATIC mp_obj_t esp_flash_erase(mp_obj_t sector_in) { mp_int_t sector = mp_obj_get_int(sector_in); - esp_err_t res = spi_flash_erase_sector(sector); + esp_err_t res = esp_flash_erase_region(NULL, sector * 4096, 4096); if (res != ESP_OK) { mp_raise_OSError(MP_EIO); } @@ -88,7 +88,9 @@ STATIC mp_obj_t esp_flash_erase(mp_obj_t sector_in) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_flash_erase_obj, esp_flash_erase); STATIC mp_obj_t esp_flash_size(void) { - return mp_obj_new_int_from_uint(spi_flash_get_chip_size()); + uint32_t size; + esp_flash_get_size(NULL, &size); + return mp_obj_new_int_from_uint(size); } STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_flash_size_obj, esp_flash_size); @@ -98,14 +100,14 @@ STATIC mp_obj_t esp_flash_user_start(void) { STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_flash_user_start_obj, esp_flash_user_start); STATIC mp_obj_t esp_gpio_matrix_in(mp_obj_t pin, mp_obj_t sig, mp_obj_t inv) { - gpio_matrix_in(mp_obj_get_int(pin), mp_obj_get_int(sig), mp_obj_get_int(inv)); + esp_rom_gpio_connect_in_signal(mp_obj_get_int(pin), mp_obj_get_int(sig), mp_obj_get_int(inv)); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp_gpio_matrix_in_obj, esp_gpio_matrix_in); STATIC mp_obj_t esp_gpio_matrix_out(size_t n_args, const mp_obj_t *args) { (void)n_args; - gpio_matrix_out(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), mp_obj_get_int(args[2]), mp_obj_get_int(args[3])); + esp_rom_gpio_connect_out_signal(mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), mp_obj_get_int(args[2]), mp_obj_get_int(args[3])); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_gpio_matrix_out_obj, 4, 4, esp_gpio_matrix_out); diff --git a/ports/esp32/modesp32.c b/ports/esp32/modesp32.c index a18ca4c1375ac..ef3ad0b76d459 100644 --- a/ports/esp32/modesp32.c +++ b/ports/esp32/modesp32.c @@ -45,10 +45,8 @@ #include "modesp32.h" // These private includes are needed for idf_heap_info. -#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 3, 0) #define MULTI_HEAP_FREERTOS #include "../multi_heap_platform.h" -#endif #include "../heap_private.h" STATIC mp_obj_t esp32_wake_on_touch(const mp_obj_t wake) { @@ -161,9 +159,9 @@ STATIC mp_obj_t esp32_raw_temperature(void) { CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_DUMP_OUT); SET_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP_FORCE); SET_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP); - ets_delay_us(100); + esp_rom_delay_us(100); SET_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_DUMP_OUT); - ets_delay_us(5); + esp_rom_delay_us(5); int res = GET_PERI_REG_BITS2(SENS_SAR_SLAVE_ADDR3_REG, SENS_TSENS_OUT, SENS_TSENS_OUT_S); return mp_obj_new_int(res); diff --git a/ports/esp32/modespnow.c b/ports/esp32/modespnow.c index c71201e9b7bde..c94fc81708a14 100644 --- a/ports/esp32/modespnow.c +++ b/ports/esp32/modespnow.c @@ -179,7 +179,7 @@ STATIC mp_obj_t espnow_make_new(const mp_obj_type_t *type, size_t n_args, // Forward declare the send and recv ESPNow callbacks STATIC void send_cb(const uint8_t *mac_addr, esp_now_send_status_t status); -STATIC void recv_cb(const uint8_t *mac_addr, const uint8_t *data, int len); +STATIC void recv_cb(const esp_now_recv_info_t *recv_info, const uint8_t *msg, int msg_len); // ESPNow.init(): Initialise the data buffers and ESP-NOW functions. // Initialise the Espressif ESPNOW software stack, register callbacks and @@ -254,13 +254,9 @@ STATIC mp_obj_t espnow_config(size_t n_args, const mp_obj_t *pos_args, mp_map_t self->recv_timeout_ms = args[ARG_timeout_ms].u_int; } if (args[ARG_rate].u_int >= 0) { - #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 3, 0) esp_initialise_wifi(); // Call the wifi init code in network_wlan.c check_esp_err(esp_wifi_config_espnow_rate(ESP_IF_WIFI_STA, args[ARG_rate].u_int)); check_esp_err(esp_wifi_config_espnow_rate(ESP_IF_WIFI_AP, args[ARG_rate].u_int)); - #else - mp_raise_ValueError(MP_ERROR_TEXT("rate option not supported")); - #endif } if (args[ARG_get].u_obj == MP_OBJ_NULL) { return mp_const_none; @@ -329,11 +325,7 @@ static inline int8_t _get_rssi_from_wifi_pkt(const uint8_t *msg) { (wifi_promiscuous_pkt_t *)(msg - sizeof_espnow_frame_format - sizeof(wifi_promiscuous_pkt_t)); - #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 2, 0) - return wifi_pkt->rx_ctrl.rssi - 100; // Offset rssi for IDF 4.0.2 - #else return wifi_pkt->rx_ctrl.rssi; - #endif } // Lookup a peer in the peers table and return a reference to the item in the @@ -573,7 +565,7 @@ STATIC void send_cb(const uint8_t *mac_addr, esp_now_send_status_t status) { // ESPNow packet. // If the buffer is full, drop the message and increment the dropped count. // Schedules the user callback if one has been registered (ESPNow.config()). -STATIC void recv_cb(const uint8_t *mac_addr, const uint8_t *msg, int msg_len) { +STATIC void recv_cb(const esp_now_recv_info_t *recv_info, const uint8_t *msg, int msg_len) { esp_espnow_obj_t *self = _get_singleton(); ringbuf_t *buf = self->recv_buffer; // TODO: Test this works with ">". @@ -590,7 +582,7 @@ STATIC void recv_cb(const uint8_t *mac_addr, const uint8_t *msg, int msg_len) { #endif // MICROPY_ESPNOW_RSSI ringbuf_put_bytes(buf, (uint8_t *)&header, sizeof(header)); - ringbuf_put_bytes(buf, mac_addr, ESP_NOW_ETH_ALEN); + ringbuf_put_bytes(buf, recv_info->src_addr, ESP_NOW_ETH_ALEN); ringbuf_put_bytes(buf, msg, msg_len); self->rx_packets++; if (self->recv_cb != mp_const_none) { diff --git a/ports/esp32/modmachine.c b/ports/esp32/modmachine.c index fc19618b7309f..01acb01029eab 100644 --- a/ports/esp32/modmachine.c +++ b/ports/esp32/modmachine.c @@ -32,20 +32,10 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" +#include "esp_mac.h" #include "esp_sleep.h" #include "esp_pm.h" -#if CONFIG_IDF_TARGET_ESP32 -#include "esp32/rom/rtc.h" -#include "esp32/clk.h" -#elif CONFIG_IDF_TARGET_ESP32S2 -#include "esp32s2/rom/rtc.h" -#include "esp32s2/clk.h" -#elif CONFIG_IDF_TARGET_ESP32S3 -#include "esp32s3/rom/rtc.h" -#include "esp32s3/clk.h" -#endif - #include "py/obj.h" #include "py/runtime.h" #include "shared/runtime/pyexec.h" @@ -79,7 +69,7 @@ int esp_clk_cpu_freq(void); STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { if (n_args == 0) { // get - return mp_obj_new_int(esp_clk_cpu_freq()); + return mp_obj_new_int(esp_rom_get_cpu_ticks_per_us() * 1000000); } else { // set mp_int_t freq = mp_obj_get_int(args[0]) / 1000000; @@ -110,7 +100,7 @@ STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { if (ret != ESP_OK) { mp_raise_ValueError(NULL); } - while (esp_clk_cpu_freq() != freq * 1000000) { + while (esp_rom_get_cpu_ticks_per_us() != freq) { vTaskDelay(1); } return mp_const_none; diff --git a/ports/esp32/modnetwork.h b/ports/esp32/modnetwork.h index ac6321d8f6f1d..b1b3fc368a450 100644 --- a/ports/esp32/modnetwork.h +++ b/ports/esp32/modnetwork.h @@ -26,7 +26,7 @@ #ifndef MICROPY_INCLUDED_ESP32_MODNETWORK_H #define MICROPY_INCLUDED_ESP32_MODNETWORK_H -#include "esp_event.h" +#include "esp_netif.h" enum { PHY_LAN8710, PHY_LAN8720, PHY_IP101, PHY_RTL8201, PHY_DP83848, PHY_KSZ8041, PHY_KSZ8081, PHY_KSZ8851SNL = 100, PHY_DM9051, PHY_W5500 }; #define IS_SPI_PHY(NUM) (NUM >= 100) @@ -40,10 +40,11 @@ enum { STAT_GOT_IP = 1010, }; -typedef struct _wlan_if_obj_t { +typedef struct _base_if_obj_t { mp_obj_base_t base; - int if_id; -} wlan_if_obj_t; + esp_interface_t if_id; + esp_netif_t *netif; +} base_if_obj_t; extern const mp_obj_type_t esp_network_wlan_type; @@ -64,7 +65,6 @@ static inline void esp_exceptions(esp_err_t e) { } void socket_events_deinit(void); -void network_wlan_event_handler(system_event_t *event); void esp_initialise_wifi(void); #endif diff --git a/ports/esp32/modnetwork_globals.h b/ports/esp32/modnetwork_globals.h index 1f657fb5c8813..7326d453be5aa 100644 --- a/ports/esp32/modnetwork_globals.h +++ b/ports/esp32/modnetwork_globals.h @@ -27,9 +27,8 @@ { MP_ROM_QSTR(MP_QSTR_AUTH_WPA2_ENTERPRISE), MP_ROM_INT(WIFI_AUTH_WPA2_ENTERPRISE) }, { MP_ROM_QSTR(MP_QSTR_AUTH_WPA3_PSK), MP_ROM_INT(WIFI_AUTH_WPA3_PSK) }, { MP_ROM_QSTR(MP_QSTR_AUTH_WPA2_WPA3_PSK), MP_ROM_INT(WIFI_AUTH_WPA2_WPA3_PSK) }, -#if ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(4, 3, 0) { MP_ROM_QSTR(MP_QSTR_AUTH_WAPI_PSK), MP_ROM_INT(WIFI_AUTH_WAPI_PSK) }, -#endif +{ MP_ROM_QSTR(MP_QSTR_AUTH_OWE), MP_ROM_INT(WIFI_AUTH_OWE) }, { MP_ROM_QSTR(MP_QSTR_AUTH_MAX), MP_ROM_INT(WIFI_AUTH_MAX) }, #endif @@ -39,14 +38,8 @@ { MP_ROM_QSTR(MP_QSTR_PHY_IP101), MP_ROM_INT(PHY_IP101) }, { MP_ROM_QSTR(MP_QSTR_PHY_RTL8201), MP_ROM_INT(PHY_RTL8201) }, { MP_ROM_QSTR(MP_QSTR_PHY_DP83848), MP_ROM_INT(PHY_DP83848) }, -#if ESP_IDF_VERSION_MINOR >= 3 -// PHY_KSZ8041 is new in ESP-IDF v4.3 { MP_ROM_QSTR(MP_QSTR_PHY_KSZ8041), MP_ROM_INT(PHY_KSZ8041) }, -#endif -#if ESP_IDF_VERSION_MINOR >= 4 -// PHY_KSZ8081 is new in ESP-IDF v4.4 { MP_ROM_QSTR(MP_QSTR_PHY_KSZ8081), MP_ROM_INT(PHY_KSZ8081) }, -#endif #if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL { MP_ROM_QSTR(MP_QSTR_PHY_KSZ8851SNL), MP_ROM_INT(PHY_KSZ8851SNL) }, diff --git a/ports/esp32/modsocket.c b/ports/esp32/modsocket.c index 731f69fbc2211..7484fce786c74 100644 --- a/ports/esp32/modsocket.c +++ b/ports/esp32/modsocket.c @@ -46,7 +46,6 @@ #include "py/stream.h" #include "py/mperrno.h" #include "shared/netutils/netutils.h" -#include "mdns.h" #include "modnetwork.h" #include "lwip/sockets.h" @@ -164,11 +163,7 @@ static int _socket_getaddrinfo3(const char *nodename, const char *servname, memcpy(nodename_no_local, nodename, nodename_len - local_len); nodename_no_local[nodename_len - local_len] = '\0'; - #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 1, 0) - struct ip4_addr addr = {0}; - #else esp_ip4_addr_t addr = {0}; - #endif esp_err_t err = mdns_query_a(nodename_no_local, MDNS_QUERY_TIMEOUT_MS, &addr); if (err != ESP_OK) { @@ -836,7 +831,7 @@ STATIC mp_obj_t esp_socket_initialize() { static int initialized = 0; if (!initialized) { ESP_LOGI("modsocket", "Initializing"); - tcpip_adapter_init(); + esp_netif_init(); initialized = 1; } return mp_const_none; diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index f81ad58fa1d20..b18681bb572df 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -6,9 +6,11 @@ #include #include +#include "esp_random.h" #include "esp_system.h" #include "freertos/FreeRTOS.h" #include "driver/i2s.h" +#include "esp_wifi_types.h" #ifndef MICROPY_CONFIG_ROM_LEVEL #define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_EXTRA_FEATURES) @@ -131,7 +133,7 @@ #define MICROPY_HW_ENABLE_SDCARD (1) #endif #define MICROPY_HW_SOFTSPI_MIN_DELAY (0) -#define MICROPY_HW_SOFTSPI_MAX_BAUDRATE (ets_get_cpu_frequency() * 1000000 / 200) // roughly +#define MICROPY_HW_SOFTSPI_MAX_BAUDRATE (esp_rom_get_cpu_ticks_per_us() * 1000000 / 200) // roughly #define MICROPY_PY_SSL (1) #define MICROPY_SSL_MBEDTLS (1) #define MICROPY_PY_SSL_FINALISER (1) @@ -162,8 +164,8 @@ void *esp_native_code_commit(void *, size_t, void *); // the only disable interrupts on the current CPU. To full manage exclusion // one should use portENTER_CRITICAL/portEXIT_CRITICAL instead. #include "freertos/FreeRTOS.h" -#define MICROPY_BEGIN_ATOMIC_SECTION() portENTER_CRITICAL_NESTED() -#define MICROPY_END_ATOMIC_SECTION(state) portEXIT_CRITICAL_NESTED(state) +#define MICROPY_BEGIN_ATOMIC_SECTION() portSET_INTERRUPT_MASK_FROM_ISR() +#define MICROPY_END_ATOMIC_SECTION(state) portCLEAR_INTERRUPT_MASK_FROM_ISR(state) #if MICROPY_PY_SOCKET_EVENTS #define MICROPY_PY_SOCKET_EVENTS_HANDLER extern void socket_events_handler(void); socket_events_handler(); @@ -219,11 +221,11 @@ typedef long mp_off_t; #endif #ifndef MICROPY_HW_ENABLE_MDNS_QUERIES -#define MICROPY_HW_ENABLE_MDNS_QUERIES (1) +#define MICROPY_HW_ENABLE_MDNS_QUERIES (0) #endif #ifndef MICROPY_HW_ENABLE_MDNS_RESPONDER -#define MICROPY_HW_ENABLE_MDNS_RESPONDER (1) +#define MICROPY_HW_ENABLE_MDNS_RESPONDER (0) #endif #ifndef MICROPY_BOARD_STARTUP @@ -233,7 +235,7 @@ typedef long mp_off_t; void boardctrl_startup(void); #ifndef MICROPY_PY_NETWORK_LAN -#if (ESP_IDF_VERSION_MAJOR == 4) && (ESP_IDF_VERSION_MINOR >= 1) && (CONFIG_IDF_TARGET_ESP32 || (CONFIG_ETH_USE_SPI_ETHERNET && (CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL || CONFIG_ETH_SPI_ETHERNET_DM9051 || CONFIG_ETH_SPI_ETHERNET_W5500))) +#if CONFIG_IDF_TARGET_ESP32 || (CONFIG_ETH_USE_SPI_ETHERNET && (CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL || CONFIG_ETH_SPI_ETHERNET_DM9051 || CONFIG_ETH_SPI_ETHERNET_W5500)) #define MICROPY_PY_NETWORK_LAN (1) #else #define MICROPY_PY_NETWORK_LAN (0) diff --git a/ports/esp32/mphalport.c b/ports/esp32/mphalport.c index 4c8fa012f4fbf..63a674c24b620 100644 --- a/ports/esp32/mphalport.c +++ b/ports/esp32/mphalport.c @@ -32,6 +32,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" +#include "esp_timer.h" #include "py/obj.h" #include "py/objstr.h" @@ -110,7 +111,7 @@ void mp_hal_stdout_tx_strn(const char *str, size_t len) { if (release_gil) { MP_THREAD_GIL_EXIT(); } - #if CONFIG_USB_ENABLED + #if CONFIG_USB_OTG_SUPPORTED usb_tx_strn(str, len); #elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG usb_serial_jtag_tx_strn(str, len); diff --git a/ports/esp32/mphalport.h b/ports/esp32/mphalport.h index 672fa306f5dca..4f25b3d4fd3a2 100644 --- a/ports/esp32/mphalport.h +++ b/ports/esp32/mphalport.h @@ -46,10 +46,8 @@ // See https://github.com/micropython/micropython/issues/5489 for history #if CONFIG_FREERTOS_UNICORE #define MP_TASK_COREID (0) -#elif ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 2, 0) -#define MP_TASK_COREID (1) #else -#define MP_TASK_COREID (0) +#define MP_TASK_COREID (1) #endif extern TaskHandle_t mp_main_task_handle; @@ -71,7 +69,7 @@ __attribute__((always_inline)) static inline uint32_t mp_hal_ticks_cpu(void) { } void mp_hal_delay_us(uint32_t); -#define mp_hal_delay_us_fast(us) ets_delay_us(us) +#define mp_hal_delay_us_fast(us) esp_rom_delay_us(us) void mp_hal_set_interrupt_char(int c); uint32_t mp_hal_get_cpu_freq(void); @@ -91,15 +89,15 @@ mp_hal_pin_obj_t machine_pin_get_id(mp_obj_t pin_in); #define mp_obj_get_pin(o) machine_pin_get_id(o) // legacy name; only to support esp8266/modonewire #define mp_hal_pin_name(p) (p) static inline void mp_hal_pin_input(mp_hal_pin_obj_t pin) { - gpio_pad_select_gpio(pin); + esp_rom_gpio_pad_select_gpio(pin); gpio_set_direction(pin, GPIO_MODE_INPUT); } static inline void mp_hal_pin_output(mp_hal_pin_obj_t pin) { - gpio_pad_select_gpio(pin); + esp_rom_gpio_pad_select_gpio(pin); gpio_set_direction(pin, GPIO_MODE_INPUT_OUTPUT); } static inline void mp_hal_pin_open_drain(mp_hal_pin_obj_t pin) { - gpio_pad_select_gpio(pin); + esp_rom_gpio_pad_select_gpio(pin); gpio_set_direction(pin, GPIO_MODE_INPUT_OUTPUT_OD); } static inline void mp_hal_pin_od_low(mp_hal_pin_obj_t pin) { diff --git a/ports/esp32/mpnimbleport.c b/ports/esp32/mpnimbleport.c index a58fcbdbf49f4..8235275be6fd6 100644 --- a/ports/esp32/mpnimbleport.c +++ b/ports/esp32/mpnimbleport.c @@ -46,13 +46,13 @@ STATIC void ble_host_task(void *param) { void mp_bluetooth_nimble_port_hci_init(void) { DEBUG_printf("mp_bluetooth_nimble_port_hci_init\n"); - esp_nimble_hci_and_controller_init(); + esp_nimble_hci_init(); } void mp_bluetooth_nimble_port_hci_deinit(void) { DEBUG_printf("mp_bluetooth_nimble_port_hci_deinit\n"); - esp_nimble_hci_and_controller_deinit(); + esp_nimble_hci_deinit(); } void mp_bluetooth_nimble_port_start(void) { diff --git a/ports/esp32/network_common.c b/ports/esp32/network_common.c index 1e76d679db046..082943e2aecc4 100644 --- a/ports/esp32/network_common.c +++ b/ports/esp32/network_common.c @@ -38,15 +38,10 @@ #include "shared/netutils/netutils.h" #include "modnetwork.h" -#include "esp_wifi.h" #include "esp_log.h" -#include "lwip/dns.h" - -#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 1, 0) -#define DNS_MAIN TCPIP_ADAPTER_DNS_MAIN -#else -#define DNS_MAIN ESP_NETIF_DNS_MAIN -#endif +#include "esp_netif.h" +#include "esp_wifi.h" +// #include "lwip/dns.h" NORETURN void esp_exceptions_helper(esp_err_t e) { switch (e) { @@ -80,64 +75,15 @@ NORETURN void esp_exceptions_helper(esp_err_t e) { mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("Wifi Would Block")); case ESP_ERR_WIFI_NOT_CONNECT: mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("Wifi Not Connected")); - case ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS: - mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("TCP/IP Invalid Parameters")); - case ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY: - mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("TCP/IP IF Not Ready")); - case ESP_ERR_TCPIP_ADAPTER_DHCPC_START_FAILED: - mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("TCP/IP DHCP Client Start Failed")); - case ESP_ERR_TCPIP_ADAPTER_NO_MEM: - mp_raise_OSError(MP_ENOMEM); default: mp_raise_msg_varg(&mp_type_RuntimeError, MP_ERROR_TEXT("Wifi Unknown Error 0x%04x"), e); } } -// This function is called by the system-event task and so runs in a different -// thread to the main MicroPython task. It must not raise any Python exceptions. -static esp_err_t event_handler(void *ctx, system_event_t *event) { - switch (event->event_id) { - #if MICROPY_PY_NETWORK_WLAN - case SYSTEM_EVENT_STA_START: - case SYSTEM_EVENT_STA_CONNECTED: - case SYSTEM_EVENT_STA_GOT_IP: - case SYSTEM_EVENT_STA_DISCONNECTED: - network_wlan_event_handler(event); - break; - #endif - case SYSTEM_EVENT_GOT_IP6: - ESP_LOGI("network", "Got IPv6"); - break; - case SYSTEM_EVENT_ETH_START: - ESP_LOGI("ethernet", "start"); - break; - case SYSTEM_EVENT_ETH_STOP: - ESP_LOGI("ethernet", "stop"); - break; - case SYSTEM_EVENT_ETH_CONNECTED: - ESP_LOGI("ethernet", "LAN cable connected"); - break; - case SYSTEM_EVENT_ETH_DISCONNECTED: - ESP_LOGI("ethernet", "LAN cable disconnected"); - break; - case SYSTEM_EVENT_ETH_GOT_IP: - ESP_LOGI("ethernet", "Got IP"); - break; - default: - ESP_LOGI("network", "event %d", event->event_id); - break; - } - return ESP_OK; -} - STATIC mp_obj_t esp_initialize() { static int initialized = 0; if (!initialized) { - ESP_LOGD("modnetwork", "Initializing TCP/IP"); - tcpip_adapter_init(); - ESP_LOGD("modnetwork", "Initializing Event Loop"); - esp_exceptions(esp_event_loop_init(event_handler, NULL)); - ESP_LOGD("modnetwork", "esp_event_loop_init done"); + esp_exceptions(esp_netif_init()); initialized = 1; } return mp_const_none; @@ -145,11 +91,11 @@ STATIC mp_obj_t esp_initialize() { MP_DEFINE_CONST_FUN_OBJ_0(esp_network_initialize_obj, esp_initialize); STATIC mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) { - wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]); - tcpip_adapter_ip_info_t info; - tcpip_adapter_dns_info_t dns_info; - tcpip_adapter_get_ip_info(self->if_id, &info); - tcpip_adapter_get_dns_info(self->if_id, DNS_MAIN, &dns_info); + base_if_obj_t *self = MP_OBJ_TO_PTR(args[0]); + esp_netif_ip_info_t info; + esp_netif_dns_info_t dns_info; + esp_netif_get_ip_info(self->netif, &info); + esp_netif_get_dns_info(self->netif, ESP_NETIF_DNS_MAIN, &dns_info); if (n_args == 1) { // get mp_obj_t tuple[4] = { @@ -171,36 +117,36 @@ STATIC mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) { // 16 -> 255.255.0.0 // etc... uint32_t *m = (uint32_t *)&info.netmask; - *m = htonl(0xffffffff << (32 - mp_obj_get_int(items[1]))); + *m = esp_netif_htonl(0xffffffff << (32 - mp_obj_get_int(items[1]))); } else { netutils_parse_ipv4_addr(items[1], (void *)&info.netmask, NETUTILS_BIG); } netutils_parse_ipv4_addr(items[2], (void *)&info.gw, NETUTILS_BIG); netutils_parse_ipv4_addr(items[3], (void *)&dns_info.ip, NETUTILS_BIG); // To set a static IP we have to disable DHCP first - if (self->if_id == WIFI_IF_STA || self->if_id == ESP_IF_ETH) { - esp_err_t e = tcpip_adapter_dhcpc_stop(self->if_id); - if (e != ESP_OK && e != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED) { + if (self->if_id == ESP_IF_WIFI_STA || self->if_id == ESP_IF_ETH) { + esp_err_t e = esp_netif_dhcpc_stop(self->netif); + if (e != ESP_OK && e != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) { esp_exceptions_helper(e); } - esp_exceptions(tcpip_adapter_set_ip_info(self->if_id, &info)); - esp_exceptions(tcpip_adapter_set_dns_info(self->if_id, DNS_MAIN, &dns_info)); - } else if (self->if_id == WIFI_IF_AP) { - esp_err_t e = tcpip_adapter_dhcps_stop(WIFI_IF_AP); - if (e != ESP_OK && e != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED) { + esp_exceptions(esp_netif_set_ip_info(self->netif, &info)); + esp_exceptions(esp_netif_set_dns_info(self->netif, ESP_NETIF_DNS_MAIN, &dns_info)); + } else if (self->if_id == ESP_IF_WIFI_AP) { + esp_err_t e = esp_netif_dhcps_stop(self->netif); + if (e != ESP_OK && e != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) { esp_exceptions_helper(e); } - esp_exceptions(tcpip_adapter_set_ip_info(WIFI_IF_AP, &info)); - esp_exceptions(tcpip_adapter_set_dns_info(WIFI_IF_AP, DNS_MAIN, &dns_info)); - esp_exceptions(tcpip_adapter_dhcps_start(WIFI_IF_AP)); + esp_exceptions(esp_netif_set_ip_info(self->netif, &info)); + esp_exceptions(esp_netif_set_dns_info(self->netif, ESP_NETIF_DNS_MAIN, &dns_info)); + esp_exceptions(esp_netif_dhcps_start(self->netif)); } } else { // check for the correct string const char *mode = mp_obj_str_get_str(args[1]); - if ((self->if_id != WIFI_IF_STA && self->if_id != ESP_IF_ETH) || strcmp("dhcp", mode)) { + if ((self->if_id != ESP_IF_WIFI_STA && self->if_id != ESP_IF_ETH) || strcmp("dhcp", mode)) { mp_raise_ValueError(MP_ERROR_TEXT("invalid arguments")); } - esp_exceptions(tcpip_adapter_dhcpc_start(self->if_id)); + esp_exceptions(esp_netif_dhcpc_start(self->netif)); } return mp_const_none; } @@ -212,9 +158,4 @@ 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(4, 3, 0) -#define TEST_WIFI_AUTH_MAX 9 -#else -#define TEST_WIFI_AUTH_MAX 8 -#endif -_Static_assert(WIFI_AUTH_MAX == TEST_WIFI_AUTH_MAX, "Synchronize WIFI_AUTH_XXX constants with the ESP-IDF. Look at esp-idf/components/esp_wifi/include/esp_wifi_types.h"); +_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_lan.c b/ports/esp32/network_lan.c index 9b7a31cc4f9d3..8128eb5e11d49 100644 --- a/ports/esp32/network_lan.c +++ b/ports/esp32/network_lan.c @@ -32,7 +32,6 @@ #include "esp_idf_version.h" -// LAN only for ESP32 (not ESP32S2) and only for ESP-IDF v4.1 and higher #if MICROPY_PY_NETWORK_LAN #include "esp_eth.h" @@ -47,8 +46,7 @@ #include "modnetwork.h" typedef struct _lan_if_obj_t { - mp_obj_base_t base; - int if_id; // MUST BE FIRST to match wlan_if_obj_t + base_if_obj_t base; bool initialized; bool active; int8_t mdc_pin; @@ -59,12 +57,11 @@ typedef struct _lan_if_obj_t { uint8_t phy_addr; uint8_t phy_type; esp_eth_phy_t *phy; - esp_netif_t *eth_netif; esp_eth_handle_t eth_handle; } lan_if_obj_t; const mp_obj_type_t lan_if_type; -STATIC lan_if_obj_t lan_obj = {{&lan_if_type}, ESP_IF_ETH, false, false}; +STATIC lan_if_obj_t lan_obj = {{{&lan_if_type}, ESP_IF_ETH, NULL}, false, false}; STATIC uint8_t eth_status = 0; static void eth_event_handler(void *arg, esp_event_base_t event_base, @@ -114,11 +111,8 @@ STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar { MP_QSTR_spi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, { MP_QSTR_cs, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, { MP_QSTR_int, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, - #if ESP_IDF_VERSION_MINOR >= 4 - // Dynamic ref_clk configuration available at v4.4 { MP_QSTR_ref_clk_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, { MP_QSTR_ref_clk, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, - #endif }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; @@ -147,12 +141,8 @@ STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar args[ARG_phy_type].u_int != PHY_LAN8720 && args[ARG_phy_type].u_int != PHY_IP101 && args[ARG_phy_type].u_int != PHY_RTL8201 && - #if ESP_IDF_VERSION_MINOR >= 3 // KSZ8041 is new in ESP-IDF v4.3 args[ARG_phy_type].u_int != PHY_KSZ8041 && - #endif - #if ESP_IDF_VERSION_MINOR >= 4 // KSZ8081 is new in ESP-IDF v4.4 args[ARG_phy_type].u_int != PHY_KSZ8081 && - #endif #if CONFIG_ETH_USE_SPI_ETHERNET #if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL args[ARG_phy_type].u_int != PHY_KSZ8851SNL && @@ -169,17 +159,21 @@ STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar } eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG(); + #if CONFIG_IDF_TARGET_ESP32 + eth_esp32_emac_config_t esp32_config = ETH_ESP32_EMAC_DEFAULT_CONFIG(); + #endif + esp_eth_mac_t *mac = NULL; - // Dynamic ref_clk configuration available at v4.4 - #if ESP_IDF_VERSION_MINOR >= 4 + #if CONFIG_IDF_TARGET_ESP32 + // Dynamic ref_clk configuration. if (args[ARG_ref_clk_mode].u_int != -1) { // Map the GPIO_MODE constants to EMAC_CLK constants. - mac_config.clock_config.rmii.clock_mode = + esp32_config.clock_config.rmii.clock_mode = args[ARG_ref_clk_mode].u_int == GPIO_MODE_INPUT ? EMAC_CLK_EXT_IN : EMAC_CLK_OUT; } if (args[ARG_ref_clk].u_obj != mp_const_none) { - mac_config.clock_config.rmii.clock_gpio = machine_pin_get_id(args[ARG_ref_clk].u_obj); + esp32_config.clock_config.rmii.clock_gpio = machine_pin_get_id(args[ARG_ref_clk].u_obj); } #endif @@ -224,7 +218,7 @@ STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar #if CONFIG_IDF_TARGET_ESP32 case PHY_LAN8710: case PHY_LAN8720: - self->phy = esp_eth_phy_new_lan8720(&phy_config); + self->phy = esp_eth_phy_new_lan87xx(&phy_config); break; case PHY_IP101: self->phy = esp_eth_phy_new_ip101(&phy_config); @@ -235,17 +229,11 @@ STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar case PHY_DP83848: self->phy = esp_eth_phy_new_dp83848(&phy_config); break; - #if ESP_IDF_VERSION_MINOR >= 3 // KSZ8041 is new in ESP-IDF v4.3 case PHY_KSZ8041: - self->phy = esp_eth_phy_new_ksz8041(&phy_config); - break; - #endif - #if ESP_IDF_VERSION_MINOR >= 4 // KSZ8081 is new in ESP-IDF v4.4 case PHY_KSZ8081: - self->phy = esp_eth_phy_new_ksz8081(&phy_config); + self->phy = esp_eth_phy_new_ksz80xx(&phy_config); break; #endif - #endif #if CONFIG_ETH_USE_SPI_ETHERNET #if CONFIG_ETH_SPI_ETHERNET_KSZ8851SNL case PHY_KSZ8851SNL: { @@ -282,9 +270,9 @@ STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar if (self->mdc_pin == -1 || self->mdio_pin == -1) { mp_raise_ValueError(MP_ERROR_TEXT("mdc and mdio must be specified")); } - mac_config.smi_mdc_gpio_num = self->mdc_pin; - mac_config.smi_mdio_gpio_num = self->mdio_pin; - mac = esp_eth_mac_new_esp32(&mac_config); + esp32_config.smi_mdc_gpio_num = self->mdc_pin; + esp32_config.smi_mdio_gpio_num = self->mdio_pin; + mac = esp_eth_mac_new_esp32(&esp32_config, &mac_config); } #endif @@ -293,11 +281,7 @@ STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar } esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH(); - self->eth_netif = esp_netif_new(&cfg); - - if (esp_eth_set_default_handlers(self->eth_netif) != ESP_OK) { - mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_eth_set_default_handlers failed (invalid parameter)")); - } + self->base.netif = esp_netif_new(&cfg); if (esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, ð_event_handler, NULL) != ESP_OK) { mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_event_handler_register failed")); @@ -323,7 +307,7 @@ STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar } } - if (esp_netif_attach(self->eth_netif, esp_eth_new_netif_glue(self->eth_handle)) != ESP_OK) { + if (esp_netif_attach(self->base.netif, esp_eth_new_netif_glue(self->eth_handle)) != ESP_OK) { mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("esp_netif_attach failed")); } @@ -384,7 +368,7 @@ STATIC mp_obj_t lan_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs } if ( (esp_eth_ioctl(self->eth_handle, ETH_CMD_S_MAC_ADDR, bufinfo.buf) != ESP_OK) || - (esp_netif_set_mac(self->eth_netif, bufinfo.buf) != ESP_OK) + (esp_netif_set_mac(self->base.netif, bufinfo.buf) != ESP_OK) ) { mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("failed setting MAC address")); } diff --git a/ports/esp32/network_ppp.c b/ports/esp32/network_ppp.c index b3cab835981e1..caad7eb48bfe5 100644 --- a/ports/esp32/network_ppp.c +++ b/ports/esp32/network_ppp.c @@ -32,6 +32,7 @@ #include "py/stream.h" #include "shared/netutils/netutils.h" #include "modmachine.h" +#include "ppp_set_auth.h" #include "netif/ppp/ppp.h" #include "netif/ppp/pppos.h" diff --git a/ports/esp32/network_wlan.c b/ports/esp32/network_wlan.c index e56d73b237bfd..63f01dfc25f5f 100644 --- a/ports/esp32/network_wlan.c +++ b/ports/esp32/network_wlan.c @@ -41,7 +41,6 @@ #include "esp_wifi.h" #include "esp_log.h" -#include "mdns.h" #if MICROPY_PY_NETWORK_WLAN @@ -49,8 +48,10 @@ #error WIFI_MODE_STA and WIFI_MODE_AP are supposed to be bitfields! #endif -STATIC const wlan_if_obj_t wlan_sta_obj; -STATIC const wlan_if_obj_t wlan_ap_obj; +typedef base_if_obj_t wlan_if_obj_t; + +STATIC wlan_if_obj_t wlan_sta_obj; +STATIC wlan_if_obj_t wlan_ap_obj; // Set to "true" if esp_wifi_start() was called static bool wifi_started = false; @@ -75,34 +76,22 @@ static uint8_t wifi_sta_reconnects; // This function is called by the system-event task and so runs in a different // thread to the main MicroPython task. It must not raise any Python exceptions. -void network_wlan_event_handler(system_event_t *event) { - switch (event->event_id) { - case SYSTEM_EVENT_STA_START: +static void network_wlan_wifi_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { + switch (event_id) { + case WIFI_EVENT_STA_START: ESP_LOGI("wifi", "STA_START"); wifi_sta_reconnects = 0; break; - case SYSTEM_EVENT_STA_CONNECTED: + + case WIFI_EVENT_STA_CONNECTED: ESP_LOGI("network", "CONNECTED"); break; - case SYSTEM_EVENT_STA_GOT_IP: - ESP_LOGI("network", "GOT_IP"); - wifi_sta_connected = true; - wifi_sta_disconn_reason = 0; // Success so clear error. (in case of new error will be replaced anyway) - #if MICROPY_HW_ENABLE_MDNS_QUERIES || MICROPY_HW_ENABLE_MDNS_RESPONDER - if (!mdns_initialised) { - mdns_init(); - #if MICROPY_HW_ENABLE_MDNS_RESPONDER - mdns_hostname_set(mod_network_hostname); - mdns_instance_name_set(mod_network_hostname); - #endif - mdns_initialised = true; - } - #endif - break; - case SYSTEM_EVENT_STA_DISCONNECTED: { + + case WIFI_EVENT_STA_DISCONNECTED: { // This is a workaround as ESP32 WiFi libs don't currently // auto-reassociate. - system_event_sta_disconnected_t *disconn = &event->event_info.disconnected; + + wifi_event_sta_disconnected_t *disconn = event_data; char *message = ""; wifi_sta_disconn_reason = disconn->reason; switch (disconn->reason) { @@ -152,20 +141,55 @@ void network_wlan_event_handler(system_event_t *event) { } } +static void network_wlan_ip_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { + switch (event_id) { + case IP_EVENT_STA_GOT_IP: + ESP_LOGI("network", "GOT_IP"); + wifi_sta_connected = true; + wifi_sta_disconn_reason = 0; // Success so clear error. (in case of new error will be replaced anyway) + #if MICROPY_HW_ENABLE_MDNS_QUERIES || MICROPY_HW_ENABLE_MDNS_RESPONDER + if (!mdns_initialised) { + mdns_init(); + #if MICROPY_HW_ENABLE_MDNS_RESPONDER + mdns_hostname_set(mod_network_hostname); + mdns_instance_name_set(mod_network_hostname); + #endif + mdns_initialised = true; + } + #endif + break; + + default: + break; + } +} + STATIC void require_if(mp_obj_t wlan_if, int if_no) { wlan_if_obj_t *self = MP_OBJ_TO_PTR(wlan_if); if (self->if_id != if_no) { - mp_raise_msg(&mp_type_OSError, if_no == WIFI_IF_STA ? MP_ERROR_TEXT("STA required") : MP_ERROR_TEXT("AP required")); + mp_raise_msg(&mp_type_OSError, if_no == ESP_IF_WIFI_STA ? MP_ERROR_TEXT("STA required") : MP_ERROR_TEXT("AP required")); } } -void esp_initialise_wifi() { +void esp_initialise_wifi(void) { static int wifi_initialized = 0; if (!wifi_initialized) { + esp_exceptions(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, network_wlan_wifi_event_handler, NULL, NULL)); + esp_exceptions(esp_event_handler_instance_register(IP_EVENT, ESP_EVENT_ANY_ID, network_wlan_ip_event_handler, NULL, NULL)); + + wlan_sta_obj.base.type = &esp_network_wlan_type; + wlan_sta_obj.if_id = ESP_IF_WIFI_STA; + wlan_sta_obj.netif = esp_netif_create_default_wifi_sta(); + + wlan_ap_obj.base.type = &esp_network_wlan_type; + wlan_ap_obj.if_id = ESP_IF_WIFI_AP; + wlan_ap_obj.netif = esp_netif_create_default_wifi_ap(); + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_LOGD("modnetwork", "Initializing WiFi"); esp_exceptions(esp_wifi_init(&cfg)); esp_exceptions(esp_wifi_set_storage(WIFI_STORAGE_RAM)); + ESP_LOGD("modnetwork", "Initialized"); wifi_initialized = 1; } @@ -176,10 +200,10 @@ STATIC mp_obj_t network_wlan_make_new(const mp_obj_type_t *type, size_t n_args, esp_initialise_wifi(); - int idx = (n_args > 0) ? mp_obj_get_int(args[0]) : WIFI_IF_STA; - if (idx == WIFI_IF_STA) { + int idx = (n_args > 0) ? mp_obj_get_int(args[0]) : ESP_IF_WIFI_STA; + if (idx == ESP_IF_WIFI_STA) { return MP_OBJ_FROM_PTR(&wlan_sta_obj); - } else if (idx == WIFI_IF_AP) { + } else if (idx == ESP_IF_WIFI_AP) { return MP_OBJ_FROM_PTR(&wlan_ap_obj); } else { mp_raise_ValueError(MP_ERROR_TEXT("invalid WLAN interface identifier")); @@ -196,7 +220,7 @@ STATIC mp_obj_t network_wlan_active(size_t n_args, const mp_obj_t *args) { esp_exceptions(esp_wifi_get_mode(&mode)); } - int bit = (self->if_id == WIFI_IF_STA) ? WIFI_MODE_STA : WIFI_MODE_AP; + int bit = (self->if_id == ESP_IF_WIFI_STA) ? WIFI_MODE_STA : WIFI_MODE_AP; if (n_args > 1) { bool active = mp_obj_is_true(args[1]); @@ -262,7 +286,7 @@ STATIC mp_obj_t network_wlan_connect(size_t n_args, const mp_obj_t *pos_args, mp esp_exceptions(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_sta_config)); } - esp_exceptions(tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, mod_network_hostname)); + esp_exceptions(esp_netif_set_hostname(wlan_sta_obj.netif, mod_network_hostname)); wifi_sta_reconnects = 0; // connect to the WiFi AP @@ -285,7 +309,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(network_wlan_disconnect_obj, network_wlan_disco STATIC mp_obj_t network_wlan_status(size_t n_args, const mp_obj_t *args) { wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]); if (n_args == 1) { - if (self->if_id == WIFI_IF_STA) { + if (self->if_id == ESP_IF_WIFI_STA) { // Case of no arg is only for the STA interface if (wifi_sta_connected) { // Happy path, connected with IP @@ -310,7 +334,7 @@ STATIC mp_obj_t network_wlan_status(size_t n_args, const mp_obj_t *args) { switch ((uintptr_t)args[1]) { case (uintptr_t)MP_OBJ_NEW_QSTR(MP_QSTR_stations): { // return list of connected stations, only if in soft-AP mode - require_if(args[0], WIFI_IF_AP); + require_if(args[0], ESP_IF_WIFI_AP); wifi_sta_list_t station_list; esp_exceptions(esp_wifi_ap_get_sta_list(&station_list)); wifi_sta_info_t *stations = (wifi_sta_info_t *)station_list.sta; @@ -324,7 +348,7 @@ STATIC mp_obj_t network_wlan_status(size_t n_args, const mp_obj_t *args) { } case (uintptr_t)MP_OBJ_NEW_QSTR(MP_QSTR_rssi): { // return signal of AP, only in STA mode - require_if(args[0], WIFI_IF_STA); + require_if(args[0], ESP_IF_WIFI_STA); wifi_ap_record_t info; esp_exceptions(esp_wifi_sta_get_ap_info(&info)); @@ -383,7 +407,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(network_wlan_scan_obj, network_wlan_scan); STATIC mp_obj_t network_wlan_isconnected(mp_obj_t self_in) { wlan_if_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (self->if_id == WIFI_IF_STA) { + if (self->if_id == ESP_IF_WIFI_STA) { return mp_obj_new_bool(wifi_sta_connected); } else { wifi_sta_list_t sta; @@ -400,7 +424,7 @@ STATIC mp_obj_t network_wlan_config(size_t n_args, const mp_obj_t *args, mp_map_ wlan_if_obj_t *self = MP_OBJ_TO_PTR(args[0]); - bool is_wifi = self->if_id == WIFI_IF_AP || self->if_id == WIFI_IF_STA; + bool is_wifi = self->if_id == ESP_IF_WIFI_AP || self->if_id == ESP_IF_WIFI_STA; wifi_config_t cfg; if (is_wifi) { @@ -428,7 +452,7 @@ STATIC mp_obj_t network_wlan_config(size_t n_args, const mp_obj_t *args, mp_map_ } case MP_QSTR_ssid: case MP_QSTR_essid: { - req_if = WIFI_IF_AP; + req_if = ESP_IF_WIFI_AP; size_t len; const char *s = mp_obj_str_get_data(kwargs->table[i].value, &len); len = MIN(len, sizeof(cfg.ap.ssid)); @@ -437,19 +461,19 @@ STATIC mp_obj_t network_wlan_config(size_t n_args, const mp_obj_t *args, mp_map_ break; } case MP_QSTR_hidden: { - req_if = WIFI_IF_AP; + req_if = ESP_IF_WIFI_AP; cfg.ap.ssid_hidden = mp_obj_is_true(kwargs->table[i].value); break; } case MP_QSTR_security: case MP_QSTR_authmode: { - req_if = WIFI_IF_AP; + req_if = ESP_IF_WIFI_AP; cfg.ap.authmode = mp_obj_get_int(kwargs->table[i].value); break; } case MP_QSTR_key: case MP_QSTR_password: { - req_if = WIFI_IF_AP; + req_if = ESP_IF_WIFI_AP; size_t len; const char *s = mp_obj_str_get_data(kwargs->table[i].value, &len); len = MIN(len, sizeof(cfg.ap.password) - 1); @@ -488,13 +512,13 @@ STATIC mp_obj_t network_wlan_config(size_t n_args, const mp_obj_t *args, mp_map_ break; } case MP_QSTR_max_clients: { - req_if = WIFI_IF_AP; + req_if = ESP_IF_WIFI_AP; cfg.ap.max_connection = mp_obj_get_int(kwargs->table[i].value); break; } case MP_QSTR_reconnects: { int reconnects = mp_obj_get_int(kwargs->table[i].value); - req_if = WIFI_IF_STA; + req_if = ESP_IF_WIFI_STA; // parameter reconnects == -1 means to retry forever. // here means conf_wifi_sta_reconnects == 0 to retry forever. conf_wifi_sta_reconnects = (reconnects == -1) ? 0 : reconnects + 1; @@ -542,8 +566,8 @@ STATIC mp_obj_t network_wlan_config(size_t n_args, const mp_obj_t *args, mp_map_ case MP_QSTR_mac: { uint8_t mac[6]; switch (self->if_id) { - case WIFI_IF_AP: // fallthrough intentional - case WIFI_IF_STA: + case ESP_IF_WIFI_AP: // fallthrough intentional + case ESP_IF_WIFI_STA: esp_exceptions(esp_wifi_get_mac(self->if_id, mac)); return mp_obj_new_bytes(mac, sizeof(mac)); default: @@ -553,23 +577,23 @@ STATIC mp_obj_t network_wlan_config(size_t n_args, const mp_obj_t *args, mp_map_ case MP_QSTR_ssid: case MP_QSTR_essid: switch (self->if_id) { - case WIFI_IF_STA: + case ESP_IF_WIFI_STA: val = mp_obj_new_str((char *)cfg.sta.ssid, strlen((char *)cfg.sta.ssid)); break; - case WIFI_IF_AP: + case ESP_IF_WIFI_AP: val = mp_obj_new_str((char *)cfg.ap.ssid, cfg.ap.ssid_len); break; default: - req_if = WIFI_IF_AP; + req_if = ESP_IF_WIFI_AP; } break; case MP_QSTR_hidden: - req_if = WIFI_IF_AP; + req_if = ESP_IF_WIFI_AP; val = mp_obj_new_bool(cfg.ap.ssid_hidden); break; case MP_QSTR_security: case MP_QSTR_authmode: - req_if = WIFI_IF_AP; + req_if = ESP_IF_WIFI_AP; val = MP_OBJ_NEW_SMALL_INT(cfg.ap.authmode); break; case MP_QSTR_channel: { @@ -582,7 +606,7 @@ STATIC mp_obj_t network_wlan_config(size_t n_args, const mp_obj_t *args, mp_map_ case MP_QSTR_hostname: case MP_QSTR_dhcp_hostname: { // TODO: Deprecated. Use network.hostname() instead. - req_if = WIFI_IF_STA; + req_if = ESP_IF_WIFI_STA; val = mp_obj_new_str(mod_network_hostname, strlen(mod_network_hostname)); break; } @@ -591,7 +615,7 @@ STATIC mp_obj_t network_wlan_config(size_t n_args, const mp_obj_t *args, mp_map_ break; } case MP_QSTR_reconnects: - req_if = WIFI_IF_STA; + req_if = ESP_IF_WIFI_STA; int rec = conf_wifi_sta_reconnects - 1; val = MP_OBJ_NEW_SMALL_INT(rec); break; @@ -654,7 +678,4 @@ MP_DEFINE_CONST_OBJ_TYPE( locals_dict, &wlan_if_locals_dict ); -STATIC const wlan_if_obj_t wlan_sta_obj = {{&esp_network_wlan_type}, WIFI_IF_STA}; -STATIC const wlan_if_obj_t wlan_ap_obj = {{&esp_network_wlan_type}, WIFI_IF_AP}; - #endif // MICROPY_PY_NETWORK_WLAN diff --git a/ports/esp32/uart.h b/ports/esp32/uart.h index 14ad30ccd84ea..6410db24c9577 100644 --- a/ports/esp32/uart.h +++ b/ports/esp32/uart.h @@ -30,7 +30,7 @@ // Whether to enable the REPL on a UART. #ifndef MICROPY_HW_ENABLE_UART_REPL -#define MICROPY_HW_ENABLE_UART_REPL (!CONFIG_USB_ENABLED && !CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG) +#define MICROPY_HW_ENABLE_UART_REPL (!CONFIG_USB_OTG_SUPPORTED && !CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG) #endif #ifndef MICROPY_HW_UART_REPL diff --git a/ports/esp32/usb.c b/ports/esp32/usb.c index 80612cd276e74..83ba9533d3802 100644 --- a/ports/esp32/usb.c +++ b/ports/esp32/usb.c @@ -28,14 +28,17 @@ #include "py/mphal.h" #include "usb.h" -#if CONFIG_USB_ENABLED +#if CONFIG_USB_OTG_SUPPORTED +#include "esp_timer.h" +#ifndef NO_QSTR #include "tinyusb.h" #include "tusb_cdc_acm.h" +#endif #define CDC_ITF TINYUSB_CDC_ACM_0 -static uint8_t usb_rx_buf[CONFIG_USB_CDC_RX_BUFSIZE]; +static uint8_t usb_rx_buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE]; static void usb_callback_rx(int itf, cdcacm_event_t *event) { // TODO: what happens if more chars come in during this function, are they lost? @@ -79,7 +82,7 @@ void usb_init(void) { void usb_tx_strn(const char *str, size_t len) { // Write out the data to the CDC interface, but only while the USB host is connected. - uint64_t timeout = esp_timer_get_time() + (uint64_t)(MICROPY_HW_USB_CDC_TX_TIMEOUT * 1000); + uint64_t timeout = esp_timer_get_time() + (uint64_t)(MICROPY_HW_USB_CDC_TX_TIMEOUT_MS * 1000); while (tud_cdc_n_connected(CDC_ITF) && len && esp_timer_get_time() < timeout) { size_t l = tinyusb_cdcacm_write_queue(CDC_ITF, (uint8_t *)str, len); str += l; @@ -88,4 +91,4 @@ void usb_tx_strn(const char *str, size_t len) { } } -#endif // CONFIG_USB_ENABLED +#endif // CONFIG_USB_OTG_SUPPORTED diff --git a/ports/esp32/usb.h b/ports/esp32/usb.h index 009bf42624d98..a4c7d40701dfc 100644 --- a/ports/esp32/usb.h +++ b/ports/esp32/usb.h @@ -26,7 +26,7 @@ #ifndef MICROPY_INCLUDED_ESP32_USB_H #define MICROPY_INCLUDED_ESP32_USB_H -#define MICROPY_HW_USB_CDC_TX_TIMEOUT (500) +#define MICROPY_HW_USB_CDC_TX_TIMEOUT_MS (500) void usb_init(void); void usb_tx_strn(const char *str, size_t len); diff --git a/ports/esp32/usb_serial_jtag.c b/ports/esp32/usb_serial_jtag.c index a7d06a355a2ea..3289a1b5c0783 100644 --- a/ports/esp32/usb_serial_jtag.c +++ b/ports/esp32/usb_serial_jtag.c @@ -79,9 +79,9 @@ void usb_serial_jtag_tx_strn(const char *str, size_t len) { if (l > USB_SERIAL_JTAG_PACKET_SZ_BYTES) { l = USB_SERIAL_JTAG_PACKET_SZ_BYTES; } - portTickType start_tick = xTaskGetTickCount(); + TickType_t start_tick = xTaskGetTickCount(); while (!usb_serial_jtag_ll_txfifo_writable()) { - portTickType now_tick = xTaskGetTickCount(); + TickType_t now_tick = xTaskGetTickCount(); if (!terminal_connected || now_tick > (start_tick + pdMS_TO_TICKS(200))) { terminal_connected = false; return; From 2cc3711e5e00c13d43771ee8a78778e7ef3c2930 Mon Sep 17 00:00:00 2001 From: Glenn Moloney Date: Thu, 25 May 2023 10:40:50 +1000 Subject: [PATCH 08/12] esp32: In recv_cb, get espnow rssi from recv_info->rx_ctrl. IDF v5.0 provides access to rssi value for received espnow packets via recv_info arg to recv_cb(). Signed-off-by: Glenn Moloney --- ports/esp32/modespnow.c | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/ports/esp32/modespnow.c b/ports/esp32/modespnow.c index c94fc81708a14..08836c0ad790e 100644 --- a/ports/esp32/modespnow.c +++ b/ports/esp32/modespnow.c @@ -313,21 +313,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(espnow_stats_obj, espnow_stats); // - to return unique bytestrings for each peer which supports more efficient // application memory usage and peer handling. -// Get the RSSI value from the wifi packet header -static inline int8_t _get_rssi_from_wifi_pkt(const uint8_t *msg) { - // Warning: Secret magic to get the rssi from the wifi packet header - // See espnow.c:espnow_recv_cb() at https://github.com/espressif/esp-now/ - // In the wifi packet the msg comes after a wifi_promiscuous_pkt_t - // and a espnow_frame_format_t. - // Backtrack to get a pointer to the wifi_promiscuous_pkt_t. - static const size_t sizeof_espnow_frame_format = 39; - wifi_promiscuous_pkt_t *wifi_pkt = - (wifi_promiscuous_pkt_t *)(msg - sizeof_espnow_frame_format - - sizeof(wifi_promiscuous_pkt_t)); - - return wifi_pkt->rx_ctrl.rssi; -} - // Lookup a peer in the peers table and return a reference to the item in the // peers_table. Add peer to the table if it is not found (may alloc memory). // Will not return NULL. @@ -577,7 +562,7 @@ STATIC void recv_cb(const esp_now_recv_info_t *recv_info, const uint8_t *msg, in header.magic = ESPNOW_MAGIC; header.msg_len = msg_len; #if MICROPY_ESPNOW_RSSI - header.rssi = _get_rssi_from_wifi_pkt(msg); + header.rssi = recv_info->rx_ctrl->rssi; header.time_ms = mp_hal_ticks_ms(); #endif // MICROPY_ESPNOW_RSSI From bccbaa92b1fc6237f0f49a7f07cc194835fbf4e3 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 21 Jun 2023 17:08:08 +1000 Subject: [PATCH 09/12] esp32/network_wlan: Wait for WIFI_EVENT_STA_START after activating. Signed-off-by: Damien George --- ports/esp32/network_wlan.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ports/esp32/network_wlan.c b/ports/esp32/network_wlan.c index 63f01dfc25f5f..cfedd898cdf24 100644 --- a/ports/esp32/network_wlan.c +++ b/ports/esp32/network_wlan.c @@ -72,7 +72,7 @@ static bool mdns_initialised = false; #endif static uint8_t conf_wifi_sta_reconnects = 0; -static uint8_t wifi_sta_reconnects; +static volatile uint8_t wifi_sta_reconnects; // This function is called by the system-event task and so runs in a different // thread to the main MicroPython task. It must not raise any Python exceptions. @@ -233,16 +233,16 @@ STATIC mp_obj_t network_wlan_active(size_t n_args, const mp_obj_t *args) { } else { esp_exceptions(esp_wifi_set_mode(mode)); if (!wifi_started) { + // WIFI_EVENT_STA_START must be received before esp_wifi_connect() can be called. + // Use the `wifi_sta_reconnects` variable to detect that event. + wifi_sta_reconnects = 1; esp_exceptions(esp_wifi_start()); wifi_started = true; + while (wifi_sta_reconnects != 0) { + MICROPY_EVENT_POLL_HOOK; + } } } - // This delay is a band-aid patch for issues #8289, #8792 and #9236, - // allowing the esp data structures to settle. It looks like some - // kind of race condition, which is not yet found. But at least - // this small delay seems not hurt much, since wlan.active() is - // usually not called in a time critical part of the code. - mp_hal_delay_ms(1); } return (mode & bit) ? mp_const_true : mp_const_false; From 717060447f7ba7e71a7f40b19c1e977e4e444f12 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 14 Jun 2023 12:45:46 +1000 Subject: [PATCH 10/12] esp32/Makefile: Provide more IDF shortcuts. And change erase_flash to erase-flash, because the former is deprecated since IDF 4.4. Signed-off-by: Damien George --- ports/esp32/Makefile | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile index 0b33fa6e5a735..7c8c225dfb6e9 100644 --- a/ports/esp32/Makefile +++ b/ports/esp32/Makefile @@ -48,6 +48,10 @@ endif HELP_BUILD_ERROR ?= "See \033[1;31mhttps://github.com/micropython/micropython/wiki/Build-Troubleshooting\033[0m" +define RUN_IDF_PY + idf.py $(IDFPY_FLAGS) -p $(PORT) -b $(BAUD) $(1) +endef + all: idf.py $(IDFPY_FLAGS) build || (echo -e $(HELP_BUILD_ERROR); false) @$(PYTHON) makeimg.py \ @@ -61,13 +65,25 @@ all: $(BUILD)/bootloader/bootloader.bin $(BUILD)/partition_table/partition-table.bin $(BUILD)/micropython.bin: FORCE clean: - idf.py $(IDFPY_FLAGS) fullclean + $(call RUN_IDF_PY,fullclean) deploy: - idf.py $(IDFPY_FLAGS) -p $(PORT) -b $(BAUD) flash + $(call RUN_IDF_PY,flash) erase: - idf.py $(IDFPY_FLAGS) -p $(PORT) -b $(BAUD) erase_flash + $(call RUN_IDF_PY,erase-flash) + +monitor: + $(call RUN_IDF_PY,monitor) + +size: + $(call RUN_IDF_PY,size) + +size-components: + $(call RUN_IDF_PY,size-components) + +size-files: + $(call RUN_IDF_PY,size-files) submodules: $(MAKE) -f ../../py/mkrules.mk GIT_SUBMODULES="$(GIT_SUBMODULES)" submodules From 68e0e889b43c4c381515691b689b5f02f037063f Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 21 Jun 2023 11:55:22 +1000 Subject: [PATCH 11/12] docs/esp32: Update esp32 docs based on IDF v5 changes. Signed-off-by: Damien George --- docs/esp32/quickref.rst | 19 ------------------- docs/library/esp32.rst | 4 ---- docs/library/machine.WDT.rst | 5 ++--- 3 files changed, 2 insertions(+), 26 deletions(-) diff --git a/docs/esp32/quickref.rst b/docs/esp32/quickref.rst index e19c6ecb40893..f4eb9a0ce8ca2 100644 --- a/docs/esp32/quickref.rst +++ b/docs/esp32/quickref.rst @@ -57,7 +57,6 @@ The :mod:`esp32` module:: import esp32 - esp32.hall_sensor() # read the internal hall sensor esp32.raw_temperature() # read the internal temperature of the MCU, in Fahrenheit esp32.ULP() # access to the Ultra-Low-Power Co-processor @@ -137,19 +136,11 @@ The keyword arguments for the constructor defining the PHY type and interface ar or output. Suitable values are Pin.IN and Pin.OUT. - ref_clk=pin-object # defines the Pin used for ref_clk. -The options ref_clk_mode and ref_clk require at least esp-idf version 4.4. For -earlier esp-idf versions, these parameters must be defined by kconfig board options. - These are working configurations for LAN interfaces of popular boards:: # Olimex ESP32-GATEWAY: power controlled by Pin(5) # Olimex ESP32 PoE and ESP32-PoE ISO: power controlled by Pin(12) - lan = network.LAN(mdc=machine.Pin(23), mdio=machine.Pin(18), power=machine.Pin(5), - phy_type=network.PHY_LAN8720, phy_addr=0) - - # or with dynamic ref_clk pin configuration - lan = network.LAN(mdc=machine.Pin(23), mdio=machine.Pin(18), power=machine.Pin(5), phy_type=network.PHY_LAN8720, phy_addr=0, ref_clk=machine.Pin(17), ref_clk_mode=machine.Pin.OUT) @@ -164,16 +155,6 @@ These are working configurations for LAN interfaces of popular boards:: lan = network.LAN(id=0, mdc=Pin(23), mdio=Pin(18), power=Pin(5), phy_type=network.PHY_IP101, phy_addr=1) -A suitable definition of the PHY interface in a sdkconfig.board file is:: - - CONFIG_ETH_PHY_INTERFACE_RMII=y - CONFIG_ETH_RMII_CLK_OUTPUT=y - CONFIG_ETH_RMII_CLK_OUT_GPIO=17 - CONFIG_LWIP_LOCAL_HOSTNAME="ESP32_POE" - -The value assigned to CONFIG_ETH_RMII_CLK_OUT_GPIO may vary depending on the -board's wiring. - Delay and timing ---------------- diff --git a/docs/library/esp32.rst b/docs/library/esp32.rst index d9241d545c46b..efdd6c1be289c 100644 --- a/docs/library/esp32.rst +++ b/docs/library/esp32.rst @@ -44,10 +44,6 @@ Functions Read the raw value of the internal temperature sensor, returning an integer. -.. function:: hall_sensor() - - Read the raw value of the internal Hall sensor, returning an integer. - .. function:: idf_heap_info(capabilities) Returns information about the ESP-IDF heap memory regions. One of them contains diff --git a/docs/library/machine.WDT.rst b/docs/library/machine.WDT.rst index 95893cec36be7..cf77df9632088 100644 --- a/docs/library/machine.WDT.rst +++ b/docs/library/machine.WDT.rst @@ -25,9 +25,8 @@ Constructors Create a WDT object and start it. The timeout must be given in milliseconds. Once it is running the timeout cannot be changed and the WDT cannot be stopped either. - Notes: On the esp32 the minimum timeout is 1 second. On the esp8266 a timeout - cannot be specified, it is determined by the underlying system. On rp2040 devices, - the maximum timeout is 8388 ms. + Notes: On the esp8266 a timeout cannot be specified, it is determined by the underlying system. + On rp2040 devices, the maximum timeout is 8388 ms. Methods ------- From 6a9db521eda2f62766749d84244c70fd2073d4a0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 17 May 2023 14:15:10 +1000 Subject: [PATCH 12/12] github/workflows: Update esp32 CI to use IDF v5.0. Signed-off-by: Damien George --- .github/workflows/ports_esp32.yml | 13 ++--------- tools/ci.sh | 38 ++++--------------------------- 2 files changed, 7 insertions(+), 44 deletions(-) diff --git a/.github/workflows/ports_esp32.yml b/.github/workflows/ports_esp32.yml index 6fc009d4fee83..2cc9f592bf23e 100644 --- a/.github/workflows/ports_esp32.yml +++ b/.github/workflows/ports_esp32.yml @@ -18,20 +18,11 @@ concurrency: cancel-in-progress: true jobs: - build_idf402: + build_idf50: runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v3 - name: Install packages - run: source tools/ci.sh && ci_esp32_idf402_setup - - name: Build - run: source tools/ci.sh && ci_esp32_build - - build_idf44: - runs-on: ubuntu-20.04 - steps: - - uses: actions/checkout@v3 - - name: Install packages - run: source tools/ci.sh && ci_esp32_idf44_setup + run: source tools/ci.sh && ci_esp32_idf50_setup - name: Build run: source tools/ci.sh && ci_esp32_build diff --git a/tools/ci.sh b/tools/ci.sh index 986fe8f074de8..84ca83e9b8aa4 100755 --- a/tools/ci.sh +++ b/tools/ci.sh @@ -115,35 +115,13 @@ function ci_cc3200_build { ######################################################################################## # ports/esp32 -function ci_esp32_setup_helper { +function ci_esp32_idf50_setup { pip3 install pyelftools git clone https://github.com/espressif/esp-idf.git - git -C esp-idf checkout $1 - git -C esp-idf submodule update --init \ - components/bt/host/nimble/nimble \ - components/esp_wifi \ - components/esptool_py/esptool \ - components/lwip/lwip \ - components/mbedtls/mbedtls - if [ -d esp-idf/components/bt/controller/esp32 ]; then - git -C esp-idf submodule update --init \ - components/bt/controller/lib_esp32 \ - components/bt/controller/lib_esp32c3_family - else - git -C esp-idf submodule update --init \ - components/bt/controller/lib - fi + git -C esp-idf checkout v5.0.2 ./esp-idf/install.sh } -function ci_esp32_idf402_setup { - ci_esp32_setup_helper v4.0.2 -} - -function ci_esp32_idf44_setup { - ci_esp32_setup_helper v4.4.2 -} - function ci_esp32_build { source esp-idf/export.sh make ${MAKEOPTS} -C mpy-cross @@ -151,15 +129,9 @@ function ci_esp32_build { make ${MAKEOPTS} -C ports/esp32 \ USER_C_MODULES=../../../examples/usercmodule/micropython.cmake \ FROZEN_MANIFEST=$(pwd)/ports/esp32/boards/manifest_test.py - if [ -d $IDF_PATH/components/esp32c3 ]; then - make ${MAKEOPTS} -C ports/esp32 BOARD=GENERIC_C3 - fi - if [ -d $IDF_PATH/components/esp32s2 ]; then - make ${MAKEOPTS} -C ports/esp32 BOARD=GENERIC_S2 - fi - if [ -d $IDF_PATH/components/esp32s3 ]; then - make ${MAKEOPTS} -C ports/esp32 BOARD=GENERIC_S3 - fi + make ${MAKEOPTS} -C ports/esp32 BOARD=GENERIC_C3 + make ${MAKEOPTS} -C ports/esp32 BOARD=GENERIC_S2 + make ${MAKEOPTS} -C ports/esp32 BOARD=GENERIC_S3 # Test building native .mpy with xtensawin architecture. ci_native_mpy_modules_build xtensawin