📄 lwipstack.c
字号:
while ((iBuf + 4) <= q->len)
{
HWREG(ETH_BASE + MAC_O_DATA) = *pulBuf++;
iBuf += 4;
}
/**
* Check if leftover data in the pbuf and save it in the gather
* buffer for the next time.
*
*/
while (iBuf < q->len)
{
/* Copy a byte from the pbuf into the gather buffer. */
pucGather[iGather] = pucBuf[iBuf++];
/* Increment the gather buffer index modulo 4. */
iGather = ((iGather + 1) % 4);
}
}
/* Send any leftover data to the FIFO. */
HWREG(ETH_BASE + MAC_O_DATA) = ulGather;
/* Wakeup the transmitter. */
HWREG(ETH_BASE + MAC_O_TR) = MAC_TR_NEWTX;
LWIP_DEBUGF(NETIF_DEBUG, ("low_level_transmit: frame sent\n"));
LINK_STATS_INC(link.xmit);
return (ERR_OK);
}
/**
* This function is passed to netif_add
*
* Should be called at the beginning of the program to set up the
* network interface. It calls the function low_level_init() to do the
* actual setup of the hardware.
*
* This function should be passed as a parameter to netif_add().
*
* @param netif the lwip network interface structure for this ethernetif
* @return ERR_OK if the loopif is initialized
* ERR_MEM if private data couldn't be allocated
* any other err_t on error
*/
err_t ethernetif_init(struct netif *netif)
{
LWIP_ASSERT("netif != NULL", (netif != NULL));
#if LWIP_NETIF_HOSTNAME
/* Initialize interface hostname */
netif->hostname = "lwip";
#endif /* LWIP_NETIF_HOSTNAME */
/*
* Initialize the snmp variables and counters inside the struct netif.
* The last argument should be replaced with your link speed, in units
* of bits per second.
*/
NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, ETH_DEFAULT_LINK_SPEED);
netif->name[0] = IFNAME0;
netif->name[1] = IFNAME1;
/* We directly use etharp_output() here to save a function call.
* You can instead declare your own function an call etharp_output()
* from it if you have to do some checks before sending (e.g. if link
* is available...)
*/
netif->output = etharp_output;
netif->linkoutput = low_level_output;
return low_level_init(netif);
}
//*****************************************************************************
//
//! Initializes the lwIP TCP/IP stack.
//! Call it from your main to initialize the lwip
//!
//! \param pucMAC is a pointer to a six byte array containing the MAC
//! address to be used for the interface.
//! \param ulIPAddr is the IP address to be used (static).
//! \param ulNetMask is the network mask to be used (static).
//! \param ulGWAddr is the Gateway address to be used (static).
//! \param ulIPMode is the IP Address Mode. \b IPADDR_USE_STATIC will force
//! static IP addressing to be used, \b IPADDR_USE_DHCP will force DHCP with
//! fallback to Link Local (Auto IP), while \b IPADDR_USE_AUTOIP will force
//! Link Local only.
//!
//! This function performs initialization of the lwIP TCP/IP stack for the
//! Stellaris Ethernet MAC, including DHCP and/or AutoIP, as configured.
//!
//! \return None.
//
//*****************************************************************************
void LWIPServiceTaskInit(void *pvParameters)
{
struct ip_addr ip_addr;
struct ip_addr net_mask;
struct ip_addr gw_addr;
//yxh LWIP_ASSERT("pvParameters != NULL", (pvParameters != NULL));
IP_CONFIG * ipCfg = (IP_CONFIG *)pvParameters;
// Check the parameters.
#if LWIP_DHCP && LWIP_AUTOIP
ASSERT((ipCfg->IPMode == IPADDR_USE_STATIC) ||
(ipCfg->IPMode == IPADDR_USE_DHCP) ||
(ipCfg->IPMode == IPADDR_USE_AUTOIP))
#elif LWIP_DHCP
ASSERT((ipCfg->IPMode == IPADDR_USE_STATIC) ||
(ipCfg->IPMode == IPADDR_USE_DHCP))
#elif LWIP_AUTOIP
ASSERT((ipCfg->IPMode == IPADDR_USE_STATIC) ||
(ipCfg->IPMode == IPADDR_USE_AUTOIP))
#else
ASSERT(ipCfg->IPMode == IPADDR_USE_STATIC)
#endif
// Start the TCP/IP thread & init stuff
tcpip_init(NULL, NULL);
vTaskDelay(100 / portTICK_RATE_MS);
// Setup the network address values.
if (ipCfg->IPMode == IPADDR_USE_STATIC)
{
ip_addr.addr = htonl(ipCfg->IPAddr);
net_mask.addr = htonl(ipCfg->NetMask);
gw_addr.addr = htonl(ipCfg->GWAddr);
}
#if LWIP_DHCP || LWIP_AUTOIP
else
{
ip_addr.addr = 0;
net_mask.addr = 0;
gw_addr.addr = 0;
}
#endif
// Create, configure and add the Ethernet controller interface with
// default settings.
// WARNING: This must only be run after the OS has been started.
// Typically this is the case, however, if not, you must place this
// in a post-OS initialization
// @SEE http://lwip.wikia.com/wiki/Initialization_using_tcpip.c
netif_add(&lwip_netif, &ip_addr, &net_mask, &gw_addr, NULL, ethernetif_init, tcpip_input);
netif_set_default(&lwip_netif);
// Start DHCP, if enabled.
#if LWIP_DHCP
if (ipCfg->IPMode == IPADDR_USE_DHCP)
{
LWIP_DEBUGF(DHCP_DEBUG, ("----- Starting DHCP client -----\n"));
dhcp_start(&lwip_netif);
}
#endif
// Start AutoIP, if enabled and DHCP is not.
#if LWIP_AUTOIP
if (ipCfg->IPMode == IPADDR_USE_AUTOIP)
{
autoip_start(&lwip_netif);
}
#endif
if (ipCfg->IPMode == IPADDR_USE_STATIC)
{
// Bring the interface up.
netif_set_up(&lwip_netif);
}
vTaskDelay(1000/portTICK_RATE_MS);
while (0 == netif_is_up(&lwip_netif))
{
vTaskDelay(5000/portTICK_RATE_MS);
/*if(0 == netif_is_up(&lwip_netif))
{
dhcp_renew(&lwip_netif);
}*/
}
/* Initialize HTTP */
httpd_init();
// Nothing else to do. No point hanging around.
vTaskDelete( NULL);
}
//*****************************************************************************
//
//! Returns the IP configuration for this interface.
//!
//! This function will read and return the currently assigned IP configuration for
//! the Stellaris Ethernet interface.
//!
//! \return ERR_OK if the interface is initialized
//! ERR_IFF if the interface is not initialized
//
//*****************************************************************************
err_t LWIPServiceTaskIPConfigGet(struct netif *netif, IP_CONFIG * ipCfg)
{
LWIP_ASSERT("netif != NULL", (netif != NULL));
if ((netif == NULL) || (!(netif_is_up(netif))))
{
return ERR_IF;
}
ipCfg->IPAddr = (unsigned long)netif->ip_addr.addr;
ipCfg->NetMask = (unsigned long)netif->netmask.addr;
ipCfg->GWAddr = (unsigned long)netif->gw.addr;
if (netif->flags & NETIF_FLAG_DHCP)
{
ipCfg->IPMode = IPADDR_USE_DHCP;
}
else
{
ipCfg->IPMode = IPADDR_USE_STATIC;
}
return ERR_OK;
}
//*****************************************************************************
//
//! Returns the local MAC/HW address for this interface.
//!
//! \param pucMAC is a pointer to an array of bytes used to store the MAC
//! address.
//!
//! This function will read the currently assigned MAC address into the array
//! passed in \e pucMAC.
//!
//! \return ERR_OK if the interface is initialized
//! ERR_IFF if the interface is not initialized
//
//*****************************************************************************
err_t LWIPServiceTaskMACGet(struct netif *netif, unsigned char *pucMAC)
{
LWIP_ASSERT("netif != NULL", (netif != NULL));
if (netif == NULL)
{
return ERR_IF;
}
memcpy(pucMAC, &(netif->hwaddr[0]), ETH_HWADDR_LEN);
return ERR_OK;
}
//*****************************************************************************
//
//! Change the configuration of the lwIP network interface.
//!
//! \param ulIPAddr is the new IP address to be used (static).
//! \param ulNetMask is the new network mask to be used (static).
//! \param ulGWAddr is the new Gateway address to be used (static).
//! \param ulIPMode is the IP Address Mode. \b IPADDR_USE_STATIC 0 will force
//! static IP addressing to be used, \b IPADDR_USE_DHCP will force DHCP with
//! fallback to Link Local (Auto IP), while \b IPADDR_USE_AUTOIP will force
//! Link Local only.
//!
//! This function will evaluate the new configuration data. If necessary, the
//! interface will be brought down, reconfigured, and then brought back up
//! with the new configuration.
//!
//! \return None.
//
//*****************************************************************************
void lwIPNetworkConfigChange(struct netif *netif, IP_CONFIG * ipCfg)
{
struct ip_addr ip_addr;
struct ip_addr net_mask;
struct ip_addr gw_addr;
IP_CONFIG currentIPConfig;
// Check the parameters.
#if LWIP_DHCP && LWIP_AUTOIP
ASSERT((ipCfg->IPMode == IPADDR_USE_STATIC) ||
(ipCfg->IPMode == IPADDR_USE_DHCP) ||
(ipCfg->IPMode == IPADDR_USE_AUTOIP))
#elif LWIP_DHCP
ASSERT((ipCfg->IPMode == IPADDR_USE_STATIC) ||
(ipCfg->IPMode == IPADDR_USE_DHCP))
#elif LWIP_AUTOIP
ASSERT((ipCfg->IPMode == IPADDR_USE_STATIC) ||
(ipCfg->IPMode == IPADDR_USE_AUTOIP))
#else
ASSERT(ipCfg->IPMode == IPADDR_USE_STATIC)
#endif
// Setup the network address values.
if(ipCfg->IPMode == IPADDR_USE_STATIC)
{
ip_addr.addr = htonl(ipCfg->IPAddr);
net_mask.addr = htonl(ipCfg->NetMask);
gw_addr.addr = htonl(ipCfg->GWAddr);
}
#if LWIP_DHCP || LWIP_AUTOIP
else
{
ip_addr.addr = 0;
net_mask.addr = 0;
gw_addr.addr = 0;
}
#endif
// Switch on the current IP Address Aquisition mode.
LWIPServiceTaskIPConfigGet(netif, ¤tIPConfig);
switch (currentIPConfig.IPMode)
{
// Static IP
case IPADDR_USE_STATIC:
{
// Set the new address parameters. This will change the address
// configuration in lwIP, and if necessary, will reset any links
// that are active. This is valid for all three modes.
//
netif_set_addr(netif, &ip_addr, &net_mask, &gw_addr);
// If we are going to DHCP mode, then start the DHCP server now.
#if LWIP_DHCP
if (ipCfg->IPMode == IPADDR_USE_DHCP)
{
dhcp_start(netif);
}
#endif
// If we are going to AutoIP mode, then start the AutoIP process
// now.
#if LWIP_AUTOIP
if (ipCfg->IPMode == IPADDR_USE_AUTOIP)
{
autoip_start(netif);
}
#endif
// And we're done.
break;
}
// DHCP (with AutoIP fallback).
#if LWIP_DHCP
case IPADDR_USE_DHCP:
{
//
// If we are going to static IP addressing, then disable DHCP and
// force the new static IP address.
//
if (ipCfg->IPMode == IPADDR_USE_STATIC)
{
dhcp_stop(netif);
// SEE bug http://savannah.nongnu.org/bugs/?22804
netif->flags &= ~NETIF_FLAG_DHCP;
netif_set_addr(netif, &ip_addr, &net_mask, &gw_addr);
}
// If we are going to AUTO IP addressing, then disable DHCP, set
// the default addresses, and start AutoIP.
#if LWIP_AUTOIP
else if (ipCfg->IPMode == IPADDR_USE_AUTOIP)
{
dhcp_stop(netif);
netif_set_addr(netif, &ip_addr, &net_mask, &gw_addr);
autoip_start(netif);
}
#endif
break;
}
#endif
// AUTOIP
#if LWIP_AUTOIP
case IPADDR_USE_AUTOIP:
{
//
// If we are going to static IP addressing, then disable AutoIP and
// force the new static IP address.
//
if (ulIPMode == IPADDR_USE_STATIC)
{
autoip_stop(netif);
netif_set_addr(netif, &ip_addr, &net_mask, &gw_addr);
}
//
// If we are going to DHCP addressing, then disable AutoIP, set the
// default addresses, and start dhcp.
//
#if LWIP_DHCP
else if (ulIPMode == IPADDR_USE_AUTOIP)
{
autoip_stop(netif);
netif_set_addr(netif, &ip_addr, &net_mask, &gw_addr);
dhcp_start(netif);
}
#endif
break;
}
#endif
}
}
#if NETIF_DEBUG
/* Print an IP header by using LWIP_DEBUGF
* @param p an IP packet, p->payload pointing to the IP header
*/
void
stellarisif_debug_print(struct pbuf *p)
{
struct eth_hdr *ethhdr = (struct eth_hdr *)p->payload;
u16_t *plen = (u16_t *)p->payload;
LWIP_DEBUGF(NETIF_DEBUG, ("ETH header:\n"));
LWIP_DEBUGF(NETIF_DEBUG, ("Packet Length:%5"U16_F" \n",*plen));
LWIP_DEBUGF(NETIF_DEBUG, ("Destination: %02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"\n",
ethhdr->dest.addr[0],
ethhdr->dest.addr[1],
ethhdr->dest.addr[2],
ethhdr->dest.addr[3],
ethhdr->dest.addr[4],
ethhdr->dest.addr[5]));
LWIP_DEBUGF(NETIF_DEBUG, ("Source: %02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"\n",
ethhdr->src.addr[0],
ethhdr->src.addr[1],
ethhdr->src.addr[2],
ethhdr->src.addr[3],
ethhdr->src.addr[4],
ethhdr->src.addr[5]));
LWIP_DEBUGF(NETIF_DEBUG, ("Packet Type:0x%04"U16_F" \n", ethhdr->type));
}
#endif /* NETIF_DEBUG */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -