2009-02-09 01:19 AM
struct initialization
2011-05-17 04:02 AM
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?2011-05-17 04:02 AM
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.
2011-05-17 04:02 AM
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)2011-05-17 04:02 AM
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:) Giles2011-05-17 04:02 AM
Thanks a lot. I guess I was being a bit naive, expecting initialization to happen automatically :)