STM32 assert_param Macro Usage Guide

In STM32, assert_param is a macro used to check if the input parameters of a function meet the expected conditions. The definition of this macro can be found in the stm32fxxx.h file in the CMSIS library.

The definition of the assert_param macro is as follows:

#define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__))

The assert_param macro takes an expression as a parameter. If the result of the expression is non-zero, it is considered valid and no action is taken. If the result is zero, it is considered invalid, and the assert_failed function is called for error handling.

The definition of the assert_failed function is as follows:

void assert_failed(uint8_t* file, uint32_t line)
{
  /* 用户可以在这里添加自定义错误处理代码 */

  /* 死循环,停止程序执行 */
  while (1)
  {
  }
}

In this function, users can add their own custom error handling code. By default, the function simply goes into an infinite loop, halting the program’s execution.

The usage of the assert_param macro is as follows:

void foo(uint32_t value)
{
  /* 检查输入参数是否合法 */
  assert_param(value <= 100);

  /* 函数的其他操作 */
}

In the previous example, assert_param(value <= 100) is used to check if the input parameter value is less than or equal to 100. If the value of value exceeds 100, the assert_param macro will trigger an error handling mechanism.

In conclusion, the assert_param macro is used in STM32 to check the validity of function input parameters, helping developers to identify and debug issues early on.

bannerAds