/************************************************************
Copyright (C), 2007 by SEED Electronic Technology LTD.
FileName: UART.c
Author: Ya.X Version : V1.0 Date:2007-09-20
Description: UART模块相关处理函数
*************************************************************/
#include "uart.h"
/************************************************************
Function: UART_rget
Description: 读取UART寄存器的值
Calls: No
Called By:
Input: channel: 通道号UART A or UART B.
regnum: UART响应寄存器偏移地址
Output: No
Return: 相应UART寄存器的值
Others: No
************************************************************/
Uint8 UART_rget(Uint32 channel, Uint16 regnum)
{
Uint8 *udata;
udata = (Uint8 *)(DEC643_UART_BASE + channel + regnum);
return (*udata & 0xff);
}
/***********************************************************
Function: UART_rset
Description: 设置UART寄存器的值
Calls: No
Called By:
Input: channel: 通道号UART A or UART B.
regnum: UART响应寄存器偏移地址
regval: 待设置寄存器值
Output: No
Return: No
Others: No
************************************************************/
void UART_rset(Uint32 channel, Uint16 regnum, Uint8 regval)
{
Uint8 *udata;
udata = (Uint8 *)(DEC643_UART_BASE + channel + regnum);
*udata = (regval &0xff);
}
/************************************************************
Function: UART_open
Description: 打开UART设备,并返回一个Uint32的值
Calls: No
Called By:
Input: uart: 选择的uart端口
Output: No
Return: 有效的Uint32位的值,如果为0xFFFFFFFF,则无效
Others: No
************************************************************/
extern Uint32 UART_open(UartId uart)
{
Uint32 ret_data;
if(uart == 0)
{
ret_data = 0x00000000;
return ret_data;
}
if(uart ==1)
{
ret_data = 0x00020000;
return ret_data;
}
ret_data = 0xFFFFFFFF;
return ret_data;
}
/************************************************************
Function: UART_setup
Description: 创建UART通道
Calls: No
Called By:
Input: channel: 通道号,UART A or UART B
UartBaud: UART波特率.
UartWordLen: UART字长
UartStopBits: UART结束位
UartParity: UART极性
UartFifoControl: UART FIFO 控制
UartLoopway: UART loop.
Output: No
Return: No
Others: No
************************************************************/
void UART_setup(Uint32 channel,
Uint16 UartBaud,
Uint8 UartWordLen,
Uint8 UartStopBits,
Uint8 UartParity,
Uint8 UartFifoControl,
Uint8 UartLoopway)
{
Uint8 baudratel,baudrateh,uartdata;
/* Set clock divisor as "1" */
UART_rset(channel,UART_LCR,0xBF);
UART_rset(channel,UART_EFR,0x10);
UART_rset(channel,UART_LCR,0x00);
UART_rset(channel,UART_MCR,0x00);
/* Set baudrate. */
UART_rset(channel,UART_LCR,0x80);
baudratel = (UartBaud & 0x00ff);
UART_rset(channel,UART_DLL,baudratel);
baudrateh = (UartBaud & 0xff00) >> 8;
UART_rset(channel,UART_DLH,baudrateh);
UART_rset(channel,UART_LCR,0x00);
/* Set word length. */
uartdata = UartWordLen + UartStopBits + UartParity;
UART_rset(channel,UART_LCR,uartdata);
/* Set FIFO parameters. */
UART_rset(channel,UART_FCR,FIFO_rreset);
UART_rset(channel,UART_FCR,UartFifoControl);
UART_rset(channel,UART_MCR,UartLoopway);
}
/************************************************************
Function: UART_receive_single
Description: UART接收函数
Calls: No
Called By:
Input: channel: 通道号,UART A or UART B
Output: No
Return:
0: 接收结束
1: 数据没有准备好
0xFFFF: 接收错误
2: 中断超时
Others: No
************************************************************/
Uint16 UART_receive_single(Uint32 channel)
{
Uint16 revdata;
revdata = UART_rget(channel,UART_RHR);
return(revdata&0xFF);
}
/************************************************************
Function: UART_send_single
Description: UART单个数据发送函数
Calls: No
Called By:
Input: channel: 通道号,UART A or UART B
send_data:待发送数据
Output: No
Return: No
Others: No
************************************************************/
void UART_send_single(Uint32 channel,Uint8 send_data)
{
Uint16 lsrdata;
do
{
lsrdata = UART_rget(channel,UART_LSR);
}
while( (lsrdata & 0x40) != 0x40 );
UART_rset(channel, UART_THR, send_data);
}
/************************************************************
Function: UART_send
Description: UART发送函数
Calls: No
Called By:
Input: channel: 通道号,UART A or UART B
BaudRate:UART波特率
send_data:待发送数据地址
length: 待发送数据长度
Output: No
Return: No
Others: No
************************************************************/
void UART_send(Uint32 channel,Uint16 BaudRate,Uint16 length,Uint8 *send_data)
{
Uint16 i;
for( i=0; i<length; i++ )
{
UART_send_single(channel, *send_data++);
}
}
/************************************************************
Function: UART_IntSetup
Description: UART中断建立
Calls: No
Called By:
Input: channel: 通道号,UART A or UART B
UartIntn: UART中断
Output: No
Return: No
Others: No
************************************************************/
void UART_IntSetup(Uint32 channel, Uint8 UartIntn)
{
UART_rget(channel, UART_LSR);
UART_rset(channel,UART_IER, UartIntn);
UART_rset(channel,UART_MCR, 0x49);
}
/********************************************************************************/
/* End of UART.C */
/********************************************************************************/