开发环境
(1)硬件平台:FS2410
(2)主机:Ubuntu 12.04
FS2410串口的原理图
串口UART寄存器配置
配置TXD0与RXD0(GPH2、GPH3)
设置波特率(UBRDIVn)
设置传输格式(ULCONn)
选择时钟源与中断方式(UCONn)
设置FIFO(UFCONn)
设置流控(UMCONn)
接收字符和发送字符(UTRSTATn)
源代码
//uart.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
|
.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
1
2
3
4
5
6
7
8
|
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
本文转自infohacker 51CTO博客,原文链接:http://blog.51cto.com/liucw/1223137