-
用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