1

I made a bit-declaration for a dspic30f4011

I declared a part

typedef struct tagCxTXxSIDBITS_tagCxTXxEIDBITS {
    unsigned        : 2;
    unsigned SRC7_2 : 6;
    unsigned        : 8;
    unsigned        : 14;
    unsigned SRC1_0 : 2;
} CxTXxSRCBITS;

extern volatile unsigned int C1TX0SRC __attribute__((__sfr__));
extern volatile CxTXxSRCBITS C1TX0SRCbits __attribute__((__sfr__));
extern volatile unsigned int C1TX1SRC __attribute__((__sfr__));
extern volatile CxTXxSRCBITS C1TX1SRCbits __attribute__((__sfr__));
extern volatile unsigned int C1TX2SRC __attribute__((__sfr__));
extern volatile CxTXxSRCBITS C1TX2SRCbits __attribute__((__sfr__)); 

Is this correct? Are the first two the CxTXxSID bits 0 and 1? Then CxTXxSID bits 2-7 the 8 are CxTXxSID bits 8-15 are the 14 for CxTXxEID bits 0-13 ? And the last two for CxTXxEID bits 14 and 15?

If it is, I made it right

If I write in my code

C1TX0SRC = 0x0001;

do I get following in the registers ?

C1TX0SID = 0b0000000000000000
C1TX0EID = 0b0100000000000000
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Johannes
  • 31
  • 1

2 Answers2

1

The bit order isn't specified by the standard, nor is the byte order, nor is anything else regarding bit fields, see this.

Are the first two the CxTXxSID bits 0 and 1?

Nobody knows, since there are no guarantees by the standard. You have to check your compiler documentation for this specific system.

Then CxTXxSID bits 2-7 the 8 are CxTXxSID bits 8-15 are the 14 for CxTXxEID bits 0-13 ? And the last two for CxTXxEID bits 14 and 15?

Nobody knows. Bit order, byte order, endianess, bit/byte padding... everything can be an issue and nothing is specified by the standard.

do I get following in the registers ?

Nobody knows. You'll have to check the compiler documentation. In practice you can get any binary goo as result. The portable, safe solution is to not use bit fields at all.

Community
  • 1
  • 1
Lundin
  • 195,001
  • 40
  • 254
  • 396
0

the bit order should be correct. But why don't you test it by yourself? I did something like this some days ago:

    #include <stdint.h>
    typedef union {
    uint8_t byte;
    struct {
        unsigned bit0:1;
        unsigned bit1:1;
        unsigned bit2:1;
        unsigned bit3:1;
        unsigned bit4:1;
        unsigned bit5:1;
        unsigned bit6:1;
        unsigned bit7:1;
    };
    } byte_t;


/*
                         Main application
 */
void main(void)
{
    byte_t testbyte;
    testbyte.byte = 0b01100011;
    while (1)
    {
        uint8_t bit0 = testbyte.bit0;
        uint8_t bit1 = testbyte.bit1;
        uint8_t bit2 = testbyte.bit2;
        uint8_t bit3 = testbyte.bit3;
        uint8_t bit4 = testbyte.bit4;
        uint8_t bit5 = testbyte.bit5;
        uint8_t bit6 = testbyte.bit6;
        uint8_t bit7 = testbyte.bit7;

        uint8_t temp  = 0;
    }
}

you can test this in the mplabx simulator, don't even need hardware. just set a debug breakpoint at the end.

EDIT: I may have been a little too fast with my answer. After rereading your question I understand that your main concern was not the bit order but the question if your struct will align to your registers. I do not understand how the sfr attribute works. In xc8 there is the "@" operator which can assign some value to a specific memory region. But you could use the debugger to get the actual address where you write your values to.

EDIT of the EDIT: Now I have some understanding of the sfr attribute. As you did not state what you did I assume you haven't changed your linker file, did you? To place your struct on top of the special function registers, you have to change your linker script (*.gld file) found in Microchip\xc16\vx.xx\support\dsPIC30F. Here is a little extract from the standard p30F4011.gld linker script:

 C1TX2SID     = 0x340;
_C1TX2SID     = 0x340;
_C1TX2SIDbits = 0x340;
 C1TX2EID     = 0x342;
_C1TX2EID     = 0x342;
_C1TX2EIDbits = 0x342;

add your own declaration to them, modify your makefile to include your modified linker script and you should be good to go (but I haven't tried that myself).

jwsc
  • 867
  • 8
  • 15