2 * Copyright © 2022 Keith Packard <keithp@keithp.com>
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.
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.
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.
22 struct ao_samd21_exti {
23 void (*callback)(void);
24 struct samd21_port *port;
28 static struct ao_samd21_exti ao_samd21_exti[SAMD21_NUM_EIC];
31 pin_id(struct samd21_port *port, uint8_t pin)
33 /* Why, atmel, why? */
34 if (port == &samd21_port_a) {
49 /* most pins use exti mapped to their pin number directly (mod 16) */
57 uint32_t intflag = samd21_eic.intflag;
60 for (id = 0; id < SAMD21_NUM_EIC; id++) {
61 uint32_t mask = (1 << id);
64 samd21_eic.intflag = mask;
65 if (ao_samd21_exti[id].callback)
66 (*ao_samd21_exti[id].callback)();
72 _ao_exti_set_sense(uint8_t id, uint8_t mode)
74 uint8_t sense = mode & (SAMD21_EIC_CONFIG_SENSE_RISE | SAMD21_EIC_CONFIG_SENSE_FALL);
75 uint8_t n = SAMD21_EIC_CONFIG_N(id);
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;
85 ao_exti_setup (struct samd21_port *port, uint8_t pin, uint8_t mode, void (*callback)(void))
87 uint8_t id = pin_id(port,pin);
88 struct ao_samd21_exti *exti = &ao_samd21_exti[id];
91 ao_panic(AO_PANIC_EXTI);
93 if (!(mode & AO_EXTI_PIN_NOCONFIGURE))
94 ao_enable_input(port, pin, mode);
96 ao_arch_block_interrupts();
100 exti->callback = callback;
102 /* Set edge triggered */
103 _ao_exti_set_sense(id, mode);
105 ao_arch_release_interrupts();
109 ao_exti_set_mode(struct samd21_port *port, uint8_t pin, uint8_t mode)
111 uint8_t id = pin_id(port,pin);
113 ao_arch_block_interrupts();
114 _ao_exti_set_sense(id, mode);
115 ao_arch_release_interrupts();
119 ao_exti_set_callback(struct samd21_port *port, uint8_t pin, void (*callback)(void))
121 uint8_t id = pin_id(port,pin);
123 ao_arch_block_interrupts();
124 ao_samd21_exti[id].callback = callback;
125 ao_arch_release_interrupts();
129 ao_exti_enable(struct samd21_port *port, uint8_t pin)
131 uint8_t id = pin_id(port,pin);
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();
141 ao_exti_disable(struct samd21_port *port, uint8_t pin)
143 uint8_t id = pin_id(port,pin);
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();
155 samd21_gclk_clkctrl(0, SAMD21_GCLK_CLKCTRL_ID_EIC);
158 samd21_eic.ctrl = (1 << SAMD21_EIC_CTRL_SWRST);
160 while (samd21_eic.status & (1 << SAMD21_EIC_STATUS_SYNCBUSY))
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);
168 samd21_eic.ctrl = (1 << SAMD21_EIC_CTRL_ENABLE);