2 * Copyright © 2018 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 = (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;
69 ao_exti_callback[pin] = callback;
71 /* configure gpio to interrupt routing */
72 stm_exticr_set(gpio, pin);
74 if (!(mode & AO_EXTI_PIN_NOCONFIGURE)) {
75 /* configure pin as input, setting selected pull-up/down mode */
76 stm_moder_set(gpio, pin, STM_MODER_INPUT);
77 switch (mode & (AO_EXTI_MODE_PULL_UP|AO_EXTI_MODE_PULL_DOWN)) {
80 pupdr = STM_PUPDR_NONE;
82 case AO_EXTI_MODE_PULL_UP:
83 pupdr = STM_PUPDR_PULL_UP;
85 case AO_EXTI_MODE_PULL_DOWN:
86 pupdr = STM_PUPDR_PULL_DOWN;
89 stm_pupdr_set(gpio, pin, pupdr);
92 /* Set interrupt mask and rising/falling mode */
93 stm_exti.imr &= ~mask;
94 if (mode & AO_EXTI_MODE_RISING)
95 stm_exti.rtsr |= mask;
97 stm_exti.rtsr &= ~mask;
98 if (mode & AO_EXTI_MODE_FALLING)
99 stm_exti.ftsr |= mask;
101 stm_exti.ftsr &= ~mask;
104 irq = STM_ISR_EXTI0_POS + pin;
106 irq = STM_ISR_EXTI9_5_POS;
108 irq = STM_ISR_EXTI15_10_POS;
111 prio = AO_STM_NVIC_MED_PRIORITY;
112 if (mode & AO_EXTI_PRIORITY_LOW)
113 prio = AO_STM_NVIC_LOW_PRIORITY;
114 else if (mode & AO_EXTI_PRIORITY_HIGH)
115 prio = AO_STM_NVIC_HIGH_PRIORITY;
117 stm_nvic_set_priority(irq, prio);
118 stm_nvic_set_enable(irq);
122 ao_exti_set_mode(struct stm_gpio *gpio, uint8_t pin, uint8_t mode) {
125 uint32_t mask = 1 << pin;
127 if (mode & AO_EXTI_MODE_RISING)
128 stm_exti.rtsr |= mask;
130 stm_exti.rtsr &= ~mask;
131 if (mode & AO_EXTI_MODE_FALLING)
132 stm_exti.ftsr |= mask;
134 stm_exti.ftsr &= ~mask;
138 ao_exti_set_callback(struct stm_gpio *gpio, uint8_t pin, void (*callback)()) {
140 ao_exti_callback[pin] = callback;
144 ao_exti_enable(struct stm_gpio *gpio, uint8_t pin) {
145 uint32_t mask = (1 << pin);
148 stm_exti.imr |= mask;
152 ao_exti_disable(struct stm_gpio *gpio, uint8_t pin) {
153 uint32_t mask = (1 << pin);
155 stm_exti.imr &= ~mask;