Skip to content

Commit d382d44

Browse files
Merge pull request #4529 from LMESTM/issue_1083
Manage multiple instances of analog out
2 parents c6be626 + 4238216 commit d382d44

File tree

54 files changed

+334
-739
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+334
-739
lines changed

targets/TARGET_STM/TARGET_STM32F0/TARGET_DISCO_F051R8/objects.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,6 @@ struct analogin_s {
6060
uint32_t channel;
6161
};
6262

63-
struct dac_s {
64-
DACName dac;
65-
PinName pin;
66-
uint32_t channel;
67-
};
68-
6963
#include "common_objects.h"
7064

7165
#ifdef __cplusplus

targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F072RB/objects.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,6 @@ struct analogin_s {
6060
uint32_t channel;
6161
};
6262

63-
struct dac_s {
64-
DACName dac;
65-
PinName pin;
66-
uint32_t channel;
67-
};
68-
6963
struct can_s {
7064
CANName can;
7165
int index;

targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F091RC/objects.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,6 @@ struct analogin_s {
6060
uint32_t channel;
6161
};
6262

63-
struct dac_s {
64-
DACName dac;
65-
PinName pin;
66-
uint32_t channel;
67-
};
68-
6963
struct can_s {
7064
CANName can;
7165
int index;

targets/TARGET_STM/TARGET_STM32F0/analogout_api.c renamed to targets/TARGET_STM/TARGET_STM32F0/analogout_device.c

Lines changed: 21 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@
3535
#include "mbed_error.h"
3636
#include "PeripheralPins.h"
3737

38-
#define DAC_RANGE (0xFFF) // 12 bits
39-
#define DAC_NB_BITS (12)
40-
41-
static DAC_HandleTypeDef DacHandle;
42-
4338
void analogout_init(dac_t *obj, PinName pin) {
4439
DAC_ChannelConfTypeDef sConfig;
4540

@@ -50,7 +45,20 @@ void analogout_init(dac_t *obj, PinName pin) {
5045
// Get the pin function and assign the used channel to the object
5146
uint32_t function = pinmap_function(pin, PinMap_DAC);
5247
MBED_ASSERT(function != (uint32_t)NC);
53-
obj->channel = STM_PIN_CHANNEL(function);
48+
49+
switch (STM_PIN_CHANNEL(function)) {
50+
case 1:
51+
obj->channel = DAC_CHANNEL_1;
52+
break;
53+
#if defined(DAC_CHANNEL_2)
54+
case 2:
55+
obj->channel = DAC_CHANNEL_2;
56+
break;
57+
#endif
58+
default:
59+
error("Unknown DAC channel");
60+
break;
61+
}
5462

5563
// Configure GPIO
5664
pinmap_pinout(pin, PinMap_DAC);
@@ -62,15 +70,16 @@ void analogout_init(dac_t *obj, PinName pin) {
6270
__HAL_RCC_DAC1_CLK_ENABLE();
6371

6472
// Configure DAC
65-
DacHandle.Instance = (DAC_TypeDef *)(obj->dac);
73+
obj->handle.Instance = (DAC_TypeDef *)(obj->dac);
74+
if (HAL_DAC_Init(&obj->handle) != HAL_OK ) {
75+
error("HAL_DAC_Init failed");
76+
}
6677

6778
sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
68-
sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE;
79+
sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
6980

70-
if (pin == PA_4) {
71-
HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1);
72-
} else { // PA_5
73-
HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_2);
81+
if (HAL_DAC_ConfigChannel(&obj->handle, &sConfig, obj->channel) != HAL_OK) {
82+
error("HAL_DAC_ConfigChannel failed");
7483
}
7584

7685
analogout_write_u16(obj, 0);
@@ -86,53 +95,4 @@ void analogout_free(dac_t *obj) {
8695
pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
8796
}
8897

89-
static inline void dac_write(dac_t *obj, int value) {
90-
if (obj->channel == 1) {
91-
HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (value & DAC_RANGE));
92-
HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1);
93-
}
94-
#if defined(DAC_CHANNEL_2)
95-
if (obj->channel == 2) {
96-
HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, (value & DAC_RANGE));
97-
HAL_DAC_Start(&DacHandle, DAC_CHANNEL_2);
98-
}
99-
#endif
100-
}
101-
102-
static inline int dac_read(dac_t *obj) {
103-
if (obj->channel == 1) {
104-
return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1);
105-
}
106-
#if defined(DAC_CHANNEL_2)
107-
if (obj->channel == 2) {
108-
return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2);
109-
}
110-
#endif
111-
return 0;
112-
}
113-
114-
void analogout_write(dac_t *obj, float value) {
115-
if (value < 0.0f) {
116-
dac_write(obj, 0); // Min value
117-
} else if (value > 1.0f) {
118-
dac_write(obj, (int)DAC_RANGE); // Max value
119-
} else {
120-
dac_write(obj, (int)(value * (float)DAC_RANGE));
121-
}
122-
}
123-
124-
void analogout_write_u16(dac_t *obj, uint16_t value) {
125-
dac_write(obj, value >> (16 - DAC_NB_BITS));
126-
}
127-
128-
float analogout_read(dac_t *obj) {
129-
uint32_t value = dac_read(obj);
130-
return (float)value * (1.0f / (float)DAC_RANGE);
131-
}
132-
133-
uint16_t analogout_read_u16(dac_t *obj) {
134-
uint32_t value = dac_read(obj);
135-
return (value << 4) | ((value >> 8) & 0x000F); // Conversion from 12 to 16 bits
136-
}
137-
13898
#endif // DEVICE_ANALOGOUT

