Skip to main content
k1220
Associate
February 9, 2009
Question

struct initialization

  • February 9, 2009
  • 5 replies
  • 1718 views
Posted on February 09, 2009 at 10:19

struct initialization

    This topic has been closed for replies.

    5 replies

    k1220
    k1220Author
    Associate
    May 17, 2011
    Posted on May 17, 2011 at 13:02

    Hello, I'm new to STM32 and so far I have managed to run a few basic programs on my board (Olimex STM32F103-STK with Eclipse and CodeSourcery). However, I have a bit of a problem getting the USB demo to work, and so far I have pinned the problem down to struct initialization - namely, all structs defined outside of a function body do not initialize properly (or at all - they seem to have random values). For example:

    ...

    int main(void)

    {

    my_struct struct_instance =

    {

    10,0,myDelay

    };

    ...

    this works fine and struct_instance has correct values

    ...

    my_struct struct_instance =

    {

    10,0,myDelay

    };

    int main(void)

    {

    ...

    struct_instance has some random values

    Is this due to some link or compile option I'm missing?

    domen2
    Associate II
    May 17, 2011
    Posted on May 17, 2011 at 13:02

    Maybe some error with .data section?

    Mixed up addresses, or not copying data to it from flash on boot (part of the reset routine that also zeroes .bss section).

    That would also mean ie. ''int a = 3;'' outside of functions wouldn't work, but ''const int a = 3;'' would, since it's in .rodata (flash)

    k1220
    k1220Author
    Associate
    May 17, 2011
    Posted on May 17, 2011 at 13:02

    Yes, you are right, it fails to initialize any global variable, it just so happened that I only had a global struct. I looked at the output file and those variables are mapped to .data and addresses 0x20000000+ (RAM). And, yes, the const initialization works fine, I might resort to that or some other workaround, but I want to clear this up, as it seems an important issue.

    k1220
    k1220Author
    Associate
    May 17, 2011
    Posted on May 17, 2011 at 13:02

    Thanks a lot. I guess I was being a bit naive, expecting initialization to happen automatically :)

    giles
    Associate II
    May 17, 2011
    Posted on May 17, 2011 at 13:02

    It sounds like you need to tell your linker file its got to put the initialised data in flash, then in your startup.c you need to copy that data accross to ram where it belongs.

    for example my linker has this:

    _etext = .;

    PROVIDE (etext = .);

    at the end of my stuff to go in flash section and this:

    /* .data section which is used for initialized data */

    .data : AT (_etext)

    {

    __data_start = .;

    *(.data .data.*)

    *(.gnu.linkonce.d.*)

    SORT(CONSTRUCTORS)

    . = ALIGN(4);

    *(.fastrun .fastrun.*)

    } >DATA

    . = ALIGN(4);

    _edata = .;

    PROVIDE (edata = .);

    following it where

    etext is at the end of code & rodata in flash

    and DATA is the ram section

    and my startup.c has

    unsigned long MySource,MyDest;

    MySource = &_etext;

    for(MyDest = &__data_start; MyDest < &_edata; )

    {

    *MyDest++ = *MySource++;

    }

    hope that helps:)

    Giles