📄 eth97j60.c
字号:
if(TxBuffers[i].Flags.bFree)
{
return TRUE;
}
}
return FALSE;
#endif
}
BUFFER MACGetTxBuffer(BOOL HighPriority)
{
#if MAC_TX_BUFFER_COUNT > 1
BUFFER i;
if(HighPriority)
#endif
{
return !ECON1bits.TXRTS ? 0 : INVALID_BUFFER;
}
#if MAC_TX_BUFFER_COUNT > 1
// Find a free buffer
for(i = 1; i < MAC_TX_BUFFER_COUNT; i++)
{
// If this buffer is free, then mark it as used and return with it
if(TxBuffers[i].Flags.bFree)
{
TxBuffers[i].Flags.bFree = FALSE;
TxBuffers[i].Flags.bTransmitted = FALSE;
return i;
}
}
return INVALID_BUFFER;
#endif
}
void MACDiscardTx(BUFFER buffer)
{
#if MAC_TX_BUFFER_COUNT > 1
if(buffer <= MAC_TX_BUFFER_COUNT)
{
TxBuffers[buffer].Flags.bFree = TRUE;
CurrentTxBuffer = buffer;
}
#endif
}
/******************************************************************************
* Function: void MACDiscardRx(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: Marks the last received packet (obtained using
* MACGetHeader())as being processed and frees the buffer
* memory associated with it
*
* Note: None
*****************************************************************************/
void MACDiscardRx(void)
{
WORD_VAL NewRXRDLocation;
// Make sure the current packet was not already discarded
if( WasDiscarded )
return;
WasDiscarded = TRUE;
// Decrement the next packet pointer before writing it into
// the ERXRDPT registers. This is a silicon errata workaround.
// RX buffer wrapping must be taken into account if the
// NextPacketLocation is precisely RXSTART.
NewRXRDLocation.Val = NextPacketLocation.Val - 1;
#if RXSTART == 0
if(NewRXRDLocation.Val > RXSTOP)
#else
if(NewRXRDLocation.Val < RXSTART || NewRXRDLocation.Val > RXSTOP)
#endif
{
NewRXRDLocation.Val = RXSTOP;
}
// Decrement the RX packet counter register, EPKTCNT
ECON2bits.PKTDEC = 1;
// Move the receive read pointer to unwrite-protect the memory used by the
// last packet. The writing order is important: set the low byte first,
// high byte last.
ERXRDPTL = NewRXRDLocation.v[0];
ERXRDPTH = NewRXRDLocation.v[1];
// Clearing the PKTIF flag should automatically be done by hardware, but
// the current silicon version requires that you manually clear it.
// Clear the packet pending interrupt bit (if we can: EPKTCNT must be 0). This
// must not occur immediately after setting ECON2bits.PKTDEC (at least one NOP
// or other instruction must execute first).
EIRbits.PKTIF = 0;
}
/******************************************************************************
* Function: WORD MACGetFreeRxSize(void)
*
* PreCondition: None
*
* Input: None
*
* Output: A WORD estimate of how much RX buffer space is free at
* the present time.
*
* Side Effects: None
*
* Overview: None
*
* Note: None
*****************************************************************************/
WORD MACGetFreeRxSize(void)
{
WORD_VAL ReadPT, WritePT;
// Read the Ethernet hardware buffer write pointer. Because packets can be
// received at any time, it can change between reading the low and high
// bytes. A loop is necessary to make certain a proper low/high byte pair
// is read.
do {
// Save EPKTCNT in a temporary location
ReadPT.v[0] = EPKTCNT;
WritePT.Val = ERXWRPT;
} while(EPKTCNT != ReadPT.v[0]);
// Determine where the write protection pointer is
ReadPT.Val = ERXRDPT;
// Calculate the difference between the pointers, taking care to account
// for buffer wrapping conditions
if ( WritePT.Val > ReadPT.Val )
{
return (RXSTOP - RXSTART) - (WritePT.Val - ReadPT.Val);
}
else if ( WritePT.Val == ReadPT.Val )
{
return RXSIZE - 1;
}
else
{
return ReadPT.Val - WritePT.Val - 1;
}
}
/******************************************************************************
* Function: BOOL MACGetHeader(MAC_ADDR *remote, BYTE* type)
*
* PreCondition: None
*
* Input: *remote: Location to store the Source MAC address of the
* received frame.
* *type: Location of a BYTE to store the constant
* MAC_UNKNOWN, ETHER_IP, or ETHER_ARP, representing
* the contents of the Ethernet type field.
*
* Output: TRUE: If a packet was waiting in the RX buffer. The
* remote, and type values are updated.
* FALSE: If a packet was not pending. remote and type are
* not changed.
*
* Side Effects: Last packet is discarded if MACDiscardRx() hasn't already
* been called.
*
* Overview: None
*
* Note: None
*****************************************************************************/
BOOL MACGetHeader(MAC_ADDR *remote, BYTE* type)
{
ENC_PREAMBLE header;
// Test if at least one packet has been received and is waiting
if(EPKTCNT == 0)
{
return FALSE;
}
// Make absolutely certain that any previous packet was discarded
if(WasDiscarded == FALSE)
{
MACDiscardRx();
return FALSE;
}
// Save the location of this packet
CurrentPacketLocation.Val = NextPacketLocation.Val;
// Set the read pointer to the beginning of the next unprocessed packet
ERDPT = NextPacketLocation.Val;
// Obtain the MAC header from the Ethernet buffer
MACGetArray((BYTE*)&header, sizeof(header));
// The EtherType field, like most items transmitted on the Ethernet medium
// are in big endian.
header.Type.Val = swaps(header.Type.Val);
if(header.NextPacketPointer > RXSTOP || ((BYTE_VAL*)(&header.NextPacketPointer))->bits.b0 ||
header.StatusVector.bits.Zero ||
header.StatusVector.bits.CRCError ||
header.StatusVector.bits.ByteCount > 1518 ||
!header.StatusVector.bits.ReceiveOk)
{
Reset();
}
// Save the location where the hardware will write the next packet to
NextPacketLocation.Val = header.NextPacketPointer;
// Return the Ethernet frame's Source MAC address field to the caller
// This parameter is useful for replying to requests without requiring an
// ARP cycle.
memcpy((void*)remote->v, (void*)header.SourceMACAddr.v, sizeof(*remote));
// Return a simplified version of the EtherType field to the caller
*type = MAC_UNKNOWN;
if( (header.Type.v[1] == 0x08u) &&
((header.Type.v[0] == ETHER_IP) || (header.Type.v[0] == ETHER_ARP)) )
{
*type = header.Type.v[0];
}
// Mark this packet as discardable
WasDiscarded = FALSE;
return TRUE;
}
/******************************************************************************
* Function: void MACPutHeader(MAC_ADDR *remote,
* BYTE type,
* WORD dataLen)
*
* PreCondition: MACIsTxReady() must return TRUE.
*
* Input: *remote: Pointer to memory which contains the destination
* MAC address (6 bytes)
* type: The constant ETHER_ARP or ETHER_IP, defining which
* value to write into the Ethernet header's type field.
* dataLen: Length of the Ethernet data payload
*
* Output: None
*
* Side Effects: None
*
* Overview: None
*
* Note: Because of the dataLen parameter, it is probably
* advantagous to call this function immediately before
* transmitting a packet rather than initially when the
* packet is first created. The order in which the packet
* is constructed (header first or data first) is not
* important.
*****************************************************************************/
void MACPutHeader(MAC_ADDR *remote,
BYTE type,
WORD dataLen)
{
#if MAC_TX_BUFFER_COUNT > 1
// Set the write pointer to the beginning of the transmit buffer
EWRPT = TxBuffers[CurrentTxBuffer].StartAddress.Val;
// Calculate where to put the TXND pointer
dataLen += (WORD)sizeof(ETHER_HEADER) + TxBuffers[CurrentTxBuffer].StartAddress.Val;
TxBuffers[CurrentTxBuffer].EndAddress.Val = dataLen;
#else
// Set the write pointer to the beginning of the transmit buffer
EWRPT = TXSTART;
// Calculate where to put the TXND pointer
dataLen += (WORD)sizeof(ETHER_HEADER) + TXSTART;
// Write the TXND pointer into the registers, given the dataLen given
ETXND = dataLen;
#endif
// Set the per-packet control byte and write the Ethernet destination
// address
MACPut(0x00); // Use default control configuration
MACPutArray((BYTE*)remote, sizeof(*remote));
// Write our MAC address in the Ethernet source field
MACPutArray((BYTE*)&AppConfig.MyMACAddr, sizeof(AppConfig.MyMACAddr));
// Write the appropriate Ethernet Type WORD for the protocol being used
MACPut(0x08);
MACPut((type == MAC_IP) ? ETHER_IP : ETHER_ARP);
}
/******************************************************************************
* Function: void MACFlush(void)
*
* PreCondition: A packet has been created by calling MACPut() and
* MACPutHeader().
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: MACFlush causes the current TX packet to be sent out on
* the Ethernet medium. The hardware MAC will take control
* and handle CRC generation, collision retransmission and
* other details.
*
* Note: After transmission completes (MACIsTxReady() returns TRUE),
* the packet can be modified and transmitted again by calling
* MACFlush() again. Until MACPutHeader() or MACPut() is
* called (in the TX data area), the data in the TX buffer
* will not be corrupted.
*****************************************************************************/
void MACFlush(void)
{
#if MAC_TX_BUFFER_COUNT > 1
// Set the packet start and end address pointers
ETXST = TxBuffers[CurrentTxBuffer].StartAddress.Val;
ETXND = TxBuffers[CurrentTxBuffer].EndAddress.Val;
LastTXedBuffer = CurrentTxBuffer;
TxBuffers[CurrentTxBuffer].Flags.bTransmitted = TRUE;
#endif
// Reset transmit logic if a TX Error has previously occured
// This may be unnecessary.
if(EIRbits.TXERIF)
{
EIRbits.TXERIF = 0;
ECON1bits.TXRST = 1;
ECON1bits.TXRST = 0;
}
// Start the transmission
// After transmission completes (MACIsTxReady() returns TRUE), the packet
// can be modified and transmitted again by calling MACFlush() again.
// Until MACPutHeader() is called, the data in the TX buffer will not be
// corrupted.
ECON1bits.TXRTS = 1;
}
/******************************************************************************
* Function: void MACSetRxBuffer(WORD offset)
*
* PreCondition: A packet has been obtained by calling MACGetHeader() and
* getting a TRUE result.
*
* Input: offset: WORD specifying how many bytes beyond the Ethernet
* header's type field to relocate the SPI read and
* write pointers.
*
* Output: None
*
* Side Effects: None
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -