#include "Uart.h"
#include "String.h"
UartData Uart0Data;
UartData Uart1Data;
UartTxd Uart0SendData;
UartTxd Uart1SendData;
UINT16 CRC_Calc(UINT8 *pBuf, UINT8 num)
{
UINT16 i, j;
UINT16 wTemp;
UINT16 wCRC = 0xFFFF;
for (i=0; i<num; i++)
{
wTemp = pBuf[i] & 0x00FF;
wCRC ^= wTemp;
for (j=0; j<8; j++)
{
if (wCRC & 0x0001)
{
wCRC >>= 1;
wCRC ^= 0xA001;
}
else
{
wCRC >>= 1;
}
}
}
return wCRC;
}
void ResetUart0(void)
{
Uart0Data.IsDataOK = 0x00;
Uart0Data.OverTime = 0x00;
}
void ResetUart1(void)
{
Uart1Data.IsDataOK = 0x00;
Uart1Data.OverTime = 0x00;
}
UINT8 IsReiveNewData0(void)
{
return Uart0Data.IsDataOK;
}
UINT8 IsReiveNewData1(void)
{
return Uart1Data.IsDataOK;
}
UINT8 IsUart0RecFinish(void)
{
return Uart0Data.OverTime;
}
UINT8 IsUart1RecFinish(void)
{
return Uart1Data.OverTime;
}
void ClearUart0SendFlag(void)
{
Uart0SendData.pBuf = 0x00;
Uart0SendData.Sending = 0x00;
Uart0SendData.Len = 0x00;
}
void ClearUart1SendFlag(void)
{
Uart1SendData.pBuf = 0x00;
Uart1SendData.Sending = 0x00;
Uart1SendData.Len = 0x00;
}
void ClearUart0RecFlag(void)
{
Uart0Data.RecLen = 0;
}
/* 功能:提取串口接收到的数据
** 输入参数:pBuf-数据缓冲区
** 输出参数:pLen-数据的长度
** 返回参数:无
*/
void CopyUart0RecDataToBuffer(UINT8 *pBuf, UINT8 *pLen)
{
memcpy(pBuf, Uart0Data.RecBuf, Uart0Data.RecLen);
*pLen = Uart0Data.RecLen;
}
void CopyUart1RecDataToBuffer(UINT8 *pBuf, UINT8 *pLen)
{
memcpy(pBuf, Uart1Data.RecBuf, Uart1Data.RecLen);
*pLen = Uart1Data.RecLen;
}
/* 功能:利用FIFO发送数据
** 输入参数pBuf = 数据缓冲区, Len = 数据长度
** return = 0xFF 失败,= 0x00 成功
*/
UINT8 SendDataToUart0(UINT8 *pBuf, UINT8 Length)
{
UINT8 Len;
UINT8 i;
if (0xFF == Uart0SendData.Sending)
{
return 0xFF; // 串口忙,发送失败,需要等待
}
Uart0SendData.Len = Length;
Uart0SendData.pBuf = pBuf;
Uart0SendData.Sending = 0xFF;
if (Uart0SendData.Len > FIFO_LENGTH)
Len = FIFO_LENGTH;
else
Len = Uart0SendData.Len;
Uart0SendData.Sending = 0xFF;
if (Uart0SendData.Len > FIFO_LENGTH)
Len = FIFO_LENGTH;
else
Len = Uart0SendData.Len;
for (i=0; i<Len; i++)
{
U0THR = *Uart0SendData.pBuf;
Uart0SendData.pBuf++;
}
Uart0SendData.Len -= Len;
return 0x00;
}
UINT8 SendDataToUart1(UINT8 *pBuf, UINT8 Length)
{
UINT8 i;
UINT8 Len;
if (0xFF == Uart1SendData.Sending)
{
return 0xFF; // 串口忙,发送失败,需要等待
}
Uart1SendData.Len = Length;
Uart1SendData.pBuf = pBuf;
Uart1SendData.Sending = 0xFF;
if (Uart1SendData.Len > FIFO_LENGTH)
Len = FIFO_LENGTH;
else
Len = Uart1SendData.Len;
for (i=0; i<Len; i++)
{
U1THR = *Uart1SendData.pBuf;
Uart1SendData.pBuf++;
}
Uart1SendData.Len -= Len;
return 0x00;
}
/* 功能:串口UART0接收中断
** 输入参数:无
** 输出参数:无
** 返回参数:无
*/
void __irq IRQ_UART0(void)
{
UINT8 IIR;
UINT8 Len;
UINT8 RD;
UINT8 i;
IIR = U0IIR;
switch (IIR & 0x0E)
{
case 0x02:
if (Uart0SendData.Len > 0)
{
if (Uart0SendData.Len > FIFO_LENGTH)
{
Len = FIFO_LENGTH;
}
else
{
Len = Uart0SendData.Len;
}
for (i=0; i<Len; i++)
{
U0THR = *Uart0SendData.pBuf;
Uart0SendData.pBuf++;
}
Uart0SendData.Len -= Len;
Uart0SendData.Sending = 0xFF;
}
else
{
ClearUart0SendFlag();
}
break;
case 0x0C: // 接收数据有效,超时
Uart0Data.OverTime = 0xFF;
case 0x04: // 接收数据有效
while ((U0LSR & 0x01) == 1) // 最多8字节
{
RD = U0RBR;
// 接收数据
if (Uart0Data.Counter < UART_TRIGGER_DEPTH)
{
Uart0Data.Buffer[Uart0Data.Counter++] = RD;
}
}
memcpy(Uart0Data.RecBuf + Uart0Data.RecLen, Uart0Data.Buffer, Uart0Data.Counter);
Uart0Data.RecLen += Uart0Data.Counter;
Uart0Data.Counter = 0;
Uart0Data.IsDataOK = 0xFF;
break;
default:
break;
}
VICVectAddr = 0x00; // interrupt end
}
void __irq IRQ_UART1(void)
{
if (0x04 == (U1IIR & 0x0F))
{
// RSV_BUF1[RSV_COUNTER1] = U1RBR;
} // end if
VICVectAddr = 0x00; // interrupt end
}
/****************************************************************************
* 名称:UART0_Ini()
* 功能:初始化串口0。设置其工作模式及波特率。
* 入口参数:baud 波特率
* set 模式设置(UARTMODE数据结构)
* 出口参数:返回值为1时表示初始化成功,为0表示参数出错
****************************************************************************/
uint8 UART0_Ini(UartMode set)
{
uint32 bak;
PCONP |= (0x01 << 3);
PINSEL0 &= ~((uint32)0x0F);
PINSEL0 |= ((uint32)0x05);
if ((0 == set.BaudRate) || (set.BaudRate > 115200))
{
return 0;
}
if ((set.DataLen < 5) || (set.DataLen > 8))
{
return 0;
}
if ((0 == set.StopBit) || (set.StopBit > 2))
{
return 0;
}
if (set.Parity > 4)
{
return 0;
}
/* 设置串口波特率 */
U0LCR = 0x80; // DLAB位置1
bak = (Fpclk>>4) / set.BaudRate;
U0DLM = bak >> 8;
U0DLL = bak & 0xff;
/* 设置串口模式 */
bak = set.DataLen - 5; // 设置字长度
if (2 == set.StopBit)
{
bak |= 0x04; // 判断是否为2位停止位
}
if (0 != set.Parity)
{
set.Parity = set.Parity - 1;
bak |= 0x08;
}
bak |= set.Parity << 4; // 设置奇偶校验
U0LCR = bak;
switch (UART_TRIGGER_DEPTH)
{
case 1:
U0FCR = 0x07; break;
case 4:
U0FCR = 0x47; break;
case 8: // 使能FIFO,并设置触发点为8字节0x07
U0FCR = 0x87; break;
case 14:
U0FCR = 0xC7; break;
default:
U0FCR = 0x07;
}
U0IER = 0x03; // 允许RBR中断,即接收中断
VICVectCntl4 = 0x20 | 6; // UART0中断通道分配到IRQ slot 0,即优先级0
VICVectAddr4 = (int)IRQ_UART0; // 设置UART0向量地址
VICIntEnable |= (1<<6); // 使能UART0中断
return 1;
}
void EnableUart1(void)
{
VICIntEnable |= (1<<7);
}
void DisableUart1(void)
{
VICIntEnClr |= (1<<7);
}
/* 功能:初始化串口1,设置其工作模式及波特率
**
*/
uint8 UART1_Ini(UartMode set)
{
/*
uint32 bak;
PCONP |= (0x01 << 4);
PINSEL0 &= ~((uint32)0xF0000);
PINSEL0 |= ((uint32)0x50000);
// 参数过滤
if ((0 == baud) || (baud > 115200))
{
return 0;
}
if ((set.datab < 5) || (set.datab > 8))
{
return 0;
}
if ((0 == set.stopb) || (set.stopb > 2))
{
return 0;
}
if (set.parity > 4)
{
return 0;
}
// 设置串口波特率
U1LCR = 0x80; // DLAB位置1
bak = (Fpclk >> 4) / baud;
U1DLM = bak >> 8;
U1DLL = bak & 0xff;
// 设置串口模式
bak = set.datab - 5; // 设置字长度
if (2 == set.stopb)
{
bak |= 0x04; // 判断是否为2位停止位
}