2 * Copyright © 2012 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.
21 #define LPC_NUM_PINS 56
22 #define LPC_NUM_PINT 8
24 static void (*ao_exti_callback[LPC_NUM_PINT])(void);
26 static uint8_t ao_pint_map[LPC_NUM_PINS];
27 static uint8_t ao_pint_mode[LPC_NUM_PINS];
28 static uint8_t ao_pint_inuse;
29 static uint8_t ao_pint_enabled;
32 ao_exti_isr(uint8_t pint)
34 uint8_t mask = 1 << pint;
36 if (lpc_gpio_pin.ist & mask) {
37 lpc_gpio_pin.ist = mask;
39 (*ao_exti_callback) ();
43 #define pin_isr(n) void lpc_pin_int ## n ## _isr(void) { ao_exti_isr(n); }
53 #define pin_id(port,pin) ((port) * 24 + (pin));
56 ao_exti_setup (uint8_t port, uint8_t pin, uint8_t mode, void (*callback)(void)) {
57 uint8_t id = pin_id(port,pin);
62 for (pint = 0; pint < LPC_NUM_PINT; pint++)
63 if ((ao_pint_inuse & (1 << pint)) == 0)
65 if (pint == LPC_NUM_PINT)
66 ao_panic(AO_PANIC_EXTI);
69 ao_pint_inuse |= mask;
70 ao_pint_enabled &= ~mask;
72 ao_pint_map[id] = pint;
73 ao_exti_callback[pin] = callback;
75 /* configure gpio to interrupt routing */
76 lpc_scb.pintsel[pint] = id;
78 ao_enable_input(port, pin, mode);
80 /* Set edge triggered */
81 lpc_gpio_pin.isel &= ~mask;
83 ao_exti_set_mode(port, pin, mode);
85 /* Set interrupt mask and rising/falling mode */
87 prio = AO_LPC_NVIC_MED_PRIORITY;
88 if (mode & AO_EXTI_PRIORITY_LOW)
89 prio = AO_LPC_NVIC_LOW_PRIORITY;
90 else if (mode & AO_EXTI_PRIORITY_HIGH)
91 prio = AO_LPC_NVIC_HIGH_PRIORITY;
93 /* Set priority and enable */
94 lpc_nvic_set_priority(LPC_ISR_PIN_INT0_POS + pint, prio);
95 lpc_nvic_set_enable(LPC_ISR_PIN_INT0_POS + pint);
99 ao_exti_set_mode(uint8_t port, uint8_t pin, uint8_t mode) {
100 uint8_t id = pin_id(port,pin);
101 uint8_t pint = ao_pint_map[id];
102 uint8_t mask = 1 << pint;
104 ao_pint_mode[pint] = mode;
106 if (mode & AO_EXTI_MODE_RISING)
107 lpc_gpio_pin.sienr = mask;
109 lpc_gpio_pin.cienr = mask;
111 if (mode & AO_EXTI_MODE_FALLING)
112 lpc_gpio_pin.sienf = mask;
114 lpc_gpio_pin.cienf = mask;
118 ao_exti_set_callback(uint8_t port, uint8_t pin, void (*callback)()) {
119 uint8_t id = pin_id(port,pin);
120 uint8_t pint = ao_pint_map[id];
122 ao_exti_callback[pint] = callback;
126 ao_exti_enable(uint8_t port, uint8_t pin)
128 uint8_t id = pin_id(port,pin);
129 uint8_t pint = ao_pint_map[id];
130 uint8_t mask = 1 << pint;
132 ao_pint_enabled |= mask;
133 ao_exti_set_mode(port, pin, ao_pint_mode[pint]);
137 ao_exti_disable(uint8_t port, uint8_t pin) {
138 uint8_t id = pin_id(port,pin);
139 uint8_t pint = ao_pint_map[id];
140 uint8_t mask = 1 << pint;
142 ao_pint_enabled &= ~mask;
143 lpc_gpio_pin.cienr = mask;
144 lpc_gpio_pin.cienf = mask;
150 lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_PINT);