关于ST库函数的assert_param

[复制链接]
8246|12
 楼主| banhushui 发表于 2011-6-4 21:12 | 显示全部楼层 |阅读模式
不少人问在ST官方的STM32的库函数里有很多assert_param是什么作用
比如下面的
assert_param(IS_ADC_ALL_PERIPH(ADCx));
assert_param(IS_ADC_IT(ADC_IT));
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));  
这样的函数,几乎是带参数的函数前面都有调用assert_param
实际这是个调试函数,当你在调试程序时打开DEBUG参数assert_param才起作用。
assert_param是反映参数你在调用库函数传递的参数是错误的。
assert_param的原型定义在stm32f10x_conf.h 文件里
定义如下:
/* Exported macro ------------------------------------------------------------*/
#ifdef  DEBUG
/*******************************************************************************
* Macro Name     : assert_param
* Description    : The assert_param macro is used for function's parameters check.
*                  It is used only if the library is compiled in DEBUG mode.
* Input          : - 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.
* Return         : None
*******************************************************************************/
  #define assert_param(expr) ((expr) ? (void)0 : assert_failed((u8 *)__FILE__, __LINE__))
/* Exported functions ------------------------------------------------------- */
  void assert_failed(u8* file, u32 line);
#else
  #define assert_param(expr) ((void)0)
#endif /* DEBUG */
#endif /* __STM32F10x_CONF_H */

可以看到assert_param实际在DEBUG打开时就是assert_failed,关闭DEBUG时是空函数

assert_failed函数如下
#ifdef  DEBUG
/*******************************************************************************
* Function Name  : assert_failed
* Description    : Reports the name of the source file and the source line number
*                  where the assert_param error has occurred.
* Input          : - file: pointer to the source file name
*                  - line: assert_param error line source number
* Output         : None
* Return         : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  //用户可以在这里添加错误信息:比如打印出出错的文件名和行号
  /* Infinite loop */
  while (1)
  {
  }
}
#endif
Chaos_zc 发表于 2011-8-6 01:38 | 显示全部楼层
解惑了,顶!
kavieen 发表于 2011-8-6 21:48 | 显示全部楼层
哇 终于找到关于assert_param的帖子了,谢谢楼主!!:loveliness:
rulu 发表于 2011-8-8 12:54 | 显示全部楼层
assert_param是什么作用
非常好的帖子
rulu 发表于 2011-8-8 12:54 | 显示全部楼层
多谢楼主
zhudongpo 发表于 2011-8-9 09:08 | 显示全部楼层
学习了,谢谢
lixiaoxu2meng 发表于 2011-8-9 09:15 | 显示全部楼层
很好 顶楼主
无冕之王 发表于 2011-8-9 09:46 | 显示全部楼层
楼主分析的很到位
buddy218 发表于 2015-5-4 09:12 | 显示全部楼层
好好好  好东西
liuchangyin 发表于 2015-5-4 11:04 | 显示全部楼层
泰山特曲123 发表于 2015-5-4 18:47 来自手机 | 显示全部楼层
asser是断言,请参考write clean code,这本书
决狐疑 发表于 2015-8-25 13:00 | 显示全部楼层
感谢楼主解惑
大道至简 发表于 2015-8-30 11:22 | 显示全部楼层
楼主写的,还是没有看明白
您需要登录后才可以回帖 登录 | 注册

本版积分规则

33

主题

157

帖子

14

粉丝
快速回复 在线客服 返回列表 返回顶部