Skip to content

Commit 632b326

Browse files
committed
Add auto-shutoff of DAC when channels de-inited
1 parent b9063dd commit 632b326

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,23 @@
4646
DAC_HandleTypeDef handle;
4747
#endif
4848

49+
STATIC bool dac_on[2];
50+
4951
void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self,
5052
const mcu_pin_obj_t *pin) {
5153
#if !(HAS_DAC)
5254
mp_raise_ValueError(translate("No DAC on chip"));
5355
#else
5456
if (pin == &pin_PA04) {
5557
self->channel = DAC_CHANNEL_1;
58+
self->dac_index = 0;
5659
} else if (pin == &pin_PA05) {
5760
self->channel = DAC_CHANNEL_2;
61+
self->dac_index = 1;
5862
} else {
5963
mp_raise_ValueError(translate("Invalid DAC pin supplied"));
6064
}
65+
dac_on[self->dac_index] = true;
6166

6267
//Only init if the shared DAC is empty or reset
6368
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,
8388
}
8489

8590
self->pin = pin;
86-
self->deinited = false;
8791
claim_pin(pin);
8892
#endif
8993
}
9094

9195
bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) {
92-
return self->deinited;
96+
return !dac_on[self->dac_index];
9397
}
9498

9599
void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) {
96100
#if HAS_DAC
97101
reset_pin_number(self->pin->port,self->pin->number);
98102
self->pin = mp_const_none;
99-
self->deinited = true;
100-
//TODO: if both are de-inited, should we turn off the DAC?
103+
dac_on[self->dac_index] = false;
104+
105+
//turn off the DAC if both channels are off
106+
if(dac_on[0] == false && dac_on[1] == false) {
107+
__HAL_RCC_DAC_CLK_DISABLE();
108+
HAL_DAC_DeInit(&handle);
109+
}
101110
#endif
102111
}
103112

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)