Skip to content

Commit b52b3b6

Browse files
committed
analogout: STM32: analogout_free implementation for F4
Implemented analogout_free for STM32F4 family and added new member "PinName pin" in dac_s structure.
1 parent d83494f commit b52b3b6

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

targets/TARGET_STM/TARGET_STM32F4/analogout_device.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
#include "stm32f4xx_hal.h"
2424
#include "PeripheralPins.h"
2525

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+
2632
#if STATIC_PINMAP_READY
2733
#define ANALOGOUT_INIT_DIRECT analogout_init_direct
2834
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)
6268
pin_function(pinmap->pin, pinmap->function);
6369
pin_mode(pinmap->pin, PullNone);
6470

71+
// Save the pin for future use
72+
obj->pin = pinmap->pin;
73+
6574
__HAL_RCC_GPIOA_CLK_ENABLE();
6675

6776
__HAL_RCC_DAC_CLK_ENABLE();
@@ -80,6 +89,14 @@ static void _analogout_init_direct(dac_t *obj, const PinMap *pinmap)
8089
error("HAL_DAC_ConfigChannel failed");
8190
}
8291

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
83100
analogout_write_u16(obj, 0);
84101
HAL_DAC_Start(&obj->handle, obj->channel);
85102
}
@@ -96,6 +113,28 @@ void analogout_init(dac_t *obj, PinName pin)
96113

97114
void analogout_free(dac_t *obj)
98115
{
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+
}
99138
}
100139

101140
const PinMap *analogout_pinmap()

targets/TARGET_STM/TARGET_STM32F4/objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ struct analogin_s {
141141
#if DEVICE_ANALOGOUT
142142
struct dac_s {
143143
DACName dac;
144+
PinName pin;
144145
uint32_t channel;
145146
DAC_HandleTypeDef handle;
146147
};

0 commit comments

Comments
 (0)