STM32固件库 assert_param函数
来源:互联网 发布:珠海华发集团待遇知乎 编辑:程序博客网 时间:2024/06/06 06:31
本文同步至个人博客:cyang.tech
一、知识点
-1、固件函数库通过检查库函书的输入来实现运行时间错误侦测。通过使用宏assert_param来实现运行时间检测。所有要求输入参数的函数都使用这个宏。它可以检查输入参数是否在允许的范围之内。
例如通过定义
#define IS_ADC_ALL_PERIPH(PERIPH) (((PERIPH) == ADC1) || \ ((PERIPH) == ADC2) || \ ((PERIPH) == ADC3))
这样在每次调用带输入参数的函数时,都会对所输入的参数进行检查,例如
void ADC_ClearFlag(ADC_TypeDef* ADCx, uint8_t ADC_FLAG){ /* Check the parameters */ assert_param(IS_ADC_ALL_PERIPH(ADCx)); assert_param(IS_ADC_CLEAR_FLAG(ADC_FLAG)); /* Clear the selected ADC flags */ ADCx->SR = ~(uint32_t)ADC_FLAG;}
实际上关于宏assert_param
的定义如下:
/* 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
的情况下,那么assert_param
的宏定义就是一个条件运算符,类似于a=(b>c)?b:c
,即如果b>c,则a=b;否则a=c。如果在没定义USE_FULL_ASSERT
的情况下,就是一个空函数。
就是说如果输入正确,则IS_ADC_ALL_PERIPH(ADCx)
值为1,则assert_param(IS_ADC_ALL_PERIPH(ADCx))
运行(void)0,否则执行后一个函数assert_failed((uint8_t *)__FILE__, __LINE__)
。
关于assert_failed((uint8_t *)__FILE__, __LINE__)
,这个函数是用来打印错误信息的,用户可以自己编程,用于打印出程序错误的地方,这个函数是在main.c里面的,函数原型如下:
#ifdef USE_FULL_ASSERT/** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */void assert_failed(uint8_t* file, uint32_t 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
二、说明
- 1、在上述的代码中,出现了
\
符号,这个符号的作用是当程序不在一行时,连接程序几部分。如下:
#define IS_ADC_ALL_PERIPH(PERIPH) (((PERIPH) == ADC1) || \ ((PERIPH) == ADC2) || \ ((PERIPH) == ADC3))
表示PERIPH
可以为ADC1
或ADC2
或ADC3
.
0 0
- STM32固件库 assert_param函数
- STM32 中的 assert_param 函数
- STM32函数assert_param
- STM32 中的 assert_param 函数
- STM32 中的 assert_param 函数
- STM32 函数assert_param
- stm32之assert_param 函数
- STM32 中的 assert_param 函数
- stm32固件库assert_param()
- STM32中的assert_param函数浅析
- 单片机之STM32 中的 assert_param 函数
- STM32的断言机制——函数assert_param()
- 关于STM32中的assert_param()
- STM32中assert_param的使用
- STM32中assert_param的使用
- STM32中assert_param的使用
- STM32中assert_param的使用
- STM32中assert_param的使用
- 测试公式
- 测试公式
- 初识人工神经网络(ANN)
- 数据结构-二叉树
- shell基础
- STM32固件库 assert_param函数
- HTML基础总结
- open edx创建超级用户
- 通过域名直接访问项目
- 大家好 ,我是一名大三的学生!
- 本地保存数据Preferences
- Linux命令初学者必知
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree
- 【经典面试题】JavaScript数组去重