好记心不如烂笔头,为方便以后查看代码及代码重复利用,这里贴出S3C2440 UART驱动代码。使用友善MINI2440开发板,开发环境为RealView MDK 4.22。需要注意的是,本代码中,对GPIO的初始化放在了s3c2440.s中完成,采用keil自带的html方式进行配置。
该源码结构简单明了,原始工程下载地址:点击打开链接
UART控制器初始化:
void Uart_Init(void) { #define rULCON0 (*(volatile unsigned int*)0x50000000) #define rUCON0 (*(volatile unsigned int*)0x50000004) #define rUBRDIV0 (*(volatile unsigned int*)0x50000028) #define PCLK 50000000 #define BUADRATE 115200 rULCON0 = 0x03; //No parity, One stop bit, 8-bits data rUCON0 = 0x05; //Tx Enable, Rx Enable, PCLK as source clock rUBRDIV0 = (int)(PCLK / (BUADRATE * 16)) - 1; //115200bps }
字符发送函数:
void Uart_Putc(unsigned char c) { #define rUTRSTAT0 (*(volatile unsigned int*)0x50000010) #define rUTXH0 (*(volatile unsigned int*)0x50000020) #define BUFFER_EMPTY (1 << 1) while(!(rUTRSTAT0 & BUFFER_EMPTY)); rUTXH0 = c; }
字符接收函数:
unsigned char Uart_Getc(void) { #define rUTRSTAT0 (*(volatile unsigned int*)0x50000010) #define rURXH0 (*(volatile unsigned int*)0x50000024) #define BUFFER_READY (1 << 0) while(!(rUTRSTAT0 & BUFFER_READY)); return rURXH0; }
为了使用printf库函数,需要进行如下重映射:
struct __FILE { int handle; /* Whatever you require here. If the only file you are using is */ /* standard output using printf() for debugging, no file handling */ /* is required. */ }; /* FILE is typedef'd in stdio.h. */ FILE __stdout; int fputc(int ch, FILE *f) { Uart_Putc(ch); return ch; } int ferror(FILE *f) { /* Your implementation of ferror */ return EOF; }
测试代码:
int main(void) { unsigned char ch; //clock_init(); Uart_Init(); printf("%s, %d", __FILE__, __LINE__); while(1) { ch = Uart_Getc(); Uart_Putc(ch); } }
时间: 2024-11-03 15:46:38