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 ao_arch_block_interrupts();
96 ao_pint_inuse |= mask;
97 ao_pint_enabled &= ~mask;
99 ao_pint_map[id] = pint;
100 ao_exti_callback[pint] = callback;
102 /* configure gpio to interrupt routing */
103 lpc_scb.pintsel[pint] = id;
105 /* Set edge triggered */
106 lpc_gpio_pin.isel &= ~mask;
108 ao_pint_enabled &= ~mask;
109 ao_pint_mode[pint] = mode;
110 _ao_exti_set_enable(pint);
112 /* Set interrupt mask and rising/falling mode */
114 prio = AO_LPC_NVIC_MED_PRIORITY;
115 if (mode & AO_EXTI_PRIORITY_LOW)
116 prio = AO_LPC_NVIC_LOW_PRIORITY;
117 else if (mode & AO_EXTI_PRIORITY_HIGH)
118 prio = AO_LPC_NVIC_HIGH_PRIORITY;
120 /* Set priority and enable */
121 lpc_nvic_set_priority(LPC_ISR_PIN_INT0_POS + pint, prio);
122 lpc_nvic_set_enable(LPC_ISR_PIN_INT0_POS + pint);
123 ao_arch_release_interrupts();
127 ao_exti_set_mode(uint8_t port, uint8_t pin, uint8_t mode)
129 uint8_t id = pin_id(port,pin);
130 uint8_t pint = ao_pint_map[id];
132 ao_arch_block_interrupts();
133 ao_pint_mode[pint] = mode;
134 _ao_exti_set_enable(pint);
135 ao_arch_release_interrupts();
139 ao_exti_set_callback(uint8_t port, uint8_t pin, void (*callback)()) {
140 uint8_t id = pin_id(port,pin);
141 uint8_t pint = ao_pint_map[id];
143 ao_exti_callback[pint] = callback;
147 ao_exti_enable(uint8_t port, uint8_t pin)
149 uint8_t id = pin_id(port,pin);
150 uint8_t pint = ao_pint_map[id];
151 uint8_t mask = 1 << pint;
153 ao_arch_block_interrupts();
154 ao_pint_enabled |= mask;
155 _ao_exti_set_enable(pint);
156 ao_arch_release_interrupts();
160 ao_exti_disable(uint8_t port, uint8_t pin) {
161 uint8_t id = pin_id(port,pin);
162 uint8_t pint = ao_pint_map[id];
163 uint8_t mask = 1 << pint;
165 ao_arch_block_interrupts();
166 ao_pint_enabled &= ~mask;
167 _ao_exti_set_enable(pint);
168 ao_arch_release_interrupts();
174 lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_PINT);