When performing manual initialisation of lwIP for use with the raw API, a variety of lwIP functions need to be called. Th exact functions will depend on the intended configuration, but the following provides an example based on a single Ethernet netif, UDP, TCP, IPv4 and DHCP.
In this example, these functions must be called in the order of appearance:
stats_init()Clears the structure where runtime statistics are gathered.
sys_init()Not generally used with raw API, but can be called for ease of compatibility if using sequential API in addition, initialised manually.
mem_init()Initializes the dynamic memory heap defined by the CDL
configuration option CYGNUM_LWIP_MEM_SIZE.
memp_init()Initializes the memory pools defined by the CDL
configuration options CYGNUM_LWIP_MEMP_NUM_*.
pbuf_init()Initializes the pbuf memory pool defined by the CDL
configuration option CYGNUM_LWIP_PBUF_POOL_SIZE.
etharp_init()Initializes the ARP table and queue.
Note: you must call etharp_tmr at a ARP_TMR_INTERVAL
(by default, 5 seconds) regular interval after this initialization.
ip_init()Doesn't do much at present - it should be called to handle future changes.
udp_init()Clears the UDP PCB list.
tcp_init()Clears the TCP PCB list and clears some internal TCP timers.
Note: as mentioned
earlier, you must call tcp_fasttmr() and
tcp_slowtmr() at the predefined regular intervals
after this initialization.
struct netif *netif_add(struct netif *netif, struct ip_addr *ipaddr,
struct ip_addr *netmask, struct ip_addr *gw,
void *state, err_t (* init)(struct netif *netif),
err_t (* input)(struct pbuf *p, struct netif *netif))Adds your network interface to the netif_list. Allocate
a struct netif and pass a pointer to this structure
as the first argument. Give pointers to cleared struct ip_addr
structures when using DHCP, or fill them with sane numbers otherwise. The state
pointer may be NULL.
The init function pointer must point to a initialization function for your ethernet netif interface. The following code illustrates it's use:
err_t netif_if_init(struct netif *netif)
{
u8_t i;
for(i = 0; i < 6; i++)
netif->hwaddr[i] = some_eth_addr[i];
init_my_eth_device();
return ERR_OK;
}
|
The input function pointer must point to the lwIP function
ip_input().
netif_set_default(struct netif *netif)Registers netif as the default network interface.
netif_set_up(struct netif *netif)When netif is fully configured, this function must be called to allow it to be used.
dhcp_start(struct netif *netif)Creates a new DHCP client for this interface on the first call.
Note: you must call dhcp_fine_tmr() and
dhcp_coarse_tmr() at
the predefined regular intervals after starting the client.
You can peek in the netif->dhcp struct for the actual DHCP status.