STM32F3 ADC internal channels update

pull/7228/head
jeromecoutant 2018-03-21 17:46:10 +01:00 committed by adbridge
parent aa84af280c
commit a95bdd8caf
3 changed files with 41 additions and 28 deletions

View File

@ -35,11 +35,9 @@
#include "pinmap.h" #include "pinmap.h"
#include "mbed_error.h" #include "mbed_error.h"
#include "PeripheralPins.h" #include "PeripheralPins.h"
#include <stdbool.h>
void analogin_init(analogin_t *obj, PinName pin) void analogin_init(analogin_t *obj, PinName pin)
{ {
static bool adc_calibrated = false;
uint32_t function = (uint32_t)NC; uint32_t function = (uint32_t)NC;
// ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...) // ADC Internal Channels "pins" (Temperature, Vref, Vbat, ...)
@ -106,12 +104,10 @@ void analogin_init(analogin_t *obj, PinName pin)
#endif #endif
if (HAL_ADC_Init(&obj->handle) != HAL_OK) { if (HAL_ADC_Init(&obj->handle) != HAL_OK) {
error("Cannot initialize ADC"); error("Cannot initialize ADC\n");
} }
// ADC calibration is done only once if (!HAL_ADCEx_Calibration_GetValue(&obj->handle, ADC_SINGLE_ENDED)) {
if (!adc_calibrated) {
adc_calibrated = true;
HAL_ADCEx_Calibration_Start(&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; sConfig.Channel = ADC_CHANNEL_14;
break; break;
case 15: 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; break;
case 16: 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; break;
case 17: 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; break;
case 18: case 18:
sConfig.Channel = ADC_CHANNEL_18; sConfig.SamplingTime = ADC_SAMPLETIME_181CYCLES_5;
sConfig.Channel = ADC_CHANNEL_VREFINT;
break; break;
default: default:
return 0; return 0;

View File

@ -47,7 +47,10 @@
/* Include ADC HAL Extended module */ /* Include ADC HAL Extended module */
/* (include on top of file since ADC structures are defined in extended file) */ /* (include on top of file since ADC structures are defined in extended file) */
#include "stm32f3xx_hal_adc_ex.h" #include "stm32f3xx_hal_adc_ex.h"
/* Include low level driver */
#include "stm32f3xx_ll_adc.h"
/** @addtogroup STM32F3xx_HAL_Driver /** @addtogroup STM32F3xx_HAL_Driver
* @{ * @{
*/ */

View File

@ -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 */ /* Set handle of the other ADC sharing the same common register */
ADC_COMMON_ADC_OTHER(hadc, &tmphadcSharingSameCommonRegister); 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 */ /* If Channel_16 is selected, enable Temp. sensor measurement path */
/* Note: Temp. sensor internal channels available on ADC1 only */ /* Note: Temp. sensor internal channels available on ADC1 only */
if ((sConfig->Channel == ADC_CHANNEL_TEMPSENSOR) && (hadc->Instance == ADC1)) 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); 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;
}
} }
} }