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
RCC_PeriphCLKInitTypeDef PeriphClkInit ;
44
43
uint32_t function = (uint32_t )NC ;
45
44
@@ -49,14 +48,14 @@ void analogin_init(analogin_t *obj, PinName pin)
49
48
if ((pin < 0xF0 ) || (pin >= 0x100 )) {
50
49
// Normal channels
51
50
// Get the peripheral name from the pin and assign it to the object
52
- obj -> handle .Instance = (ADC_TypeDef * ) pinmap_peripheral (pin , PinMap_ADC );
51
+ obj -> handle .Instance = (ADC_TypeDef * )pinmap_peripheral (pin , PinMap_ADC );
53
52
// Get the functions (adc channel) from the pin and assign it to the object
54
53
function = pinmap_function (pin , PinMap_ADC );
55
54
// Configure GPIO
56
55
pinmap_pinout (pin , PinMap_ADC );
57
56
} else {
58
57
// Internal channels
59
- obj -> handle .Instance = (ADC_TypeDef * ) pinmap_peripheral (pin , PinMap_ADC_Internal );
58
+ obj -> handle .Instance = (ADC_TypeDef * )pinmap_peripheral (pin , PinMap_ADC_Internal );
60
59
function = pinmap_function (pin , PinMap_ADC_Internal );
61
60
// No GPIO configuration for internal channels
62
61
}
@@ -68,13 +67,26 @@ void analogin_init(analogin_t *obj, PinName pin)
68
67
// Save pin number for the read function
69
68
obj -> pin = pin ;
70
69
71
- // The ADC initialization is done once
72
- if (adc_inited == 0 ) {
73
- adc_inited = 1 ;
74
-
75
- // Enable ADC clock
76
- __HAL_RCC_ADC1_CLK_ENABLE ();
70
+ // Enable ADC clock
71
+ __HAL_RCC_ADC1_CLK_ENABLE ();
72
+
73
+ // Configure ADC object structures
74
+ obj -> handle .State = HAL_ADC_STATE_RESET ;
75
+ obj -> handle .Init .DataAlign = ADC_DATAALIGN_RIGHT ;
76
+ obj -> handle .Init .ScanConvMode = 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_SOFTWARE_START ;
82
+
83
+ if (HAL_ADC_Init (& obj -> handle ) != HAL_OK ) {
84
+ error ("Cannot initialize ADC" );
85
+ }
77
86
87
+ // This section is done only once
88
+ if (adc_calibrated == 0 ) {
89
+ adc_calibrated = 1 ;
78
90
// Configure ADC clock prescaler
79
91
// Caution: On STM32F1, ADC clock frequency max is 14 MHz (refer to device datasheet).
80
92
// Therefore, ADC clock prescaler must be configured in function
@@ -84,29 +96,14 @@ void analogin_init(analogin_t *obj, PinName pin)
84
96
PeriphClkInit .PeriphClockSelection = RCC_PERIPHCLK_ADC ;
85
97
PeriphClkInit .AdcClockSelection = RCC_ADCPCLK2_DIV6 ;
86
98
HAL_RCCEx_PeriphCLKConfig (& PeriphClkInit );
87
-
88
- // Configure ADC
89
- obj -> handle .State = HAL_ADC_STATE_RESET ;
90
- obj -> handle .Init .DataAlign = ADC_DATAALIGN_RIGHT ;
91
- obj -> handle .Init .ScanConvMode = DISABLE ;
92
- obj -> handle .Init .ContinuousConvMode = DISABLE ;
93
- obj -> handle .Init .NbrOfConversion = 1 ;
94
- obj -> handle .Init .DiscontinuousConvMode = DISABLE ;
95
- obj -> handle .Init .NbrOfDiscConversion = 0 ;
96
- obj -> handle .Init .ExternalTrigConv = ADC_SOFTWARE_START ;
97
-
98
- if (HAL_ADC_Init (& obj -> handle ) != HAL_OK ) {
99
- error ("Cannot initialize ADC\n" );
100
- }
101
-
102
- // Calibrate ADC
99
+ // Calibration
103
100
HAL_ADCEx_Calibration_Start (& obj -> handle );
104
101
}
105
102
}
106
103
107
104
static inline uint16_t adc_read (analogin_t * obj )
108
105
{
109
- ADC_ChannelConfTypeDef sConfig ;
106
+ ADC_ChannelConfTypeDef sConfig = { 0 } ;
110
107
111
108
// Configure ADC channel
112
109
sConfig .Rank = 1 ;
@@ -177,7 +174,7 @@ static inline uint16_t adc_read(analogin_t *obj)
177
174
178
175
// Wait end of conversion and get value
179
176
if (HAL_ADC_PollForConversion (& obj -> handle , 10 ) == HAL_OK ) {
180
- return (HAL_ADC_GetValue (& obj -> handle ) );
177
+ return (uint16_t ) HAL_ADC_GetValue (& obj -> handle );
181
178
} else {
182
179
return 0 ;
183
180
}
0 commit comments