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
//! Version 1 of the SERCOM pads module
//!
//! This module is a compatibility shim that allows existing code to use the new
//! [`sercom::v2`](crate::sercom::v2) module. This API will eventually be
//! deprecated and removed.
//!
//! To recreate the `v1` API with `v2` types, this module defines its own
//! [`Pad`] type, which is just a wrapper around a [`Pin`] configured as a
//! SERCOM pad. The `SercomXPadY` types of the original, `v1` API are recreated
//! as type aliases of the form
//!
//! ```
//! type SercomXPadY<Z> = Pad<SercomX, PadY, Z>
//! ```
//!
//! Use the [`PadPin`] trait to construct `Pad`s. The corresponding `Pin` can be
//! recovered using the [`free`] method.
//!
//! ```
//! use atsamd_hal::pac::Peripherals;
//! use atsamd_hal::gpio::v1::GpioExt;
//! use atsamd_hal::sercom::v1::{PadPin, Sercom0Pad0};
//!
//! let peripherals = Peripherals::take().unwrap();
//! let mut parts = peripherals.PORT.split();
//! let pad: Sercom0Pad0<_> = parts.pa8.into_pad(&mut parts.port);
//! let pin = pad.free();
//! ```
//!
//! [`free`]: Pad::free

#![allow(deprecated)]

use core::marker::PhantomData;

use paste::paste;

use crate::gpio::v1::{self, IntoFunction, Pin, Port};
use crate::gpio::v2::{self, AnyPin, PinId, PinMode};
use crate::sercom::v2::*;
use crate::typelevel::Sealed;

//==============================================================================
// IsPad
//==============================================================================

/// Extend implementations of [`IsPad`] from [`v2::Pin`]s to [`v1::Pin`]s
impl<I, M> IsPad for v1::Pin<I, M>
where
    I: PinId,
    M: PinMode,
    v1::Pin<I, M>: AnyPin,
    v2::Pin<I, M>: IsPad,
{
    type Sercom = <v2::Pin<I, M> as IsPad>::Sercom;
    type PadNum = <v2::Pin<I, M> as IsPad>::PadNum;
}

//==============================================================================
// Pad
//==============================================================================

/// A GPIO [`Pin`] configured to act as a SERCOM [`Pad`]
///
/// This type is just a wrapper around a correctly-configured [`Pin`]. The
/// `SercomXPadY` types of the original, `v1` API are recreated as type aliases
/// of the form
///
/// ```
/// type SercomXPadY<Z> = Pad<SercomX, PadY, Z>
/// ```
pub struct Pad<S, N, P>
where
    S: Sercom,
    N: PadNum,
    P: IsPad<Sercom = S, PadNum = N>,
{
    sercom: PhantomData<S>,
    padnum: PhantomData<N>,
    pin: P,
}

impl<S, N, P> Pad<S, N, P>
where
    S: Sercom,
    N: PadNum,
    P: IsPad<Sercom = S, PadNum = N>,
{
    /// Consume the [`Pad`] and recover the corresponding [`Pin`]
    #[inline]
    pub fn free(self) -> P {
        self.pin
    }
}

impl<S, N, P> Sealed for Pad<S, N, P>
where
    S: Sercom,
    N: PadNum,
    P: IsPad<Sercom = S, PadNum = N>,
{
}

//==============================================================================
// Pad aliases
//==============================================================================

macro_rules! pad_alias {
    ( $($Sercom:ty),+ ) => {
        $(
            pad_alias!($Sercom: Pad0);
            pad_alias!($Sercom: Pad1);
            pad_alias!($Sercom: Pad2);
            pad_alias!($Sercom: Pad3);
        )+
    };
    ($Sercom:ty: $Pad:ty) => {
        paste! {
            /// Represents a numbered pad for the associated sercom instance. The pad is
            /// generic over any pin, only the PadPin implementations in this the sercom
            /// module make sense.
            pub type [<$Sercom $Pad>]<P> = Pad<$Sercom, $Pad, P>;
        }
    }
}

pad_alias!(Sercom0, Sercom1);
#[cfg(any(feature = "samd21", feature = "min-samd51g"))]
pad_alias!(Sercom2, Sercom3);
#[cfg(any(feature = "min-samd21g", feature = "min-samd51g"))]
pad_alias!(Sercom4, Sercom5);
#[cfg(feature = "min-samd51n")]
pad_alias!(Sercom6, Sercom7);

//==============================================================================
// CompatiblePad
//==============================================================================

/// Type class to improve compatibility between `v1` and `v2` SERCOM pad types
///
/// The `sercom::v1::pads` module uses a wrapper [`Pad`] type to represent
/// SERCOM pads. The `v2::pad` module, on the other hand, does not use a
/// wrapper. Instead, it labels each correctly-configured [`v2::Pin`] with the
/// [`IsPad`] trait.
///
/// This trait forms a [type class] over both. It allows the [`v1::uart`],
/// [`v1::spi`] and [`v1::i2c`] modules to accept both `v1` and `v2` pad types.
///
/// [`v1::uart`]: super::uart
/// [`v1::spi`]: super::spi
/// [`v1::i2c`]: super::i2c
/// [type class]: crate::typelevel#type-classes
pub trait CompatiblePad: Sealed {
    type Sercom: Sercom;
    type PadNum: PadNum;
}

impl<S, N, P> CompatiblePad for Pad<S, N, P>
where
    S: Sercom,
    N: PadNum,
    P: IsPad<Sercom = S, PadNum = N>,
{
    type Sercom = S;
    type PadNum = N;
}

impl<P: IsPad> CompatiblePad for P {
    type Sercom = P::Sercom;
    type PadNum = P::PadNum;
}

//==============================================================================
// PadPin
//==============================================================================

/// The PadPin trait makes it more ergonomic to convert a pin into a Sercom pad.
/// You should not implement this trait for yourself; only the implementations
/// in the sercom module make sense.
pub trait PadPin<P>: Sealed {
    fn into_pad(self, port: &mut Port) -> P;
}

#[cfg(feature = "samd11")]
impl<S, N, I, M> PadPin<Pad<S, N, Pin<I, I::PinMode>>> for Pin<I, M>
where
    S: Sercom,
    N: PadNum,
    I: GetPad<S, N>,
    M: PinMode,
    Pin<I, M>: IntoFunction<Pin<I, I::PinMode>>,
    Pin<I, I::PinMode>: IsPad<Sercom = S, PadNum = N>,
{
    #[inline]
    fn into_pad(self, port: &mut Port) -> Pad<S, N, Pin<I, I::PinMode>> {
        let pin = self.into_function(port);
        Pad {
            sercom: PhantomData,
            padnum: PhantomData,
            pin,
        }
    }
}

#[cfg(not(feature = "samd11"))]
impl<S, N, I, M> PadPin<Pad<S, N, Pin<I, I::PinMode>>> for Pin<I, M>
where
    S: Sercom,
    N: PadNum,
    I: GetPad<S>,
    M: PinMode,
    Pin<I, M>: IntoFunction<Pin<I, I::PinMode>>,
    Pin<I, I::PinMode>: IsPad<Sercom = S, PadNum = N>,
{
    #[inline]
    fn into_pad(self, port: &mut Port) -> Pad<S, N, Pin<I, I::PinMode>> {
        let pin = self.into_function(port);
        Pad {
            sercom: PhantomData,
            padnum: PhantomData,
            pin,
        }
    }
}