Merge pull request #2268 from hierophect/stm32-dac-deinit

STM32: DAC auto shutoff
This commit is contained in:
hierophect 2019-11-07 09:38:19 -05:00 committed by GitHub
commit 147a1bb860
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 7 deletions

View File

@ -40,12 +40,12 @@
#include "stm32f4xx_hal.h" #include "stm32f4xx_hal.h"
//DAC is shared between both channels. //DAC is shared between both channels.
//TODO: store as struct with channel info, automatically turn it off if unused
//on both channels for power save?
#if HAS_DAC #if HAS_DAC
DAC_HandleTypeDef handle; DAC_HandleTypeDef handle;
#endif #endif
STATIC bool dac_on[2];
void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self, void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self,
const mcu_pin_obj_t *pin) { const mcu_pin_obj_t *pin) {
#if !(HAS_DAC) #if !(HAS_DAC)
@ -53,8 +53,10 @@ void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self,
#else #else
if (pin == &pin_PA04) { if (pin == &pin_PA04) {
self->channel = DAC_CHANNEL_1; self->channel = DAC_CHANNEL_1;
self->dac_index = 0;
} else if (pin == &pin_PA05) { } else if (pin == &pin_PA05) {
self->channel = DAC_CHANNEL_2; self->channel = DAC_CHANNEL_2;
self->dac_index = 1;
} else { } else {
mp_raise_ValueError(translate("Invalid DAC pin supplied")); mp_raise_ValueError(translate("Invalid DAC pin supplied"));
} }
@ -82,22 +84,27 @@ void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self,
mp_raise_ValueError(translate("DAC Channel Init Error")); mp_raise_ValueError(translate("DAC Channel Init Error"));
} }
dac_on[self->dac_index] = true;
self->pin = pin; self->pin = pin;
self->deinited = false;
claim_pin(pin); claim_pin(pin);
#endif #endif
} }
bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) { 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) { void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) {
#if HAS_DAC #if HAS_DAC
reset_pin_number(self->pin->port,self->pin->number); reset_pin_number(self->pin->port,self->pin->number);
self->pin = mp_const_none; self->pin = mp_const_none;
self->deinited = true; dac_on[self->dac_index] = false;
//TODO: if both are de-inited, should we turn off the DAC?
//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 #endif
} }

View File

@ -41,7 +41,7 @@ typedef struct {
#endif #endif
const mcu_pin_obj_t * pin; const mcu_pin_obj_t * pin;
uint8_t channel; uint8_t channel;
bool deinited; uint8_t dac_index:1;
} analogio_analogout_obj_t; } analogio_analogout_obj_t;
void analogout_reset(void); void analogout_reset(void);