Advertisement
Guest User

Untitled

a guest
Apr 6th, 2017
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include "platform.h"
  5.  
  6. #include "xparameters.h"
  7. #include "xil_exception.h"
  8. #include "xstreamer.h"
  9. #include "xil_cache.h"
  10. #include "xllfifo.h"
  11. #include "xstatus.h"
  12. #include "xscugic.h"
  13.  
  14. /* base addresses */
  15. #define FIFO_RX_BASE_ADDRESS XPAR_AXI_FIFO_0_BASEADDR
  16. #define FIFO_TX_BASE_ADDRESS XPAR_AXI_FIFO_1_BASEADDR
  17.  
  18. /* interrupt ids */
  19. #define FIFO_TX_IRQ XPAR_FABRIC_AXI_FIFO_MM_S_TX_INTERRUPT_INTR
  20. #define FIFO_RX_IRQ XPAR_FABRIC_AXI_FIFO_MM_S_RX_INTERRUPT_INTR
  21.  
  22. /* device ids */
  23. #define FIFO_TX_DEVICE_ID XPAR_AXI_FIFO_MM_S_TX_DEVICE_ID
  24. #define FIFO_RX_DEVICE_ID XPAR_AXI_FIFO_MM_S_RX_DEVICE_ID
  25.  
  26. /* interrupt controller */
  27. #define INTC           XScuGic
  28. #define INTC_HANDLER   XScuGic_InterruptHandler
  29. #define INTC_DEVICE_ID          XPAR_SCUGIC_SINGLE_DEVICE_ID
  30.  
  31. /* fifo parameters */
  32. #define WORD_SIZE 4         /* Size of words in bytes */
  33. #define MAX_PACKET_LEN 4
  34. #define NO_OF_PACKETS 64
  35. #define MAX_DATA_BUFFER_SIZE NO_OF_PACKETS*MAX_PACKET_LEN
  36.  
  37. /* Flags interrupt handlers use to notify the application context the events */
  38. volatile int Done;
  39. volatile int Error;
  40.  
  41. /* device instances */
  42. XLlFifo FifoInstance_tx;
  43. XLlFifo FifoInstance_rx;
  44. static INTC Intc;
  45.  
  46. /* global buffers */
  47. u32 SourceBuffer[MAX_DATA_BUFFER_SIZE * WORD_SIZE];
  48. u32 DestinationBuffer[MAX_DATA_BUFFER_SIZE * WORD_SIZE];
  49.  
  50. /* function prototypes */
  51. int fifo_enable(XLlFifo *InstancePtr, u16 DeviceId, u16 IRQ_ID);
  52. int FIFOloopbackTest(XLlFifo *InstancePtr_tx, u16 DeviceId_tx);
  53. int TxSend(XLlFifo *InstancePtr, u32 *SourceAddr);
  54. static void FifoHandler(XLlFifo *Fifo);
  55. static void FifoRecvHandler(XLlFifo *Fifo);
  56. static void FifoSendHandler(XLlFifo *Fifo);
  57. static void FifoErrorHandler(XLlFifo *InstancePtr, u32 Pending);
  58. int SetupInterruptSystem(INTC *IntcInstancePtr, XLlFifo *InstancePtr,
  59.                 u16 FifoIntrId);
  60. static void DisableIntrSystem(INTC *IntcInstancePtr, u16 FifoIntrId);
  61.  
  62. int main()
  63. {
  64.     init_platform();
  65.  
  66.     int Status;
  67.     int i = 0;
  68.  
  69.     printf("\nTesting : ");
  70.     printf("PS_AXI4-LITE -> PL_FIFO_TX --AXI4-STREAM--> PL_FIFO_RX -> PS_AXI4-LITE\n\r");
  71.  
  72.     /* enable tx fifo */
  73.     Status = fifo_enable(&FifoInstance_tx, FIFO_TX_DEVICE_ID, FIFO_TX_IRQ);
  74.     if (Status != XST_SUCCESS) {
  75.         xil_printf("Failed to initialize TX_FIFO\n\r");
  76.         return XST_FAILURE;
  77.     }
  78.  
  79.     /* enable rx fifo */
  80.     Status = fifo_enable(&FifoInstance_rx, FIFO_RX_DEVICE_ID, FIFO_RX_IRQ);
  81.     if (Status != XST_SUCCESS) {
  82.         xil_printf("Failed to initialize RX_FIFO\n\r");
  83.         return XST_FAILURE;
  84.     }
  85.  
  86.     /* transmit data from tx_fifo and wait until data is recieved from rx_fifo */
  87.     Status = FIFOloopbackTest(&FifoInstance_tx, FIFO_TX_DEVICE_ID);
  88.     if (Status != XST_SUCCESS) {
  89.         xil_printf("Axi Streaming FIFO Interrupt Example Test Failed\n\r");
  90.         return XST_FAILURE;
  91.     }
  92.  
  93.     /* disable interrupts */
  94.     DisableIntrSystem(&Intc, FIFO_TX_IRQ);
  95.     DisableIntrSystem(&Intc, FIFO_RX_IRQ);
  96.  
  97. //  xil_printf("Din | Dout\n\r");
  98. //  xil_printf("----------\n\r");
  99. //  for(i = 0; i < MAX_DATA_BUFFER_SIZE; i++)
  100. //      xil_printf("%03d | %03d\n\r",(int)SourceBuffer[i],(int)DestinationBuffer[i]);
  101.     xil_printf("Axi Streaming FIFO Interrupt Example Test passed\n\r");
  102.  
  103.     cleanup_platform();
  104.     return XST_SUCCESS;
  105. }
  106.  
  107. int FIFOloopbackTest(XLlFifo *InstancePtrTx, u16 DeviceId_tx)
  108. {
  109.     /* BEGIN STREAMING EXAMPLE */
  110.     int Status;
  111.     int i = 0;
  112.     int err = 0;
  113.  
  114.     /* Transmit the Data Stream */
  115.     Status = TxSend(InstancePtrTx, SourceBuffer);
  116.     if (Status != XST_SUCCESS){
  117.         xil_printf("Transmission of Data failed\n\r");
  118.         return XST_FAILURE;
  119.     }
  120.     while(!Done);
  121.  
  122.     /* Check for errors */
  123.     if(Error) {
  124.         xil_printf("Errors in the FIFO\n\r");
  125.         return XST_FAILURE;
  126.     }
  127.  
  128.     err = 0;
  129.  
  130.     /* Compare the data sent with the data received */
  131.     xil_printf("Comparing data...\n\r");
  132.     for( i=0 ; i<MAX_DATA_BUFFER_SIZE ; i++ ){
  133.         if ( *(SourceBuffer + i) != *(DestinationBuffer + i) ){
  134.             err++;
  135.         }
  136.     }
  137.  
  138.     if (err != 0){
  139.         return XST_FAILURE;
  140.     }
  141.  
  142.     printf("%d transmission errors occured\n\r",err);
  143.     return Status;
  144. }
  145.  
  146. int TxSend(XLlFifo *InstancePtr, u32  *SourceAddr)
  147. {
  148.     int i;
  149.     int j;
  150.  
  151.     if(InstancePtr->BaseAddress == FIFO_RX_BASE_ADDRESS)
  152.         xil_printf("Transmitting Data from RX_FIFO\n\r");
  153.     else if(InstancePtr->BaseAddress == FIFO_TX_BASE_ADDRESS)
  154.         xil_printf("Transmitting Data from TX_FIFO\n\r");
  155.  
  156.     XLlFifo_TxReset(InstancePtr);
  157.  
  158.     /* Filling the buffer with data */
  159.     for (i=0; i < MAX_DATA_BUFFER_SIZE; i++)
  160.         *(SourceAddr + i) = i;
  161.  
  162.     xil_printf("tx fifo vacancy (pre-send): %d\n\r",(int)XLlFifo_iTxVacancy(InstancePtr));
  163.  
  164.     for(i=0; i < NO_OF_PACKETS; i++){
  165.         /* Writing into the FIFO Transmit Port Buffer */
  166.         for (j=0; j < MAX_PACKET_LEN; j++){
  167.             if( XLlFifo_iTxVacancy(InstancePtr) ){
  168.                 XLlFifo_TxPutWord(InstancePtr,
  169.                     *(SourceAddr+(i*MAX_PACKET_LEN)+j));
  170.             }
  171.         }
  172.     }
  173.  
  174.     xil_printf("tx fifo vacancy (post-send): %d\n\r",(int)XLlFifo_iTxVacancy(InstancePtr));
  175.  
  176.     /* Start Transmission by writing transmission length into the TLR */
  177.     XLlFifo_iTxSetLen(InstancePtr, (MAX_DATA_BUFFER_SIZE * WORD_SIZE));
  178.  
  179.  
  180.     /* Transmission Complete */
  181.     return XST_SUCCESS;
  182. }
  183.  
  184. int fifo_enable(XLlFifo *InstancePtr, u16 DeviceId, u16 IRQ_ID)
  185. {
  186.     XLlFifo_Config *Config;
  187.     int Status;
  188.     Status = XST_SUCCESS;
  189.  
  190.     /* Initialize the Device Configuration Interface driver */
  191.     Config = XLlFfio_LookupConfig(DeviceId);
  192.     if (!Config) {
  193.         xil_printf("No config found for %d\r\n", DeviceId);
  194.         return XST_FAILURE;
  195.     }
  196.  
  197.     /*
  198.      * This is where the virtual address would be used, this example
  199.      * uses physical address.
  200.      */
  201.     Status = XLlFifo_CfgInitialize(InstancePtr, Config, Config->BaseAddress);
  202.     if (Status != XST_SUCCESS) {
  203.         xil_printf("fifo initialization failed\n\r");
  204.         return Status;
  205.     }
  206.  
  207.  
  208.     /* Check for the Reset value */
  209.     Status = XLlFifo_Status(InstancePtr);
  210.     XLlFifo_IntClear(InstancePtr,0xffffffff);
  211.     Status = XLlFifo_Status(InstancePtr);
  212.     if(Status != 0x0) {
  213.         xil_printf("\n ERROR : Reset value of ISR : 0x%x\t"
  214.                 "Expected : 0x0\n\r",
  215.                 XLlFifo_Status(InstancePtr));
  216.         return XST_FAILURE;
  217.     }
  218.  
  219.     /*
  220.      * Connect the Axi Streaming FIFO to the interrupt subsystem such
  221.      * that interrupts can occur. This function is application specific.
  222.      */
  223.     Status = SetupInterruptSystem(&Intc, InstancePtr, IRQ_ID);
  224.     if (Status != XST_SUCCESS) {
  225.         xil_printf("Failed intr setup\r\n");
  226.         return XST_FAILURE;
  227.     }
  228.  
  229.     XLlFifo_IntEnable(InstancePtr, XLLF_INT_ALL_MASK);
  230.  
  231.     return XST_SUCCESS;
  232. }
  233.  
  234. int SetupInterruptSystem(INTC *IntcInstancePtr, XLlFifo *InstancePtr,
  235.                 u16 FifoIntrId)
  236. {
  237.  
  238.     int Status;
  239.     XScuGic_Config *IntcConfig;
  240.  
  241.     /*
  242.      * Initialize the interrupt controller driver so that it is ready to
  243.      * use.
  244.      */
  245.     IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);
  246.     if (NULL == IntcConfig) {
  247.         return XST_FAILURE;
  248.     }
  249.  
  250.     Status = XScuGic_CfgInitialize(IntcInstancePtr, IntcConfig,
  251.                 IntcConfig->CpuBaseAddress);
  252.     if (Status != XST_SUCCESS) {
  253.         return XST_FAILURE;
  254.     }
  255.  
  256.     XScuGic_SetPriorityTriggerType(IntcInstancePtr, FifoIntrId, 0xA0, 0x3);
  257.  
  258.     /*
  259.      * Connect the device driver handler that will be called when an
  260.      * interrupt for the device occurs, the handler defined above performs
  261.      * the specific interrupt processing for the device.
  262.      */
  263.     Status = XScuGic_Connect(IntcInstancePtr, FifoIntrId,
  264.                 (Xil_InterruptHandler)FifoHandler,
  265.                 InstancePtr);
  266.     if (Status != XST_SUCCESS) {
  267.         return Status;
  268.     }
  269.  
  270.     XScuGic_Enable(IntcInstancePtr, FifoIntrId);
  271.  
  272.     /*
  273.      * Initialize the exception table.
  274.      */
  275.     Xil_ExceptionInit();
  276.  
  277.     /*
  278.      * Register the interrupt controller handler with the exception table.
  279.      */
  280.     Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
  281.         (Xil_ExceptionHandler)INTC_HANDLER,
  282.         (void *)IntcInstancePtr);;
  283.  
  284.     /*
  285.      * Enable exceptions.
  286.      */
  287.     Xil_ExceptionEnable();
  288.  
  289.     return XST_SUCCESS;
  290. }
  291.  
  292. static void DisableIntrSystem(INTC *IntcInstancePtr, u16 FifoIntrId)
  293. {
  294.     XScuGic_Disconnect(IntcInstancePtr, FifoIntrId);
  295. }
  296.  
  297. static void FifoErrorHandler(XLlFifo *InstancePtr, u32 Pending)
  298. {
  299.     if (Pending & XLLF_INT_RPURE_MASK) {
  300.         xil_printf("error : receive under read\n\r");
  301.         XLlFifo_RxReset(InstancePtr);
  302.     } else if (Pending & XLLF_INT_RPORE_MASK) {
  303.         xil_printf("error : receive over read\n\r");
  304.         XLlFifo_RxReset(InstancePtr);
  305.     } else if(Pending & XLLF_INT_RPUE_MASK) {
  306.         xil_printf("error : receive under run (empty)\n\r");
  307.         XLlFifo_RxReset(InstancePtr);
  308.     } else if (Pending & XLLF_INT_TPOE_MASK) {
  309.         xil_printf("error : transmit over read\n\r");
  310.         XLlFifo_TxReset(InstancePtr);
  311.     } else if (Pending & XLLF_INT_TSE_MASK) {
  312.         xil_printf("error : transmit length mismatch\n\r");
  313.     }
  314.     Error++;
  315. }
  316.  
  317. static void FifoSendHandler(XLlFifo *InstancePtr)
  318. {
  319.     XLlFifo_IntClear(InstancePtr, XLLF_INT_TC_MASK);
  320.  
  321.     Done = 1;
  322. }
  323.  
  324. static void FifoRecvHandler(XLlFifo *InstancePtr)
  325. {
  326.     int i;
  327.     u32 RxWord;
  328.     static u32 ReceiveLength;
  329.  
  330.     if(InstancePtr->BaseAddress == FIFO_RX_BASE_ADDRESS)
  331.         xil_printf("Receiving Data from RX_FIFO\n\r");
  332.     else if(InstancePtr->BaseAddress == FIFO_TX_BASE_ADDRESS)
  333.         xil_printf("Receiving Data from TX_FIFO\n\r");
  334.  
  335.     /* Read Recieve Length */
  336.     ReceiveLength = (XLlFifo_iRxGetLen(InstancePtr))/WORD_SIZE;
  337.     xil_printf("rx receive length : %d\n\r", (int)ReceiveLength);
  338.     xil_printf("rd fifo occupancy : %d\n\r",(int)XLlFifo_iRxOccupancy(InstancePtr));
  339. //  while(XLlFifo_iRxOccupancy(InstancePtr)) {
  340.         for (i=0; i < ReceiveLength; i++) {
  341.                 RxWord = XLlFifo_RxGetWord(InstancePtr);
  342.                 *(DestinationBuffer+i) = RxWord;
  343.         }
  344. //  }
  345. }
  346.  
  347. static void FifoHandler(XLlFifo *InstancePtr)
  348. {
  349.     u32 Pending;
  350.  
  351.     Pending = XLlFifo_IntPending(InstancePtr);
  352.     while (Pending) {
  353.         if (Pending & XLLF_INT_RC_MASK) {
  354.             FifoRecvHandler(InstancePtr);
  355.             XLlFifo_IntClear(InstancePtr, XLLF_INT_RC_MASK);
  356.         }
  357.         else if (Pending & XLLF_INT_TC_MASK) {
  358.             FifoSendHandler(InstancePtr);
  359.         }
  360.         else if (Pending & XLLF_INT_ERROR_MASK){
  361.             FifoErrorHandler(InstancePtr, Pending);
  362.             XLlFifo_IntClear(InstancePtr, XLLF_INT_ERROR_MASK);
  363.         } else {
  364.             XLlFifo_IntClear(InstancePtr, Pending);
  365.         }
  366.         Pending = XLlFifo_IntPending(InstancePtr);
  367.     }
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement