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 static void (*ao_exti_callback[16])(void);
25 uint32_t pending = stm_exti.pr;
28 /* Clear pending interrupts */
29 stm_exti.pr = pending;
30 for (pin = 0; pin < 16 && pending; pin++) {
31 uint32_t mask = (1 << pin);
35 if (ao_exti_callback[pin])
36 (*ao_exti_callback[pin])();
41 void stm_exti0_isr(void) { ao_exti_isr(); }
42 void stm_exti1_isr(void) { ao_exti_isr(); }
43 void stm_exti2_isr(void) { ao_exti_isr(); }
44 void stm_exti3_isr(void) { ao_exti_isr(); }
45 void stm_exti4_isr(void) { ao_exti_isr(); }
46 void stm_exti9_5_isr(void) { ao_exti_isr(); }
47 void stm_exti15_10_isr(void) { ao_exti_isr(); }
50 ao_exti_setup (struct stm_gpio *gpio, uint8_t pin, uint8_t mode, void (*callback)(void)) {
51 uint32_t mask = 1 << pin;
55 ao_exti_callback[pin] = callback;
56 /* configure pin as input, setting selected pull-up/down mode */
57 stm_moder_set(gpio, pin, STM_MODER_INPUT);
58 switch (mode & (AO_EXTI_MODE_PULL_UP|AO_EXTI_MODE_PULL_DOWN)) {
61 pupdr = STM_PUPDR_NONE;
63 case AO_EXTI_MODE_PULL_UP:
64 pupdr = STM_PUPDR_PULL_UP;
66 case AO_EXTI_MODE_PULL_DOWN:
67 pupdr = STM_PUPDR_PULL_DOWN;
70 stm_pupdr_set(gpio, pin, pupdr);
72 /* Set interrupt mask and rising/falling mode */
73 stm_exti.imr &= ~mask;
74 stm_exti.rtsr |= mask;
75 if (mode & AO_EXTI_MODE_RISING)
76 stm_exti.rtsr |= mask;
77 if (mode & AO_EXTI_MODE_FALLING)
78 stm_exti.ftsr |= mask;
81 irq = STM_ISR_EXTI0_POS + pin;
83 irq = STM_ISR_EXTI9_5_POS;
85 irq = STM_ISR_EXTI15_10_POS;
86 stm_nvic_set_priority(irq, 10);
87 stm_nvic_set_enable(irq);
91 ao_exti_enable(struct stm_gpio *gpio, uint8_t pin) {
92 stm_exti.imr |= (1 << pin);
96 ao_exti_disable(struct stm_gpio *gpio, uint8_t pin) {
97 stm_exti.imr &= ~(1 << pin);
103 stm_nvic_set_priority(STM_ISR_EXTI1_POS, 10);
104 stm_nvic_set_priority(STM_ISR_EXTI2_POS, 10);
105 stm_nvic_set_priority(STM_ISR_EXTI3_POS, 10);
106 stm_nvic_set_priority(STM_ISR_EXTI4_POS, 10);
107 stm_nvic_set_priority(STM_ISR_EXTI9_5_POS, 10);
108 stm_nvic_set_priority(STM_ISR_EXTI15_10_POS, 10);