From e4650125b88a35f074097f16d84a8f49bd22ac06 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 9 May 2023 09:52:54 +1000 Subject: [PATCH] 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 | 141 ++++++++++++------ ports/esp32/machine_timer.c | 32 +--- ports/esp32/machine_uart.c | 9 -- ports/esp32/machine_wdt.c | 10 +- ports/esp32/main.c | 69 ++------- 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 | 135 ++++++++++------- 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, 491 insertions(+), 681 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 0000000000..a78ecd0191 --- /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 6992737c9b..a4970c1a1b 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 c37213b303..dc9e9f8b72 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 9373a52232..ea263cad9f 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 f0cbad00e4..d9e7c7f61f 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 07e208a09a..7b4313b7de 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 c9726d4232..7b9d932503 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 c9726d4232..7b9d932503 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 c9726d4232..7b9d932503 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 dcffe5f01e..2e1d799b93 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 a2859acb5f..0000000000 --- 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 f0cbad00e4..d9e7c7f61f 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 ccda7bff68..88bc9e72ac 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 556de1f8b4..87a92892d0 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 5e20045125..a4a167de8a 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 06b3a00a1c..74ca6df8f6 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 8ed083e62d..766419c7f8 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 39a2a293ac..5e129e4d8e 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 2b9ddbebe7..0ac3c1f4cb 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 e36884009d..c89a1ed251 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 138de095b4..c74a19c0cf 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 08d5e481f4..91ac3240eb 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 cacaff1197..0000000000 --- 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 33653cc4b1..0000000000 --- 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 5b4ce118b8..74d35f7b4a 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 ef24e90829..fe38846780 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 657edbc580..4090c710e6 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 4311af801c..d55dd38134 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 3cd43e8474..f92af636f5 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 439ee32836..97041c60bd 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 403a3c7dfa..6fa287de28 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 0500726d71..7c9249b072 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 4284b5f8ba..87a5ae53cf 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 36f8d3f069..662d0e5994 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 9ec39e5649..e3b7647790 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 f0163486cd..59b24ae70c 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 f103b96963..5ea41701af 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 445ac80fb2..462d0fa79c 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 5b495f8494..a2d133442f 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() - }; - #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() - }; - #endif - DEBUG_printf(" Setting up SPI slot configuration"); - sdspi_slot_config_t slot_config = slot_defaults[slot_num]; + 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 + spi_dma_chan_t dma_channel = SPI_DMA_CH_AUTO; + #endif + sdspi_device_config_t dev_config = spi_dev_defaults[slot_num]; - 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(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); - DEBUG_printf(" Calling init_slot()"); - check_esp_err(sdspi_host_init_slot(self->host.slot, &slot_config)); + 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 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 c66cb2a48d..5855cfb3e0 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 8e6e5788d0..d9b845047b 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 4ccf417b60..2cb6c51817 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 3e7c9ee162..b8ba03e513 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; + 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); + } - #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; - } - #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 0000000000..40188abff8 --- /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 0000000000..307c0f3218 --- /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 0000000000..40188abff8 --- /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 0000000000..fe3213bd26 --- /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 0000000000..40188abff8 --- /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 0000000000..fe3213bd26 --- /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 f125b614bd..4726ce5874 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 a18ca4c137..ef3ad0b76d 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 c71201e9b7..c94fc81708 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 fc19618b73..01acb01029 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 ac6321d8f6..b1b3fc368a 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 1f657fb5c8..7326d453be 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 731f69fbc2..7484fce786 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 f81ad58fa1..b18681bb57 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 4c8fa012f4..63a674c24b 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 672fa306f5..4f25b3d4fd 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 a58fcbdbf4..8235275be6 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 1e76d679db..082943e2ae 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 9b7a31cc4f..8128eb5e11 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 b3cab83598..caad7eb48b 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 e56d73b237..63f01dfc25 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 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")); +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; } } -void esp_initialise_wifi() { +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 == ESP_IF_WIFI_STA ? MP_ERROR_TEXT("STA required") : MP_ERROR_TEXT("AP required")); + } +} + +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 14ad30ccd8..6410db24c9 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 80612cd276..83ba9533d3 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 009bf42624..a4c7d40701 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 a7d06a355a..3289a1b5c0 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;