@kerryzhou
We've done more investigation into this and the startup code does not work properly if it is not optimized. If it is built with -O0, then the variable used for iterating through the data and bss sections gets corrupted after the first call to data_init(). Using stack variables in ResetISR() does not work properly and stack only seem to work a after a function call.
In ResetISR:
//
// Copy the data sections from flash to SRAM.
//
unsigned int LoadAddr, ExeAddr, SectionLen;
unsigned int *SectionTableAddr;
// Load base address of Global Section Table
SectionTableAddr = &__data_section_table;
// Copy the data sections from flash to SRAM.
while (SectionTableAddr < &__data_section_table_end) {
LoadAddr = *SectionTableAddr++;
ExeAddr = *SectionTableAddr++;
SectionLen = *SectionTableAddr++;
data_init(LoadAddr, ExeAddr, SectionLen);
}
// At this point, SectionTableAddr = &__bss_section_table;
// Zero fill the bss segment
while (SectionTableAddr < &__bss_section_table_end) {
ExeAddr = *SectionTableAddr++;
SectionLen = *SectionTableAddr++;
bss_init(ExeAddr, SectionLen);
}
When data_init() is first called and execution returns back to ResetISR(), SectionTableAddr is now overwritten with the value of pulDest in data_init(). This causes any other calls to data_init() and bss_init() to be skipped and therefore the application fails to run properly.
If the above code is moved into a separate function and called from ResetISR(), then initialization happens correctly because the stack variables are preserved properly.
When the code is optimized with -Og, then the stack variables get optimized out and registers operations are used instead without accessing the stack. This causes the code to execute as intended.
This is very confusing because the preprocessor macro DEBUG leads one to believe that it is only used for optimizing the code for debugging, not that it is essential to the startup code operating correctly.
This should really be addressed...either so the start up code's use of stack is fixed or at least so that the startup code is always optimized (not through defining DEBUG).