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