|
40 | 40 | #include "stm32f4xx_hal.h" |
41 | 41 |
|
42 | 42 | //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? |
45 | 43 | #if HAS_DAC |
46 | 44 | DAC_HandleTypeDef handle; |
47 | 45 | #endif |
48 | 46 |
|
| 47 | +STATIC bool dac_on[2]; |
| 48 | + |
49 | 49 | void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self, |
50 | 50 | const mcu_pin_obj_t *pin) { |
51 | 51 | #if !(HAS_DAC) |
52 | 52 | mp_raise_ValueError(translate("No DAC on chip")); |
53 | 53 | #else |
54 | 54 | if (pin == &pin_PA04) { |
55 | 55 | self->channel = DAC_CHANNEL_1; |
| 56 | + self->dac_index = 0; |
56 | 57 | } else if (pin == &pin_PA05) { |
57 | 58 | self->channel = DAC_CHANNEL_2; |
| 59 | + self->dac_index = 1; |
58 | 60 | } else { |
59 | 61 | mp_raise_ValueError(translate("Invalid DAC pin supplied")); |
60 | 62 | } |
@@ -82,22 +84,27 @@ void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self, |
82 | 84 | mp_raise_ValueError(translate("DAC Channel Init Error")); |
83 | 85 | } |
84 | 86 |
|
| 87 | + dac_on[self->dac_index] = true; |
85 | 88 | self->pin = pin; |
86 | | - self->deinited = false; |
87 | 89 | claim_pin(pin); |
88 | 90 | #endif |
89 | 91 | } |
90 | 92 |
|
91 | 93 | bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) { |
92 | | - return self->deinited; |
| 94 | + return !dac_on[self->dac_index]; |
93 | 95 | } |
94 | 96 |
|
95 | 97 | void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) { |
96 | 98 | #if HAS_DAC |
97 | 99 | reset_pin_number(self->pin->port,self->pin->number); |
98 | 100 | 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 | + } |
101 | 108 | #endif |
102 | 109 | } |
103 | 110 |
|
|
0 commit comments