33
33
#include "mbed_wait_api.h"
34
34
#include "cmsis.h"
35
35
#include "pinmap.h"
36
- #include "PeripheralPins.h"
37
36
#include "mbed_error.h"
37
+ #include "PeripheralPins.h"
38
38
39
39
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 );
50
+ // Get the functions (adc channel) from the pin and assign it to the object
59
51
function = pinmap_function (pin , PinMap_ADC );
60
52
// Configure GPIO
61
53
pinmap_pinout (pin , PinMap_ADC );
62
54
} else {
63
55
// Internal channels
64
- obj -> handle .Instance = (ADC_TypeDef * ) pinmap_peripheral (pin , PinMap_ADC_Internal );
56
+ obj -> handle .Instance = (ADC_TypeDef * )pinmap_peripheral (pin , PinMap_ADC_Internal );
65
57
function = pinmap_function (pin , PinMap_ADC_Internal );
66
58
// No GPIO configuration for internal channels
67
59
}
@@ -73,45 +65,37 @@ void analogin_init(analogin_t *obj, PinName pin)
73
65
// Save pin number for the read function
74
66
obj -> pin = pin ;
75
67
76
- // Check if ADC is already initialized
77
- // 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_DIV4 ;
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
+
78
83
#if defined(ADC1 )
79
- if (((ADCName )obj -> handle .Instance == ADC_1 ) && adc1_inited ) return ;
80
84
if ((ADCName )obj -> handle .Instance == ADC_1 ) {
81
85
__HAL_RCC_ADC1_CLK_ENABLE ();
82
- adc1_inited = 1 ;
83
86
}
84
87
#endif
85
88
#if defined(ADC2 )
86
- if (((ADCName )obj -> handle .Instance == ADC_2 ) && adc2_inited ) return ;
87
89
if ((ADCName )obj -> handle .Instance == ADC_2 ) {
88
90
__HAL_RCC_ADC2_CLK_ENABLE ();
89
- adc2_inited = 1 ;
90
91
}
91
92
#endif
92
93
#if defined(ADC3 )
93
- if (((ADCName )obj -> handle .Instance == ADC_3 ) && adc3_inited ) return ;
94
94
if ((ADCName )obj -> handle .Instance == ADC_3 ) {
95
95
__HAL_RCC_ADC3_CLK_ENABLE ();
96
- adc3_inited = 1 ;
97
96
}
98
97
#endif
99
98
100
- // Configure ADC
101
- obj -> handle .State = HAL_ADC_STATE_RESET ;
102
- obj -> handle .Init .ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4 ;
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
-
115
99
if (HAL_ADC_Init (& obj -> handle ) != HAL_OK ) {
116
100
error ("Cannot initialize ADC" );
117
101
}
@@ -188,16 +172,13 @@ static inline uint16_t adc_read(analogin_t *obj)
188
172
return 0 ;
189
173
}
190
174
191
- if (HAL_ADC_ConfigChannel (& obj -> handle , & sConfig ) != HAL_OK ) {
192
- error ("Cannot configure ADC channel" );
193
- }
175
+ HAL_ADC_ConfigChannel (& obj -> handle , & sConfig );
194
176
195
177
HAL_ADC_Start (& obj -> handle ); // Start conversion
196
178
197
179
// Wait end of conversion and get value
198
- HAL_ADC_PollForConversion (& obj -> handle , 10 );
199
- if (HAL_ADC_GetState (& obj -> handle ) & HAL_ADC_STATE_EOC_REG ) {
200
- return (HAL_ADC_GetValue (& obj -> handle ));
180
+ if (HAL_ADC_PollForConversion (& obj -> handle , 10 ) == HAL_OK ) {
181
+ return (uint16_t )HAL_ADC_GetValue (& obj -> handle );
201
182
} else {
202
183
return 0 ;
203
184
}
0 commit comments