annotate py/touch/touch.c @ 521:94965c99b917

screenshot of parameter UI
author Matt Johnston <matt@ucc.asn.au>
date Fri, 19 Dec 2014 22:50:37 +0800
parents e12d6d677320
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 //
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 // How to access GPIO registers from C-code on the Raspberry-Pi
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 // Example program
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 // 15-January-2012
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 // Dom and Gert
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 //
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 // Access from ARM Running Linux
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 #define BCM2708_PERI_BASE 0x20000000
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14
455
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
15 // for usleep
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
16 #define _BSD_SOURCE
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
17
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
18 // clock_gettime
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
19 #define _POSIX_C_SOURCE 199309L
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
20
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 #include <stdio.h>
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 #include <string.h>
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 #include <stdlib.h>
455
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
24 #include <stdint.h>
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 #include <dirent.h>
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 #include <fcntl.h>
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 #include <assert.h>
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 #include <sys/mman.h>
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 #include <sys/types.h>
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 #include <sys/stat.h>
455
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
31 #include <time.h>
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
32 #include <sched.h>
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 #include <unistd.h>
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 #define PAGE_SIZE (4*1024)
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 #define BLOCK_SIZE (4*1024)
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 int mem_fd;
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 char *gpio_mem, *gpio_map;
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 char *spi0_mem, *spi0_map;
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 // I/O access
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 volatile unsigned *gpio;
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or SET_GPIO_ALT(x,y)
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 #define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 #define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3))
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 #define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 #define FSEL_OFFSET 0 // 0x0000
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 #define SET_OFFSET 7 // 0x001c / 4
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 #define CLR_OFFSET 10 // 0x0028 / 4
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 #define PINLEVEL_OFFSET 13 // 0x0034 / 4
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 #define EVENT_DETECT_OFFSET 16 // 0x0040 / 4
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 #define RISING_ED_OFFSET 19 // 0x004c / 4
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 #define FALLING_ED_OFFSET 22 // 0x0058 / 4
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 #define HIGH_DETECT_OFFSET 25 // 0x0064 / 4
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 #define LOW_DETECT_OFFSET 28 // 0x0070 / 4
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 #define PULLUPDN_OFFSET 37 // 0x0094 / 4
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 #define PULLUPDNCLK_OFFSET 38 // 0x0098 / 4
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 #define GPIO_GET(g) ((*(gpio+PINLEVEL_OFFSET) & (1<<(g))) >> (g))
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 #define GPIO_SET(g) (*(gpio+SET_OFFSET) = 1<<(g)) // sets bits which are 1 ignores bits which are 0
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 #define GPIO_CLR(g) (*(gpio+CLR_OFFSET) = 1<<(g))// clears bits which are 1 ignores bits which are 0
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 #define TOUCH_IN 24
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 #define TOUCH_OUT 25
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 void setup_io();
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74
455
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
75 static int cmp_int(const void *a, const void *b)
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
76 {
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
77 const int *ia = a;
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
78 const int *ib = b;
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
79 return (*ia - *ib);
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
80 }
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
81
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
82 static int clock_diff(struct timespec *t1, struct timespec *t2)
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
83 {
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
84 uint64_t v1 = t1->tv_sec * 1000000000 + t1->tv_nsec;
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
85 uint64_t v2 = t2->tv_sec * 1000000000 + t2->tv_nsec;
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
86 return v2-v1;
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
87 }
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
88
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 int main(int argc, char **argv)
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 {
455
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
91 // Set up gpi pointer for direct register access
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
92 setup_io();
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 INP_GPIO(TOUCH_IN);
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 INP_GPIO(TOUCH_OUT);
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 OUT_GPIO(TOUCH_OUT);
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97
455
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
98 const int num = 200;
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
99 const int margin = 30;
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
100 int print = 0;
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
101 if (argc > 1)
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
102 {
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
103 print = 1;
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
104 }
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 while (1)
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 {
455
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
108 int vals[num];
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
109 int nsecs[num];
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
110 for (int n = 0; n < num; n++)
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
111 {
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 GPIO_CLR(TOUCH_OUT);
455
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
113 struct timespec t = {.tv_nsec=20};
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
114 nanosleep(&t, NULL);
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
115 //sched_yield();
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
116 //usleep(1);
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
117 if (GPIO_GET(TOUCH_IN))
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
118 {
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
119 printf("short ");
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
120 }
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 GPIO_SET(TOUCH_OUT);
455
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
122 int val = 0;
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 while (GPIO_GET(TOUCH_IN) == 0)
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 {
455
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
125 val++;
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
126 }
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
127
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
128 vals[n] = val;
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
129 }
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
130 qsort(vals, num, sizeof(*vals), cmp_int);
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
131 int sum = 0, count = 0;
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
132 for (int n = 0; n < num; n++)
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
133 {
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
134 if (print)
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
135 {
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
136 printf("%3d ", vals[n]);
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
137 if (n == num-1 || n % 10 == 9)
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
138 {
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
139 printf("\n");
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
140 }
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
141 if (n == margin || n == num-margin)
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
142 {
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
143 printf("#");
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
144 }
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
145 }
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
146 if (n >= margin && n < (num-margin))
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
147 {
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
148 sum += vals[n];
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
149 count ++;
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150 }
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151 }
455
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
152 printf("total %f from %d\n", (float)sum / count, count);
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
153 usleep(500000);
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
154 }
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155 return 0;
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157 } // main
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160 //
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161 // Set up a memory regions to access GPIO
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162 //
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163 void setup_io()
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164 {
455
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
165 /* open /dev/mem */
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
166 if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
167 printf("can't open /dev/mem \n");
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
168 exit (-1);
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
169 }
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170
455
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
171 /* mmap GPIO */
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172
455
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
173 // Allocate MAP block
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
174 if ((gpio_mem = malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) {
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
175 printf("allocation error \n");
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
176 exit (-1);
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
177 }
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178
455
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
179 // Make sure pointer is on 4K boundary
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
180 if ((unsigned long)gpio_mem % PAGE_SIZE)
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
181 gpio_mem += PAGE_SIZE - ((unsigned long)gpio_mem % PAGE_SIZE);
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182
455
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
183 // Now map it
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
184 gpio_map = mmap(
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
185 (caddr_t)gpio_mem,
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
186 BLOCK_SIZE,
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
187 PROT_READ|PROT_WRITE,
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
188 MAP_SHARED|MAP_FIXED,
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
189 mem_fd,
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
190 GPIO_BASE
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
191 );
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192
455
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
193 if ((long)gpio_map < 0) {
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
194 printf("mmap error %d\n", (int)gpio_map);
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
195 exit (-1);
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
196 }
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
197
455
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
198 // Always use volatile pointer!
e12d6d677320 Try some other ways with touch sensing. Linux scheduling isn't fast enough
Matt Johnston <matt@ucc.asn.au>
parents: 454
diff changeset
199 gpio = (volatile unsigned *)gpio_map;
454
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
200
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
201
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
202 } // setup_io