Skip to content

Commit

Permalink
esp32/DAC: Support one-shot mode of driver.
Browse files Browse the repository at this point in the history
  • Loading branch information
IhorNehrutsa authored and IhorNehrutsa committed Nov 12, 2023
1 parent d0fcdf2 commit 5887414
Showing 1 changed file with 30 additions and 5 deletions.
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, &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

0 comments on commit 5887414

Please sign in to comment.