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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
//! PORTx digital IO Implementations
//!
//! # Design Rationale
//! Each pin has a distinct type which allows pin-dependent HAL functionality to ensure at
//! compile-time that the correct pins are used.  For example, certain peripherals have the IO
//! hardwired to some specific pins which can't be changed.  For purposes where the exact pin does
//! not matter, the distinct types can be 'downgraded' into a generic `Pin<MODE>` type.  See the
//! section about [downgrading](#downgrading) further down.
//!
//! To instanciate the pin types, a port is `.split()` into its pins:
//!
//! ```ignore
//! let dp = atmega32u4::Peripherals::take().unwrap();
//!
//! let mut portd = dp.PORTD.split();
//!
//! let pd2 = portd.pd2.into_output(&mut portd.ddr);
//! ```
//!
//! Board crates usually provide a wrapper around that which makes access more convenient:
//!
//! ```ignore
//! let dp = arduino_leonardo::Peripherals::take().unwrap();
//!
//! let mut pins = arduino_leonardo::Pins::new(
//!     dp.PORTB,
//!     dp.PORTC,
//!     dp.PORTD,
//!     dp.PORTE,
//! );
//!
//! let mut led0 = pins.led_rx.into_output(&mut pins.ddr);
//! let mut led1 = pins.led_tx.into_output(&mut pins.ddr);
//! let mut led2 = pins.d13.into_output(&mut pins.ddr);
//! ```
//!
//! # Modes
//! A pin's mode is modelled via the `<MODE>` generic parameter.  Only when the pin is in the
//! correct mode, relevant methods (e.g. `set_high()`) are available.  Changing the mode is done
//! via conversion methods that consume the pin:
//!
//! ```ignore
//! // By default, pins are floating inputs
//! let pd2: PD2<mode::Input<mode::Floating>> = portd.pd2;
//!
//! // Convert into pull-up input
//! let pd2: PD2<mode::Input<mode::PullUp>> = pd2.into_pull_up_input(&mut portd.ddr);
//!
//! // Convert into output
//! let pd2: PD2<mode::Output> = pd2.into_output(&mut portd.ddr);
//!
//! // Convert into tri-state input and output.
//! let pd2: PD2<mode::TriState> = pd2.into_tri_state(&mut portd.ddr);
//! ```
//!
//! ### Digital Input
//! Digital Input pins (i.e. where `MODE` = `mode::Input<_>`) have the following methods available:
//!
//! ```ignore
//! // `true` if the pin is high, `false` if it is low
//! pd2.is_high().void_unwrap();
//!
//! // `true if the pin is low, `false` if it is high
//! pd2.is_low().void_unwrap();
//! ```
//!
//! ### Digital Output
//! Digital Output pins (i.e. where `MODE` = `mode::Output`) can be used like this:
//!
//! ```ignore
//! // Set high or low
//! pd2.set_high().void_unwrap();
//! pd2.set_low().void_unwrap();
//!
//! // Check what the pin was last set to
//! pd2.is_set_high().void_unwrap();
//! pd2.is_set_low().void_unwrap();
//! ```
//!
//! ### Digital Tri-State Output and Input
//! Digital I/O pins in tri-state mode (i.e. where `MODE` = `mode::TriState`),
//! usually with an external pull-up, are useful for a one wire bus.
//! They can be used as both output and input pins, like this:
//!
//! ```ignore
//! // Actively drive the pin low.
//! pd2.set_low().void_unwrap();
//! // Release the pin, allowing the external pull-up to pull the pin
//! // in the absence of another driver
//! pd2.set_high().void_unwrap();
//!
//! // `true` if the pin is electrically high, `false` if it is low
//! pd2.is_high().void_unwrap();
//! // `true` if the pin is electrically low, driven by either the
//! // microcontroller or externally
//! pd2.is_low().void_unwrap();
//! ```
//!
//! ### Other Modes
//! Apart from input and output, certain pins can have other functionality associated with them.
//! E.g. some pins can be used for PWM output, others as ADC inputs.  For those pins, specific
//! conversion methods exist:
//!
//! ```ignore
//! // Digital IO by default
//! let pd2 = portd.pd2;
//!
//! // Make a pin an ADC channel
//! let pd2_analog = pd2.into_analog_input(&mut adc);
//!
//! // Make a pin a PWM output
//! let pd2_pwm = pd2.into_output().into_pwm(&mut timer0);
//! ```
//!
//! ## Downgrading
//! As described above, usually each pin has its own distinct type.  This is useful in a lot of
//! cases but can be difficult to deal with when code does not care about the exact pin(s) it is
//! working with.  An easy example is trying to store a number of pins in an array; this is not
//! possible when each pin has its own type.
//!
//! For those usecases, a generic `Pin` type exists which can represent any pin.  Specific pins are
//! converted using the `.downgrade()` method:
//!
//! ```ignore
//! let pd2 = portd.pd2.into_output(&mut portd.ddr);
//! let pd3 = portd.pd3.into_output(&mut portd.ddr);
//!
//! let pins: [Pin<mode::Output>; 2] = [pd2.downgrade(), pd3.downgrade()];
//! ```

