altos: Starting to write cc1120 driver
[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         /* configure pin as input, setting selected pull-up/down mode */
57         stm_moder_set(gpio, pin, STM_MODER_INPUT);
58         switch (mode & (AO_EXTI_MODE_PULL_UP|AO_EXTI_MODE_PULL_DOWN)) {
59         case 0:
60         default:
61                 pupdr  = STM_PUPDR_NONE;
62                 break;
63         case AO_EXTI_MODE_PULL_UP:
64                 pupdr = STM_PUPDR_PULL_UP;
65                 break;
66         case AO_EXTI_MODE_PULL_DOWN:
67                 pupdr = STM_PUPDR_PULL_DOWN;
68                 break;
69         }
70         stm_pupdr_set(gpio, pin, pupdr);
71
72         /* Set interrupt mask and rising/falling mode */
73         stm_exti.imr &= ~mask;
74         stm_exti.rtsr |= mask;
75         if (mode & AO_EXTI_MODE_RISING)
76                 stm_exti.rtsr |= mask;
77         if (mode & AO_EXTI_MODE_FALLING)
78                 stm_exti.ftsr |= mask;
79
80         if (pin <= 4)
81                 irq = STM_ISR_EXTI0_POS + pin;
82         else if (pin <= 9)
83                 irq = STM_ISR_EXTI9_5_POS;
84         else
85                 irq = STM_ISR_EXTI15_10_POS;
86         stm_nvic_set_priority(irq, 10);
87         stm_nvic_set_enable(irq);
88 }
89
90 void
91 ao_exti_enable(struct stm_gpio *gpio, uint8_t pin) {
92         stm_exti.imr |= (1 << pin);
93 }
94
95 void
96 ao_exti_disable(struct stm_gpio *gpio, uint8_t pin) {
97         stm_exti.imr &= ~(1 << pin);
98 }
99
100 void
101 ao_exti_init(void)
102 {
103         stm_nvic_set_priority(STM_ISR_EXTI1_POS, 10);
104         stm_nvic_set_priority(STM_ISR_EXTI2_POS, 10);
105         stm_nvic_set_priority(STM_ISR_EXTI3_POS, 10);
106         stm_nvic_set_priority(STM_ISR_EXTI4_POS, 10);
107         stm_nvic_set_priority(STM_ISR_EXTI9_5_POS, 10);
108         stm_nvic_set_priority(STM_ISR_EXTI15_10_POS, 10);
109 }