Skip to content

Commit cdd581b

Browse files
committed
STM32: analag_out: Use dynamic handle in object rather than static
This allows a proper handling of multiple instances. Also this commit stores the channel in the HAL format so that it can be re-used more easily and call to HAL are straightforward.
1 parent e3e54e5 commit cdd581b

File tree

6 files changed

+25
-28
lines changed

6 files changed

+25
-28
lines changed

targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F302x8/objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ struct dac_s {
6464
DACName dac;
6565
PinName pin;
6666
uint32_t channel;
67+
DAC_HandleTypeDef handle;
6768
};
6869

6970
struct can_s {

targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303x8/objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ struct dac_s {
6464
DACName dac;
6565
PinName pin;
6666
uint32_t channel;
67+
DAC_HandleTypeDef handle;
6768
};
6869

6970
struct can_s {

targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xC/objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ struct analogin_s {
5858
ADCName adc;
5959
PinName pin;
6060
uint32_t channel;
61+
DAC_HandleTypeDef handle;
6162
};
6263

6364
struct dac_s {

targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xE/objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ struct dac_s {
6464
DACName dac;
6565
PinName pin;
6666
uint32_t channel;
67+
DAC_HandleTypeDef handle;
6768
};
6869

6970
struct can_s {

targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F334x8/objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ struct dac_s {
6464
DACName dac;
6565
PinName pin;
6666
uint32_t channel;
67+
DAC_HandleTypeDef handle;
6768
};
6869

6970
#if defined (DEVICE_CAN)

targets/TARGET_STM/TARGET_STM32F3/analogout_api.c

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
#define DAC_RANGE (0xFFF) // 12 bits
3939
#define DAC_NB_BITS (12)
4040

41-
static DAC_HandleTypeDef DacHandle;
42-
4341
// These variables are used for the "free" function
4442
static int pa4_used = 0;
4543
static int pa5_used = 0;
@@ -54,7 +52,20 @@ void analogout_init(dac_t *obj, PinName pin) {
5452
// Get the pin function and assign the used channel to the object
5553
uint32_t function = pinmap_function(pin, PinMap_DAC);
5654
MBED_ASSERT(function != (uint32_t)NC);
57-
obj->channel = STM_PIN_CHANNEL(function);
55+
56+
switch (STM_PIN_CHANNEL(function)) {
57+
case 1:
58+
obj->channel = DAC_CHANNEL_1;
59+
break;
60+
#if defined(DAC_CHANNEL_2)
61+
case 2:
62+
obj->channel = DAC_CHANNEL_2;
63+
break;
64+
#endif
65+
default:
66+
error("Unknown DAC channel");
67+
break;
68+
}
5869

5970
// Configure GPIO
6071
pinmap_pinout(pin, PinMap_DAC);
@@ -73,26 +84,23 @@ void analogout_init(dac_t *obj, PinName pin) {
7384
#endif
7485

7586
// Configure DAC
76-
DacHandle.Instance = (DAC_TypeDef *)(obj->dac);
87+
obj->handle.Instance = (DAC_TypeDef *)(obj->dac);
7788

7889
sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
7990
sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE;
8091

92+
8193
if (pin == PA_4) {
82-
HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1);
8394
pa4_used = 1;
8495
}
8596

8697
#if defined(DAC_CHANNEL_2)
8798
if (pin == PA_5) {
88-
HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_2);
8999
pa5_used = 1;
90100
}
91101
#endif
92102

93-
if (pin == PA_6) {
94-
HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1);
95-
}
103+
HAL_DAC_ConfigChannel(&obj->handle, &sConfig, obj->channel);
96104

97105
analogout_write_u16(obj, 0);
98106
}
@@ -121,28 +129,12 @@ void analogout_free(dac_t *obj) {
121129
}
122130

123131
static inline void dac_write(dac_t *obj, int value) {
124-
if (obj->channel == 1) {
125-
HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (value & DAC_RANGE));
126-
HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1);
127-
}
128-
#if defined(DAC_CHANNEL_2)
129-
if (obj->channel == 2) {
130-
HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, (value & DAC_RANGE));
131-
HAL_DAC_Start(&DacHandle, DAC_CHANNEL_2);
132-
}
133-
#endif
132+
HAL_DAC_SetValue(&obj->handle, obj->channel, DAC_ALIGN_12B_R, (value & DAC_RANGE));
133+
HAL_DAC_Start(&obj->handle, obj->channel);
134134
}
135135

136136
static inline int dac_read(dac_t *obj) {
137-
if (obj->channel == 1) {
138-
return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1);
139-
}
140-
#if defined(DAC_CHANNEL_2)
141-
if (obj->channel == 2) {
142-
return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2);
143-
}
144-
#endif
145-
return 0;
137+
return (int)HAL_DAC_GetValue(&obj->handle, obj->channel);
146138
}
147139

148140
void analogout_write(dac_t *obj, float value) {

0 commit comments

Comments
 (0)