接上一篇,因爲當時項目急,可以用了也沒去研究,現在省出來點玩DOTA的時間記錄一下,就說那個串口輸出函數吧,各種宏,他感覺那樣好,其實更不好理解,我們可以嘗試把宏替代進去就形成了這麼一個函數。
#define TF_STACK_LIMIT 0x10000
#define SIO_TX_EMPTY(s) ((s) & 0xFF00)
#define WAIT_FIFO_EMPTY \
{ \
while( SIO_TX_EMPTY(*(volatile uint32*)(0x8400000c)));\
}
LOCAL void WriteCharToUART(char c)
{
while ((((*(volatile uint32*)0x8400000c) >> 8 )&0xFF) >= 32 ) {};
*(volatile uint32*)0x84000000 = c;
}
LOCAL void TF_SendMsgOut(char * buf, int size)
{
while (size --)
{
WriteCharToUART(*(buf++));
}
WriteCharToUART('\r');
WriteCharToUART('\n');
}
PUBLIC void TF_UartTrace(
const char *x_format, ...)
{
char format_str[256];
va_list args;
int nBuf;
WAIT_FIFO_EMPTY
memset (format_str,0,256);
va_start(args, x_format);\
nBuf = vsprintf(format_str, x_format, args);\
/* was there an error? */\
/* Was the expanded string too long? */\
va_end(args);\
/* Send message to serial buffer! */ \
TF_SendMsgOut(format_str, nBuf + 1);
WAIT_FIFO_EMPTY
}
嗯,這樣就舒坦多了。其實這樣就是標C裏面的printf函數,其關鍵無非就是va_list、va_start、va_end!
關於這組被包含在stdarg.h頭裏面的函數,在網上已經傳欄了,我也不過多闡述了!其實,在網上我看到別人寫的類如此的函數可以貼出來。
http://www.cnblogs.com/rainduck/archive/2010/11/10/1873417.html
這樣就是自己寫的printf函數!