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...
hi everyone, i will to see compile file uart.txt and iotest.txt,the result are blow:
uart.txt AREA ||.data||, DATA, ALIGN=2 UART0TxEmpty 000000 03 DCB 0x01 UART1TxEmpty 000001 010000 DCB 0x01,0x00,0x00 UART0Count DCD 0x00000000 UART1Count DCD 0x00000000 UART0Status DCD 0x00000000 UART1Status DCD 0x00000000 iotest.txt 000042 0000 DCW 0x0000 |L1.68| DCD UART0Count |L1.72| DCD 0x4000c000 |L1.76| DCD UART0Buffer |L1.80| DCD Teststring AREA ||.data||, DATA, ALIGN=0 Teststring 000000 01020304 DCB 0x31,0x32,0x33,0x34 000004 35363738 DCB 0x35,0x36,0x37,0x38 000008 3930 DCB 0x39,0x30
the compiler seem to initialize global variables, but in the debugger did not.in the debugger i to saw symbols,that shown UART0Count value was 0x20485231 and Teststring was 0xd0,0x42,...this value was memory(ram) value,and then i used memset to clean ram(start:0x10000000 size:512byte data:0x0),i to saw UART0Count and Teststring value were 0x00000000,so compiler did not initial,but compile file shown the value were correct.did i setting wrong?
i solve the problem,because i modify IMPORT __main to IMPORT main,but i do not understand why modify this __main to main,will cause global variables not initial.anyone can tell me thank..
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?
i modify IMPORT __main to IMPORT main,but i do not understand
Then what on earth made you do it? On what basis did you believe you knew better than the authors of that startup code how to start the main function?
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