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
//! Digital IO implementations for the `PORT#` peripherals
//!
//! Please take a look at the documentation for [`Pin`] for a detailed explanation.

use core::marker::PhantomData;
use embedded_hal::digital::v2::{InputPin, OutputPin};

pub trait PinMode: crate::Sealed {}
/// GPIO pin modes
pub mod mode {
    use core::marker::PhantomData;

    pub trait Io: crate::Sealed + super::PinMode {}

    /// Pin is configured as a digital output.
    pub struct Output;
    impl super::PinMode for Output {}
    impl Io for Output {}
    impl crate::Sealed for Output {}

    /// Pin is configured as a digital output with open drain behaviour
    pub struct OpenDrain;
    impl super::PinMode for OpenDrain {}
    impl Io for OpenDrain {}
    impl crate::Sealed for OpenDrain {}

    pub struct PwmOutput<TC> {
        pub(crate) _timer: PhantomData<TC>,
    }
    impl<TC> super::PinMode for PwmOutput<TC> {}
    impl<TC> crate::Sealed for PwmOutput<TC> {}

    pub trait InputMode: crate::Sealed {}

    /// Pin is configured as digital input (floating or pulled-up).
    pub struct Input<IMODE = AnyInput> {
        pub(crate) _imode: PhantomData<IMODE>,
    }
    impl<IMODE: InputMode> super::PinMode for Input<IMODE> {}
    impl<IMODE: InputMode> Io for Input<IMODE> {}
    impl<IMODE: InputMode> crate::Sealed for Input<IMODE> {}

    /// Floating input, used like `Input<Floating>`.
    pub struct Floating;
    impl InputMode for Floating {}
    impl crate::Sealed for Floating {}

    /// Pulled-up input, used like `Input<PullUp>`.
    pub struct PullUp;
    impl InputMode for PullUp {}
    impl crate::Sealed for PullUp {}

    /// Any input (floating or pulled-up), used like `Input<AnyInput>`.
    pub struct AnyInput;
    impl InputMode for AnyInput {}
    impl crate::Sealed for AnyInput {}

    /// Pin is configured as an analog input (for the ADC).
    pub struct Analog;
}

pub trait PinOps {
    type Dynamic;

    fn into_dynamic(self) -> Self::Dynamic;

    unsafe fn out_set(&mut self);
    unsafe fn out_clear(&mut self);
    unsafe fn out_toggle(&mut self);
    unsafe fn out_get(&self) -> bool;

    unsafe fn in_get(&self) -> bool;

    unsafe fn make_output(&mut self);
    unsafe fn make_input(&mut self, pull_up: bool);
}

/// Representation of an MCU pin.
///
/// # Design Rationale
/// We want individual types per pin to model constraints which depend on a specific pin.  For
/// example, some peripherals are internally hard-wired to certain pins of the MCU.
///
/// Additionally, the mode of a pin should also be a part of the type to model enforcement of pins
/// being in a certain mode and preventing misuse like for example calling `set_high()` on a pin
/// configured as input.
///
/// To do this, the [`Pin`] type is generic over the `MODE` (input, output, ...) and the `PIN`
/// (pd0, pb5, pc6, ...).
///
/// Of course, in some applications one does not care about the specific pin used.  For these
/// situations, the specific pin types can be "downgraded" into a dynamic type that can represent
/// any pin.  See [Downgrading](#downgrading) for more details.
///
/// # Instantiation
/// The `Peripherals` struct in HAL and board-support crates usually contains a `.pins` field which
/// is of type `Pins`.  This `Pins` struct in turn has fields for each individual pin, in its
/// default mode.  You can then move the pin out of this struct to reconfigure it (examples in this
/// documentation are for `atmega-hal`):
///
/// ```ignore
/// use atmega_hal::port::{Pin, mode, self};
///
/// let dp = atmega_hal::Peripherals::take().unwrap();
/// let pins = atmega_hal::pins!(dp);
///
/// let output: Pin<mode::Output, port::PD3> = pins.pd3.into_output();
/// ```
pub struct Pin<MODE, PIN> {
    pub(crate) pin: PIN,
    pub(crate) _mode: PhantomData<MODE>,
}

