assert_param函数的用法

在STM32的固件库和提供的例程中,到处都可以见到assert_param()的使用。如果打开任何一个例程中的stm32f10x_conf.h文件,就可以看到实际上assert_param是一个宏定义;

在固件库中,它的作用就是检测传递给函数的参数是否是有效的参数。

举例说明:

  assert_param(IS_USART_ALL_PERIPH(USARTx));  

 这句代码用于检查参数USARTx是否有效,其中IS_USART_ALL_PERIPH(USARTx)是一个宏定义,如下:

#define IS_USART_ALL_PERIPH(PERIPH) (((PERIPH) == USART1) || \
                                     ((PERIPH) == USART2) || \
                                     ((PERIPH) == USART3) || \
                                     ((PERIPH) == USART4) || \
                                     ((PERIPH) == USART5) || \
                                     ((PERIPH) == USART6) || \
                                     ((PERIPH) == USART7) || \
                                     ((PERIPH) == USART8))

宏定义的功能是参数USARTx是USART1~USART8其中的一个,表示参数USARTx有效,返回true,否则返回false。

当然这个库函数也用的很有意思,看:首先对PERIPH进行取址,也就是求地址,&PERIPH,然后对这个地址强制转化为32位的指针,即前面加(uint32_t *),然后通过*进行访问这个地址(指针)中的内容。

assert_param()也是一个宏,定义在stm32f0xx_conf.h中,具体如下:

/* #define USE_FULL_ASSERT    1 */

/* 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)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宏定义了,则执行下面的代码:

#define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__))  //表示参数expr为false,则执行后面的assert_failed()函数,__FILE__, __LINE__是标准库函数中的宏定义,表示文件名和行号
 void assert_failed(uint8_t* file, uint32_t line);  //申明该函数

如果USE_FULL_ASSERT没有宏定义,则执行((void)0),即什么都不做。

assert_failed()函数的具体实现在main.c中(在任何一个例程中的main.c中都有这个函数的模板),内容如下:

 1 #ifdef  USE_FULL_ASSERT
 2 
 3 /**
 4   * @brief  Reports the name of the source file and the source line number
 5   *         where the assert_param error has occurred.
 6   * @param  file: pointer to the source file name
 7   * @param  line: assert_param error line source number
 8   * @retval None
 9   */
10 void assert_failed(uint8_t* file, uint32_t line)
11 { 
12   /* User can add his own implementation to report the file name and line number,
13      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
14 
15   /* Infinite loop */
16   while (1)
17   {
18   }
19 }
20 #endif

assert_failed()函数给了一个框架,内部具体实现需要开发者自己去写,通常是用串口把文件名和行号打印出来。

 

要使assert_failed()函数生效,需要宏定义USE_FULL_ASSERT,并且包含头文件stm32f0xx_conf.h;在stm32f0xx.h文件中,有如下代码:

#ifdef USE_STDPERIPH_DRIVER
  #include "stm32f0xx_conf.h"
#endif

只要宏定义USE_STDPERIPH_DRIVER,就能包含头文件stm32f0xx_conf.h了。

宏定义可以在文件中写,也可以直接在软件中设置,如下图:

注意:assert_failed()函数一般在代码调试时使用,可以帮助开发者检查输入参数无效的错误,但由于assert_failed()函数会影响代码执行效率,在程序release时,需要屏蔽掉,将宏定义USE_FULL_ASSERT注释即可。

参考:https://www.cnblogs.com/leo0621/p/9435794.html

断言机制(assert)

  • 何为断言

           我们通常会做出一些假设,断言就是在代码中捕捉这些假设。可以将断言看作是异常处理的一种高级形式。

  • 断言表达形式 断言表达形式分为两种
    1)assert 条件
    2)assert 条件:表达式
    结果为false,则抛出AssertionError异常,终止程序执行
  • 什么情况下使用断言

    使用断言之前,请记住下面几点
    1)断言失败是致命的,不可恢复的错误。(使程序直接崩溃)
    2)断言检查只用于开发和测试阶段。

    现在可用于测试的工具有JUnit,JUnit比使用断言有更多的好处,因此不建议使用断言。特别是如果在程序中误用断言,会带来很大的麻烦(比如将断言用于逻辑判断)。
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值