This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/TMS320F28335: How to Initialize Union at compile time

Part Number: TMS320F28335

Tool/software: Code Composer Studio

Hi,

I would like to know if it is possible to Initialize each bit of the following Union/Structure using #define INIT_XB

struct X_BITS {

    Uint16 a1:1;
    Uint16 a2:1;
    Uint16 a3:1;
    Uint16 a4:1;
    Uint16 a5:1;
    Uint16 a6:1;
    Uint16 a7:1;
    Uint16 a8:1;
    Uint16 a9:1;
    Uint16 a10:1;
    Uint16 a11:1;
    Uint16 a12:1;
    Uint16 a13:1;
    Uint16 a14:1;
    Uint16 a15:1;
    Uint16 a16:1;
    };
    
    typedef union X_B{
        Uint16             all;
        struct             X_BITS;
    } regXB;    

#define INIT_XB = {1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0}

...is it possible to do it at compile time?

regXB    rXB = INIT_XB;

Regards,

Thanks, Jorge

  • There are a few different ways to go about it.  Here is one ...

    #include <stdint.h>
    
    typedef uint16_t Uint16;
    
    struct X_BITS {
        Uint16 a1:1;
        Uint16 a2:1;
        Uint16 a3:1;
        Uint16 a4:1;
        Uint16 a5:1;
        Uint16 a6:1;
        Uint16 a7:1;
        Uint16 a8:1;
        Uint16 a9:1;
        Uint16 a10:1;
        Uint16 a11:1;
        Uint16 a12:1;
        Uint16 a13:1;
        Uint16 a14:1;
        Uint16 a15:1;
        Uint16 a16:1;
    };
        
    typedef union X_B{
        struct X_BITS bits;
        Uint16 all;
    } regXB;    
    
    #define INIT_XB {1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0}
    
    regXB global_bits = INIT_XB;

    I'll describe this a few lines at a time.  Starting with ...

    #include <stdint.h>
    
    typedef uint16_t Uint16;
    

    This is one way to define the type Uint16, which is used throughout the rest of the code.  The header file stdint.h is available with every C compiler (except very old ones).  Among other things, it defines several fixed-width types, one of which is uint16_t.  It is used here to define the type Uint16.  

    When defining the union ...

    typedef union X_B{
        struct X_BITS bits;
        Uint16 all;
    } regXB;    
    

    The struct X comes first.  That is because, when initialized on a later line, the initialization values are applied to the first member of the union.

    In the definition of the preprocessor symbol INIT_XB ...

    #define INIT_XB {1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0}

    There is no equals sign.

    This line ...

    regXB global_bits = INIT_XB;

    ... creates a global variable of type regXB named global_bits.  It is initialized with the expansion of the preprocessor symbol INIT_XB.

    Thanks and regards,

    -George

  • Excellent!!! Thanks!!!

    Just one more question, is it possible to initialize the same structure with the second member (all) ?

    Thanks, Regards
    Jorge
    
    
  • Jorge Idiart said:
    is it possible to initialize the same structure with the second member (all) ?

    The C99 standard for C introduced a new feature named designated initializers.  In your case, you could use it like this ...

    regXB global_bits = { .all = 0xf0f0 };

    Thanks and regards,

    -George

  • George,
    Thanks.
    The above initialization give me the following error:
    "error: expected an expression"
    Regards, Jorge.

  • Jorge Idiart said:
    The above initialization give me the following error:
    "error: expected an expression"

    Which compiler version and options are used for the project?

    The designated initializers shown in George's example were introduced in C99. E.g. with the C2000 v20.2.1 compiler then if the --c89 --strict_ansi options are used to enable strict conformance to C89 then use of designated initializers fails with the "error #29: expected an expression".

    Whereas using the options --c99 --strict_ansi then allows the use of designated initializers.

    Use of --strict_ansi isn't required to use C99 features, but I just used it with both --c89 and --c99 to demonstrate under which conditions use of  designated initializers will generate a compiler error.

  • Hi Chester,

    Thanks for your help

    I'm using Code Composer 3.3

    Buid Tools:

    Texas Instruments C2000 Code Generation Tools <v5.2.11>

    ...C2000 Code Generation Tools 5.2.11/bin/cl2000 " -g -pm -pdsw225 -o2 -fr"

    Can you showme where add --c99 --strict_ansi option?

    Thanks!

    Regards, Jorge

  • C2000 compiler version 5.2.11 does not support the C99 standard, which means it does not support designated initializers.  This means the suggestion I made in my previous post does not work.  That being the case, there is no method by which you can initialize the second member of a union.

    Thanks and regards,

    -George

  • Thanks George!

    Regards, Jorge