hi,i used MDK4.10 compile LPC1768 test code,and used jlink to debug,but i discovered global variables not initialize(did not i give value), it are Random value.Local variables are correct. do i setting wrong? this issue let me can't debug,someone can solve this problem? or it is normal?
example: uart.c volatile uint32_t UART0Count = 0; iotest.c extern volatile uint32_t UART0Count; //in the debugger,Teststring are not below,are 0xda,0xfc.....why uint8_t Teststring[LENGTH] = {'1','2','3','4','5','6','7','8','9','0'}; while (1) { //in the debugger,UART0Count is not 0,is 0x24865414why if ( UART0Count != 0 ) { LPC_UART0->IER = IER_THRE | IER_RLS; /* Disable RBR */ UARTSend( 0, (uint8_t *)UART0Buffer, UART0Count ); UARTSend( 0, (uint8_t *)string, 10 ); UART0Count = 0; LPC_UART0->IER = IER_THRE | IER_RLS | IER_RBR; /* Re-enable RBR */ } }
my english is bad, hope you don't mind......^^ please help me this question...very thank you...
When you power up the processor, SRAM memory will hold random (or at least semi-random) contents.
So the processor needs to initialize the RAM. It does that by clearing all memory regions that holds zero-initialized variables (global variables that haven't been given any initial value explicitly by you). And in the flash, it has a copy of the initial values for all global variables that you have specified an initial value for.
But this clearing/copying is performed by the startup code, i.e. code run before the processor reaches your main() function. If you do play with the startup code, so you don't run all the initialization functions inside the runtime library, you may still get to main(), but can't have any expectations on memory contents, or that internal structures inside the RTL have been correctly initialized.
So you have to realize that main _main or __main are completely different symbols. Your main() normally gets the external linkage name _main, i.e. with a single underscore added. Symbols with two underscores are reserved for the compiler/RTL, meaning you should not create own symbols that starts with a single '_' character. Also, linkage symbols without any leading underscore are also belonging to a namespace your C program can't reach (since the C compiler adds a single underscore to your symbol names) and hence a symbol "owned" by the compiler/RTL vendor.
In the end, it is your responsibility to make sure that the startup code does call the required RTL initialization functions and let them call your main(), instead of trying to get the startup file to get directly to your main().
Or more specifically - if your startup file gets directly to your main(): who do you think will then distribute initialized values to your ZI and RW variables? The processor itself?
thank Per Westermark reply^^,i understand you say.thank again. and i accidentally changed it,so i debuged so long~~~~~but i learned this konwleage, thank you