547d71accea1b18d3e275c8f02ad6a5050d00cdb
[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 #define BUTTON_1_PIN    (P0_4)
23 #define BUTTON_1_MASK   (1 << 4)        /* P0_4 */
24
25 #define BUTTON_2_PIN    (P2_3)
26 #define BUTTON_2_MASK   (1 << 3)        /* P2_3 */
27
28 #define BUTTON_3_PIN    (P2_4)
29 #define BUTTON_3_MASK   (1 << 4)        /* P2_4 */
30
31 static void
32 ao_button_insert(char n)
33 {
34         ao_fifo_insert(ao_button_fifo, n);
35         ao_wakeup(&ao_button_fifo);
36 }
37
38 char
39 ao_button_get(void) __critical
40 {
41         char    b;
42
43         while (ao_fifo_empty(ao_button_fifo))
44                 ao_sleep(&ao_button_fifo);
45         ao_fifo_remove(ao_button_fifo, b);
46         return b;
47 }
48
49 void
50 ao_p2_isr(void)
51 {
52         if (P2IFG & BUTTON_2_MASK)
53                 ao_button_insert(2);
54         if (P2IFG & BUTTON_3_MASK)
55                 ao_button_insert(3);
56         P2IFG = 0;
57 }
58
59 void
60 ao_p0_isr(void) ao_arch_interrupt(13)
61 {
62         P0IF = 0;
63         if (P0IFG & BUTTON_1_MASK)
64                 ao_button_insert(1);
65         P0IFG = 0;
66 }
67
68 void
69 ao_button_init(void)
70 {
71         /* Pins are configured as inputs with pull-up by default */
72
73         /* Enable interrupts for P2_0 - P2_4
74          * Enable interrupts for P0_4 - P0_7
75          * Set P2 interrupts to falling edge
76          * Set P0 interrupts to falling edge
77          */
78         
79         PICTL |= PICTL_P2IEN | PICTL_P0IENH | PICTL_P2ICON | PICTL_P0ICON;
80
81         /* Enable interrupts for P0 inputs */
82         IEN1 |= IEN1_P0IE;
83
84         /* Enable interrupts for P2 inputs */
85         IEN2 |= IEN2_P2IE;
86 }