//读出DMA的数据包数据包 void Read_Eth_Packet() { FrameTypeDef fd; frameds = ETH_RxPkt_ChainMode(); fd=frameds; if(frameds.length<=1500) { memcpy(Eth_Buff, (u8 *)frameds.buffer, frameds.length); //复制进缓存 } /* Set Own bit of the Rx descriptor Status: gives the buffer back to ETHERNET DMA */ frameds.descriptor->Status = ETH_DMARxDesc_OWN; /* 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; } }
/** * 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; p = NULL; frame = ETH_RxPkt_ChainMode(); /* 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 pool. */ p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); 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; } } /* Set Own bit of the Rx descriptor Status: gives the buffer back to ETHERNET DMA */ frame.descriptor->Status = ETH_DMARxDesc_OWN; /* 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; }
/** * 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= NULL, *q; u32_t len; FrameTypeDef frame; u8 *buffer; __IO ETH_DMADESCTypeDef *DMARxDesc; uint32_t bufferoffset = 0; uint32_t payloadoffset = 0; uint32_t byteslefttocopy = 0; uint32_t i=0; /* get received frame */ frame = ETH_RxPkt_ChainMode(); /* Obtain the size of the packet and put it into the "len" variable. */ len = frame.length; buffer = (u8 *)frame.buffer; if (len > 0) { /* We allocate a pbuf chain of pbufs from the Lwip buffer pool */ p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); } if (p != NULL) { DMARxDesc = frame.descriptor; bufferoffset = 0; for(q = p; q != NULL; q = q->next) { byteslefttocopy = q->len; payloadoffset = 0; /* Check if the length of bytes to copy in current pbuf is bigger than Rx buffer size*/ while( (byteslefttocopy + bufferoffset) > ETH_MAX_PACKET_SIZE ) { /* Copy data to pbuf*/ memcpy( (u8_t*)((u8_t*)q->payload + payloadoffset), (u8_t*)((u8_t*)buffer + bufferoffset), (ETH_MAX_PACKET_SIZE - bufferoffset)); /* Point to next descriptor */ DMARxDesc = (ETH_DMADESCTypeDef *)(DMARxDesc->Buffer2NextDescAddr); buffer = (unsigned char *)(DMARxDesc->Buffer1Addr); byteslefttocopy = byteslefttocopy - (ETH_MAX_PACKET_SIZE - bufferoffset); payloadoffset = payloadoffset + (ETH_MAX_PACKET_SIZE - bufferoffset); bufferoffset = 0; } /* Copy remaining data in pbuf */ memcpy( (u8_t*)((u8_t*)q->payload + payloadoffset), (u8_t*)((u8_t*)buffer + bufferoffset), byteslefttocopy); bufferoffset = bufferoffset + byteslefttocopy; } /* Release descriptors to DMA */ DMARxDesc =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++) { DMARxDesc->Status = ETH_DMARxDesc_OWN; DMARxDesc = (ETH_DMADESCTypeDef *)(DMARxDesc->Buffer2NextDescAddr); } /* Clear Segment_Count */ DMA_RX_FRAME_infos.Seg_Count =0; /* added for test*/ } /* 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; }
/** * 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) { static xSemaphoreHandle xRxSemaphore = NULL; struct pbuf *p, *q; u16_t len; int l =0; FrameTypeDef frame; u8 *buffer; p = NULL; if( xRxSemaphore ==NULL) { vSemaphoreCreateBinary(xRxSemaphore); } /* access to emac is guarded using a semphore */ if (xSemaphoreTake(xRxSemaphore, netifGUARD_BLOCK_TIME)) { frame = ETH_RxPkt_ChainMode(); /* Obtain the size of the packet and put it into the "len" variable. */ len = frame.length; if (len) { buffer = (u8 *)frame.buffer; /* We allocate a pbuf chain of pbufs from the pool. */ p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); 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; } /* Set Own bit of the Rx descriptor Status: gives the buffer back to ETHERNET DMA */ frame.descriptor->Status = ETH_DMARxDesc_OWN; /* 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; } } } } xSemaphoreGive(xRxSemaphore); return p; }