Add auto-shutoff of DAC when channels de-inited

This commit is contained in:
Hierophect 2019-11-04 17:08:00 -05:00
parent b9063dd226
commit 632b3267dc
2 changed files with 14 additions and 5 deletions

View File

@ -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
}

View File

@ -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);