精品博文ARM中打印函数print 的几种实现方法

精品博文ARM中打印函数print 的几种实现方法

编码文章call10242025-09-08 14:18:506A+A-

1 利用C库函数printf

步骤:

1) 首先需要包含头文件stdio.h。

2) 然后定义文件句柄。实际上就是一个int型变量封装在结构体中。

struct __FILE{int handle;};

3)定义FILE __stdout; FILE即为__FILE,通过stdio.h宏定义。

4) 实现函数

int fputc(int ch, FILE *f){ char tempch = ch; sendchar(tempch); return ch;}

5) 实现函数

int sendchar (int ch){

if (ch == '\n') {

while (!(console_tty_f->lsr & UART_LSR_THRE));

console_tty_f->dll_fifo = 0x0d;

}

while (!(console_tty_f->lsr & UART_LSR_THRE));

return (console_tty_f->dll_fifo = ch);

}

由以上代码可见,printf为阻塞函数,采用等待发完的办法,可能影响其它进程。如果编写非等待的打印函数,可以采用第二种方法。

2 利用C库函数vsprintf和变参函数

步骤:

1) 包含头文件stdio.h和stdarg.h。

2) 编写变参数函数。

void print(const char *lpszFormat, ...){char szBuffer[PRINT_BUF]={0};

va_list args; int ret; va_start(args, lpszFormat);

ret = vsprintf(szBuffer, lpszFormat, args);

va_end(args);

uart[console_uart].put_2_ring(console_uart,szBuffer, ret);

}

由此可见,利用库函数vsprintf格式化输入字符串,然后在空闲时发送。

3 自行完成参数提取,格式化

步骤:

1) 定义可变参数列表typedef char *va_list;

2) 定义地址对齐宏

#define _AUPBND (sizeof (int) - 1)

#define _ADNBND (sizeof (int) - 1)

#define _bnd(X, bnd) (((sizeof (X)) + (bnd)) & (~(bnd)))

3) 定义可变长参数提取宏

#define va_start(ap, A) (void) ((ap) = (((char *) &(A)) + (_bnd (A,_AUPBND))))

#define va_arg(ap,T) (*(T *)(((ap) += (_bnd (T, _AUPBND))) - (_bnd (T,_ADNBND)))

#define va_end(ap) (void) 0

4)编写变参数函数。如方法2第2步。

5) 实现vsprintf函数。实现源码较多,如linux等。只是没有对浮点的支持。

点击这里复制本文地址 以上内容由文彬编程网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

文彬编程网 © All Rights Reserved.  蜀ICP备2024111239号-4