您的位置:首页 > 其它

【ARM】串口通信·FS2410·裸机UART

2013-06-27 17:40 176 查看
开发环境
(1)硬件平台:FS2410 (2)主机:Ubuntu 12.04
FS2410串口的原理图
650) this.width=650;" src="http://img1.51cto.com/attachment/201306/185514751.jpg" title="串口原理图.jpg" />

串口UART寄存器配置

配置TXD0与RXD0(GPH2、GPH3)

650) this.width=650;" src="http://img1.51cto.com/attachment/201306/190428439.jpg" title="TXD0.jpg" />
650) this.width=650;" src="http://img1.51cto.com/attachment/201306/190428979.jpg" title="GPH设置.jpg" />
设置波特率(UBRDIVn)

650) this.width=650;" src="http://img1.51cto.com/attachment/201306/191054296.jpg" title="UBRDIV设置.jpg" />

设置传输格式(ULCONn)
650) this.width=650;" src="http://img1.51cto.com/attachment/201306/191328567.jpg" title="ULCON设置.jpg" />

选择时钟源与中断方式(UCONn)
650) this.width=650;" src="http://img1.51cto.com/attachment/201306/191554998.jpg" title="UCON设置.jpg" />

设置FIFO(UFCONn)
650) this.width=650;" src="http://img1.51cto.com/attachment/201306/191752756.jpg" title="UFCON设置.jpg" />

设置流控(UMCONn)
650) this.width=650;" src="http://img1.51cto.com/attachment/201306/191908623.jpg" title="UMCON设置.jpg" />

接收字符和发送字符(UTRSTATn)
650) this.width=650;" src="http://img1.51cto.com/attachment/201306/192047873.jpg" title="UART TR设置.jpg" />

源代码

//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


编译运行

650) this.width=650;" src="http://img1.51cto.com/attachment/201306/192535329.jpg" title="编译uart.jpg" />

编译运行都OK

本文出自 “成鹏致远” 博客,请务必保留此出处http://infohacker.blog.51cto.com/6751239/1223137
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: