S3C2440在MDK4.22下使用printf向串口打印調(diào)試
串口的基本知識已經(jīng)在上一篇講過了。這里重點講解如何在MDK4.22下使用printf函數(shù),這樣的話就可以很方便的打印調(diào)試信息,追蹤。
本文引用地址:http://www.ex-cimer.com/article/201611/318140.htm這個知識來源于MDK自帶的幫助手冊。有現(xiàn)成的代碼提供。
實現(xiàn)方式有2種,使用標準C庫下裁剪合適的函數(shù),使用微庫C下裁剪合適的函數(shù)。
微庫下的情況,在魔術棒那里要勾選上使用微庫。然后需要定義如下結(jié)構(gòu)和改寫如下函數(shù)--FILE stdout fputc ferror。
標準庫的情況,也是需要關注FILE stdout fputc ferror。注意網(wǎng)上很多文章說,在標準庫下,需要關掉半主機模式,我嘗試過,關掉后,需要定義_sys_exit函數(shù),可以達到效果,但是如果不關掉半主機模式,和微庫一樣也只定義該定義的,也可以達到效果。不知道,是不是MDK版本升級后,已經(jīng)統(tǒng)一了兩種模式。
具體代碼:
uart.c
#include "S3C2440.h"#include "uart.h"void init_uart0(void){rULCON0 = 0x03; rUCON0 = (0x05);//15---12 11-10 9 8 7 6 5 4 3-2 1-0//not pclk/n pclk Tpulse Rpulse timeout disable rx error int disable loop dis break dis int or poll int or pollrUFCON0 = 0x00; rUMCON0 = 0x0; rUBRDIV0 = UART_BRDIV;}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; int fputc(int ch, FILE *f) { WrUTXH0_L(ch); /* Loop until the end of transmission */ while(!(rUTRSTAT0 & TXD0READY)) ;return ch; } int ferror(FILE *f) {/* Your implementation of ferror */return EOF;}
main.c
#include "S3C2440.h"#include "uart.h"#includeint main(void){ init_uart0();printf("hello worldrn");}
最終可以在UART0上打印hello world,這樣以后程序就可以拿來復用了!
評論