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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 #define LPC_NUM_PINS 56
23 #define LPC_NUM_PINT 8
25 static void (*ao_exti_callback[LPC_NUM_PINT])(void);
27 static uint8_t ao_pint_map[LPC_NUM_PINS];
28 static uint8_t ao_pint_mode[LPC_NUM_PINS];
29 static uint8_t ao_pint_inuse;
30 static uint8_t ao_pint_enabled;
33 ao_exti_isr(uint8_t pint)
35 uint8_t mask = 1 << pint;
37 if (lpc_gpio_pin.ist & mask) {
38 lpc_gpio_pin.ist = mask;
39 lpc_gpio_pin.rise = mask;
40 lpc_gpio_pin.fall = mask;
42 (*ao_exti_callback[pint]) ();
46 #define pin_isr(n) void lpc_pin_int ## n ## _isr(void) { ao_exti_isr(n); }
56 #define pin_id(port,pin) ((port) * 24 + (pin));
59 _ao_exti_set_enable(uint8_t pint)
61 uint8_t mask = 1 << pint;
64 if (ao_pint_enabled & mask)
65 mode = ao_pint_mode[pint];
69 if (mode & AO_EXTI_MODE_RISING)
70 lpc_gpio_pin.sienr = mask;
72 lpc_gpio_pin.cienr = mask;
74 if (mode & AO_EXTI_MODE_FALLING)
75 lpc_gpio_pin.sienf = mask;
77 lpc_gpio_pin.cienf = mask;
78 lpc_gpio_pin.rise = mask;
79 lpc_gpio_pin.fall = mask;
83 ao_exti_setup (uint8_t port, uint8_t pin, uint8_t mode, void (*callback)(void)) {
84 uint8_t id = pin_id(port,pin);
89 for (pint = 0; pint < LPC_NUM_PINT; pint++)
90 if ((ao_pint_inuse & (1 << pint)) == 0)
92 if (pint == LPC_NUM_PINT)
93 ao_panic(AO_PANIC_EXTI);
95 if (!(mode & AO_EXTI_PIN_NOCONFIGURE))
96 ao_enable_input(port, pin, mode);
98 ao_arch_block_interrupts();
100 ao_pint_inuse |= mask;
101 ao_pint_enabled &= ~mask;
103 ao_pint_map[id] = pint;
104 ao_exti_callback[pint] = callback;
106 /* configure gpio to interrupt routing */
107 lpc_scb.pintsel[pint] = id;
109 /* Set edge triggered */
110 lpc_gpio_pin.isel &= ~mask;
112 ao_pint_enabled &= ~mask;
113 ao_pint_mode[pint] = mode;
114 _ao_exti_set_enable(pint);
116 /* Set interrupt mask and rising/falling mode */
118 prio = AO_LPC_NVIC_MED_PRIORITY;
119 if (mode & AO_EXTI_PRIORITY_LOW)
120 prio = AO_LPC_NVIC_LOW_PRIORITY;
121 else if (mode & AO_EXTI_PRIORITY_HIGH)
122 prio = AO_LPC_NVIC_HIGH_PRIORITY;
124 /* Set priority and enable */
125 lpc_nvic_set_priority(LPC_ISR_PIN_INT0_POS + pint, prio);
126 lpc_nvic_set_enable(LPC_ISR_PIN_INT0_POS + pint);
127 ao_arch_release_interrupts();
131 ao_exti_set_mode(uint8_t port, uint8_t pin, uint8_t mode)
133 uint8_t id = pin_id(port,pin);
134 uint8_t pint = ao_pint_map[id];
136 ao_arch_block_interrupts();
137 ao_pint_mode[pint] = mode;
138 _ao_exti_set_enable(pint);
139 ao_arch_release_interrupts();
143 ao_exti_set_callback(uint8_t port, uint8_t pin, void (*callback)()) {
144 uint8_t id = pin_id(port,pin);
145 uint8_t pint = ao_pint_map[id];
147 ao_exti_callback[pint] = callback;
151 ao_exti_enable(uint8_t port, uint8_t pin)
153 uint8_t id = pin_id(port,pin);
154 uint8_t pint = ao_pint_map[id];
155 uint8_t mask = 1 << pint;
157 ao_arch_block_interrupts();
158 ao_pint_enabled |= mask;
159 _ao_exti_set_enable(pint);
160 ao_arch_release_interrupts();
164 ao_exti_disable(uint8_t port, uint8_t pin) {
165 uint8_t id = pin_id(port,pin);
166 uint8_t pint = ao_pint_map[id];
167 uint8_t mask = 1 << pint;
169 ao_arch_block_interrupts();
170 ao_pint_enabled &= ~mask;
171 _ao_exti_set_enable(pint);
172 ao_arch_release_interrupts();
178 lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_PINT);