📄 enc28j60.c
字号:
{
return !ReadETHReg(ECON1).ECON1bits.TXRTS;
}
#if MAC_TX_BUFFER_COUNT > 1
// Check if the current buffer can be modified. It cannot be modified if
// the TX hardware is currently transmitting it.
if(CurrentTxBuffer == LastTXedBuffer)
{
return !ReadETHReg(ECON1).ECON1bits.TXRTS;
}
// Check if a buffer is available for a new packet
for(i = 1; i < MAC_TX_BUFFER_COUNT; i++)
{
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 !ReadETHReg(ECON1).ECON1bits.TXRTS ? 0 : INVALID_BUFFER;
}
#if MAC_TX_BUFFER_COUNT > 1
// Find a free buffer. Do not use buffer 0, it is reserved for
// high priority messages that don't need to be acknowledged
// before being discarded (TCP control packets, all ICMP
// packets, all UDP packets, etc.)
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 < sizeof(TxBuffers)/sizeof(TxBuffers[0]))
{
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
BFSReg(ECON2, ECON2_PKTDEC);
// 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.
BankSel(ERXRDPTL);
WriteReg(ERXRDPTL, NewRXRDLocation.v[0]);
WriteReg(ERXRDPTH, NewRXRDLocation.v[1]);
}
/******************************************************************************
* 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.
BankSel(EPKTCNT);
do {
// Save EPKTCNT in a temporary location
ReadPT.v[0] = ReadETHReg((BYTE)EPKTCNT).Val;
BankSel(ERXWRPTL);
WritePT.v[0] = ReadETHReg(ERXWRPTL).Val;
WritePT.v[1] = ReadETHReg(ERXWRPTH).Val;
BankSel(EPKTCNT);
} while(ReadETHReg((BYTE)EPKTCNT).Val != ReadPT.v[0]);
// Determine where the write protection pointer is
BankSel(ERXRDPTL);
ReadPT.v[0] = ReadETHReg(ERXRDPTL).Val;
ReadPT.v[1] = ReadETHReg(ERXRDPTH).Val;
// 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
BankSel(EPKTCNT);
if(ReadETHReg((BYTE)EPKTCNT).Val == 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 SPI read pointer to the beginning of the next unprocessed packet
BankSel(ERDPTL);
WriteReg(ERDPTL, NextPacketLocation.v[0]);
WriteReg(ERDPTH, NextPacketLocation.v[1]);
// 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);
// Validate the data returned from the ENC28J60. Random data corruption,
// such as if a single SPI bit error occurs while communicating or a
// momentary power glitch could cause this to occur in rare circumstances.
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)
{
BankSel(EWRPTL);
#if MAC_TX_BUFFER_COUNT > 1
// Set the SPI write pointer to the beginning of the transmit buffer
WriteReg(EWRPTL, TxBuffers[CurrentTxBuffer].StartAddress.v[0]);
WriteReg(EWRPTH, TxBuffers[CurrentTxBuffer].StartAddress.v[1]);
// Calculate where to put the TXND pointer
dataLen += (WORD)sizeof(ETHER_HEADER) + TxBuffers[CurrentTxBuffer].StartAddress.Val;
TxBuffers[CurrentTxBuffer].EndAddress.Val = dataLen;
#else
// Set the SPI write pointer to the beginning of the transmit buffer
WriteReg(EWRPTL, LOW(TXSTART));
WriteReg(EWRPTH, HIGH(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
WriteReg(ETXNDL, ((WORD_VAL*)&dataLen)->v[0]);
WriteReg(ETXNDH, ((WORD_VAL*)&dataLen)->v[1]);
#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
BankSel(ETXSTL);
WriteReg(ETXSTL, TxBuffers[CurrentTxBuffer].StartAddress.v[0]);
WriteReg(ETXSTH, TxBuffers[CurrentTxBuffer].StartAddress.v[1]);
WriteReg(ETXNDL, TxBuffers[CurrentTxBuffer].EndAddress.v[0]);
WriteReg(ETXNDH, TxBuffers[CurrentTxBuffer].EndAddress.v[1]);
LastTXedBuffer = CurrentTxBuffer;
TxBuffers[CurrentTxBuffer].Flags.bTransmitted = TRUE;
#endif
// Reset transmit logic if a TX Error has previously occured
// This is a silicon errata workaround
if(ReadETHReg(EIR).EIRbits.TXERIF)
{
BFSReg(ECON1, ECON1_TXRST);
BFCReg(ECON1, ECON1_TXRST);
}
BFCReg(EIR, EIR_TXERIF | EIR_TXIF);
// 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.
BFSReg(ECON1, ECON1_TXRTS);
// Revision B5 silicon errata workaround
if(ENCRevID == 0x05)
{
while(!(ReadETHReg(EIR).Val & (EIR_TXERIF | EIR_TXIF)));
if(ReadETHReg(EIR).EIRbits.TXERIF)
{
WORD_VAL ReadPtrSave;
WORD_VAL TXEnd;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -