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;
38 lpc_gpio_pin.rise = mask;
39 lpc_gpio_pin.fall = mask;
41 (*ao_exti_callback[pint]) ();
45 #define pin_isr(n) void lpc_pin_int ## n ## _isr(void) { ao_exti_isr(n); }
55 #define pin_id(port,pin) ((port) * 24 + (pin));
58 _ao_exti_set_enable(uint8_t pint)
60 uint8_t mask = 1 << pint;
63 if (ao_pint_enabled & mask)
64 mode = ao_pint_mode[pint];
68 if (mode & AO_EXTI_MODE_RISING)
69 lpc_gpio_pin.sienr = mask;
71 lpc_gpio_pin.cienr = mask;
73 if (mode & AO_EXTI_MODE_FALLING)
74 lpc_gpio_pin.sienf = mask;
76 lpc_gpio_pin.cienf = mask;
77 lpc_gpio_pin.rise = mask;
78 lpc_gpio_pin.fall = mask;
82 ao_exti_setup (uint8_t port, uint8_t pin, uint8_t mode, void (*callback)(void)) {
83 uint8_t id = pin_id(port,pin);
88 for (pint = 0; pint < LPC_NUM_PINT; pint++)
89 if ((ao_pint_inuse & (1 << pint)) == 0)
91 if (pint == LPC_NUM_PINT)
92 ao_panic(AO_PANIC_EXTI);
94 if (!(mode & AO_EXTI_PIN_NOCONFIGURE))
95 ao_enable_input(port, pin, mode);
97 ao_arch_block_interrupts();
99 ao_pint_inuse |= mask;
100 ao_pint_enabled &= ~mask;
102 ao_pint_map[id] = pint;
103 ao_exti_callback[pint] = callback;
105 /* configure gpio to interrupt routing */
106 lpc_scb.pintsel[pint] = id;
108 /* Set edge triggered */
109 lpc_gpio_pin.isel &= ~mask;
111 ao_pint_enabled &= ~mask;
112 ao_pint_mode[pint] = mode;
113 _ao_exti_set_enable(pint);
115 /* Set interrupt mask and rising/falling mode */
117 prio = AO_LPC_NVIC_MED_PRIORITY;
118 if (mode & AO_EXTI_PRIORITY_LOW)
119 prio = AO_LPC_NVIC_LOW_PRIORITY;
120 else if (mode & AO_EXTI_PRIORITY_HIGH)
121 prio = AO_LPC_NVIC_HIGH_PRIORITY;
123 /* Set priority and enable */
124 lpc_nvic_set_priority(LPC_ISR_PIN_INT0_POS + pint, prio);
125 lpc_nvic_set_enable(LPC_ISR_PIN_INT0_POS + pint);
126 ao_arch_release_interrupts();
130 ao_exti_set_mode(uint8_t port, uint8_t pin, uint8_t mode)
132 uint8_t id = pin_id(port,pin);
133 uint8_t pint = ao_pint_map[id];
135 ao_arch_block_interrupts();
136 ao_pint_mode[pint] = mode;
137 _ao_exti_set_enable(pint);
138 ao_arch_release_interrupts();
142 ao_exti_set_callback(uint8_t port, uint8_t pin, void (*callback)()) {
143 uint8_t id = pin_id(port,pin);
144 uint8_t pint = ao_pint_map[id];
146 ao_exti_callback[pint] = callback;
150 ao_exti_enable(uint8_t port, uint8_t pin)
152 uint8_t id = pin_id(port,pin);
153 uint8_t pint = ao_pint_map[id];
154 uint8_t mask = 1 << pint;
156 ao_arch_block_interrupts();
157 ao_pint_enabled |= mask;
158 _ao_exti_set_enable(pint);
159 ao_arch_release_interrupts();
163 ao_exti_disable(uint8_t port, uint8_t pin) {
164 uint8_t id = pin_id(port,pin);
165 uint8_t pint = ao_pint_map[id];
166 uint8_t mask = 1 << pint;
168 ao_arch_block_interrupts();
169 ao_pint_enabled &= ~mask;
170 _ao_exti_set_enable(pint);
171 ao_arch_release_interrupts();
177 lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_PINT);