Advertisement
Guest User

xdma_test

a guest
May 18th, 2021
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.89 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <fcntl.h>
  4. #include <errno.h>
  5. #include <unistd.h>
  6. #include <assert.h>
  7. #include <string.h>
  8.  
  9. #define BRAM_0_ADDRESS 0x0000000
  10. #define BRAM_1_ADDRESS 0x0010000
  11. #define MEM_DDRC       0x80000000
  12. #define TOTAL_MEMORY 3
  13. const unsigned mem_addr[TOTAL_MEMORY] = {BRAM_0_ADDRESS, BRAM_1_ADDRESS, MEM_DDRC};
  14.  
  15. // randomize a string of size <size>
  16. static char *rand_str(char *str, size_t size)
  17. {
  18.     const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRTSUVWXYZ1234567890";
  19.     int i;
  20.  
  21.     for (i = 0; i < size; i++)
  22.     {
  23.         int key = rand() % (int)(sizeof charset - 1);
  24.         str[i] = charset[key];
  25.     }
  26.  
  27.     str[size - 1] = '\0';
  28.     return str;
  29. }
  30.  
  31. int main(int argc, char *argv[])
  32. {
  33.     char *srcBuf = NULL;
  34.     char *dstBuf = NULL;
  35.     int read_fd;
  36.     int write_fd;
  37.     int ret;
  38.  
  39.     int buf_size = 20;
  40.     int n_transfers = 10;
  41.     if (argc >= 2)
  42.     {
  43.         n_transfers = atoi(argv[1]);
  44.     }
  45.     if (argc == 3)
  46.     {
  47.         buf_size = atoi(argv[2]);
  48.     }
  49.  
  50.     printf("n_transfers = %d, buf_size = %d\n", n_transfers, buf_size);
  51.  
  52.     srcBuf = (char *)malloc(buf_size * sizeof(char));
  53.     dstBuf = (char *)malloc(buf_size * sizeof(char));
  54.  
  55.  
  56.     /* Open a XDMA write channel (Host to Core) */
  57.     if ((write_fd = open("/dev/xdma0_h2c_0", O_WRONLY)) == -1)
  58.     {
  59.         perror("open failed with errno");
  60.     }
  61.  
  62.     /* Open a XDMA read channel (Core to Host) */
  63.     if ((read_fd = open("/dev/xdma0_c2h_0", O_RDONLY)) == -1)
  64.     {
  65.         perror("open failed with errno");
  66.     }
  67.  
  68.     for (int i = 0; i < n_transfers; i++)
  69.     {
  70.         /* Initialize srcBuf */
  71.         rand_str(srcBuf, buf_size);
  72.         u_int32_t address = mem_addr[i % TOTAL_MEMORY];
  73.  
  74.         /* Write the entire source buffer to offset OFFSET_IN_FPGA_DRAM */
  75.         ret = pwrite(write_fd, srcBuf, buf_size, address);
  76.         if (ret < 0)
  77.         {
  78.             printf("PWrite failed to address 0x%x with code %d\n", address, errno);
  79.             printf("Error: %s\n", strerror(errno));
  80.         }
  81.  
  82.         printf("Tried to write %u bytes to address 0x%x, succeeded in writing %u bytes\n", buf_size, address, ret);
  83.  
  84.         ret = pread(read_fd, dstBuf, buf_size, address);
  85.         if (ret < 0)
  86.         {
  87.             printf("PRead failed to address 0x%x with code %d\n", address, errno);
  88.             printf("Error: %s\n", strerror(errno));
  89.         }
  90.  
  91.         // printf("Tried to read %u bytes to address 0x%x, succeeded in reading %u bytes\n", buf_size, address, ret);
  92.  
  93.         if (strcmp(srcBuf, dstBuf) != 0)
  94.         {
  95.             printf("Strings don't match, address 0x%x\n", address);
  96.         }
  97.     }
  98.  
  99.     if (close(write_fd) < 0)
  100.     {
  101.         perror("write_fd close failed with errno");
  102.     }
  103.     if (close(read_fd) < 0)
  104.     {
  105.         perror("read_fd close failed with errno");
  106.     }
  107.  
  108.     return 0;
  109. }
  110.  
  111.  
  112. /*
  113. n_transfers = 10, buf_size = 20
  114. Tried to write 20 bytes to address 0x0, succeeded in writing 20 bytes
  115. Strings don't match, address 0x0
  116. Tried to write 20 bytes to address 0x10000, succeeded in writing 20 bytes
  117. Strings don't match, address 0x10000
  118. Tried to write 20 bytes to address 0x80000000, succeeded in writing 20 bytes
  119. Tried to write 20 bytes to address 0x0, succeeded in writing 20 bytes
  120. Strings don't match, address 0x0
  121. Tried to write 20 bytes to address 0x10000, succeeded in writing 20 bytes
  122. Strings don't match, address 0x10000
  123. Tried to write 20 bytes to address 0x80000000, succeeded in writing 20 bytes
  124. Tried to write 20 bytes to address 0x0, succeeded in writing 20 bytes
  125. Strings don't match, address 0x0
  126. Tried to write 20 bytes to address 0x10000, succeeded in writing 20 bytes
  127. Strings don't match, address 0x10000
  128. Tried to write 20 bytes to address 0x80000000, succeeded in writing 20 bytes
  129. Tried to write 20 bytes to address 0x0, succeeded in writing 20 bytes
  130. Strings don't match, address 0x0
  131. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement