📄 uart.c
字号:
/*----------------------------------------------------------------------------------
File: uart.c
Author: Qiu Peng [roc98@163.ne]
Note: UART driver for S3C44B0X
Create Date: 2004.7.25
Last Modified: 2004.7.28
-----------------------------------------------------------------------------------*/
#include "../inc/uart.h"
char TxBuf[TxBufLen], RxBuf[RxBufLen];
char *inTxBuf, *outTxBuf, *inRxBuf, *outRxBuf;
//for statistic
int UartTxCount, UartRxCount;
//use Uart0 or Uart1, default for the Uart0
static int UartInUse = 0;
/*------------------------------------------------------------------------------
Function: Uart_Init
Note: S3C44B0X UART(0,1) initial
In:
1. mclk: used for count UBRDIVn, 0 to use CPU MCLK
2. baud: Uart baud rate
example: Uart_Init(0, 9600)
Out: None
Date: 2004.7.25 / 2004.7.25
-------------------------------------------------------------------------------*/
void Uart_Init_2(int mclk, int baud)
{
int i;
if (mclk == 0)
mclk=MCLK;
//UART0
rUFCON0 = 0x0; //FIFO disable
rUMCON0 = 0x0; //AFC Disable
rULCON0 = 0x3; //Normal,No parity,1 stop,8 bit
rUCON0 = 0x245; //rx=edge,tx=level,disable timeout int.,enable rx error int.,normal,interrupt or polling
rUBRDIV0 = ( (int)(mclk/16./baud + 0.5) -1 ); //see S3C44B0X datasheet Ch10 UART p10-18
//UART1
rUFCON1=0x0;
rUMCON1=0x0;
rULCON1=0x3;
rUCON1=0x245;
rUBRDIV1=( (int)(mclk/16./baud + 0.5) -1 );
//pISR_UTXD0=(unsigned)UartTx;
UartTxCount = 0;
UartRxCount = 0;
for(i = 0; i < 100; i++); //do some delay
}
/*------------------------------------------------------------------------------
Function: Uart_InitBuffer
Note: Uart Tx and Rx buffer init
In: None
Out: None
Date: 2004.7.25 / 2004.7.25
-------------------------------------------------------------------------------*/
void Uart_InitBuffer(void)
{
//at first, the in/out pointers both in the base of their buffer
inTxBuf = TxBuf;
outTxBuf = TxBuf;
inRxBuf = RxBuf;
outRxBuf = RxBuf;
UartTxCount = 0;
UartRxCount = 0;
}
/*------------------------------------------------------------------------------
Function: Uart_Select
Note: S3C44B0X has 2 UARTs. to choose one
In: UartNo: 0, Uart0; 1, Uart1
Out: None
Date: 2004.7.25 / 2004.7.25
-------------------------------------------------------------------------------*/
void Uart_select(int UartNo)
{
if (UartNo == 0)
UartInUse = 0;
else
UartInUse = 1;
}
/*------------------------------------------------------------------------------
Function: Uart_TxStart
Note: In order to enter Tx INT, write a byte(fisrt of TxBuf) to Tx BUF.
In: None
Out: None
Date: 2004.7.25 / 2004.7.28
-------------------------------------------------------------------------------*/
void Uart_TxStart(void)
{
if (outTxBuf == inTxBuf) //TxBuf empty
{
return;
}
if (UartInUse == 0)
{
if (rUTRSTAT0 & 0x2) //if Transmit buffer is empty, Tx not start.
WrUTXH0(*outTxBuf);
else
return;
}
else
{
if (rUTRSTAT1 & 0x2) //if Transmit buffer is empty, Tx not start.
WrUTXH0(*outTxBuf);
else
return;
}
outTxBuf++; //point to next data that will be sent
if (outTxBuf == (TxBuf+TxBufLen)) //beyond the range
outTxBuf = TxBuf;
UartTxCount++;
}
/*------------------------------------------------------------------------------
Function: UartTx
Note: called by real Tx ISR, send data and do buffer process
In: None
Out: None
Date: 2004.7.25 / 2004.7.25
-------------------------------------------------------------------------------*/
void UartTx(void)
{
if (outTxBuf == inTxBuf) //TxBuf empty
{
//TIflag = 0;
return;
}
if (UartInUse == 0)
{
WrUTXH0(*outTxBuf); //from TxBuf, write a byte to Tx reg. see 44b.h
rI_ISPC |= BIT_UTXD0; //clear pending bit
}
else
{
WrUTXH1(*outTxBuf);
rI_ISPC |= BIT_UTXD1; //clear pending bit
}
outTxBuf++; //point to next data that will be sent
if (outTxBuf == (TxBuf+TxBufLen)) //beyond the range
outTxBuf = TxBuf;
UartTxCount++;
}
/*------------------------------------------------------------------------------
Function: UartRx
Note: called by real Rx ISR, receive a byte to RxBuf
In: None
Out: None
Date: 2004.7.25 / 2004.7.25
-------------------------------------------------------------------------------*/
void UartRx(void)
{
char *t;
t = inRxBuf;
t++;
if (t == (RxBuf+RxBufLen))
t = RxBuf;
if (t==outRxBuf) //RxBuf full, no receive
return;
if (UartInUse == 0)
*inRxBuf = RdURXH0();
else
*inRxBuf = RdURXH1();
inRxBuf = t;
UartRxCount++;
}
/*------------------------------------------------------------------------------
Function: Uart_PrintChar
Note: write a byte to TxBuf
In: ch: the byte to send
Out: None
Date: 2004.7.25 / 2004.7.25
-------------------------------------------------------------------------------*/
void Uart_PrintChar(char ch)
{
char *t;
t = inTxBuf;
t++;
if (t == (TxBuf+TxBufLen))
t = TxBuf;
if (t == outTxBuf) //TxBuf full
return; //no send
*inTxBuf = ch;
inTxBuf = t;
Uart_TxStart(); //Tx begin
}
/*------------------------------------------------------------------------------
Function: Uart_PrintStr
Note: put a string to TxBuf
In: pt: the pointer of the string
Out: None
Date: 2004.7.25 / 2004.7.28
-------------------------------------------------------------------------------*/
void Uart_PrintStr(char *pt)
{
char *t, *p;
t = inTxBuf;
p = pt;
while (*p != '\0') //put into TxBuf one by one
{
t++;
if (t == (TxBuf+TxBufLen))
t = TxBuf;
if (t == outTxBuf) //TxBuf full
return; //no send
*inTxBuf = *p++;
inTxBuf = t;
}
Uart_TxStart(); //Tx begin
}
/*------------------------------------------------------------------------------
Function: Uart_Printf
Note: put a string to TxBuf, like printf on PC platform.
In: fmt: the number is uncertain.
Out: None
Date: 2004.7.25 / 2004.7.25
-------------------------------------------------------------------------------*/
void Uart_printf(char *fmt,...)
{
va_list ap;
char string[512];
va_start(ap,fmt);
vsprintf(string,fmt,ap);
Uart_PrintStr(string);
va_end(ap);
}
/*------------------------------------------------------------------------------
Function: Uart_GetStr
Note: get a string, then send message to OS TaskShell to parse.
In: str
Out: str: received string.
Date: 2004.7.25 / 2004.7.25
-------------------------------------------------------------------------------*/
int Uart_GetStr(char *str)
{
char *p;
p = str;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -