altos: Add ao_gpio_get and ao_exti_set_mode
[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         /* 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)) {
76         case 0:
77         default:
78                 pupdr  = STM_PUPDR_NONE;
79                 break;
80         case AO_EXTI_MODE_PULL_UP:
81                 pupdr = STM_PUPDR_PULL_UP;
82                 break;
83         case AO_EXTI_MODE_PULL_DOWN:
84                 pupdr = STM_PUPDR_PULL_DOWN;
85                 break;
86         }
87         stm_pupdr_set(gpio, pin, pupdr);
88
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;
93         else
94                 stm_exti.rtsr &= ~mask;
95         if (mode & AO_EXTI_MODE_FALLING)
96                 stm_exti.ftsr |= mask;
97         else
98                 stm_exti.ftsr &= ~mask;
99
100         if (pin <= 4)
101                 irq = STM_ISR_EXTI0_POS + pin;
102         else if (pin <= 9)
103                 irq = STM_ISR_EXTI9_5_POS;
104         else
105                 irq = STM_ISR_EXTI15_10_POS;
106
107         /* Set priority */
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;
113
114         stm_nvic_set_priority(irq, prio);
115         stm_nvic_set_enable(irq);
116 }
117
118 void
119 ao_exti_set_mode(struct stm_gpio *gpio, uint8_t pin, uint8_t mode) {
120         uint32_t        mask = 1 << pin;
121         
122         if (mode & AO_EXTI_MODE_RISING)
123                 stm_exti.rtsr |= mask;
124         else
125                 stm_exti.rtsr &= ~mask;
126         if (mode & AO_EXTI_MODE_FALLING)
127                 stm_exti.ftsr |= mask;
128         else
129                 stm_exti.ftsr &= ~mask;
130 }
131
132 void
133 ao_exti_set_callback(struct stm_gpio *gpio, uint8_t pin, void (*callback)()) {
134         ao_exti_callback[pin] = callback;
135 }
136
137 void
138 ao_exti_enable(struct stm_gpio *gpio, uint8_t pin) {
139         uint32_t        mask = (1 << pin);
140         stm_exti.pr = mask;
141         stm_exti.imr |= (1 << pin);
142 }
143
144 void
145 ao_exti_disable(struct stm_gpio *gpio, uint8_t pin) {
146         uint32_t        mask = (1 << pin);
147         stm_exti.imr &= ~mask;
148         stm_exti.pr = mask;
149 }
150
151 void
152 ao_exti_init(void)
153 {
154         stm_nvic_set_priority(STM_ISR_EXTI1_POS, AO_STM_NVIC_MED_PRIORITY);
155         stm_nvic_set_priority(STM_ISR_EXTI2_POS, AO_STM_NVIC_MED_PRIORITY);
156         stm_nvic_set_priority(STM_ISR_EXTI3_POS, AO_STM_NVIC_MED_PRIORITY);
157         stm_nvic_set_priority(STM_ISR_EXTI4_POS, AO_STM_NVIC_MED_PRIORITY);
158         stm_nvic_set_priority(STM_ISR_EXTI9_5_POS, AO_STM_NVIC_MED_PRIORITY);
159         stm_nvic_set_priority(STM_ISR_EXTI15_10_POS, AO_STM_NVIC_MED_PRIORITY);
160 }