36
36
#include "mbed_error.h"
37
37
#include "PeripheralPins.h"
38
38
39
- int adc_inited = 0 ;
40
-
41
39
void analogin_init (analogin_t * obj , PinName pin )
42
40
{
41
+ static int adc_calibrated = 0 ;
43
42
uint32_t function = (uint32_t )NC ;
44
- obj -> handle .Instance = (ADC_TypeDef * )NC ;
45
43
46
44
// ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...)
47
45
// are described in PinNames.h and PeripheralPins.c
@@ -68,41 +66,38 @@ void analogin_init(analogin_t *obj, PinName pin)
68
66
// Save pin number for the read function
69
67
obj -> pin = pin ;
70
68
71
- // The ADC initialization is done once
72
- if (adc_inited == 0 ) {
73
- adc_inited = 1 ;
74
-
75
- obj -> handle .State = HAL_ADC_STATE_RESET ;
76
- // Enable ADC clock
77
- __ADC1_CLK_ENABLE ();
78
-
79
- // Configure ADC
80
- obj -> handle .Init .OversamplingMode = DISABLE ;
81
- obj -> handle .Init .ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV1 ;
82
- obj -> handle .Init .Resolution = ADC_RESOLUTION12b ;
83
- obj -> handle .Init .SamplingTime = ADC_SAMPLETIME_239CYCLES_5 ;
84
- obj -> handle .Init .ScanConvMode = ADC_SCAN_DIRECTION_FORWARD ;
85
- obj -> handle .Init .DataAlign = ADC_DATAALIGN_RIGHT ;
86
- obj -> handle .Init .ContinuousConvMode = DISABLE ;
87
- obj -> handle .Init .DiscontinuousConvMode = DISABLE ;
88
- obj -> handle .Init .ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE ;
89
- obj -> handle .Init .ExternalTrigConv = ADC_EXTERNALTRIG0_T6_TRGO ; // Not used here
90
- obj -> handle .Init .DMAContinuousRequests = DISABLE ;
91
- obj -> handle .Init .EOCSelection = EOC_SINGLE_CONV ;
92
- obj -> handle .Init .Overrun = OVR_DATA_OVERWRITTEN ;
93
- obj -> handle .Init .LowPowerAutoWait = ENABLE ;
94
- obj -> handle .Init .LowPowerFrequencyMode = DISABLE ; // To be enabled only if ADC clock < 2.8 MHz
95
- obj -> handle .Init .LowPowerAutoPowerOff = DISABLE ;
96
-
97
- if (HAL_ADC_Init (& obj -> handle ) != HAL_OK ) {
98
- error ("Cannot initialize ADC" );
99
- }
100
-
101
- // Calibration
102
- HAL_ADCEx_Calibration_Start (& obj -> handle , ADC_SINGLE_ENDED );
69
+ // Configure ADC object structures
70
+ obj -> handle .State = HAL_ADC_STATE_RESET ;
71
+ obj -> handle .Init .OversamplingMode = DISABLE ;
72
+ obj -> handle .Init .ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV1 ;
73
+ obj -> handle .Init .Resolution = ADC_RESOLUTION_12B ;
74
+ obj -> handle .Init .SamplingTime = ADC_SAMPLETIME_239CYCLES_5 ;
75
+ obj -> handle .Init .ScanConvMode = ADC_SCAN_DIRECTION_FORWARD ;
76
+ obj -> handle .Init .DataAlign = ADC_DATAALIGN_RIGHT ;
77
+ obj -> handle .Init .ContinuousConvMode = DISABLE ;
78
+ obj -> handle .Init .DiscontinuousConvMode = DISABLE ;
79
+ obj -> handle .Init .ExternalTrigConvEdge = ADC_EXTERNALTRIG_EDGE_NONE ;
80
+ obj -> handle .Init .ExternalTrigConv = ADC_EXTERNALTRIG0_T6_TRGO ; // Not used here
81
+ obj -> handle .Init .DMAContinuousRequests = DISABLE ;
82
+ obj -> handle .Init .EOCSelection = EOC_SINGLE_CONV ;
83
+ obj -> handle .Init .Overrun = OVR_DATA_OVERWRITTEN ;
84
+ obj -> handle .Init .LowPowerAutoWait = ENABLE ;
85
+ obj -> handle .Init .LowPowerFrequencyMode = DISABLE ; // To be enabled only if ADC clock < 2.8 MHz
86
+ obj -> handle .Init .LowPowerAutoPowerOff = DISABLE ;
87
+
88
+ __HAL_RCC_ADC1_CLK_ENABLE ();
89
+
90
+ if (HAL_ADC_Init (& obj -> handle ) != HAL_OK ) {
91
+ error ("Cannot initialize ADC" );
92
+ }
103
93
104
- __HAL_ADC_ENABLE (& obj -> handle );
94
+ // ADC calibration is done only once
95
+ if (adc_calibrated == 0 ) {
96
+ adc_calibrated = 1 ;
97
+ HAL_ADCEx_Calibration_Start (& obj -> handle , ADC_SINGLE_ENDED );
105
98
}
99
+
100
+ __HAL_ADC_ENABLE (& obj -> handle );
106
101
}
107
102
108
103
static inline uint16_t adc_read (analogin_t * obj )
@@ -182,7 +177,7 @@ static inline uint16_t adc_read(analogin_t *obj)
182
177
183
178
// Wait end of conversion and get value
184
179
if (HAL_ADC_PollForConversion (& obj -> handle , 10 ) == HAL_OK ) {
185
- return (HAL_ADC_GetValue (& obj -> handle ) );
180
+ return (uint16_t ) HAL_ADC_GetValue (& obj -> handle );
186
181
} else {
187
182
return 0 ;
188
183
}
0 commit comments