Skip to content

Commit

Permalink
Merge branch 'idf_v5.1' into OWL_v1.18
Browse files Browse the repository at this point in the history
  • Loading branch information
IhorNehrutsa committed Nov 16, 2023
2 parents 51e1064 + 6178740 commit 2704dfd
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 11 deletions.
13 changes: 13 additions & 0 deletions docs/esp32/quickref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,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)
----------------------------------

Expand Down
2 changes: 1 addition & 1 deletion ports/esp32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
35 changes: 30 additions & 5 deletions ports/esp32/machine_dac.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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);

Expand All @@ -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
2 changes: 1 addition & 1 deletion ports/esp32/machine_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions ports/esp32/machine_i2s.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,22 +354,22 @@ 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;
i2s_config.mclk_multiple = I2S_MCLK_MULTIPLE_256;
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
Expand Down
5 changes: 5 additions & 0 deletions ports/esp32/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions ports/esp32/modnetwork_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions ports/esp32/network_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,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

0 comments on commit 2704dfd

Please sign in to comment.