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
- int adc_inited = 0 ;
40
-
41
- void analogin_init ( analogin_t * obj , PinName pin ) {
39
+ void analogin_init ( analogin_t * obj , PinName pin )
40
+ {
41
+ static int adc_calibrated = 0 ;
42
42
uint32_t function = (uint32_t )NC ;
43
43
44
44
// ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...)
@@ -47,14 +47,14 @@ void analogin_init(analogin_t *obj, PinName pin) {
47
47
if ((pin < 0xF0 ) || (pin >= 0x100 )) {
48
48
// Normal channels
49
49
// Get the peripheral name from the pin and assign it to the object
50
- obj -> handle .Instance = (ADC_TypeDef * ) pinmap_peripheral (pin , PinMap_ADC );
50
+ obj -> handle .Instance = (ADC_TypeDef * )pinmap_peripheral (pin , PinMap_ADC );
51
51
// Get the functions (adc channel) from the pin and assign it to the object
52
52
function = pinmap_function (pin , PinMap_ADC );
53
53
// Configure GPIO
54
54
pinmap_pinout (pin , PinMap_ADC );
55
55
} else {
56
56
// Internal channels
57
- obj -> handle .Instance = (ADC_TypeDef * ) pinmap_peripheral (pin , PinMap_ADC_Internal );
57
+ obj -> handle .Instance = (ADC_TypeDef * )pinmap_peripheral (pin , PinMap_ADC_Internal );
58
58
function = pinmap_function (pin , PinMap_ADC_Internal );
59
59
// No GPIO configuration for internal channels
60
60
}
@@ -66,40 +66,38 @@ void analogin_init(analogin_t *obj, PinName pin) {
66
66
// Save pin number for the read function
67
67
obj -> pin = pin ;
68
68
69
- // The ADC initialization is done once
70
- if (adc_inited == 0 ) {
71
- adc_inited = 1 ;
72
-
73
- // Enable ADC clock
74
- __ADC1_CLK_ENABLE ();
75
-
76
- // Configure ADC
77
- obj -> handle .State = HAL_ADC_STATE_RESET ;
78
- obj -> handle .Init .ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4 ;
79
- obj -> handle .Init .Resolution = ADC_RESOLUTION12b ;
80
- obj -> handle .Init .DataAlign = ADC_DATAALIGN_RIGHT ;
81
- obj -> handle .Init .ScanConvMode = ADC_SCAN_DIRECTION_FORWARD ;
82
- obj -> handle .Init .EOCSelection = EOC_SINGLE_CONV ;
83
- obj -> handle .Init .LowPowerAutoWait = DISABLE ;
84
- obj -> handle .Init .LowPowerAutoPowerOff = DISABLE ;
85
- obj -> handle .Init .ContinuousConvMode = DISABLE ;
86
- obj -> handle .Init .DiscontinuousConvMode = DISABLE ;
87
- obj -> handle .Init .ExternalTrigConv = ADC_SOFTWARE_START ;
88
- obj -> handle .Init .ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE ;
89
- obj -> handle .Init .DMAContinuousRequests = DISABLE ;
90
- obj -> handle .Init .Overrun = OVR_DATA_OVERWRITTEN ;
91
- if (HAL_ADC_Init (& obj -> handle ) != HAL_OK ) {
92
- error ("Cannot initialize ADC" );
93
- }
94
- // Run the ADC calibration
95
- if (HAL_ADCEx_Calibration_Start (& obj -> handle ) != HAL_OK ) {
96
- error ("Cannot Start ADC_Calibration" );
97
- }
69
+ // Configure ADC object structures
70
+ obj -> handle .State = HAL_ADC_STATE_RESET ;
71
+ obj -> handle .Init .ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4 ;
72
+ obj -> handle .Init .Resolution = ADC_RESOLUTION_12B ;
73
+ obj -> handle .Init .DataAlign = ADC_DATAALIGN_RIGHT ;
74
+ obj -> handle .Init .ScanConvMode = ADC_SCAN_DIRECTION_FORWARD ;
75
+ obj -> handle .Init .EOCSelection = EOC_SINGLE_CONV ;
76
+ obj -> handle .Init .LowPowerAutoWait = DISABLE ;
77
+ obj -> handle .Init .LowPowerAutoPowerOff = DISABLE ;
78
+ obj -> handle .Init .ContinuousConvMode = DISABLE ;
79
+ obj -> handle .Init .DiscontinuousConvMode = DISABLE ;
80
+ obj -> handle .Init .ExternalTrigConv = ADC_SOFTWARE_START ;
81
+ obj -> handle .Init .ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE ;
82
+ obj -> handle .Init .DMAContinuousRequests = DISABLE ;
83
+ obj -> handle .Init .Overrun = OVR_DATA_OVERWRITTEN ;
84
+
85
+ __HAL_RCC_ADC1_CLK_ENABLE ();
86
+
87
+ if (HAL_ADC_Init (& obj -> handle ) != HAL_OK ) {
88
+ error ("Cannot initialize ADC" );
89
+ }
90
+
91
+ // ADC calibration is done only once
92
+ if (adc_calibrated == 0 ) {
93
+ adc_calibrated = 1 ;
94
+ HAL_ADCEx_Calibration_Start (& obj -> handle );
98
95
}
99
96
}
100
97
101
- static inline uint16_t adc_read (analogin_t * obj ) {
102
- ADC_ChannelConfTypeDef sConfig ;
98
+ static inline uint16_t adc_read (analogin_t * obj )
99
+ {
100
+ ADC_ChannelConfTypeDef sConfig = {0 };
103
101
104
102
// Configure ADC channel
105
103
sConfig .Rank = ADC_RANK_CHANNEL_NUMBER ;
@@ -182,20 +180,22 @@ static inline uint16_t adc_read(analogin_t *obj) {
182
180
183
181
// Wait end of conversion and get value
184
182
if (HAL_ADC_PollForConversion (& obj -> handle , 10 ) == HAL_OK ) {
185
- return (HAL_ADC_GetValue (& obj -> handle ) );
183
+ return (uint16_t ) HAL_ADC_GetValue (& obj -> handle );
186
184
} else {
187
185
return 0 ;
188
186
}
189
187
}
190
188
191
- uint16_t analogin_read_u16 (analogin_t * obj ) {
189
+ uint16_t analogin_read_u16 (analogin_t * obj )
190
+ {
192
191
uint16_t value = adc_read (obj );
193
192
// 12-bit to 16-bit conversion
194
193
value = ((value << 4 ) & (uint16_t )0xFFF0 ) | ((value >> 8 ) & (uint16_t )0x000F );
195
194
return value ;
196
195
}
197
196
198
- float analogin_read (analogin_t * obj ) {
197
+ float analogin_read (analogin_t * obj )
198
+ {
199
199
uint16_t value = adc_read (obj );
200
200
return (float )value * (1.0f / (float )0xFFF ); // 12 bits range
201
201
}
0 commit comments