AnsweredAssumed Answered

CubeMX ADC Init bug

Question asked by Lukasz Przenioslo on Nov 29, 2017
Latest reply on Jan 10, 2018 by Sirma Siang

Hello there,

Here is my CubeMX version:

 

 

The libs versions:

 

So I am using STM32L4. The bug is within ADC initialization procedure created by CubeMX. This is how to trace the bug:

Locate this function:

HAL_StatusTypeDef HAL_ADC_ConfigChannel(ADC_HandleTypeDef* hadc, ADC_ChannelConfTypeDef* sConfig)

In the file stm32l4xx_hal_adc.c. In line 2201 there is an assert:

assert_param(IS_ADC_REGULAR_RANK(sConfig->Rank));

The condition for it is wrong, If we go to the IS_ADC_REGULAR_RANK code we get:

/**
  * @brief Verify the ADC regular channel setting.
  * @param  __CHANNEL__ programmed ADC regular channel.
  * @retval SET (__CHANNEL__ is valid) or RESET (__CHANNEL__ is invalid)
  */
#define IS_ADC_REGULAR_RANK(__CHANNEL__) (((__CHANNEL__) == ADC_REGULAR_RANK_1 ) || \
                                          ((__CHANNEL__) == ADC_REGULAR_RANK_2 ) || \
                                          ((__CHANNEL__) == ADC_REGULAR_RANK_3 ) || \
                                          ((__CHANNEL__) == ADC_REGULAR_RANK_4 ) || \
                                          ((__CHANNEL__) == ADC_REGULAR_RANK_5 ) || \
                                          ((__CHANNEL__) == ADC_REGULAR_RANK_6 ) || \
                                          ((__CHANNEL__) == ADC_REGULAR_RANK_7 ) || \
                                          ((__CHANNEL__) == ADC_REGULAR_RANK_8 ) || \
                                          ((__CHANNEL__) == ADC_REGULAR_RANK_9 ) || \
                                          ((__CHANNEL__) == ADC_REGULAR_RANK_10) || \
                                          ((__CHANNEL__) == ADC_REGULAR_RANK_11) || \
                                          ((__CHANNEL__) == ADC_REGULAR_RANK_12) || \
                                          ((__CHANNEL__) == ADC_REGULAR_RANK_13) || \
                                          ((__CHANNEL__) == ADC_REGULAR_RANK_14) || \
                                          ((__CHANNEL__) == ADC_REGULAR_RANK_15) || \
                                          ((__CHANNEL__) == ADC_REGULAR_RANK_16)   )

Then we check what is under the ADC_REGULAR_RANK_1:

#define ADC_REGULAR_RANK_1                 (LL_ADC_REG_RANK_1)  /*!< ADC group regular sequencer rank 1 */

Then going further what is under LL_ADC_REG_RANK_1:

#define LL_ADC_REG_RANK_1                  (ADC_SQR1_REGOFFSET | ADC_REG_RANK_1_SQRX_BITOFFSET_POS)  /*!< ADC group regular sequencer rank 1 */

ADC_SQR1_REGOFFSET is 0, ADC_REG_RANK_1_SQRX_BITOFFSET_POS is 6, ADC_REG_RANK_2_SQRX_BITOFFSET_POS is 12 and so on.

 

The parameter generated by CubeMX for Rank 1 (sConfig->Rank) is 1, thus it cannot past an assert because 1 != 6, 2 != 12 and so on.

Could someone please confirm this bug?

Outcomes