SHARE
TWEET

Untitled

a guest Aug 14th, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. mod generic {
  2.     use core::marker;
  3.  
  4.     ///Marker trait for readable register
  5.     pub trait Readable {}
  6.  
  7.     ///Marker trait for writable register
  8.     pub trait Writable {}
  9.  
  10.     pub trait RegSize {
  11.         ///Register size
  12.         type Type;
  13.     }
  14.  
  15.     ///Reset value of the register
  16.     pub trait ResetValue : RegSize {
  17.         ///Reset value of the register
  18.         fn reset_value() -> Self::Type;
  19.     }
  20.  
  21.     ///Converting enumerated values to bits
  22.     pub trait ToBits<N> {
  23.         ///Conversion method
  24.         fn _bits(&self) -> N;
  25.     }
  26.  
  27.     ///Wrapper for registers
  28.     pub struct Reg<U, REG> {
  29.         pub(crate) register: vcell::VolatileCell<U>,
  30.         pub(crate) _marker: marker::PhantomData<REG>,
  31.     }
  32.  
  33.     unsafe impl<U, REG> Send for Reg<U, REG> { }
  34.  
  35.     impl<U, REG> Reg<U, REG>
  36.     where
  37.         Self: Readable,
  38.         U: Copy
  39.     {
  40.         ///Reads the contents of the register
  41.         #[inline(always)]
  42.         pub fn read(&self) -> R<U, Self> {
  43.             R {bits: self.register.get(), _reg: marker::PhantomData}
  44.         }
  45.     }
  46.  
  47.     impl<U, REG> Reg<U, REG>
  48.     where
  49.         Self: ResetValue<Type=U> + Writable,
  50.         U: Copy,
  51.     {
  52.         ///Writes the reset value to the register
  53.         #[inline(always)]
  54.         pub fn reset(&self) {
  55.             self.register.set(Self::reset_value())
  56.         }
  57.     }
  58.  
  59.     impl<U, REG> Reg<U, REG>
  60.     where
  61.         Self: ResetValue<Type=U> + Writable,
  62.         U: Copy
  63.     {
  64.         ///Writes to the register
  65.         #[inline(always)]
  66.         pub fn write<F>(&self, f: F)
  67.         where
  68.             F: FnOnce(&mut W<U, Self>) -> &mut W<U, Self>
  69.         {
  70.             self.register.set(f(&mut W {bits: Self::reset_value(), _reg: marker::PhantomData}).bits);
  71.         }
  72.     }
  73.  
  74.     impl<U, REG> Reg<U, REG>
  75.     where
  76.         Self: Writable,
  77.         U: Copy + Default
  78.     {
  79.         ///Writes Zero to the register
  80.         #[inline(always)]
  81.         pub fn write_with_zero<F>(&self, f: F)
  82.         where
  83.             F: FnOnce(&mut W<U, Self>) -> &mut W<U, Self>
  84.         {
  85.             self.register.set(f(&mut W {bits: U::default(), _reg: marker::PhantomData }).bits);
  86.         }
  87.     }
  88.  
  89.     impl<U, REG> Reg<U, REG>
  90.     where
  91.         Self: Readable + Writable,
  92.         U: Copy,
  93.     {
  94.         ///Modifies the contents of the register
  95.         #[inline(always)]
  96.         pub fn modify<F>(&self, f: F)
  97.         where
  98.             for<'w> F: FnOnce(&R<U, Self>, &'w mut W<U, Self>) -> &'w mut W<U, Self>
  99.         {
  100.             let bits = self.register.get();
  101.             self.register.set(f(&R {bits, _reg: marker::PhantomData}, &mut W {bits, _reg: marker::PhantomData}).bits);
  102.         }
  103.     }
  104.  
  105.     ///Register/field reader
  106.     pub struct R<U, T> {
  107.         pub(crate) bits: U,
  108.         _reg: marker::PhantomData<T>,
  109.     }
  110.  
  111.     impl<U, T> R<U, T>
  112.     where
  113.         U: Copy
  114.     {
  115.         ///Create new instance of reader
  116.         #[inline(always)]
  117.         pub(crate) fn new(bits: U) -> Self {
  118.             Self {
  119.                 bits,
  120.                 _reg: marker::PhantomData,
  121.             }
  122.         }
  123.         ///Read raw bits from register/field
  124.         #[inline(always)]
  125.         pub fn bits(&self) -> U {
  126.             self.bits
  127.         }
  128.     }
  129.  
  130.     impl<U, T, FI> PartialEq<FI> for R<U, T>
  131.     where
  132.         U: PartialEq,
  133.         FI: ToBits<U>
  134.     {
  135.         fn eq(&self, other: &FI) -> bool {
  136.             self.bits.eq(&other._bits())
  137.         }
  138.     }
  139.  
  140.     impl<FI> R<bool, FI> {
  141.         ///Value of the field as raw bits
  142.         #[inline(always)]
  143.         pub fn bit(&self) -> bool {
  144.             self.bits
  145.         }
  146.         ///Returns `true` if the bit is clear (0)
  147.         #[inline(always)]
  148.         pub fn bit_is_clear(&self) -> bool {
  149.             !self.bit()
  150.         }
  151.         ///Returns `true` if the bit is set (1)
  152.         #[inline(always)]
  153.         pub fn bit_is_set(&self) -> bool {
  154.             self.bit()
  155.         }
  156.     }
  157.  
  158.     ///Register writer
  159.     pub struct W<U, REG> {
  160.         ///Writable bits
  161.         pub bits: U,
  162.         _reg: marker::PhantomData<REG>,
  163.     }
  164.  
  165.     ///Used if enumerated values cover not the whole range
  166.     #[derive(Clone,Copy,PartialEq)]
  167.     pub enum Variant<U, T> {
  168.         ///Expected variant
  169.         Val(T),
  170.         ///Raw bits
  171.         Res(U),
  172.     }
  173.  
  174.     impl<U, REG> W<U, REG>
  175.     where
  176.         Self: Safety<S=Safe>
  177.     {
  178.         ///Writes raw bits to the register
  179.         #[inline(always)]
  180.         pub fn bits(&mut self, bits: U) -> &mut Self {
  181.             self.bits = bits;
  182.             self
  183.         }
  184.     }
  185.  
  186.     impl<U, REG> W<U, REG>
  187.     where
  188.         Self: Safety<S=Unsafe>
  189.     {
  190.         ///Writes raw bits to the register
  191.         #[inline(always)]
  192.         pub unsafe fn bits(&mut self, bits: U) -> &mut Self {
  193.             self.bits = bits;
  194.             self
  195.         }
  196.     }
  197.  
  198.     /// Mask of field
  199.     pub trait Mask {
  200.         type Type;
  201.         const MASK: Self::Type;
  202.     }
  203.  
  204.     ///Marker struct for register/field with safe write
  205.     pub struct Safe;
  206.     ///Marker struct for register/field with unsafe write
  207.     pub struct Unsafe;
  208.  
  209.     pub trait Safety {
  210.         ///Safety type: `Safe` or `Unsafe`
  211.         type S;
  212.     }
  213.  
  214.     ///Writer Proxy
  215.     pub struct WProxy<'a, U, REG, N, FI> {
  216.         w: &'a mut W<U, REG>,
  217.         offset: u8,
  218.         _field: marker::PhantomData<(FI, N)>,
  219.     }
  220.  
  221.     impl<'a, U, REG, N, FI> WProxy<'a, U, REG, N, FI> {
  222.         pub(crate) fn new(w: &'a mut W<U, REG>, offset: u8) -> Self {
  223.             Self {
  224.                 w,
  225.                 offset,
  226.                 _field: marker::PhantomData,
  227.             }
  228.         }
  229.     }
  230.  
  231.     impl<'a, U, REG, FI> WProxy<'a, U, REG, bool, FI>
  232.     where
  233.         U: Copy + SetBit
  234.     {
  235.         /// Sets the field bit"]
  236.         pub fn set_bit(self) -> &'a mut W<U, REG> {
  237.             self.bit(true)
  238.         }
  239.         /// Clears the field bit"]
  240.         pub fn clear_bit(self) -> &'a mut W<U, REG> {
  241.             self.bit(false)
  242.         }
  243.         /// Writes raw bits to the field"]
  244.         #[inline(always)]
  245.         pub fn bit(self, value: bool) -> &'a mut W<U, REG> {
  246.             self.w.bits.setbit(self.offset, value);
  247.             self.w
  248.         }
  249.     }
  250.  
  251.     impl<'a, U, REG, FI> WProxy<'a, U, REG, bool, FI>
  252.     where
  253.         FI: ToBits<bool>,
  254.         U: Copy + SetBit,
  255.     {
  256.         ///Writes `variant` to the field
  257.         #[inline]
  258.         pub fn variant(self, variant: FI) -> &'a mut W<U, REG> {
  259.             self.bit(variant._bits())
  260.         }
  261.     }
  262.  
  263.     impl<'a, U, REG, N, FI> WProxy<'a, U, REG, N, FI>
  264.     where
  265.         Self: Safety<S=Safe>,
  266.         FI: Mask<Type=U>,
  267.         U: Copy + SetBits<N>,
  268.     {
  269.         ///Writes raw bits to the field"]
  270.         #[inline]
  271.         pub fn bits(self, value: N) -> &'a mut W<U, REG> {
  272.             self.w.bits.setbits(FI::MASK, self.offset, value);
  273.             self.w
  274.         }
  275.     }
  276.  
  277.     impl<'a, U, REG, N, FI> WProxy<'a, U, REG, N, FI>
  278.     where
  279.         Self: Safety<S=Unsafe>,
  280.         FI: Mask<Type=U>,
  281.         U: Copy + SetBits<N>,
  282.     {
  283.         ///Writes raw bits to the field"]
  284.         #[inline]
  285.         pub unsafe fn bits(self, value: N) -> &'a mut W<U, REG> {
  286.             self.w.bits.setbits(FI::MASK, self.offset, value);
  287.             self.w
  288.         }
  289.     }
  290.  
  291.     impl<'a, U, REG, N, FI> WProxy<'a, U, REG, N, FI>
  292.     where
  293.         Self: Safety<S=Safe>,
  294.         FI: Mask<Type=U> + ToBits<N>,
  295.         U: Copy + SetBits<N>,
  296.     {
  297.         ///Writes `variant` to the field
  298.         #[inline]
  299.         pub fn variant(self, variant: FI) -> &'a mut W<U, REG> {
  300.             self.bits(variant._bits())
  301.         }
  302.     }
  303.  
  304.     impl<'a, U, REG, N, FI> WProxy<'a, U, REG, N, FI>
  305.     where
  306.         Self: Safety<S=Unsafe>,
  307.         FI: Mask<Type=U> + ToBits<N>,
  308.         U: Copy + SetBits<N>,
  309.     {
  310.         ///Writes `variant` to the field
  311.         #[inline]
  312.         pub fn variant(self, variant: FI) -> &'a mut W<U, REG> {
  313.             unsafe { self.bits(variant._bits()) }
  314.         }
  315.     }
  316.  
  317.     pub trait SetBits<N> {
  318.         fn setbits(&mut self, mask: Self, offset: u8, value: N);
  319.     }
  320.  
  321.     pub trait SetBit {
  322.         fn setbit(&mut self, offset: u8, value: bool);
  323.     }
  324.  
  325.     macro_rules! impl_set_bits {
  326.         ($U:ty : $($N:ty),+) => {
  327.             impl SetBit for $U {
  328.                 #[inline(always)]
  329.                 fn setbit(&mut self, offset: u8, value: bool) {
  330.                     *self = *self & !(1 << offset) | ((value as Self) << offset)
  331.                 }
  332.             }
  333.             $(
  334.                 impl SetBits<$N> for $U {
  335.                     #[inline(always)]
  336.                     fn setbits(&mut self, mask: Self, offset: u8, value: $N) {
  337.                         *self = *self & !(mask << offset) | (((value as Self) & mask) << offset)
  338.                     }
  339.                 }
  340.             )+
  341.         }
  342.     }
  343.  
  344.     impl_set_bits!(u64: u64, u32, u16, u8);
  345.     impl_set_bits!(u32: u32, u16, u8);
  346.     impl_set_bits!(u16: u16, u8);
  347.     impl_set_bits!(u8: u8);
  348. }
  349.  
  350. pub use generic::*;
  351.  
  352. // --------- USAGE: ------------------------
  353.  
  354. pub mod pac {
  355.     pub struct _CR1;
  356.     pub type CR1 = crate::Reg<u32, _CR1>;
  357.  
  358.     impl crate::Readable for CR1 {}
  359.     impl crate::Writable for CR1 {}
  360.  
  361.     impl crate::RegSize for CR1 {
  362.         type Type = u32;
  363.     }
  364.  
  365.     impl crate::ResetValue for CR1 {
  366.         #[inline(always)]
  367.         fn reset_value() -> Self::Type { 0x3 }
  368.     }
  369.  
  370.     #[allow(non_camel_case_types)]
  371.     pub mod cr1 {
  372.  
  373.         pub type R = crate::R<u32, super::CR1>;
  374.         pub type W = crate::W<u32, super::CR1>;
  375.  
  376.         impl crate::Safety for W {
  377.             type S = crate::Unsafe;
  378.         }
  379.  
  380.         pub type CKD_R = crate::R<u8, CKD_A>;
  381.         pub type CKD_W<'a> = crate::WProxy<'a, u32, super::CR1, u8, CKD_A>;
  382.  
  383.         impl crate::Safety for CKD_W<'_> {
  384.             type S = crate::Unsafe;
  385.         }
  386.  
  387.         impl crate::Mask for CKD_A {
  388.             type Type = u32;
  389.             const MASK: u32 = 0x03;
  390.         }
  391.  
  392.         pub type CEN_R = crate::R<bool, CEN_A>;
  393.         pub type CEN_W<'a> = crate::WProxy<'a, u32, super::CR1, bool, CEN_A>;
  394.  
  395.         pub type CEN2_R = CEN_R;
  396.         pub type CEN2_W<'a> = CEN_W<'a>;
  397.  
  398.         pub type CB_R = crate::R<u8, u8>;
  399.  
  400.         impl W {
  401.             #[doc = "Bits 8:9 - Clock division"]
  402.             #[inline(always)]
  403.             pub fn ckd(&mut self) -> CKD_W {
  404.                 CKD_W::new(self, 8)
  405.             }
  406.             #[doc = "Bit 0 - Counter enable"]
  407.             #[inline(always)]
  408.             pub fn cen(&mut self) -> CEN_W {
  409.                 CEN_W::new(self, 0)
  410.             }
  411.             #[doc = "Bit 0 - Counter enable"]
  412.             #[inline(always)]
  413.             pub fn cen2(&mut self) -> CEN2_W {
  414.                 CEN2_W::new(self, 5)
  415.             }
  416.         }
  417.  
  418.         impl R {
  419.             #[doc = "Bits 8:9 - Clock division"]
  420.             #[inline(always)]
  421.             pub fn ckd(&self) -> CKD_R {
  422.                 CKD_R::new(((self.bits() >> 8) & 0x03) as u8)
  423.             }
  424.             #[doc = "Bit 0 - Counter enable"]
  425.             #[inline(always)]
  426.             pub fn cen(&self) -> CEN_R {
  427.                 CEN_R::new(((self.bits() >> 0) & 0x01) != 0)
  428.             }
  429.             #[doc = "Bit 0 - Counter enable"]
  430.             #[inline(always)]
  431.             pub fn cen2(&self) -> CEN2_R {
  432.                 CEN2_R::new(((self.bits() >> 5) & 0x01) != 0)
  433.             }
  434.             #[doc = "Bit 0 - Counter enable"]
  435.             #[inline(always)]
  436.             pub fn cb(&self) -> CB_R {
  437.                 CB_R::new(((self.bits() >> 15) & 0x07) as u8)
  438.             }
  439.         }
  440.  
  441.         #[doc = "Values that can be written to the field `CKD`"]
  442.         #[derive(Clone, Copy, Debug, PartialEq)]
  443.         pub enum CKD_A {
  444.             #[doc = "t_DTS = t_CK_INT"]
  445.             DIV1,
  446.             #[doc = "t_DTS = 2 \u{d7} t_CK_INT"]
  447.             DIV2,
  448.             #[doc = "t_DTS = 4 \u{d7} t_CK_INT"]
  449.             DIV4,
  450.         }
  451.  
  452.         impl crate::ToBits<u8> for CKD_A {
  453.             #[allow(missing_docs)]
  454.             #[doc(hidden)]
  455.             #[inline(always)]
  456.             fn _bits(&self) -> u8 {
  457.                 match *self {
  458.                     CKD_A::DIV1 => 0,
  459.                     CKD_A::DIV2 => 1,
  460.                     CKD_A::DIV4 => 2,
  461.                 }
  462.             }
  463.         }
  464.  
  465.         impl CKD_R {
  466.             #[doc = "Checks if the value of the field is `DIV1`"]
  467.             #[inline(always)]
  468.             pub fn is_div1(&self) -> bool {
  469.                 *self == CKD_A::DIV1
  470.             }
  471.             #[doc = "Checks if the value of the field is `DIV2`"]
  472.             #[inline(always)]
  473.             pub fn is_div2(&self) -> bool {
  474.                 *self == CKD_A::DIV2
  475.             }
  476.             #[doc = "Checks if the value of the field is `DIV4`"]
  477.             #[inline(always)]
  478.             pub fn is_div4(&self) -> bool {
  479.                 *self == CKD_A::DIV4
  480.             }
  481.         }
  482.  
  483.         impl<'a> CKD_W<'a> {
  484.             #[doc = "t_DTS = t_CK_INT"]
  485.             #[inline(always)]
  486.             pub fn div1(self) -> &'a mut W {
  487.                 self.variant(CKD_A::DIV1)
  488.             }
  489.             #[doc = "t_DTS = 2 \u{d7} t_CK_INT"]
  490.             #[inline(always)]
  491.             pub fn div2(self) -> &'a mut W {
  492.                 self.variant(CKD_A::DIV2)
  493.             }
  494.             #[doc = "t_DTS = 4 \u{d7} t_CK_INT"]
  495.             #[inline(always)]
  496.             pub fn div4(self) -> &'a mut W {
  497.                 self.variant(CKD_A::DIV4)
  498.             }
  499.         }
  500.  
  501.  
  502.  
  503.         #[doc = "Possible values of the field `CEN`"]
  504.         #[derive(Clone, Copy, Debug, PartialEq)]
  505.         pub enum CEN_A {
  506.             #[doc = "Counter disabled"]
  507.             DISABLED,
  508.             #[doc = "Counter enabled"]
  509.             ENABLED,
  510.         }
  511.  
  512.         impl crate::ToBits<bool> for CEN_A {
  513.             #[inline(always)]
  514.             fn _bits(&self) -> bool {
  515.                 match *self {
  516.                     CEN_A::DISABLED => false,
  517.                     CEN_A::ENABLED => true,
  518.                 }
  519.             }
  520.         }
  521.         impl CEN_R {
  522.             #[doc = "Checks if the value of the field is `DISABLED`"]
  523.             #[inline(always)]
  524.             pub fn is_disabled(&self) -> bool {
  525.                 *self == CEN_A::DISABLED
  526.             }
  527.             #[doc = "Checks if the value of the field is `ENABLED`"]
  528.             #[inline(always)]
  529.             pub fn is_enabled(&self) -> bool {
  530.                 *self == CEN_A::ENABLED
  531.             }
  532.         }
  533.  
  534.         impl<'a> CEN_W<'a> {
  535.             #[doc = "Counter disabled"]
  536.             #[inline(always)]
  537.             pub fn disabled(self) -> &'a mut W {
  538.                 self.variant(CEN_A::DISABLED)
  539.             }
  540.             #[doc = "Counter enabled"]
  541.             #[inline(always)]
  542.             pub fn enabled(self) -> &'a mut W {
  543.                 self.variant(CEN_A::ENABLED)
  544.             }
  545.         }
  546.     }
  547. }
  548.  
  549. fn main() {
  550.     let reg: pac::CR1 = crate::Reg {
  551.         register: vcell::VolatileCell::new(12),
  552.         _marker: core::marker::PhantomData,
  553.     }; // only for test. Will be get from periph
  554.  
  555.     reg.modify(|_,w| w
  556.         .cen2() .set_bit()
  557.         .ckd() .div2()
  558.     );
  559.  
  560.     let r = reg.read();
  561.     println!("{}", r.cen2().bit_is_set());
  562.     assert_eq!(r.bits(), 300);
  563.     println!("{}", r.cen().bit_is_set());
  564.     println!("{}", r.ckd().is_div4());
  565.     reg.reset();
  566.     assert_eq!(reg.read().bits(), 3);
  567. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top