impl<PIN: PinOps> Pin<mode::Input<mode::Floating>, PIN> {
    #[doc(hidden)]
    pub fn new(pin: PIN) -> Self {
        Pin {
            pin,
            _mode: PhantomData,
        }
    }
}

/// # Configuration
/// To change the mode of a pin, use one of the following conversion functions.  They consume the
/// original [`Pin`] and return one with the desired mode.  Only when a pin is in the correct mode,
/// does it have the mode-relevant methods availailable (e.g. `set_high()` is only available for
/// `Output` pins).
impl<PIN: PinOps, MODE: mode::Io> Pin<MODE, PIN> {
    /// Convert this pin into an output pin, setting the state to low.
    /// See [Digital Output](#digital-output).
    pub fn into_output(mut self) -> Pin<mode::Output, PIN> {
        unsafe { self.pin.out_clear() };
        unsafe { self.pin.make_output() };
        Pin {
            pin: self.pin,
            _mode: PhantomData,
        }
    }

    /// Convert this pin into an output pin, setting the state to high.
    /// See [Digital Output](#digital-output).
    pub fn into_output_high(mut self) -> Pin<mode::Output, PIN> {
        unsafe { self.pin.out_set() };
        unsafe { self.pin.make_output() };
        Pin {
            pin: self.pin,
            _mode: PhantomData,
        }
    }

    /// Convert this pin into an open-drain output pin, setting the state to low.
    /// See [Digital Output Open Drain](#digital-output-open-drain)
    pub fn into_opendrain(mut self) -> Pin<mode::OpenDrain, PIN> {
        unsafe { self.pin.out_clear() };
        unsafe { self.pin.make_output() };
        Pin {
            pin: self.pin,
            _mode: PhantomData,
        }
    }

    /// Convert this pin into an open-drain output pin, setting the state to high.
    /// See [Digital Output Open Drain](#digital-output-open-drain)
    pub fn into_opendrain_high(mut self) -> Pin<mode::OpenDrain, PIN> {
        unsafe { self.pin.make_input(false) };
        Pin {
            pin: self.pin,
            _mode: PhantomData,
        }
    }

    /// Convert this pin into a floating input pin.  See [Digital Input](#digital-input).
    ///
    /// *Note*: To read deterministic values from the pin, it must be externally pulled to a
    /// defined level (either VCC or GND).
    pub fn into_floating_input(mut self) -> Pin<mode::Input<mode::Floating>, PIN> {
        unsafe { self.pin.make_input(false) };
        Pin {
            pin: self.pin,
            _mode: PhantomData,
        }
    }

    /// Convert this pin into a pulled-up input pin.  See [Digital Input](#digital-input).
    ///
    /// With no external circuit pulling the pin low, it will be read high.
    pub fn into_pull_up_input(mut self) -> Pin<mode::Input<mode::PullUp>, PIN> {
        unsafe { self.pin.make_input(true) };
        Pin {
            pin: self.pin,
            _mode: PhantomData,
        }
    }

    /// Convert this pin into an analog input (ADC channel).  See [Analog Input](#analog-input).
    ///
    /// Some pins can be repurposed as ADC channels.  For those pins, the `into_analog_input()`
    /// method is available.
    pub fn into_analog_input<H, ADC, CLOCK>(
        self,
        adc: &mut crate::adc::Adc<H, ADC, CLOCK>,
    ) -> Pin<mode::Analog, PIN>
    where
        Pin<mode::Analog, PIN>: crate::adc::AdcChannel<H, ADC>,
        ADC: crate::adc::AdcOps<H>,
        CLOCK: crate::clock::Clock,
    {
        let new = Pin {
            pin: self.pin,
            _mode: PhantomData,
        };
        adc.enable_pin(&new);
        new
    }
}

