下面是初始化以太网控制器的代码:
void Ethernet_Init()
{
// The variables to read the MAC address from the flash
unsigned long ulUser0, ulUser1;
unsigned long i;
// #ulIntStatus and ulEthernetStatus# are variables to record the Ethernet
// information when ethernet control is starting.
unsigned long ulIntStatus,ulEthernetStatus;
// Reset Ethernet
SysCtlPeripheralReset(SYSCTL_PERIPH_ETH);
// Enable relative peripherals.
SysCtlPeripheralEnable(SYSCTL_PERIPH_ETH); // Enable NIC
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); // Enable Ethernet LED(GPIO F)
SysCtlPeripheralReset(SYSCTL_PERIPH_ETH); // Reset NIC
// Enable Port F for Ethernet LEDs.
// LED0 Bit 3 Output
// LED1 Bit 2 Output
GPIODirModeSet( GPIO_PORTF_BASE, //
(GPIO_PIN_2 | GPIO_PIN_3), //
GPIO_DIR_MODE_HW); //
// Port F Set
GPIOPadConfigSet(GPIO_PORTF_BASE,
(GPIO_PIN_2 | GPIO_PIN_3),
GPIO_STRENGTH_2MA,
GPIO_PIN_TYPE_STD);
// Disable Ethernet. Ethernet interrupt should be disabled
// when being configured.
EthernetIntDisable(ETH_BASE, ETH_INT_ALL); // Disable Interrupt
ulIntStatus = EthernetIntStatus(ETH_BASE, false);
EthernetIntClear(ETH_BASE, ulIntStatus);
EthernetDisable(ETH_BASE); // Disable Ethernet
// The Function be called FIRST, BEFORE any other Eth-API
EthernetInitExpClk(ETH_BASE,
SysCtlClockGet()); // Ethernet clk = processor clk
// Delay
for(i=0;i> 0) & 0xff);
*(pucLocalMACAddr + 1) = ((ulUser0 >> 8) & 0xff);
*(pucLocalMACAddr + 2) = ((ulUser0 >> 16) & 0xff);
*(pucLocalMACAddr + 3) = ((ulUser1 >> 0) & 0xff);
*(pucLocalMACAddr + 4) = ((ulUser1 >> 8) & 0xff);
*(pucLocalMACAddr + 5) = ((ulUser1 >> 16) & 0xff);
/* Destination MAC address is in the head of send buffer*/
pucDestMACAddr=pucSendBuffer+0;
/* Set MAC address*/
EthernetMACAddrSet(ETH_BASE, pucLocalMACAddr);
/* Enable interrupt of Ethernet controller*/
IntEnable(INT_ETH);
/* The Ethernet IntPriority is low*/
IntPrioritySet(INT_ETH, 0xFF);
/* Enable Ethernet receiving interrupt*/
EthernetIntEnable(ETH_BASE, ETH_INT_RX);
/* Initialize the Receive Buffer*/
pucReceiveBuffer[0]=0;
}
帖一下代码:IP层
unsigned char
IPv4DatagramSend(unsigned char* pucDestIP,
unsigned char ucUpperProtocol,
unsigned short usDatagramLen)
{
ARPTableEntry* DestAddrPTR;
EthFrameHead* EthSendFrameHeadPTR;
/*Search the ARP Table for the MAC address*/
DestAddrPTR=pucARPEntry(pucDestIP);
if (DestAddrPTR==NULL)
{
/* If no IP address is found, send a ARP request and return 0*/
ARPPacketOut(pucDestIP,
pucBroadcastMACAddr,
ARP_REQUEST);
return 0;
}
else/* Find the IP address in the ARP Table and send the datagram*/
{
/* Ethernet Frame Header Set*/
EthSendFrameHeadPTR=(EthFrameHead *)(&(pucSendBuffer[0]));
memcpy(EthSendFrameHeadPTR->pucDestMACAddr,
DestAddrPTR->pucMACAddr,6);
/* Source MAC is in the Frame.*/
/* Upper protocol is IPv4 protocol*/
EthSendFrameHeadPTR->usUpperProtocol=RFC894_IPv4;
/* IP Datagram header Set*/
/* 1 version and header length*/
SENDIPv4HeaderPTR->ucVerHL=IPv4_nOP;
/* 2 Type of Service */
SENDIPv4HeaderPTR->ucTOS=DEFAULT_TOS_SETTING;
/* Datagram Length (IPv4 datagram Header included)*/
if (usDatagramLen+IPV4_HEADER_LENusDataLen=usDatagramLen+IPV4_HEADER_LEN;
#endif
#ifdef Big_Endian
SENDIPv4HeaderPTR->usDataLen=ByteReverseShort(usDatagramLen+IPV4_HEADER_LEN);
#endif
// Identification
usIPID++;
SENDIPv4HeaderPTR->usId=usIPID;
/* Flag Fragment offset
fragment when necessary
Fragment offset is zero*/
SENDIPv4HeaderPTR->usFlagFragment=0x00;
/* TTL hops=128 in this system */
SENDIPv4HeaderPTR->ucTTL=128;
/* Upper Protocol*/
SENDIPv4HeaderPTR->ucUpperProtocol=ucUpperProtocol;
/* Source IP address set:pucSourceIPAddr*/
memcpy(SENDIPv4HeaderPTR->pucSourceIPAddr,pucHostIPAddr,4);
/* Destination IP address set:pucDestIPAddr*/
memcpy(SENDIPv4HeaderPTR->pucDestIPAddr,pucDestIP,4);
/* Header checksum*/
IPv4HeaderChecksumWrite(SENDIPv4HeaderPTR);
/* Compute the length of the data to be transmitted*/
ulTxLen=(unsigned long)usDatagramLen
+(unsigned long)IPV4_HEADER_LEN
+(unsigned long)ETH_HEADER_LEN;
EthernetPacketPut(ETH_BASE,pucSendBuffer,(long)ulTxLen);
ulTxLen=0;
return 1;
}
}