1use crate::pac::generic::{BitReader, FieldReader, FieldSpec, IsEnum};
2
3pub 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 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
27pub 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
48pub 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 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 unsafe fn bits(&mut self, bits: u32) -> &mut Self;
78 }
79 }
80}}
81
82pub 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
95pub trait BWriterWithZero {
99 type W: BWriterFields;
100 #[allow(clippy::missing_safety_doc)]
101 unsafe fn write_with_zero<F>(&self, f: F)
103 where
104 F: FnOnce(&mut Self::W) -> &mut Self::W;
105}
106
107pub 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
119pub trait FRead<FI: FieldSpec> {
123 fn bits(&self) -> FI::Ux;
124}
125
126pub trait FWrite<'a, const WI: u8, FI: FieldSpec, W> {
130 const WIDTH: u8 = WI;
134 fn width(&self) -> u8;
138 fn offset(&self) -> u8;
142 #[allow(clippy::missing_safety_doc)]
143 unsafe fn bits(self, value: FI::Ux) -> &'a mut W;
147}
148
149pub 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
156pub 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;