/// # Downgrading
/// For applications where the exact pin is irrelevant, a specific pin can be downgraded to a
/// "dynamic pin" which can represent any pin:
///
/// ```ignore
/// use atmega_hal::port::{Pin, mode};
///
/// let dp = atmega_hal::Peripherals::take().unwrap();
/// let pins = atmega_hal::pins!(dp);
///
/// let any_output_pin1: Pin<mode::Output> = pins.pd0.into_output().downgrade();
/// let any_output_pin2: Pin<mode::Output> = pins.pd1.into_output().downgrade();
///
/// // Because they now have the same type, you can, for example, stuff them into an array:
/// let pins: [Pin<mode::Output>; 2] = [any_output_pin1, any_output_pin2];
/// ```
impl<PIN: PinOps, MODE: mode::Io> Pin<MODE, PIN> {
    /// "Erase" type-level information about which specific pin is represented.
    ///
    /// *Note*: The returned "dynamic" pin has runtime overhead compared to a specific pin.
    pub fn downgrade(self) -> Pin<MODE, PIN::Dynamic> {
        Pin {
            pin: self.pin.into_dynamic(),
            _mode: PhantomData,
        }
    }
}

/// # Input-Mode Downgrading
/// There is a second kind of downgrading: In some cases it is not important whether an input pin
/// is configured as [`mode::PullUp`] or [`mode::Floating`].  For this, you can "forget" the
/// concrete input mode, leaving you with a type that is the same for pull-up or floating inputs:
///
/// ```ignore
/// use atmega_hal::port::{Pin, mode};
///
/// let dp = atmega_hal::Peripherals::take().unwrap();
/// let pins = atmega_hal::pins!(dp);
///
/// // This demo uses downgraded pins, but it works just as well
/// // with non-downgraded ones!
/// let input_pin1: Pin<mode::Input<mode::Floating>> = pins.pd0
///     .into_floating_input()
///     .downgrade();
/// let input_pin2: Pin<mode::Input<mode::Floating>> = pins.pd1
///     .into_pull_up_input()
///     .downgrade();
///
/// // With the input mode "forgotten", they have the same type now,
/// // even if electically different.
/// let any_inputs: [Pin<mode::Input>; 2] = [
///     input_pin1.forget_imode(),
///     input_pin2.forget_imode(),
/// ];
/// ```
impl<PIN: PinOps, IMODE> Pin<mode::Input<IMODE>, PIN> {
    /// "Erase" type-level information about whether the pin is currently a pull-up or a floating
    /// input.
    pub fn forget_imode(self) -> Pin<mode::Input, PIN> {
        Pin {
            pin: self.pin,
            _mode: PhantomData,
        }
    }
}

/// # Digital Output
impl<PIN: PinOps> Pin<mode::Output, PIN> {
    /// Set pin high (pull it to supply voltage).
    #[inline]
    pub fn set_high(&mut self) {
        unsafe { self.pin.out_set() }
    }

    /// Set pin low (pull it to GND).
    #[inline]
    pub fn set_low(&mut self) {
        unsafe { self.pin.out_clear() }
    }

    /// Toggle a high pin to low and a low pin to high.
    #[inline]
    pub fn toggle(&mut self) {
        unsafe { self.pin.out_toggle() }
    }

    /// Check whether the pin is set high.
    ///
    /// *Note*: The electrical state of the pin might differ due to external circuitry.
    #[inline]
    pub fn is_set_high(&self) -> bool {
        unsafe { self.pin.out_get() }
    }

    /// Check whether the pin is set low.
    ///
    /// *Note*: The electrical state of the pin might differ due to external circuitry.
    #[inline]
    pub fn is_set_low(&self) -> bool {
        !unsafe { self.pin.out_get() }
    }
}

