23
23
#include "stm32f4xx_hal.h"
24
24
#include "PeripheralPins.h"
25
25
26
+ // These variables are used for the "free" function
27
+ static int dac_channel1_used = 0 ;
28
+ #if defined(DAC_CHANNEL_2 )
29
+ static int dac_channel2_used = 0 ;
30
+ #endif
31
+
26
32
#if STATIC_PINMAP_READY
27
33
#define ANALOGOUT_INIT_DIRECT analogout_init_direct
28
34
void analogout_init_direct (dac_t * obj , const PinMap * pinmap )
@@ -62,6 +68,9 @@ static void _analogout_init_direct(dac_t *obj, const PinMap *pinmap)
62
68
pin_function (pinmap -> pin , pinmap -> function );
63
69
pin_mode (pinmap -> pin , PullNone );
64
70
71
+ // Save the pin for future use
72
+ obj -> pin = pinmap -> pin ;
73
+
65
74
__HAL_RCC_GPIOA_CLK_ENABLE ();
66
75
67
76
__HAL_RCC_DAC_CLK_ENABLE ();
@@ -80,6 +89,14 @@ static void _analogout_init_direct(dac_t *obj, const PinMap *pinmap)
80
89
error ("HAL_DAC_ConfigChannel failed" );
81
90
}
82
91
92
+ if (obj -> channel == DAC_CHANNEL_1 ) {
93
+ dac_channel1_used = 1 ;
94
+ }
95
+ #if defined(DAC_CHANNEL_2 )
96
+ if (obj -> channel == DAC_CHANNEL_2 ) {
97
+ dac_channel2_used = 1 ;
98
+ }
99
+ #endif
83
100
analogout_write_u16 (obj , 0 );
84
101
HAL_DAC_Start (& obj -> handle , obj -> channel );
85
102
}
@@ -96,6 +113,28 @@ void analogout_init(dac_t *obj, PinName pin)
96
113
97
114
void analogout_free (dac_t * obj )
98
115
{
116
+ if (obj -> channel == DAC_CHANNEL_1 ) {
117
+ dac_channel1_used = 0 ;
118
+ }
119
+ #if defined(DAC_CHANNEL_2 )
120
+ if (obj -> channel == DAC_CHANNEL_2 ) {
121
+ dac_channel2_used = 0 ;
122
+ }
123
+ #endif
124
+
125
+ if ((dac_channel1_used == 0 )
126
+ #if defined(DAC_CHANNEL_2 )
127
+ & & (dac_channel2_used == 0 )
128
+ #endif
129
+ ) {
130
+ // Reset DAC and disable clock
131
+ __HAL_RCC_DAC_FORCE_RESET ();
132
+ __HAL_RCC_DAC_RELEASE_RESET ();
133
+ __HAL_RCC_DAC_CLK_DISABLE ();
134
+
135
+ // Configure GPIO back to reset value
136
+ pin_function (obj -> pin , STM_PIN_DATA (STM_MODE_ANALOG , GPIO_NOPULL , 0 ));
137
+ }
99
138
}
100
139
101
140
const PinMap * analogout_pinmap ()
0 commit comments