/// IO Modes
pub mod mode {
    /// Any digital IO mode
    pub trait DigitalIO: private::Unimplementable {}
    /// Any input mode
    pub trait InputMode: private::Unimplementable {}

    /// Pin configured as a digital input
    pub struct Input<MODE: InputMode> {
        _m: core::marker::PhantomData<MODE>,
    }
    /// Pin configured as a digital output
    pub struct Output;
    /// Pin configured as an ADC channel
    pub struct Analog;
    /// Pin configured as PWM output
    pub struct Pwm<TIMER> {
        _m: core::marker::PhantomData<TIMER>,
    }
    /// Pin configured in open drain mode.
    pub struct TriState;

    impl private::Unimplementable for Output {}
    impl<M: InputMode> private::Unimplementable for Input<M> {}
    impl private::Unimplementable for TriState {}
    impl DigitalIO for Output {}
    impl<M: InputMode> DigitalIO for Input<M> {}
    impl DigitalIO for TriState {}

    /// Pin input configured **without** internal pull-up
    pub struct Floating;
    /// Pin input configured with internal pull-up
    pub struct PullUp;

    impl private::Unimplementable for Floating {}
    impl private::Unimplementable for PullUp {}
    impl InputMode for Floating {}
    impl InputMode for PullUp {}

    mod private {
        pub trait Unimplementable {}
    }
}

