Skip to content

Commit 538552a

Browse files
committed
STM32H7 ADC issue correction
1 parent 59df4ef commit 538552a

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

targets/TARGET_STM/TARGET_STM32H7/analogin_device.c

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,42 @@ void analogin_init(analogin_t *obj, PinName pin)
6868

6969
// Configure ADC object structures
7070
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;
7272
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;
7476
obj->handle.Init.ContinuousConvMode = DISABLE;
77+
obj->handle.Init.NbrOfConversion = 1;
7578
obj->handle.Init.DiscontinuousConvMode = DISABLE;
7679
obj->handle.Init.NbrOfDiscConversion = 0;
80+
obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
7781
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;
8182
obj->handle.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR;
82-
obj->handle.Init.EOCSelection = DISABLE;
83-
obj->handle.Init.LowPowerAutoWait = DISABLE;
8483
obj->handle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
84+
obj->handle.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
8585
obj->handle.Init.OversamplingMode = DISABLE;
8686

87-
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
88-
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_ADC;
89-
PeriphClkInitStruct.AdcClockSelection = RCC_ADCCLKSOURCE_CLKP;
90-
PeriphClkInitStruct.PLL2.PLL2P = 4;
9187
#if defined(DUAL_CORE)
9288
while (LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID)) {
9389
}
9490
#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+
97107
#if defined(DUAL_CORE)
98108
LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, HSEM_CR_COREID_CURRENT);
99109
#endif /* DUAL_CORE */
@@ -115,7 +125,15 @@ void analogin_init(analogin_t *obj, PinName pin)
115125
#endif
116126

117127
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+
}
119137
}
120138

121139
// Calibration
@@ -223,11 +241,13 @@ uint16_t adc_read(analogin_t *obj)
223241
return 0;
224242
}
225243

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+
}
229247

230-
HAL_ADC_Start(&obj->handle); // Start conversion
248+
if (HAL_ADC_Start(&obj->handle) != HAL_OK) {
249+
error("HAL_ADC_Start issue");
250+
}
231251

232252
// Wait end of conversion and get value
233253
uint16_t adcValue = 0;

0 commit comments

Comments
 (0)