STMicroelectronics / stm32h7xx_hal_driver

Provides the STM32Cube MCU Component "hal_driver" of the STM32H7 series.
BSD 3-Clause "New" or "Revised" License
86 stars 36 forks source link

Incorrect ADC calibration factor size #32

Closed wouter-palmsens closed 6 months ago

wouter-palmsens commented 1 year ago

The macro IS_ADC_CALFACT is currently defined (see here) as:

#define IS_ADC_CALFACT(__CALIBRATION_FACTOR__) ((__CALIBRATION_FACTOR__) <= (0x7FU))

This macro is used to check the parameters of HAL_ADCEx_Calibration_SetValue (see here):

  assert_param(IS_ADC_CALFACT(CalibrationFactor));

Consequently, when USE_FULL_ASSERT is enabled in the HAL, the assertion is violated when using a calibration factor with a value larger than 127.

However, the calibration factor is 11 bits, so I think the check should be <= 0x7FFU.

Patch

The corrected code should be:

/**
  * @brief Calibration factor size verification (11 bits maximum).
  * @param __CALIBRATION_FACTOR__ Calibration factor value.
  * @retval SET (__CALIBRATION_FACTOR__ is within the authorized size) or RESET (__CALIBRATION_FACTOR__ is too large)
  */
#define IS_ADC_CALFACT(__CALIBRATION_FACTOR__) ((__CALIBRATION_FACTOR__) <= 0x7FFU)

Note that the documentation of HAL_ADCEx_Calibration_SetValue also mentions the 7 bits maximum instead of 11 bits maximum:

  * @param CalibrationFactor Calibration factor (coded on 7 bits maximum)

should be:

  * @param CalibrationFactor Calibration factor (coded on 11 bits maximum)
RJMSTM commented 11 months ago

ST Internal Reference: 157901

TOUNSTM commented 6 months ago

Fixed in 6e22b5b