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;

0 commit comments

Comments
 (0)