⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 enc28j60.c

📁 Taiwan sunplus develop spce3200, it is a test program ----- testboard source code
💻 C
📖 第 1 页 / 共 5 页
字号:
 *
 * Side Effects:	None
 *
 * Overview:		Returns the ECON1.TXRTS bit.
 *
 * Note:			None
 *****************************************************************************/
BOOL MACIsTxReady(void)
{
    return !ReadETHReg(ECON1).ECON1bits.TXRTS;
}


void MACReserveTxBuffer(BUFFER buffer)
{
#if MAC_TX_BUFFER_COUNT > 1
    TxBuffers[buffer].bFree = FALSE;
#endif
}


void MACDiscardTx(BUFFER buffer)
{
#if MAC_TX_BUFFER_COUNT > 1
    TxBuffers[buffer].bFree = TRUE;
    NICCurrentTxBuffer = 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
 *                  Free the receiving buffer
 *
 * 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;
	
 
 //-------added by lfei 2007/01/30----
 #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);

	//Process the received package, and then modify ERXRDPT pointer to free buffer
	WriteReg(ERXRDPTL, LSB(NewRXRDLocation));
	WriteReg(ERXRDPTH, MSB(NewRXRDLocation));
}


/******************************************************************************
 * 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(EPKTCNT).Val;
	
		BankSel(ERXRDPTL);
		WritePT.v[0] = ReadETHReg(ERXWRPTL).Val;
		WritePT.v[1] = ReadETHReg(ERXWRPTH).Val;
	
		BankSel(EPKTCNT);
	} while(ReadETHReg(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, unsigned char* type)
 *
 * PreCondition:	None
 *
 * Input:			*remote: Location to store the Source MAC address of the received frame.
 *					*type: Location of a unsigned char 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, unsigned char* type)
{
    MCP_PREAMBLE header;

	// Test if at least one packet has been received and is waiting
	BankSel(EPKTCNT);

	if( ReadETHReg(EPKTCNT).Val == 0 )
		return FALSE;	

	// Make absolutely certain that any previous packet was discarded	
	if( WasDiscarded == FALSE)
		MACDiscardRx();

	// Save the location of this packet
	CurrentPacketLocation.Val = NextPacketLocation.Val; 		// NextPacketLocation.Val = 0; added by lfei

	// Set the SPI read pointer to the beginning of the next unprocessed packet
	BankSel(ERDPTL);
	WriteReg(ERDPTL, LSB(NextPacketLocation));
	WriteReg(ERDPTH, MSB(NextPacketLocation));

	// Obtain the MAC header from Ethernet buffer

	MACGetArray((unsigned char*)&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);

	// 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] == 0x08) && 
    	((header.Type.v[0] == ETHER_IP) || (header.Type.v[0] == ETHER_ARP)) )
    	*type = header.Type.v[0];
           
    WasDiscarded = FALSE;										// Mark this packet as discardable
	return TRUE;
}


/******************************************************************************
 * Function:		void    MACPutHeader(MAC_ADDR *remote,
 *					unsigned char 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,
                     unsigned char 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, ((WORD_VAL*)&TxBuffers[NICCurrentTxBuffer].StartAddress->v[0]));
	WriteReg(EWRPTH, ((WORD_VAL*)&TxBuffers[NICCurrentTxBuffer].StartAddress->v[1]));

	// Calculate where to put the TXND pointer
    dataLen += (WORD)sizeof(ETHER_HEADER) + TxBuffers[NICCurrentTxBuffer].StartAddress;
#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;
#endif

	// 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]);

	// Set the per-packet control byte and write the Ethernet destination 
	// address
	MACPut(0x00);	// Use default control configuration
    MACPutArray((unsigned char*)remote, sizeof(*remote));

	// Write our MAC address in the Ethernet source field
    MACPut(MY_MAC_BYTE1);
    MACPut(MY_MAC_BYTE2);
    MACPut(MY_MAC_BYTE3);
    MACPut(MY_MAC_BYTE4);
    MACPut(MY_MAC_BYTE5);
    MACPut(MY_MAC_BYTE6);

	// 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)
{
/***added by lfei 2007/02/05********************************/
/*    function:	replace the commented segment                              */
#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;
			TXSTATUS TXStatus;
			unsigned char i;
			
			// Cancel the previous transmission if it has become stuck set
			BFCReg(ECON1, ECON1_TXRTS);

			// Save the current read pointer (controlled by application)
			BankSel(ERDPTL);
			ReadPtrSave.v[0] = ReadETHReg(ERDPTL).Val;
			ReadPtrSave.v[1] = ReadETHReg(ERDPTH).Val;

			// Get the location of the transmit status vector
			TXEnd.v[0] = ReadETHReg(ETXNDL).Val;
			TXEnd.v[1] = ReadETHReg(ETXNDH).Val;
			TXEnd.Val++;
			
			// Read the transmit status vector
			WriteReg(ERDPTL, TXEnd.v[0]);
			WriteReg(ERDPTH, TXEnd.v[1]);
			MACGetArray((unsigned char*)&TXStatus, sizeof(TXStatus));

			// Implement retransmission if a late collision occured (this can 
			// happen on B5 when certain link pulses arrive at the same time 
			// as the transmission)
			for(i = 0; i < 16; i++)
			{
				if(ReadETHReg(EIR).EIRbits.TXERIF && TXStatus.bits.LateCollision)
				{
					// Reset the TX logic
					BFSReg(ECON1, ECON1_TXRST);
					BFCReg(ECON1, ECON1_TXRST);
					BFCReg(EIR, EIR_TXERIF | EIR_TXIF);

					// Transmit the packet again
					BFSReg(ECON1, ECON1_TXRTS);
					while(!(ReadETHReg(EIR).Val & (EIR_TXERIF | EIR_TXIF)));

					// Cancel the previous transmission if it has become stuck set
					BFCReg(ECON1, ECON1_TXRTS);

					// Read transmit status vector
					WriteReg(ERDPTL, TXEnd.v[0]);
					WriteReg(ERDPTH, TXEnd.v[1]);
					MACGetArray((unsigned char*)&TXStatus, sizeof(TXStatus));
				}
				else
				{
					break;
				}
			}

			// Restore the current read pointer
			WriteReg(ERDPTL, ReadPtrSave.v[0]);
			WriteReg(ERDPTH, ReadPtrSave.v[1]);
		}
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -