sam3_hal/
structure.rs

1use crate::pac::generic::{BitReader, FieldReader, FieldSpec, IsEnum};
2
3/// [`Reg::read`][rr] as a trait.
4///
5/// [rr]: crate::pac::generic::Reg::read
6pub trait BReader {
7    type R: BReaderFields;
8    fn read(&self) -> Self::R;
9}
10
11seq_macro::seq! {N in 0..32 {
12    paste::paste! {
13        /// Trait providing `.p0()`, `.p1()`, ..., `.p31()`, and [`.bits()`][bits] methods as
14        /// readers as found on most PIO registers.
15        ///
16        /// [bits]: crate::pac::generic::R#method.bits
17        pub trait BReaderFields {
18            #(
19                type [<P~N R>]: BRead;
20                fn p~N(&self) -> Self::[<P~N R>];
21            )*
22            fn bits(&self) -> u32;
23        }
24    }
25}}
26
27/// [`BitReader`] methods as a trait.
28pub trait BRead {
29    fn bit(&self) -> bool;
30    fn bit_is_clear(&self) -> bool;
31    fn bit_is_set(&self) -> bool;
32}
33
34impl BRead for BitReader {
35    fn bit(&self) -> bool {
36        self.bit()
37    }
38
39    fn bit_is_clear(&self) -> bool {
40        self.bit_is_clear()
41    }
42
43    fn bit_is_set(&self) -> bool {
44        self.bit_is_set()
45    }
46}
47
48/// [`Reg::reset`][rr] and [`Reg::write`][rw] as a trait.
49///
50/// [rr]: crate::pac::generic::Reg::reset
51/// [rw]: crate::pac::generic::Reg::write
52pub trait BWriter {
53    fn reset(&self);
54    type W: BWriterFields;
55    fn write<F>(&self, f: F)
56    where
57        F: FnOnce(&mut Self::W) -> &mut Self::W;
58}
59
60seq_macro::seq! {N in 0..32 {
61    paste::paste! {
62        /// Trait providing `.p0()`, `.p1()`, ..., `.p31()`, and [`.bits(bits)`][bits] methods as
63        /// writers as found on most PIO registers.
64        ///
65        /// [bits]: crate::pac::generic::W#method.bits
66        pub trait BWriterFields: Sized {
67            #(
68                type [<P~N W>]<'a>: BWrite<'a, Self>
69                where
70                    Self: 'a + Sized;
71                fn p~N(&mut self) -> Self::[<P~N W>]<'_>;
72            )*
73            #[allow(clippy::missing_safety_doc)]
74            /// See [`W::bits`][bits].
75            ///
76            /// [bits]: crate::pac::generic::W#method.bits
77            unsafe fn bits(&mut self, bits: u32) -> &mut Self;
78        }
79    }
80}}
81
82/// [`BitWriter`][bw] methods as a trait.
83///
84/// [bw]: crate::pac::generic::BitWriter
85pub trait BWrite<'a, W> {
86    const WIDTH: u8 = 1u8;
87    fn width(&self) -> u8;
88    fn offset(&self) -> u8;
89    fn bit(self, value: bool) -> &'a mut W;
90    fn variant(self, variant: bool) -> &'a mut W;
91    fn set_bit(self) -> &'a mut W;
92    fn clear_bit(self) -> &'a mut W;
93}
94
95/// [`Reg::write_with_zero`][rwwz] as a trait.
96///
97/// [rwwz]: crate::pac::generic::Reg::write_with_zero
98pub trait BWriterWithZero {
99    type W: BWriterFields;
100    #[allow(clippy::missing_safety_doc)]
101    /// See [`Reg::write_with_zero`](crate::pac::generic::Reg::write_with_zero).
102    unsafe fn write_with_zero<F>(&self, f: F)
103    where
104        F: FnOnce(&mut Self::W) -> &mut Self::W;
105}
106
107/// [`Reg::modify`][rm] as a trait.
108///
109/// [rm]: crate::pac::generic::Reg::modify
110pub trait BModify: BReader + BWriterWithZero {
111    fn modify<F>(&self, f: F)
112    where
113        for<'w> F: FnOnce(
114            &<Self as BReader>::R,
115            &'w mut <Self as BWriterWithZero>::W,
116        ) -> &'w mut <Self as BWriterWithZero>::W;
117}
118
119/// [`FieldReader::bits`][frb] as a trait.
120///
121/// [frb]: crate::pac::generic::FieldReader#method.bits
122pub trait FRead<FI: FieldSpec> {
123    fn bits(&self) -> FI::Ux;
124}
125
126/// [`FieldWriter`][fw] methods as a trait.
127///
128/// [fw]: crate::pac::generic::FieldWriter
129pub trait FWrite<'a, const WI: u8, FI: FieldSpec, W> {
130    /// See [`FieldWriter::WIDTH`][width].
131    ///
132    /// [width]: crate::pac::generic::FieldWriter#associatedconstant.WIDTH
133    const WIDTH: u8 = WI;
134    /// See [`FieldWriter::width`][width].
135    ///
136    /// [width]: crate::pac::generic::FieldWriter#method.width
137    fn width(&self) -> u8;
138    /// See [`FieldWriter::offset`][offset].
139    ///
140    /// [offset]: crate::pac::generic::FieldWriter#method.offset
141    fn offset(&self) -> u8;
142    #[allow(clippy::missing_safety_doc)]
143    /// See [`FieldWriter::bits`][bits].
144    ///
145    /// [bits]: crate::pac::generic::FieldWriter#method.bits
146    unsafe fn bits(self, value: FI::Ux) -> &'a mut W;
147}
148
149/// [`FieldWriter::variant`][fwv] as a trait.
150///
151/// [fwv]: crate::pac::generic::FieldWriter#method.variant
152pub trait FWriteVariant<'a, const WI: u8, FI: IsEnum, W>: FWrite<'a, WI, FI, W> {
153    fn variant(self, variant: FI) -> &'a mut W;
154}
155
156/// [`FieldWriter::set`][fws] as a trait.
157///
158/// [fws]: crate::pac::generic::FieldWriter#method.set
159pub trait FWriteSafe<'a, const WI: u8, FI: FieldSpec, W>: FWrite<'a, WI, FI, W> {
160    fn set(self, value: FI::Ux) -> &'a mut W;
161}
162
163impl<FI: FieldSpec> FRead<FI> for FieldReader<FI> {
164    fn bits(&self) -> FI::Ux {
165        self.bits()
166    }
167}
168
169macro_rules! bwrite_impl {
170    (
171        $(meta: [$(#[$meta:meta])*],)?
172        ty: $ty:ident,
173        reg: $reg:ident,
174    ) => {
175        paste::paste! {
176            $($(#[$meta])*)?
177            impl<'a> crate::structure::BWrite<'a, crate::pac::[<$ty:lower>]::[<$reg:lower>]::W>
178                for crate::pac::generic::BitWriter<'a, crate::pac::[<$ty:lower>]::[<$reg:lower>]::[<$reg Spec>]>
179            {
180                const WIDTH: u8 = Self::WIDTH;
181                fn width(&self) -> u8 {
182                    self.width()
183                }
184                fn offset(&self) -> u8 {
185                    self.offset()
186                }
187                fn bit(self, value: bool) -> &'a mut crate::pac::[<$ty:lower>]::[<$reg:lower>]::W {
188                    self.bit(value)
189                }
190                fn variant(
191                    self,
192                    variant: bool,
193                ) -> &'a mut crate::pac::[<$ty:lower>]::[<$reg:lower>]::W {
194                    self.variant(variant)
195                }
196                fn set_bit(self) -> &'a mut crate::pac::[<$ty:lower>]::[<$reg:lower>]::W {
197                    self.set_bit()
198                }
199                fn clear_bit(self) -> &'a mut crate::pac::[<$ty:lower>]::[<$reg:lower>]::W {
200                    self.clear_bit()
201                }
202            }
203        }
204    }
205}
206
207pub(crate) use bwrite_impl;