altos: Add EXTI_PIN_NOCONFIGURE to exti interface, use for MS5607
[fw/altos] / src / stm / ao_exti_stm.c
1 /*
2  * Copyright © 2012 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_one_isr(uint8_t pin) {
26         uint32_t        pending = (ao_last_exti = stm_exti.pr) & (1 << pin);
27
28         stm_exti.pr = pending;
29         if (pending && ao_exti_callback[pin])
30                 (*ao_exti_callback[pin])();
31 }
32
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;
35         uint8_t         pin;
36         static uint16_t last_mask;
37         static uint8_t  last_pin;
38
39         if (pending == last_mask) {
40                 stm_exti.pr = last_mask;
41                 (*ao_exti_callback[last_pin])();
42                 return;
43         }
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);
48                         last_pin = pin;
49                         (*ao_exti_callback[pin])();
50                 }
51 }
52
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); }
60
61 void
62 ao_exti_setup (struct stm_gpio *gpio, uint8_t pin, uint8_t mode, void (*callback)(void)) {
63         uint32_t        mask = 1 << pin;
64         uint32_t        pupdr;
65         uint8_t         irq;
66         uint8_t         prio;
67
68         ao_exti_callback[pin] = callback;
69
70         /* configure gpio to interrupt routing */
71         stm_exticr_set(gpio, pin);
72
73         if (!(mode & AO_EXTI_PIN_NOCONFIGURE)) {
74                 /* configure pin as input, setting selected pull-up/down mode */
75                 stm_moder_set(gpio, pin, STM_MODER_INPUT);
76                 switch (mode & (AO_EXTI_MODE_PULL_UP|AO_EXTI_MODE_PULL_DOWN)) {
77                 case 0:
78                 default:
79                         pupdr  = STM_PUPDR_NONE;
80                         break;
81                 case AO_EXTI_MODE_PULL_UP:
82                         pupdr = STM_PUPDR_PULL_UP;
83                         break;
84                 case AO_EXTI_MODE_PULL_DOWN:
85                         pupdr = STM_PUPDR_PULL_DOWN;
86                         break;
87                 }
88                 stm_pupdr_set(gpio, pin, pupdr);
89         }
90
91         /* Set interrupt mask and rising/falling mode */
92         stm_exti.imr &= ~mask;
93         if (mode & AO_EXTI_MODE_RISING)
94                 stm_exti.rtsr |= mask;
95         else
96                 stm_exti.rtsr &= ~mask;
97         if (mode & AO_EXTI_MODE_FALLING)
98                 stm_exti.ftsr |= mask;
99         else
100                 stm_exti.ftsr &= ~mask;
101
102         if (pin <= 4)
103                 irq = STM_ISR_EXTI0_POS + pin;
104         else if (pin <= 9)
105                 irq = STM_ISR_EXTI9_5_POS;
106         else
107                 irq = STM_ISR_EXTI15_10_POS;
108
109         /* Set priority */
110         prio = AO_STM_NVIC_MED_PRIORITY;
111         if (mode & AO_EXTI_PRIORITY_LOW)
112                 prio = AO_STM_NVIC_LOW_PRIORITY;
113         else if (mode & AO_EXTI_PRIORITY_HIGH)
114                 prio = AO_STM_NVIC_HIGH_PRIORITY;
115
116         stm_nvic_set_priority(irq, prio);
117         stm_nvic_set_enable(irq);
118 }
119
120 void
121 ao_exti_set_mode(struct stm_gpio *gpio, uint8_t pin, uint8_t mode) {
122         uint32_t        mask = 1 << pin;
123         
124         if (mode & AO_EXTI_MODE_RISING)
125                 stm_exti.rtsr |= mask;
126         else
127                 stm_exti.rtsr &= ~mask;
128         if (mode & AO_EXTI_MODE_FALLING)
129                 stm_exti.ftsr |= mask;
130         else
131                 stm_exti.ftsr &= ~mask;
132 }
133
134 void
135 ao_exti_set_callback(struct stm_gpio *gpio, uint8_t pin, void (*callback)()) {
136         ao_exti_callback[pin] = callback;
137 }
138
139 void
140 ao_exti_enable(struct stm_gpio *gpio, uint8_t pin) {
141         uint32_t        mask = (1 << pin);
142         stm_exti.pr = mask;
143         stm_exti.imr |= (1 << pin);
144 }
145
146 void
147 ao_exti_disable(struct stm_gpio *gpio, uint8_t pin) {
148         uint32_t        mask = (1 << pin);
149         stm_exti.imr &= ~mask;
150         stm_exti.pr = mask;
151 }
152
153 void
154 ao_exti_init(void)
155 {
156 }