962197837713975d0950d9e7db048fe0ae0f5050
[fw/altos] / src / samd21 / ao_exti_samd21.c
1 /*
2  * Copyright © 2022 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <ao.h>
20 #include <ao_exti.h>
21
22 struct ao_samd21_exti {
23         void                    (*callback)(void);
24         struct samd21_port      *port;
25         uint8_t                 pin;
26 };
27
28 static struct ao_samd21_exti ao_samd21_exti[SAMD21_NUM_EIC];
29
30 static uint8_t
31 pin_id(struct samd21_port *port, uint8_t pin)
32 {
33         /* Why, atmel, why? */
34         if (port == &samd21_port_a) {
35                 switch (pin) {
36                 case 24:
37                 case 25:
38                 case 27:
39                         return pin - 12;
40                 case 28:
41                 case 30:
42                 case 31:
43                         return pin - 20;
44                 default:
45                         break;
46                 }
47         }
48
49         /* most pins use exti mapped to their pin number directly (mod 16) */
50         return pin & 0xf;
51 }
52
53
54 void
55 samd21_eic_isr(void)
56 {
57         uint32_t        intflag = samd21_eic.intflag;
58         uint8_t         id;
59
60         for (id = 0; id < SAMD21_NUM_EIC; id++) {
61                 uint32_t mask = (1 << id);
62
63                 if (intflag & mask) {
64                         samd21_eic.intflag = mask;
65                         if (ao_samd21_exti[id].callback)
66                                 (*ao_samd21_exti[id].callback)();
67                 }
68         }
69 }
70
71 static void
72 _ao_exti_set_sense(uint8_t id, uint8_t mode)
73 {
74         uint8_t         sense = mode & (SAMD21_EIC_CONFIG_SENSE_RISE | SAMD21_EIC_CONFIG_SENSE_FALL);
75         uint8_t         n = SAMD21_EIC_CONFIG_N(id);
76         uint32_t        config;
77
78         config = samd21_eic.config[n];
79         config &= ~(SAMD21_EIC_CONFIG_SENSE_MASK << SAMD21_EIC_CONFIG_SENSE(id));
80         config |= (sense << SAMD21_EIC_CONFIG_SENSE(id));
81         samd21_eic.config[n] = config;
82 }
83
84 void
85 ao_exti_setup (struct samd21_port *port, uint8_t pin, uint8_t mode, void (*callback)(void))
86 {
87         uint8_t                 id = pin_id(port,pin);
88         struct ao_samd21_exti   *exti = &ao_samd21_exti[id];
89
90         if (exti->port)
91                 ao_panic(AO_PANIC_EXTI);
92
93         if (!(mode & AO_EXTI_PIN_NOCONFIGURE))
94                 ao_enable_input(port, pin, mode);
95
96         ao_arch_block_interrupts();
97
98         exti->port = port;
99         exti->pin = pin;
100         exti->callback = callback;
101
102         /* Set edge triggered */
103         _ao_exti_set_sense(id, mode);
104
105         ao_arch_release_interrupts();
106 }
107
108 void
109 ao_exti_set_mode(struct samd21_port *port, uint8_t pin, uint8_t mode)
110 {
111         uint8_t                 id = pin_id(port,pin);
112
113         ao_arch_block_interrupts();
114         _ao_exti_set_sense(id, mode);
115         ao_arch_release_interrupts();
116 }
117
118 void
119 ao_exti_set_callback(struct samd21_port *port, uint8_t pin, void (*callback)(void))
120 {
121         uint8_t         id = pin_id(port,pin);
122
123         ao_arch_block_interrupts();
124         ao_samd21_exti[id].callback = callback;
125         ao_arch_release_interrupts();
126 }
127
128 void
129 ao_exti_enable(struct samd21_port *port, uint8_t pin)
130 {
131         uint8_t         id = pin_id(port,pin);
132
133         ao_arch_block_interrupts();
134         samd21_eic.intenset = 1 << id;
135         /* configure gpio to interrupt routing */
136         samd21_port_pmux_set(port, pin, SAMD21_PORT_PMUX_FUNC_A);
137         ao_arch_release_interrupts();
138 }
139
140 void
141 ao_exti_disable(struct samd21_port *port, uint8_t pin)
142 {
143         uint8_t         id = pin_id(port,pin);
144
145         ao_arch_block_interrupts();
146         samd21_eic.intenclr = 1 << id;
147         /* configure gpio to interrupt routing */
148         samd21_port_pmux_clr(port, pin);
149         ao_arch_release_interrupts();
150 }
151
152 void
153 ao_exti_init(void)
154 {
155         samd21_gclk_clkctrl(0, SAMD21_GCLK_CLKCTRL_ID_EIC);
156
157         /* Reset */
158         samd21_eic.ctrl = (1 << SAMD21_EIC_CTRL_SWRST);
159
160         while (samd21_eic.status & (1 << SAMD21_EIC_STATUS_SYNCBUSY))
161                 ;
162
163         /* Wire up interrupts */
164         samd21_nvic_set_enable(SAMD21_NVIC_ISR_EIC_POS);
165         samd21_nvic_set_priority(SAMD21_NVIC_ISR_EIC_POS, 3);
166
167         /* Enable */
168         samd21_eic.ctrl = (1 << SAMD21_EIC_CTRL_ENABLE);
169 }