Advertisement
Matqux

Untitled

Feb 25th, 2024
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.91 KB | None | 0 0
  1. /***************************** Include Files *********************************/
  2. #include "xaxidma.h"
  3. #include "xparameters.h"
  4. #include "xdebug.h"
  5. #include "sleep.h"
  6.  
  7. #if defined(XPAR_UARTNS550_0_BASEADDR)
  8. #include "xuartns550_l.h"       /* to use uartns550 */
  9. #endif
  10.  
  11. /******************** Constant Definitions **********************************/
  12.  
  13. /*
  14.  * Device hardware build related constants.
  15.  */
  16.  
  17. #define DMA_DEV_ID      XPAR_AXIDMA_0_DEVICE_ID
  18.  
  19. #ifdef XPAR_AXI_7SDDR_0_S_AXI_BASEADDR
  20. #define DDR_BASE_ADDR       XPAR_AXI_7SDDR_0_S_AXI_BASEADDR
  21. #elif defined (XPAR_MIG7SERIES_0_BASEADDR)
  22. #define DDR_BASE_ADDR   XPAR_MIG7SERIES_0_BASEADDR
  23. #elif defined (XPAR_MIG_0_C0_DDR4_MEMORY_MAP_BASEADDR)
  24. #define DDR_BASE_ADDR   XPAR_MIG_0_C0_DDR4_MEMORY_MAP_BASEADDR
  25. #elif defined (XPAR_PSU_DDR_0_S_AXI_BASEADDR)
  26. #define DDR_BASE_ADDR   XPAR_PSU_DDR_0_S_AXI_BASEADDR
  27. #endif
  28.  
  29. #ifndef DDR_BASE_ADDR
  30. #warning CHECK FOR THE VALID DDR ADDRESS IN XPARAMETERS.H, \
  31.          DEFAULT SET TO 0x01000000
  32. #define MEM_BASE_ADDR       0x01000000
  33. #else
  34. #define MEM_BASE_ADDR       (DDR_BASE_ADDR + 0x1000000)
  35. #endif
  36.  
  37. #define TX_BUFFER_BASE      (MEM_BASE_ADDR + 0x00100000)
  38. #define RX_BUFFER_BASE      (MEM_BASE_ADDR + 0x00300000)
  39. #define RX_BUFFER_HIGH      (MEM_BASE_ADDR + 0x004FFFFF)
  40.  
  41. #define MAX_PKT_LEN     1024
  42.  
  43. #define TEST_START_VALUE    0xC
  44.  
  45. #define NUMBER_OF_TRANSFERS 1
  46. #define POLL_TIMEOUT_COUNTER    1000000U
  47.  
  48. /**************************** Type Definitions *******************************/
  49.  
  50.  
  51. /***************** Macros (Inline Functions) Definitions *********************/
  52.  
  53.  
  54. /************************** Function Prototypes ******************************/
  55.  
  56. #if (!defined(DEBUG))
  57. extern void xil_printf(const char *format, ...);
  58. #endif
  59.  
  60. int XAxiDma_SimplePollExample(u16 DeviceId);
  61. static int CheckData(void);
  62.  
  63. /************************** Variable Definitions *****************************/
  64. /*
  65.  * Device instance definitions
  66.  */
  67. XAxiDma AxiDma;
  68.  
  69.  
  70. /*****************************************************************************/
  71. /**
  72. * The entry point for this example. It invokes the example function,
  73. * and reports the execution status.
  74. *
  75. * @param    None.
  76. *
  77. * @return
  78. *       - XST_SUCCESS if example finishes successfully
  79. *       - XST_FAILURE if example fails.
  80. *
  81. * @note     None.
  82. *
  83. ******************************************************************************/
  84. int main()
  85. {
  86.     int Status;
  87.  
  88.     xil_printf("\r\n--- Entering main() --- \r\n");
  89.  
  90.     /* Run the poll example for simple transfer */
  91.     Status = XAxiDma_SimplePollExample(DMA_DEV_ID);
  92.  
  93.     if (Status != XST_SUCCESS) {
  94.         xil_printf("XAxiDma_SimplePoll Example Failed\r\n");
  95.         return XST_FAILURE;
  96.     }
  97.  
  98.     xil_printf("Successfully ran XAxiDma_SimplePoll Example\r\n");
  99.  
  100.     xil_printf("--- Exiting main() --- \r\n");
  101.  
  102.     return XST_SUCCESS;
  103.  
  104. }
  105.  
  106. #if defined(XPAR_UARTNS550_0_BASEADDR)
  107. /*****************************************************************************/
  108. /*
  109. *
  110. * Uart16550 setup routine, need to set baudrate to 9600, and data bits to 8
  111. *
  112. * @param    None.
  113. *
  114. * @return   None
  115. *
  116. * @note     None.
  117. *
  118. ******************************************************************************/
  119. static void Uart550_Setup(void)
  120. {
  121.  
  122.     /* Set the baudrate to be predictable
  123.      */
  124.     XUartNs550_SetBaud(XPAR_UARTNS550_0_BASEADDR,
  125.             XPAR_XUARTNS550_CLOCK_HZ, 9600);
  126.  
  127.     XUartNs550_SetLineControlReg(XPAR_UARTNS550_0_BASEADDR,
  128.             XUN_LCR_8_DATA_BITS);
  129.  
  130. }
  131. #endif
  132.  
  133. /*****************************************************************************/
  134. /**
  135. * The example to do the simple transfer through polling. The constant
  136. * NUMBER_OF_TRANSFERS defines how many times a simple transfer is repeated.
  137. *
  138. * @param    DeviceId is the Device Id of the XAxiDma instance
  139. *
  140. * @return
  141. *       - XST_SUCCESS if example finishes successfully
  142. *       - XST_FAILURE if error occurs
  143. *
  144. * @note     None
  145. *
  146. *
  147. ******************************************************************************/
  148. int XAxiDma_SimplePollExample(u16 DeviceId)
  149. {
  150.     XAxiDma_Config *CfgPtr;
  151.     int Status;
  152.     int Tries = NUMBER_OF_TRANSFERS;
  153.     int Index;
  154.     u8 *TxBufferPtr;
  155.     u8 *RxBufferPtr;
  156.     u8 Value;
  157.     int TimeOut = POLL_TIMEOUT_COUNTER;
  158.  
  159.     TxBufferPtr = (u8 *)TX_BUFFER_BASE ;
  160.     RxBufferPtr = (u8 *)RX_BUFFER_BASE;
  161.  
  162.     /* Initialize the XAxiDma device.
  163.      */
  164.     CfgPtr = XAxiDma_LookupConfig(DeviceId);
  165.     if (!CfgPtr) {
  166.         xil_printf("No config found for %d\r\n", DeviceId);
  167.         return XST_FAILURE;
  168.     }
  169.  
  170.     Status = XAxiDma_CfgInitialize(&AxiDma, CfgPtr);
  171.     if (Status != XST_SUCCESS) {
  172.         xil_printf("Initialization failed %d\r\n", Status);
  173.         return XST_FAILURE;
  174.     }
  175.  
  176.     if(XAxiDma_HasSg(&AxiDma)){
  177.         xil_printf("Device configured as SG mode \r\n");
  178.         return XST_FAILURE;
  179.     }
  180.  
  181.     /* Disable interrupts, we use polling mode
  182.      */
  183.     XAxiDma_IntrDisable(&AxiDma, XAXIDMA_IRQ_ALL_MASK,
  184.                         XAXIDMA_DEVICE_TO_DMA);
  185.     XAxiDma_IntrDisable(&AxiDma, XAXIDMA_IRQ_ALL_MASK,
  186.                         XAXIDMA_DMA_TO_DEVICE);
  187.  
  188.     Value = TEST_START_VALUE;
  189.  
  190.     for(Index = 0; Index < MAX_PKT_LEN; Index ++) {
  191.             TxBufferPtr[Index] = Value;
  192.  
  193.             Value = (Value + 1) & 0xFF;
  194.     }
  195.     /* Flush the buffers before the DMA transfer, in case the Data Cache
  196.      * is enabled
  197.      */
  198.     Xil_DCacheFlushRange((UINTPTR)TxBufferPtr, MAX_PKT_LEN);
  199.     Xil_DCacheFlushRange((UINTPTR)RxBufferPtr, MAX_PKT_LEN);
  200.  
  201.     for(Index = 0; Index < Tries; Index ++) {
  202.  
  203.  
  204.         Status = XAxiDma_SimpleTransfer(&AxiDma,(UINTPTR) RxBufferPtr,
  205.                     MAX_PKT_LEN, XAXIDMA_DEVICE_TO_DMA);
  206.  
  207.         if (Status != XST_SUCCESS) {
  208.             return XST_FAILURE;
  209.         }
  210.  
  211.         Status = XAxiDma_SimpleTransfer(&AxiDma,(UINTPTR) TxBufferPtr,
  212.                     MAX_PKT_LEN, XAXIDMA_DMA_TO_DEVICE);
  213.  
  214.         if (Status != XST_SUCCESS) {
  215.             return XST_FAILURE;
  216.         }
  217.  
  218.         /*Wait till tranfer is done or 1usec * 10^6 iterations of timeout occurs*/
  219.         while (TimeOut) {
  220.             if (!(XAxiDma_Busy(&AxiDma,XAXIDMA_DEVICE_TO_DMA)) &&
  221.             !(XAxiDma_Busy(&AxiDma,XAXIDMA_DMA_TO_DEVICE)))
  222.                 break;
  223.             TimeOut--;
  224.             //usleep(1U);
  225.         }
  226.  
  227.         //Status = CheckData();
  228.         //if (Status != XST_SUCCESS) {
  229.             //return XST_FAILURE;
  230.         //}
  231.  
  232.     }
  233.  
  234.     /* Test finishes successfully
  235.      */
  236.     return XST_SUCCESS;
  237. }
  238.  
  239.  
  240.  
  241. /*****************************************************************************/
  242. /*
  243. *
  244. * This function checks data buffer after the DMA transfer is finished.
  245. *
  246. * @param    None
  247. *
  248. * @return
  249. *       - XST_SUCCESS if validation is successful.
  250. *       - XST_FAILURE otherwise.
  251. *
  252. * @note     None.
  253. *
  254. ******************************************************************************/
  255. static int CheckData(void)
  256. {
  257.     u8 *RxPacket;
  258.     int Index = 0;
  259.     u8 Value;
  260.  
  261.     RxPacket = (u8 *) RX_BUFFER_BASE;
  262.     Value = TEST_START_VALUE;
  263.  
  264.     /* Invalidate the DestBuffer before receiving the data, in case the
  265.      * Data Cache is enabled
  266.      */
  267.     Xil_DCacheInvalidateRange((UINTPTR)RxPacket, MAX_PKT_LEN);
  268.  
  269.     for(Index = 0; Index < MAX_PKT_LEN; Index++) {
  270.         if (RxPacket[Index] != Value) {
  271.             xil_printf("Data error %d: %x/%x\r\n",
  272.             Index, (unsigned int)RxPacket[Index],
  273.                 (unsigned int)Value);
  274.  
  275.             return XST_FAILURE;
  276.         }
  277.         Value = (Value + 1) & 0xFF;
  278.     }
  279.  
  280.     return XST_SUCCESS;
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement