tm4c129x/
generic.rs

1use core::marker;
2#[doc = "This trait shows that register has `read` method"]
3#[doc = ""]
4#[doc = "Registers marked with `Writable` can be also `modify`'ed"]
5pub trait Readable {}
6#[doc = "This trait shows that register has `write`, `write_with_zero` and `reset` method"]
7#[doc = ""]
8#[doc = "Registers marked with `Readable` can be also `modify`'ed"]
9pub trait Writable {}
10#[doc = "Reset value of the register"]
11#[doc = ""]
12#[doc = "This value is initial value for `write` method."]
13#[doc = "It can be also directly writed to register by `reset` method."]
14pub trait ResetValue {
15    #[doc = "Register size"]
16    type Type;
17    #[doc = "Reset value of the register"]
18    fn reset_value() -> Self::Type;
19}
20#[doc = "This structure provides volatile access to register"]
21pub struct Reg<U, REG> {
22    register: vcell::VolatileCell<U>,
23    _marker: marker::PhantomData<REG>,
24}
25unsafe impl<U: Send, REG> Send for Reg<U, REG> {}
26impl<U, REG> Reg<U, REG>
27where
28    Self: Readable,
29    U: Copy,
30{
31    #[doc = "Reads the contents of `Readable` register"]
32    #[doc = ""]
33    #[doc = "You can read the contents of a register in such way:"]
34    #[doc = "```ignore"]
35    #[doc = "let bits = periph.reg.read().bits();"]
36    #[doc = "```"]
37    #[doc = "or get the content of a particular field of a register."]
38    #[doc = "```ignore"]
39    #[doc = "let reader = periph.reg.read();"]
40    #[doc = "let bits = reader.field1().bits();"]
41    #[doc = "let flag = reader.field2().bit_is_set();"]
42    #[doc = "```"]
43    #[inline(always)]
44    pub fn read(&self) -> R<U, Self> {
45        R { bits: self.register.get(), _reg: marker::PhantomData }
46    }
47}
48impl<U, REG> Reg<U, REG>
49where
50    Self: ResetValue<Type = U> + Writable,
51    U: Copy,
52{
53    #[doc = "Writes the reset value to `Writable` register"]
54    #[doc = ""]
55    #[doc = "Resets the register to its initial state"]
56    #[inline(always)]
57    pub fn reset(&self) {
58        self.register.set(Self::reset_value())
59    }
60}
61impl<U, REG> Reg<U, REG>
62where
63    Self: ResetValue<Type = U> + Writable,
64    U: Copy,
65{
66    #[doc = "Writes bits to `Writable` register"]
67    #[doc = ""]
68    #[doc = "You can write raw bits into a register:"]
69    #[doc = "```ignore"]
70    #[doc = "periph.reg.write(|w| unsafe { w.bits(rawbits) });"]
71    #[doc = "```"]
72    #[doc = "or write only the fields you need:"]
73    #[doc = "```ignore"]
74    #[doc = "periph.reg.write(|w| w"]
75    #[doc = "    .field1().bits(newfield1bits)"]
76    #[doc = "    .field2().set_bit()"]
77    #[doc = "    .field3().variant(VARIANT)"]
78    #[doc = ");"]
79    #[doc = "```"]
80    #[doc = "Other fields will have reset value."]
81    #[inline(always)]
82    pub fn write<F>(&self, f: F)
83    where
84        F: FnOnce(&mut W<U, Self>) -> &mut W<U, Self>,
85    {
86        self.register.set(f(&mut W { bits: Self::reset_value(), _reg: marker::PhantomData }).bits);
87    }
88}
89impl<U, REG> Reg<U, REG>
90where
91    Self: Writable,
92    U: Copy + Default,
93{
94    #[doc = "Writes Zero to `Writable` register"]
95    #[doc = ""]
96    #[doc = "Similar to `write`, but unused bits will contain 0."]
97    #[inline(always)]
98    pub fn write_with_zero<F>(&self, f: F)
99    where
100        F: FnOnce(&mut W<U, Self>) -> &mut W<U, Self>,
101    {
102        self.register.set(f(&mut W { bits: U::default(), _reg: marker::PhantomData }).bits);
103    }
104}
105impl<U, REG> Reg<U, REG>
106where
107    Self: Readable + Writable,
108    U: Copy,
109{
110    #[doc = "Modifies the contents of the register"]
111    #[doc = ""]
112    #[doc = "E.g. to do a read-modify-write sequence to change parts of a register:"]
113    #[doc = "```ignore"]
114    #[doc = "periph.reg.modify(|r, w| unsafe { w.bits("]
115    #[doc = "   r.bits() | 3"]
116    #[doc = ") });"]
117    #[doc = "```"]
118    #[doc = "or"]
119    #[doc = "```ignore"]
120    #[doc = "periph.reg.modify(|_, w| w"]
121    #[doc = "    .field1().bits(newfield1bits)"]
122    #[doc = "    .field2().set_bit()"]
123    #[doc = "    .field3().variant(VARIANT)"]
124    #[doc = ");"]
125    #[doc = "```"]
126    #[doc = "Other fields will have value they had before call `modify`."]
127    #[inline(always)]
128    pub fn modify<F>(&self, f: F)
129    where
130        for<'w> F: FnOnce(&R<U, Self>, &'w mut W<U, Self>) -> &'w mut W<U, Self>,
131    {
132        let bits = self.register.get();
133        self.register.set(f(&R { bits, _reg: marker::PhantomData }, &mut W { bits, _reg: marker::PhantomData }).bits);
134    }
135}
136#[doc = "Register/field reader"]
137#[doc = ""]
138#[doc = "Result of the [`read`](Reg::read) method of a register."]
139#[doc = "Also it can be used in the [`modify`](Reg::read) method"]
140pub struct R<U, T> {
141    pub(crate) bits: U,
142    _reg: marker::PhantomData<T>,
143}
144impl<U, T> R<U, T>
145where
146    U: Copy,
147{
148    #[doc = "Create new instance of reader"]
149    #[inline(always)]
150    pub(crate) fn new(bits: U) -> Self {
151        Self { bits, _reg: marker::PhantomData }
152    }
153    #[doc = "Read raw bits from register/field"]
154    #[inline(always)]
155    pub fn bits(&self) -> U {
156        self.bits
157    }
158}
159impl<U, T, FI> PartialEq<FI> for R<U, T>
160where
161    U: PartialEq,
162    FI: Copy + Into<U>,
163{
164    #[inline(always)]
165    fn eq(&self, other: &FI) -> bool {
166        self.bits.eq(&(*other).into())
167    }
168}
169impl<FI> R<bool, FI> {
170    #[doc = "Value of the field as raw bits"]
171    #[inline(always)]
172    pub fn bit(&self) -> bool {
173        self.bits
174    }
175    #[doc = "Returns `true` if the bit is clear (0)"]
176    #[inline(always)]
177    pub fn bit_is_clear(&self) -> bool {
178        !self.bit()
179    }
180    #[doc = "Returns `true` if the bit is set (1)"]
181    #[inline(always)]
182    pub fn bit_is_set(&self) -> bool {
183        self.bit()
184    }
185}
186#[doc = "Register writer"]
187#[doc = ""]
188#[doc = "Used as an argument to the closures in the [`write`](Reg::write) and [`modify`](Reg::modify) methods of the register"]
189pub struct W<U, REG> {
190    #[doc = "Writable bits"]
191    pub(crate) bits: U,
192    _reg: marker::PhantomData<REG>,
193}
194impl<U, REG> W<U, REG> {
195    #[doc = "Writes raw bits to the register"]
196    #[inline(always)]
197    pub unsafe fn bits(&mut self, bits: U) -> &mut Self {
198        self.bits = bits;
199        self
200    }
201}
202#[doc = "Used if enumerated values cover not the whole range"]
203#[derive(Clone, Copy, PartialEq)]
204pub enum Variant<U, T> {
205    #[doc = "Expected variant"]
206    Val(T),
207    #[doc = "Raw bits"]
208    Res(U),
209}