36
36
#include "mbed_error.h"
37
37
#include "PeripheralPins.h"
38
38
39
-
40
39
void analogin_init (analogin_t * obj , PinName pin )
41
40
{
42
- #if defined(ADC1 )
43
- static int adc1_inited = 0 ;
44
- #endif
45
- #if defined(ADC2 )
46
- static int adc2_inited = 0 ;
47
- #endif
48
- #if defined(ADC3 )
49
- static int adc3_inited = 0 ;
50
- #endif
51
- #if defined(ADC4 )
52
- static int adc4_inited = 0 ;
53
- #endif
54
-
41
+ static int adc_calibrated = 0 ;
55
42
uint32_t function = (uint32_t )NC ;
56
43
57
44
// ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...)
@@ -60,14 +47,14 @@ void analogin_init(analogin_t *obj, PinName pin)
60
47
if ((pin < 0xF0 ) || (pin >= 0x100 )) {
61
48
// Normal channels
62
49
// Get the peripheral name from the pin and assign it to the object
63
- obj -> handle .Instance = (ADC_TypeDef * ) pinmap_peripheral (pin , PinMap_ADC );
50
+ obj -> handle .Instance = (ADC_TypeDef * )pinmap_peripheral (pin , PinMap_ADC );
64
51
// Get the functions (adc channel) from the pin and assign it to the object
65
52
function = pinmap_function (pin , PinMap_ADC );
66
53
// Configure GPIO
67
54
pinmap_pinout (pin , PinMap_ADC );
68
55
} else {
69
56
// Internal channels
70
- obj -> handle .Instance = (ADC_TypeDef * ) pinmap_peripheral (pin , PinMap_ADC_Internal );
57
+ obj -> handle .Instance = (ADC_TypeDef * )pinmap_peripheral (pin , PinMap_ADC_Internal );
71
58
function = pinmap_function (pin , PinMap_ADC_Internal );
72
59
// No GPIO configuration for internal channels
73
60
}
@@ -79,60 +66,53 @@ void analogin_init(analogin_t *obj, PinName pin)
79
66
// Save pin number for the read function
80
67
obj -> pin = pin ;
81
68
82
- // Check if ADC is already initialized
83
- // Enable ADC clock
69
+ // Configure ADC object structures
70
+ obj -> handle .State = HAL_ADC_STATE_RESET ;
71
+ obj -> handle .Init .ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2 ;
72
+ obj -> handle .Init .Resolution = ADC_RESOLUTION_12B ;
73
+ obj -> handle .Init .DataAlign = ADC_DATAALIGN_RIGHT ;
74
+ obj -> handle .Init .ScanConvMode = DISABLE ;
75
+ obj -> handle .Init .EOCSelection = EOC_SINGLE_CONV ;
76
+ obj -> handle .Init .LowPowerAutoWait = DISABLE ;
77
+ obj -> handle .Init .ContinuousConvMode = DISABLE ;
78
+ obj -> handle .Init .NbrOfConversion = 1 ;
79
+ obj -> handle .Init .DiscontinuousConvMode = DISABLE ;
80
+ obj -> handle .Init .NbrOfDiscConversion = 0 ;
81
+ obj -> handle .Init .ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1 ;
82
+ obj -> handle .Init .ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE ;
83
+ obj -> handle .Init .DMAContinuousRequests = DISABLE ;
84
+ obj -> handle .Init .Overrun = OVR_DATA_OVERWRITTEN ;
85
+
84
86
#if defined(ADC1 )
85
- if (((ADCName )obj -> handle .Instance == ADC_1 ) && adc1_inited ) return ;
86
87
if ((ADCName )obj -> handle .Instance == ADC_1 ) {
87
- __ADC1_CLK_ENABLE ();
88
- adc1_inited = 1 ;
88
+ __HAL_RCC_ADC1_CLK_ENABLE ();
89
89
}
90
90
#endif
91
91
#if defined(ADC2 )
92
- if (((ADCName )obj -> handle .Instance == ADC_2 ) && adc2_inited ) return ;
93
92
if ((ADCName )obj -> handle .Instance == ADC_2 ) {
94
- __ADC2_CLK_ENABLE ();
95
- adc2_inited = 1 ;
93
+ __HAL_RCC_ADC2_CLK_ENABLE ();
96
94
}
97
95
#endif
98
96
#if defined(ADC3 )
99
- if (((ADCName )obj -> handle .Instance == ADC_3 ) && adc3_inited ) return ;
100
97
if ((ADCName )obj -> handle .Instance == ADC_3 ) {
101
- __ADC34_CLK_ENABLE ();
102
- adc3_inited = 1 ;
98
+ __HAL_RCC_ADC34_CLK_ENABLE ();
103
99
}
104
100
#endif
105
101
#if defined(ADC4 )
106
- if (((ADCName )obj -> handle .Instance == ADC_4 ) && adc4_inited ) return ;
107
102
if ((ADCName )obj -> handle .Instance == ADC_4 ) {
108
- __ADC34_CLK_ENABLE ();
109
- adc4_inited = 1 ;
103
+ __HAL_RCC_ADC34_CLK_ENABLE ();
110
104
}
111
105
#endif
112
106
113
- // Configure ADC
114
- obj -> handle .State = HAL_ADC_STATE_RESET ;
115
- obj -> handle .Init .ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2 ;
116
- obj -> handle .Init .Resolution = ADC_RESOLUTION12b ;
117
- obj -> handle .Init .DataAlign = ADC_DATAALIGN_RIGHT ;
118
- obj -> handle .Init .ScanConvMode = DISABLE ;
119
- obj -> handle .Init .EOCSelection = EOC_SINGLE_CONV ;
120
- obj -> handle .Init .LowPowerAutoWait = DISABLE ;
121
- obj -> handle .Init .ContinuousConvMode = DISABLE ;
122
- obj -> handle .Init .NbrOfConversion = 1 ;
123
- obj -> handle .Init .DiscontinuousConvMode = DISABLE ;
124
- obj -> handle .Init .NbrOfDiscConversion = 0 ;
125
- obj -> handle .Init .ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1 ;
126
- obj -> handle .Init .ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE ;
127
- obj -> handle .Init .DMAContinuousRequests = DISABLE ;
128
- obj -> handle .Init .Overrun = OVR_DATA_OVERWRITTEN ;
129
-
130
107
if (HAL_ADC_Init (& obj -> handle ) != HAL_OK ) {
131
108
error ("Cannot initialize ADC" );
132
109
}
133
110
134
- // Calibrate ADC
135
- HAL_ADCEx_Calibration_Start (& obj -> handle , ADC_SINGLE_ENDED );
111
+ // ADC calibration is done only once
112
+ if (adc_calibrated == 0 ) {
113
+ adc_calibrated = 1 ;
114
+ HAL_ADCEx_Calibration_Start (& obj -> handle , ADC_SINGLE_ENDED );
115
+ }
136
116
}
137
117
138
118
static inline uint16_t adc_read (analogin_t * obj )
@@ -211,7 +191,7 @@ static inline uint16_t adc_read(analogin_t *obj)
211
191
212
192
// Wait end of conversion and get value
213
193
if (HAL_ADC_PollForConversion (& obj -> handle , 10 ) == HAL_OK ) {
214
- return (HAL_ADC_GetValue (& obj -> handle ) );
194
+ return (uint16_t ) HAL_ADC_GetValue (& obj -> handle );
215
195
} else {
216
196
return 0 ;
217
197
}
0 commit comments