pio_test.c 1.9 KB

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