targets/TARGET_STM/TARGET_STM32F0/common_objects.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@ struct i2c_s {
113113

114114
#include "gpio_object.h"
115115

116+
#if DEVICE_ANALOGOUT
117+
struct dac_s {
118+
DACName dac;
119+
PinName pin;
120+
uint32_t channel;
121+
DAC_HandleTypeDef handle;
122+
};
123+
#endif
124+
116125
#ifdef __cplusplus
117126
}
118127
#endif

targets/TARGET_STM/TARGET_STM32F2/analogout_api.c renamed to targets/TARGET_STM/TARGET_STM32F2/analogout_device.c

Lines changed: 18 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,29 @@
3535
#include "stm32f2xx_hal.h"
3636
#include "PeripheralPins.h"
3737

38-
#define DAC_RANGE (0xFFF) // 12 bits
39-
#define DAC_NB_BITS (12)
40-
41-
DAC_HandleTypeDef DacHandle;
42-
static DAC_ChannelConfTypeDef sConfig;
43-
4438
void analogout_init(dac_t *obj, PinName pin)
4539
{
46-
uint32_t channel ;
47-
HAL_StatusTypeDef status;
40+
DAC_ChannelConfTypeDef sConfig;
4841

4942
// Get the peripheral name (DAC_1, ...) from the pin and assign it to the object
5043
obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
5144
// Get the functions (dac channel) from the pin and assign it to the object
5245
uint32_t function = pinmap_function(pin, PinMap_DAC);
5346
MBED_ASSERT(function != (uint32_t)NC);
5447
// Save the channel for the write and read functions
55-
obj->channel = STM_PIN_CHANNEL(function);
48+
switch (STM_PIN_CHANNEL(function)) {
49+
case 1:
50+
obj->channel = DAC_CHANNEL_1;
51+
break;
52+
#if defined(DAC_CHANNEL_2)
53+
case 2:
54+
obj->channel = DAC_CHANNEL_2;
55+
break;
56+
#endif
57+
default:
58+
error("Unknown DAC channel");
59+
break;
60+
}
5661

5762
if (obj->dac == (DACName)NC) {
5863
error("DAC pin mapping failed");
@@ -65,91 +70,25 @@ void analogout_init(dac_t *obj, PinName pin)
6570

6671
__DAC_CLK_ENABLE();
6772

68-
DacHandle.Instance = DAC;
69-
70-
status = HAL_DAC_Init(&DacHandle);
71-
if (status != HAL_OK) {
73+
obj->handle.Instance = DAC;
74+
if (HAL_DAC_Init(&obj->handle) != HAL_OK ) {
7275
error("HAL_DAC_Init failed");
7376
}
7477

7578
sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
7679
sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
7780

78-
if (obj->channel == 1) {
79-
channel = DAC_CHANNEL_1;
80-
} else {
81-
channel = DAC_CHANNEL_2;
82-
}
83-
84-
if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, channel) != HAL_OK) {
81+
if (HAL_DAC_ConfigChannel(&obj->handle, &sConfig, obj->channel) != HAL_OK) {
8582
error("HAL_DAC_ConfigChannel failed");
8683
}
8784

88-
if (HAL_DAC_Start(&DacHandle, channel) != HAL_OK) {
89-
error("HAL_DAC_Start failed");
90-
}
91-
92-
if (HAL_DAC_SetValue(&DacHandle, channel, DAC_ALIGN_12B_R, 0x000) != HAL_OK) {
93-
error("HAL_DAC_SetValue failed");
94-
}
95-
85+
analogout_write_u16(obj, 0);
9686
}
9787

9888
void analogout_free(dac_t *obj)
9989
{
10090
}
10191

102-
static inline void dac_write(dac_t *obj, int value)
103-
{
104-
HAL_StatusTypeDef status = HAL_ERROR;
105-
106-
if (obj->channel == 1) {
107-
status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (value & DAC_RANGE));
108-
} else if (obj->channel == 2) {
109-
status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, (value & DAC_RANGE));
110-
}
111-
112-
if (status != HAL_OK) {
113-
error("DAC pin mapping failed");
114-
}
115-
}
11692

