diff --git a/targets/TARGET_STM/TARGET_STM32F3/analogin_device.c b/targets/TARGET_STM/TARGET_STM32F3/analogin_device.c index c903bb898a..dd447d90bf 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/analogin_device.c +++ b/targets/TARGET_STM/TARGET_STM32F3/analogin_device.c @@ -35,11 +35,9 @@ #include "pinmap.h" #include "mbed_error.h" #include "PeripheralPins.h" -#include void analogin_init(analogin_t *obj, PinName pin) { - static bool adc_calibrated = false; uint32_t function = (uint32_t)NC; // ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...) @@ -106,12 +104,10 @@ void analogin_init(analogin_t *obj, PinName pin) #endif if (HAL_ADC_Init(&obj->handle) != HAL_OK) { - error("Cannot initialize ADC"); + error("Cannot initialize ADC\n"); } - // ADC calibration is done only once - if (!adc_calibrated) { - adc_calibrated = true; + if (!HAL_ADCEx_Calibration_GetValue(&obj->handle, ADC_SINGLE_ENDED)) { HAL_ADCEx_Calibration_Start(&obj->handle, ADC_SINGLE_ENDED); } } @@ -171,16 +167,47 @@ uint16_t adc_read(analogin_t *obj) sConfig.Channel = ADC_CHANNEL_14; break; case 15: - sConfig.Channel = ADC_CHANNEL_15; + if ((ADCName)obj->handle.Instance == ADC_1) { + sConfig.Channel = ADC_CHANNEL_VOPAMP1; + sConfig.SamplingTime = ADC_SAMPLETIME_181CYCLES_5; + } + else { + sConfig.Channel = ADC_CHANNEL_15; + } break; case 16: - sConfig.Channel = ADC_CHANNEL_16; + if ((ADCName)obj->handle.Instance == ADC_1) { + sConfig.Channel = ADC_CHANNEL_TEMPSENSOR; + sConfig.SamplingTime = ADC_SAMPLETIME_181CYCLES_5; + } + else { + sConfig.Channel = ADC_CHANNEL_16; + } break; case 17: - sConfig.Channel = ADC_CHANNEL_17; + sConfig.SamplingTime = ADC_SAMPLETIME_181CYCLES_5; + if ((ADCName)obj->handle.Instance == ADC_1) { + sConfig.Channel = ADC_CHANNEL_VBAT; + } +#if defined(ADC2) + if ((ADCName)obj->handle.Instance == ADC_2) { + sConfig.Channel = ADC_CHANNEL_VOPAMP2; + } +#endif +#if defined(ADC3) + if ((ADCName)obj->handle.Instance == ADC_3) { + sConfig.Channel = ADC_CHANNEL_VOPAMP3; + } +#endif +#if defined(ADC4) + if ((ADCName)obj->handle.Instance == ADC_4) { + sConfig.Channel = ADC_CHANNEL_VOPAMP4; + } +#endif break; case 18: - sConfig.Channel = ADC_CHANNEL_18; + sConfig.SamplingTime = ADC_SAMPLETIME_181CYCLES_5; + sConfig.Channel = ADC_CHANNEL_VREFINT; break; default: return 0; diff --git a/targets/TARGET_STM/TARGET_STM32F3/device/stm32f3xx_hal_adc.h b/targets/TARGET_STM/TARGET_STM32F3/device/stm32f3xx_hal_adc.h index 16eca9a7bb..a110d853bc 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/device/stm32f3xx_hal_adc.h +++ b/targets/TARGET_STM/TARGET_STM32F3/device/stm32f3xx_hal_adc.h @@ -47,7 +47,10 @@ /* Include ADC HAL Extended module */ /* (include on top of file since ADC structures are defined in extended file) */ #include "stm32f3xx_hal_adc_ex.h" - + +/* Include low level driver */ +#include "stm32f3xx_ll_adc.h" + /** @addtogroup STM32F3xx_HAL_Driver * @{ */ diff --git a/targets/TARGET_STM/TARGET_STM32F3/device/stm32f3xx_hal_adc_ex.c b/targets/TARGET_STM/TARGET_STM32F3/device/stm32f3xx_hal_adc_ex.c index cd93581c54..b688574722 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/device/stm32f3xx_hal_adc_ex.c +++ b/targets/TARGET_STM/TARGET_STM32F3/device/stm32f3xx_hal_adc_ex.c @@ -5660,12 +5660,6 @@ HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConf /* Set handle of the other ADC sharing the same common register */ ADC_COMMON_ADC_OTHER(hadc, &tmphadcSharingSameCommonRegister); - /* Software is allowed to change common parameters only when all ADCs */ - /* of the common group are disabled. */ - if ((ADC_IS_ENABLE(hadc) == RESET) && - ( (tmphadcSharingSameCommonRegister.Instance == NULL) || - (ADC_IS_ENABLE(&tmphadcSharingSameCommonRegister) == RESET) ) ) - { /* If Channel_16 is selected, enable Temp. sensor measurement path */ /* Note: Temp. sensor internal channels available on ADC1 only */ if ((sConfig->Channel == ADC_CHANNEL_TEMPSENSOR) && (hadc->Instance == ADC1)) @@ -5694,17 +5688,6 @@ HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConf { SET_BIT(tmpADC_Common->CCR, ADC_CCR_VREFEN); } - } - /* If the requested internal measurement path has already been */ - /* enabled and other ADC of the common group are enabled, internal */ - /* measurement paths cannot be enabled. */ - else - { - /* Update ADC state machine to error */ - SET_BIT(hadc->State, HAL_ADC_STATE_ERROR_CONFIG); - - tmp_hal_status = HAL_ERROR; - } } }