summaryrefslogtreecommitdiffstats
path: root/apps/pio_test.c
blob: 7eca362d5a3201546ad87eff9cf3544194fdb810 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#define _BSD_SOURCE
#define _POSIX_C_SOURCE 199309L
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <time.h>
#include <sched.h>
#include <sys/time.h>

#include "pcilib.h"
#include "irq.h"
#include "kmem.h"

#define DEVICE "/dev/fpga0"
//#define REALTIME

#define BAR PCILIB_BAR0
#define BITS 16
#define MASK ((1ll << BITS) - 1)


#define WR(addr, value) { *(uint32_t*)(bar + addr) = value; }
#define RD(addr, value) { value = *(uint32_t*)(bar + addr); }

unsigned long long bits[BITS];

int main(int argc, char *argv[]) {
    uint32_t i, j;
    pcilib_t *pci;
    uint32_t reg, value, diff, errors;
    void* volatile bar;
    
    unsigned long long attempts = 0, failures = 0;

    if (argc < 2) {
	printf("Usage: %s <register>\n", argv[0]);
	exit(0);
    }
    
    if (sscanf(argv[1], "%x", &reg) != 1) {
	printf("Can't parse register %s\n", argv[1]);
	exit(1);
    }
    
#ifdef REALTIME
    pid_t pid;
    struct sched_param sched = {0};

    pid = getpid();
    sched.sched_priority = sched_get_priority_min(SCHED_FIFO);
    if (sched_setscheduler(pid, SCHED_FIFO, &sched))
	printf("Warning: not able to get real-time priority\n");
#endif /* REALTIME */

    pci = pcilib_open(DEVICE, PCILIB_MODEL_DETECT);
    if (!pci) {
	printf("pcilib_open\n");
	exit(1);
    }

    bar = pcilib_map_bar(pci, BAR);
    if (!bar) {
	pcilib_close(pci);
	printf("map bar\n");
	exit(1);
    }
    
    for (i = 0; 1; i++) {
	WR(reg, (i%MASK));
	RD(reg, value);
	
	attempts++;
	if (value != (i%MASK)) {
	    failures++;

	    diff = value ^ (i%MASK);
	    for (errors = 0, j = 0; j < BITS; j++)
		if (diff&(1<<j)) errors++;
	    bits[errors]++;
		
	    //printf("written: %x, read: %x\n", i, value);
	}

	if ((i % 1048576 ) == 0) {
	    printf("Attempts %llu, failures %llu (", attempts, failures);
	    for (j = 1; j < BITS; j++)
		printf("%llu ", bits[j]);
	    printf(")\n");
	}

    }

    pcilib_unmap_bar(pci, BAR, bar);
    pcilib_close(pci);
}