Skip to content

Commit 147a1bb

Browse files
authored
Merge pull request #2268 from hierophect/stm32-dac-deinit
STM32: DAC auto shutoff
2 parents b8373ac + d42c4b0 commit 147a1bb

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

ports/stm32f4/common-hal/analogio/AnalogOut.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,23 @@
4040
#include "stm32f4xx_hal.h"
4141

4242
//DAC is shared between both channels.
43-
//TODO: store as struct with channel info, automatically turn it off if unused
44-
//on both channels for power save?
4543
#if HAS_DAC
4644
DAC_HandleTypeDef handle;
4745
#endif
4846

47+
STATIC bool dac_on[2];
48+
4949
void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self,
5050
const mcu_pin_obj_t *pin) {
5151
#if !(HAS_DAC)
5252
mp_raise_ValueError(translate("No DAC on chip"));
5353
#else
5454
if (pin == &pin_PA04) {
5555
self->channel = DAC_CHANNEL_1;
56+
self->dac_index = 0;
5657
} else if (pin == &pin_PA05) {
5758
self->channel = DAC_CHANNEL_2;
59+
self->dac_index = 1;
5860
} else {
5961
mp_raise_ValueError(translate("Invalid DAC pin supplied"));
6062
}
@@ -82,22 +84,27 @@ void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self,
8284
mp_raise_ValueError(translate("DAC Channel Init Error"));
8385
}
8486

87+
dac_on[self->dac_index] = true;
8588
self->pin = pin;
86-
self->deinited = false;
8789
claim_pin(pin);
8890
#endif
8991
}
9092

9193
bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) {
92-
return self->deinited;
94+
return !dac_on[self->dac_index];
9395
}
9496

9597
void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) {
9698
#if HAS_DAC
9799
reset_pin_number(self->pin->port,self->pin->number);
98100
self->pin = mp_const_none;
99-
self->deinited = true;
100-
//TODO: if both are de-inited, should we turn off the DAC?
101+
dac_on[self->dac_index] = false;
102+
103+
//turn off the DAC if both channels are off
104+
if(dac_on[0] == false && dac_on[1] == false) {
105+
__HAL_RCC_DAC_CLK_DISABLE();
106+
HAL_DAC_DeInit(&handle);
107+
}
101108
#endif
102109
}
103110

ports/stm32f4/common-hal/analogio/AnalogOut.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ typedef struct {
4141
#endif
4242
const mcu_pin_obj_t * pin;
4343
uint8_t channel;
44-
bool deinited;
44+
uint8_t dac_index:1;
4545
} analogio_analogout_obj_t;
4646

4747
void analogout_reset(void);

0 commit comments

Comments
 (0)