altosdroid: initial implementation of telemetry logging.
[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 __xdata uint16_t ao_button_tick[NUM_BUTTONS];
40
41 static void
42 ao_button_insert(char n)
43 {
44         uint16_t        now = ao_time();
45         if ((now - ao_button_tick[n]) > 20) {
46                 ao_button_tick[n] = now;
47                 ao_fifo_insert(ao_button_fifo, n);
48                 ao_wakeup(&ao_button_fifo);
49         }
50 }
51
52 static void
53 ao_button_isr(uint8_t flag, uint8_t reg)
54 {
55         uint8_t b;
56
57         for (b = 0; b < NUM_BUTTONS; b++)
58                 if (ao_buttons[b].reg == reg && (ao_buttons[b].mask & flag))
59                         ao_button_insert(b + 1);
60 }
61
62 static uint8_t
63 ao_button_mask(uint8_t reg)
64 {
65         uint8_t b;
66         uint8_t mask = 0;
67
68         for (b = 0; b < NUM_BUTTONS; b++)
69                 if (ao_buttons[b].reg == reg)
70                         mask |= ao_buttons[b].mask;
71         return mask;
72 }
73
74 char
75 ao_button_get(void) __critical
76 {
77         char    b;
78
79         while (ao_fifo_empty(ao_button_fifo))
80                 if (ao_sleep(&ao_button_fifo))
81                         return 0;
82         ao_fifo_remove(ao_button_fifo, b);
83         return b;
84 }
85
86 void
87 ao_button_clear(void) __critical
88 {
89         char b;
90
91         while (!ao_fifo_empty(ao_button_fifo))
92                 ao_fifo_remove(ao_button_fifo, b);
93 }
94
95 void
96 ao_p0_isr(void) ao_arch_interrupt(13)
97 {
98         P0IF = 0;
99         ao_button_isr(P0IFG, 0);
100         P0IFG = 0;
101 }
102
103 void
104 ao_p1_isr(void) ao_arch_interrupt(15)
105 {
106         P1IF = 0;
107         ao_button_isr(P1IFG, 1);
108         P1IFG = 0;
109 }
110
111 /* Shared with USB */
112 void
113 ao_p2_isr(void)
114 {
115         ao_button_isr(P2IFG, 2);
116         P2IFG = 0;
117 }
118
119 void
120 ao_button_init(void)
121 {
122         uint8_t mask;
123
124         /* Pins are configured as inputs with pull-up by default */
125
126         /* Enable interrupts for P0 inputs */
127         mask = ao_button_mask(0);
128         if (mask) {
129                 if (mask & 0x0f)
130                         PICTL |= PICTL_P0IENL;
131                 if (mask & 0xf0)
132                         PICTL |= PICTL_P0IENH;
133                 P0IFG = 0;
134                 P0IF = 0;
135                 IEN1 |= IEN1_P0IE;
136                 PICTL |= PICTL_P0ICON;
137         }
138
139         /* Enable interrupts for P1 inputs */
140         mask = ao_button_mask(1);
141         if (mask) {
142                 P1IEN |= mask;
143                 P1IFG = 0;
144                 P1IF = 0;
145                 IEN2 |= IEN2_P1IE;
146                 PICTL |= PICTL_P1ICON;
147         }
148
149         /* Enable interrupts for P2 inputs */
150         mask = ao_button_mask(2);
151         if (mask) {
152                 PICTL |= PICTL_P2IEN;
153                 P2IFG = 0;
154                 P2IF = 0;
155                 IEN2 |= IEN2_P2IE;
156                 PICTL |= PICTL_P2ICON;
157         }
158 }