/* Uncomment the line below to expanse the "assert_param" macro in the Standard Peripheral Library drivers code */
/* #define USE_FULL_ASSERT 1 */
#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 */
1、assert_param()宏函数不是仅仅在编译期间检查参数的,而是在任何使用它的地方,任何时刻检查语句的正确性,即在运行时检查的这样的话同时也实现了编译期检查的功能,只要程序员传入了错误的参数,就会立即停止(实际上是进入死循环),通常用来进行参数检查。
2、由1可知,它是生成代码的。
3、如果语句正确,那么不执行任何动作,如果错误,那个调用assert_failed()函数,这个是真正意义上的函数。定义如下:
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)
{
}
}
4、程序调试好后,取消 #define USE_FULL_ASSERT 1 的注释,那么就全速运行了,不再进行任何检查。