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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#[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.
#[allow(unused_macros)]
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()
            }
        }

        pub fn enable_event(&mut self, eic: &mut super::ConfigurableEIC) {
            eic.eic.evctrl.modify(|_, w| unsafe {
                w.bits(1 << $num)
            });
        }

        pub fn enable_interrupt(&mut self, eic: &mut super::ConfigurableEIC) {
            eic.eic.intenset.write(|w| unsafe {
                w.bits(1 << $num)
            })
        }

        pub fn disable_interrupt(&mut self, eic: &mut super::ConfigurableEIC) {
            eic.eic.intenclr.write(|w| unsafe {
                w.bits(1 << $num)
            })
        }

        pub fn is_interrupt(&mut self) -> bool {
            let intflag = unsafe { &(*pac::EIC::ptr()) }.intflag.read().bits();
            intflag & (1 << $num) != 0
        }

        pub fn state(&mut self) -> bool {
            let state = unsafe { &(*pac::EIC::ptr()) }.pinstate.read().bits();
            state & (1 << $num) != 0
        }

        pub fn clear_interrupt(&mut self) {
            unsafe {
                {&(*pac::EIC::ptr())}.intflag.write(|w| { w.bits(1 << $num) });
            }
        }

        pub fn sense(&mut self, _eic: &mut super::ConfigurableEIC, 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::ConfigurableEIC, 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
            }
        }
    )+
}

    };
}

ei!(ExtInt[0] {
    PA00,
    PA16,
    #[cfg(feature = "min-samd51j")]
    PB00,
    #[cfg(feature = "min-samd51j")]
    PB16,
    #[cfg(feature = "min-samd51n")]
    PC00,
    #[cfg(feature = "min-samd51n")]
    PC16,
    #[cfg(feature = "min-samd51p")]
    PD00,
});

ei!(ExtInt[1] {
    PA01,
    PA17,
    #[cfg(feature = "min-samd51j")]
    PB01,
    #[cfg(feature = "min-samd51j")]
    PB17,
    #[cfg(feature = "min-samd51n")]
    PC01,
    #[cfg(feature = "min-samd51n")]
    PC17,
    #[cfg(feature = "min-samd51p")]
    PD01,
});

ei!(ExtInt[2] {
    PA02,
    PA18,
    PB02,
    #[cfg(feature = "min-samd51n")]
    PB18,
    #[cfg(feature = "min-samd51n")]
    PC02,
    #[cfg(feature = "min-samd51n")]
    PC18,
});

ei!(ExtInt[3] {
    PA03,
    PA19,
    PB03,
    #[cfg(feature = "min-samd51n")]
    PB19,
    #[cfg(feature = "min-samd51n")]
    PC03,
    #[cfg(feature = "min-samd51n")]
    PC19,
    #[cfg(feature = "min-samd51p")]
    PD08,
});

ei!(ExtInt[4] {
    PA04,
    PA20,
    #[cfg(feature = "min-samd51j")]
    PB04,
    #[cfg(feature = "min-samd51n")]
    PB20,
    #[cfg(feature = "min-samd51p")]
    PC04,
    #[cfg(feature = "min-samd51n")]
    PC20,
    #[cfg(feature = "min-samd51p")]
    PD09,
});

ei!(ExtInt[5] {
    PA05,
    PA21,
    #[cfg(feature = "min-samd51j")]
    PB05,
    #[cfg(feature = "min-samd51n")]
    PB21,
    #[cfg(feature = "min-samd51n")]
    PC05,
    #[cfg(feature = "min-samd51n")]
    PC21,
    #[cfg(feature = "min-samd51p")]
    PD10,
});

ei!(ExtInt[6] {
    PA06,
    PA22,
    #[cfg(feature = "min-samd51j")]
    PB06,
    PB22,
    #[cfg(feature = "min-samd51n")]
    PC06,
    #[cfg(feature = "min-samd51p")]
    PC22,
    #[cfg(feature = "min-samd51p")]
    PD11,
});

ei!(ExtInt[7] {
    PA07,
    PA23,
    #[cfg(feature = "min-samd51j")]
    PB07,
    PB23,
    #[cfg(feature = "min-samd51p")]
    PC23,
    #[cfg(feature = "min-samd51p")]
    PD12,
});

ei!(ExtInt[8] {
    PA24,
    PB08,
    #[cfg(feature = "min-samd51n")]
    PB24,
    #[cfg(feature = "min-samd51n")]
    PC24,
});

ei!(ExtInt[9] {
    PA09,
    PA25,
    PB09,
    #[cfg(feature = "min-samd51n")]
    PB25,
    #[cfg(feature = "min-samd51n")]
    PC07,
    #[cfg(feature = "min-samd51n")]
    PC25,
});

ei!(ExtInt[10] {
    PA10,
    PB10,
    #[cfg(feature = "min-samd51n")]
    PC10,
    #[cfg(feature = "min-samd51n")]
    PC26,
    #[cfg(feature = "min-samd51p")]
    PD20,
});

ei!(ExtInt[11] {
    PA11,
    PA27,
    PB11,
    #[cfg(feature = "min-samd51n")]
    PC11,
    #[cfg(feature = "min-samd51n")]
    PC27,
    #[cfg(feature = "min-samd51p")]
    PD21,
});

ei!(ExtInt[12] {
    PA12,
    #[cfg(feature = "min-samd51j")]
    PB12,
    #[cfg(feature = "min-samd51p")]
    PB26,
    #[cfg(feature = "min-samd51n")]
    PC12,
    #[cfg(feature = "min-samd51n")]
    PC28,
});

ei!(ExtInt[13] {
    PA13,
    #[cfg(feature = "min-samd51j")]
    PB13,
    #[cfg(feature = "min-samd51p")]
    PB27,
    #[cfg(feature = "min-samd51n")]
    PC13,
});

ei!(ExtInt[14] {
    PA14,
    PA30,
    #[cfg(feature = "min-samd51j")]
    PB14,
    #[cfg(feature = "min-samd51p")]
    PB28,
    #[cfg(feature = "min-samd51j")]
    PB30,
    #[cfg(feature = "min-samd51n")]
    PC14,
    #[cfg(feature = "min-samd51p")]
    PC30,
});

ei!(ExtInt[15] {
    PA15,
    PA31,
    #[cfg(feature = "min-samd51j")]
    PB15,
    #[cfg(feature = "min-samd51p")]
    PB29,
    #[cfg(feature = "min-samd51j")]
    PB31,
    #[cfg(feature = "min-samd51n")]
    PC15,
    #[cfg(feature = "min-samd51p")]
    PC31,
});