2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
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.
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.
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.
20 volatile __xdata struct ao_fifo ao_button_fifo;
22 static __code struct {
27 { BUTTON_1_MASK, BUTTON_1_REG },
30 { BUTTON_2_MASK, BUTTON_2_REG },
33 { BUTTON_3_MASK, BUTTON_3_REG },
37 #define NUM_BUTTONS ((sizeof ao_buttons) / sizeof (ao_buttons[0]))
40 ao_button_insert(char n)
42 ao_fifo_insert(ao_button_fifo, n);
43 ao_wakeup(&ao_button_fifo);
47 ao_button_isr(uint8_t flag, uint8_t reg)
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);
57 ao_button_mask(uint8_t reg)
62 for (b = 0; b < NUM_BUTTONS; b++)
63 if (ao_buttons[b].reg == reg)
64 mask |= ao_buttons[b].mask;
69 ao_button_get(void) __critical
73 while (ao_fifo_empty(ao_button_fifo))
74 if (ao_sleep(&ao_button_fifo))
76 ao_fifo_remove(ao_button_fifo, b);
81 ao_p0_isr(void) ao_arch_interrupt(13)
84 ao_button_isr(P0IFG, 0);
89 ao_p1_isr(void) ao_arch_interrupt(15)
92 ao_button_isr(P1IFG, 1);
100 ao_button_isr(P2IFG, 2);
109 /* Pins are configured as inputs with pull-up by default */
111 /* Enable interrupts for P0 inputs */
112 mask = ao_button_mask(0);
115 PICTL |= PICTL_P0IENL;
117 PICTL |= PICTL_P0IENH;
121 PICTL |= PICTL_P0ICON;
124 /* Enable interrupts for P1 inputs */
125 mask = ao_button_mask(1);
131 PICTL |= PICTL_P1ICON;
134 /* Enable interrupts for P2 inputs */
135 mask = ao_button_mask(2);
137 PICTL |= PICTL_P2IEN;
141 PICTL |= PICTL_P2ICON;