// Implements OutputPin from embedded-hal to make sure external libraries work
impl<PIN: PinOps> OutputPin for Pin<mode::Output, PIN> {
    type Error = core::convert::Infallible;

    fn set_high(&mut self) -> Result<(), Self::Error> {
        self.set_high();
        Ok(())
    }

    fn set_low(&mut self) -> Result<(), Self::Error> {
        self.set_low();
        Ok(())
    }
}

/// # Digital Output Open Drain
impl<PIN: PinOps> Pin<mode::OpenDrain, PIN> {
    /// Set the pin high (Input without PullUp so it is floating)
    #[inline]
    pub fn set_high(&mut self) {
        unsafe { self.pin.make_input(false) }
    }

    /// Set pin low (pull it to GND, Output to low).
    #[inline]
    pub fn set_low(&mut self) {
        unsafe { self.pin.make_output() }
    }

    /// Check whether the pin is set high.
    ///
    /// *Note*: The electrical state of the pin might differ due to external circuitry.
    #[inline]
    pub fn is_high(&self) -> bool {
        unsafe { self.pin.in_get() }
    }

    /// Check whether the pin is set low.
    ///
    /// *Note*: The electrical state of the pin might differ due to external circuitry.
    #[inline]
    pub fn is_low(&self) -> bool {
        !self.is_high()
    }
}

// Implements OutputPin from embedded-hal to make sure external libraries work
impl<PIN: PinOps> OutputPin for Pin<mode::OpenDrain, PIN> {
    type Error = core::convert::Infallible;

    fn set_high(&mut self) -> Result<(), Self::Error> {
        self.set_high();
        Ok(())
    }

    fn set_low(&mut self) -> Result<(), Self::Error> {
        self.set_low();
        Ok(())
    }
}

// Implements InputPin from embedded-hal to make sure external libraries work
impl<PIN: PinOps> InputPin for Pin<mode::OpenDrain, PIN> {
    type Error = core::convert::Infallible;

    fn is_high(&self) -> Result<bool, Self::Error> {
        Ok(self.is_high())
    }

    fn is_low(&self) -> Result<bool, Self::Error> {
        Ok(self.is_low())
    }
}

// Implements InputPin from embedded-hal to make sure external libraries work
impl<PIN: PinOps, IMODE: mode::InputMode> InputPin for Pin<mode::Input<IMODE>, PIN> {
    type Error = core::convert::Infallible;

    fn is_high(&self) -> Result<bool, Self::Error> {
        Ok(self.is_high())
    }

    fn is_low(&self) -> Result<bool, Self::Error> {
        Ok(self.is_low())
    }
}

/// # Digital Input
impl<PIN: PinOps, IMODE: mode::InputMode> Pin<mode::Input<IMODE>, PIN> {
    /// Check whether the pin is driven high.
    #[inline]
    pub fn is_high(&self) -> bool {
        unsafe { self.pin.in_get() }
    }

    /// Check whether the pin is driven low.
    #[inline]
    pub fn is_low(&self) -> bool {
        !unsafe { self.pin.in_get() }
    }
}

/// # Analog Input
///
/// Some pins can be configured as ADC channels.  For those pins, `analog_read()` can be used to
/// read the voltage.  `analog_read()` corresponds to a blocking ADC read:
///
/// ```
/// let dp = atmega_hal::Peripherals::take().unwrap();
/// let pins = atmega_hal::pins!(dp);
/// let mut adc = atmega_hal::Adc::new(dp.ADC, Default::default());
///
/// let a0 = dp.pc0.into_analog_input(&mut adc);
///
/// let voltage = a0.analog_read(&mut adc);
/// // ^- this is equivalent to -v
/// let voltage = adc.read_blocking(&a0);
/// ```
impl<PIN: PinOps> Pin<mode::Analog, PIN> {
    pub fn analog_read<H, ADC, CLOCK>(&self, adc: &mut crate::adc::Adc<H, ADC, CLOCK>) -> u16
    where
        Pin<mode::Analog, PIN>: crate::adc::AdcChannel<H, ADC>,
        ADC: crate::adc::AdcOps<H>,
        CLOCK: crate::clock::Clock,
    {
        adc.read_blocking(self)
    }

