STM32调试时打开assert_failed

前言

在改一个工程,发现STM32F407工程,将6个串口全打开时,串口2发送数据时,等发送完成标志那死循环了,等不到发送完成的状态位(那个状态位是硬件设置的).

有点怀疑工程中调用库函数时,参数设置错了。
想打开STM32库函数中, 自带的参数检查机制。

试验

stm32f4xx_conf.h 有宏 USE_FULL_ASSERT,默认是关闭的

/* Uncomment the line below to expanse the "assert_param" macro in the 
   Standard Peripheral Library drivers code */
/* #define USE_FULL_ASSERT    1 */ // 默认是关闭的
#define USE_FULL_ASSERT    1 // 打开调试宏 USE_FULL_ASSERT, 使assert_failed生效

/* 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 */

#endif /* __STM32F4xx_CONF_H */

增加 assert_failed 实现

void assert_failed(uint8_t* file, uint32_t line)
{
	// 这里就为断点能停下,然后返回, 看看,哪里参数设置错了
	while (0) {
	}
}

用SWD调试时,用的4线制, 没接SWO. 看不到ITM输出.
只能在assert_failed()内下断点,如果有库函数参数设置的不对,就从assert_failed()返回,看调用处,是什么参数设置的不对。

发布了436 篇原创文章 · 获赞 126 · 访问量 175万+

猜你喜欢

转载自blog.csdn.net/LostSpeed/article/details/101597180
今日推荐