Union in bit field for mcu register mapping in C++

403 views Asked by At

I have been developing software drivers for the hw peripherals implemented in the field programmable gate array on this platform. I have been using C++ programming language and arm-none-eabi-gcc compiler.

One of the peripherals has following register map

Register            Offset in bytes in          Bits description
                respect to the base address     
========================================================================================
REGISTER_0              0x00                         0-15
                                                    16-31
----------------------------------------------------------------------------------------
REGISTER_1              0x04                         0-15
----------------------------------------------------------------------------------------
REGISTER_2              0x08                         0-15
----------------------------------------------------------------------------------------
REGISTER_3              0x0C                         0-15
                                                    16-31
----------------------------------------------------------------------------------------
CONTROL_REG             0x10                         0: enable
                                                     1: reset
                                                     2: mask_00 
                                                     3: mask_01
                                                     4: mask_02
                                                     5: mask_03
                                                     6: mask_04
                                                     7: mask_05 
                                                     8: mask_06
                                                     9: mask_07                              
----------------------------------------------------------------------------------------
REGISTER_5              0x14                         0-15
                                                    16-31
----------------------------------------------------------------------------------------
REGISTER_6              0x18                         0-15
                                                    16
----------------------------------------------------------------------------------------
REGISTER_7              0x1C                         0-15
                                                    16
----------------------------------------------------------------------------------------
REGISTER_8              0x20                         0-31
----------------------------------------------------------------------------------------

For the register CONTROL_REG it would be convenient for me to have a possibility to work with individual mask bits along with all the mask bits at once. So I have decided to use union in bit field

int main(int argc, char** argv) {

    struct ControlReg
    {
        uint32_t enable_bit: 1;
        uint32_t reset_bit:  1;
        union
        {
            struct
            {
                uint32_t bit_0: 1;
                uint32_t bit_1: 1;
                uint32_t bit_2: 1;
                uint32_t bit_3: 1;
                uint32_t bit_4: 1;
                uint32_t bit_5: 1;
                uint32_t bit_6: 1;
                uint32_t bit_7: 1;
            }bits;
            uint8_t byte;
       }mask;
   };

   ControlReg control_reg;
   control_reg.mask.byte = 0xAA;
    
   cout << "Mask bit 0: " << control_reg.mask.bits.bit_0 << endl;
   cout << "Mask bit 1: " << control_reg.mask.bits.bit_1 << endl;        
   cout << "Mask bit 2: " << control_reg.mask.bits.bit_2 << endl;
   cout << "Mask bit 3: " << control_reg.mask.bits.bit_3 << endl;        
   cout << "Mask bit 4: " << control_reg.mask.bits.bit_4 << endl;
   cout << "Mask bit 5: " << control_reg.mask.bits.bit_5 << endl;        
   cout << "Mask bit 6: " << control_reg.mask.bits.bit_6 << endl;
   cout << "Mask bit 7: " << control_reg.mask.bits.bit_7 << endl;
}

I have tested that and it works. Nevertheless I have doubts whether this construction is correct. Can anybody tell me whether the approach I have chosen is correct solution for my problem? Thanks in advance.

0

There are 0 answers