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 static void (*ao_exti_callback[16])(void);
24 uint32_t ao_last_exti;
26 static void ao_exti_one_isr(uint8_t pin) {
27 uint32_t pending = (ao_last_exti = stm_exti.pr) & (1 << pin);
29 stm_exti.pr = pending;
30 if (pending && ao_exti_callback[pin])
31 (*ao_exti_callback[pin])();
34 static void ao_exti_range_isr(uint8_t first, uint8_t last, uint16_t mask) {
35 uint16_t pending = (uint16_t) (ao_last_exti = stm_exti.pr) & mask;
37 static uint16_t last_mask;
38 static uint8_t last_pin;
40 if (pending == last_mask) {
41 stm_exti.pr = last_mask;
42 (*ao_exti_callback[last_pin])();
45 stm_exti.pr = pending;
46 for (pin = first; pin <= last; pin++)
47 if ((pending & ((uint32_t) 1 << pin)) && ao_exti_callback[pin]) {
48 last_mask = (1 << pin);
50 (*ao_exti_callback[pin])();
54 void stm_exti0_isr(void) { ao_exti_one_isr(0); }
55 void stm_exti1_isr(void) { ao_exti_one_isr(1); }
56 void stm_exti2_isr(void) { ao_exti_one_isr(2); }
57 void stm_exti3_isr(void) { ao_exti_one_isr(3); }
58 void stm_exti4_isr(void) { ao_exti_one_isr(4); }
59 void stm_exti9_5_isr(void) { ao_exti_range_isr(5, 9, 0x3e0); }
60 void stm_exti15_10_isr(void) { ao_exti_range_isr(10, 15, 0xfc00); }
63 ao_exti_setup (struct stm_gpio *gpio, uint8_t pin, uint8_t mode, void (*callback)(void)) {
64 uint32_t mask = 1 << pin;
68 ao_exti_callback[pin] = callback;
70 /* configure gpio to interrupt routing */
71 stm_exticr_set(gpio, pin);
73 if (!(mode & AO_EXTI_PIN_NOCONFIGURE)) {
74 /* configure pin as input, setting selected pull-up/down mode */
75 ao_enable_input(gpio, pin, mode);
78 /* Set interrupt mask and rising/falling mode */
79 stm_exti.imr &= ~mask;
80 if (mode & AO_EXTI_MODE_RISING)
81 stm_exti.rtsr |= mask;
83 stm_exti.rtsr &= ~mask;
84 if (mode & AO_EXTI_MODE_FALLING)
85 stm_exti.ftsr |= mask;
87 stm_exti.ftsr &= ~mask;
90 irq = STM_ISR_EXTI0_POS + pin;
92 irq = STM_ISR_EXTI9_5_POS;
94 irq = STM_ISR_EXTI15_10_POS;
97 prio = AO_STM_NVIC_MED_PRIORITY;
98 if (mode & AO_EXTI_PRIORITY_LOW)
99 prio = AO_STM_NVIC_LOW_PRIORITY;
100 else if (mode & AO_EXTI_PRIORITY_HIGH)
101 prio = AO_STM_NVIC_HIGH_PRIORITY;
103 stm_nvic_set_priority(irq, prio);
104 stm_nvic_set_enable(irq);
108 ao_exti_set_mode(struct stm_gpio *gpio, uint8_t pin, uint8_t mode) {
111 uint32_t mask = 1 << pin;
113 if (mode & AO_EXTI_MODE_RISING)
114 stm_exti.rtsr |= mask;
116 stm_exti.rtsr &= ~mask;
117 if (mode & AO_EXTI_MODE_FALLING)
118 stm_exti.ftsr |= mask;
120 stm_exti.ftsr &= ~mask;
124 ao_exti_set_callback(struct stm_gpio *gpio, uint8_t pin, void (*callback)(void)) {
126 ao_exti_callback[pin] = callback;
130 ao_exti_enable(struct stm_gpio *gpio, uint8_t pin) {
131 uint32_t mask = (1 << pin);
134 stm_exti.imr |= mask;
138 ao_exti_disable(struct stm_gpio *gpio, uint8_t pin) {
139 uint32_t mask = (1 << pin);
141 stm_exti.imr &= ~mask;