@@ -40,29 +40,20 @@ void analogin_init(analogin_t *obj, PinName pin)
40
40
{
41
41
uint32_t function = (uint32_t )NC ;
42
42
43
- #if defined(ADC1 )
44
- static int adc1_inited = 0 ;
45
- #endif
46
- #if defined(ADC2 )
47
- static int adc2_inited = 0 ;
48
- #endif
49
- #if defined(ADC3 )
50
- static int adc3_inited = 0 ;
51
- #endif
52
43
// ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...)
53
44
// are described in PinNames.h and PeripheralPins.c
54
45
// Pin value must be between 0xF0 and 0xFF
55
46
if ((pin < 0xF0 ) || (pin >= 0x100 )) {
56
47
// Normal channels
57
48
// Get the peripheral name from the pin and assign it to the object
58
- obj -> handle .Instance = (ADC_TypeDef * ) pinmap_peripheral (pin , PinMap_ADC );
49
+ obj -> handle .Instance = (ADC_TypeDef * )pinmap_peripheral (pin , PinMap_ADC );
59
50
// Get the functions (adc channel) from the pin and assign it to the object
60
51
function = pinmap_function (pin , PinMap_ADC );
61
52
// Configure GPIO
62
53
pinmap_pinout (pin , PinMap_ADC );
63
54
} else {
64
55
// Internal channels
65
- obj -> handle .Instance = (ADC_TypeDef * ) pinmap_peripheral (pin , PinMap_ADC_Internal );
56
+ obj -> handle .Instance = (ADC_TypeDef * )pinmap_peripheral (pin , PinMap_ADC_Internal );
66
57
function = pinmap_function (pin , PinMap_ADC_Internal );
67
58
// No GPIO configuration for internal channels
68
59
}
@@ -74,46 +65,39 @@ void analogin_init(analogin_t *obj, PinName pin)
74
65
// Save pin number for the read function
75
66
obj -> pin = pin ;
76
67
77
- // Check if ADC is already initialized
78
- // Enable ADC clock
68
+ // Configure ADC object structures
69
+ obj -> handle .State = HAL_ADC_STATE_RESET ;
70
+ obj -> handle .Init .ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2 ;
71
+ obj -> handle .Init .Resolution = ADC_RESOLUTION_12B ;
72
+ obj -> handle .Init .ScanConvMode = DISABLE ;
73
+ obj -> handle .Init .ContinuousConvMode = DISABLE ;
74
+ obj -> handle .Init .DiscontinuousConvMode = DISABLE ;
75
+ obj -> handle .Init .NbrOfDiscConversion = 0 ;
76
+ obj -> handle .Init .ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE ;
77
+ obj -> handle .Init .ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1 ;
78
+ obj -> handle .Init .DataAlign = ADC_DATAALIGN_RIGHT ;
79
+ obj -> handle .Init .NbrOfConversion = 1 ;
80
+ obj -> handle .Init .DMAContinuousRequests = DISABLE ;
81
+ obj -> handle .Init .EOCSelection = DISABLE ;
82
+
79
83
#if defined(ADC1 )
80
- if (((ADCName )obj -> handle .Instance == ADC_1 ) && adc1_inited ) return ;
81
84
if ((ADCName )obj -> handle .Instance == ADC_1 ) {
82
85
__HAL_RCC_ADC1_CLK_ENABLE ();
83
- adc1_inited = 1 ;
84
86
}
85
87
#endif
86
88
#if defined(ADC2 )
87
- if (((ADCName )obj -> handle .Instance == ADC_2 ) && adc2_inited ) return ;
88
89
if ((ADCName )obj -> handle .Instance == ADC_2 ) {
89
90
__HAL_RCC_ADC2_CLK_ENABLE ();
90
- adc2_inited = 1 ;
91
91
}
92
92
#endif
93
93
#if defined(ADC3 )
94
- if (((ADCName )obj -> handle .Instance == ADC_3 ) && adc3_inited ) return ;
95
94
if ((ADCName )obj -> handle .Instance == ADC_3 ) {
96
95
__HAL_RCC_ADC3_CLK_ENABLE ();
97
- adc3_inited = 1 ;
98
96
}
99
97
#endif
100
- // Configure ADC
101
- obj -> handle .State = HAL_ADC_STATE_RESET ;
102
- obj -> handle .Init .ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2 ;
103
- obj -> handle .Init .Resolution = ADC_RESOLUTION_12B ;
104
- obj -> handle .Init .ScanConvMode = DISABLE ;
105
- obj -> handle .Init .ContinuousConvMode = DISABLE ;
106
- obj -> handle .Init .DiscontinuousConvMode = DISABLE ;
107
- obj -> handle .Init .NbrOfDiscConversion = 0 ;
108
- obj -> handle .Init .ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE ;
109
- obj -> handle .Init .ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1 ;
110
- obj -> handle .Init .DataAlign = ADC_DATAALIGN_RIGHT ;
111
- obj -> handle .Init .NbrOfConversion = 1 ;
112
- obj -> handle .Init .DMAContinuousRequests = DISABLE ;
113
- obj -> handle .Init .EOCSelection = DISABLE ;
114
98
115
99
if (HAL_ADC_Init (& obj -> handle ) != HAL_OK ) {
116
- error ("Cannot initialize ADC\n " );
100
+ error ("Cannot initialize ADC" );
117
101
}
118
102
}
119
103
0 commit comments