- 用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)
// >\n
//==================================================================================================
//==========================================================
// DEBUG配置
// DEBUG使能
#ifndef DEBUG_ENABLED
#define DEBUG_ENABLED 1
#endif
// ASSERT参数检查使能
#ifndef IS_USE_FULL_ASSERT
#define IS_USE_FULL_ASSERT 1
#endif
#if IS_USE_FULL_ASSERT
#define USE_FULL_ASSERT
#endif
//
// >
#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