Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STM32 HAL issue - reading internal temp after VBAT measurement. #1685

Closed
campag opened this issue Apr 23, 2016 · 4 comments
Closed

STM32 HAL issue - reading internal temp after VBAT measurement. #1685

campag opened this issue Apr 23, 2016 · 4 comments

Comments

@campag
Copy link

campag commented Apr 23, 2016

@bcostm
Measuring VBAT sets the ADC_CCR_VBATE bit in ADC->CCR, and there doesn't appear to be a way with the HAL to clear it.

If it isn't cleared, VBAT remains connected to the ADC channel in preference to temperature, so VBAT readings are returned in place of temperature. See the note at the bottom of page 226 of the reference manual RM0368, DM00096844.pdf

I resorted to poking the register directly:

float read_temp(void)
{
    ADC_ChannelConfTypeDef sConfig = {0};

    // Configure ADC channel
    sConfig.Rank         = 1;
    sConfig.SamplingTime = ADC_SAMPLETIME_84CYCLES;
    sConfig.Offset       = 0;
    sConfig.Channel      = ADC_CHANNEL_TEMPSENSOR;

    HAL_ADC_ConfigChannel(&hAdc, &sConfig);

    //disable Vbat from  ADC1_IN18 input channel, shared with temperature sensor
    ADC->CCR &= ~ADC_CCR_VBATE;
    //better added instead in stm32f4xx_hal_adc.c at line 1123, also line 753 in adc_ex.c?

    HAL_ADC_Start(&hAdc); // Start conversion

    // Wait end of conversion and get value
    if (HAL_ADC_PollForConversion(&hAdc, 10) == HAL_OK) {
        uint16_t value=HAL_ADC_GetValue(&hAdc);
        HAL_ADC_Stop(&hAdc);
        return  25.0f+(3.3f*(float)value/(float)ADCMAXVALUE-0.76f)/0.0025f;
    } else {
        return -273.15f; //hell has frozen over
    }
}

As per the comments in the above code, perhaps that bit should be cleared when configuring the ADC to read the temperature in stm32f4xx_hal_adc.c & stm32f4xx_hal_adc_ex.c ?

@bcostm
Copy link
Contributor

bcostm commented Apr 25, 2016

Hi, thanks for pointing that out. We'll have a deeper look when we'll add the ADC internal channels measurement in mbed Classic. FYI we have started doing this in mbed OS on another device (F0, but not public for the moment). I'll check if there is the same problem.

@campag
Copy link
Author

campag commented Apr 26, 2016

Hi. I have seen such ADC-internal extensions to mbed(os?) for the F4 over at https://github.com/ARMmbed which might benefit from clearing that bit too.

@ciarmcom
Copy link
Member

ciarmcom commented Aug 1, 2016

ARM Internal Ref: IOTMORF-199

@sg- sg- removed the mirrored label Aug 12, 2016
@bcostm
Copy link
Contributor

bcostm commented Nov 18, 2016

A patch is on-going in mbed SDK (not ST HAL driver).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants