From 9aee7d5f252a0a615c87626cd1d8fdb538b2a51a Mon Sep 17 00:00:00 2001 From: IhorNehrutsa Date: Sun, 12 Nov 2023 20:38:46 +0200 Subject: [PATCH 1/7] esp32/machine_i2c: Use APB_CLK_FREQ instead of I2C_APB_CLK_FREQ. Signed-off-by: IhorNehrutsa --- ports/esp32/machine_i2c.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/esp32/machine_i2c.c b/ports/esp32/machine_i2c.c index 50d5681514ae7..eefdeaaf96583 100644 --- a/ports/esp32/machine_i2c.c +++ b/ports/esp32/machine_i2c.c @@ -50,7 +50,7 @@ #if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 #define I2C_SCLK_FREQ XTAL_CLK_FREQ #elif CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 -#define I2C_SCLK_FREQ I2C_APB_CLK_FREQ +#define I2C_SCLK_FREQ APB_CLK_FREQ #else #error "unsupported I2C for ESP32 SoC variant" #endif From 78c1264e068ebb3201ee6edb09bee8678936e504 Mon Sep 17 00:00:00 2001 From: IhorNehrutsa Date: Sun, 12 Nov 2023 20:43:52 +0200 Subject: [PATCH 2/7] esp32/network: Add WiFi AUTH_WPA3_ENT_192 authenticate mode. Signed-off-by: IhorNehrutsa --- ports/esp32/modnetwork_globals.h | 3 +++ ports/esp32/network_common.c | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/ports/esp32/modnetwork_globals.h b/ports/esp32/modnetwork_globals.h index 7326d453be5aa..c6369ab9a79aa 100644 --- a/ports/esp32/modnetwork_globals.h +++ b/ports/esp32/modnetwork_globals.h @@ -29,6 +29,9 @@ { MP_ROM_QSTR(MP_QSTR_AUTH_WPA2_WPA3_PSK), MP_ROM_INT(WIFI_AUTH_WPA2_WPA3_PSK) }, { MP_ROM_QSTR(MP_QSTR_AUTH_WAPI_PSK), MP_ROM_INT(WIFI_AUTH_WAPI_PSK) }, { MP_ROM_QSTR(MP_QSTR_AUTH_OWE), MP_ROM_INT(WIFI_AUTH_OWE) }, +#if ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(5, 1, 1) +{ MP_ROM_QSTR(MP_QSTR_AUTH_WPA3_ENT_192), MP_ROM_INT(WIFI_AUTH_WPA3_ENT_192) }, +#endif { MP_ROM_QSTR(MP_QSTR_AUTH_MAX), MP_ROM_INT(WIFI_AUTH_MAX) }, #endif diff --git a/ports/esp32/network_common.c b/ports/esp32/network_common.c index ca07f3c06ba18..1c5979175d0a1 100644 --- a/ports/esp32/network_common.c +++ b/ports/esp32/network_common.c @@ -168,4 +168,8 @@ STATIC mp_obj_t esp_phy_mode(size_t n_args, const mp_obj_t *args) { } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_network_phy_mode_obj, 0, 1, esp_phy_mode); +#if ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(5, 1, 1) +_Static_assert(WIFI_AUTH_MAX == 11, "Synchronize WIFI_AUTH_XXX constants with the ESP-IDF. Look at esp-idf/components/esp_wifi/include/esp_wifi_types.h"); +#else _Static_assert(WIFI_AUTH_MAX == 10, "Synchronize WIFI_AUTH_XXX constants with the ESP-IDF. Look at esp-idf/components/esp_wifi/include/esp_wifi_types.h"); +#endif From 3f79896f1daff3494984b06baa6b9d2e71ac0660 Mon Sep 17 00:00:00 2001 From: IhorNehrutsa Date: Sun, 12 Nov 2023 20:46:26 +0200 Subject: [PATCH 3/7] esp32/DAC: Support one-shot mode of driver. Signed-off-by: IhorNehrutsa --- ports/esp32/machine_dac.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/ports/esp32/machine_dac.c b/ports/esp32/machine_dac.c index 0e85dc9c9bfb2..978a1825e8e16 100644 --- a/ports/esp32/machine_dac.c +++ b/ports/esp32/machine_dac.c @@ -32,23 +32,33 @@ #include "modmachine.h" #if MICROPY_PY_MACHINE_DAC +#if SOC_DAC_SUPPORTED #include "driver/gpio.h" +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0) +#include "driver/dac_oneshot.h" +#else #include "driver/dac.h" +#define DAC_CHAN_0 DAC_CHANNEL_1 +#define DAC_CHAN_1 DAC_CHANNEL_2 +#endif typedef struct _mdac_obj_t { mp_obj_base_t base; gpio_num_t gpio_id; dac_channel_t dac_id; + #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0) + dac_oneshot_handle_t dac_oneshot_handle; + #endif } mdac_obj_t; -STATIC const mdac_obj_t mdac_obj[] = { +STATIC mdac_obj_t mdac_obj[] = { #if CONFIG_IDF_TARGET_ESP32 - {{&machine_dac_type}, GPIO_NUM_25, DAC_CHANNEL_1}, - {{&machine_dac_type}, GPIO_NUM_26, DAC_CHANNEL_2}, + {{&machine_dac_type}, GPIO_NUM_25, DAC_CHAN_0}, + {{&machine_dac_type}, GPIO_NUM_26, DAC_CHAN_1}, #else - {{&machine_dac_type}, GPIO_NUM_17, DAC_CHANNEL_1}, - {{&machine_dac_type}, GPIO_NUM_18, DAC_CHANNEL_2}, + {{&machine_dac_type}, GPIO_NUM_17, DAC_CHAN_0}, + {{&machine_dac_type}, GPIO_NUM_18, DAC_CHAN_1}, #endif }; @@ -68,6 +78,12 @@ STATIC mp_obj_t mdac_make_new(const mp_obj_type_t *type, size_t n_args, size_t n mp_raise_ValueError(MP_ERROR_TEXT("invalid Pin for DAC")); } + #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0) + dac_oneshot_config_t dac_oneshot_config = {.chan_id = self->dac_id}; + check_esp_err(dac_oneshot_new_channel(&dac_oneshot_config, (dac_oneshot_handle_t *)&self->dac_oneshot_handle)); + check_esp_err(dac_oneshot_output_voltage(self->dac_oneshot_handle, 0)); + return MP_OBJ_FROM_PTR(self); + #else esp_err_t err = dac_output_enable(self->dac_id); if (err == ESP_OK) { err = dac_output_voltage(self->dac_id, 0); @@ -76,6 +92,7 @@ STATIC mp_obj_t mdac_make_new(const mp_obj_type_t *type, size_t n_args, size_t n return MP_OBJ_FROM_PTR(self); } mp_raise_ValueError(MP_ERROR_TEXT("parameter error")); + #endif } STATIC void mdac_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { @@ -90,11 +107,16 @@ STATIC mp_obj_t mdac_write(mp_obj_t self_in, mp_obj_t value_in) { mp_raise_ValueError(MP_ERROR_TEXT("value out of range")); } + #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0) + check_esp_err(dac_oneshot_output_voltage(self->dac_oneshot_handle, value)); + return mp_const_none; + #else esp_err_t err = dac_output_voltage(self->dac_id, value); if (err == ESP_OK) { return mp_const_none; } mp_raise_ValueError(MP_ERROR_TEXT("parameter error")); + #endif } MP_DEFINE_CONST_FUN_OBJ_2(mdac_write_obj, mdac_write); @@ -113,4 +135,7 @@ MP_DEFINE_CONST_OBJ_TYPE( locals_dict, &mdac_locals_dict ); +#else +#error DAC is not supported! +#endif // SOC_DAC_SUPPORTED #endif // MICROPY_PY_MACHINE_DAC From a361f4b0b3903c0f4abaa78db0289b43ffff2c66 Mon Sep 17 00:00:00 2001 From: IhorNehrutsa Date: Sun, 12 Nov 2023 20:49:07 +0200 Subject: [PATCH 4/7] esp32/README: ESP-IDF v5.1.2 is available. Signed-off-by: IhorNehrutsa --- ports/esp32/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/esp32/README.md b/ports/esp32/README.md index 169de4073bf71..6dfbd45ee9f54 100644 --- a/ports/esp32/README.md +++ b/ports/esp32/README.md @@ -28,7 +28,7 @@ manage the ESP32 microcontroller, as well as a way to manage the required build environment and toolchains needed to build the firmware. The ESP-IDF changes quickly and MicroPython only supports certain versions. -Currently MicroPython supports only v5.0.2. +Currently MicroPython supports v5.0.2, v5.1.2. To install the ESP-IDF the full instructions can be found at the [Espressif Getting Started guide](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/index.html#installation-step-by-step). From 14eaed858a7a0a8462b8bbcdef68eea29758241d Mon Sep 17 00:00:00 2001 From: Ihor Nehrutsa Date: Mon, 13 Nov 2023 12:06:42 +0200 Subject: [PATCH 5/7] esp32/quickref.rst: Add DAC example. Signed-off-by: IhorNehrutsa --- docs/esp32/quickref.rst | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/docs/esp32/quickref.rst b/docs/esp32/quickref.rst index e03a5ffbc9d24..08df25b0554d0 100644 --- a/docs/esp32/quickref.rst +++ b/docs/esp32/quickref.rst @@ -141,7 +141,7 @@ These are working configurations for LAN interfaces of popular boards:: # Olimex ESP32-GATEWAY: power controlled by Pin(5) # Olimex ESP32 PoE and ESP32-PoE ISO: power controlled by Pin(12) - lan = network.LAN(mdc=machine.Pin(23), mdio=machine.Pin(18), power=machine.Pin(5), + lan = network.LAN(mdc=machine.Pin(23), mdio=machine.Pin(18), power=machine.Pin(5), phy_type=network.PHY_LAN8720, phy_addr=0, ref_clk=machine.Pin(17), ref_clk_mode=machine.Pin.OUT) @@ -330,6 +330,19 @@ possible at the same frequency. See more examples in the :ref:`esp32_pwm` tutorial. +DAC (digital to analog conversion) +---------------------------------- + +On the ESP32, DAC functionality is available on pins 25, 26. +On the ESP32S2, DAC functionality is available on pins 17, 18. + +Use the DAC:: + + from machine import DAC, Pin + + dac = DAC(Pin(25)) # create an DAC object acting on a pin + dac.write(128) # set a raw analog value in the range 0-255, 50% now + ADC (analog to digital conversion) ---------------------------------- From 451acf4f635f536145e198d13488103af0668fed Mon Sep 17 00:00:00 2001 From: Ihor Nehrutsa Date: Mon, 13 Nov 2023 13:58:22 +0200 Subject: [PATCH 6/7] esp32/modmachine.c: Fix deprecated esp_pm_config_XXX_t. Co-Authored-By: Trent Piepho <35062987+xyzzy42@users.noreply.github.com> Signed-off-by: IhorNehrutsa --- ports/esp32/modmachine.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ports/esp32/modmachine.c b/ports/esp32/modmachine.c index 461ddacdad1d1..08a2a756e053a 100644 --- a/ports/esp32/modmachine.c +++ b/ports/esp32/modmachine.c @@ -78,6 +78,9 @@ STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { mp_raise_ValueError(MP_ERROR_TEXT("frequency must be 20MHz, 40MHz, 80Mhz, 160MHz or 240MHz")); #endif } + #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0) + esp_pm_config_t pm; + #else #if CONFIG_IDF_TARGET_ESP32 esp_pm_config_esp32_t pm; #elif CONFIG_IDF_TARGET_ESP32C3 @@ -87,6 +90,8 @@ STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { #elif CONFIG_IDF_TARGET_ESP32S3 esp_pm_config_esp32s3_t pm; #endif + #endif + pm.max_freq_mhz = freq; pm.min_freq_mhz = freq; pm.light_sleep_enable = false; From 6178740b21048e240e869f6d7b2bee0efd9d6574 Mon Sep 17 00:00:00 2001 From: Ihor Nehrutsa Date: Mon, 13 Nov 2023 14:07:59 +0200 Subject: [PATCH 7/7] esp32/machine_i2s.c: Fix deprecated fields and constants. Signed-off-by: IhorNehrutsa --- ports/esp32/machine_i2s.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ports/esp32/machine_i2s.c b/ports/esp32/machine_i2s.c index 89d460e452ff3..8a9787c34e859 100644 --- a/ports/esp32/machine_i2s.c +++ b/ports/esp32/machine_i2s.c @@ -354,14 +354,14 @@ STATIC void mp_machine_i2s_init_helper(machine_i2s_obj_t *self, mp_arg_val_t *ar self->io_mode = BLOCKING; i2s_config_t i2s_config; - i2s_config.communication_format = I2S_COMM_FORMAT_I2S; + i2s_config.communication_format = I2S_COMM_FORMAT_STAND_I2S; i2s_config.mode = mode; i2s_config.bits_per_sample = get_dma_bits(mode, bits); i2s_config.channel_format = get_dma_format(mode, format); i2s_config.sample_rate = self->rate; i2s_config.intr_alloc_flags = ESP_INTR_FLAG_LOWMED; - i2s_config.dma_buf_count = get_dma_buf_count(mode, bits, format, self->ibuf); - i2s_config.dma_buf_len = DMA_BUF_LEN_IN_I2S_FRAMES; + i2s_config.dma_desc_num = get_dma_buf_count(mode, bits, format, self->ibuf); + i2s_config.dma_frame_num = DMA_BUF_LEN_IN_I2S_FRAMES; i2s_config.use_apll = false; i2s_config.tx_desc_auto_clear = true; i2s_config.fixed_mclk = 0; @@ -369,7 +369,7 @@ STATIC void mp_machine_i2s_init_helper(machine_i2s_obj_t *self, mp_arg_val_t *ar i2s_config.bits_per_chan = 0; // I2S queue size equals the number of DMA buffers - check_esp_err(i2s_driver_install(self->i2s_id, &i2s_config, i2s_config.dma_buf_count, &self->i2s_event_queue)); + check_esp_err(i2s_driver_install(self->i2s_id, &i2s_config, i2s_config.dma_desc_num, &self->i2s_event_queue)); // apply low-level workaround for bug in some ESP-IDF versions that swap // the left and right channels