📄 复件 uart.c
字号:
//***************************************************************************************
//
// FOUNCTION : UART.C
// DESCRIPTION: Routines for enabling the UARTs and sending/receiving data via them
//
// AUTHOR :
// HISTORY :
//***************************************************************************************
#include "cog7312.h"
#include "Uart.h"
#include "Irq.h"
#include "nucleus.h"
//#include <debug.h>
#define SIZE 256 /* max length of command or response */
//Founction declearation
void UART2_LISR(int);
void UART2Tx_HISR(void);
void UART2Rx_HISR(void);
int readerOpen( int , int , int , int , int );
int readerWrite( unsigned char * , int );
int readerRead( unsigned char * );
int readerClose( void );
void UART2RxTimer(unsigned long );
//Added 2002-5-14 15:52
int readerClear( void );
//****************************************************************************
//
// UARTs 配置完成标志:0未配置;1已配置。
//
//****************************************************************************
unsigned int lPort2Enabled = 0;
//****************************************************************************
//
// 分配UART1 and UART2 的收发缓冲区
//
//****************************************************************************
unsigned int iPort2TxRead = 0, iPort2TxWrite = 0;
NU_HISR UART2Tx_HISR_Control;
NU_HISR UART2Rx_HISR_Control;
unsigned long UART2Tx_HISR_Stack[2000];
unsigned long UART2Rx_HISR_Stack[2000];
//Removed this timer 2002-5-14 11:06
//NU_TIMER UART2RxTimer_Control;
char Command_Buf2[SIZE];
char Command_Buf3[SIZE];
char Rxbuffer[SIZE];
char pcPort2TxData[SIZE];
unsigned int Rxlen=0;
unsigned int cstart=0,pw=0,pr=0;
unsigned int actual_commnum=0;
unsigned int Flag_Reader_Timeout=0;
//****************************************************************************
//
// UART2_LISR is the interrupt handler for UART2. It takes care of keeping the
// transmit FIFO full and the receive FIFO empty.
//
//****************************************************************************
void UART2_LISR(int vector)
{
unsigned long * volatile pulPtr = (unsigned long *)HwBaseAddress;
long lIntStatus;
//
// Determine which interrupt(s) the UART is currently asserting.
//0x80001240
lIntStatus = pulPtr[HwIntStatus2 >> 2];
// Handle the Tx interrupt.
//
if(lIntStatus & HwIrqUartTx)
{
irq_disable(LISR_UART2_TX); //irq19
NU_Activate_HISR(&UART2Tx_HISR_Control);
}
//
// Handle the Rx interrupt.
//
if(lIntStatus & HwIrqUartRx)
{
irq_disable(LISR_UART2_RX); //irq20
NU_Activate_HISR(&UART2Rx_HISR_Control);
}
}
//***************************************************************************/
//UART2Tx_HISR
//
//
//***************************************************************************/
void UART2Tx_HISR(void)
{
unsigned long * volatile pulPtr = (unsigned long *)HwBaseAddress;
while(iPort2TxRead != iPort2TxWrite)
{
if(pulPtr[HwStatus2 >> 2] & HwStatusUartTxFifoFull)
{
irq_enable(LISR_UART2_TX); //irq19
break;
}
//
// Write the next character to the UART.
//
if(iPort2TxRead>=SIZE)
iPort2TxRead=0;
pulPtr[HwUart2Data >> 2] = pcPort2TxData[iPort2TxRead++];
}
if(iPort2TxRead == iPort2TxWrite )
{
irq_disable(LISR_UART2_TX); //irq19
}
}
//***************************************************************************/
//UART2Rx_HISR
//
//
//***************************************************************************/
void UART2Rx_HISR(void)
{
unsigned long * volatile pulPtr = (unsigned long *)HwBaseAddress;
unsigned int i,j,actual_commlen=0;
unsigned int len=0;
unsigned int Flag_Command_Ready=0;
//2002-5-14 11:29 Remove receive timer
//Close the UART2RxTimer
/*
NU_Control_Timer(&UART2RxTimer_Control,NU_DISABLE_TIMER);
Flag_Reader_Timeout=0;
*/
// Copy all the data from the UART's receive FIFO.
while(!(pulPtr[HwStatus2 >> 2] & HwStatusUartRxFifoEmpty))
{
if(Rxlen>= SIZE)
Rxlen=0x0;
Rxbuffer[Rxlen++] = pulPtr[HwUart2Data >> 2] & 0xff;
}
//Remember the received length
//no loopback
// printf("UART ---Rxlen = %d cstart = %d \n",Rxlen,cstart);
if(Rxlen >= cstart)
{
len= Rxlen-cstart;
}
else //loopback
{
len= SIZE-cstart+Rxlen;
}
//2002-5-14 11:35
//if(len!=0)
if(len>0)
{
if(len < 3 )
{
// Received a bad command ,Rxlen return, 2002-5-14 11:44
//It is a uncomplete command ,then wait for the other data
//Rxlen = cstart;
irq_enable(LISR_UART2_RX);
//2002-5-14 11:29 Remove receive timer
//NU_Control_Timer(&UART2RxTimer_Control,NU_ENABLE_TIMER);
}
else
{
while( len )
{
//printf("UART----Rev--len1=%d---\n",len);
// Read the command header and /header from the UART.
if(cstart >= SIZE)
cstart = 0;
Command_Buf2[0] = Rxbuffer[cstart];
if(((unsigned int)Command_Buf2[0]>=0x00)&&((unsigned int)Command_Buf2[0]<=0x7f))
{
if( (cstart+1) < SIZE)
{
Command_Buf2[1] = Rxbuffer[cstart+1];
}
else
{
Command_Buf2[1] = Rxbuffer[0];
}
if((char)Command_Buf2[1]==(char)(~Command_Buf2[0]) )
{
if( (cstart+2) < SIZE )
{
Command_Buf2[2]= Rxbuffer[cstart+2];
actual_commlen = (unsigned int)Command_Buf2[2]+3;
}
else
{
Command_Buf2[2]= Rxbuffer[2-(SIZE-cstart)];
actual_commlen = (unsigned int)Command_Buf2[2]+3;
}
if(actual_commlen <= len ) //commandlen
{
for(j=3,cstart+=3;j<actual_commlen;j++)
{
if(cstart>=SIZE)
cstart=cstart-SIZE;
Command_Buf2[j]= Rxbuffer[cstart++];
}
len -= actual_commlen;
Flag_Command_Ready=1;
if(cstart>=SIZE)
cstart-=SIZE;
}
else //uncomplete command
{
Flag_Command_Ready=0;
irq_enable(LISR_UART2_RX);
//Remove receive timer 2002-5-14 14:03
//NU_Control_Timer(&UART2RxTimer_Control,NU_ENABLE_TIMER);
break;
}
}
else //command head and /head do not matching
{
/* 2002-5-14 19:51 changed to the following code
Flag_Command_Ready=0;
Rxlen=cstart;
irq_enable(LISR_UART2_RX);
break;
*/
cstart++;
len--;
continue;
}
}
else //First byet is not command head
{
/* 2002-5-14 19:51 changed to the following code
Flag_Command_Ready=0;
Rxlen=cstart;
irq_enable(LISR_UART2_RX);
break;
*/
cstart++;
len--;
continue;
}
//Store command
if(Flag_Command_Ready==1)
{
for(i=0;i<actual_commlen;i++)
{
if(pw >= SIZE)
{
pw = 0;
}
Command_Buf3[pw++]=Command_Buf2[i];
}
actual_commnum++;
}
//enable rx int
irq_enable(LISR_UART2_RX);
} //end while(len)
}//end else
}
else
{
irq_enable(LISR_UART2_RX);
}
}
//*********************************************************************************/
//UART2RxTimer()
//
//
//*********************************************************************************/
/* Remove this timer 2002-5-14 11:09
void UART2RxTimer(unsigned long id )
{
readerClose( );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -