0

I am somewhat new to C in regards to structures, unions and bitfields and need help. Below is the SLPH_CONTROL which will "control" the bits in the destination of DRVCONF0 and DRVCONF1. After the code snippet I will explain what I need.

/* SLPH_CONTROL **********************************************/

typedef union
{
    struct
    {
        unsigned int SH0_BIT0 :1;
        unsigned int SH0_BIT1 :1;
    };
    uint8 SLPH_CONTROL_byte;
} SLPH_CONTROL;

SLPH_CONTROL my_SLPH_CONTROL;

/* DRVCONF0 **********************************************/

typedef union
{
    struct
    {
        unsigned int DF0_BIT0 :1;
        unsigned int DF0_BIT1 :1;
        unsigned int DF0_BIT2 :1;
        unsigned int DF0_BIT3 :1;
        unsigned int DF0_BIT4 :1;
    };
    uint8 DRVCONF0_byte;
} DRVCONF0;

DRVCONF0 my_DRVCONF0;

/* DRVCONF0 **********************************************/

typedef union
{
    struct
    {
        unsigned int DF1_BIT0 :1;
        unsigned int DF1_BIT1 :1;
        unsigned int DF1_BIT2 :1;
        unsigned int DF1_BIT3 :1;
        unsigned int DF1_BIT4 :1;
    };
    uint8 DRVCONF1_byte;
} DRVCONF1;

DRVCONF1 my_DRVCONF1;

Now with the code above I want to link or copy data from and to like so:

SH0_BIT0:1 to DF0_BIT4:1;
SH1_BIT1:1 to DF1_BIT0:1;

Is there a way to do this? I am really lost and looked everywhere online for a solution to this. Any suggestions are welcome!!!

Thanks,

Eric

LPs
  • 16,045
  • 8
  • 30
  • 61
Eric
  • 1
  • The only way to do that would be with a couple of assignment statements. Changes to the `SH0` bits cannot be automagically reflected in the corresponding `DFx` bits. And there is no way to create a pointer to a bitfield. – user3386109 Feb 07 '17 at 06:49
  • If you are by any chance using bitfields as a map to individual register bits, take a look here http://stackoverflow.com/questions/41976147/cast-char-to-a-bit-field-entry-of-4-bits#comment71130153_41976147 – StoryTeller - Unslander Monica Feb 07 '17 at 06:56
  • `my_SLPH_CONTROL.SH0_BIT0 = my_DRVCONF1.DF0_BIT4;` etc.. Put it in a function that takes pointers to unions as arguments. – 2501 Feb 07 '17 at 11:27
  • Thanks everyone for your input. I did what you mentioned 2501 which is: – Eric Feb 07 '17 at 14:06
  • Thanks everyone for your input. 2501: I did this before I posted but did not think to put it in a function. How do you recommend I put this in a function 2501? my_SLPH_CONTROL.SH0_BIT0 = my_DRVCONF1.DF0_BIT4; yes the naming scheme is atrocious but wanted to throw something together quickly to test an idea before implementing it. This is for an embedded microcontroller application using GCC C99... – Eric Feb 07 '17 at 14:20

1 Answers1

0

Maybe this is possible on special embedded H/W (I don't know) but not on a regular PC with standard C.

Bit fields describe individual bits in one or more consecutive bytes. Actually, bytes are addressable but bits are not. The read/write access to bits is internally solved by bit-wise operations (like in C expressed with &, |, ^, ~).

my_DRVCONF0 and my_DRVCONF1 are individual variables in your sample. Thus, it's even not granted that they will be allocated/stored in consecutive memory. (They definitely will not if some kind of word-alignment is enabled.)

You probably have to make the control more sophisticated (e.g. providing pairs of address and bit index) to do what's intended.

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56