配置好了以太网中断,也知道了中断入口函数的名称,下面我们就来编写以太网中断函数。打开工程中的stm32f4xx_it.c文件,一般每一个使用了中断的STM32工程都会有这么一个文件,用来集中管理中断入口。追加以下代码:
/** * @brief This function handles ethernet DMA interrupt request. * @param None * @retval None */void ETH_IRQHandler(void){ /* Handles all the received frames */ /* check if any packet received */ while(ETH_CheckFrameReceived()){ /* process received ethernet packet */ Pkt_Handle(); } /* Clear the Eth DMA Rx IT pending bits */ ETH_DMAClearITPendingBit(ETH_DMA_IT_R); ETH_DMAClearITPendingBit(ETH_DMA_IT_NIS);}
上面的代码配上英文的注释也很好理解,以太网中断中首先检查是否接受到以太网的数据包,如果是,就调用Pkt_Handle()函数进行下一层的分析和处理,最后两行Clear复位中断标记,让下一次中断能够产生。那么问题就集中到Pkt_Handle()函数上来了。kt_Handle()函数是我自己命名的,这个函数的原型取自LWIP中的LWIP_Pkt_Handle(),我们先来观察一下LWIP中的包分析函数怎么写的:在STM32F4x7_ETH_LwIP_V1.1.1/Project/Standalone/udp_echo_client/src/netconf.c中,有如下代码:
/*** @brief Called when a frame is received* @param None* @retval None*/void LwIP_Pkt_Handle(void){ /* Read a received packet from the Ethernet buffers and send it to the lwIP for handling */ ethernetif_input(&gnetif);}
可以看到LWIP继续调用了下层的ethernetif_input(),继续追踪到我们之前提到的最底层文件ethernetif.c,是不是有种似曾相识的感觉,这个文件前几章我们不止一次遇到过,分别为我们提供了low_level_init、low_level_output多个重要函数,我们现在又一次遇到它了,看上去它这次要为我们的以太网监听提供low_level_input了。
果不其然,在ethernetif_input函数中,我们看到了这个预料中的函数调用,截图如下:
我这里把路径为STM32F4x7_ETH_LwIP_V1.1.1/Utilities/Third_Party/lwip-1.4.1/port/STM32F4x7/Standalone/ethernetif.c的原版low_level_input()的所有代码都贴出来,感兴趣的朋友可以仔细研读:
/** * Should allocate a pbuf and transfer the bytes of the incoming * packet from the interface into the pbuf. * * @param netif the lwip network interface structure for this ethernetif * @return a pbuf filled with the received packet (including MAC header) * NULL on memory error */static struct pbuf * low_level_input(struct netif *netif){ struct pbuf *p, *q; u16_t len; int l =0; FrameTypeDef frame; u8 *buffer; uint32_t i=0; __IO ETH_DMADESCTypeDef *DMARxNextDesc; p = NULL; /* get received frame */ frame = ETH_Get_Received_Frame(); /* Obtain the size of the packet and put it into the "len" variable. */ len = frame.length; buffer = (u8 *)frame.buffer; /* We allocate a pbuf chain of pbufs from the Lwip buffer pool */ p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); /* copy received frame to pbuf chain */ if (p != NULL) { for (q = p; q != NULL; q = q->next) { memcpy((u8_t*)q->payload, (u8_t*)&buffer[l], q->len); l = l + q->len; } } /* Release descriptors to DMA */ /* Check if frame with multiple DMA buffer segments */ if (DMA_RX_FRAME_infos->Seg_Count > 1) { DMARxNextDesc = DMA_RX_FRAME_infos->FS_Rx_Desc; } else { DMARxNextDesc = frame.descriptor; } /* Set Own bit in Rx descriptors: gives the buffers back to DMA */ for (i=0; iSeg_Count; i++) { DMARxNextDesc->Status = ETH_DMARxDesc_OWN; DMARxNextDesc = (ETH_DMADESCTypeDef *)(DMARxNextDesc->Buffer2NextDescAddr); } /* Clear Segment_Count */ DMA_RX_FRAME_infos->Seg_Count =0; /* When Rx Buffer unavailable flag is set: clear it and resume reception */ if ((ETH->DMASR & ETH_DMASR_RBUS) != (u32)RESET) { /* Clear RBUS ETHERNET DMA flag */ ETH->DMASR = ETH_DMASR_RBUS; /* Resume DMA reception */ ETH->DMARPDR = 0; } return p;}
同样的,配合注释应该也容易理解。22行的frame变量取到了以太网数据包,ETH_Get_Received_Frame函数,不用LWIP也是有的,在之前我们提到过的stm32f4x7_eth.c文件中,len和buffer两个变量一个是包长度,一个是包内容头指针。下面/* copy received frame to pbuf chain */那一段是用链表遍历的方式,将以太网包数据放入LWIP处理数据的pbuf链表中,方便LWIP上层逻辑获取数据,这里我们不使用LWIP,这一段可忽略。接下来所有的操作都是针对DMA进行的,将DMA复位,因此我们需要保留,否则会一直产生重复的中断。
通过以上的分析,我们可以轻松写出自己的Pkg_Handle()函数了:
void Pkt_Handle(void) { FrameTypeDef frame; /* get received frame */ frame = ETH_Get_Received_Frame(); /* Obtain the size of the packet and put it into the "len" variable. */ receiveLen = frame.length; receiveBuffer = (u8 *)frame.buffer; printf("0011%d0022n", receiveLen); //将每一个的包长度发往串口 if(receiveBuffer[41] == 201){ //如果第42字节是十进制201,则将整个包内容发往串口 for (i = 0; i < receiveLen; i++) { printf("%c", receiveBuffer); } } /* Check if frame with multiple DMA buffer segments */ if (DMA_RX_FRAME_infos->Seg_Count > 1) { DMARxNextDesc = DMA_RX_FRAME_infos->FS_Rx_Desc; } else { DMARxNextDesc = frame.descriptor; } /* Set Own bit in Rx descriptors: gives the buffers back to DMA */ for (i = 0; i < DMA_RX_FRAME_infos->Seg_Count; i++) { DMARxNextDesc->Status = ETH_DMARxDesc_OWN; DMARxNextDesc = (ETH_DMADESCTypeDef *)(DMARxNextDesc->Buffer2NextDescAddr); } /* Clear Segment_Count */ DMA_RX_FRAME_infos->Seg_Count = 0; /* When Rx Buffer unavailable flag is set: clear it and resume reception */ if ((ETH->DMASR & ETH_DMASR_RBUS) != (u32)RESET) { /* Clear RBUS ETHERNET DMA flag */ ETH->DMASR = ETH_DMASR_RBUS; /* Resume DMA reception */ ETH->DMARPDR = 0; }} 验证和总结
配置好了以太网中断,也知道了中断入口函数的名称,下面我们就来编写以太网中断函数。打开工程中的stm32f4xx_it.c文件,一般每一个使用了中断的STM32工程都会有这么一个文件,用来集中管理中断入口。追加以下代码:
/** * @brief This function handles ethernet DMA interrupt request. * @param None * @retval None */void ETH_IRQHandler(void){ /* Handles all the received frames */ /* check if any packet received */ while(ETH_CheckFrameReceived()){ /* process received ethernet packet */ Pkt_Handle(); } /* Clear the Eth DMA Rx IT pending bits */ ETH_DMAClearITPendingBit(ETH_DMA_IT_R); ETH_DMAClearITPendingBit(ETH_DMA_IT_NIS);}
上面的代码配上英文的注释也很好理解,以太网中断中首先检查是否接受到以太网的数据包,如果是,就调用Pkt_Handle()函数进行下一层的分析和处理,最后两行Clear复位中断标记,让下一次中断能够产生。那么问题就集中到Pkt_Handle()函数上来了。kt_Handle()函数是我自己命名的,这个函数的原型取自LWIP中的LWIP_Pkt_Handle(),我们先来观察一下LWIP中的包分析函数怎么写的:在STM32F4x7_ETH_LwIP_V1.1.1/Project/Standalone/udp_echo_client/src/netconf.c中,有如下代码:
/*** @brief Called when a frame is received* @param None* @retval None*/void LwIP_Pkt_Handle(void){ /* Read a received packet from the Ethernet buffers and send it to the lwIP for handling */ ethernetif_input(&gnetif);}
可以看到LWIP继续调用了下层的ethernetif_input(),继续追踪到我们之前提到的最底层文件ethernetif.c,是不是有种似曾相识的感觉,这个文件前几章我们不止一次遇到过,分别为我们提供了low_level_init、low_level_output多个重要函数,我们现在又一次遇到它了,看上去它这次要为我们的以太网监听提供low_level_input了。
果不其然,在ethernetif_input函数中,我们看到了这个预料中的函数调用,截图如下:
我这里把路径为STM32F4x7_ETH_LwIP_V1.1.1/Utilities/Third_Party/lwip-1.4.1/port/STM32F4x7/Standalone/ethernetif.c的原版low_level_input()的所有代码都贴出来,感兴趣的朋友可以仔细研读:
/** * Should allocate a pbuf and transfer the bytes of the incoming * packet from the interface into the pbuf. * * @param netif the lwip network interface structure for this ethernetif * @return a pbuf filled with the received packet (including MAC header) * NULL on memory error */static struct pbuf * low_level_input(struct netif *netif){ struct pbuf *p, *q; u16_t len; int l =0; FrameTypeDef frame; u8 *buffer; uint32_t i=0; __IO ETH_DMADESCTypeDef *DMARxNextDesc; p = NULL; /* get received frame */ frame = ETH_Get_Received_Frame(); /* Obtain the size of the packet and put it into the "len" variable. */ len = frame.length; buffer = (u8 *)frame.buffer; /* We allocate a pbuf chain of pbufs from the Lwip buffer pool */ p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); /* copy received frame to pbuf chain */ if (p != NULL) { for (q = p; q != NULL; q = q->next) { memcpy((u8_t*)q->payload, (u8_t*)&buffer[l], q->len); l = l + q->len; } } /* Release descriptors to DMA */ /* Check if frame with multiple DMA buffer segments */ if (DMA_RX_FRAME_infos->Seg_Count > 1) { DMARxNextDesc = DMA_RX_FRAME_infos->FS_Rx_Desc; } else { DMARxNextDesc = frame.descriptor; } /* Set Own bit in Rx descriptors: gives the buffers back to DMA */ for (i=0; iSeg_Count; i++) { DMARxNextDesc->Status = ETH_DMARxDesc_OWN; DMARxNextDesc = (ETH_DMADESCTypeDef *)(DMARxNextDesc->Buffer2NextDescAddr); } /* Clear Segment_Count */ DMA_RX_FRAME_infos->Seg_Count =0; /* When Rx Buffer unavailable flag is set: clear it and resume reception */ if ((ETH->DMASR & ETH_DMASR_RBUS) != (u32)RESET) { /* Clear RBUS ETHERNET DMA flag */ ETH->DMASR = ETH_DMASR_RBUS; /* Resume DMA reception */ ETH->DMARPDR = 0; } return p;}
同样的,配合注释应该也容易理解。22行的frame变量取到了以太网数据包,ETH_Get_Received_Frame函数,不用LWIP也是有的,在之前我们提到过的stm32f4x7_eth.c文件中,len和buffer两个变量一个是包长度,一个是包内容头指针。下面/* copy received frame to pbuf chain */那一段是用链表遍历的方式,将以太网包数据放入LWIP处理数据的pbuf链表中,方便LWIP上层逻辑获取数据,这里我们不使用LWIP,这一段可忽略。接下来所有的操作都是针对DMA进行的,将DMA复位,因此我们需要保留,否则会一直产生重复的中断。
通过以上的分析,我们可以轻松写出自己的Pkg_Handle()函数了:
void Pkt_Handle(void) { FrameTypeDef frame; /* get received frame */ frame = ETH_Get_Received_Frame(); /* Obtain the size of the packet and put it into the "len" variable. */ receiveLen = frame.length; receiveBuffer = (u8 *)frame.buffer; printf("0011%d0022n", receiveLen); //将每一个的包长度发往串口 if(receiveBuffer[41] == 201){ //如果第42字节是十进制201,则将整个包内容发往串口 for (i = 0; i < receiveLen; i++) { printf("%c", receiveBuffer); } } /* Check if frame with multiple DMA buffer segments */ if (DMA_RX_FRAME_infos->Seg_Count > 1) { DMARxNextDesc = DMA_RX_FRAME_infos->FS_Rx_Desc; } else { DMARxNextDesc = frame.descriptor; } /* Set Own bit in Rx descriptors: gives the buffers back to DMA */ for (i = 0; i < DMA_RX_FRAME_infos->Seg_Count; i++) { DMARxNextDesc->Status = ETH_DMARxDesc_OWN; DMARxNextDesc = (ETH_DMADESCTypeDef *)(DMARxNextDesc->Buffer2NextDescAddr); } /* Clear Segment_Count */ DMA_RX_FRAME_infos->Seg_Count = 0; /* When Rx Buffer unavailable flag is set: clear it and resume reception */ if ((ETH->DMASR & ETH_DMASR_RBUS) != (u32)RESET) { /* Clear RBUS ETHERNET DMA flag */ ETH->DMASR = ETH_DMASR_RBUS; /* Resume DMA reception */ ETH->DMARPDR = 0; }} 验证和总结