Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mod generic {
- use core::marker;
- ///Marker trait for readable register
- pub trait Readable {}
- ///Marker trait for writable register
- pub trait Writable {}
- pub trait RegSize {
- ///Register size
- type Type;
- }
- ///Reset value of the register
- pub trait ResetValue : RegSize {
- ///Reset value of the register
- fn reset_value() -> Self::Type;
- }
- ///Converting enumerated values to bits
- pub trait ToBits<N> {
- ///Conversion method
- fn _bits(&self) -> N;
- }
- ///Wrapper for registers
- pub struct Reg<U, REG> {
- pub(crate) register: vcell::VolatileCell<U>,
- pub(crate) _marker: marker::PhantomData<REG>,
- }
- unsafe impl<U, REG> Send for Reg<U, REG> { }
- impl<U, REG> Reg<U, REG>
- where
- Self: Readable,
- U: Copy
- {
- ///Reads the contents of the register
- #[inline(always)]
- pub fn read(&self) -> R<U, Self> {
- R {bits: self.register.get(), _reg: marker::PhantomData}
- }
- }
- impl<U, REG> Reg<U, REG>
- where
- Self: ResetValue<Type=U> + Writable,
- U: Copy,
- {
- ///Writes the reset value to the register
- #[inline(always)]
- pub fn reset(&self) {
- self.register.set(Self::reset_value())
- }
- }
- impl<U, REG> Reg<U, REG>
- where
- Self: ResetValue<Type=U> + Writable,
- U: Copy
- {
- ///Writes to the register
- #[inline(always)]
- pub fn write<F>(&self, f: F)
- where
- F: FnOnce(&mut W<U, Self>) -> &mut W<U, Self>
- {
- self.register.set(f(&mut W {bits: Self::reset_value(), _reg: marker::PhantomData}).bits);
- }
- }
- impl<U, REG> Reg<U, REG>
- where
- Self: Writable,
- U: Copy + Default
- {
- ///Writes Zero to the register
- #[inline(always)]
- pub fn write_with_zero<F>(&self, f: F)
- where
- F: FnOnce(&mut W<U, Self>) -> &mut W<U, Self>
- {
- self.register.set(f(&mut W {bits: U::default(), _reg: marker::PhantomData }).bits);
- }
- }
- impl<U, REG> Reg<U, REG>
- where
- Self: Readable + Writable,
- U: Copy,
- {
- ///Modifies the contents of the register
- #[inline(always)]
- pub fn modify<F>(&self, f: F)
- where
- for<'w> F: FnOnce(&R<U, Self>, &'w mut W<U, Self>) -> &'w mut W<U, Self>
- {
- let bits = self.register.get();
- self.register.set(f(&R {bits, _reg: marker::PhantomData}, &mut W {bits, _reg: marker::PhantomData}).bits);
- }
- }
- ///Register/field reader
- pub struct R<U, T> {
- pub(crate) bits: U,
- _reg: marker::PhantomData<T>,
- }
- impl<U, T> R<U, T>
- where
- U: Copy
- {
- ///Create new instance of reader
- #[inline(always)]
- pub(crate) fn new(bits: U) -> Self {
- Self {
- bits,
- _reg: marker::PhantomData,
- }
- }
- ///Read raw bits from register/field
- #[inline(always)]
- pub fn bits(&self) -> U {
- self.bits
- }
- }
- impl<U, T, FI> PartialEq<FI> for R<U, T>
- where
- U: PartialEq,
- FI: ToBits<U>
- {
- fn eq(&self, other: &FI) -> bool {
- self.bits.eq(&other._bits())
- }
- }
- impl<FI> R<bool, FI> {
- ///Value of the field as raw bits
- #[inline(always)]
- pub fn bit(&self) -> bool {
- self.bits
- }
- ///Returns `true` if the bit is clear (0)
- #[inline(always)]
- pub fn bit_is_clear(&self) -> bool {
- !self.bit()
- }
- ///Returns `true` if the bit is set (1)
- #[inline(always)]
- pub fn bit_is_set(&self) -> bool {
- self.bit()
- }
- }
- ///Register writer
- pub struct W<U, REG> {
- ///Writable bits
- pub bits: U,
- _reg: marker::PhantomData<REG>,
- }
- ///Used if enumerated values cover not the whole range
- #[derive(Clone,Copy,PartialEq)]
- pub enum Variant<U, T> {
- ///Expected variant
- Val(T),
- ///Raw bits
- Res(U),
- }
- impl<U, REG> W<U, REG>
- where
- Self: Safety<S=Safe>
- {
- ///Writes raw bits to the register
- #[inline(always)]
- pub fn bits(&mut self, bits: U) -> &mut Self {
- self.bits = bits;
- self
- }
- }
- impl<U, REG> W<U, REG>
- where
- Self: Safety<S=Unsafe>
- {
- ///Writes raw bits to the register
- #[inline(always)]
- pub unsafe fn bits(&mut self, bits: U) -> &mut Self {
- self.bits = bits;
- self
- }
- }
- /// Mask of field
- pub trait Mask {
- type Type;
- const MASK: Self::Type;
- }
- ///Marker struct for register/field with safe write
- pub struct Safe;
- ///Marker struct for register/field with unsafe write
- pub struct Unsafe;
- pub trait Safety {
- ///Safety type: `Safe` or `Unsafe`
- type S;
- }
- ///Writer Proxy
- pub struct WProxy<'a, U, REG, N, FI> {
- w: &'a mut W<U, REG>,
- offset: u8,
- _field: marker::PhantomData<(FI, N)>,
- }
- impl<'a, U, REG, N, FI> WProxy<'a, U, REG, N, FI> {
- pub(crate) fn new(w: &'a mut W<U, REG>, offset: u8) -> Self {
- Self {
- w,
- offset,
- _field: marker::PhantomData,
- }
- }
- }
- impl<'a, U, REG, FI> WProxy<'a, U, REG, bool, FI>
- where
- U: Copy + SetBit
- {
- /// Sets the field bit"]
- pub fn set_bit(self) -> &'a mut W<U, REG> {
- self.bit(true)
- }
- /// Clears the field bit"]
- pub fn clear_bit(self) -> &'a mut W<U, REG> {
- self.bit(false)
- }
- /// Writes raw bits to the field"]
- #[inline(always)]
- pub fn bit(self, value: bool) -> &'a mut W<U, REG> {
- self.w.bits.setbit(self.offset, value);
- self.w
- }
- }
- impl<'a, U, REG, FI> WProxy<'a, U, REG, bool, FI>
- where
- FI: ToBits<bool>,
- U: Copy + SetBit,
- {
- ///Writes `variant` to the field
- #[inline]
- pub fn variant(self, variant: FI) -> &'a mut W<U, REG> {
- self.bit(variant._bits())
- }
- }
- impl<'a, U, REG, N, FI> WProxy<'a, U, REG, N, FI>
- where
- Self: Safety<S=Safe>,
- FI: Mask<Type=U>,
- U: Copy + SetBits<N>,
- {
- ///Writes raw bits to the field"]
- #[inline]
- pub fn bits(self, value: N) -> &'a mut W<U, REG> {
- self.w.bits.setbits(FI::MASK, self.offset, value);
- self.w
- }
- }
- impl<'a, U, REG, N, FI> WProxy<'a, U, REG, N, FI>
- where
- Self: Safety<S=Unsafe>,
- FI: Mask<Type=U>,
- U: Copy + SetBits<N>,
- {
- ///Writes raw bits to the field"]
- #[inline]
- pub unsafe fn bits(self, value: N) -> &'a mut W<U, REG> {
- self.w.bits.setbits(FI::MASK, self.offset, value);
- self.w
- }
- }
- impl<'a, U, REG, N, FI> WProxy<'a, U, REG, N, FI>
- where
- Self: Safety<S=Safe>,
- FI: Mask<Type=U> + ToBits<N>,
- U: Copy + SetBits<N>,
- {
- ///Writes `variant` to the field
- #[inline]
- pub fn variant(self, variant: FI) -> &'a mut W<U, REG> {
- self.bits(variant._bits())
- }
- }
- impl<'a, U, REG, N, FI> WProxy<'a, U, REG, N, FI>
- where
- Self: Safety<S=Unsafe>,
- FI: Mask<Type=U> + ToBits<N>,
- U: Copy + SetBits<N>,
- {
- ///Writes `variant` to the field
- #[inline]
- pub fn variant(self, variant: FI) -> &'a mut W<U, REG> {
- unsafe { self.bits(variant._bits()) }
- }
- }
- pub trait SetBits<N> {
- fn setbits(&mut self, mask: Self, offset: u8, value: N);
- }
- pub trait SetBit {
- fn setbit(&mut self, offset: u8, value: bool);
- }
- macro_rules! impl_set_bits {
- ($U:ty : $($N:ty),+) => {
- impl SetBit for $U {
- #[inline(always)]
- fn setbit(&mut self, offset: u8, value: bool) {
- *self = *self & !(1 << offset) | ((value as Self) << offset)
- }
- }
- $(
- impl SetBits<$N> for $U {
- #[inline(always)]
- fn setbits(&mut self, mask: Self, offset: u8, value: $N) {
- *self = *self & !(mask << offset) | (((value as Self) & mask) << offset)
- }
- }
- )+
- }
- }
- impl_set_bits!(u64: u64, u32, u16, u8);
- impl_set_bits!(u32: u32, u16, u8);
- impl_set_bits!(u16: u16, u8);
- impl_set_bits!(u8: u8);
- }
- pub use generic::*;
- // --------- USAGE: ------------------------
- pub mod pac {
- pub struct _CR1;
- pub type CR1 = crate::Reg<u32, _CR1>;
- impl crate::Readable for CR1 {}
- impl crate::Writable for CR1 {}
- impl crate::RegSize for CR1 {
- type Type = u32;
- }
- impl crate::ResetValue for CR1 {
- #[inline(always)]
- fn reset_value() -> Self::Type { 0x3 }
- }
- #[allow(non_camel_case_types)]
- pub mod cr1 {
- pub type R = crate::R<u32, super::CR1>;
- pub type W = crate::W<u32, super::CR1>;
- impl crate::Safety for W {
- type S = crate::Unsafe;
- }
- pub type CKD_R = crate::R<u8, CKD_A>;
- pub type CKD_W<'a> = crate::WProxy<'a, u32, super::CR1, u8, CKD_A>;
- impl crate::Safety for CKD_W<'_> {
- type S = crate::Unsafe;
- }
- impl crate::Mask for CKD_A {
- type Type = u32;
- const MASK: u32 = 0x03;
- }
- pub type CEN_R = crate::R<bool, CEN_A>;
- pub type CEN_W<'a> = crate::WProxy<'a, u32, super::CR1, bool, CEN_A>;
- pub type CEN2_R = CEN_R;
- pub type CEN2_W<'a> = CEN_W<'a>;
- pub type CB_R = crate::R<u8, u8>;
- impl W {
- #[doc = "Bits 8:9 - Clock division"]
- #[inline(always)]
- pub fn ckd(&mut self) -> CKD_W {
- CKD_W::new(self, 8)
- }
- #[doc = "Bit 0 - Counter enable"]
- #[inline(always)]
- pub fn cen(&mut self) -> CEN_W {
- CEN_W::new(self, 0)
- }
- #[doc = "Bit 0 - Counter enable"]
- #[inline(always)]
- pub fn cen2(&mut self) -> CEN2_W {
- CEN2_W::new(self, 5)
- }
- }
- impl R {
- #[doc = "Bits 8:9 - Clock division"]
- #[inline(always)]
- pub fn ckd(&self) -> CKD_R {
- CKD_R::new(((self.bits() >> 8) & 0x03) as u8)
- }
- #[doc = "Bit 0 - Counter enable"]
- #[inline(always)]
- pub fn cen(&self) -> CEN_R {
- CEN_R::new(((self.bits() >> 0) & 0x01) != 0)
- }
- #[doc = "Bit 0 - Counter enable"]
- #[inline(always)]
- pub fn cen2(&self) -> CEN2_R {
- CEN2_R::new(((self.bits() >> 5) & 0x01) != 0)
- }
- #[doc = "Bit 0 - Counter enable"]
- #[inline(always)]
- pub fn cb(&self) -> CB_R {
- CB_R::new(((self.bits() >> 15) & 0x07) as u8)
- }
- }
- #[doc = "Values that can be written to the field `CKD`"]
- #[derive(Clone, Copy, Debug, PartialEq)]
- pub enum CKD_A {
- #[doc = "t_DTS = t_CK_INT"]
- DIV1,
- #[doc = "t_DTS = 2 \u{d7} t_CK_INT"]
- DIV2,
- #[doc = "t_DTS = 4 \u{d7} t_CK_INT"]
- DIV4,
- }
- impl crate::ToBits<u8> for CKD_A {
- #[allow(missing_docs)]
- #[doc(hidden)]
- #[inline(always)]
- fn _bits(&self) -> u8 {
- match *self {
- CKD_A::DIV1 => 0,
- CKD_A::DIV2 => 1,
- CKD_A::DIV4 => 2,
- }
- }
- }
- impl CKD_R {
- #[doc = "Checks if the value of the field is `DIV1`"]
- #[inline(always)]
- pub fn is_div1(&self) -> bool {
- *self == CKD_A::DIV1
- }
- #[doc = "Checks if the value of the field is `DIV2`"]
- #[inline(always)]
- pub fn is_div2(&self) -> bool {
- *self == CKD_A::DIV2
- }
- #[doc = "Checks if the value of the field is `DIV4`"]
- #[inline(always)]
- pub fn is_div4(&self) -> bool {
- *self == CKD_A::DIV4
- }
- }
- impl<'a> CKD_W<'a> {
- #[doc = "t_DTS = t_CK_INT"]
- #[inline(always)]
- pub fn div1(self) -> &'a mut W {
- self.variant(CKD_A::DIV1)
- }
- #[doc = "t_DTS = 2 \u{d7} t_CK_INT"]
- #[inline(always)]
- pub fn div2(self) -> &'a mut W {
- self.variant(CKD_A::DIV2)
- }
- #[doc = "t_DTS = 4 \u{d7} t_CK_INT"]
- #[inline(always)]
- pub fn div4(self) -> &'a mut W {
- self.variant(CKD_A::DIV4)
- }
- }
- #[doc = "Possible values of the field `CEN`"]
- #[derive(Clone, Copy, Debug, PartialEq)]
- pub enum CEN_A {
- #[doc = "Counter disabled"]
- DISABLED,
- #[doc = "Counter enabled"]
- ENABLED,
- }
- impl crate::ToBits<bool> for CEN_A {
- #[inline(always)]
- fn _bits(&self) -> bool {
- match *self {
- CEN_A::DISABLED => false,
- CEN_A::ENABLED => true,
- }
- }
- }
- impl CEN_R {
- #[doc = "Checks if the value of the field is `DISABLED`"]
- #[inline(always)]
- pub fn is_disabled(&self) -> bool {
- *self == CEN_A::DISABLED
- }
- #[doc = "Checks if the value of the field is `ENABLED`"]
- #[inline(always)]
- pub fn is_enabled(&self) -> bool {
- *self == CEN_A::ENABLED
- }
- }
- impl<'a> CEN_W<'a> {
- #[doc = "Counter disabled"]
- #[inline(always)]
- pub fn disabled(self) -> &'a mut W {
- self.variant(CEN_A::DISABLED)
- }
- #[doc = "Counter enabled"]
- #[inline(always)]
- pub fn enabled(self) -> &'a mut W {
- self.variant(CEN_A::ENABLED)
- }
- }
- }
- }
- fn main() {
- let reg: pac::CR1 = crate::Reg {
- register: vcell::VolatileCell::new(12),
- _marker: core::marker::PhantomData,
- }; // only for test. Will be get from periph
- reg.modify(|_,w| w
- .cen2() .set_bit()
- .ckd() .div2()
- );
- let r = reg.read();
- println!("{}", r.cen2().bit_is_set());
- assert_eq!(r.bits(), 300);
- println!("{}", r.cen().bit_is_set());
- println!("{}", r.ckd().is_div4());
- reg.reset();
- assert_eq!(reg.read().bits(), 3);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement