Page 1 of 1

SFR Structure/Union question

PostPosted: Fri Dec 29, 2017 2:31 am
by jmusselman64
How would I do something like this in Firewing? This is a fragment of a 'C' PIC24 .h file:
Code: Select all
#define CLKDIV CLKDIV
extern volatile uint16_t  CLKDIV __attribute__((__sfr__));
__extension__ typedef struct tagCLKDIVBITS {
  union {
    struct {
      uint16_t :5;
      uint16_t PLLEN:1;
      uint16_t :2;
      uint16_t RCDIV:3;
      uint16_t DOZEN:1;
      uint16_t DOZE:3;
      uint16_t ROI:1;
    };
    struct {
      uint16_t :8;
      uint16_t RCDIV0:1;
      uint16_t RCDIV1:1;
      uint16_t RCDIV2:1;
      uint16_t :1;
      uint16_t DOZE0:1;
      uint16_t DOZE1:1;
      uint16_t DOZE2:1;
    };
  };
} CLKDIVBITS;
extern volatile CLKDIVBITS CLKDIVbits __attribute__((__sfr__));

I can translate the basic structure part...something like this:
Code: Select all
Structure CLKDIVBITS
     dim value as ushort
     dim PLLEN as value.5
     dim RCDIV0 as value.8
     dim RCDIV1 as value.9
     dim RCDIV2 as value.10
     dim DOZEN as value.11
     dim DOZE0 as value.12
     dim DOZE1 as value.13
     dim DOZE2 as value.14
     dim ROI as value.15
end structure
dim CLKDIVbits as CLKDIVBITS

But how do I 'Union' in the 3-bit values (0-7) for the DOZE and RCDIV so I can do either:
Code: Select all
CLKDIVbits.RCDIV = 3

or individually as:
Code: Select all
CLKDIVbits.RCDIV0 = 1
CLKDIVbits.RCDIV1 = 1
CLKDIVbits.RCDIV2 = 0

Obviously this would be applicable to many SFR's so I'm trying to get a grasp on the concept before I proceed...

Thanks,
Jerry

Re: SFR Structure/Union question

PostPosted: Fri Dec 29, 2017 11:45 am
by Jerry Messina
But how do I 'Union' in the 3-bit values (0-7) for the DOZE and RCDIV

You can't. Firewing doesn't support the multiple bit field found in C, so you have to use the single bits.

Also, the following won't work:
Code: Select all
dim CLKDIVbits as CLKDIVBITS

Firewing isn't case-sensitive so you'll have to have a different name for the structure type declaration vs the actual variable definition, something like...
Code: Select all
Structure T_CLKDIVBITS
     dim value as ushort
end structure
dim CLKDIVbits as T_CLKDIVBITS