/// Create a generic pin to be used for downgrading
#[macro_export]
macro_rules! impl_generic_pin {
    (
        pub enum $GenericPin:ident {
            $($PortEnum:ident($PORTX:ty, $reg_port:ident, $reg_pin:ident, $reg_ddr:ident),)+
        }
    ) => {
        mod generic_pin {
            use $crate::hal::digital::v2 as digital;
            use $crate::port::mode;
            use $crate::void::Void;
            use core::marker;

            /// Generic pin type.
            ///
            /// As described in the [general Digital IO documentation][1], this type can represent
            /// any pin for use-cases where the exact pin is not relevant.  This is especially
            /// useful when, e.g. wanting to store a number of pins in an array.
            ///
            /// The generic pin implements all the same digital IO methods (`set_high()`,
            /// `set_low()`, `is_high()`, `is_low()`, etc.) as specific pin types, except that it
            /// cannot be used for pin-specific functions (e.g. PWM, ADC Channel).
            ///
            /// [1]: ../../avr_hal_generic/port/index.html
            pub enum $GenericPin<MODE> {
                $($PortEnum(u8, marker::PhantomData<MODE>),)+
            }

            // Input & Output implementations ------------------------- {{{
            // - Unsafe:  The unsafe blocks in here are ok, because these
            //            operations will compile down to single, atomic
            //            `sbi`, `cbi`, `sbic`, `sbis` instructions.
            impl digital::OutputPin for $GenericPin<mode::Output> {
                type Error = Void;

                fn set_high(&mut self) -> Result<(), Self::Error> {
                    match self {
                        $(
                            $GenericPin::$PortEnum(i, _) => unsafe {
                                (*<$PORTX>::ptr())
                                    .$reg_port
                                    .modify(|r, w| {
                                        w.bits(r.bits() | (1 << *i))
                                    })
                            },
                        )+
                    }
                    Ok(())
                }

                fn set_low(&mut self) -> Result<(), Self::Error> {
                    match self {
                        $(
                            $GenericPin::$PortEnum(i, _) => unsafe {
                                (*<$PORTX>::ptr())
                                    .$reg_port
                                    .modify(|r, w| {
                                        w.bits(r.bits() & !(1 << *i))
                                    })
                            },
                        )+
                    }
                    Ok(())
                }
            }

            impl digital::StatefulOutputPin for $GenericPin<mode::Output> {
                fn is_set_high(&self) -> Result<bool, Self::Error> {
                    Ok(match self {
                        $(
                            $GenericPin::$PortEnum(i, _) => unsafe {
                                (*<$PORTX>::ptr())
                                    .$reg_port.read().bits() & (1 << *i) != 0
                            },
                        )+
                    })
                }

                fn is_set_low(&self) -> Result<bool, Self::Error> {
                    self.is_set_high().map(|b| !b)
                }
            }

            impl digital::ToggleableOutputPin for $GenericPin<mode::Output> {
                type Error = Void;

                fn toggle(&mut self) -> Result<(), Self::Error> {
                    match self {
                        $(
                            $GenericPin::$PortEnum(i, _) => unsafe {
                                (*<$PORTX>::ptr())
                                    .$reg_pin
                                    .write(|w| w.bits(1 << *i))
                            },
                        )+
                    }
                    Ok(())
                }
            }

            impl<MODE: mode::InputMode> digital::InputPin for $GenericPin<mode::Input<MODE>> {
                type Error = Void;

                fn is_high(&self) -> Result<bool, Self::Error> {
                    Ok(match self {
                        $(
                            $GenericPin::$PortEnum(i, _) => unsafe {
                                (*<$PORTX>::ptr())
                                    .$reg_pin.read().bits() & (1 << *i) != 0
                            }
                        )+
                    })
                }

                fn is_low(&self) -> Result<bool, Self::Error> {
                    Ok(match self {
                        $(
                            $GenericPin::$PortEnum(i, _) => unsafe {
                                (*<$PORTX>::ptr())
                                    .$reg_pin.read().bits() & (1 << *i) == 0
                            }
                        )+
                    })
                }
            }

            impl digital::OutputPin for $GenericPin<mode::TriState> {
                type Error = Void;

                fn set_high(&mut self) -> Result<(), Self::Error> {
                    match self {
                        $(
                            $GenericPin::$PortEnum(i, _) => unsafe {
                                (*<$PORTX>::ptr()).$reg_ddr.modify(|r, w| {
                                        w.bits(r.bits() & !(1 << *i))
                                    })
                            },
                        )+
                    }
                    Ok(())
                }

                fn set_low(&mut self) -> Result<(), Self::Error> {
                    match self {
                        $(
                            $GenericPin::$PortEnum(i, _) => unsafe {
                                (*<$PORTX>::ptr()).$reg_ddr.modify(|r, w| {
                                        w.bits(r.bits() | (1 << *i))
                                    })
                            },
                        )+
                    }
                    Ok(())
                }
            }

            impl digital::InputPin for $GenericPin<mode::TriState> {
                type Error = Void;

                fn is_high(&self) -> Result<bool, Self::Error> {
                    Ok(match self {
                        $(
                            $GenericPin::$PortEnum(i, _) => unsafe {
                                (*<$PORTX>::ptr())
                                    .$reg_pin.read().bits() & (1 << *i) != 0
                            }
                        )+
                    })
                }

                fn is_low(&self) -> Result<bool, Self::Error> {
                    Ok(match self {
                        $(
                            $GenericPin::$PortEnum(i, _) => unsafe {
                                (*<$PORTX>::ptr())
                                    .$reg_pin.read().bits() & (1 << *i) == 0
                            }
                        )+
                    })
                }
            }
            // -------------------------------------------------------- }}}
        }
        pub use self::generic_pin::$GenericPin;
    };
}