    /// Convert this pin into a generic [`Channel`][adc-channel] type.
    ///
    /// The generic channel type can be used to store multiple channels in an array.
    ///
    /// [adc-channel]: crate::adc::Channel
    pub fn into_channel<H, ADC>(self) -> crate::adc::Channel<H, ADC>
    where
        Pin<mode::Analog, PIN>: crate::adc::AdcChannel<H, ADC>,
        ADC: crate::adc::AdcOps<H>,
    {
        crate::adc::Channel::new(self)
    }
}

#[macro_export]
macro_rules! impl_port_traditional {
    (
        enum Ports {
            $($PortName:ident: ($Port:ty, $port_port_reg:ident, $port_pin_reg:ident, $port_ddr_reg:ident),)+
        }

        $(#[$pins_attr:meta])*
        pub struct Pins {
            $($pin:ident: $Pin:ident = ($PinPort:ty, $PinPortName:ident, $pin_num:expr,
                                        $pin_port_reg:ident, $pin_pin_reg:ident,
                                        $pin_ddr_reg:ident),)+
        }
    ) => {
        pub use $crate::port::mode;

        /// Type-alias for a pin type which can represent any concrete pin.
        ///
        /// Sometimes it is easier to handle pins if they are all of the same type.  By default,
        /// each pin gets its own distinct type in `avr-hal`, but by
        /// [downgrading][avr_hal_generic::port::Pin#downgrading], you can cast them into this
        /// "dynamic" type.  Do note, however, that using this dynamic type has a runtime cost.
        pub type Pin<MODE, PIN = Dynamic> = $crate::port::Pin<MODE, PIN>;

        $(#[$pins_attr])*
        pub struct Pins {
            $(pub $pin: Pin<
                mode::Input<mode::Floating>,
                $Pin,
            >,)+
        }

        impl Pins {
            pub fn new(
                $(_: $Port,)+
            ) -> Self {
                Self {
                    $($pin: $crate::port::Pin::new(
                        $Pin { _private: (), }
                    ),)+
                }
            }
        }

        #[repr(u8)]
        pub enum DynamicPort {
            $($PortName,)+
        }

        pub struct Dynamic {
            port: DynamicPort,
            // We'll store the mask instead of the pin number because this allows much less code to
            // be generated for the trait method implementations.
            mask: u8,
        }

        impl Dynamic {
            fn new(port: DynamicPort, pin_num: u8) -> Self {
                Self {
                    port,
                    mask: 1 << pin_num,
                }
            }
        }

        impl $crate::port::PinOps for Dynamic {
            type Dynamic = Self;

            #[inline]
            fn into_dynamic(self) -> Self::Dynamic {
                self
            }

            #[inline]
            unsafe fn out_set(&mut self) {
                match self.port {
                    $(DynamicPort::$PortName => (*<$Port>::ptr()).$port_port_reg.modify(|r, w| {
                        w.bits(r.bits() | self.mask)
                    }),)+
                }
            }

            #[inline]
            unsafe fn out_clear(&mut self) {
                match self.port {
                    $(DynamicPort::$PortName => (*<$Port>::ptr()).$port_port_reg.modify(|r, w| {
                        w.bits(r.bits() & !self.mask)
                    }),)+
                }
            }

            #[inline]
            unsafe fn out_toggle(&mut self) {
                match self.port {
                    $(DynamicPort::$PortName => (*<$Port>::ptr()).$port_pin_reg.write(|w| {
                        w.bits(self.mask)
                    }),)+
                }
            }

            #[inline]
            unsafe fn out_get(&self) -> bool {
                match self.port {
                    $(DynamicPort::$PortName => (*<$Port>::ptr()).$port_port_reg.read().bits()
                        & self.mask != 0,)+
                }
            }

            #[inline]
            unsafe fn in_get(&self) -> bool {
                match self.port {
                    $(DynamicPort::$PortName => (*<$Port>::ptr()).$port_pin_reg.read().bits()
                        & self.mask != 0,)+
                }
            }

            #[inline]
            unsafe fn make_output(&mut self) {
                match self.port {
                    $(DynamicPort::$PortName => (*<$Port>::ptr()).$port_ddr_reg.modify(|r, w| {
                        w.bits(r.bits() | self.mask)
                    }),)+
                }
            }

            #[inline]
            unsafe fn make_input(&mut self, pull_up: bool) {
                match self.port {
                    $(DynamicPort::$PortName => (*<$Port>::ptr()).$port_ddr_reg.modify(|r, w| {
                        w.bits(r.bits() & !self.mask)
                    }),)+
                }
                if pull_up {
                    self.out_clear()
                } else {
                    self.out_set()
                }
            }
        }

        $(
            pub struct $Pin {
                _private: ()
            }

            impl $crate::port::PinOps for $Pin {
                type Dynamic = Dynamic;

                #[inline]
                fn into_dynamic(self) -> Self::Dynamic {
                    Dynamic::new(DynamicPort::$PinPortName, $pin_num)
                }

                #[inline]
                unsafe fn out_set(&mut self) {
                    (*<$PinPort>::ptr()).$pin_port_reg.modify(|r, w| {
                        w.bits(r.bits() | (1 << $pin_num))
                    })
                }

                #[inline]
                unsafe fn out_clear(&mut self) {
                    (*<$PinPort>::ptr()).$pin_port_reg.modify(|r, w| {
                        w.bits(r.bits() & !(1 << $pin_num))
                    })
                }

                #[inline]
                unsafe fn out_toggle(&mut self) {
                    (*<$PinPort>::ptr()).$pin_pin_reg.write(|w| w.bits(1 << $pin_num))
                }

                #[inline]
                unsafe fn out_get(&self) -> bool {
                    (*<$PinPort>::ptr()).$pin_port_reg.read().bits() & (1 << $pin_num) != 0
                }

                #[inline]
                unsafe fn in_get(&self) -> bool {
                    (*<$PinPort>::ptr()).$pin_pin_reg.read().bits() & (1 << $pin_num) != 0
                }

                #[inline]
                unsafe fn make_output(&mut self) {
                    (*<$PinPort>::ptr()).$pin_ddr_reg.modify(|r, w| {
                        w.bits(r.bits() | (1 << $pin_num))
                    })
                }

                #[inline]
                unsafe fn make_input(&mut self, pull_up: bool) {
                    (*<$PinPort>::ptr()).$pin_ddr_reg.modify(|r, w| {
                        w.bits(r.bits() & !(1 << $pin_num))
                    });
                    if pull_up {
                        self.out_set()
                    } else {
                        self.out_clear()
                    }
                }
            }
        )+
    };
}

#[macro_export]
macro_rules! renamed_pins {
    (
        type Pin = $PinType:ident;

        $(#[$pins_attr:meta])*
        pub struct Pins from $McuPins:ty {
            $($(#[$pin_attr:meta])* pub $pin:ident: $Pin:ty = $pin_orig:ident,)+
        }
    ) => {
        $(#[$pins_attr])*
        pub struct Pins {
            $($(#[$pin_attr])*
            pub $pin: $PinType<
                $crate::port::mode::Input<$crate::port::mode::Floating>,
                $Pin,
            >,)+
        }

        impl Pins {
            pub fn with_mcu_pins(pins: $McuPins) -> Self {
                Self {
                    $($pin: pins.$pin_orig,)+
                }
            }
        }
    };
}