開發環境linux
(1)硬件平臺:FS2410bash
(2)主機:Ubuntu 12.04ide
FS2410串口的原理圖oop
串口UART寄存器配置spa
配置TXD0與RXD0(GPH二、GPH3)3d
設置波特率(UBRDIVn)
blog
設置傳輸格式(ULCONn)開發
選擇時鐘源與中斷方式(UCONn)字符串
設置FIFO(UFCONn)get
設置流控(UMCONn)
接收字符和發送字符(UTRSTATn)
源代碼
//uart.c
#include "2410addr.h" void putc(char ch) //發送字符 { while(!(rUTRSTAT0 & 0x2) ); //判斷髮送緩衝區是否爲空 rUTXH0 = ch; //發送字符 } char getc(void) //接收字符 { while(!(rUTRSTAT0 & 0x1)); //判斷接收緩衝區是否爲空 return rURXH0; //返回接收的字符 } void putstr(char *str) //發送字符串 { while(*str != '\0') { if(*str == '\n') { putc('\n'); putc('\r'); } else putc(*str); str++; } } int main(void) { char ch; char str[100]; int i = 0, j; for(j = 0; j < 100; j++) //清空字符串 str[j] = '\0'; rGPHCON = rGPHCON & ~(0xf << 4) | (0xa << 4); //配置RXD0、TXD0 rGPHUP = 0x0c; //設置上拉電阻 rUFCON0 = 0; //不用FIFO rUMCON0 = 0; //不用流控 rULCON0 = 0x3; //8位數據,1箇中止位,無校驗,正常模式 rUCON0 = 0x5; //時鐘源位PCLK, 中斷方式爲查詢 rUBRDIV0 = ((int)(50000000 / 115200 / 16) - 1); //設置波特率爲115200 putc('\r'); putstr("hello mystery \n"); while(1) { ch = getc(); //接收字符 putc(ch); //發送字符 str[i++] = ch; if(ch == '\r') { i = 0; putc('\n'); putstr("mystery:"); putstr(str); putstr("\n"); for(j = 0; j < 100; j++) //清空字符串 str[j] = '\0'; } } return 0; }
//start.S
.text .global _start _start: #define WATCHDOG 0x53000000 ldr r0, =WATCHDOG mov r1, #0 str r1, [r0] ldr sp, =1024*4 bl main loop: b loop
//Makefile
uart.bin: start.S uart.c arm-linux-gcc -c start.S -o start.o arm-linux-gcc -c uart.c -o uart.o arm-linux-ld -Ttext 0x30008000 start.o uart.o -o uart arm-linux-objcopy -O binary -S uart uart.bin clean: rm -f *.o uart.binxyyc
編譯運行
編譯運行都OK