ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

STM32中HAL库函数的断言函数assert_param()用法解析

2021-05-16 18:32:19  阅读:270  来源: 互联网

标签:__ HAL failed expr param assert INSTANCE 库函数


assert_param()的作用

assert_param是一个宏定义,在程序开发中,它的作用就是检测传递给函数的参数是否是有效的参数。默认是不开启的,可以无视它的存在,但在调试程序时,可以打开这个检测机制,调试完再关闭。

assert_param()的函数原型

从库函数源码可以看出,assert_param()的函数功能默认是不开启的,取消 #define USE_FULL_ASSERT 1U 的注释,可以开启该功能。

/* ########################## Assert Selection ############################## */
/**
  * @brief Uncomment the line below to expanse the "assert_param" macro in the 
  *        HAL drivers code
  */
// #define USE_FULL_ASSERT    1U 
/* Exported macro ------------------------------------------------------------*/
#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)0U : assert_failed((uint8_t *)__FILE__, __LINE__))
/* Exported functions ------------------------------------------------------- */
  void assert_failed(uint8_t* file, uint32_t line);
#else
  #define assert_param(expr) ((void)0U)
#endif /* USE_FULL_ASSERT */   

assert_param(expr)断言函数解析

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

该宏定义基于条件运算符,功能为:如果expr为真,则执行(void)0U,如果expr为假,则执行asser_failed()。
其中“ expr ”为判断参数是否合法的表达式,也可以用户自己定义,以下示例为判断串口句柄是否合法。
其中__FILE__ , __LINE__为编译器内置宏。

assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
/******************** UART Instances : Half-Duplex mode **********************/
#define IS_UART_HALFDUPLEX_INSTANCE(INSTANCE) (((INSTANCE) == USART1) || \
                                               ((INSTANCE) == USART2) || \
                                               ((INSTANCE) == USART3) || \
                                               ((INSTANCE) == UART4)  || \
                                               ((INSTANCE) == UART5)  || \
                                               ((INSTANCE) == USART6))

/* Legacy defines */
#define IS_UART_INSTANCE          IS_UART_HALFDUPLEX_INSTANCE

用户可自己定义断言失败函数asser_failed()的功能,如下所示。

#ifdef USE_FULL_ASSERT

/**
  * 函数功能: 断言失败服务函数
  * 输入参数: file : 源代码文件名称。关键字__FILE__表示源代码文件名。
  *           line :代码行号。关键字 __LINE__ 表示源代码行号
  * 返 回 值: 无
  * 说    明: 无
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
	/* 
	 *	用户可以添加自己的代码报告源代码文件名和代码行号,比如将错误文件和行号打印到串口
	 *	printf("Wrong parameters value: file %s on line %d\r\n", file, line)
	 */
  
	/* 这是一个死循环,断言失败时程序会在此处死机,以便于用户查错 */
	while (1)
	{
	}
}
#endif

拓展阅读

assert()中的(void(0))浅析
编译器内置宏

标签:__,HAL,failed,expr,param,assert,INSTANCE,库函数
来源: https://blog.csdn.net/weixin_41163322/article/details/116896911

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有