Skip to main content
Doug Kehn
Associate II
Jul 15, 2016 12:35 PM UTC
Solved

STM32L4 ADC Initialization Problem

Posted on July 15, 2016 at 14:35

Hi All,

I'm using STM32L475, STM32CubeMX 4.15.1, STM32L4 firmware library 1.5.1 and the code generated by STM32CubeMX. When initializing ADC1 and ADC2 the ADC2 initialization, MX_ADC2_Init(), sometimes calls ErrorHandler() because HAL_ADCEx_MultiModeConfigChannel() is returning and error.

HAL_ADCEx_MultiModeConfigChannel(), stm32l4xx_hal_adc_ex.c, is called with hadc->Instance = ADC2; therefore, ADC_MULTI_SLAVE(hadc, &tmphadcSlave) sets tmphadcSlave->Instance = NULL. When ADC_IS_CONVERSION_ONGOING_REGULAR(&tmphadcSlave) is called a NULL pointer, tmphadcSlave->Instance->CR, is dereferenced and leads to unexpected results.

The NULL pointer deference can be fixed by changing macro ADC_IS_CONVERSION_ONGOING_REGULAR, stm32l4xx_hal_adc.h, to

#define ADC_IS_CONVERSION_ONGOING_REGULAR(__HANDLE__)                                         \

       (( !(__HANDLE__)->Instance || (((__HANDLE__)->Instance->CR) & ADC_CR_ADSTART) == RESET \

        ) ? RESET : SET)

With this change ADC_IS_CONVERSION_ONGOING_REGULAR will return RESET when Instance is NULL.

HAL_ADCEx_MultiModeConfigChannel() also calls macro ADC_IS_ENABLE(&tmphadcSlave). ADC_IS_ENABLE should also check for NULL before using tmphadcSlave->Instance.

#define ADC_IS_ENABLE(__HANDLE__)                                                           \

       (( (__HANDLE__)->Instance                                                         && \

          ((((__HANDLE__)->Instance->CR) & (ADC_CR_ADEN | ADC_CR_ADDIS)) == ADC_CR_ADEN) && \

          ((((__HANDLE__)->Instance->ISR) & ADC_FLAG_RDY) == ADC_FLAG_RDY)                  \

        ) ? SET : RESET)

Is this analysis and resolution correct? I believe returning RESET is appropriate when Instance = NULL. Also, updating the macros seemed cleaner than adding NULL checks to the source; but this is debatable.

Thanks and Regards,

...doug
    This topic has been closed for replies.
    Jeanne Joly
    Jeanne JolyBest answer
    Associate
    Jan 13, 2017 2:51 PM UTC
    Posted on January 13, 2017 at 15:51

    Hi kehn.doug,

    This issue is solved in CubeMX4.18.

    Please, upgrade CubeMX to avoid having this issue.

    BR. Eric

    2 replies

    Amel NASRI
    ST Technical Moderator
    Jul 21, 2016 10:34 AM UTC
    Posted on July 21, 2016 at 12:34

    Hi kehn.doug,

    Thanks for highlighting these issues.

    Please note that they are reported internally for farther investigation.

    -Mayla-

    To give better visibility on the answered topics, please click on "Best Answer" on the reply which solved your issue or answered your question.
    Jeanne Joly
    Jeanne JolyBest answer
    Associate
    Jan 13, 2017 2:51 PM UTC
    Posted on January 13, 2017 at 15:51

    Hi kehn.doug,

    This issue is solved in CubeMX4.18.

    Please, upgrade CubeMX to avoid having this issue.

    BR. Eric