【ARM】2410裸机系列-uart串口通信
开发环境
(1)硬件平台:FS2410
(2)主机:Ubuntu 12.04
FS2410串口的原理图
串口UART寄存器配置
配置TXD0与RXD0(GPH2、GPH3)
设置波特率(UBRDIVn)
设置传输格式(ULCONn)
选择时钟源与中断方式(UCONn)
设置FIFO(UFCONn)
设置流控(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