STM32H7 ADC issue correction

pull/13073/head
jeromecoutant 2020-06-04 17:06:11 +02:00
parent 59df4efaac
commit 538552adea
1 changed files with 38 additions and 18 deletions

View File

@ -68,32 +68,42 @@ void analogin_init(analogin_t *obj, PinName pin)
// Configure ADC object structures
obj->handle.State = HAL_ADC_STATE_RESET;
obj->handle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
obj->handle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV4;
obj->handle.Init.Resolution = ADC_RESOLUTION_16B;
obj->handle.Init.ScanConvMode = DISABLE;
obj->handle.Init.ScanConvMode = ADC_SCAN_DISABLE;
obj->handle.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
obj->handle.Init.LowPowerAutoWait = DISABLE;
obj->handle.Init.ContinuousConvMode = DISABLE;
obj->handle.Init.NbrOfConversion = 1;
obj->handle.Init.DiscontinuousConvMode = DISABLE;
obj->handle.Init.NbrOfDiscConversion = 0;
obj->handle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
obj->handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
obj->handle.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T1_CC1;
obj->handle.Init.LeftBitShift = 0;
obj->handle.Init.NbrOfConversion = 1;
obj->handle.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR;
obj->handle.Init.EOCSelection = DISABLE;
obj->handle.Init.LowPowerAutoWait = DISABLE;
obj->handle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
obj->handle.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
obj->handle.Init.OversamplingMode = DISABLE;
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_ADC;
PeriphClkInitStruct.AdcClockSelection = RCC_ADCCLKSOURCE_CLKP;
PeriphClkInitStruct.PLL2.PLL2P = 4;
#if defined(DUAL_CORE)
while (LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID)) {
}
#endif /* DUAL_CORE */
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
__HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_CLKP);
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_ADC;
PeriphClkInitStruct.PLL2.PLL2M = 4;
PeriphClkInitStruct.PLL2.PLL2N = 240;
PeriphClkInitStruct.PLL2.PLL2P = 2;
PeriphClkInitStruct.PLL2.PLL2Q = 2;
PeriphClkInitStruct.PLL2.PLL2R = 2;
PeriphClkInitStruct.PLL2.PLL2RGE = RCC_PLL2VCIRANGE_1;
PeriphClkInitStruct.PLL2.PLL2VCOSEL = RCC_PLL2VCOWIDE;
PeriphClkInitStruct.PLL2.PLL2FRACN = 0;
PeriphClkInitStruct.AdcClockSelection = RCC_ADCCLKSOURCE_PLL2;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) {
error("analogin_init HAL_RCCEx_PeriphCLKConfig");
}
#if defined(DUAL_CORE)
LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, HSEM_CR_COREID_CURRENT);
#endif /* DUAL_CORE */
@ -115,7 +125,15 @@ void analogin_init(analogin_t *obj, PinName pin)
#endif
if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
error("Cannot initialize ADC");
error("analogin_init HAL_ADC_Init");
}
if ( ((ADCName)obj->handle.Instance == ADC_1) || ((ADCName)obj->handle.Instance == ADC_2) ) {
ADC_MultiModeTypeDef multimode = {0};
multimode.Mode = ADC_MODE_INDEPENDENT;
if (HAL_ADCEx_MultiModeConfigChannel(&obj->handle, &multimode) != HAL_OK) {
error("analogin_init HAL_ADC_Init");
}
}
// Calibration
@ -223,11 +241,13 @@ uint16_t adc_read(analogin_t *obj)
return 0;
}
LL_ADC_Disable((&obj->handle)->Instance);
if (HAL_ADC_ConfigChannel(&obj->handle, &sConfig) != HAL_OK) {
error("HAL_ADC_ConfigChannel issue");
}
HAL_ADC_ConfigChannel(&obj->handle, &sConfig);
HAL_ADC_Start(&obj->handle); // Start conversion
if (HAL_ADC_Start(&obj->handle) != HAL_OK) {
error("HAL_ADC_Start issue");
}
// Wait end of conversion and get value
uint16_t adcValue = 0;