annotate py/touch/touch.c @ 454:dce9f7841696

rough touch sensor
author Matt Johnston <matt@ucc.asn.au>
date Fri, 21 Dec 2012 23:30:16 +0800
parents
children bed65c321b46
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
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 #include <stdio.h>
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 #include <string.h>
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 #include <stdlib.h>
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 #include <dirent.h>
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 #include <fcntl.h>
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 #include <assert.h>
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 #include <sys/mman.h>
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 #include <sys/types.h>
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 #include <sys/stat.h>
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 #include <unistd.h>
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 #define PAGE_SIZE (4*1024)
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 #define BLOCK_SIZE (4*1024)
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 int mem_fd;
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 char *gpio_mem, *gpio_map;
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 char *spi0_mem, *spi0_map;
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
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 // I/O access
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 volatile unsigned *gpio;
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37
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 // 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
40 #define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 #define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3))
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 #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
43
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 #define FSEL_OFFSET 0 // 0x0000
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 #define SET_OFFSET 7 // 0x001c / 4
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 #define CLR_OFFSET 10 // 0x0028 / 4
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 #define PINLEVEL_OFFSET 13 // 0x0034 / 4
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 #define EVENT_DETECT_OFFSET 16 // 0x0040 / 4
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 #define RISING_ED_OFFSET 19 // 0x004c / 4
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 #define FALLING_ED_OFFSET 22 // 0x0058 / 4
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 #define HIGH_DETECT_OFFSET 25 // 0x0064 / 4
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 #define LOW_DETECT_OFFSET 28 // 0x0070 / 4
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 #define PULLUPDN_OFFSET 37 // 0x0094 / 4
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 #define PULLUPDNCLK_OFFSET 38 // 0x0098 / 4
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 #define GPIO_GET(g) ((*(gpio+PINLEVEL_OFFSET) & (1<<(g))) >> (g))
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 #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
59 #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
60
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 #define TOUCH_IN 24
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 #define TOUCH_OUT 25
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 void setup_io();
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 int main(int argc, char **argv)
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 {
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 int g,rep;
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 // Set up gpi pointer for direct register access
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 setup_io();
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 // Switch GPIO 7..11 to output mode
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 /************************************************************************\
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 * You are about to change the GPIO settings of your computer. *
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 * Mess this up and it will stop working! *
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 * It might be a good idea to 'sync' before running this program *
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 * so at least you still have your code changes written to the SD-card! *
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 \************************************************************************/
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 INP_GPIO(TOUCH_IN);
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 INP_GPIO(TOUCH_OUT);
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 OUT_GPIO(TOUCH_OUT);
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 int num = 1000;
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 int sum = 0;
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 while (1)
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 {
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 int n;
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 sum = 0;
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 for (n = 0; n < num; n++)
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 {
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 GPIO_CLR(TOUCH_OUT);
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 usleep(1000);
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 GPIO_SET(TOUCH_OUT);
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 while (GPIO_GET(TOUCH_IN) == 0)
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 {
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 sum++;
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102 }
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 }
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 printf("total %f\n", (float)sum / num);
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 return 0;
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108 } // main
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 //
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 // Set up a memory regions to access GPIO
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 //
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 void setup_io()
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 {
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116 /* open /dev/mem */
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 printf("can't open /dev/mem \n");
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 exit (-1);
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 }
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122 /* mmap GPIO */
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 // Allocate MAP block
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 if ((gpio_mem = malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) {
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126 printf("allocation error \n");
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 exit (-1);
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128 }
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 // Make sure pointer is on 4K boundary
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 if ((unsigned long)gpio_mem % PAGE_SIZE)
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 gpio_mem += PAGE_SIZE - ((unsigned long)gpio_mem % PAGE_SIZE);
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134 // Now map it
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135 gpio_map = (unsigned char *)mmap(
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136 (caddr_t)gpio_mem,
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 BLOCK_SIZE,
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138 PROT_READ|PROT_WRITE,
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 MAP_SHARED|MAP_FIXED,
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140 mem_fd,
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 GPIO_BASE
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142 );
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 if ((long)gpio_map < 0) {
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145 printf("mmap error %d\n", (int)gpio_map);
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 exit (-1);
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 }
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 // Always use volatile pointer!
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150 gpio = (volatile unsigned *)gpio_map;
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152
dce9f7841696 rough touch sensor
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153 } // setup_io