嵌入式C语言基础之断言

断言一般会用做函数入口参数的有效性判断,在STM32的HAL库中就有很多应用,比如在STM32F429中断优先级分组配置函数中(此处只说断言应用,不对STM32F429的中断分组做过多讨论):

void HAL_NVIC_SetPriorityGrouping(uint32_t PriorityGroup)
{
  /* Check the parameters */
  assert_param(IS_NVIC_PRIORITY_GROUP(PriorityGroup));
  
  /* Set the PRIGROUP[10:8] bits according to the PriorityGroup parameter value */
  NVIC_SetPriorityGrouping(PriorityGroup);
}

这里第一条语句assert_param就是断言函数,它用来判断函数参数PriorityGroup是否合法,断言函数assert_param使用一个宏来实现的,通过MDK的“Go To Definition of ‘assert_param’”可以在stm32f4xx_hal_conf.h中找到其定义如下:

#ifdef  USE_FULL_ASSERT
/**
  * @brief  The assert_param macro is used for function's parameters check.
  * @param  expr: If expr is false, it calls assert_failed function
  *         which reports the name of the source file and the source
  *         line number of the call that failed. 
  *         If expr is true, it returns no value.
  * @retval None
  */
  #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__))
/* Exported functions ------------------------------------------------------- */
  void assert_failed(uint8_t* file, uint32_t line);
#else
  #define assert_param(expr) ((void)0)
#endif /* USE_FULL_ASSERT */  

宏USE_FULL_ASSERT是用来表示是否支持断言的开关,如果没有定义这个宏,那么断言函数assert_param无任何操作;如果开启了断言,那么它将通过三目操作符来进行判断,如果expr为真,不执行任何操作,如果expr为假,执行assert_failed函数。assert_failed函数在库中只有声明,没有定义,需要用户自己根据实际开发需要去实现其功能,该函数的两个参数分别是调用assert_failed函数(即参数不合法)的文件的文件名和行号,可以通过该函数打印参数不合法的文件及不合法的行号位置。
回到最初的HAL_NVIC_SetPriorityGrouping函数,这里assert_param函数的参数IS_NVIC_PRIORITY_GROUP也是一个宏,定义在stm32f4xx_hal_cortex.h中:

#define IS_NVIC_PRIORITY_GROUP(GROUP) (((GROUP) == NVIC_PRIORITYGROUP_0) || 
                                       ((GROUP) == NVIC_PRIORITYGROUP_1) || 
                                       ((GROUP) == NVIC_PRIORITYGROUP_2) || 
                                       ((GROUP) == NVIC_PRIORITYGROUP_3) || 
                                       ((GROUP) == NVIC_PRIORITYGROUP_4))

stm32f429的中断分组共0-4五组,如果函数参数PriorityGroup不是这五个之一,那么整个表达式为假,此时调用断言函数中的assert_failed函数来打印错误信息。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>