【说明】
前一小节,对于UART的基本设置已经介绍得很清楚了,这一节就是完全按前面讲的步骤要一步一步设置UART,并实现2个函数,用来往串口发送一个字符和接收一个字符,我们采用轮询方式发送,显然这种方式十分浪费CPU资源,后面章节我们将采用中断方式发送
【代码】
1】工程文件
2】clock.S start.S
这几个和前面几章一样的就不贴了
3】main.c
#include "uart.h"
int main()
{
char c;
init_uart(); /* 波特率,数据位,停止位... */
while (1)
{
c = getchar();
putchar(c+1);
}
return 0;
}
4】uart.h
void putchar(char c);
char getchar(void);
void init_uart(void);
5】uart.c
#include "uart.h"
#define ULCON0 (*((volatile unsigned long *)0x7F005000))
#define UCON0 (*((volatile unsigned long *)0x7F005004))
#define UFCON0 (*((volatile unsigned long *)0x7F005008))
#define UMCON0 (*((volatile unsigned long *)0x7F00500C))
#define UTRSTAT0 (*((volatile unsigned long *)0x7F005010))
#define UFSTAT0 (*((volatile unsigned long *)0x7F005018))
#define UTXH0 (*((volatile unsigned char *)0x7F005020))
#define URXH0 (*((volatile unsigned char *)0x7F005024))
#define UBRDIV0 (*((volatile unsigned short *)0x7F005028))
#define UDIVSLOT0 (*((volatile unsigned short *)0x7F00502C))
#define GPACON (*((volatile unsigned long *)0x7F008000))
void init_uart(void)
{
GPACON &= ~0xff;
GPACON |= 0x22;
/* ULCON0 */
ULCON0 = 0x3; /* 数据位:8, 无较验, 停止位: 1, 8n1 */
UCON0 = 0x5; /* 使能UART发送、接收 */
UFCON0 = 0x01; /* FIFO ENABLE */
UMCON0 = 0;
/* 波特率 */
/* DIV_VAL = (PCLK / (bps x 16 ) ) - 1
* bps = 115200
* DIV_VAL = (66500000 / (115200 x 16 ) ) - 1
* = 35.08
*/
UBRDIV0 = 35;
/* x/16 = 0.08
* x = 1
*/
UDIVSLOT0 = 0x1;
}
char getchar(void)
{
while ((UFSTAT0 & 0x7f) == 0); /* 如果RX FIFO空,等待 */
return URXH0; /* 取数据 */
}
void putchar(char c)
{
while (UFSTAT0 & (1<<14)); /* 如果TX FIFO满,等待 */
UTXH0 = c; /* 写数据 */
}
6】makefile
uart.bin: start.o main.o uart.o clock.o
arm-linux-ld -Ttext 0 -o uart.elf $^
arm-linux-objcopy -O binary uart.elf uart.bin
arm-linux-objdump -D uart.elf > uart.dis
%.o : %.S
arm-linux-gcc -o $@ $< -c
%.o : %.c
arm-linux-gcc -o $@ $< -c
clean:
rm *.o uart.elf uart.bin uart.dis
【烧写】
同LED
【实验现象】
在串口上输出a 会出现b