From 495be71d56337a7b162b4fe97d4407d97f87d9f0 Mon Sep 17 00:00:00 2001 From: IhorNehrutsa Date: Sun, 12 Nov 2023 20:46:26 +0200 Subject: [PATCH] esp32/machine_dac: Support one-shot mode of driver. And simplify board configuration of DAC by using SOC_DAC_SUPPORTED. Signed-off-by: IhorNehrutsa --- .../boards/ARDUINO_NANO_ESP32/mpconfigboard.h | 2 -- .../boards/ESP32_GENERIC_C3/mpconfigboard.h | 1 - .../boards/ESP32_GENERIC_S3/mpconfigboard.h | 2 -- .../boards/LOLIN_C3_MINI/mpconfigboard.h | 1 - .../esp32/boards/UM_FEATHERS3/mpconfigboard.h | 2 -- ports/esp32/boards/UM_NANOS3/mpconfigboard.h | 2 -- ports/esp32/boards/UM_PROS3/mpconfigboard.h | 2 -- ports/esp32/boards/UM_TINYS3/mpconfigboard.h | 2 -- .../boards/UM_TINYWATCHS3/mpconfigboard.h | 2 -- ports/esp32/machine_dac.c | 31 ++++++++++++++++--- ports/esp32/mpconfigport.h | 2 +- 11 files changed, 27 insertions(+), 22 deletions(-) diff --git a/ports/esp32/boards/ARDUINO_NANO_ESP32/mpconfigboard.h b/ports/esp32/boards/ARDUINO_NANO_ESP32/mpconfigboard.h index ec85a7bb36..873838352e 100644 --- a/ports/esp32/boards/ARDUINO_NANO_ESP32/mpconfigboard.h +++ b/ports/esp32/boards/ARDUINO_NANO_ESP32/mpconfigboard.h @@ -4,8 +4,6 @@ // Network config #define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-nano-esp32" -#define MICROPY_PY_MACHINE_DAC (0) - #define MICROPY_HW_I2C0_SCL (12) #define MICROPY_HW_I2C0_SDA (11) diff --git a/ports/esp32/boards/ESP32_GENERIC_C3/mpconfigboard.h b/ports/esp32/boards/ESP32_GENERIC_C3/mpconfigboard.h index 42e77ecb1a..0dbfae03a3 100644 --- a/ports/esp32/boards/ESP32_GENERIC_C3/mpconfigboard.h +++ b/ports/esp32/boards/ESP32_GENERIC_C3/mpconfigboard.h @@ -4,7 +4,6 @@ #define MICROPY_HW_MCU_NAME "ESP32C3" #define MICROPY_HW_ENABLE_SDCARD (0) -#define MICROPY_PY_MACHINE_DAC (0) #define MICROPY_PY_MACHINE_I2S (0) // Enable UART REPL for modules that have an external USB-UART and don't use native USB. diff --git a/ports/esp32/boards/ESP32_GENERIC_S3/mpconfigboard.h b/ports/esp32/boards/ESP32_GENERIC_S3/mpconfigboard.h index 8d369c7db0..c714fe2c22 100644 --- a/ports/esp32/boards/ESP32_GENERIC_S3/mpconfigboard.h +++ b/ports/esp32/boards/ESP32_GENERIC_S3/mpconfigboard.h @@ -4,8 +4,6 @@ #endif #define MICROPY_HW_MCU_NAME "ESP32S3" -#define MICROPY_PY_MACHINE_DAC (0) - // Enable UART REPL for modules that have an external USB-UART and don't use native USB. #define MICROPY_HW_ENABLE_UART_REPL (1) diff --git a/ports/esp32/boards/LOLIN_C3_MINI/mpconfigboard.h b/ports/esp32/boards/LOLIN_C3_MINI/mpconfigboard.h index 12fd285da1..9b304b69f6 100644 --- a/ports/esp32/boards/LOLIN_C3_MINI/mpconfigboard.h +++ b/ports/esp32/boards/LOLIN_C3_MINI/mpconfigboard.h @@ -3,7 +3,6 @@ #define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-c3-mini" #define MICROPY_HW_ENABLE_SDCARD (0) -#define MICROPY_PY_MACHINE_DAC (0) #define MICROPY_PY_MACHINE_I2S (0) #define MICROPY_HW_I2C0_SCL (10) diff --git a/ports/esp32/boards/UM_FEATHERS3/mpconfigboard.h b/ports/esp32/boards/UM_FEATHERS3/mpconfigboard.h index 488a588442..f4abfb21bc 100644 --- a/ports/esp32/boards/UM_FEATHERS3/mpconfigboard.h +++ b/ports/esp32/boards/UM_FEATHERS3/mpconfigboard.h @@ -2,8 +2,6 @@ #define MICROPY_HW_MCU_NAME "ESP32-S3" #define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "FeatherS3" -#define MICROPY_PY_MACHINE_DAC (0) - #define MICROPY_HW_I2C0_SCL (9) #define MICROPY_HW_I2C0_SDA (8) diff --git a/ports/esp32/boards/UM_NANOS3/mpconfigboard.h b/ports/esp32/boards/UM_NANOS3/mpconfigboard.h index 1c2782bfb7..44197be2a8 100644 --- a/ports/esp32/boards/UM_NANOS3/mpconfigboard.h +++ b/ports/esp32/boards/UM_NANOS3/mpconfigboard.h @@ -2,8 +2,6 @@ #define MICROPY_HW_MCU_NAME "ESP32-S3-FN8" #define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "NanoS3" -#define MICROPY_PY_MACHINE_DAC (0) - #define MICROPY_HW_I2C0_SCL (9) #define MICROPY_HW_I2C0_SDA (8) diff --git a/ports/esp32/boards/UM_PROS3/mpconfigboard.h b/ports/esp32/boards/UM_PROS3/mpconfigboard.h index ecff1e6570..cc0ebbefa3 100644 --- a/ports/esp32/boards/UM_PROS3/mpconfigboard.h +++ b/ports/esp32/boards/UM_PROS3/mpconfigboard.h @@ -2,8 +2,6 @@ #define MICROPY_HW_MCU_NAME "ESP32-S3" #define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "ProS3" -#define MICROPY_PY_MACHINE_DAC (0) - #define MICROPY_HW_I2C0_SCL (9) #define MICROPY_HW_I2C0_SDA (8) diff --git a/ports/esp32/boards/UM_TINYS3/mpconfigboard.h b/ports/esp32/boards/UM_TINYS3/mpconfigboard.h index c0f81f0c44..74c7622cb4 100644 --- a/ports/esp32/boards/UM_TINYS3/mpconfigboard.h +++ b/ports/esp32/boards/UM_TINYS3/mpconfigboard.h @@ -2,8 +2,6 @@ #define MICROPY_HW_MCU_NAME "ESP32-S3-FN8" #define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "TinyS3" -#define MICROPY_PY_MACHINE_DAC (0) - #define MICROPY_HW_I2C0_SCL (9) #define MICROPY_HW_I2C0_SDA (8) diff --git a/ports/esp32/boards/UM_TINYWATCHS3/mpconfigboard.h b/ports/esp32/boards/UM_TINYWATCHS3/mpconfigboard.h index ce8de58a01..6cc1345413 100644 --- a/ports/esp32/boards/UM_TINYWATCHS3/mpconfigboard.h +++ b/ports/esp32/boards/UM_TINYWATCHS3/mpconfigboard.h @@ -2,8 +2,6 @@ #define MICROPY_HW_MCU_NAME "ESP32-S3-PICO-1-N8R2" #define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "TinyWATCHS3" -#define MICROPY_PY_MACHINE_DAC (0) - #define MICROPY_HW_I2C0_SCL (9) #define MICROPY_HW_I2C0_SDA (8) diff --git a/ports/esp32/machine_dac.c b/ports/esp32/machine_dac.c index 0e85dc9c9b..34f51cc521 100644 --- a/ports/esp32/machine_dac.c +++ b/ports/esp32/machine_dac.c @@ -34,21 +34,30 @@ #if MICROPY_PY_MACHINE_DAC #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 +77,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 +91,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 +106,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); diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index ff6f6ab8d0..aeaff3e503 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -123,7 +123,7 @@ #define MICROPY_PY_MACHINE_SPI_LSB (1) #define MICROPY_PY_MACHINE_SOFTSPI (1) #ifndef MICROPY_PY_MACHINE_DAC -#define MICROPY_PY_MACHINE_DAC (1) +#define MICROPY_PY_MACHINE_DAC (SOC_DAC_SUPPORTED) #endif #ifndef MICROPY_PY_MACHINE_I2S #define MICROPY_PY_MACHINE_I2S (1)