帖一下代码: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;
/* 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;
}
}
下面是初始化以太网控制器的代码:
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;
// 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<255;i++)
{;}
// After the EthernetInitExpClk is called, call this function
// to configure the Ethernet peripheral.
EthernetConfigSet(ETH_BASE,
(ETH_CFG_TX_DPLXEN | // Full duplex transmit mode
ETH_CFG_TX_PADEN | // Padding of transmit data to
// minimum size
ETH_CFG_TX_CRCEN)); // Auto CRC generation
/* Wait for the link to become active.*/
ulEthernetStatus = EthernetPHYRead(ETH_BASE, PHY_MR1);
while((ulEthernetStatus & 0x0004) == 0)
{
ulEthernetStatus = EthernetPHYRead(ETH_BASE, PHY_MR1);
}
/* Enable Ethernet peripheral after configurations are done.*/
EthernetEnable(ETH_BASE);
/* Set MAC Address
The MAC address is stored in the Flash of the ARM.
Read the flash and set the MAC address byte by byte.
*/
/* Read the Flash */
FlashUserGet(&ulUser0, &ulUser1);
/* Set where the MAC is stored in the buffer*/
pucLocalMACAddr=(unsigned char*)pucSendBuffer+6;
*(pucLocalMACAddr + 0) = ((ulUser0 >> 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);