cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_ADC_Init assert issue for oversampling right bit shift value on STM32H7

Simon Sq
Associate III

I am currently seeing an assert_param failure within the HAL_ADC_Init() method in stm32h7xx_hal_adc.c file.

Specifically the following assertion is failing:

assert_param(IS_ADC_RIGHT_BIT_SHIFT(hadc->Init.Oversampling.RightBitShift));

The value being passed into the assertion is 0x140 which is being initialised in MX_ADC1_Init() and MX_ADC3_Init() as follows:

hadc1.Init.Oversampling.RightBitShift = ADC_RIGHTBITSHIFT_10;
hadc3.Init.Oversampling.RightBitShift = ADC_RIGHTBITSHIFT_10;

I believe the actual issue is the macro in the file stm32h7xx_hal_adc_ex.h:

#define IS_ADC_RIGHT_BIT_SHIFT(__SHIFT__)        (((__SHIFT__) == ADC_RIGHTBITSHIFT_NONE) || \
                                                  ((__SHIFT__) == ADC_RIGHTBITSHIFT_1   ) || \
                                                  ((__SHIFT__) == ADC_RIGHTBITSHIFT_2   ) || \
                                                  ((__SHIFT__) == ADC_RIGHTBITSHIFT_3   ) || \
                                                  ((__SHIFT__) == ADC_RIGHTBITSHIFT_4   ) || \
                                                  ((__SHIFT__) == ADC_RIGHTBITSHIFT_5   ) || \
                                                  ((__SHIFT__) == ADC_RIGHTBITSHIFT_6   ) || \
                                                  ((__SHIFT__) == ADC_RIGHTBITSHIFT_7   ) || \
                                                  ((__SHIFT__) == ADC_RIGHTBITSHIFT_8   ))

which only checks values up to ADC_RIGHTBITSHIFT_8 and does not include the possible values of ADC_RIGHTBITSHIFT_9, ADC_RIGHTBITSHIFT_10 and ADC_RIGHTBITSHIFT_11 which are possible on the STM32H7.

I propose update the macro to include the missing potential values.

1 ACCEPTED SOLUTION

Accepted Solutions
Simon Sq
Associate III

@Houssem CHAABANI​  @Imen DAHMEN​  I believe there is an issue with stm32h7xx_hal_adc_ex.h file in that the IS_ADC_RIGHT_BIT_SHIFT() macro does not include the possible values for ADC_RIGHTBITSHIFT_9, ADC_RIGHTBITSHIFT_10, and ADC_RIGHTBITSHIFT_11 - it only goes up to ADC_RIGHTBITSHIFT_8.

In my opinion the IS_ADC_RIGHT_BIT_SHIFT() macro in stm32h7xx_hal_adc_ex.h needs to be changed to include the missing values.

View solution in original post

9 REPLIES 9
Houssem CHAABANI
Senior II

Dear @Simon Sq​ 

This will be internally check from CubeMX side, I'll give you an update as soon as possible.

According to Stm32h7 ADC page 6 under this link the right shift is from one to eight binary digits.

So in your case you need to fix this :

hadc1.Init.Oversampling.RightBitShift = ADC_RIGHTBITSHIFT_8;

hadc3.Init.Oversampling.RightBitShift = ADC_RIGHTBITSHIFT_8;

Hope that this fix your problem.

Kind regards

Houssem

Simon Sq
Associate III

@[Houssem CHAABANI]​ 

Thanks for you're reply. If that is the case then CubeMX is setting the value incorrectly.

If have set the oversampling ratio for both ADCs to 1024 in CubeMx. The oversampler right bit shift is also set to 10 bits in CubeMX, which results in the oversampler value being set to ADC_RIGHTBITSHIFT_10 in the initialisation code. This fails the assertion. Below is an image showing my ADC setup.

0693W000005AQQUQA4.jpg

I'm not manually initialise the ADCs but just running the code generated from CubeMX. So it could be a problem with the CubeMX code generation? Or CubeMX is allowing you to set invalid options?

I don't think setting the value to ADC_RIGHTBITSHIFT_8 as you suggested would work as my adc samples would end up 4 times too big. Oversampling ratio would also need to be adjusted to 256?

Perhaps I'm misunderstanding the role of the ADC right bit shift and oversampling ratio. Logically I think that if I accumulate 1024 samples, I need to then divide by 1024 to get the averaged ADC value.

1024 = 2^10

Houssem CHAABANI
Senior II

Hello @Simon Sq​ 

Thank you for these details.

According to stm32h7xx_hal_adc.h the ADC_RIGHTBITSHIFT_10 is defined.

@Imen DAHMEN​  could you please check if there is a problem with the stm32h7xx_hal_adc_ex.h file ?

Best regards

Houssem

Simon Sq
Associate III

@Houssem CHAABANI​  @Imen DAHMEN​  I believe there is an issue with stm32h7xx_hal_adc_ex.h file in that the IS_ADC_RIGHT_BIT_SHIFT() macro does not include the possible values for ADC_RIGHTBITSHIFT_9, ADC_RIGHTBITSHIFT_10, and ADC_RIGHTBITSHIFT_11 - it only goes up to ADC_RIGHTBITSHIFT_8.

In my opinion the IS_ADC_RIGHT_BIT_SHIFT() macro in stm32h7xx_hal_adc_ex.h needs to be changed to include the missing values.

Hello @Simon Sq​ ,

Thank you for pointing me out this issue.

I will check this reported problem and I will come back to you with update and the actions taken.

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
Imen.D
ST Employee

Hello @Simon Sq​ ,

I confirm the issue and I have passed it along to our development team for fix.

Thanks for your contribution and for pointing out this issue.

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen

Hello Imen

i'm an electronic engineer and i'm working on LORA project using the i-cube-lrwan expansion package. An expansion package by definition must include .ioc file for STM32CubeMX but the LORA expansion package doesn't include this file!!! why?

i work in a customised board and i need to use CubeMX to initialise graphically my I/O of my board. how can i integrate this initialisation in the expansion package. i use the STM32L476 MCU and there's an example project with this MCU.

Thanks to explain how can i do the portage of my design in your package to make a functional software with lorawan protocole

Thanks

Hello @SHATE.1​ ,

Welcome to the STM32 Community :smiling_face_with_smiling_eyes:.

This thread is already closed and your question is not relevant to this topic.

I suggest you create a new discussion on which you ask your new question. 

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen

Hi @SHATE.1​ ,

I just saw your discussion :smiling_face_with_smiling_eyes:

I will answer you on your post.

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen