stm8 print


原文链接: stm8 print

然后就是重写fputc函数,使之定向到指定的串口中去。代码如下:

/*
** Rewrite fputc function and make printf function work
*/
int fputc(int ch, FILE
file)
{

USART1->TDR = ch & 0xFF;

while((USART1->ISR & UART_FLAG_TC) == RESET);

return ch;

}

在该例中使用的USART1,这个看自己的实际需要改动。
做完这些,就可以直接使用printf函数了,不过个人建议最好重写printf函数,我的重写如下:

#include <stdio.h>
#include <stdarg.h>

/* Log enable/disable switch */
#define ENABLE_LOG 1

#if ENABLE_LOG
    #define Log(format, ...)    printf(format, ##__VA_ARGS__)
#else
    #define Log(format, ...)
#endif

同理可以重写fgetc函数,使scanf函数起作用,代码如下:

/*
** Rewrite fgetc function and make scanf function work
**/
int fgetc(FILE* file)
{
    while((USART1->ISR & UART_IT_RXNE) == RESET);

    return USART1->RDR;
}
/*
File  : log.c
Breif : Define a macro to optition print log
Date  : 2016-12-16
Author: Andy
*/

#include <stdio.h>

/* Define Log print macro */
#define MyLog(DebugLevel, format, ...)   \
        do{  \
            switch (DebugLevel)  \
            {  \
            case 1:  \
                printf(format, ##__VA_ARGS__);  \
                break;  \
            case 2: \
                printf("Function: "__FUNCTION__", Line: %d, ---> "format"", __LINE__, ##__VA_ARGS__); \
                break;  \
            case 3:  \
                printf("File: "__FILE__", Function: "__FUNCTION__", Line: %d, ---> "format"", __LINE__, ##__VA_ARGS__); \
                break; \
            default:   \
                break;  \
            }   \
        }while(0)

int main(void)
{
    MyLog(1, "Simple Log print!\r\n");
    MyLog(2, "Satndard Log display!\r\n");
    MyLog(3, "Detail Log view!\r\n");

    MyLog(1, "If debug level is not equal 1,2 or 3 that log is invisible, such as next line :\r\n");
    MyLog(6, "I am invisible log!\r\n");
    MyLog(1, "Now, I think you have understood how to use MyLog macro.\r\n");

    return 0;
}



/** color log macro define */
#define NONE          "\033[m"
#define RED           "\033[0;32;31m"
#define GREEN         "\033[0;32;32m"
#define BLUE          "\033[0;32;34m"
#define YELLOW        "\033[1;33m"

/** product log level define */
#define HI_LOG_LEVEL_FATAL      (0)
#define HI_LOG_LEVEL_ERROR      (1)
#define HI_LOG_LEVEL_WARNING    (2)
#define HI_LOG_LEVEL_DEBUG      (3)

/** product log level */
#ifdef CFG_DEBUG_LOG_ON
#define HI_LOG_LEVEL  HI_LOG_LEVEL_DEBUG
#else
#define HI_LOG_LEVEL  HI_LOG_LEVEL_ERROR
#endif

#ifndef LOG_LEVEL_DEFAULT
#define LOG_LEVEL_DEFAULT LOG_LEVEL_INFO
#endif


#if LOG_LEVEL_FATAL <= LOG_LEVEL
#define _LEVEL_FILTER_FATAL(a) a
#else
#define _LEVEL_FILTER_FATAL(...)
#endif

#if LOG_LEVEL_ERROR <= LOG_LEVEL
#define _LEVEL_FILTER_ERROR(a) a
#else
#define _LEVEL_FILTER_ERROR(...)
#endif

#if LOG_LEVELl_WARN <= LOG_LEVEL
#define _LEVEL_FILTER_WARN(a) a
#else
#define _LEVEL_FILTER_WARN(...)
#endif

#if LOG_LEVEL_INFO <= LOG_LEVEL
#define _LEVEL_FILTER_INFO(a) a
#else
#define _LEVEL_FILTER_INFO(...)
#endif

#if LOG_LEVEL_DEBUG <= LOG_LEVEL
#define _LEVEL_FILTER_DEBUG(a) a
#else
#define _LEVEL_FILTER_DEBUG(...)
#endif

#if LOG_LEVEL_PACKET <= LOG_LEVEL
#define _LEVEL_FILTER_PACKET(a) a
#else
#define _LEVEL_FILTER_PACKET(...)
#endif

#if LOG_LEVEL_DUMP <= LOG_LEVEL
#define _LEVEL_FILTER_DUMP(a) a
#else
#define _LEVEL_FILTER_DUMP(...)
#endif

#if LOG_LEVEL_TRACE <= LOG_LEVEL
#define _LEVEL_FILTER_TRACE(a) a
#else
#define _LEVEL_FILTER_TRACE(...)
#endif



typedef enum LEVEL {
	DEBUG	 = 0x0,    /* debug-level								   */
	INFO	 = 0x1,    /* informational 							   */
	NOTICE  = 0x2,    /* normal but significant condition			   */
	WARNING = 0x3,    /* warning conditions						   */
	ERROR	 = 0x4,    /* error conditions							   */
	CRIT	 = 0x5,    /* critical conditions						   */
	ALERT	 = 0x6,    /* action must be taken immediately			   */
	FATAL	= 0x7,	   /* just for compatibility with previous version */

	BUTT
}LEVEL_E;


#define PRINTF(LevelStr,Msg, ...) do { fprintf(stdout,"[Level]:%s,[Func]:%s [Line]:%d [Info]:"Msg,LevelStr, __FUNCTION__, __LINE__,## __VA_ARGS__); } while (0)
#define PRINTF_RED(LevelStr,Msg, ...) do { fprintf(stderr,"\033[0;31m [Level]:%s,[Func]:%s [Line]:%d [Info]:"Msg"\033[0;39m\n",LevelStr, __FUNCTION__, __LINE__,## __VA_ARGS__); } while (0)
/* system is unusable	*/
#define TRACE_FATAL(Msg,...)   PRINTF_RED("Fatal",Msg,##__VA_ARGS__)
/* action must be taken immediately */
#define TRACE_ALERT(Msg,...)   PRINTF_RED("Alert",Msg,##__VA_ARGS__)
/* critical conditions */
#define TRACE_CRIT(Msg,...)    PRINTF_RED("Critical",Msg,##__VA_ARGS__)
/* error conditions */
#define TRACE_ERR(Msg,...)     PRINTF_RED("Error",Msg,##__VA_ARGS__)
/* warning conditions */
#define TRACE_WARN(Msg,...)    PRINTF("Warning",Msg,##__VA_ARGS__)
/* normal but significant condition  */
#define TRACE_NOTICE(Msg,...)  PRINTF("Notice",Msg,##__VA_ARGS__)
/* informational */
#define TRACE_INFO(Msg,...)    PRINTF("Info",Msg,##__VA_ARGS__)
/* debug-level messages  */
#define TRACE_DEBUG(Msg, ...)  PRINTF("Debug",Msg,##__VA_ARGS__)


#define log(Level,Msg, ...)\
do\
{\
	switch(Level){\
		case DEBUG:\
		    TRACE_DEBUG(Msg,##__VA_ARGS__);\
			break;\
		case INFO:\
		    TRACE_INFO(Msg,##__VA_ARGS__);\
			break;\
		case NOTICE:\
		    TRACE_NOTICE(Msg,##__VA_ARGS__);\
			break;\
		case WARNING:\
		    TRACE_WARN(Msg,##__VA_ARGS__);\
			break;\
		case ERROR:\
			TRACE_ERR(Msg,##__VA_ARGS__);\
			break;\
		case CRIT:\
			TRACE_CRIT(Msg,##__VA_ARGS__);\
			break;\
		case ALERT:\
			TRACE_ALERT(Msg,##__VA_ARGS__);\
			break;\
		case FATAL:\
			TRACE_FATAL(Msg,##__VA_ARGS__);\
			break;\
		default:\
			break;\
		}\
}while(0)

————————————————
版权声明:本文为CSDN博主「Andy001847」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Andy001847/article/details/77914168

`