将printf()用作debug

app-debug.c文件内容如下,主要是增加了两个函数和重写fputc()函数。

vAppDebugLine();//带换行的输出
vAppDebugBuffer();//16进制输出

/************************************************************************
 *   Name: app-debug.c
 *
 *   Date: 2019-2-15
 *
 * Author: Msming
 * 
 * Description: 用于调试
 */
#include "em_chip.h"
#include "em_cmu.h"
#include "em_leuart.h"
#include "em_usart.h"
#include "stdio.h"
#include <stdarg.h>
#include "app-debug.h"
 
#define XDEBUG_LEUART   LEUART0
#define DEBUG_USART   USART1

#ifdef APP_DEBUG_ENABLE
#pragma import(__use_no_semihosting)             
//标准库需要的支持函数                 
struct __FILE 
{ 
	int handle; 
	/* Whatever you require here. If the only file you are using is */ 
	/* standard output using printf() for debugging, no file handling */ 
	/* is required. */ 
}; 
/* FILE is typedef’ d in stdio.h. */ 
FILE __stdout;       
//定义_sys_exit()以避免使用半主机模式    
void _sys_exit(int x) 
{ 
	x = x; 
} 
//重定义fputc函数 
int fputc(int ch, FILE *f)
{      
#if defined(DEBUG_LEUART) 
{  
	LEUART_Tx(DEBUG_LEUART, ch);
}
#elif defined(DEBUG_USART)
{ 
	USART_Tx(DEBUG_USART, ch);
}
#endif
	return ch;
}
#endif

/*********************************************************************
 * @fn          vAppDebugLine
 *
 * @brief       格式化输出,最后换行
 *
 * @return      
 */
void vAppDebugLine(const char *formatString, ...)
{
#ifdef APP_DEBUG_ENABLE
  va_list ap = {0};
	char buff[128] = {0};
  va_start(ap, formatString);
	vsprintf(buff, formatString, ap);
  printf(buff);
  va_end(ap);
  printf("\r\n");
  
#endif
}
/*********************************************************************
 * @fn          vAppDebugBuffer
 *
 * @brief       将buff以16进制输出,spaceChar:间隔符号
 *
 * @return      
 */
void vAppDebugBuffer(const char *buff, uint8_t length, const char *spaceChar)
{
#ifdef APP_DEBUG_ENABLE
  
	for(uint8_t i=0; i<length; i++)
	{
	  printf("%02X", *buff++);
		if(i<(length-1))
			printf(spaceChar);
	}
  
#endif
}

app-debug.h 内容如下,主要有:
#define vAppDebug(…)
#define vAppDebugFile(Args, …)//输出文件路径和行号。
void vAppDebugLine(const char *formatString, …);
void vAppDebugBuffer(const char buff, uint8_t length, const charspaceChar);

/************************************************************************
 *   Name: app-debug.h
 *
 *   Date: 2019-2-15
 *
 * Author: Msming
 * 
 * Description: 用于调试
 */

#ifndef __APP_DEBUG_H__
#define __APP_DEBUG_H__
 
#define APP_DEBUG_ENABLE

#ifndef __APP_DEBUG_H__
#define __APP_DEBUG_H__
 
#define APP_DEBUG_ENABLE

#ifdef APP_DEBUG_ENABLE
#define vAppDebug(...)  		  printf(__VA_ARGS__)//print string
#define vAppDebugFile(Args, ...)  printf("[%s:%d]"##Args, __FILE__, __LINE__, ##__VA_ARGS__)//print file path & line
#else
#define vAppDebug(...)
#define vAppDebugFile(Args, ...)
#endif

void vAppDebugLine(const char *formatString, ...);//print string with new line
void vAppDebugBuffer(const char *buff, uint8_t length, const char *spaceChar);//print hex string


#endif //__APP_DEBUG_H__

#endif //__APP_DEBUG_H__

如需要在其他文件调用app-debug中的函数可以作如下定义:


#define APP_OLED_DEBUG_ENABLE

#ifdef  APP_OLED_DEBUG_ENABLE
#define appOledPrint(...)          						vAppDebug(__VA_ARGS__)
#define appOledPrintFile(Args, ...)  				    vAppDebugFile(Args, __VA_ARGS__)
#define appOledPrintln(...)  					    	vAppDebugLine(__VA_ARGS__)
#define appOledPrintBuffer(buff, length, spaceChar) 	vAppDebugBuffer(buff, length, spaceChar)
#else
#define appOledPrint(...)  
#define appOledPrintFile(Args, ...)
#define appOledPrintln(...)
#define appOledPrintBuffer(buff, length, spaceChar) 
#endif

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值