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