1. 程式人生 > >STM32中的最後面幾句話的意思

STM32中的最後面幾句話的意思

/* 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 的註釋,那麼就全速運行了,不再進行任何檢查。

在這裡說一下,這個是其他網友的 部落格,我直接拿過來了,如果有侵權,立馬聯絡我,我只是想儲存這個帖子,用來提醒自己。