/** * * This function demonstrates the usage of the EMACPS by sending and * receiving a single frame in DMA interrupt mode. * The source packet will be described by two descriptors. It will be * received into a buffer described by a single descriptor. * * @param EmacPsInstancePtr is a pointer to the instance of the EmacPs * driver. * * @return XST_SUCCESS to indicate success, otherwise XST_FAILURE. * * @note None. * *****************************************************************************/ int EmacPsDmaSingleFrameIntrExample(XEmacPs *EmacPsInstancePtr) { int Status; u32 PayloadSize = 1000; u32 NumRxBuf = 0; XEmacPs_Bd *Bd1Ptr; XEmacPs_Bd *Bd2Ptr; XEmacPs_Bd *BdRxPtr; /* * Clear variables shared with callbacks */ FramesRx = 0; FramesTx = 0; DeviceErrors = 0; /* * Calculate the frame length (not including FCS) */ TxFrameLength = XEMACPS_HDR_SIZE + PayloadSize; /* * Setup packet to be transmitted */ EmacPsUtilFrameHdrFormatMAC(&TxFrame, EmacPsMAC); EmacPsUtilFrameHdrFormatType(&TxFrame, PayloadSize); EmacPsUtilFrameSetPayloadData(&TxFrame, PayloadSize); Xil_DCacheFlushRange((u32)&TxFrame, TxFrameLength); /* * Clear out receive packet memory area */ EmacPsUtilFrameMemClear(&RxFrame); Xil_DCacheFlushRange((u32)&RxFrame, TxFrameLength); /* * Allocate RxBDs since we do not know how many BDs will be used * in advance, use RXBD_CNT here. */ Status = XEmacPs_BdRingAlloc(& (XEmacPs_GetRxRing(EmacPsInstancePtr)), 1, &BdRxPtr); if (Status != XST_SUCCESS) { EmacPsUtilErrorTrap("Error allocating RxBD"); return XST_FAILURE; } /* * Setup the BD. The XEmacPs_BdRingClone() call will mark the * "wrap" field for last RxBD. Setup buffer address to associated * BD. */ XEmacPs_BdSetAddressRx(BdRxPtr, &RxFrame); /* * Enqueue to HW */ Status = XEmacPs_BdRingToHw(&(XEmacPs_GetRxRing(EmacPsInstancePtr)), 1, BdRxPtr); if (Status != XST_SUCCESS) { EmacPsUtilErrorTrap("Error committing RxBD to HW"); return XST_FAILURE; } /* * Allocate, setup, and enqueue 2 TxBDs. The first BD will * describe the first 32 bytes of TxFrame and the rest of BDs * will describe the rest of the frame. * * The function below will allocate 2 adjacent BDs with Bd1Ptr * being set as the lead BD. */ Status = XEmacPs_BdRingAlloc(&(XEmacPs_GetTxRing(EmacPsInstancePtr)), 2, &Bd1Ptr); if (Status != XST_SUCCESS) { EmacPsUtilErrorTrap("Error allocating TxBD"); return XST_FAILURE; } /* * Setup first TxBD */ XEmacPs_BdSetAddressTx(Bd1Ptr, &TxFrame); XEmacPs_BdSetLength(Bd1Ptr, FIRST_FRAGMENT_SIZE); XEmacPs_BdClearTxUsed(Bd1Ptr); XEmacPs_BdClearLast(Bd1Ptr); /* * Setup second TxBD */ Bd2Ptr = XEmacPs_BdRingNext(&(XEmacPs_GetTxRing(EmacPsInstancePtr)), Bd1Ptr); XEmacPs_BdSetAddressTx(Bd2Ptr, (u32) (&TxFrame) + FIRST_FRAGMENT_SIZE); XEmacPs_BdSetLength(Bd2Ptr, TxFrameLength - FIRST_FRAGMENT_SIZE); XEmacPs_BdClearTxUsed(Bd2Ptr); XEmacPs_BdSetLast(Bd2Ptr); /* * Enqueue to HW */ Status = XEmacPs_BdRingToHw(&(XEmacPs_GetTxRing(EmacPsInstancePtr)), 2, Bd1Ptr); if (Status != XST_SUCCESS) { EmacPsUtilErrorTrap("Error committing TxBD to HW"); return XST_FAILURE; } /* * Start the device */ XEmacPs_Start(EmacPsInstancePtr); /* Start transmit */ XEmacPs_Transmit(EmacPsInstancePtr); /* * Wait for transmission to complete */ while (!FramesTx); /* * Now that the frame has been sent, post process our TxBDs. * Since we have only submitted 2 to hardware, then there should * be only 2 ready for post processing. */ if (XEmacPs_BdRingFromHwTx(&(XEmacPs_GetTxRing(EmacPsInstancePtr)), 2, &Bd1Ptr) == 0) { EmacPsUtilErrorTrap ("TxBDs were not ready for post processing"); return XST_FAILURE; } /* * Examine the TxBDs. * * There isn't much to do. The only thing to check would be DMA * exception bits. But this would also be caught in the error * handler. So we just return these BDs to the free list. */ Status = XEmacPs_BdRingFree(&(XEmacPs_GetTxRing(EmacPsInstancePtr)), 2, Bd1Ptr); if (Status != XST_SUCCESS) { EmacPsUtilErrorTrap("Error freeing up TxBDs"); return XST_FAILURE; } /* * Wait for Rx indication */ while (!FramesRx); /* * Now that the frame has been received, post process our RxBD. * Since we have submitted to hardware, then there should be only 1 * ready for post processing. */ NumRxBuf = XEmacPs_BdRingFromHwRx(&(XEmacPs_GetRxRing (EmacPsInstancePtr)), 1, &BdRxPtr); if (0 == NumRxBuf) { EmacPsUtilErrorTrap("RxBD was not ready for post processing"); return XST_FAILURE; } /* * There is no device status to check. If there was a DMA error, * it should have been reported to the error handler. Check the * receive lengthi against the transmitted length, then verify * the data. */ if ((XEmacPs_BdGetLength(BdRxPtr)) != TxFrameLength) { EmacPsUtilErrorTrap("Length mismatch"); return XST_FAILURE; } if (EmacPsUtilFrameVerify(&TxFrame, &RxFrame) != 0) { EmacPsUtilErrorTrap("Data mismatch"); return XST_FAILURE; } /* * Return the RxBD back to the channel for later allocation. Free * the exact number we just post processed. */ Status = XEmacPs_BdRingFree(&(XEmacPs_GetRxRing(EmacPsInstancePtr)), NumRxBuf, BdRxPtr); if (Status != XST_SUCCESS) { EmacPsUtilErrorTrap("Error freeing up RxBDs"); return XST_FAILURE; } /* * Finished this example. If everything worked correctly, all TxBDs * and RxBDs should be free for allocation. Stop the device. */ XEmacPs_Stop(EmacPsInstancePtr); return XST_SUCCESS; }
void emacps_recv_handler(void *arg) { struct pbuf *p; XEmacPs_Bd *rxbdset, *curbdptr; struct xemac_s *xemac; xemacpsif_s *xemacpsif; XEmacPs_BdRing *rxring; volatile s32_t bd_processed; s32_t rx_bytes, k; u32_t bdindex; u32_t regval; xemac = (struct xemac_s *)(arg); xemacpsif = (xemacpsif_s *)(xemac->state); rxring = &XEmacPs_GetRxRing(&xemacpsif->emacps); #ifdef OS_IS_FREERTOS xInsideISR++; #endif /* * If Reception done interrupt is asserted, call RX call back function * to handle the processed BDs and then raise the according flag. */ regval = XEmacPs_ReadReg(xemacpsif->emacps.Config.BaseAddress, XEMACPS_RXSR_OFFSET); XEmacPs_WriteReg(xemacpsif->emacps.Config.BaseAddress, XEMACPS_RXSR_OFFSET, regval); resetrx_on_no_rxdata(xemacpsif); while(1) { bd_processed = XEmacPs_BdRingFromHwRx(rxring, XLWIP_CONFIG_N_RX_DESC, &rxbdset); if (bd_processed <= 0) { break; } for (k = 0, curbdptr=rxbdset; k < bd_processed; k++) { bdindex = XEMACPS_BD_TO_INDEX(rxring, curbdptr); p = (struct pbuf *)rx_pbufs_storage[bdindex]; /* * Adjust the buffer size to the actual number of bytes received. */ rx_bytes = XEmacPs_BdGetLength(curbdptr); pbuf_realloc(p, rx_bytes); /* store it in the receive queue, * where it'll be processed by a different handler */ if (pq_enqueue(xemacpsif->recv_q, (void*)p) < 0) { #if LINK_STATS lwip_stats.link.memerr++; lwip_stats.link.drop++; #endif pbuf_free(p); } else { #if !NO_SYS sys_sem_signal(&xemac->sem_rx_data_available); #endif } curbdptr = XEmacPs_BdRingNext( rxring, curbdptr); } /* free up the BD's */ XEmacPs_BdRingFree(rxring, bd_processed, rxbdset); setup_rx_bds(rxring); } #ifdef OS_IS_FREERTOS xInsideISR--; #endif return; }
void emacps_recv_handler(void *arg) { struct pbuf *p; unsigned irq_status, i; XEmacPs_Bd *rxbdset, *CurBdPtr; struct xemac_s *xemac; xemacpsif_s *xemacpsif; XEmacPs_BdRing *rxring; volatile int bd_processed; int rx_bytes, k; unsigned int BdIndex; xemac = (struct xemac_s *)(arg); xemacpsif = (xemacpsif_s *)(xemac->state); rxring = &XEmacPs_GetRxRing(&xemacpsif->emacps); #ifdef OS_IS_FREERTOS xInsideISR++; #endif /* * If Reception done interrupt is asserted, call RX call back function * to handle the processed BDs and then raise the according flag. */ while(1) { bd_processed = XEmacPs_BdRingFromHwRx(rxring, XLWIP_CONFIG_N_RX_DESC, &rxbdset); if (bd_processed <= 0) { #ifdef OS_IS_FREERTOS xInsideISR--; #endif return; } for (k = 0, CurBdPtr=rxbdset; k < bd_processed; k++) { BdIndex = XEMACPS_BD_TO_INDEX(rxring, CurBdPtr); p = (struct pbuf *)rx_pbufs_storage[BdIndex]; /* * Adjust the buffer size to the actual number of bytes received. */ rx_bytes = XEmacPs_BdGetLength(CurBdPtr); pbuf_realloc(p, rx_bytes); Xil_DCacheInvalidateRange((unsigned int)p->payload, (unsigned)XEMACPS_MAX_FRAME_SIZE); /* store it in the receive queue, * where it'll be processed by a different handler */ if (pq_enqueue(xemacpsif->recv_q, (void*)p) < 0) { #if LINK_STATS lwip_stats.link.memerr++; lwip_stats.link.drop++; #endif pbuf_free(p); } else { #if !NO_SYS sys_sem_signal(&xemac->sem_rx_data_available); #endif } CurBdPtr = XEmacPs_BdRingNext( rxring, CurBdPtr); } /* free up the BD's */ XEmacPs_BdRingFree(rxring, bd_processed, rxbdset); _setup_rx_bds(rxring); } return; }