From 632b3267dc33ec8f822159860bd012851b0ef977 Mon Sep 17 00:00:00 2001 From: Hierophect Date: Mon, 4 Nov 2019 17:08:00 -0500 Subject: [PATCH] Add auto-shutoff of DAC when channels de-inited --- ports/stm32f4/common-hal/analogio/AnalogOut.c | 17 +++++++++++++---- ports/stm32f4/common-hal/analogio/AnalogOut.h | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/ports/stm32f4/common-hal/analogio/AnalogOut.c b/ports/stm32f4/common-hal/analogio/AnalogOut.c index 5eb8c2dc03..fd9608112d 100644 --- a/ports/stm32f4/common-hal/analogio/AnalogOut.c +++ b/ports/stm32f4/common-hal/analogio/AnalogOut.c @@ -46,6 +46,8 @@ DAC_HandleTypeDef handle; #endif +STATIC bool dac_on[2]; + void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self, const mcu_pin_obj_t *pin) { #if !(HAS_DAC) @@ -53,11 +55,14 @@ void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self, #else if (pin == &pin_PA04) { self->channel = DAC_CHANNEL_1; + self->dac_index = 0; } else if (pin == &pin_PA05) { self->channel = DAC_CHANNEL_2; + self->dac_index = 1; } else { mp_raise_ValueError(translate("Invalid DAC pin supplied")); } + dac_on[self->dac_index] = true; //Only init if the shared DAC is empty or reset if (handle.Instance == NULL || handle.State == HAL_DAC_STATE_RESET) { @@ -83,21 +88,25 @@ void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self, } self->pin = pin; - self->deinited = false; claim_pin(pin); #endif } bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) { - return self->deinited; + return !dac_on[self->dac_index]; } void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) { #if HAS_DAC reset_pin_number(self->pin->port,self->pin->number); self->pin = mp_const_none; - self->deinited = true; - //TODO: if both are de-inited, should we turn off the DAC? + dac_on[self->dac_index] = false; + + //turn off the DAC if both channels are off + if(dac_on[0] == false && dac_on[1] == false) { + __HAL_RCC_DAC_CLK_DISABLE(); + HAL_DAC_DeInit(&handle); + } #endif } diff --git a/ports/stm32f4/common-hal/analogio/AnalogOut.h b/ports/stm32f4/common-hal/analogio/AnalogOut.h index 1381d4d3a7..61591d0e65 100644 --- a/ports/stm32f4/common-hal/analogio/AnalogOut.h +++ b/ports/stm32f4/common-hal/analogio/AnalogOut.h @@ -41,7 +41,7 @@ typedef struct { #endif const mcu_pin_obj_t * pin; uint8_t channel; - bool deinited; + uint8_t dac_index:1; } analogio_analogout_obj_t; void analogout_reset(void);