📄 uart.c
字号:
#define UARTMODEL
#include "uart.h"
struct UART uart[SYS_COM_NUM];
/*********************************************************************************************************
**函数名称: UART_HANDLER InitUart(uint8 com,uint32 baud,uint8 datab,uint8 stopb,uint8 parity,uint8 vic_id)
**函数说明: 串口0 1 初始化
**输入参数: com 串口
baud 波特率
datab 数据位
stopb 停止位
parity 校验位
vic_id 串口中断号
**输出参数: 串口的指针
**作者: 崔敬军
**创建时间: 2006年11月06日
********************************************************************************************************/
UART_HANDLER InitUart(uint8 com,uint32 baud,uint8 datab,uint8 stopb,uint8 parity,uint8 vic_id)
{
uint32 bak;
UART_HANDLER hcom;
switch(com)
{
case 0:
{
PINSEL0 = (PINSEL0 & 0xfffffff0) | 0x05;
/* 参数过滤 */
if ((baud ==0 ) || (baud > 115200))
{
return (0);
}
if ((datab <5) || (datab > 8))
{
return (0);
}
if ((stopb == 0) || (stopb > 2))
{
return (0);
}
if (parity > 4)
{
return (0);
}
/* 设置串口波特率 */
U0LCR = 0x80; // DLAB = 1
bak = (Fpclk >> 4)/ baud;
U0DLM = bak >> 8;
U0DLL = bak & 0xFF;
/* 设置串口模式 */
bak = datab - 5; // 设置字长
if (stopb == 2)
{
bak |= 0x04; // 判断是否为2位停止位
}
if (parity != 0)
{
parity = parity - 1;
bak |= 0x08;
}
bak |= parity << 4; // 设置奇偶校验
U0LCR = bak;
U0IER = 0x03; //允许接收和发送中断
U0FCR = 0x00; //初始化FIFO
uart[0].sendhead=0;
uart[0].sendend=0;
uart[0].rechead=0;
uart[0].recend=0;
uart[0].ier=(uint8*)(0xe000c004);
uart[0].thr=(uint8*)(0xe000c000);
uart[0].lsr=(uint8*)(0xe000c014);
uart[0].ter=(uint8*)(0xe000c030);
(* uart[0].ter)=0x80; // 发送使能
uart[0].inthandle=vic_id;
hcom=&uart[0];
}
break;
case 1:
{
PINSEL0 = (PINSEL0 & 0xfff0ffff) | 0x50000;
/* 参数过滤 */
if ((baud ==0 ) || (baud > 115200))
{
return (0);
}
if ((datab <5) || (datab > 8))
{
return (0);
}
if ((stopb == 0) || (stopb > 2))
{
return (0);
}
if (parity > 4)
{
return (0);
}
/* 设置串口波特率 */
U1LCR = 0x80; // DLAB = 1
bak = (Fpclk >> 4)/ baud;
U1DLM = bak >> 8;
U1DLL = bak & 0xFF;
/* 设置串口模式 */
bak = datab - 5; // 设置字长
if (stopb == 2)
{
bak |= 0x04; // 判断是否为2位停止位
}
if (parity != 0)
{
parity = parity - 1;
bak |= 0x08;
}
bak |= parity << 4; // 设置奇偶校验
U1LCR = bak;
U1IER = 0x03; //允许接收和发送中断
U1FCR = 0x00; //初始化FIFO
uart[1].sendhead=0;
uart[1].sendend=0;
uart[1].rechead=0;
uart[1].recend=0;
uart[1].ier=(uint8*)(0xe0010004);
uart[1].thr=(uint8*)(0xe0010000);
uart[1].lsr=(uint8*)(0xe0010014);
uart[1].ter=(uint8*)(0xe0010030);
(* uart[1].ter)=0x80; // 发送使能
uart[1].inthandle=vic_id;
hcom=&uart[1];
}
break;
default:
{
hcom=NULL;
}
break;
}
return hcom;
}
/*********************************************************************************************************
**函数名称: int32 uartgetc(UART_HANDLER port)
**函数说明: 从接收缓冲中读取一个字节,如果没有读出返回-1
**输入参数: port 为被读取的串口的句柄
**输出参数: 返回-1表示操作失败,成功的话返回的是读取的数据
**作者: 崔敬军
**创建时间: 2006年11月06日
********************************************************************************************************/
int32 uartgetc(UART_HANDLER port)
{
int32 st=-1;
if(port==NULL)
{
return -1;
}
if(port->recend!=port->rechead)
{
st=(uint8)(port->rec[port->recend++]);
port->recend=port->recend%UART_FIFO_SIZE;
}
return st;
}
/***********************************************************************************************
** 函数名称:int32 ReadCom(UART_HANDLER port,uint8 * strhead,uint32 len,uint32 timeout)
** 函数说明:从串口中读取多个字节
** 入口参数:UART_HANDLER port 为被读取的串口的句柄
uint8 * strhead 读取数据存放的起始地址
int32 len 期望读取的数据字节数
int32 timeout 超时时间,0或小于0表示直到读出指定的字节数
** 出口参数:返回实际读取的字节数
**作者: 崔敬军
**创建时间: 2006年11月06日
***********************************************************************************************/
int32 ReadCom(UART_HANDLER port,uint8 * strhead,uint32 len,uint32 timeout)
{
int32 c,i,time;
i=0;
time=OSTimeGet();
while(i<len)
{
c=uartgetc(port);
if(c!=-1)
{
strhead[i]=(uint8)c;
i++;
}
else
{
OSTimeDly(8);
if((timeout>0)&&((OSTimeGet()-time)>timeout))
{
break;
}
}
}
return i;
}
/***********************************************************************************************
** 函数名称:int32 ReadCom(UART_HANDLER port,uint8 * strhead,uint32 len,uint32 timeout)
** 函数说明:写串口函数,必须在操作系统下运行
** 入口参数:UART_HANDLER port 为被读取的串口的句柄
uint8 * strhead 待发送数据起始地址
int32 len 待发送数据字节数
int32 timeout 超时时间,0或小于0表示直到发送完成
** 出口参数:返回值是写入了多少字节
**作者: 崔敬军
**创建时间: 2006年11月06日
***********************************************************************************************/
int32 WriteCom(UART_HANDLER port,uint8 * strhead,uint32 len,uint32 timeout)
{
int32 i=0;
uint32 time;
if(len>0&&port!=NULL)
{
time=OSTimeGet();
while(((OSTimeGet()-time)<timeout)&&(len!=0))
{
if((port->sendhead+1)==port->sendend)//满
{
OSTimeDly(8);
}
else
{
port->send[port->sendhead++]=*strhead++;//将要发送的数据,放到发送缓冲区
port->sendhead=(port->sendhead%UART_FIFO_SIZE);//当头指针到尾部时,重新从头开始
i++;
len--;
}
}
if(i)
{
if((* port->lsr)&0x40)//如果发送寄存器中没有数据
{
(* port->ter)=0x80;
if(port->sendend!=port->sendhead)
{
*(port->thr)=port->send[port->sendend++];
port->sendend=port->sendend%UART_FIFO_SIZE;
}
}
}
}
else
{
i=0;
}
return i;
}
/*********************************************************************************************************
** End Of File
********************************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -