annotate py/touch/touch.c @ 612:5fc41e0833b4 rust

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