Skip to content

STM32: DAC auto shutoff #2268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions ports/stm32f4/common-hal/analogio/AnalogOut.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,23 @@
#include "stm32f4xx_hal.h"

//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
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)
mp_raise_ValueError(translate("No DAC on chip"));
#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"));
}
Expand Down Expand Up @@ -82,22 +84,27 @@ void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self,
mp_raise_ValueError(translate("DAC Channel Init Error"));
}

dac_on[self->dac_index] = true;
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
}

Expand Down
2 changes: 1 addition & 1 deletion ports/stm32f4/common-hal/analogio/AnalogOut.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down