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_range_isr(uint8_t first, uint8_t last, uint16_t mask) {
27 uint16_t pending = (ao_last_exti = stm_exti.pr) & mask;
29 static uint16_t last_mask;
30 static uint8_t last_pin;
32 if (pending == last_mask) {
33 stm_exti.pr = last_mask;
34 (*ao_exti_callback[last_pin])();
37 stm_exti.pr = pending;
38 for (pin = first; pin <= last; pin++)
39 if ((pending & ((uint32_t) 1 << pin)) && ao_exti_callback[pin]) {
40 last_mask = (1 << pin);
42 (*ao_exti_callback[pin])();
46 void stm_exti1_0_isr(void) { ao_exti_range_isr(0, 1, 0x3); }
47 void stm_exti3_2_isr(void) { ao_exti_range_isr(2, 3, 0xc); }
48 void stm_exti15_4_isr(void) { ao_exti_range_isr(4, 15, 0xfff0); }
51 ao_exti_setup (struct stm_gpio *gpio, uint8_t pin, uint8_t mode, void (*callback)(void)) {
52 uint32_t mask = 1 << pin;
56 ao_exti_callback[pin] = callback;
58 /* configure gpio to interrupt routing */
59 stm_exticr_set(gpio, pin);
62 if (!(mode & AO_EXTI_PIN_NOCONFIGURE)) {
64 /* configure pin as input, setting selected pull-up/down mode */
65 stm_moder_set(gpio, pin, STM_MODER_INPUT);
66 switch (mode & (AO_EXTI_MODE_PULL_UP|AO_EXTI_MODE_PULL_DOWN)) {
69 pupdr = STM_PUPDR_NONE;
71 case AO_EXTI_MODE_PULL_UP:
72 pupdr = STM_PUPDR_PULL_UP;
74 case AO_EXTI_MODE_PULL_DOWN:
75 pupdr = STM_PUPDR_PULL_DOWN;
78 stm_pupdr_set(gpio, pin, pupdr);
82 /* Set interrupt mask and rising/falling mode */
83 stm_exti.imr &= ~mask;
84 if (mode & AO_EXTI_MODE_RISING)
85 stm_exti.rtsr |= mask;
87 stm_exti.rtsr &= ~mask;
88 if (mode & AO_EXTI_MODE_FALLING)
89 stm_exti.ftsr |= mask;
91 stm_exti.ftsr &= ~mask;
94 irq = STM_ISR_EXTI1_0_POS;
95 else if (2 <= pin && pin <= 3)
96 irq = STM_ISR_EXTI3_2_POS;
98 irq = STM_ISR_EXTI15_4_POS;
101 prio = AO_STM_NVIC_MED_PRIORITY;
102 if (mode & AO_EXTI_PRIORITY_LOW)
103 prio = AO_STM_NVIC_LOW_PRIORITY;
104 else if (mode & AO_EXTI_PRIORITY_HIGH)
105 prio = AO_STM_NVIC_HIGH_PRIORITY;
107 stm_nvic_set_priority(irq, prio);
108 stm_nvic_set_enable(irq);
112 ao_exti_set_mode(struct stm_gpio *gpio, uint8_t pin, uint8_t mode) {
115 uint32_t mask = 1 << pin;
117 if (mode & AO_EXTI_MODE_RISING)
118 stm_exti.rtsr |= mask;
120 stm_exti.rtsr &= ~mask;
121 if (mode & AO_EXTI_MODE_FALLING)
122 stm_exti.ftsr |= mask;
124 stm_exti.ftsr &= ~mask;
128 ao_exti_set_callback(struct stm_gpio *gpio, uint8_t pin, void (*callback)(void)) {
130 ao_exti_callback[pin] = callback;
134 ao_exti_enable(struct stm_gpio *gpio, uint8_t pin) {
135 uint32_t mask = (1 << pin);
138 stm_exti.imr |= mask;
142 ao_exti_disable(struct stm_gpio *gpio, uint8_t pin) {
143 uint32_t mask = (1 << pin);
145 stm_exti.imr &= ~mask;