pio_test.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #define _BSD_SOURCE
  2. #define _POSIX_C_SOURCE 199309L
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <stdarg.h>
  7. #include <time.h>
  8. #include <sched.h>
  9. #include <sys/time.h>
  10. #include "pcilib.h"
  11. #include "irq.h"
  12. #include "kmem.h"
  13. #define DEVICE "/dev/fpga0"
  14. //#define REALTIME
  15. #define BAR PCILIB_BAR0
  16. #define BITS 32
  17. #define MASK ((1ll << BITS) - 1)
  18. #define WR(addr, value) { *(uint32_t*)(bar + addr) = value; }
  19. #define RD(addr, value) { value = *(uint32_t*)(bar + addr); }
  20. unsigned long long bits[BITS];
  21. int main(int argc, char *argv[]) {
  22. uint32_t i, j;
  23. pcilib_t *pci;
  24. uint32_t reg, value, diff, errors;
  25. void* volatile bar;
  26. unsigned long long attempts = 0, failures = 0;
  27. if (argc < 2) {
  28. printf("Usage: %s <register>\n", argv[0]);
  29. exit(0);
  30. }
  31. if (sscanf(argv[1], "%x", &reg) != 1) {
  32. printf("Can't parse register %s\n", argv[1]);
  33. exit(1);
  34. }
  35. #ifdef REALTIME
  36. pid_t pid;
  37. struct sched_param sched = {0};
  38. pid = getpid();
  39. sched.sched_priority = sched_get_priority_min(SCHED_FIFO);
  40. if (sched_setscheduler(pid, SCHED_FIFO, &sched))
  41. printf("Warning: not able to get real-time priority\n");
  42. #endif /* REALTIME */
  43. pci = pcilib_open(DEVICE, PCILIB_MODEL_DETECT);
  44. if (!pci) {
  45. printf("pcilib_open\n");
  46. exit(1);
  47. }
  48. bar = pcilib_map_bar(pci, BAR);
  49. if (!bar) {
  50. pcilib_close(pci);
  51. printf("map bar\n");
  52. exit(1);
  53. }
  54. for (i = 0; 1; i++) {
  55. WR(reg, (i%MASK));
  56. RD(reg, value);
  57. attempts++;
  58. if (value != (i%MASK)) {
  59. failures++;
  60. diff = value ^ (i%MASK);
  61. for (errors = 0, j = 0; j < BITS; j++)
  62. if (diff&(1<<j)) errors++;
  63. bits[errors]++;
  64. //printf("written: %x, read: %x\n", i, value);
  65. }
  66. if ((i % 1048576 ) == 0) {
  67. printf("Attempts %llu, failures %llu (", attempts, failures);
  68. for (j = 1; j < BITS; j++)
  69. printf("%llu ", bits[j]);
  70. printf(")\n");
  71. }
  72. }
  73. pcilib_unmap_bar(pci, BAR, bar);
  74. pcilib_close(pci);
  75. }