-
- #ifndef __URIL_REG_OBJECT_HPP__
- #define __URIL_REG_OBJECT_HPP__
- #ifdef __cplusplus //仅在C++使用
- #include "uni_int.h"
- #include "util_macro.h"
- #include "iar_macro.h"
- template <typename T> class REG_OBJECT_t;
- template <typename T,uint8 bit_start> class REG_BIT_OBJECT_t;
- template <typename T,uint8 bit_start,uint8 bit_size> class REG_FIELD_OBJECT_t;
- template <typename T> class REG_OBJECT_t
- {
- public:
- __always_inline__ static REG_OBJECT_t<T> &Attach(volatile T * REG_ADDR)
- {
- return *(REG_OBJECT_t<T> *)REG_ADDR;
- }
-
- __always_inline__ REG_OBJECT_t<T> &ToREG_OBJECT_t()
- {
- return *(REG_OBJECT_t<T> *)®
- }
- template <uint8 b1>
- __inline__ REG_BIT_OBJECT_t<T,b1> &ToREG_BIT_OBJECT_t()
- {
- return *(REG_BIT_OBJECT_t<T,b1> *)®
- }
-
- template <uint8 b1,uint8 s>
- __inline__ REG_FIELD_OBJECT_t<T,b1,s> &ToREG_FIELD_OBJECT_t()
- {
- return *(REG_FIELD_OBJECT_t<T,b1,s> *)®
- }
-
-
- union
- {
- T reg;
- volatile T Reg;
- };
-
-
-
- __always_inline__ REG_OBJECT_t<T> &set_bit(uint8 bit_start)
- {
- reg |= 1U<<bit_start;
- return *this;
- }
- __always_inline__ REG_OBJECT_t<T> &Set_Bit(uint8 bit_start)
- {
- Reg |= 1U<<bit_start;
- return *this;
- }
-
- __always_inline__ REG_OBJECT_t<T> &clr_bit(uint8 bit_start)
- {
- reg &= ~(1U<<bit_start);
- return *this;
- }
-
- __always_inline__ REG_OBJECT_t<T> &Clr_Bit(uint8 bit_start)
- {
- Reg &= ~(1U<<bit_start);
- return *this;
- }
-
- __always_inline__ REG_OBJECT_t<T> &toggle_bit(uint8 bit_start)
- {
- reg ^= 1U<<bit_start;
- return *this;
- }
-
- __always_inline__ REG_OBJECT_t<T> &Toggle_Bit(uint8 bit_start)
- {
- Reg ^= 1U<<bit_start;
- return *this;
- }
-
- __always_inline__ REG_OBJECT_t<T> &write_bit(uint8 bit_start,T value)
- {
- if(value)
- {
- reg |= 1U<<bit_start;
- }
- else
- {
- reg &= ~(1U<<bit_start);
- }
- return *this;
- }
- __always_inline__ REG_OBJECT_t<T> &Write_Bit(uint8 bit_start,T value)
- {
- if(value)
- {
- Reg |= 1U<<bit_start;
- }
- else
- {
- Reg &= ~(1U<<bit_start);
- }
- return *this;
- }
- __always_inline__ REG_OBJECT_t<T> &read_bit(uint8 bit_start,T &value)
- {
-
- if(reg & (1U<<bit_start))
- {
- value=1;
- }
- else
- {
- value=0;
- }
-
- return *this;
- }
- __always_inline__ REG_OBJECT_t<T> &Read_Bit(uint8 bit_start,T &value)
- {
-
- if(Reg & (1U<<bit_start))
- {
- value=1;
- }
- else
- {
- value=0;
- }
-
- return *this;
- }
-
- __always_inline__ REG_OBJECT_t<T> &write_field(uint8 bit_start,uint8 bit_size,T value)
- {
- T temp_reg=this->reg;
- temp_reg &= ~_BVF_DATA(~value,bit_start,bit_size);
- temp_reg |= _BVF_DATA(value,bit_start,bit_size);
- this->reg = temp_reg;
-
- return *this;
- }
-
- __always_inline__ REG_OBJECT_t<T> &Write_Field(uint8 bit_start,uint8 bit_size,T value)
- {
- T temp_reg=this->Reg;
- temp_reg &= ~_BVF_DATA(~value,bit_start,bit_size);
- temp_reg |= _BVF_DATA(value,bit_start,bit_size);
- this->Reg = temp_reg;
-
- return *this;
- }
-
- __always_inline__ REG_OBJECT_t<T> &read_field(uint8 bit_start,uint8 bit_size,T &value)
- {
- value= (reg & _BVF(bit_start,bit_size))>>bit_start;
-
- return *this;
- }
-
- __always_inline__ REG_OBJECT_t<T> &Read_Field(uint8 bit_start,uint8 bit_size,T &value)
- {
- value= (Reg & _BVF(bit_start,bit_size))>>bit_start;
-
- return *this;
- }
-
- };
- template <typename T,uint8 b> class REG_BIT_OBJECT_t
- {
- public:
- __always_inline__ static REG_BIT_OBJECT_t<T,b> &Attach(volatile T * REG_ADDR)
- {
- return *(REG_BIT_OBJECT_t<T,b> *)REG_ADDR;
- }
-
- __always_inline__ REG_OBJECT_t<T> &ToREG_OBJECT_t()
- {
- return *(REG_OBJECT_t<T> *)®
- }
- template <uint8 b1>
- __inline__ REG_BIT_OBJECT_t<T,b1> &ToREG_BIT_OBJECT_t()
- {
- return *(REG_BIT_OBJECT_t<T,b1> *)®
- }
-
- template <uint8 b1,uint8 s>
- __inline__ REG_FIELD_OBJECT_t<T,b1,s> &ToREG_FIELD_OBJECT_t()
- {
- return *(REG_FIELD_OBJECT_t<T,b1,s> *)®
- }
-
- union
- {
- T reg;
- volatile T Reg;
- };
-
- __always_inline__ REG_BIT_OBJECT_t<T,b> &set()
- {
- reg |= 1U<<b;
- return *this;
- }
-
- __always_inline__ REG_BIT_OBJECT_t<T,b> &Set()
- {
- Reg |= 1U<<b;
- return *this;
- }
-
- __always_inline__ REG_BIT_OBJECT_t<T,b> &clr()
- {
- reg &= ~(1U<<b);
- return *this;
- }
-
- __always_inline__ REG_BIT_OBJECT_t<T,b> &Clr()
- {
- Reg &= ~(1U<<b);
- return *this;
- }
-
- __always_inline__ REG_BIT_OBJECT_t<T,b> &toggle()
- {
- reg ^= 1U<<b;
- return *this;
- }
-
- __always_inline__ REG_BIT_OBJECT_t<T,b> &Toggle()
- {
- Reg ^= 1U<<b;
- return *this;
- }
-
- __always_inline__ REG_BIT_OBJECT_t<T,b> &write(T value)
- {
- if(value)
- {
- reg |= 1U<<b;
- }
- else
- {
- reg &= ~(1U<<b);
- }
- return *this;
- }
- __always_inline__ REG_BIT_OBJECT_t<T,b> &Write(T value)
- {
- if(value)
- {
- Reg |= 1U<<b;
- }
- else
- {
- Reg &= ~(1U<<b);
- }
- return *this;
- }
- __always_inline__ REG_BIT_OBJECT_t<T,b> &read(T &value)
- {
-
- if(reg & (1U<<b))
- {
- value=1;
- }
- else
- {
- value=0;
- }
-
- return *this;
- }
- __always_inline__ REG_BIT_OBJECT_t<T,b> &Read(T &value)
- {
-
- if(Reg & (1U<<b))
- {
- value=1;
- }
- else
- {
- value=0;
- }
-
- return *this;
- }
- };
- template <typename T,uint8 bit_start,uint8 bit_size> class REG_FIELD_OBJECT_t
- {
- public:
-
- __always_inline__ static REG_FIELD_OBJECT_t<T,bit_start,bit_size> &Attach(volatile T * REG_ADDR)
- {
- return *(REG_FIELD_OBJECT_t<T,bit_start,bit_size> *)REG_ADDR;
- }
-
- __always_inline__ REG_OBJECT_t<T> &ToREG_OBJECT_t()
- {
- return *(REG_OBJECT_t<T> *)®
- }
- template <uint8 b1>
- __inline__ REG_BIT_OBJECT_t<T,b1> &ToREG_BIT_OBJECT_t()
- {
- return *(REG_BIT_OBJECT_t<T,b1> *)®
- }
-
- template <uint8 b1,uint8 s>
- __inline__ REG_FIELD_OBJECT_t<T,b1,s> &ToREG_FIELD_OBJECT_t()
- {
- return *(REG_FIELD_OBJECT_t<T,b1,s> *)®
- }
-
-
- union
- {
- T reg;
- volatile T Reg;
- };
-
-
- __always_inline__ REG_FIELD_OBJECT_t<T,bit_start,bit_size> &write(T value)
- {
- T temp_reg=this->reg;
- temp_reg &= ~_BVF_DATA(~value,bit_start,bit_size);
- temp_reg |= _BVF_DATA(value,bit_start,bit_size);
- this->reg = temp_reg;
- return *this;
- }
-
- __always_inline__ REG_FIELD_OBJECT_t<T,bit_start,bit_size> &Write(T value)
- {
- T temp_reg=this->Reg;
- temp_reg &= ~_BVF_DATA(~value,bit_start,bit_size);
- temp_reg |= _BVF_DATA(value,bit_start,bit_size);
- this->Reg = temp_reg;
- return *this;
- }
-
- __always_inline__ REG_FIELD_OBJECT_t<T,bit_start,bit_size> &read(T &value)
- {
- value= (reg & _BVF(bit_start,bit_size))>>bit_start;
- return *this;
- }
-
- __always_inline__ REG_FIELD_OBJECT_t<T,bit_start,bit_size> &Read(T &value)
- {
- value=(Reg & _BVF(bit_start,bit_size))>>bit_start;
-
- return *this;
- }
-
- };
- #endif
- #endif
-
- #include "proj_incs.h"
- #include "util_reg_object.hpp"
- uint8 io;
- uint8 x;
- void test_io1a(void)
- {
- REG_OBJECT_t<uint8>::Attach(&io).set_bit(0).clr_bit(1).write_field(2,3,7).ToREG_BIT_OBJECT_t<5>().set().clr().toggle().ToREG_FIELD_OBJECT_t<6,2>().write(2);
- }
- void test_io1b(void)
- {
- REG_OBJECT_t<uint8>::Attach(&io).Set_Bit(0).Clr_Bit(1).Write_Field(2,3,7).ToREG_BIT_OBJECT_t<5>().Set().Clr().Toggle().ToREG_FIELD_OBJECT_t<6,2>().Write(2);
- }
- void test_io2a(void)
- {
- REG_BIT_OBJECT_t<uint8,2+3>::Attach(&io).write(1).write(0).toggle().read(x).write(~x);
- //优化用小写
- }
- void test_io2b(void)
- {
- REG_BIT_OBJECT_t<uint8,2>::Attach(&io).Write(1).Write(0).Toggle().Read(x).Write(~x);
- //防优化用大写
- }
- void test_io3a(void)
- {
- REG_FIELD_OBJECT_t<uint8,2,3>::Attach(&io).write(3).write(4).read(x).write(~x);
- //优化用小写
- }
- void test_io3b(void)
- {
- REG_FIELD_OBJECT_t<uint8,2,3>::Attach(&io).Write(3).Write(4).Read(x).Write(~x);
- //防优化用大写
- }
- int main()
- {
- test_io1a();
- test_io1b();
- test_io2a();
- test_io2b();
- test_io3a();
- test_io3b();
-
- while(1);
- }