diff --git a/ports/esp32s2/common-hal/analogio/AnalogOut.c b/ports/esp32s2/common-hal/analogio/AnalogOut.c index b23c405142..a85a171837 100644 --- a/ports/esp32s2/common-hal/analogio/AnalogOut.c +++ b/ports/esp32s2/common-hal/analogio/AnalogOut.c @@ -35,25 +35,39 @@ #include "shared-bindings/microcontroller/Pin.h" #include "supervisor/shared/translate.h" +#include "components/driver/include/driver/dac.h" + #include "common-hal/microcontroller/Pin.h" void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self, const mcu_pin_obj_t *pin) { - mp_raise_NotImplementedError(translate("No DAC on chip")); + if (pin == &pin_GPIO17) { + self->channel = DAC_CHANNEL_1; + } else if (pin == &pin_GPIO18) { + self->channel = DAC_CHANNEL_2; + } else { + mp_raise_ValueError(translate("Invalid DAC pin supplied")); + } + dac_output_enable(self->channel); } bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) { - return true; + return (self->channel == DAC_CHANNEL_MAX); } void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) { - + dac_output_disable(self->channel); + self->channel = DAC_CHANNEL_MAX; } void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self, uint16_t value) { + uint8_t dac_value = (value * 255) / 65535; + dac_output_enable(self->channel); + dac_output_voltage(self->channel, dac_value); } void analogout_reset(void) { - + dac_output_disable(DAC_CHANNEL_1); + dac_output_disable(DAC_CHANNEL_2); } diff --git a/ports/esp32s2/common-hal/analogio/AnalogOut.h b/ports/esp32s2/common-hal/analogio/AnalogOut.h index a8cbc39d5f..6285151ba0 100644 --- a/ports/esp32s2/common-hal/analogio/AnalogOut.h +++ b/ports/esp32s2/common-hal/analogio/AnalogOut.h @@ -36,7 +36,6 @@ typedef struct { mp_obj_base_t base; const mcu_pin_obj_t * pin; uint8_t channel; - uint8_t dac_index:1; } analogio_analogout_obj_t; void analogout_reset(void); diff --git a/ports/esp32s2/supervisor/port.c b/ports/esp32s2/supervisor/port.c index 4290b9bb4a..0b9c03f747 100644 --- a/ports/esp32s2/supervisor/port.c +++ b/ports/esp32s2/supervisor/port.c @@ -35,6 +35,7 @@ #include "freertos/task.h" #include "common-hal/microcontroller/Pin.h" +#include "common-hal/analogio/AnalogOut.h" #include "common-hal/busio/I2C.h" #include "common-hal/busio/SPI.h" #include "common-hal/busio/UART.h" @@ -95,6 +96,10 @@ void reset_port(void) { // A larger delay so the idle task can run and do any IDF cleanup needed. vTaskDelay(4); +#if CIRCUITPY_ANALOGIO + analogout_reset(); +#endif + #if CIRCUITPY_PULSEIO esp32s2_peripherals_rmt_reset(); pulsein_reset();