add op_lidstate
[pandora-misc.git] / tests / test_gpio.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/time.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <sys/mman.h>
9 #include <sys/ioctl.h>
10 #include <unistd.h>
11 #include <errno.h>
12 #include <linux/fb.h>
13 #include <time.h>
14
15
16 int devopen(const char *path)
17 {
18         int dev;
19
20         dev = open(path, O_RDWR|O_SYNC);
21         if (dev == -1)
22         {
23                 printf("open(\"%s\") failed with %i\n", path, errno);
24                 exit(1);
25         }
26
27         return dev;
28 }
29
30
31 volatile void *xmmap(int dev, int addr, int size)
32 {
33         volatile void *ret;
34         ret = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, dev, addr);
35         //printf("memregs are @ %p\n", memregs);
36         if (ret == MAP_FAILED)
37         {
38                 perror("mmap(memregs) failed");
39                 exit(1);
40         }
41         return ret;
42 }
43
44 static unsigned int get_us(void)
45 {
46         struct timeval tv;
47
48         gettimeofday(&tv, NULL);
49         return tv.tv_sec * 1000000 + tv.tv_usec;
50 }
51
52 #define LOG_BUF 16
53
54 int main(int argc, char *argv[])
55 {
56         volatile unsigned int *memregs;
57         unsigned int val1 = 0, us, prev_us;
58         int reads = 0;
59         int memdev;
60         struct {
61                 unsigned int val_old, val, time, rcount;
62         } log[LOG_BUF];
63         int i, lp = 0;
64
65
66         if (argv[1] == NULL) {
67                 fprintf(stderr, "usage:\n%s <gpio_bank_base>\n", argv[0]);
68                 return 1;
69         }
70
71         memdev = devopen("/dev/mem");
72
73         memregs = xmmap(memdev, strtoul(argv[1], NULL, 0), 0x1000);
74
75         //memregs[0x5403c] = 0;
76         //memregs[0x54090] = 0xffffffff;
77
78         printf("SYSCONFIG: %08x\n", memregs[0x010>>2]);
79         printf("OE:        %08x\n", memregs[0x034>>2]);
80 //      printf("LEVELDET0: %08x\n", memregs[0x040>>2]);
81 //      printf("LEVELDET1: %08x\n", memregs[0x044>>2]);
82         printf("DEBOUNCEN: %08x\n", memregs[0x050>>2]);
83
84 //      memregs[0x54040>>2] = 0xff00;
85 //      memregs[0x54044>>2] = 0xff00;
86         //memregs[0x54010>>2] = 0x0c; // no idle, wakeup enable
87         //while (val == memregs[0x54038>>2]);
88
89         prev_us = get_us();
90
91         while (1)
92         {
93                 unsigned int v1 = memregs[0x038>>2];
94                 reads++;
95
96                 if (v1 != val1) {
97                         us = get_us();
98                         log[lp].val_old = val1;
99                         log[lp].val = v1;
100                         log[lp].time = us - prev_us;
101                         log[lp].rcount = reads;
102                         val1 = v1;
103                         reads = 0;
104                         prev_us = us;
105
106                         lp++;
107                         if (lp == LOG_BUF) {
108                                 for (i = 0; i < LOG_BUF; i++)
109                                         printf("%08x -> %08x [%7u %d]\n",
110                                                 log[i].val_old, log[i].val,
111                                                 log[i].time, log[i].rcount);
112                                 lp = 0;
113                                 if (LOG_BUF > 1)
114                                         printf("-\n");
115                         }
116                 }
117                 //usleep(1);
118         }
119
120         return 0;
121 }
122