altos: Bring up basic TeleTerra v0.2 UI
[fw/altos] / src / cc1111 / ao_button.c
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19
20 volatile __xdata struct ao_fifo ao_button_fifo;
21
22 static __code struct {
23         uint8_t mask;
24         uint8_t reg;
25 } ao_buttons[] = {
26 #ifdef BUTTON_1_MASK
27         { BUTTON_1_MASK, BUTTON_1_REG },
28 #endif
29 #ifdef BUTTON_2_MASK
30         { BUTTON_2_MASK, BUTTON_2_REG },
31 #endif
32 #ifdef BUTTON_3_MASK
33         { BUTTON_3_MASK, BUTTON_3_REG },
34 #endif
35 };
36
37 #define NUM_BUTTONS     ((sizeof ao_buttons) / sizeof (ao_buttons[0]))
38
39 static void
40 ao_button_insert(char n)
41 {
42         ao_fifo_insert(ao_button_fifo, n);
43         ao_wakeup(&ao_button_fifo);
44 }
45
46 static void
47 ao_button_isr(uint8_t flag, uint8_t reg)
48 {
49         uint8_t b;
50
51         for (b = 0; b < NUM_BUTTONS; b++)
52                 if (ao_buttons[b].reg == reg && (ao_buttons[b].mask & flag))
53                         ao_button_insert(b + 1);
54 }
55
56 static uint8_t
57 ao_button_mask(uint8_t reg)
58 {
59         uint8_t b;
60         uint8_t mask = 0;
61
62         for (b = 0; b < NUM_BUTTONS; b++)
63                 if (ao_buttons[b].reg == reg)
64                         mask |= ao_buttons[b].mask;
65         return mask;
66 }
67
68 char
69 ao_button_get(void) __critical
70 {
71         char    b;
72
73         while (ao_fifo_empty(ao_button_fifo))
74                 if (ao_sleep(&ao_button_fifo))
75                         return 0;
76         ao_fifo_remove(ao_button_fifo, b);
77         return b;
78 }
79
80 void
81 ao_p0_isr(void) ao_arch_interrupt(13)
82 {
83         P0IF = 0;
84         ao_button_isr(P0IFG, 0);
85         P0IFG = 0;
86 }
87
88 void
89 ao_p1_isr(void) ao_arch_interrupt(15)
90 {
91         P1IF = 0;
92         ao_button_isr(P1IFG, 1);
93         P1IFG = 0;
94 }
95
96 /* Shared with USB */
97 void
98 ao_p2_isr(void)
99 {
100         ao_button_isr(P2IFG, 2);
101         P2IFG = 0;
102 }
103
104 void
105 ao_button_init(void)
106 {
107         uint8_t mask;
108
109         /* Pins are configured as inputs with pull-up by default */
110
111         /* Enable interrupts for P0 inputs */
112         mask = ao_button_mask(0);
113         if (mask) {
114                 if (mask & 0x0f)
115                         PICTL |= PICTL_P0IENL;
116                 if (mask & 0xf0)
117                         PICTL |= PICTL_P0IENH;
118                 P0IFG = 0;
119                 P0IF = 0;
120                 IEN1 |= IEN1_P0IE;
121                 PICTL |= PICTL_P0ICON;
122         }
123
124         /* Enable interrupts for P1 inputs */
125         mask = ao_button_mask(1);
126         if (mask) {
127                 P1IEN |= mask;
128                 P1IFG = 0;
129                 P1IF = 0;
130                 IEN2 |= IEN2_P1IE;
131                 PICTL |= PICTL_P1ICON;
132         }
133
134         /* Enable interrupts for P2 inputs */
135         mask = ao_button_mask(2);
136         if (mask) {
137                 PICTL |= PICTL_P2IEN;
138                 P2IFG = 0;
139                 P2IF = 0;
140                 IEN2 |= IEN2_P2IE;
141                 PICTL |= PICTL_P2ICON;
142         }
143 }