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;
57 /* configure gpio to interrupt routing */
58 stm_exticr_set(gpio, pin);
60 /* configure pin as input, setting selected pull-up/down mode */
61 stm_moder_set(gpio, pin, STM_MODER_INPUT);
62 switch (mode & (AO_EXTI_MODE_PULL_UP|AO_EXTI_MODE_PULL_DOWN)) {
65 pupdr = STM_PUPDR_NONE;
67 case AO_EXTI_MODE_PULL_UP:
68 pupdr = STM_PUPDR_PULL_UP;
70 case AO_EXTI_MODE_PULL_DOWN:
71 pupdr = STM_PUPDR_PULL_DOWN;
74 stm_pupdr_set(gpio, pin, pupdr);
76 /* Set interrupt mask and rising/falling mode */
77 stm_exti.imr &= ~mask;
78 if (mode & AO_EXTI_MODE_RISING)
79 stm_exti.rtsr |= mask;
81 stm_exti.rtsr &= ~mask;
82 if (mode & AO_EXTI_MODE_FALLING)
83 stm_exti.ftsr |= mask;
85 stm_exti.ftsr &= ~mask;
88 irq = STM_ISR_EXTI0_POS + pin;
90 irq = STM_ISR_EXTI9_5_POS;
92 irq = STM_ISR_EXTI15_10_POS;
93 stm_nvic_set_priority(irq, 10);
94 stm_nvic_set_enable(irq);
98 ao_exti_enable(struct stm_gpio *gpio, uint8_t pin) {
99 uint32_t mask = (1 << pin);
101 stm_exti.imr |= (1 << pin);
105 ao_exti_disable(struct stm_gpio *gpio, uint8_t pin) {
106 uint32_t mask = (1 << pin);
107 stm_exti.imr &= ~mask;
114 stm_nvic_set_priority(STM_ISR_EXTI1_POS, 10);
115 stm_nvic_set_priority(STM_ISR_EXTI2_POS, 10);
116 stm_nvic_set_priority(STM_ISR_EXTI3_POS, 10);
117 stm_nvic_set_priority(STM_ISR_EXTI4_POS, 10);
118 stm_nvic_set_priority(STM_ISR_EXTI9_5_POS, 10);
119 stm_nvic_set_priority(STM_ISR_EXTI15_10_POS, 10);