void MX_LWIP_Init(void)
{
IP_ADDRESS[0] = 192;
IP_ADDRESS[1] = 168;
IP_ADDRESS[2] = 1;
IP_ADDRESS[3] = 179;
NETMASK_ADDRESS[0] = 255;
NETMASK_ADDRESS[1] = 255;
NETMASK_ADDRESS[2] = 255;
NETMASK_ADDRESS[3] = 0;
GATEWAY_ADDRESS[0] = 192;
GATEWAY_ADDRESS[1] = 168;
GATEWAY_ADDRESS[2] = 1;
GATEWAY_ADDRESS[3] = 1;
/* Initilialize the LwIP stack with RTOS */
tcpip_init( NULL, NULL );
/* IP addresses initialization without DHCP (IPv4) */
IP4_ADDR(&ipaddr, IP_ADDRESS[0], IP_ADDRESS[1], IP_ADDRESS[2], IP_ADDRESS[3]);
IP4_ADDR(&netmask, NETMASK_ADDRESS[0], NETMASK_ADDRESS[1] , NETMASK_ADDRESS[2], NETMASK_ADDRESS[3]);
IP4_ADDR(&gw, GATEWAY_ADDRESS[0], GATEWAY_ADDRESS[1], GATEWAY_ADDRESS[2], GATEWAY_ADDRESS[3]);
/* add the network interface (IPv4/IPv6) with RTOS */
netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, ðernetif_init, &tcpip_input);
ethernetif_init初始化函数在 netif_add()内部会被调用。
既: if (init(netif) != ERR_OK) {
return NULL;
}
/* Registers the default network interface */
netif_set_default(&gnetif);
if (netif_is_link_up(&gnetif))
{
/* When the netif is fully configured this function must be called /
netif_set_up(&gnetif);
}
else
{
/ When the netif link is down this function must be called */
netif_set_down(&gnetif);
}
/* Set the link callback function, this function is called on change of link status*/
netif_set_link_callback(&gnetif, ethernetif_update_config);
ethernetif_update_config()在 下面创建的线程中被调用
既:
osThreadDef(LinkThr, ethernetif_set_link, osPriorityNormal, 0, configMINIMAL_STACK_SIZE * 2);
osThreadCreate (osThread(LinkThr), &link_arg);
ethernetif_set_link该线程每隔一段时间去读取PHY芯片的参数(轮询PHY芯片),并根据轮询结果及当前链路状态去调用函数ethernetif_update_config()。既就是netif_set_link_up(struct netif *netif)函数里的 NETIF_LINK_CALLBACK(netif)函数;
/* create a binary semaphore used for informing ethernetif of frame reception */
osSemaphoreDef(Netif_SEM);
Netif_LinkSemaphore = osSemaphoreCreate(osSemaphore(Netif_SEM) , 1 );
link_arg.netif = &gnetif;
link_arg.semaphore = Netif_LinkSemaphore;
// osQUE_link_state = xQueueCreate(0x5,sizeof(&link_arg.netif));
/* Create the Ethernet link handler thread */
osThreadDef(LinkThr, ethernetif_set_link, osPriorityNormal, 0, configMINIMAL_STACK_SIZE * 2);
osThreadCreate (osThread(LinkThr), &link_arg);
xTaskCreate (Task_Tcp_LinkMonitor,(const portCHAR *)“monitor”, 256,&link_arg,osPriorityHigh,0);
/* Start DHCP negotiation for a network interface (IPv4) */
#ifdef DHCP_ENABLE
if(gnetif.flags &NETIF_FLAG_LINK_UP)
{
dhcp_start(&gnetif);
}
#endif
/* USER CODE BEGIN 3 /
// if(gnetif.flags &NETIF_FLAG_LINK_UP)
// {
//
// xSemaphoreGive(link_arg.semaphore);
// }
/ USER CODE END 3 */
}