STM32F0 internal temperature sensor always returns 0x0FFF
Hi
I'm using the STM32F091RC controller and I try to read the value from the internal temperature sensor using the HAL functions in a FreeRTOS project. The code I use:
static HAL_StatusTypeDef initADC(void) {
/**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc.Instance = ADC1;
hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
hadc.Init.Resolution = ADC_RESOLUTION_12B;
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc.Init.LowPowerAutoWait = DISABLE;
hadc.Init.LowPowerAutoPowerOff = DISABLE;
hadc.Init.ContinuousConvMode = ENABLE;
hadc.Init.DiscontinuousConvMode = DISABLE;
hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc.Init.DMAContinuousRequests = DISABLE;
hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
if (HAL_ADC_Init(&hadc) != HAL_OK) {
printLogError(ERR_HalAdcError, "Initialization of ADC failed, error code %u", hadc.ErrorCode);
errorHandler();
}
if (HAL_ADCEx_Calibration_Start(&hadc) != HAL_OK) {
printLogError(ERR_HalAdcError, "Calibration of ADC failed, error code %u", hadc.ErrorCode);
errorHandler();
}
return HAL_OK;
}This function is called when the peripheral "internal temperature sensor" is enabled.
After enabling it, I try to read the value of the temperature sensor with the following code:
uint32_t adc_value;
ADC_ChannelConfTypeDef adc_config = { 0 };
//Add channel to Conversion list
adc_config.Channel = ADC_CHANNEL_TEMPSENSOR;
adc_config.Rank = ADC_RANK_CHANNEL_NUMBER;
adc_config.SamplingTime = ADC_SAMPLETIME_28CYCLES_5;
HAL_StatusTypeDef status;
status = HAL_ADC_ConfigChannel(&hadc, &adc_config);
if (status != HAL_OK) {
printLogWarning(ERR_HalAdcError,
"ADC configuration of channel %u failed, error code %u",
hardware_settings->analogInputNr,
hadc.ErrorCode);
return halToCANxError(status);
}
// stop scheduling as a workaround mentioned in
// the errata sheet "ES0282 Rev 3" in May 2018
vTaskSuspendAll();
status = HAL_ADC_Start(&hadc);
if (status != HAL_OK) {
printLogWarning(ERR_HalAdcError,
"Starting of ADC conversion failed, error code %u",
hadc.ErrorCode);
return halToCANxError(status);
}
status = HAL_ADC_PollForConversion(&hadc, 10);
if (status != HAL_OK) {
printLogWarning(ERR_HalAdcError,
"Waiting for ADC conversion failed, error code %u",
hadc.ErrorCode);
return halToCANxError(status);
}
adc_value = HAL_ADC_GetValue(&hadc);
status = HAL_ADC_Stop(&hadc);
if (status != HAL_OK) {
printLogWarning(ERR_HalAdcError,
"Stopping ADC conversion failed, error code %u",
hadc.ErrorCode);
return halToCANxError(status);
}
xTaskResumeAll();
// restart scheduling
//Remove channel to Conversion list
adc_config.Rank = ADC_RANK_NONE;
status = HAL_ADC_ConfigChannel(&hadc, &adc_config);
if (status != HAL_OK) {
printLogWarning(ERR_HalAdcError,
"Removing channel %u from ADC conversion list failed, error code %u",
hardware_settings->analogInputNr,
hadc.ErrorCode);
return halToCANxError(status);
}
...The same code is used for hardware ADC inputs and there it works. But for the temperature sensor, the feedback value is always 0x0FFF.
Any idea what the problem could be?
Thanks.
Corono
