altos/stmf0: Remove ao_usb_free
[fw/altos] / src / stmf0 / ao_exti_stm.c
1 /*
2  * Copyright © 2016 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; version 2 of the License.
7  *
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.
12  *
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.
16  */
17
18 #include <ao.h>
19 #include <ao_exti.h>
20
21 static void     (*ao_exti_callback[16])(void);
22
23 uint32_t        ao_last_exti;
24
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;
27         uint8_t         pin;
28         static uint16_t last_mask;
29         static uint8_t  last_pin;
30
31         if (pending == last_mask) {
32                 stm_exti.pr = last_mask;
33                 (*ao_exti_callback[last_pin])();
34                 return;
35         }
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);
40                         last_pin = pin;
41                         (*ao_exti_callback[pin])();
42                 }
43 }
44
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); }
48
49 void
50 ao_exti_setup (struct stm_gpio *gpio, uint8_t pin, uint8_t mode, void (*callback)(void)) {
51         uint32_t        mask = 1 << pin;
52         uint32_t        pupdr;
53         uint8_t         irq;
54         uint8_t         prio;
55
56         ao_exti_callback[pin] = callback;
57
58         /* configure gpio to interrupt routing */
59         stm_exticr_set(gpio, pin);
60
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)) {
65                 case 0:
66                 default:
67                         pupdr  = STM_PUPDR_NONE;
68                         break;
69                 case AO_EXTI_MODE_PULL_UP:
70                         pupdr = STM_PUPDR_PULL_UP;
71                         break;
72                 case AO_EXTI_MODE_PULL_DOWN:
73                         pupdr = STM_PUPDR_PULL_DOWN;
74                         break;
75                 }
76                 stm_pupdr_set(gpio, pin, pupdr);
77         }
78
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;
83         else
84                 stm_exti.rtsr &= ~mask;
85         if (mode & AO_EXTI_MODE_FALLING)
86                 stm_exti.ftsr |= mask;
87         else
88                 stm_exti.ftsr &= ~mask;
89
90         if (pin <= 1)
91                 irq = STM_ISR_EXTI0_1_POS;
92         else if (pin <= 3)
93                 irq = STM_ISR_EXTI2_3_POS;
94         else
95                 irq = STM_ISR_EXTI4_15_POS;
96
97         /* Set priority */
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;
103
104         stm_nvic_set_priority(irq, prio);
105         stm_nvic_set_enable(irq);
106 }
107
108 void
109 ao_exti_set_mode(struct stm_gpio *gpio, uint8_t pin, uint8_t mode) {
110         (void) gpio;
111
112         uint32_t        mask = 1 << pin;
113
114         if (mode & AO_EXTI_MODE_RISING)
115                 stm_exti.rtsr |= mask;
116         else
117                 stm_exti.rtsr &= ~mask;
118         if (mode & AO_EXTI_MODE_FALLING)
119                 stm_exti.ftsr |= mask;
120         else
121                 stm_exti.ftsr &= ~mask;
122 }
123
124 void
125 ao_exti_set_callback(struct stm_gpio *gpio, uint8_t pin, void (*callback)()) {
126         (void) gpio;
127         ao_exti_callback[pin] = callback;
128 }
129
130 void
131 ao_exti_enable(struct stm_gpio *gpio, uint8_t pin) {
132         uint32_t        mask = (1 << pin);
133         (void) gpio;
134         stm_exti.pr = mask;
135         stm_exti.imr |= (1 << pin);
136 }
137
138 void
139 ao_exti_disable(struct stm_gpio *gpio, uint8_t pin) {
140         uint32_t        mask = (1 << pin);
141         (void) gpio;
142         stm_exti.imr &= ~mask;
143         stm_exti.pr = mask;
144 }
145
146 void
147 ao_exti_init(void)
148 {
149 }