Skip to content

Commit f08d5a4

Browse files
Merge pull request #4623 from LMESTM/analogin_handle
Improve management handling of multiple instances of analogin ojects
2 parents c8657d0 + 7414b12 commit f08d5a4

File tree

66 files changed

+284
-570
lines changed

Some content is hidden

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

66 files changed

+284
-570
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
@@ -54,12 +54,6 @@ struct port_s {
5454
__IO uint32_t *reg_out;
5555
};
5656

57-
struct analogin_s {
58-
ADCName adc;
59-
PinName pin;
60-
uint32_t channel;
61-
};
62-
6357
#include "common_objects.h"
6458

6559
#ifdef __cplusplus

targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F030R8/objects.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ struct port_s {
5454
__IO uint32_t *reg_out;
5555
};
5656

57-
struct analogin_s {
58-
ADCName adc;
59-
PinName pin;
60-
uint32_t channel;
61-
};
62-
6357
#include "common_objects.h"
6458

6559
#ifdef __cplusplus

targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F031K6/objects.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ struct port_s {
5454
__IO uint32_t *reg_out;
5555
};
5656

57-
struct analogin_s {
58-
ADCName adc;
59-
PinName pin;
60-
uint32_t channel;
61-
};
62-
6357
#include "common_objects.h"
6458

6559
#ifdef __cplusplus

targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F042K6/objects.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ struct port_s {
5454
__IO uint32_t *reg_out;
5555
};
5656

