S3C2440 UART使用筆記
Uart_Init(0,115200);
////////////////////////////////********************************************************************////////////////////////////////////////////////
void Uart_Init(int pclk,int baud)
{
int i;
if(pclk == 0)
pclk = PCLK;
rUFCON0 = 0x0; //UART channel 0 FIFO control register, FIFO disable
rUFCON1 = 0x0; //UART channel 1 FIFO control register, FIFO disable
rUFCON2 = 0x0; //UART channel 2 FIFO control register, FIFO disable
rUMCON0 = 0x0; //UART chaneel 0 MODEM control register, AFC disable
rUMCON1 = 0x0; //UART chaneel 1 MODEM control register, AFC disable
//UART0
rULCON0 = 0x3; //Line control register : Normal,No parity,1 stop,8 bits
注: bit7:保留位
bit6: 通訊模式0--Normal mode 1--Infrared Tx/Rx Mode
bit[5:3]:校驗(yàn)?zāi)J?00--Noparity
bit[2]:停止位 0--one stop bit 1--two stop bit
bit[1:0]:沒幀數(shù)據(jù)位 00-5 bits 01--6 bits 10--7bits 11--8bits
// [10] [9] [8] [7] [6] [5] [4] [3:2] [1:0]
// Clock Sel, Tx Int, Rx Int, Rx Time Out, Rx err, Loop-back, Send break, Transmit Mode, Receive Mode
// 0 1 0 , 0 1 0 0 , 01 01
// PCLK Level Pulse Disable Generate Normal Normal Interrupt or Polling
rUCON0 = 0x245; // Control register
注: bit[15:12]: all zero
bit[11:10]:00/10--PCLK 01--UEXTCLK 11-FCLK/n
bit[9]:TX中斷設(shè)置:0--Pulse 1--Level
bit[8]:RX中斷設(shè)置:0--Pulse1--Level
bit[7]:Rx Time Out:Disable
bit[6]:1--Generate Rx err interrupt 0--不生成中斷
bit[5]bit[4]:Normal Mode
bit[3:2]bit[1:0]: 01--Interrupt or Polling Mode
rUBRDIV0=( (int)(pclk/16./baud+0.5) -1 ); //Baud rate divisior register 0
注: UBRDIVn = (int)( UART clock / ( buad rate x 16) ) –1
//UART1
rULCON1 = 0x3;
rUCON1 = 0x245;
rUBRDIV1=( (int)(pclk/16./baud+0.5) -1 );
//UART2
rULCON2 = 0x3;
rUCON2 = 0x245;
rUBRDIV2=( (int)(pclk/16./baud+0.5) -1 );
注:串口1、2與串口0設(shè)置一樣
for(i=0;i<100;i++);
}
////////////////////////////////********************************************************************////////////////////////////////////////////////
consoleNum = 0;
Uart_Select( consoleNum );//whichUart =consoleNum;
Uart_SendByte('n');
////////////////////////////////********************************************************************////////////////////////////////////////////////
void Uart_SendByte(int data)
{
if(whichUart==0)
{
if(data=='n')
{
while(!(rUTRSTAT0 & 0x2));
注: UTRSTATn為只讀寄存器,只有低3位有效,該處為死等到發(fā)射buffer為空
bit[0]: Receive buffer data ready: 0--Empty 1--buffer has a received data
bit[1]:Transmit buffer empty:0--The buffer not empty 1--empty
bit[2]:Transmit empty:0--not empty 1--Transmitter (transmit buffer & shifter register) empty
// Delay(1); //because the slow response of hyper_terminal
WrUTXH0('r');
注: 發(fā)送數(shù)據(jù)'r'回車
}
while(!(rUTRSTAT0 & 0x2)); //Wait until THR is empty.
// Delay(1);
WrUTXH0(data);
注: 發(fā)送數(shù)據(jù)'n' next line
}
else if(whichUart==1)
{
if(data=='n')
{
while(!(rUTRSTAT1 & 0x2));
//Delay(1); //because the slow response of hyper_terminal
rUTXH1 = 'r';
}
while(!(rUTRSTAT1 & 0x2)); //Wait until THR is empty.
//Delay(1);
rUTXH1 = data;
}
else if(whichUart==2)
{
if(data=='n')
{
while(!(rUTRSTAT2 & 0x2));
//Delay(1); //because the slow response of hyper_terminal
rUTXH2 = 'r';
}
while(!(rUTRSTAT2 & 0x2)); //Wait until THR is empty.
//Delay(1);
rUTXH2 = data;
}
}
////////////////////////////////********************************************************************////////////////////////////////////////////////