Hi,
As we use more S12XE based boards we face this problem more frequently. Finally we were able to find something...
We are able to reproduce the problem fairly easy now - we close and open supply circuit very quickly (manualy) like several times per second and usually after few tries we have this problem produced. We had a look at voltage waveforms all over our boards and no problems were found.
Here's the code
/* INIT CLOCK */ /* 1. configure mcu clock: Fosc = 4MHz >> Fbus = 8MHz */ _SYNR.Byte = 0x03; _REFDV.Byte = 0x40; _POSTDIV.Byte = 0x01; /* 2. wait for PLL to lock */ while(!_CRGFLG.Bits.LOCK) { } /* 3. select PLL as clock source */ _CLKSEL.Bits.PLLSEL = 1; /* INIT FLASH */ /* clear the illegal address reset flag */ CRGFLG_ILAF = 1; /* initialise the memory controller prior to any */ /* commands being launched or EEE data access */ while(!FSTAT_CCIF) { } /* load the clock divider before any flash command. Assumes 4MHz XTAL */ FCLKDIV = 3; /* wait for ready state */ while(!FSTAT_CCIF) { } /* setup no protection */ FPROT = 0xFF; /* wait for ready state */ while(!FSTAT_CCIF) { } bt_flag_value = Read_Flash_Byte(bt_flag_addr); app_ok_value = Read_Flash_Byte(app_ok_addr); app_start_value = Read_Flash_Dword(app_start_addr);
Where Read_Flash_Byte and Read_Flash_Dword are:
byte Read_Flash_Byte(dword globalAddress) { globalAddress &= 0x00FFFFFF; return *(byte * __far) globalAddress; }dword Read_Flash_Dword(dword globalAddress) { globalAddress &= 0x00FFFFFF; return *(dword * __far) globalAddress; }
What we found out is if we insert wait loop before first Read_Flash_Byte(bt_flag_addr) instruction the problem is impossible to reproduce.
while(delay) delay--;bt_flag_value = Read_Flash_Byte(bt_flag_addr);app_ok_value = Read_Flash_Byte(app_ok_addr);app_start_value = Read_Flash_Dword(app_start_addr);
Where delay is set to arbitrary value of 0x0008FFFF (589 823 idle cycles). We tried with different values and this is just somewhere close to minimum OK value.
Placing wait loop between Read_Flash... functions or after last Read_Flash_Dword we were still able to reproduce the problem. So placing wait loop before those three instructions is our only solution for now but it results in a ~2 seconds delay 
PS. We use startup address as a code variable as we have many projects with different applications (with different flash locations) and we use common bootloader everwhere. During application upload the startup address is set depending on the application being uploaded.
I would greatly apreciate your help