193f45470b4ca6dee4774c2587f413af1d5c747d
[fw/altos] / src / lpc / ao_exti_lpc.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <ao.h>
20 #include <ao_exti.h>
21
22 #define LPC_NUM_PINS    56
23 #define LPC_NUM_PINT    8
24
25 static void     (*ao_exti_callback[LPC_NUM_PINT])(void);
26
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;
31
32 static void
33 ao_exti_isr(uint8_t pint)
34 {
35         uint8_t mask = 1 << pint;
36
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;
41
42                 (*ao_exti_callback[pint]) ();
43         }
44 }
45
46 #define pin_isr(n)      void lpc_pin_int ## n ## _isr(void) { ao_exti_isr(n); }
47 pin_isr(0)
48 pin_isr(1)
49 pin_isr(2)
50 pin_isr(3)
51 pin_isr(4)
52 pin_isr(5)
53 pin_isr(6)
54 pin_isr(7)
55
56 #define pin_id(port,pin)        ((port) * 24 + (pin));
57
58 static void
59 _ao_exti_set_enable(uint8_t pint)
60 {
61         uint8_t         mask = 1 << pint;
62         uint8_t         mode;
63
64         if (ao_pint_enabled & mask)
65                 mode = ao_pint_mode[pint];
66         else
67                 mode = 0;
68
69         if (mode & AO_EXTI_MODE_RISING)
70                 lpc_gpio_pin.sienr = mask;
71         else
72                 lpc_gpio_pin.cienr = mask;
73
74         if (mode & AO_EXTI_MODE_FALLING)
75                 lpc_gpio_pin.sienf = mask;
76         else
77                 lpc_gpio_pin.cienf = mask;
78         lpc_gpio_pin.rise = mask;
79         lpc_gpio_pin.fall = mask;
80 }
81
82 void
83 ao_exti_setup (uint8_t port, uint8_t pin, uint8_t mode, void (*callback)(void)) {
84         uint8_t         id = pin_id(port,pin);
85         uint8_t         pint;
86         uint32_t        mask;
87         uint8_t         prio;
88
89         for (pint = 0; pint < LPC_NUM_PINT; pint++)
90                 if ((ao_pint_inuse & (1 << pint)) == 0)
91                         break;
92         if (pint == LPC_NUM_PINT)
93                 ao_panic(AO_PANIC_EXTI);
94
95         if (!(mode & AO_EXTI_PIN_NOCONFIGURE))
96                 ao_enable_input(port, pin, mode);
97
98         ao_arch_block_interrupts();
99         mask = (1 << pint);
100         ao_pint_inuse |= mask;
101         ao_pint_enabled &= ~mask;
102
103         ao_pint_map[id] = pint;
104         ao_exti_callback[pint] = callback;
105
106         /* configure gpio to interrupt routing */
107         lpc_scb.pintsel[pint] = id;
108
109         /* Set edge triggered */
110         lpc_gpio_pin.isel &= ~mask;
111
112         ao_pint_enabled &= ~mask;
113         ao_pint_mode[pint] = mode;
114         _ao_exti_set_enable(pint);
115
116         /* Set interrupt mask and rising/falling mode */
117
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;
123
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();
128 }
129
130 void
131 ao_exti_set_mode(uint8_t port, uint8_t pin, uint8_t mode)
132 {
133         uint8_t         id = pin_id(port,pin);
134         uint8_t         pint = ao_pint_map[id];
135
136         ao_arch_block_interrupts();
137         ao_pint_mode[pint] = mode;
138         _ao_exti_set_enable(pint);
139         ao_arch_release_interrupts();
140 }
141
142 void
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];
146
147         ao_exti_callback[pint] = callback;
148 }
149
150 void
151 ao_exti_enable(uint8_t port, uint8_t pin)
152 {
153         uint8_t         id = pin_id(port,pin);
154         uint8_t         pint = ao_pint_map[id];
155         uint8_t         mask = 1 << pint;
156
157         ao_arch_block_interrupts();
158         ao_pint_enabled |= mask;
159         _ao_exti_set_enable(pint);
160         ao_arch_release_interrupts();
161 }
162
163 void
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;
168
169         ao_arch_block_interrupts();
170         ao_pint_enabled &= ~mask;
171         _ao_exti_set_enable(pint);
172         ao_arch_release_interrupts();
173 }
174
175 void
176 ao_exti_init(void)
177 {
178         lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_PINT);
179 }