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);
23 uint32_t ao_last_exti;
25 static void ao_exti_one_isr(uint8_t pin) {
26 uint32_t pending = (ao_last_exti = stm_exti.pr) & (1 << pin);
28 stm_exti.pr = pending;
29 if (pending && ao_exti_callback[pin])
30 (*ao_exti_callback[pin])();
33 static void ao_exti_range_isr(uint8_t first, uint8_t last, uint16_t mask) {
34 uint16_t pending = (ao_last_exti = stm_exti.pr) & mask;
36 static uint16_t last_mask;
37 static uint8_t last_pin;
39 if (pending == last_mask) {
40 stm_exti.pr = last_mask;
41 (*ao_exti_callback[last_pin])();
44 stm_exti.pr = pending;
45 for (pin = first; pin <= last; pin++)
46 if ((pending & ((uint32_t) 1 << pin)) && ao_exti_callback[pin]) {
47 last_mask = (1 << pin);
49 (*ao_exti_callback[pin])();
53 void stm_exti0_isr(void) { ao_exti_one_isr(0); }
54 void stm_exti1_isr(void) { ao_exti_one_isr(1); }
55 void stm_exti2_isr(void) { ao_exti_one_isr(2); }
56 void stm_exti3_isr(void) { ao_exti_one_isr(3); }
57 void stm_exti4_isr(void) { ao_exti_one_isr(4); }
58 void stm_exti9_5_isr(void) { ao_exti_range_isr(5, 9, 0x3e0); }
59 void stm_exti15_10_isr(void) { ao_exti_range_isr(10, 15, 0xfc00); }
62 ao_exti_setup (struct stm_gpio *gpio, uint8_t pin, uint8_t mode, void (*callback)(void)) {
63 uint32_t mask = 1 << pin;
68 ao_exti_callback[pin] = callback;
70 /* configure gpio to interrupt routing */
71 stm_exticr_set(gpio, pin);
73 /* configure pin as input, setting selected pull-up/down mode */
74 stm_moder_set(gpio, pin, STM_MODER_INPUT);
75 switch (mode & (AO_EXTI_MODE_PULL_UP|AO_EXTI_MODE_PULL_DOWN)) {
78 pupdr = STM_PUPDR_NONE;
80 case AO_EXTI_MODE_PULL_UP:
81 pupdr = STM_PUPDR_PULL_UP;
83 case AO_EXTI_MODE_PULL_DOWN:
84 pupdr = STM_PUPDR_PULL_DOWN;
87 stm_pupdr_set(gpio, pin, pupdr);
89 /* Set interrupt mask and rising/falling mode */
90 stm_exti.imr &= ~mask;
91 if (mode & AO_EXTI_MODE_RISING)
92 stm_exti.rtsr |= mask;
94 stm_exti.rtsr &= ~mask;
95 if (mode & AO_EXTI_MODE_FALLING)
96 stm_exti.ftsr |= mask;
98 stm_exti.ftsr &= ~mask;
101 irq = STM_ISR_EXTI0_POS + pin;
103 irq = STM_ISR_EXTI9_5_POS;
105 irq = STM_ISR_EXTI15_10_POS;
108 prio = AO_STM_NVIC_MED_PRIORITY;
109 if (mode & AO_EXTI_PRIORITY_LOW)
110 prio = AO_STM_NVIC_LOW_PRIORITY;
111 else if (mode & AO_EXTI_PRIORITY_HIGH)
112 prio = AO_STM_NVIC_HIGH_PRIORITY;
114 stm_nvic_set_priority(irq, prio);
115 stm_nvic_set_enable(irq);
119 ao_exti_set_mode(struct stm_gpio *gpio, uint8_t pin, uint8_t mode) {
120 uint32_t mask = 1 << pin;
122 if (mode & AO_EXTI_MODE_RISING)
123 stm_exti.rtsr |= mask;
125 stm_exti.rtsr &= ~mask;
126 if (mode & AO_EXTI_MODE_FALLING)
127 stm_exti.ftsr |= mask;
129 stm_exti.ftsr &= ~mask;
133 ao_exti_set_callback(struct stm_gpio *gpio, uint8_t pin, void (*callback)()) {
134 ao_exti_callback[pin] = callback;
138 ao_exti_enable(struct stm_gpio *gpio, uint8_t pin) {
139 uint32_t mask = (1 << pin);
141 stm_exti.imr |= (1 << pin);
145 ao_exti_disable(struct stm_gpio *gpio, uint8_t pin) {
146 uint32_t mask = (1 << pin);
147 stm_exti.imr &= ~mask;