您当前的位置: 首页 >  stm32

仙剑情缘

暂无认证

  • 3浏览

    0关注

    333博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

STM32F103VE RTT打印及错误代码打印实现

仙剑情缘 发布时间:2019-11-05 23:42:44 ,浏览量:3

  • RTT LOG功能实现
首先要将rtt文件加入工程,如下 新建一个sdk_config.h加入工程,并加入以下内容


#ifndef SDK_CONFIG_H
#define SDK_CONFIG_H

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

// >\n



//##############################################################################################################################################
//																			以下是nRF_Segger_RTT仿真器打印配置
//##############################################################################################################################################
//  nRF_Segger_RTT仿真器打印配置菜单 

//==========================================================
//  segger_rtt - SEGGER RTT

//==========================================================
//  SEGGER_RTT_CONFIG_BUFFER_SIZE_UP - Size of upstream buffer. 
//  Note that either @ref NRF_LOG_BACKEND_RTT_OUTPUT_BUFFER_SIZE
//  or this value is actually used. It depends on which one is bigger.

#ifndef SEGGER_RTT_CONFIG_BUFFER_SIZE_UP
#define SEGGER_RTT_CONFIG_BUFFER_SIZE_UP 512
#endif

//  SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS - Maximum number of upstream buffers. 
#ifndef SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS
#define SEGGER_RTT_CONFIG_MAX_NUM_UP_BUFFERS 2
#endif

//  SEGGER_RTT_CONFIG_BUFFER_SIZE_DOWN - Size of downstream buffer. 
#ifndef SEGGER_RTT_CONFIG_BUFFER_SIZE_DOWN
#define SEGGER_RTT_CONFIG_BUFFER_SIZE_DOWN 16
#endif

//  SEGGER_RTT_CONFIG_MAX_NUM_DOWN_BUFFERS - Maximum number of downstream buffers. 
#ifndef SEGGER_RTT_CONFIG_MAX_NUM_DOWN_BUFFERS
#define SEGGER_RTT_CONFIG_MAX_NUM_DOWN_BUFFERS 2
#endif

//  SEGGER_RTT_CONFIG_DEFAULT_MODE  - RTT behavior if the buffer is full.
 

//  The following modes are supported:
//  - SKIP  - Do not block, output nothing.
//  - TRIM  - Do not block, output as much as fits.
//  - BLOCK - Wait until there is space in the buffer.
//  SKIP 
//  TRIM 
//  BLOCK_IF_FIFO_FULL 

#ifndef SEGGER_RTT_CONFIG_DEFAULT_MODE
#define SEGGER_RTT_CONFIG_DEFAULT_MODE 0
#endif

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

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


//  DEBUG配置
//  DEBUG使能
#ifndef DEBUG_ENABLED
#define DEBUG_ENABLED 1
#endif


//  ASSERT参数检查使能
#ifndef USE_FULL_ASSERT
#define USE_FULL_ASSERT 	1
#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 NRF_LOG_DEBUG(fmt,args...) \
      do					\
			{						\
				sprintf(_SEGGER_RTT.aUp->pBuffer,fmt,##args); \
				SEGGER_RTT_WriteString(0, _SEGGER_RTT.aUp->pBuffer);	\
			}while(0)			
      
#define RTT_INIT()  \
SEGGER_RTT_Init()			
			
#else
#define NRF_LOG_INFO(fmt,args...) ((void)0)
#define NRF_LOG_DEBUG(fmt,args...) ((void)0)     
			
#define RTT_INIT()  ((void)0)
	
#endif


		
#ifdef __cplusplus
}
#endif
		
#endif
			
在主函数中调用RTT_INIT()宏进行初时化RTT打印,调用NRF_LOG_INFO宏就可以打印信息

/**
  * @brief  主函数,程序的入口
  *        
  *         
  * @param  None
  * @retval int:不用理会,对于嵌入式系统,永远都不会返回
  */
int main(void)
{
	//------------初时化RTT打印--------------------------	
	RTT_INIT();
	
	NRF_LOG_INFO("program is running!");
	//--------独立看门狗配置-----------------------------------
	IWDG_Configuration();  /*!>= 16; // right shift to lsb

    if((CFSRValue & (1HFSR);

		if ((SCB->HFSR & (1 CFSR );

		if((SCB->CFSR & 0xFFFF0000) != 0) {
			printUsageErrorMsg(SCB->CFSR);
		}
		if((SCB->CFSR & 0xFF00) != 0) {
			printBusFaultErrorMsg(SCB->CFSR);
		}
		if((SCB->CFSR & 0xFF) != 0) {
			printMemoryManagementErrorMsg(SCB->CFSR);
    }
}
		stackDump(stack);
		__ASM volatile("BKPT #01");
		//}
	
}


__ASM void HardFault_Handler_a(void)
{
   IMPORT Hard_Fault_Handler

   TST lr, #4
   ITE EQ
   MRSEQ r0, MSP
   MRSNE r0, PSP
   B Hard_Fault_Handler
}
#endif


在stm32f10x_it.c中的HardFault_Handler中断函数加入以下内容
/**
  * @brief  This function handles Hard Fault exception.
  * @param  None
  * @retval None
  */
void HardFault_Handler(void)
{
  /* Go to infinite loop when Hard Fault exception occurs */
 #if DEBUG_ENABLED
	HardFault_Handler_a();
	
#else
   NVIC_SystemReset();
#endif
  
}
在主函数中测试一下,现使用一个空指针操作来演示出错打印

DEMO下载地址:

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

 

                                   

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

关注
打赏
1658017818
查看更多评论
立即登录/注册

微信扫码登录

0.0387s