/// Implement pin abstractions for a port peripheral
#[macro_export]
macro_rules! impl_port {
    // With a generic pin
    (
        pub mod $portx:ident {
            #[port_ext]
            use $portext_use:path;

            #[generic_pin]
            use $GenericPin:ident::$PortEnum:ident;

            impl $PortExt:ident for $PORTX:ty {
                regs: ($reg_pin:ident, $reg_ddr:ident, $reg_port:ident),
                $($pxi:ident: ($PXi:ident, $i:expr),)+
            }
        }
    ) => {
        $crate::impl_port! {
            pub mod $portx {
                #[port_ext]
                use $portext_use;

                impl $PortExt for $PORTX {
                    regs: ($reg_pin, $reg_ddr, $reg_port),
                    $($pxi: ($PXi, $i),)+
                }
            }
        }

        // Downgrade implementation ------------------------------- {{{
        $(
            impl<MODE> $portx::$PXi<MODE> {
                /// Downgrade this pin into a type that is generic over all pins.
                ///
                /// The main use for this function is to store multiple pins in an array.  Please
                /// note that generic pins have a runtime overhead.
                ///
                /// See the [general Digital IO documentation][1] and the [`Pin` type][2] for more
                /// information.
                ///
                /// # Example
                /// ```rust
                /// let p1 = portb.pb1.downgrade();
                /// let p2 = portc.pc7.downgrade();
                /// let pins = [p1, p2];
                /// ```
                ///
                /// [1]: ../../../avr_hal_generic/port/index.html
                /// [2]: ../enum.Pin.html
                pub fn downgrade(self) -> $GenericPin<MODE> {
                    $GenericPin::$PortEnum($i, ::core::marker::PhantomData)
                }
            }
        )+
        // -------------------------------------------------------- }}}
    };
    // Without a generic pin
    (
        pub mod $portx:ident {
            #[port_ext]
            use $portext_use:path;

            impl $PortExt:ident for $PORTX:ty {
                regs: ($reg_pin:ident, $reg_ddr:ident, $reg_port:ident),
                $($pxi:ident: ($PXi:ident, $i:expr),)+
            }
        }
    ) => {
        pub mod $portx {
            use core::marker;
            use $crate::void::Void;
            use $crate::hal::digital::v2 as digital;
            use $crate::port::mode;

            // We have to "use" the port-ext trait so we can implement it inside
            // the macro.
            use $portext_use;

            pub struct Parts {
                pub ddr: DDR,
                $(
                    pub $pxi: $PXi<mode::Input<mode::Floating>>,
                )+
            }

            impl $PortExt for $PORTX {
                type Parts = Parts;

                fn split(self) -> Parts {
                    Parts {
                        ddr: DDR { _0: () },
                        $($pxi: $PXi { _mode: marker::PhantomData },)+
                    }
                }
            }

            /// Marker trait for types that can be used as DDR
            pub trait AsDDR {
                fn as_ddr(&self) -> &DDR;
            }

            pub struct DDR {
                _0: (),
            }

            impl AsDDR for DDR {
                fn as_ddr(&self) -> &DDR { self }
            }

            $(
                /// Type representing a specific pin.
                ///
                /// See the [general Digital IO documentation][1] for more info.  In short:
                ///
                /// - Mode is changed with
                ///   - `.into_output()`
                ///   - `.into_floating_input()`
                ///   - `.into_pull_up_input()`
                /// - Input pins are sampled with
                ///   - `.is_high().void_unwrap()`
                ///   - `.is_low().void_unwrap()`
                /// - Output pins are set with
                ///   - `.set_high().void_unwrap()`
                ///   - `.set_low().void_unwrap()`
                ///
                ///   and can be checked with
                ///   - `.is_set_high().void_unwrap()`
                ///   - `.is_set_low().void_unwrap()`
                /// - Pins can be downgraded into a generic type using `.downgrade()`.
                ///
                /// [1]: ../../../avr_hal_generic/port/index.html
                pub struct $PXi<MODE> {
                    pub(crate)_mode: marker::PhantomData<MODE>,
                }

                // Mode Switch implementations ---------------------------- {{{
                // - Unsafe:  The unsafe blocks in here are ok, because these
                //            operations will compile down to single, atomic
                //            `sbi`, `cbi`, `sbic`, `sbis` instructions.
                impl<MODE: mode::DigitalIO> $PXi<MODE> {
                    // The following methods are only defined if the pin is in
                    // a digital-io mode.  This ensures that a pin used by another
                    // peripheral can't be converted back (because that would
                    // not be universally possible).

                    /// Make this pin a digital output.
                    pub fn into_output<D: AsDDR>(self, ddr: &D) -> $PXi<mode::Output> {
                        unsafe {
                            (*<$PORTX>::ptr()).$reg_ddr.modify(|r, w| {
                                w.bits(r.bits() | (1 << $i))
                            });
                        }
                        $PXi { _mode: marker::PhantomData }
                    }

                    /// Make this pin a digital input **without** enabling the internal pull-up.
                    pub fn into_floating_input<D: AsDDR>(self, ddr: &D) -> $PXi<mode::Input<mode::Floating>> {
                        unsafe {
                            (*<$PORTX>::ptr()).$reg_ddr.modify(|r, w| {
                                w.bits(r.bits() & !(1 << $i))
                            });
                            (*<$PORTX>::ptr()).$reg_port.modify(|r, w| {
                                w.bits(r.bits() & !(1 << $i))
                            });
                        }
                        $PXi { _mode: marker::PhantomData }
                    }

                    /// Make this pin a digital input and enable the internal pull-up.
                    pub fn into_pull_up_input<D: AsDDR>(self, ddr: &D) -> $PXi<mode::Input<mode::PullUp>> {
                        unsafe {
                            (*<$PORTX>::ptr()).$reg_ddr.modify(|r, w| {
                                w.bits(r.bits() & !(1 << $i))
                            });
                            (*<$PORTX>::ptr()).$reg_port.modify(|r, w| {
                                w.bits(r.bits() | (1 << $i))
                            });
                        }
                        $PXi { _mode: marker::PhantomData }
                    }

                    /// Make this pin a tri-state pin. Default state is released (high) mode.
                    /// Internal pull-up is not used.
                    ///
                    /// Note that, as always, it is ***not safe*** to connect the external
                    /// pull-up to a voltage higher than VCC + 0.5.  See your chip's
                    /// datasheet for more details.
                    pub fn into_tri_state<D: AsDDR>(self, ddr: &D) -> $PXi<mode::TriState> {
                        unsafe {
                            (*<$PORTX>::ptr()).$reg_ddr.modify(|r, w| {
                                w.bits(r.bits() & !(1 << $i))
                            });
                            (*<$PORTX>::ptr()).$reg_port.modify(|r, w| {
                                w.bits(r.bits() & !(1 << $i))
                            });
                        }
                        $PXi { _mode: marker::PhantomData }
                    }
                }
                // -------------------------------------------------------- }}}

                // Input & Output implementations ------------------------- {{{
                // - Unsafe:  The unsafe blocks in here are ok, because these
                //            operations will compile down to single, atomic
                //            `sbi`, `cbi`, `sbic`, `sbis` instructions.
                impl digital::OutputPin for $PXi<mode::Output> {
                    type Error = Void;

                    fn set_high(&mut self) -> Result<(), Self::Error> {
                        unsafe {
                            (*<$PORTX>::ptr()).$reg_port.modify(|r, w| {
                                w.bits(r.bits() | (1 << $i))
                            });
                        }
                        Ok(())
                    }

                    fn set_low(&mut self) -> Result<(), Self::Error> {
                        unsafe {
                            (*<$PORTX>::ptr()).$reg_port.modify(|r, w| {
                                w.bits(r.bits() & !(1 << $i))
                            });
                        }
                        Ok(())
                    }
                }

                impl digital::StatefulOutputPin for $PXi<mode::Output> {
                    fn is_set_high(&self) -> Result<bool, Self::Error> {
                        Ok(unsafe {
                            (*<$PORTX>::ptr()).$reg_port.read().bits()
                        } & (1 << $i) != 0)
                    }

                    fn is_set_low(&self) -> Result<bool, Self::Error> {
                        self.is_set_high().map(|b| !b)
                    }
                }

                impl digital::ToggleableOutputPin for $PXi<mode::Output> {
                    type Error = Void;

                    fn toggle(&mut self) -> Result<(), Self::Error> {
                        unsafe {
                            (*<$PORTX>::ptr()).$reg_pin.write(|w| {
                                w.bits(1 << $i)
                            });
                        }
                        Ok(())
                    }
                }

                impl<MODE: mode::InputMode> digital::InputPin for $PXi<mode::Input<MODE>> {
                    type Error = Void;

                    fn is_high(&self) -> Result<bool, Self::Error> {
                        Ok(unsafe {
                            (*<$PORTX>::ptr()).$reg_pin.read().bits()
                        } & (1 << $i) != 0)
                    }

                    fn is_low(&self) -> Result<bool, Self::Error> {
                        Ok(unsafe {
                            (*<$PORTX>::ptr()).$reg_pin.read().bits()
                        } & (1 << $i) == 0)
                    }
                }

                impl digital::OutputPin for $PXi<mode::TriState> {
                    type Error = Void;

                    fn set_high(&mut self) -> Result<(), Self::Error> {
                        unsafe {
                            (*<$PORTX>::ptr()).$reg_ddr.modify(|r, w| {
                                w.bits(r.bits() & !(1 << $i))
                            });
                        }
                        Ok(())
                    }

                    fn set_low(&mut self) -> Result<(), Self::Error> {
                        unsafe {
                            (*<$PORTX>::ptr()).$reg_ddr.modify(|r, w| {
                                w.bits(r.bits() | (1 << $i))
                            });
                        }
                        Ok(())
                    }
                }

                impl digital::InputPin for $PXi<mode::TriState> {
                    type Error = Void;

                    fn is_high(&self) -> Result<bool, Self::Error> {
                        Ok(unsafe {
                            (*<$PORTX>::ptr()).$reg_pin.read().bits()
                        } & (1 << $i) != 0)
                    }

                    fn is_low(&self) -> Result<bool, Self::Error> {
                        Ok(unsafe {
                            (*<$PORTX>::ptr()).$reg_pin.read().bits()
                        } & (1 << $i) == 0)
                    }
                }

                // -------------------------------------------------------- }}}
            )+
        }
    };
}

