STM32F030R8Tx加入RTT打印

11 篇文章 2 订阅
  • 用STM32CubeMX工具生成STM32F030R8Tx MCU的Keil工程

  • 在工程中加入SEGGER_RTT.c和SEGGER_RTT_printf.c 

  • sdk_config.h主要是配置DEBUG开关宏,内容如下



#ifndef SDK_CONFIG_H
#define SDK_CONFIG_H

	
#define STM_MODULE_ENABLED(module) \
    ((defined(module ## _ENABLED) && (module ## _ENABLED)) ? 1 : 0)

// <<< Use Configuration Wizard in Context Menu >>>\n

//==================================================================================================





//==========================================================


// <h> DEBUG配置
// <q> DEBUG使能
#ifndef DEBUG_ENABLED
#define DEBUG_ENABLED 1
#endif


// <q> ASSERT参数检查使能
#ifndef IS_USE_FULL_ASSERT
#define IS_USE_FULL_ASSERT 	1
#endif
#if IS_USE_FULL_ASSERT
#define USE_FULL_ASSERT
#endif
// </h>

// <<< end of configuration section >>>
#endif //SDK_CONFIG_H
  • 准备一个stm_log.h文件,内容如下

#include "sdk_config.h"
#include "SEGGER_RTT.h"
#include "stdio.h"
#ifndef STM_LOG_H__
#define STM_LOG_H__


#ifdef __cplusplus
extern "C" {
#endif

#if STM_MODULE_ENABLED(DEBUG)




/**
* @brief RTT打印宏
* @note 
* - 最多不能超过128个字符否则出错
*/
#define NRF_LOG_INFO(fmt,args...) \
      do					\
			{						\
				sprintf(_SEGGER_RTT.aUp->pBuffer,fmt,##args); \
				SEGGER_RTT_WriteString(0, _SEGGER_RTT.aUp->pBuffer);	\
				SEGGER_RTT_WriteString(0,"\r\n");  \
			}while(0)

      
#define RTT_INIT()  \
SEGGER_RTT_Init()			
			
#else
#define NRF_LOG_INFO(fmt,args...) ((void)0)
     
#define RTT_INIT()  ((void)0)
	
#endif


		
#ifdef __cplusplus
}
#endif
		
#endif
			
  • 在main.c中入包含stm_log.h头文件,在main函数中初时化及调用打印,内容如下

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
	RTT_INIT();
	
  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
    HAL_Delay(1000);
    NRF_LOG_INFO("main is running!");
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}
  • 运行打印结果如下

  • 在main文件中的assert_failed函数中加入RTT打印

#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 CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
  NRF_LOG_INFO("%s,line %d",file,line);
  while(1);

}
#endif /* USE_FULL_ASSERT */
  •  在main函数中加入会出错的测试代码

  • 运行结果如下

  • 找到 ../Drivers/STM32F0xx_HAL_Driver/Src/stm32f0xx_hal_gpio.c文件中的第185行,引脚参数抛出了断言

 

例程下载链接

https://download.csdn.net/download/mygod2008ok/12522332 

 

 

 

 

 

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风雨依依

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值