From ec00ea565555b24bea4076091e13f4affcbfa9ce Mon Sep 17 00:00:00 2001 From: jeromecoutant Date: Fri, 15 Mar 2019 17:26:30 +0100 Subject: [PATCH] STM32 ADC INTERNAL CHANNEL reset after read Internal channels use is enabling ADC "internal path" which needs to be disabled after measurement --- .../TARGET_STM/TARGET_STM32F0/analogin_device.c | 7 ++++--- .../TARGET_STM/TARGET_STM32F1/analogin_device.c | 7 ++++--- .../TARGET_STM32F1/device/stm32f1xx_hal_adc.h | 4 ++++ .../TARGET_STM/TARGET_STM32F2/analogin_device.c | 7 ++++--- .../TARGET_STM32F2/device/stm32f2xx_hal_adc.h | 3 +++ .../TARGET_STM/TARGET_STM32F3/analogin_device.c | 2 +- .../TARGET_STM/TARGET_STM32F4/analogin_device.c | 7 ++++--- .../TARGET_STM/TARGET_STM32F7/analogin_device.c | 7 ++++--- .../TARGET_STM/TARGET_STM32H7/analogin_device.c | 7 ++++--- .../TARGET_STM/TARGET_STM32L0/analogin_device.c | 5 ++--- .../TARGET_STM/TARGET_STM32L1/analogin_device.c | 7 ++++--- .../TARGET_STM/TARGET_STM32L4/analogin_device.c | 14 ++++---------- 12 files changed, 42 insertions(+), 35 deletions(-) diff --git a/targets/TARGET_STM/TARGET_STM32F0/analogin_device.c b/targets/TARGET_STM/TARGET_STM32F0/analogin_device.c index b625bb72649..7755174ed75 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/analogin_device.c +++ b/targets/TARGET_STM/TARGET_STM32F0/analogin_device.c @@ -175,11 +175,12 @@ uint16_t adc_read(analogin_t *obj) HAL_ADC_Start(&obj->handle); // Start conversion // Wait end of conversion and get value + uint16_t adcValue = 0; if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) { - return (uint16_t)HAL_ADC_GetValue(&obj->handle); - } else { - return 0; + adcValue = (uint16_t)HAL_ADC_GetValue(&obj->handle); } + LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE((&obj->handle)->Instance), LL_ADC_PATH_INTERNAL_NONE); + return adcValue; } const PinMap *analogin_pinmap() diff --git a/targets/TARGET_STM/TARGET_STM32F1/analogin_device.c b/targets/TARGET_STM/TARGET_STM32F1/analogin_device.c index df61b7a056e..e920b90613e 100644 --- a/targets/TARGET_STM/TARGET_STM32F1/analogin_device.c +++ b/targets/TARGET_STM/TARGET_STM32F1/analogin_device.c @@ -173,11 +173,12 @@ uint16_t adc_read(analogin_t *obj) HAL_ADC_Start(&obj->handle); // Start conversion // Wait end of conversion and get value + uint16_t adcValue = 0; if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) { - return (uint16_t)HAL_ADC_GetValue(&obj->handle); - } else { - return 0; + adcValue = (uint16_t)HAL_ADC_GetValue(&obj->handle); } + LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE((&obj->handle)->Instance), LL_ADC_PATH_INTERNAL_NONE); + return adcValue; } const PinMap *analogin_pinmap() diff --git a/targets/TARGET_STM/TARGET_STM32F1/device/stm32f1xx_hal_adc.h b/targets/TARGET_STM/TARGET_STM32F1/device/stm32f1xx_hal_adc.h index d4f0d9a95c2..8bac17e8a7c 100644 --- a/targets/TARGET_STM/TARGET_STM32F1/device/stm32f1xx_hal_adc.h +++ b/targets/TARGET_STM/TARGET_STM32F1/device/stm32f1xx_hal_adc.h @@ -43,6 +43,10 @@ /* Includes ------------------------------------------------------------------*/ #include "stm32f1xx_hal_def.h" + +/* Include low level driver */ +#include "stm32f1xx_ll_adc.h" + /** @addtogroup STM32F1xx_HAL_Driver * @{ */ diff --git a/targets/TARGET_STM/TARGET_STM32F2/analogin_device.c b/targets/TARGET_STM/TARGET_STM32F2/analogin_device.c index 450031e1424..a7498b26fab 100644 --- a/targets/TARGET_STM/TARGET_STM32F2/analogin_device.c +++ b/targets/TARGET_STM/TARGET_STM32F2/analogin_device.c @@ -180,11 +180,12 @@ uint16_t adc_read(analogin_t *obj) HAL_ADC_Start(&obj->handle); // Start conversion // Wait end of conversion and get value + uint16_t adcValue = 0; if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) { - return (uint16_t)HAL_ADC_GetValue(&obj->handle); - } else { - return 0; + adcValue = (uint16_t)HAL_ADC_GetValue(&obj->handle); } + LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE((&obj->handle)->Instance), LL_ADC_PATH_INTERNAL_NONE); + return adcValue; } const PinMap *analogin_pinmap() diff --git a/targets/TARGET_STM/TARGET_STM32F2/device/stm32f2xx_hal_adc.h b/targets/TARGET_STM/TARGET_STM32F2/device/stm32f2xx_hal_adc.h index a20c4a0d73d..50ccc2f1ed4 100644 --- a/targets/TARGET_STM/TARGET_STM32F2/device/stm32f2xx_hal_adc.h +++ b/targets/TARGET_STM/TARGET_STM32F2/device/stm32f2xx_hal_adc.h @@ -46,6 +46,9 @@ /* Includes ------------------------------------------------------------------*/ #include "stm32f2xx_hal_def.h" +/* Include low level driver */ +#include "stm32f2xx_ll_adc.h" + /** @addtogroup STM32F2xx_HAL_Driver * @{ */ diff --git a/targets/TARGET_STM/TARGET_STM32F3/analogin_device.c b/targets/TARGET_STM/TARGET_STM32F3/analogin_device.c index 56ab3004310..f4d85037060 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/analogin_device.c +++ b/targets/TARGET_STM/TARGET_STM32F3/analogin_device.c @@ -227,7 +227,7 @@ uint16_t adc_read(analogin_t *obj) } else { debug("HAL_ADC_PollForConversion issue\n"); } - + LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE((&obj->handle)->Instance), LL_ADC_PATH_INTERNAL_NONE); if (HAL_ADC_Stop(&obj->handle) != HAL_OK) { debug("HAL_ADC_Stop issue\n");; } diff --git a/targets/TARGET_STM/TARGET_STM32F4/analogin_device.c b/targets/TARGET_STM/TARGET_STM32F4/analogin_device.c index 2f38235e22f..75ac50f609b 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/analogin_device.c +++ b/targets/TARGET_STM/TARGET_STM32F4/analogin_device.c @@ -186,11 +186,12 @@ uint16_t adc_read(analogin_t *obj) HAL_ADC_Start(&obj->handle); // Start conversion // Wait end of conversion and get value + uint16_t adcValue = 0; if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) { - return (uint16_t)HAL_ADC_GetValue(&obj->handle); - } else { - return 0; + adcValue = (uint16_t)HAL_ADC_GetValue(&obj->handle); } + LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE((&obj->handle)->Instance), LL_ADC_PATH_INTERNAL_NONE); + return adcValue; } const PinMap *analogin_pinmap() diff --git a/targets/TARGET_STM/TARGET_STM32F7/analogin_device.c b/targets/TARGET_STM/TARGET_STM32F7/analogin_device.c index 14ef0e24948..e53027e2601 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/analogin_device.c +++ b/targets/TARGET_STM/TARGET_STM32F7/analogin_device.c @@ -186,11 +186,12 @@ uint16_t adc_read(analogin_t *obj) HAL_ADC_Start(&obj->handle); // Start conversion // Wait end of conversion and get value + uint16_t adcValue = 0; if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) { - return (uint16_t)HAL_ADC_GetValue(&obj->handle); - } else { - return 0; + adcValue = (uint16_t)HAL_ADC_GetValue(&obj->handle); } + LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE((&obj->handle)->Instance), LL_ADC_PATH_INTERNAL_NONE); + return adcValue; } const PinMap *analogin_pinmap() diff --git a/targets/TARGET_STM/TARGET_STM32H7/analogin_device.c b/targets/TARGET_STM/TARGET_STM32H7/analogin_device.c index 7a55c3a09d7..398a23f853b 100644 --- a/targets/TARGET_STM/TARGET_STM32H7/analogin_device.c +++ b/targets/TARGET_STM/TARGET_STM32H7/analogin_device.c @@ -223,11 +223,12 @@ uint16_t adc_read(analogin_t *obj) HAL_ADC_Start(&obj->handle); // Start conversion // Wait end of conversion and get value + uint16_t adcValue = 0; if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) { - return (uint16_t)HAL_ADC_GetValue(&obj->handle); - } else { - return 0; + adcValue = (uint16_t)HAL_ADC_GetValue(&obj->handle); } + LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE((&obj->handle)->Instance), LL_ADC_PATH_INTERNAL_NONE); + return adcValue; } const PinMap *analogin_pinmap() diff --git a/targets/TARGET_STM/TARGET_STM32L0/analogin_device.c b/targets/TARGET_STM/TARGET_STM32L0/analogin_device.c index 1bb70cfd5bf..11a1bd24223 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/analogin_device.c +++ b/targets/TARGET_STM/TARGET_STM32L0/analogin_device.c @@ -179,9 +179,8 @@ uint16_t adc_read(analogin_t *obj) if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) { adcValue = (uint16_t)HAL_ADC_GetValue(&obj->handle); } - sConfig.Rank = ADC_RANK_NONE; - HAL_ADC_ConfigChannel(&obj->handle, &sConfig); - return adcValue; + LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE((&obj->handle)->Instance), LL_ADC_PATH_INTERNAL_NONE); + return adcValue; } const PinMap *analogin_pinmap() diff --git a/targets/TARGET_STM/TARGET_STM32L1/analogin_device.c b/targets/TARGET_STM/TARGET_STM32L1/analogin_device.c index 766205055ba..d6471d0f4a8 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/analogin_device.c +++ b/targets/TARGET_STM/TARGET_STM32L1/analogin_device.c @@ -226,11 +226,12 @@ uint16_t adc_read(analogin_t *obj) HAL_ADC_Start(&obj->handle); // Start conversion // Wait end of conversion and get value + uint16_t adcValue = 0; if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) { - return (uint16_t)HAL_ADC_GetValue(&obj->handle); - } else { - return 0; + adcValue = (uint16_t)HAL_ADC_GetValue(&obj->handle); } + LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE((&obj->handle)->Instance), LL_ADC_PATH_INTERNAL_NONE); + return adcValue; } const PinMap *analogin_pinmap() diff --git a/targets/TARGET_STM/TARGET_STM32L4/analogin_device.c b/targets/TARGET_STM/TARGET_STM32L4/analogin_device.c index dd836c4d346..db013463ba6 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/analogin_device.c +++ b/targets/TARGET_STM/TARGET_STM32L4/analogin_device.c @@ -192,18 +192,12 @@ uint16_t adc_read(analogin_t *obj) HAL_ADC_Start(&obj->handle); // Start conversion // Wait end of conversion and get value + uint16_t adcValue = 0; if (HAL_ADC_PollForConversion(&obj->handle, 10) == HAL_OK) { - - /* Ref Manual: To prevent any unwanted consumption on the battery, - it is recommended to enable the bridge divider only when needed for ADC conversion */ - if (sConfig.Channel == ADC_CHANNEL_VBAT) { - CLEAR_BIT(__LL_ADC_COMMON_INSTANCE(obj->handle.Instance)->CCR, LL_ADC_PATH_INTERNAL_VBAT); - } - - return (uint16_t)HAL_ADC_GetValue(&obj->handle); - } else { - return 0; + adcValue = (uint16_t)HAL_ADC_GetValue(&obj->handle); } + LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE((&obj->handle)->Instance), LL_ADC_PATH_INTERNAL_NONE); + return adcValue; } const PinMap *analogin_pinmap()