Skip to content

Commit 7c272fa

Browse files
authored
Merge pull request #6412 from jeromecoutant/PR_L4_ADC
STM32L4 ADC correct internal channel management
2 parents 04a3635 + ef00693 commit 7c272fa

File tree

1 file changed

+44
-60
lines changed

1 file changed

+44
-60
lines changed

targets/TARGET_STM/TARGET_STM32L4/device/stm32l4xx_hal_adc.c

Lines changed: 44 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2312,7 +2312,6 @@ HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConf
23122312
/* Parameters update conditioned to ADC state: */
23132313
/* Parameters that can be updated only when ADC is disabled: */
23142314
/* - Single or differential mode */
2315-
/* - Internal measurement channels: Vbat/VrefInt/TempSensor */
23162315
if (ADC_IS_ENABLE(hadc) == RESET)
23172316
{
23182317
/* Set mode single-ended or differential input of the selected ADC channel */
@@ -2325,71 +2324,56 @@ HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConf
23252324
LL_ADC_SetChannelSamplingTime(hadc->Instance, __LL_ADC_DECIMAL_NB_TO_CHANNEL(__LL_ADC_CHANNEL_TO_DECIMAL_NB(sConfig->Channel) + 1), sConfig->SamplingTime);
23262325
}
23272326

2328-
/* Management of internal measurement channels: Vbat/VrefInt/TempSensor. */
2329-
/* If internal channel selected, enable dedicated internal buffers and */
2330-
/* paths. */
2331-
/* Note: these internal measurement paths can be disabled using */
2332-
/* HAL_ADC_DeInit(). */
2333-
2334-
/* Configuration of common ADC parameters */
2335-
/* If the requested internal measurement path has already been enabled, */
2336-
/* bypass the configuration processing. */
2337-
if (( (sConfig->Channel == ADC_CHANNEL_TEMPSENSOR) &&
2338-
((LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) & LL_ADC_PATH_INTERNAL_TEMPSENSOR) == 0U)) ||
2339-
( (sConfig->Channel == ADC_CHANNEL_VBAT) &&
2340-
((LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) & LL_ADC_PATH_INTERNAL_VBAT) == 0U)) ||
2341-
( (sConfig->Channel == ADC_CHANNEL_VREFINT) &&
2342-
((LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) & LL_ADC_PATH_INTERNAL_VREFINT) == 0U))
2343-
)
2327+
}
2328+
2329+
/* Management of internal measurement channels: Vbat/VrefInt/TempSensor. */
2330+
/* If internal channel selected, enable dedicated internal buffers and */
2331+
/* paths. */
2332+
/* Note: these internal measurement paths can be disabled using */
2333+
/* HAL_ADC_DeInit(). */
2334+
2335+
/* Configuration of common ADC parameters */
2336+
/* If the requested internal measurement path has already been enabled, */
2337+
/* bypass the configuration processing. */
2338+
if (( (sConfig->Channel == ADC_CHANNEL_TEMPSENSOR) &&
2339+
((LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) & LL_ADC_PATH_INTERNAL_TEMPSENSOR) == 0U)) ||
2340+
( (sConfig->Channel == ADC_CHANNEL_VBAT) &&
2341+
((LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) & LL_ADC_PATH_INTERNAL_VBAT) == 0U)) ||
2342+
( (sConfig->Channel == ADC_CHANNEL_VREFINT) &&
2343+
((LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)) & LL_ADC_PATH_INTERNAL_VREFINT) == 0U))
2344+
)
2345+
{
2346+
/* Configuration of common ADC parameters (continuation) */
2347+
2348+
if (sConfig->Channel == ADC_CHANNEL_TEMPSENSOR)
23442349
{
2345-
/* Configuration of common ADC parameters (continuation) */
2346-
2347-
/* Software is allowed to change common parameters only when all ADCs */
2348-
/* of the common group are disabled. */
2349-
if ((ADC_IS_ENABLE(hadc) == RESET) &&
2350-
(ADC_ANY_OTHER_ENABLED(hadc) == RESET) )
2350+
if (ADC_TEMPERATURE_SENSOR_INSTANCE(hadc))
23512351
{
2352-
if (sConfig->Channel == ADC_CHANNEL_TEMPSENSOR)
2353-
{
2354-
if (ADC_TEMPERATURE_SENSOR_INSTANCE(hadc))
2355-
{
2356-
LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_TEMPSENSOR | LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)));
2357-
2358-
/* Delay for temperature sensor stabilization time */
2359-
/* Wait loop initialization and execution */
2360-
/* Note: Variable divided by 2 to compensate partially */
2361-
/* CPU processing cycles. */
2362-
wait_loop_index = (LL_ADC_DELAY_TEMPSENSOR_STAB_US * (SystemCoreClock / (1000000 * 2)));
2363-
while(wait_loop_index != 0)
2364-
{
2365-
wait_loop_index--;
2366-
}
2367-
}
2368-
}
2369-
else if (sConfig->Channel == ADC_CHANNEL_VBAT)
2352+
LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_TEMPSENSOR | LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)));
2353+
2354+
/* Delay for temperature sensor stabilization time */
2355+
/* Wait loop initialization and execution */
2356+
/* Note: Variable divided by 2 to compensate partially */
2357+
/* CPU processing cycles. */
2358+
wait_loop_index = (LL_ADC_DELAY_TEMPSENSOR_STAB_US * (SystemCoreClock / (1000000 * 2)));
2359+
while(wait_loop_index != 0)
23702360
{
2371-
if (ADC_BATTERY_VOLTAGE_INSTANCE(hadc))
2372-
{
2373-
LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VBAT | LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)));
2374-
}
2375-
}
2376-
else if (sConfig->Channel == ADC_CHANNEL_VREFINT)
2377-
{
2378-
if (ADC_VREFINT_INSTANCE(hadc))
2379-
{
2380-
LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VREFINT | LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)));
2381-
}
2361+
wait_loop_index--;
23822362
}
23832363
}
2384-
/* If the requested internal measurement path has already been */
2385-
/* enabled and other ADC of the common group are enabled, internal */
2386-
/* measurement paths cannot be enabled. */
2387-
else
2364+
}
2365+
else if (sConfig->Channel == ADC_CHANNEL_VBAT)
2366+
{
2367+
if (ADC_BATTERY_VOLTAGE_INSTANCE(hadc))
23882368
{
2389-
/* Update ADC state machine to error */
2390-
SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG);
2391-
2392-
tmp_hal_status = HAL_ERROR;
2369+
LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VBAT | LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)));
2370+
}
2371+
}
2372+
else if (sConfig->Channel == ADC_CHANNEL_VREFINT)
2373+
{
2374+
if (ADC_VREFINT_INSTANCE(hadc))
2375+
{
2376+
LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance), LL_ADC_PATH_INTERNAL_VREFINT | LL_ADC_GetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(hadc->Instance)));
23932377
}
23942378
}
23952379
}

0 commit comments

Comments
 (0)