通信设计应用



// *******************************************************************************
// Send an OUT record to end point 'ep'.
// pBuf points to the byte buffer; TBC is total byte count.
// NAKLimit is the number of NAKs to accept before returning.
//
// Returns HRSL code (0 for success, 4 for NAK limit exceeded, HRSL for problems)
// *******************************************************************************
//
BYTE Send_OUT_Record(BYTE ep, BYTE *pBuf, WORD TBC, WORD NAKLimit)
{
static WORD NAKct,rb; // Buf index, NAK counter, remaining bytes to send
WORD bytes2send; // temp
BYTE Available_Buffers; // Remaining buffers count (0-2)
BYTE FI_FB; // Temporary FIFO first byte
static BYTE CurrentBC; // Byte count for currently-sending FIFO
static BYTE CurrentFB; // First FIFO byte for currently-sending FIFO
static BYTE PendingBC; // Byte count for next 64 byte packet scheduled for sending
static BYTE PendingFB; // First FIFO byte for next 64 byte packet scheduled for sending
BYTE dum;
BYTE Transfer_In_Progress,FirstPass; // flags
//
NAKct=0;
Available_Buffers = 2;
rb = TBC; // initial remaining bytes = total byte count
FirstPass = 1;
//
do
{
while((rb!=0)&&(Available_Buffers!=0))
// WHILE there are more bytes to load and a buffer is available
{
// Pwreg(rEPIRQ,bmOUT1DAVIRQ);// *1* enable the 3420 for another OUT transfer
FI_FB = *pBuf; // Save the first byte of the 64 byte packet
bytes2send = (rb >= 64) ? 64: rb; // Lower of 64 bytes and remaining bytes
rb -= bytes2send; // Adjust 'remaining bytes'
Hwritebytes(rSNDFIFO,64,pBuf);
pBuf += 64; // Advance the buffer pointer to the next 64-byte chunk
Available_Buffers -= 1 // One fewer buffer is now available
//
if(Available_Buffers==1) // Only one has been loaded
{
CurrentBC = bytes2send;
CurrentFB = FI_FB;
}
else // Available_Buffers must be 0, both loaded.
{
PendingBC = bytes2send;
PendingFB = FI_FB;
}
//
if(FirstPass)
{
FirstPass = 0;
Hwreg(rSNDBC,CurrentBC); // Load the byte count
L7_ON // Light 7 is used as scope pulse
Hwreg(rHIRQ,bmHXFRDNIRQ); // Clear the IRQ
Hwreg(rHXFR,(tokOUT | ep)); // Launch an OUT1 transfer
L7_OFF
}
} // While there are bytes to load and there is space for them
//
do // While a transfer is in progress (not yet ACK'd)
{
// while((Hrreg(rHRSL) & 0x0F) == hrBUSY) ; // Hang here until current packet completes
while((Hrreg(rHIRQ) & bmHXFRDNIRQ) != bmHXFRDNIRQ) ;
dum = Hrreg(rHRSL) & 0x0F; // Get transfer result
if (dum == hrNAK)
{
Transfer_In_Progress = 1;
NAKct += 1;
if (NAKct == NAKLimit)
return(hrNAK);
else
{
Hwreg(rSNDBC,0); // Flip FIFOs
Hwreg(rSNDFIFO,CurrentFB);
Hwreg(rSNDBC,CurrentBC); // Flip FIFOs back
L7_ON // Scope pulse
Hwreg(rHIRQ,bmHXFRDNIRQ); // Clear the IRQ
Hwreg(rHXFR,(tokOUT | ep)); // Launch an OUT1 transfer
L7_OFF
}
}
else if (dum == hrACK)
{
Available_Buffers += 1;
NAKct = 0;
Transfer_In_Progress = 0; // Finished this transfer
if (Available_Buffers != 2) // Still some data to send
{
CurrentBC = PendingBC;
CurrentFB = PendingFB;
Hwreg(rSNDBC,CurrentBC);
L7_ON // Scope pulse
Hwreg(rHIRQ,bmHXFRDNIRQ); // Clear the IRQ
Hwreg(rHXFR,(tokOUT | ep)); // Launch an OUT1 transfer
L7_OFF
}
}
else return(dum);
}
while(Transfer_In_Progress);
}
while(Available_Buffers!=2); // Go until both buffers are available (have been sent)
return(0);
}
全部0条评论
快来发表一下你的评论吧 !