2 * Copyright © 2016 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);
23 uint32_t ao_last_exti;
25 static void ao_exti_range_isr(uint8_t first, uint8_t last, uint16_t mask) {
26 uint16_t pending = (ao_last_exti = stm_exti.pr) & mask;
28 static uint16_t last_mask;
29 static uint8_t last_pin;
31 if (pending == last_mask) {
32 stm_exti.pr = last_mask;
33 (*ao_exti_callback[last_pin])();
36 stm_exti.pr = pending;
37 for (pin = first; pin <= last; pin++)
38 if ((pending & ((uint32_t) 1 << pin)) && ao_exti_callback[pin]) {
39 last_mask = (1 << pin);
41 (*ao_exti_callback[pin])();
45 void stm_exti0_1_isr(void) { ao_exti_range_isr(0, 1, 0x0003); }
46 void stm_exti2_3_isr(void) { ao_exti_range_isr(2, 3, 0x000c); }
47 void stm_exti4_15_isr(void) { ao_exti_range_isr(4, 15, 0xfff0); }
50 ao_exti_setup (struct stm_gpio *gpio, uint8_t pin, uint8_t mode, void (*callback)(void)) {
51 uint32_t mask = 1 << pin;
56 ao_exti_callback[pin] = callback;
58 /* configure gpio to interrupt routing */
59 stm_exticr_set(gpio, pin);
61 if (!(mode & AO_EXTI_PIN_NOCONFIGURE)) {
62 /* configure pin as input, setting selected pull-up/down mode */
63 stm_moder_set(gpio, pin, STM_MODER_INPUT);
64 switch (mode & (AO_EXTI_MODE_PULL_UP|AO_EXTI_MODE_PULL_DOWN)) {
67 pupdr = STM_PUPDR_NONE;
69 case AO_EXTI_MODE_PULL_UP:
70 pupdr = STM_PUPDR_PULL_UP;
72 case AO_EXTI_MODE_PULL_DOWN:
73 pupdr = STM_PUPDR_PULL_DOWN;
76 stm_pupdr_set(gpio, pin, pupdr);
79 /* Set interrupt mask and rising/falling mode */
80 stm_exti.imr &= ~mask;
81 if (mode & AO_EXTI_MODE_RISING)
82 stm_exti.rtsr |= mask;
84 stm_exti.rtsr &= ~mask;
85 if (mode & AO_EXTI_MODE_FALLING)
86 stm_exti.ftsr |= mask;
88 stm_exti.ftsr &= ~mask;
91 irq = STM_ISR_EXTI0_1_POS;
93 irq = STM_ISR_EXTI2_3_POS;
95 irq = STM_ISR_EXTI4_15_POS;
98 prio = AO_STM_NVIC_MED_PRIORITY;
99 if (mode & AO_EXTI_PRIORITY_LOW)
100 prio = AO_STM_NVIC_LOW_PRIORITY;
101 else if (mode & AO_EXTI_PRIORITY_HIGH)
102 prio = AO_STM_NVIC_HIGH_PRIORITY;
104 stm_nvic_set_priority(irq, prio);
105 stm_nvic_set_enable(irq);
109 ao_exti_set_mode(struct stm_gpio *gpio, uint8_t pin, uint8_t mode) {
112 uint32_t mask = 1 << pin;
114 if (mode & AO_EXTI_MODE_RISING)
115 stm_exti.rtsr |= mask;
117 stm_exti.rtsr &= ~mask;
118 if (mode & AO_EXTI_MODE_FALLING)
119 stm_exti.ftsr |= mask;
121 stm_exti.ftsr &= ~mask;
125 ao_exti_set_callback(struct stm_gpio *gpio, uint8_t pin, void (*callback)()) {
127 ao_exti_callback[pin] = callback;
131 ao_exti_enable(struct stm_gpio *gpio, uint8_t pin) {
132 uint32_t mask = (1 << pin);
135 stm_exti.imr |= (1 << pin);
139 ao_exti_disable(struct stm_gpio *gpio, uint8_t pin) {
140 uint32_t mask = (1 << pin);
142 stm_exti.imr &= ~mask;