altos: Get cc1120 packet reception working
[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 static void
24 ao_exti_isr(void) {
25         uint32_t        pending = stm_exti.pr;
26         uint8_t         pin;
27
28         /* Clear pending interrupts */
29         stm_exti.pr = pending;
30         for (pin = 0; pin < 16 && pending; pin++) {
31                 uint32_t        mask = (1 << pin);
32
33                 if (pending & mask) {
34                         pending &= ~mask;
35                         if (ao_exti_callback[pin])
36                                 (*ao_exti_callback[pin])();
37                 }
38         }
39 }
40
41 void stm_exti0_isr(void) { ao_exti_isr(); }
42 void stm_exti1_isr(void) { ao_exti_isr(); }
43 void stm_exti2_isr(void) { ao_exti_isr(); }
44 void stm_exti3_isr(void) { ao_exti_isr(); }
45 void stm_exti4_isr(void) { ao_exti_isr(); }
46 void stm_exti9_5_isr(void) { ao_exti_isr(); }
47 void stm_exti15_10_isr(void) { ao_exti_isr(); }
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
55         ao_exti_callback[pin] = callback;
56
57         /* configure gpio to interrupt routing */
58         stm_exticr_set(gpio, pin);
59
60         /* configure pin as input, setting selected pull-up/down mode */
61         stm_moder_set(gpio, pin, STM_MODER_INPUT);
62         switch (mode & (AO_EXTI_MODE_PULL_UP|AO_EXTI_MODE_PULL_DOWN)) {
63         case 0:
64         default:
65                 pupdr  = STM_PUPDR_NONE;
66                 break;
67         case AO_EXTI_MODE_PULL_UP:
68                 pupdr = STM_PUPDR_PULL_UP;
69                 break;
70         case AO_EXTI_MODE_PULL_DOWN:
71                 pupdr = STM_PUPDR_PULL_DOWN;
72                 break;
73         }
74         stm_pupdr_set(gpio, pin, pupdr);
75
76         /* Set interrupt mask and rising/falling mode */
77         stm_exti.imr &= ~mask;
78         if (mode & AO_EXTI_MODE_RISING)
79                 stm_exti.rtsr |= mask;
80         else
81                 stm_exti.rtsr &= ~mask;
82         if (mode & AO_EXTI_MODE_FALLING)
83                 stm_exti.ftsr |= mask;
84         else
85                 stm_exti.ftsr &= ~mask;
86
87         if (pin <= 4)
88                 irq = STM_ISR_EXTI0_POS + pin;
89         else if (pin <= 9)
90                 irq = STM_ISR_EXTI9_5_POS;
91         else
92                 irq = STM_ISR_EXTI15_10_POS;
93         stm_nvic_set_priority(irq, 10);
94         stm_nvic_set_enable(irq);
95 }
96
97 void
98 ao_exti_set_callback(struct stm_gpio *gpio, uint8_t pin, void (*callback)()) {
99         ao_exti_callback[pin] = callback;
100 }
101
102 void
103 ao_exti_enable(struct stm_gpio *gpio, uint8_t pin) {
104         uint32_t        mask = (1 << pin);
105         stm_exti.pr = mask;
106         stm_exti.imr |= (1 << pin);
107 }
108
109 void
110 ao_exti_disable(struct stm_gpio *gpio, uint8_t pin) {
111         uint32_t        mask = (1 << pin);
112         stm_exti.imr &= ~mask;
113         stm_exti.pr = mask;
114 }
115
116 void
117 ao_exti_init(void)
118 {
119         stm_nvic_set_priority(STM_ISR_EXTI1_POS, 10);
120         stm_nvic_set_priority(STM_ISR_EXTI2_POS, 10);
121         stm_nvic_set_priority(STM_ISR_EXTI3_POS, 10);
122         stm_nvic_set_priority(STM_ISR_EXTI4_POS, 10);
123         stm_nvic_set_priority(STM_ISR_EXTI9_5_POS, 10);
124         stm_nvic_set_priority(STM_ISR_EXTI15_10_POS, 10);
125 }