sam3_hal/pio/
mod.rs

1//! Pin and configuration definitions and peripheral mappings
2//!
3//! Relevant manual pages:
4//! - SAM3A, SAM3X: [manual][ax], pages 618-675
5//! - SAM3N: [manual][n] pages 376-437
6//! - SAM3S1, SAM3S2, SAM3S4: [manual][s124] pages 467-538
7//! - SAM3S8, SAM3SD8: [manual][sd8] pages 474-550
8//! - SAM3U: [manual][u] pages 494-550
9//!
10//! [ax]: https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-11057-32-bit-Cortex-M3-Microcontroller-SAM3X-SAM3A_Datasheet.pdf
11//! [n]: https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-11011-32-bit-Cortex-M3-Microcontroller-SAM3N_Datasheet.pdf
12//! [s124]: https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-6500-32-bit-Cortex-M3-Microcontroller-SAM3S4-SAM3S2-SAM3S1_Datasheet.pdf
13//! [sd8]: https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-11090-32-bit%20Cortex-M3-Microcontroller-SAM-3S8-SD8_Datasheet.pdf
14//! [u]: https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-6430-32-bit-Cortex-M3-Microcontroller-SAM3U4-SAM3U2-SAM3U1_Datasheet.pdf
15
16// pub mod dynpin;
17pub mod filter;
18pub mod interrupt;
19pub mod peripheral;
20pub mod pin;
21pub mod pioa;
22pub mod piob;
23#[cfg(feature = "pioc")]
24pub mod pioc;
25#[cfg(feature = "piod")]
26pub mod piod;
27#[cfg(feature = "pioe")]
28pub mod pioe;
29#[cfg(feature = "piof")]
30pub mod piof;
31pub mod structure;
32
33use crate::write_protect::{WpmrWpsrRegs, WriteProtect, WriteProtectKey};
34#[allow(clippy::wildcard_imports)]
35use structure::*;
36
37impl<Pio: PioRegisters> WriteProtectKey for Pio {
38    const WPKEY: u32 = 0x50494F;
39}
40
41impl<Pio: PioRegisters> WpmrWpsrRegs for Pio {
42    type Wpmr = <Self as PioRegisters>::Wpmr;
43
44    fn _wpmr(&self) -> &Self::Wpmr {
45        self._wpmr()
46    }
47
48    type Wpsr = <Self as PioRegisters>::Wpsr;
49
50    fn _wpsr(&self) -> &Self::Wpsr {
51        self._wpsr()
52    }
53}
54
55/// PIO types have write protection on the following fields:
56///
57///   - PIO Enable Register (`PIO_PER`)
58///   - PIO Disable Register (`PIO_PDR`)
59///   - Output Enable Register (`PIO_OER`)
60///   - Output Disable Register (`PIO_ODR`)
61///   - Input Filter Enable Register (`PIO_IFER`)
62///   - Input Filter Disable Register (`PIO_IDER`)
63///   - Multi-driver Enable Register (`PIO_MDER`)
64///   - Multi-driver Disable Register (`PIO_MDDR`)
65///   - Pull Up Enable Register (`PIO_PUER`)
66///   - Pull Up Disable Register (`PIO_PUDR`)
67#[cfg_attr(
68    feature = "2fn",
69    doc = "    - Peripheral AB Select Register (`PIO_ABSR`)"
70)]
71#[cfg_attr(
72    any(feature = "3fn", feature = "4fn"),
73    doc = "    - Peripheral ABCD Select Register 0 (`PIO_ABCDSR0`)"
74)]
75#[cfg_attr(
76    any(feature = "3fn", feature = "4fn"),
77    doc = "    - Peripheral ABCD Select Register 1 (`PIO_ABCDSR1`)"
78)]
79///   - Output Write Enable Register (`PIO_OWER`)
80///   - Output Write Disable Register (`PIO_OWDR`)
81impl<Pio: PioRegisters> WriteProtect for Pio {}
82
83macro_rules! def_pioc {
84    ($(
85        $pio:ident($inner:ty) => {
86            $(
87                $(#[$meta:meta])*
88                $pio_abbv:ident: $num:literal
89            ),+$(,)?
90        }
91    ),+$(,)?) => {$(
92        paste::paste! {
93            $(
94                $(#[$meta])*
95                pub struct [<$pio_abbv:camel $num:camel>];
96                impl crate::pio::pin::PinId for [<$pio_abbv $num>] {
97                    type Controller = crate::pac::$inner;
98                    const MASK: u32 = 1 << $num;
99                }
100            )+
101
102            pub struct $pio {
103                $(
104                    $(#[$meta])*
105                    pub [<$pio_abbv:snake $num:snake>]: crate::pio::pin::Pin<
106                        crate::pac::[<$pio:upper>],
107                        [<$pio_abbv:camel $num:camel>],
108                        crate::pio::pin::Unconfigured,
109                        crate::pio::pin::Unconfigured,
110                        crate::pio::pin::Unconfigured,
111                        crate::pio::pin::Unconfigured,
112                        crate::pio::pin::Unconfigured,
113                    >,
114                )+
115            }
116
117            impl $pio {
118                pub fn [<from_ $pio:lower>](_pio: crate::pac::[<$pio:upper>]) -> Self {
119                    $pio {
120                        $(
121                            [<$pio_abbv:snake $num:snake>]: unsafe { crate::pio::pin::Pin::new() },
122                        )+
123                    }
124                }
125
126                pub(crate) const fn inner(&self) -> &crate::pac::[<$pio:lower>]::RegisterBlock {
127                    unsafe { &*crate::pac::[<$pio:upper>]::PTR }
128                }
129            }
130
131            const _: () = {
132                use crate::write_protect::{WriteProtect, WriteProtectKey, WpmrWpsrRegs};
133
134                impl WriteProtectKey for $pio {
135                    const WPKEY: u32 = crate::pac::[<$pio:upper>]::WPKEY;
136                }
137
138                impl WpmrWpsrRegs for $pio {
139                    type Wpmr = <crate::pac::[<$pio:upper>] as WpmrWpsrRegs>::Wpmr;
140                    fn _wpmr(&self) -> &Self::Wpmr {
141                        self.inner()._wpmr()
142                    }
143                    type Wpsr = <crate::pac::[<$pio:upper>] as WpmrWpsrRegs>::Wpsr;
144                    fn _wpsr(&self) -> &Self::Wpsr {
145                        self.inner()._wpsr()
146                    }
147                }
148
149                impl WriteProtect for $pio {}
150            };
151        }
152    )+};
153}
154
155pub(crate) use def_pioc;
156
157#[allow(clippy::module_name_repetitions)]
158#[derive(Clone, Copy, Debug)]
159/// Type returned in case a configuration change fails.
160pub enum PioError {
161    LineLocked,
162    WriteProtected,
163}
164
165macro_rules! pin_peripherals {
166    (
167        pio: $pio:ty,
168        pinopts: [$($pinopts:tt),+$(,)?],
169    ) => {
170        $(
171            crate::pio::pin_peripherals! {
172                @pin
173                pio: $pio,
174                pinopts: $pinopts,
175            }
176        )+
177    };
178    (
179        @pin
180        pio: $pio:ty,
181        pinopts: [
182            pin: $pin:ty,
183            peripherals: [$($peripheral:ident),+]$(,)?
184        ],
185    ) => {
186        $(
187            crate::pio::pin_peripherals! {
188                @impl
189                pio: $pio,
190                pin: $pin,
191                peripheral: $peripheral,
192            }
193        )+
194    };
195    (
196        @impl
197        pio: $pio:ty,
198        pin: $pin:ty,
199        peripheral: $peripheral:ident,
200    ) => {
201        paste::paste! {
202            impl crate::pio::peripheral::PeripheralExistsFor<$pio, $pin>
203                for [<Peripheral $peripheral>] {}
204        }
205    };
206}
207
208pub(crate) use pin_peripherals;
209
210// impl<Pio: IsPio> Pio {
211//     pub fn status_reg(&self) -> u32 {
212//         unsafe {
213//             (&*(<Pio as IsPio>::PTR as *const RegisterBlock))
214//                 .psr
215//                 .read()
216//                 .bits()
217//         }
218//     }
219// }