117-
static inline int dac_read(dac_t *obj)
118-
{
119-
if (obj->channel == 1) {
120-
return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1);
121-
} else if (obj->channel == 2) {
122-
return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2);
123-
}
124-
return 0; /* Just silented warning */
125-
}
126-
127-
void analogout_write(dac_t *obj, float value)
128-
{
129-
if (value < 0.0f) {
130-
dac_write(obj, 0); // Min value
131-
} else if (value > 1.0f) {
132-
dac_write(obj, (int)DAC_RANGE); // Max value
133-
} else {
134-
dac_write(obj, (int)(value * (float)DAC_RANGE));
135-
}
136-
}
137-
138-
void analogout_write_u16(dac_t *obj, uint16_t value)
139-
{
140-
dac_write(obj, value >> (16 - DAC_NB_BITS));
141-
}
142-
143-
float analogout_read(dac_t *obj)
144-
{
145-
uint32_t value = dac_read(obj);
146-
return (float)value * (1.0f / (float)DAC_RANGE);
147-
}
148-
149-
uint16_t analogout_read_u16(dac_t *obj)
150-
{
151-
uint32_t value = dac_read(obj);
152-
return (value << 4) | ((value >> 8) & 0x000F); // Conversion from 12 to 16 bits
153-
}
15493

15594
#endif // DEVICE_ANALOGOUT

targets/TARGET_STM/TARGET_STM32F2/objects.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ struct analogin_s {
6363
struct dac_s {
6464
DACName dac;
6565
uint8_t channel;
66+
DAC_HandleTypeDef handle;
6667
};
6768

6869
struct serial_s {

targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F302x8/objects.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,6 @@ struct analogin_s {
6060
uint32_t channel;
6161
};
6262

63-
struct dac_s {
64-
DACName dac;
65-
PinName pin;
66-
uint32_t channel;
67-
};
68-
6963
struct can_s {
7064
CANName can;
7165
int index;

targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303x8/objects.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,6 @@ struct analogin_s {
6060
uint32_t channel;
6161
};
6262

63-
struct dac_s {
64-
DACName dac;
65-
PinName pin;
66-
uint32_t channel;
67-
};
68-
6963
struct can_s {
7064
CANName can;
7165
int index;

targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xC/objects.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,7 @@ struct analogin_s {
5858
ADCName adc;
5959
PinName pin;
6060
uint32_t channel;
61-
};
62-
63-
struct dac_s {
64-
DACName dac;
65-
PinName pin;
66-
uint32_t channel;
61+
DAC_HandleTypeDef handle;
6762
};
6863

6964
struct can_s {

targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xE/objects.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,6 @@ struct analogin_s {
6060
uint32_t channel;
6161
};
6262

63-
struct dac_s {
64-
DACName dac;
65-
PinName pin;
66-
uint32_t channel;
67-
};
68-
6963
struct can_s {
7064
CANName can;
7165
int index;

targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F334x8/objects.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,6 @@ struct analogin_s {
6060
uint32_t channel;
6161
};
6262

63-
struct dac_s {
64-
DACName dac;
65-
PinName pin;
66-
uint32_t channel;
67-
};
68-
6963
#if defined (DEVICE_CAN)
7064
struct can_s {
7165
CANName can;

0 commit comments

Comments
 (0)