/alps/ipecamera

To get this branch, use:
bzr branch http://darksoft.org/webbzr/alps/ipecamera

« back to all changes in this revision

Viewing changes to apps/pio_test.c

  • Committer: Suren A. Chilingaryan
  • Date: 2015-04-27 00:28:57 UTC
  • Revision ID: csa@suren.me-20150427002857-82fk6r3e8rfgy4wr
First stand-alone ipecamera implementation

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
11
 
#include "pcilib.h"
12
 
#include "irq.h"
13
 
#include "kmem.h"
14
 
 
15
 
#define DEVICE "/dev/fpga0"
16
 
//#define REALTIME
17
 
 
18
 
#define BAR PCILIB_BAR0
19
 
#define BITS 32
20
 
#define MASK ((1ll << BITS) - 1)
21
 
 
22
 
 
23
 
#define WR(addr, value) { *(uint32_t*)(bar + addr) = value; }
24
 
#define RD(addr, value) { value = *(uint32_t*)(bar + addr); }
25
 
 
26
 
unsigned long long bits[BITS];
27
 
 
28
 
int main(int argc, char *argv[]) {
29
 
    uint32_t i, j;
30
 
    pcilib_t *pci;
31
 
    uint32_t reg, value, diff, errors;
32
 
    void* volatile bar;
33
 
    
34
 
    unsigned long long attempts = 0, failures = 0;
35
 
 
36
 
    if (argc < 2) {
37
 
        printf("Usage: %s <register>\n", argv[0]);
38
 
        exit(0);
39
 
    }
40
 
    
41
 
    if (sscanf(argv[1], "%x", &reg) != 1) {
42
 
        printf("Can't parse register %s\n", argv[1]);
43
 
        exit(1);
44
 
    }
45
 
    
46
 
#ifdef REALTIME
47
 
    pid_t pid;
48
 
    struct sched_param sched = {0};
49
 
 
50
 
    pid = getpid();
51
 
    sched.sched_priority = sched_get_priority_min(SCHED_FIFO);
52
 
    if (sched_setscheduler(pid, SCHED_FIFO, &sched))
53
 
        printf("Warning: not able to get real-time priority\n");
54
 
#endif /* REALTIME */
55
 
 
56
 
    pci = pcilib_open(DEVICE, PCILIB_MODEL_DETECT);
57
 
    if (!pci) {
58
 
        printf("pcilib_open\n");
59
 
        exit(1);
60
 
    }
61
 
 
62
 
    bar = pcilib_map_bar(pci, BAR);
63
 
    if (!bar) {
64
 
        pcilib_close(pci);
65
 
        printf("map bar\n");
66
 
        exit(1);
67
 
    }
68
 
    
69
 
    for (i = 0; 1; i++) {
70
 
        WR(reg, (i%MASK));
71
 
        RD(reg, value);
72
 
        
73
 
        attempts++;
74
 
        if (value != (i%MASK)) {
75
 
            failures++;
76
 
 
77
 
            diff = value ^ (i%MASK);
78
 
            for (errors = 0, j = 0; j < BITS; j++)
79
 
                if (diff&(1<<j)) errors++;
80
 
            bits[errors]++;
81
 
                
82
 
            //printf("written: %x, read: %x\n", i, value);
83
 
        }
84
 
 
85
 
        if ((i % 1048576 ) == 0) {
86
 
            printf("Attempts %llu, failures %llu (", attempts, failures);
87
 
            for (j = 1; j < BITS; j++)
88
 
                printf("%llu ", bits[j]);
89
 
            printf(")\n");
90
 
        }
91
 
 
92
 
    }
93
 
 
94
 
    pcilib_unmap_bar(pci, BAR, bar);
95
 
    pcilib_close(pci);
96
 
}