@@ -68,32 +68,42 @@ void analogin_init(analogin_t *obj, PinName pin)
68
68
69
69
// Configure ADC object structures
70
70
obj -> handle .State = HAL_ADC_STATE_RESET ;
71
- obj -> handle .Init .ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4 ;
71
+ obj -> handle .Init .ClockPrescaler = ADC_CLOCK_ASYNC_DIV4 ;
72
72
obj -> handle .Init .Resolution = ADC_RESOLUTION_16B ;
73
- obj -> handle .Init .ScanConvMode = DISABLE ;
73
+ obj -> handle .Init .ScanConvMode = ADC_SCAN_DISABLE ;
74
+ obj -> handle .Init .EOCSelection = ADC_EOC_SINGLE_CONV ;
75
+ obj -> handle .Init .LowPowerAutoWait = DISABLE ;
74
76
obj -> handle .Init .ContinuousConvMode = DISABLE ;
77
+ obj -> handle .Init .NbrOfConversion = 1 ;
75
78
obj -> handle .Init .DiscontinuousConvMode = DISABLE ;
76
79
obj -> handle .Init .NbrOfDiscConversion = 0 ;
80
+ obj -> handle .Init .ExternalTrigConv = ADC_SOFTWARE_START ;
77
81
obj -> handle .Init .ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE ;
78
- obj -> handle .Init .ExternalTrigConv = ADC_EXTERNALTRIG_T1_CC1 ;
79
- obj -> handle .Init .LeftBitShift = 0 ;
80
- obj -> handle .Init .NbrOfConversion = 1 ;
81
82
obj -> handle .Init .ConversionDataManagement = ADC_CONVERSIONDATA_DR ;
82
- obj -> handle .Init .EOCSelection = DISABLE ;
83
- obj -> handle .Init .LowPowerAutoWait = DISABLE ;
84
83
obj -> handle .Init .Overrun = ADC_OVR_DATA_OVERWRITTEN ;
84
+ obj -> handle .Init .LeftBitShift = ADC_LEFTBITSHIFT_NONE ;
85
85
obj -> handle .Init .OversamplingMode = DISABLE ;
86
86
87
- RCC_PeriphCLKInitTypeDef PeriphClkInitStruct ;
88
- PeriphClkInitStruct .PeriphClockSelection = RCC_PERIPHCLK_ADC ;
89
- PeriphClkInitStruct .AdcClockSelection = RCC_ADCCLKSOURCE_CLKP ;
90
- PeriphClkInitStruct .PLL2 .PLL2P = 4 ;
91
87
#if defined(DUAL_CORE )
92
88
while (LL_HSEM_1StepLock (HSEM , CFG_HW_RCC_SEMID )) {
93
89
}
94
90
#endif /* DUAL_CORE */
95
- HAL_RCCEx_PeriphCLKConfig (& PeriphClkInitStruct );
96
- __HAL_RCC_ADC_CONFIG (RCC_ADCCLKSOURCE_CLKP );
91
+
92
+ RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0 };
93
+ PeriphClkInitStruct .PeriphClockSelection = RCC_PERIPHCLK_ADC ;
94
+ PeriphClkInitStruct .PLL2 .PLL2M = 4 ;
95
+ PeriphClkInitStruct .PLL2 .PLL2N = 240 ;
96
+ PeriphClkInitStruct .PLL2 .PLL2P = 2 ;
97
+ PeriphClkInitStruct .PLL2 .PLL2Q = 2 ;
98
+ PeriphClkInitStruct .PLL2 .PLL2R = 2 ;
99
+ PeriphClkInitStruct .PLL2 .PLL2RGE = RCC_PLL2VCIRANGE_1 ;
100
+ PeriphClkInitStruct .PLL2 .PLL2VCOSEL = RCC_PLL2VCOWIDE ;
101
+ PeriphClkInitStruct .PLL2 .PLL2FRACN = 0 ;
102
+ PeriphClkInitStruct .AdcClockSelection = RCC_ADCCLKSOURCE_PLL2 ;
103
+ if (HAL_RCCEx_PeriphCLKConfig (& PeriphClkInitStruct ) != HAL_OK ) {
104
+ error ("analogin_init HAL_RCCEx_PeriphCLKConfig" );
105
+ }
106
+
97
107
#if defined(DUAL_CORE )
98
108
LL_HSEM_ReleaseLock (HSEM , CFG_HW_RCC_SEMID , HSEM_CR_COREID_CURRENT );
99
109
#endif /* DUAL_CORE */
@@ -115,7 +125,15 @@ void analogin_init(analogin_t *obj, PinName pin)
115
125
#endif
116
126
117
127
if (HAL_ADC_Init (& obj -> handle ) != HAL_OK ) {
118
- error ("Cannot initialize ADC" );
128
+ error ("analogin_init HAL_ADC_Init" );
129
+ }
130
+
131
+ if ( ((ADCName )obj -> handle .Instance == ADC_1 ) || ((ADCName )obj -> handle .Instance == ADC_2 ) ) {
132
+ ADC_MultiModeTypeDef multimode = {0 };
133
+ multimode .Mode = ADC_MODE_INDEPENDENT ;
134
+ if (HAL_ADCEx_MultiModeConfigChannel (& obj -> handle , & multimode ) != HAL_OK ) {
135
+ error ("analogin_init HAL_ADC_Init" );
136
+ }
119
137
}
120
138
121
139
// Calibration
@@ -223,11 +241,13 @@ uint16_t adc_read(analogin_t *obj)
223
241
return 0 ;
224
242
}
225
243
226
- LL_ADC_Disable ( (& obj -> handle ) -> Instance );
227
-
228
- HAL_ADC_ConfigChannel ( & obj -> handle , & sConfig );
244
+ if ( HAL_ADC_ConfigChannel (& obj -> handle , & sConfig ) != HAL_OK ) {
245
+ error ( "HAL_ADC_ConfigChannel issue" );
246
+ }
229
247
230
- HAL_ADC_Start (& obj -> handle ); // Start conversion
248
+ if (HAL_ADC_Start (& obj -> handle ) != HAL_OK ) {
249
+ error ("HAL_ADC_Start issue" );
250
+ }
231
251
232
252
// Wait end of conversion and get value
233
253
uint16_t adcValue = 0 ;
0 commit comments