/// Create a pin reexport struct for convenient access
#[macro_export]
macro_rules! impl_board_pins {
    (
        #[port_defs]
        use $portpath:path;

        $(#[$ddr_attr:meta])*
        pub struct $DDR:ident {
            $($portx:ident: $PORTX:ty,)+
        }

        $(#[$pins_attr:meta])*
        pub struct $Pins:ident {
            $(
                $(#[$pin_attr:meta])*
                pub $name:ident: $pinport:ident::$pin:ident::$Pin:ident,
            )+
        }
    ) => {
        use $portpath::{$($portx),+};

        $(#[$ddr_attr])*
        pub struct $DDR {
            $($portx: $portx::DDR,)+
        }

        $(
            impl $portx::AsDDR for $DDR {
                fn as_ddr(&self) -> &$portx::DDR {
                    &self.$portx
                }
            }
        )+

        $(#[$pins_attr])*
        pub struct $Pins {
            pub ddr: $DDR,
            $(
                $(#[$pin_attr])*
                pub $name: $pinport::$Pin<
                    $crate::port::mode::Input<$crate::port::mode::Floating>
                >,
            )+
        }

        impl $Pins {
            pub fn new($($portx: $PORTX),+) -> $Pins {
                $(let $portx = $portx.split();)+

                $Pins {
                    ddr: $DDR {
                        $($portx: $portx.ddr,)+
                    },
                    $($name: $pinport.$pin,)+
                }
            }
        }
    };
}