1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#[cfg(feature = "unproven")]
use crate::ehal::digital::v2::InputPin;
use crate::gpio::{
    self, pin::*, AnyPin, FloatingInterrupt, PinMode, PullDownInterrupt, PullUpInterrupt,
};
use crate::pac;

/// The EicPin trait makes it more ergonomic to convert a gpio pin into an EIC
/// pin. You should not implement this trait for yourself; only the
/// implementations in the EIC module make sense.
// This is more complicated than it needs to be, due to the ExtInt structs being
// defined through macros below.
pub trait EicPin {
    type Floating;
    type PullUp;
    type PullDown;

    /// Configure a pin as a floating external interrupt
    fn into_floating_ei(self) -> Self::Floating;

    /// Configure a pin as pulled-up external interrupt
    fn into_pull_up_ei(self) -> Self::PullUp;

    /// Configure a pin as pulled-down external interrupt
    fn into_pull_down_ei(self) -> Self::PullDown;
}

pub type Sense = pac::eic::config::SENSE0_A;

pub type ExternalInterruptID = usize;

/// ExternalInterrupt describes something with an external interrupt ID.
pub trait ExternalInterrupt {
    fn id(&self) -> ExternalInterruptID;
}

/// The pad macro defines the given EIC pin and implements EicPin for the
/// given pins. The EicPin implementation will configure the pin for the
/// appropriate function and return the pin wrapped in the EIC type.
macro_rules! ei {
    (
        $PadType:ident [ $num:expr ] {
            $(
                $(#[$attr:meta])*
                $PinType:ident,
            )+
        }
    ) => {

crate::paste::item! {
    /// Represents a numbered external interrupt. The external interrupt is generic
    /// over any pin, only the EicPin implementations in this module make sense.
    pub struct [<$PadType $num>]<GPIO>
    where
        GPIO: AnyPin,
    {
        _pin: Pin<GPIO::Id, GPIO::Mode>,
    }

    // impl !Send for [<$PadType $num>]<GPIO> {}
    // impl !Sync for [<$PadType $num>]<GPIO> {}

    impl<GPIO: AnyPin> [<$PadType $num>]<GPIO> {
        /// Construct pad from the appropriate pin in any mode.
        /// You may find it more convenient to use the `into_pad` trait
        /// and avoid referencing the pad type.
        pub fn new(pin: GPIO) -> Self {
            [<$PadType $num>]{
                _pin:pin.into()
            }
        }

        /// Configure the eic with options for this external interrupt
        pub fn enable_event(&mut self, eic: &mut super::EIC) {
            eic.eic.evctrl.modify(|_, w| {
                w.[<extinteo $num>]().set_bit()
            });
        }

        pub fn enable_interrupt(&mut self, eic: &mut super::EIC) {
            eic.eic.intenset.modify(|_, w| {
                w.[<extint $num>]().set_bit()
            });
        }

        pub fn enable_interrupt_wake(&mut self, eic: &mut super::EIC) {
            eic.eic.wakeup.modify(|_, w| {
                w.[<wakeupen $num>]().set_bit()
            })
        }

        pub fn disable_interrupt(&mut self, eic: &mut super::EIC) {
            eic.eic.intenclr.modify(|_, w| {
                w.[<extint $num>]().set_bit()
            });
        }

        pub fn is_interrupt(&mut self) -> bool {
            unsafe { &(*pac::EIC::ptr()) }.intflag.read().[<extint $num>]().bit_is_set()
        }

        pub fn clear_interrupt(&mut self) {
            unsafe { &(*pac::EIC::ptr()) }.intflag.modify(|_, w| {
                w.[<extint $num>]().set_bit()
            });
        }

        pub fn sense(&mut self, _eic: &mut super::EIC, sense: Sense) {
            // Which of the two config blocks this eic config is in
            let offset = ($num >> 3) & 0b0001;
            let config = unsafe { &(*pac::EIC::ptr()).config[offset] };

            config.modify(|_, w| unsafe {
                // Which of the eight eic configs in this config block
                match $num & 0b111 {
                    0b000 => w.sense0().bits(sense as u8),
                    0b001 => w.sense1().bits(sense as u8),
                    0b010 => w.sense2().bits(sense as u8),
                    0b011 => w.sense3().bits(sense as u8),
                    0b100 => w.sense4().bits(sense as u8),
                    0b101 => w.sense5().bits(sense as u8),
                    0b110 => w.sense6().bits(sense as u8),
                    0b111 => w.sense7().bits(sense as u8),
                    _ => unreachable!(),
                }
            });
        }

        pub fn filter(&mut self, _eic: &mut super::EIC, filter: bool) {
            // Which of the two config blocks this eic config is in
            let offset = ($num >> 3) & 0b0001;
            let config = unsafe { &(*pac::EIC::ptr()).config[offset] };

            config.modify(|_, w| {
                // Which of the eight eic configs in this config block
                match $num & 0b111 {
                    0b000 => w.filten0().bit(filter),
                    0b001 => w.filten1().bit(filter),
                    0b010 => w.filten2().bit(filter),
                    0b011 => w.filten3().bit(filter),
                    0b100 => w.filten4().bit(filter),
                    0b101 => w.filten5().bit(filter),
                    0b110 => w.filten6().bit(filter),
                    0b111 => w.filten7().bit(filter),
                    _ => unreachable!(),
                }
            });
        }
    }

    impl<GPIO: AnyPin> ExternalInterrupt for [<$PadType $num>]<GPIO> {
        fn id(&self) -> ExternalInterruptID {
            $num
        }
    }

    #[cfg(feature = "unproven")]
    impl<GPIO, C> InputPin for [<$PadType $num>]<GPIO>
    where
        GPIO: AnyPin<Mode = Interrupt<C>>,
        C: InterruptConfig,
    {
        type Error = core::convert::Infallible;
        #[inline]
        fn is_high(&self) -> Result<bool, Self::Error> {
            self._pin.is_high()
        }
        #[inline]
        fn is_low(&self) -> Result<bool, Self::Error> {
            self._pin.is_low()
        }
    }

    $(
        $(#[$attr])*
        impl<M: PinMode> EicPin for Pin<gpio::$PinType, M> {
            type Floating = [<$PadType $num>]<Pin<gpio::$PinType, FloatingInterrupt>>;
            type PullUp = [<$PadType $num>]<Pin<gpio::$PinType, PullUpInterrupt>>;
            type PullDown = [<$PadType $num>]<Pin<gpio::$PinType, PullDownInterrupt>>;

            fn into_floating_ei(self) -> Self::Floating {
                [<$PadType $num>]::new(self.into_floating_interrupt())
            }

            fn into_pull_up_ei(self) -> Self::PullUp {
                [<$PadType $num>]::new(self.into_pull_up_interrupt())
            }

            fn into_pull_down_ei(self) -> Self::PullDown {
                [<$PadType $num>]::new(self.into_pull_down_interrupt())
            }
        }

        $(#[$attr])*
        impl<M: PinMode> ExternalInterrupt for Pin<gpio::$PinType, M> {
            fn id(&self) -> ExternalInterruptID {
                $num
            }
        }
    )+
}

    };
}

// The SAMD11 and SAMD21 devices have different ExtInt designations. Just for
// clarity's sake, the `ei!()` invocations below have been split into SAMD11-
// and SAMD21-specific declarations.

// SAMD11

#[cfg(feature = "samd11")]
ei!(ExtInt[1] {
    PA15,
});

#[cfg(feature = "samd11")]
ei!(ExtInt[2] {
    PA02,
});

#[cfg(feature = "samd11")]
ei!(ExtInt[3] {
    PA31,
});

#[cfg(feature = "samd11")]
ei!(ExtInt[4] {
    PA04,
    PA24,
});

#[cfg(feature = "samd11")]
ei!(ExtInt[5] {
    PA05,
    PA25,
});

#[cfg(feature = "samd11")]
ei!(ExtInt[6] {
    PA08,
});

#[cfg(feature = "samd11")]
ei!(ExtInt[7] {
    PA09,
});

// SAMD21

#[cfg(feature = "samd21")]
ei!(ExtInt[0] {
    #[cfg(not(any(feature = "samd21el", feature = "samd21gl")))]
    PA00,
    PA16,
    #[cfg(feature = "min-samd21j")]
    PB00,
    #[cfg(feature = "min-samd21j")]
    PB16,
});

#[cfg(feature = "samd21")]
ei!(ExtInt[1] {
    #[cfg(not(any(feature = "samd21el", feature = "samd21gl")))]
    PA01,
    PA17,
    #[cfg(feature = "min-samd21j")]
    PB01,
    #[cfg(feature = "min-samd21j")]
    PB17,
});

#[cfg(feature = "samd21")]
ei!(ExtInt[2] {
    PA02,
    PA18,
    #[cfg(feature = "min-samd21g")]
    PB02,
});

#[cfg(feature = "samd21")]
ei!(ExtInt[3] {
    PA03,
    PA19,
    #[cfg(feature = "min-samd21g")]
    PB03,
});

#[cfg(feature = "samd21")]
ei!(ExtInt[4] {
    PA04,
    #[cfg(feature = "min-samd21g")]
    PA20,
    #[cfg(feature = "min-samd21j")]
    PB04,
});

#[cfg(feature = "samd21")]
ei!(ExtInt[5] {
    PA05,
    #[cfg(feature = "min-samd21g")]
    PA21,
    #[cfg(feature = "min-samd21j")]
    PB05,
});

#[cfg(feature = "samd21")]
ei!(ExtInt[6] {
    PA06,
    PA22,
    #[cfg(feature = "min-samd21j")]
    PB06,
    #[cfg(all(feature = "min-samd21g", not(feature = "samd21gl")))]
    PB22,
});

#[cfg(feature = "samd21")]
ei!(ExtInt[7] {
    PA07,
    PA23,
    #[cfg(feature = "min-samd21j")]
    PB07,
    #[cfg(all(feature = "min-samd21g", not(feature = "samd21gl")))]
    PB23,
});

#[cfg(feature = "samd21")]
ei!(ExtInt[8] {
    #[cfg(not(feature = "samd21el"))]
    PA28,
    #[cfg(feature = "min-samd21g")]
    PB08,
});

#[cfg(feature = "samd21")]
ei!(ExtInt[9] {
    PA09,
    #[cfg(feature = "min-samd21g")]
    PB09,
});

#[cfg(feature = "samd21")]
ei!(ExtInt[10] {
    PA10,
    PA30,
    #[cfg(feature = "min-samd21g")]
    PB10,
});

#[cfg(feature = "samd21")]
ei!(ExtInt[11] {
   PA11,
   PA31,
   #[cfg(feature = "min-samd21g")]
   PB11,
});

#[cfg(feature = "samd21")]
ei!(ExtInt[12] {
    #[cfg(feature = "min-samd21g")]
    PA12,
    PA24,
    #[cfg(feature = "min-samd21j")]
    PB12,
});

#[cfg(feature = "samd21")]
ei!(ExtInt[13] {
    #[cfg(feature = "min-samd21g")]
    PA13,
    PA25,
    #[cfg(feature = "min-samd21j")]
    PB13,
});

#[cfg(feature = "samd21")]
ei!(ExtInt[14] {
    PA14,
    #[cfg(feature = "min-samd21j")]
    PB14,
    #[cfg(feature = "min-samd21j")]
    PB30,
});

#[cfg(feature = "samd21")]
ei!(ExtInt[15] {
    PA15,
    PA27,
    #[cfg(feature = "min-samd21j")]
    PB15,
    #[cfg(feature = "min-samd21j")]
    PB31,
});