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
//! Define a container for a set of SERCOM pads
//!
//! See the [i2c module](super) documentation for more details on declaring and
//! instantiating a [`Pads`] type.
use crate::{gpio::AnyPin, sercom::*, typelevel::Sealed};
use core::marker::PhantomData;
/// Container for a set of SERCOM [`Pad`]s
///
/// See the [module-level](crate::sercom::i2c) documentation for more
/// details on specifying a `Pads` type and creating instances.
pub struct Pads<S, I, SDA, SCL>
where
S: Sercom,
I: IoSet,
SDA: IsI2cPad<PadNum = Pad0, Sercom = S> + InIoSet<I>,
SCL: IsI2cPad<PadNum = Pad1, Sercom = S> + InIoSet<I>,
{
sercom: PhantomData<S>,
ioset: PhantomData<I>,
sda: SDA,
scl: SCL,
}
impl<S, I, DI, CI> PadsFromIds<S, I, DI, CI>
where
S: Sercom,
I: IoSet,
DI: GetPad<S>,
CI: GetPad<S>,
Pad<S, DI>: IsI2cPad<Sercom = S, PadNum = Pad0> + InIoSet<I>,
Pad<S, CI>: IsI2cPad<Sercom = S, PadNum = Pad1> + InIoSet<I>,
{
pub fn new(sda: impl AnyPin<Id = DI>, scl: impl AnyPin<Id = CI>) -> Self {
Self {
sercom: PhantomData,
ioset: PhantomData,
sda: sda.into().into_mode(),
scl: scl.into().into_mode(),
}
}
}
impl<S, I, SDA, SCL> Pads<S, I, SDA, SCL>
where
S: Sercom,
I: IoSet,
SDA: IsI2cPad<PadNum = Pad0, Sercom = S> + InIoSet<I>,
SCL: IsI2cPad<PadNum = Pad1, Sercom = S> + InIoSet<I>,
{
/// Consume the [`Pads`] and return each individual
/// [`Pin`](crate::gpio::Pin)
#[inline]
pub fn free(self) -> (SDA, SCL) {
(self.sda, self.scl)
}
}
//=============================================================================
// PadsFromIds
//=============================================================================
/// Define a set of [`Pads`] using [`PinId`]s instead of [`Pin`]s
///
/// In some cases, it is more convenient to specify a set of `Pads` using
/// `PinId`s rather than `Pin`s. This alias makes it easier to do so.
///
/// The first two parameters are the [`Sercom`] and [`IoSet`], while the
/// remaining two are [`PinId`]s representing the corresponding type
/// parameters of [`Pads`], i.e. `SDA` & `SCL`.
///
/// ```
/// use atsamd_hal::pac::Peripherals;
/// use atsamd_hal::gpio::{PA08, PA09, Pins};
/// use atsamd_hal::sercom::{Sercom0, IoSet1, i2c};
/// use atsamd_hal::typelevel::NoneT;
///
/// pub type Pads = i2c::PadsFromIds<Sercom0, IoSet1, PA08, PA09>;
///
/// pub fn create_pads() -> Pads {
/// let peripherals = Peripherals::take().unwrap();
/// let pins = Pins::new(peripherals.PORT);
/// i2c::Pads::default().sda(pins.pa08).scl(pins.pa09)
/// }
/// ```
///
/// [`Pin`]: crate::gpio::Pin
/// [`PinId`]: crate::gpio::PinId
pub type PadsFromIds<S, I, SDA, SCL> = Pads<S, I, Pad<S, SDA>, Pad<S, SCL>>;
//=============================================================================
// PadSet
//=============================================================================
/// Type-level function to recover the [`Pad`] types from a generic set
/// of [`Pads`]
///
/// This trait is used as an interface between the [`Pads`] type and other
/// types in this module. It acts as a [type-level function], returning the
/// corresponding [`Sercom`] and [`OptionalPad`] types. It serves to cut down on
/// the total number of type parameters needed in the [`Config`] struct. The
/// `Config` struct doesn't need access to the [`Pin`]s directly. Rather, it
/// only needs to apply the [`SomePad`] trait bound when a `Pin` is required.
/// The `PadSet` trait allows each `Config` struct to store an instance of
/// `Pads` without itself being generic over all six type parameters of the
/// `Pads` type.
///
/// This trait is a simplified version of the [`AnyKind`] trait pattern.
///
/// [`Pin`]: crate::gpio::Pin
/// [`Config`]: super::Config
/// [type-level function]: crate::typelevel#type-level-functions
/// [`AnyKind`]: crate::typelevel#anykind-trait-pattern
pub trait PadSet: Sealed {
type Sercom: Sercom;
type IoSet: IoSet;
type Sda: IsI2cPad<PadNum = Pad0, Sercom = Self::Sercom> + InIoSet<Self::IoSet>;
type Scl: IsI2cPad<PadNum = Pad1, Sercom = Self::Sercom> + InIoSet<Self::IoSet>;
}
impl<S, I, SDA, SCL> Sealed for Pads<S, I, SDA, SCL>
where
S: Sercom,
I: IoSet,
SDA: IsI2cPad<PadNum = Pad0, Sercom = S> + InIoSet<I>,
SCL: IsI2cPad<PadNum = Pad1, Sercom = S> + InIoSet<I>,
{
}
impl<S, I, SDA, SCL> PadSet for Pads<S, I, SDA, SCL>
where
S: Sercom,
I: IoSet,
SDA: IsI2cPad<PadNum = Pad0, Sercom = S> + InIoSet<I>,
SCL: IsI2cPad<PadNum = Pad1, Sercom = S> + InIoSet<I>,
{
type Sercom = S;
type IoSet = I;
type Sda = SDA;
type Scl = SCL;
}