Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / src / stm32f4 / ao_exti_stm32f4.c
1 /*
2  * Copyright © 2018 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <ao.h>
20 #include <ao_exti.h>
21
22 static void     (*ao_exti_callback[16])(void);
23
24 uint32_t        ao_last_exti;
25
26 static void ao_exti_one_isr(uint8_t pin) {
27         uint32_t        pending = (ao_last_exti = stm_exti.pr) & (1 << pin);
28
29         stm_exti.pr = pending;
30         if (pending && ao_exti_callback[pin])
31                 (*ao_exti_callback[pin])();
32 }
33
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;
36         uint8_t         pin;
37         static uint16_t last_mask;
38         static uint8_t  last_pin;
39
40         if (pending == last_mask) {
41                 stm_exti.pr = last_mask;
42                 (*ao_exti_callback[last_pin])();
43                 return;
44         }
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);
49                         last_pin = pin;
50                         (*ao_exti_callback[pin])();
51                 }
52 }
53
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); }
61
62 void
63 ao_exti_setup (struct stm_gpio *gpio, uint8_t pin, uint8_t mode, void (*callback)(void)) {
64         uint32_t        mask = 1 << pin;
65         uint32_t        pupdr;
66         uint8_t         irq;
67         uint8_t         prio;
68
69         ao_exti_callback[pin] = callback;
70
71         /* configure gpio to interrupt routing */
72         stm_exticr_set(gpio, pin);
73
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)) {
78                 case 0:
79                 default:
80                         pupdr  = STM_PUPDR_NONE;
81                         break;
82                 case AO_EXTI_MODE_PULL_UP:
83                         pupdr = STM_PUPDR_PULL_UP;
84                         break;
85                 case AO_EXTI_MODE_PULL_DOWN:
86                         pupdr = STM_PUPDR_PULL_DOWN;
87                         break;
88                 }
89                 stm_pupdr_set(gpio, pin, pupdr);
90         }
91
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;
96         else
97                 stm_exti.rtsr &= ~mask;
98         if (mode & AO_EXTI_MODE_FALLING)
99                 stm_exti.ftsr |= mask;
100         else
101                 stm_exti.ftsr &= ~mask;
102
103         if (pin <= 4)
104                 irq = STM_ISR_EXTI0_POS + pin;
105         else if (pin <= 9)
106                 irq = STM_ISR_EXTI9_5_POS;
107         else
108                 irq = STM_ISR_EXTI15_10_POS;
109
110         /* Set priority */
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;
116
117         stm_nvic_set_priority(irq, prio);
118         stm_nvic_set_enable(irq);
119 }
120
121 void
122 ao_exti_set_mode(struct stm_gpio *gpio, uint8_t pin, uint8_t mode) {
123         (void) gpio;
124
125         uint32_t        mask = 1 << pin;
126
127         if (mode & AO_EXTI_MODE_RISING)
128                 stm_exti.rtsr |= mask;
129         else
130                 stm_exti.rtsr &= ~mask;
131         if (mode & AO_EXTI_MODE_FALLING)
132                 stm_exti.ftsr |= mask;
133         else
134                 stm_exti.ftsr &= ~mask;
135 }
136
137 void
138 ao_exti_set_callback(struct stm_gpio *gpio, uint8_t pin, void (*callback)()) {
139         (void) gpio;
140         ao_exti_callback[pin] = callback;
141 }
142
143 void
144 ao_exti_enable(struct stm_gpio *gpio, uint8_t pin) {
145         uint32_t        mask = (1 << pin);
146         (void) gpio;
147         stm_exti.pr = mask;
148         stm_exti.imr |= mask;
149 }
150
151 void
152 ao_exti_disable(struct stm_gpio *gpio, uint8_t pin) {
153         uint32_t        mask = (1 << pin);
154         (void) gpio;
155         stm_exti.imr &= ~mask;
156         stm_exti.pr = mask;
157 }
158
159 void
160 ao_exti_init(void)
161 {
162 }