硬件連線就是將dp83848的INT腳連到STM32的某個中斷腳上,這裏是PB14緩存
PB14的中斷處理函數中,會釋放一個信號量,這裏只是發生鏈路狀態改變中斷(網線插上或拔下)網絡
1 void EXTI15_10_IRQHandler(void) 2 { 3 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE; 4 5 if(EXTI_GetITStatus(ETH_LINK_EXTI_LINE) != RESET) 6 { 7 /* Give the semaphore to wakeup LwIP task */ 8 xSemaphoreGiveFromISR( ETH_link_xSemaphore, &xHigherPriorityTaskWoken ); 9 } 10 /* Clear interrupt pending bit */ 11 EXTI_ClearITPendingBit(ETH_LINK_EXTI_LINE); 12 13 /* Switch tasks if necessary. */ 14 if( xHigherPriorityTaskWoken != pdFALSE ) 15 { 16 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken ); 17 } 18 }
該信號量在eth_bsp.c中定義,在ETH_BSP_Config中建立,同時,啓動一個Eth_Link_IT_task任務,阻塞在信號量上,偵聽鏈路狀態,函數
1 xSemaphoreHandle ETH_link_xSemaphore = NULL; 2 3 void ETH_BSP_Config(void) 4 { 5 6 if (ETH_link_xSemaphore == NULL) 7 { 8 /* create binary semaphore used for ETH_link handling */ 9 vSemaphoreCreateBinary( ETH_link_xSemaphore ); 10 } 11 12 /* create the task that handles the ETH_link */ 13 xTaskCreate(Eth_Link_IT_task, (signed char*) "E_link", ETH_LINK_TASK_STACK_SIZE, (void *)DP83848_PHY_ADDRESS, 14 ETH_LINK_TASK_PRIORITY,NULL); 15 }
在Eth_Link_IT_task任務中,會讀取以太網寄存器,進一步判斷鏈路狀態變化的類型,是通仍是斷,網線插上了仍是拔下了,ui
而後,將網絡接口的鏈路狀態設置爲up或down。此處,在eth_bsp中引用xnetif有點彆扭。。。因爲須要讀取PHY寄存器,因此,放在這兒了,是否是能夠封裝一下,由上面調用。。。this
1 void Eth_Link_IT_task( void * pvParameters ) 2 { 3 uint32_t pcPHYAddress; 4 5 pcPHYAddress = ( uint32_t ) pvParameters; 6 7 for(;;) 8 { 9 if (xSemaphoreTake( ETH_link_xSemaphore, emacBLOCK_TIME_WAITING_ETH_LINK_IT)==pdTRUE) 10 { 11 /* Check whether the link interrupt has occurred or not */ 12 if(((ETH_ReadPHYRegister((uint16_t) pcPHYAddress, PHY_MISR)) & PHY_LINK_STATUS) != 0) 13 { 14 if((ETH_ReadPHYRegister((uint16_t) pcPHYAddress, PHY_SR) & 1)) 15 { 16 netif_set_link_up(&xnetif); 17 } 18 else 19 { 20 netif_set_link_down(&xnetif); 21 } 22 } 23 } 24 } 25 }
以後,netif_set_link_up裏會調用用戶掛接的link up down回調函數,該函數也在eth_bsp中,是ETH_link_callback,掛接是在netconf中建立xnetif的時候。spa
在ETH_link_callback中,code
若是是up,會啓動MAC(不知道啓動的什麼?),設置netif up(是接口up,不是link up);blog
若是是down,會中止MAC,設置netif down。接口
1 void ETH_link_callback(struct netif *netif) 2 { 6 if(netif_is_link_up(netif)) 7 { 8 /* Restart MAC interface */ 9 ETH_Start(); 10 11 /* When the netif is fully configured this function must be called.*/ 12 netif_set_up(&xnetif); 15 } 16 else 17 { 18 ETH_Stop(); 19 20 /* When the netif link is down this function must be called.*/ 21 netif_set_down(&xnetif);24 } 25 }
上面的註釋提到,link up down以後,netif up down必須調用,回調函數
由於,有些協議,好比arp和nd6,都是在netif down以後,清理arp緩存,nd的前綴、鄰居信息的。