button support
[my-code/arm.git] / betty / buttons.c
1 /*
2  * buttons.c - button api
3  *
4  * author: hackbard@hackdaworld.org
5  *
6  */
7
8 #include "buttons.h"
9
10 void button_init(void) {
11
12         /* 
13          * select as input the following pins
14          *  - p0.30 : interrupt, wtf is the 'vw 3 p9'?
15          *  - p0.28, p0.27, p3.21, p3.20, p0.22, p0.13 : column select
16          *
17          *  select as output
18          *  - p2.18-p2.24 : the rows
19          */
20
21         // gpio, as is: p3.20, p3.21
22         PINSEL1&=~((1<<29)|(1<<28));    // p0.30
23         PINSEL1&=~((1<<25)|(1<<24));    // p0.28
24         PINSEL1&=~((1<<23)|(1<<22));    // p0.27
25         PINSEL1&=~((1<<13)|(1<<12));    // p0.22
26         PINSEL0&=~((1<<27)|(1<<26));    // p0.13
27         // ctrl databus for p2.18 - p2.24
28         PINSEL2&=(PINSEL2&(~((1<<5)|(1<<4))))|(1<<4);
29         // input
30         IODIR0&=~((1<<30)|(1<<28)|(1<<27)|(1<<22)|(1<<13));
31         IODIR3&=~((1<<21)|(1<<20));
32         // output + pull them high
33         IODIR2|=((1<<18)|(1<<19)|(1<<20)|(1<<21)|(1<<22)|(1<<23)|(1<<24));
34         IOSET2=((1<<18)|(1<<19)|(1<<20)|(1<<21)|(1<<22)|(1<<23)|(1<<24));
35 }
36
37 void button_select_row(u8 row) {
38
39         IOSET2=((1<<18)|(1<<19)|(1<<20)|(1<<21)|(1<<22)|(1<<23)|(1<<24));
40         if(row>6)
41                 return;
42         IOCLR2=(1<<(18+row));
43 }
44
45 u8 button_get_event(u64 *keys,int retries) {
46
47         u8 row;
48         u8 offset;
49         u8 cnt;
50         u32 port0,port3;
51
52         *keys=0;
53         cnt=0;
54
55         while(retries--) {
56                 /* rest counter */
57                 offset=0;
58                 /* rows */
59                 for(row=0;row<7;row++) {
60                         /* select the row */
61                         button_select_row(row);
62                         /* scan the columns 6 */
63                         port0=IOPIN0;
64                         port3=IOPIN2;
65                         if(!(port0&(1<<28))) {
66                                 *keys|=(1<<(offset+0));
67                                 cnt+=1;
68                         }
69                         if(!(port0&(1<<27))) {
70                                 *keys|=(1<<(offset+1));
71                                 cnt+=1;
72                         }
73                         if(!(port0&(1<<22))) {
74                                 *keys|=(1<<(offset+2));
75                                 cnt+=1;
76                         }
77                         if(!(port0&(1<<13))) {
78                                 *keys|=(1<<(offset+3));
79                                 cnt+=1;
80                         }
81                         if(!(port3&(1<<21))) {
82                                 *keys|=(1<<(offset+4));
83                                 cnt+=1;
84                         }
85                         if(!(port3&(1<<20))) {
86                                 *keys|=(1<<(offset+5));
87                                 cnt+=1;
88                         }
89                         offset+=6;
90                 }
91                 if(*keys)
92                         break;
93         }
94
95         return cnt;
96 }
97