57-
struct analogin_s {
58-
ADCName adc;
59-
PinName pin;
60-
uint32_t channel;
61-
};
62-
6357
struct can_s {
6458
CANName can;
6559
int index;

targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F070RB/objects.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ struct port_s {
5454
__IO uint32_t *reg_out;
5555
};
5656

57-
struct analogin_s {
58-
ADCName adc;
59-
PinName pin;
60-
uint32_t channel;
61-
};
62-
6357
#include "common_objects.h"
6458

6559
#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
@@ -54,12 +54,6 @@ struct port_s {
5454
__IO uint32_t *reg_out;
5555
};
5656

57-
struct analogin_s {
58-
ADCName adc;
59-
PinName pin;
60-
uint32_t channel;
61-
};
62-
6357
struct can_s {
6458
CANName can;
6559
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
@@ -54,12 +54,6 @@ struct port_s {
5454
__IO uint32_t *reg_out;
5555
};
5656

57-
struct analogin_s {
58-
ADCName adc;
59-
PinName pin;
60-
uint32_t channel;
61-
};
62-
6357
struct can_s {
6458
CANName can;
6559
int index;

targets/TARGET_STM/TARGET_STM32F0/analogin_api.c

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,13 @@
3636
#include "PeripheralPins.h"
3737
#include "mbed_error.h"
3838

39-
ADC_HandleTypeDef AdcHandle;
40-
4139
int adc_inited = 0;
4240

4341
void analogin_init(analogin_t *obj, PinName pin) {
4442
// Get the peripheral name from the pin and assign it to the object
45-
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
46-
MBED_ASSERT(obj->adc != (ADCName)NC);
47-
43+
obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC);
44+
MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC);
45+
4846
// Get the functions (adc channel) from the pin and assign it to the object
4947
uint32_t function = pinmap_function(pin, PinMap_ADC);
5048
MBED_ASSERT(function != (uint32_t)NC);
@@ -67,25 +65,25 @@ void analogin_init(analogin_t *obj, PinName pin) {
6765
__ADC1_CLK_ENABLE();
6866

6967
// Configure ADC
70-
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
71-
AdcHandle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
72-
AdcHandle.Init.Resolution = ADC_RESOLUTION12b;
73-
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
74-
AdcHandle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
75-
AdcHandle.Init.EOCSelection = EOC_SINGLE_CONV;
76-
AdcHandle.Init.LowPowerAutoWait = DISABLE;
77-
AdcHandle.Init.LowPowerAutoPowerOff = DISABLE;
78-
AdcHandle.Init.ContinuousConvMode = DISABLE;
79-
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
80-
AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
81-
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
82-
AdcHandle.Init.DMAContinuousRequests = DISABLE;
83-
AdcHandle.Init.Overrun = OVR_DATA_OVERWRITTEN;
84-
if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {
68+
obj->handle.State = HAL_ADC_STATE_RESET;
69+
obj->handle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
70+
obj->handle.Init.Resolution = ADC_RESOLUTION12b;
71+
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
72+
obj->handle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
73+
obj->handle.Init.EOCSelection = EOC_SINGLE_CONV;
74+
obj->handle.Init.LowPowerAutoWait = DISABLE;
75+
obj->handle.Init.LowPowerAutoPowerOff = DISABLE;
76+
obj->handle.Init.ContinuousConvMode = DISABLE;
77+
obj->handle.Init.DiscontinuousConvMode = DISABLE;
78+
obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
79+
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
80+
obj->handle.Init.DMAContinuousRequests = DISABLE;
81+
obj->handle.Init.Overrun = OVR_DATA_OVERWRITTEN;
82+
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
8583
error("Cannot initialize ADC");
8684
}
8785
// Run the ADC calibration
88-
if (HAL_ADCEx_Calibration_Start(&AdcHandle) != HAL_OK) {
86+
if (HAL_ADCEx_Calibration_Start(&obj->handle) != HAL_OK) {
8987
error("Cannot Start ADC_Calibration");
9088
}
9189
}
@@ -94,8 +92,6 @@ void analogin_init(analogin_t *obj, PinName pin) {
9492
static inline uint16_t adc_read(analogin_t *obj) {
9593
ADC_ChannelConfTypeDef sConfig;
9694

97-
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
98-
9995
// Configure ADC channel
10096
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
10197
#if defined (TARGET_STM32F091RC)
@@ -169,15 +165,15 @@ static inline uint16_t adc_read(analogin_t *obj) {
169165
}
170166

171167
// Clear all channels as it is not done in HAL_ADC_ConfigChannel()
172-
AdcHandle.Instance->CHSELR = 0;
168+
obj->handle.Instance->CHSELR = 0;
173169

174-
HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
170+
HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
175171

176-
HAL_ADC_Start(&AdcHandle); // Start conversion
172+
HAL_ADC_Start(&obj->handle); // Start conversion
177173

178174
// Wait end of conversion and get value
179-
if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
180-
return (HAL_ADC_GetValue(&AdcHandle));
175+
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
176+
return (HAL_ADC_GetValue(&obj->handle));
181177
} else {
182178
return 0;
183179
}

targets/TARGET_STM/TARGET_STM32F0/common_objects.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ struct i2c_s {
111111
#endif
112112
};
113113

114+
struct analogin_s {
115+
ADC_HandleTypeDef handle;
116+
PinName pin;
117+
uint8_t channel;
118+
};
119+
114120
#include "gpio_object.h"
115121

116122
#if DEVICE_ANALOGOUT

targets/TARGET_STM/TARGET_STM32F1/TARGET_BLUEPILL_F103C8/objects.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ struct port_s {
5454
__IO uint32_t *reg_out;
5555
};
5656

57-
struct analogin_s {
58-
ADCName adc;
59-
PinName pin;
60-
uint8_t channel;
61-
};
62-
6357
struct can_s {
6458
CANName can;
6559
int index;

targets/TARGET_STM/TARGET_STM32F1/TARGET_DISCO_F100RB/objects.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ struct port_s {
5454
__IO uint32_t *reg_out;
5555
};
5656

57-
struct analogin_s {
58-
ADCName adc;
59-
PinName pin;
60-
uint8_t channel;
61-
};
62-
6357
#include "common_objects.h"
6458

6559
#ifdef __cplusplus

targets/TARGET_STM/TARGET_STM32F1/TARGET_NUCLEO_F103RB/objects.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ struct port_s {
5454
__IO uint32_t *reg_out;
5555
};
5656

57-
struct analogin_s {
58-
ADCName adc;
59-
PinName pin;
60-
uint8_t channel;
61-
};
62-
6357
struct can_s {
6458
CANName can;
6559
int index;

targets/TARGET_STM/TARGET_STM32F1/analogin_api.c

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,16 @@
3535
#include "pinmap.h"
3636
#include "PeripheralPins.h"
3737

38-
ADC_HandleTypeDef AdcHandle;
39-
4038
int adc_inited = 0;
4139

4240
void analogin_init(analogin_t *obj, PinName pin)
4341
{
4442
RCC_PeriphCLKInitTypeDef PeriphClkInit;
4543

4644
// Get the peripheral name from the pin and assign it to the object
47-
obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
48-
MBED_ASSERT(obj->adc != (ADCName)NC);
49-
45+
obj->handle.Instance = (ADC_TypeDef *) pinmap_peripheral(pin, PinMap_ADC);
46+
MBED_ASSERT(obj->handle.Instance != (ADC_TypeDef *)NC);
47+
5048
// Get the functions (adc channel) from the pin and assign it to the object
5149
uint32_t function = pinmap_function(pin, PinMap_ADC);
5250
MBED_ASSERT(function != (uint32_t)NC);
@@ -79,24 +77,22 @@ void analogin_init(analogin_t *obj, PinName pin)
7977
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
8078

8179
// Configure ADC
82-
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
83-
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
84-
AdcHandle.Init.ScanConvMode = DISABLE;
85-
AdcHandle.Init.ContinuousConvMode = DISABLE;
86-
AdcHandle.Init.NbrOfConversion = 1;
87-
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
88-
AdcHandle.Init.NbrOfDiscConversion = 0;
89-
AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
90-
HAL_ADC_Init(&AdcHandle);
80+
obj->handle.State = HAL_ADC_STATE_RESET;
81+
obj->handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
82+
obj->handle.Init.ScanConvMode = DISABLE;
83+
obj->handle.Init.ContinuousConvMode = DISABLE;
84+
obj->handle.Init.NbrOfConversion = 1;
85+
obj->handle.Init.DiscontinuousConvMode = DISABLE;
86+
obj->handle.Init.NbrOfDiscConversion = 0;
87+
obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
88+
HAL_ADC_Init(&obj->handle);
9189
}
9290
}
9391

9492
static inline uint16_t adc_read(analogin_t *obj)
9593
{
9694
ADC_ChannelConfTypeDef sConfig;
9795

98-
AdcHandle.Instance = (ADC_TypeDef *)(obj->adc);
99-
10096
// Configure ADC channel
10197
sConfig.Rank = 1;
10298
sConfig.SamplingTime = ADC_SAMPLETIME_7CYCLES_5;
@@ -160,13 +156,13 @@ static inline uint16_t adc_read(analogin_t *obj)
160156
return 0;
161157
}
162158

163-
HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
159+
HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
164160

165-
HAL_ADC_Start(&AdcHandle); // Start conversion
161+
HAL_ADC_Start(&obj->handle); // Start conversion
166162

167163
// Wait end of conversion and get value
168-
if (HAL_ADC_PollForConversion(&AdcHandle, 10) == HAL_OK) {
169-
return (HAL_ADC_GetValue(&AdcHandle));
164+
if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) {
165+
return (HAL_ADC_GetValue(&obj->handle));
170166
} else {
171167
return 0;
172168
}

targets/TARGET_STM/TARGET_STM32F1/common_objects.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ struct i2c_s {
110110
#endif
111111
};
112112

113+
struct analogin_s {
114+
ADC_HandleTypeDef handle;
115+
PinName pin;
116+
uint8_t channel;
117+
};
118+
113119
#include "gpio_object.h"
114120

115121
#ifdef __cplusplus

0 commit comments

Comments
 (0)