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.

How to initialize global variables using CCS

Hello!

I am using CCS compiler for my project, I have faced a critical issue regarding initialization of global variables before jump to main function. Usually when I use IAR, IAR compiler generates zero init code and this function initialize all global variable which are not set. But CCS dose not generate any code which does an initialization of global variables before jump to main function.

Could you tell me if there is any  way  to initialize global variables before main function?

Best regards,

Thomas Song

 

  • You have to clear memory with a user function.  Use something like this:

    int _system_pre_init( void )
    /**
     * Set RAM to zero.
     * This is a lowlevel initialization routine of the runtime!
     *
     * \note
     *    the runtime of the CCE20 does not do that on its own!
     */
    {
       extern uint8_t __bss__;
       extern uint8_t _stack;

       // turn watchdog off
       watchdog_init();

       //lint -e(946)
       memset( &__bss__, 0, &_stack - &__bss__ );
       //lint -e(419)
       memset( &_stack, LOW_STACK_PATTERN, MM_STACKSIZE - 32 );
       return 1;
    }   // _system_pre_init

     

    Hardy

  • If you don't initialize global variables with a definition and just declare them, I'm not sure whether the standard defines a pre-initialization with 0. It is usually done, but I don't think it is a neccessity by the C standard. So it works only coincidentally, based on the compiler.

    So if you have a global variable X then do not just declare it with e.g. unsigned char X; but do define it in one of the code files with unsigned char X = 0; Then it will (must) be initialized. It depends on the compiler whether this variable will initialized by copying the 0 from the init data flash area or placing it in an area which is initialized with 0 with a code loop.

    My guess is that you might be able to force CCS to init the variables by either putting them all into a segment that is initialized (but then you can as well just define an init value to them) or maybe by altering the linker scripts, so the linker will consider all uninitialized variables to fall into an initialized data segment rather than an uninitialized.

    Unfortunately, I don't know the CCS linker scrips well enough to tell you how this might work.

**Attention** This is a public forum