Hello, I am trying to erase Flash 1 on the MC68HC908GZ60 using the Flash 1 CTRL Register but I am having some issues. After a mass erase, the uC resets even if i clear the watchdog at every step. Here is my function to do a mass erase:
void EraseFlash1()
{
unsigned char ucTemp;
unsigned char *pFlash1;
unsigned int uiCounter;
FL1BPR=0xFE; //Protect FL1BPR and Interrupt Vectors
FL1CR = 0x06;
ucTemp = FL1BPR; //Read FL1BPR as described in the datasheet
pFlash1 = (unsigned char*)0x8000;
*pFlash1 = 0x00;
// wait minimum 10µs
for(uiCounter = 0; uiCounter < 20; uiCounter++) //Wait double of the required time
__RESET_WATCHDOG();
FL1CR = 0x0E;
// wait minimum 4ms
for(uiCounter = 0; uiCounter < 8000; uiCounter++) //Wait double of the required time
__RESET_WATCHDOG();
FL1CR = 0x08;
// wait minimum 100µs
for(uiCounter = 0; uiCounter < 200; uiCounter++) //Wait double of the required time
__RESET_WATCHDOG();
FL1CR = 0x00;
}
For information, my quartz is at 1Mhz so I am pretty sure my sleep time between steps is correct. I made sure that my code was located in flash2 otherwise I know i would erase myself. The beginning of my file looks like this
#pragma CONST_SEG SPECIAL_CONST
#pragma CODE_SEG SPECIAL_ROM
#pragma DATA_SEG SPECIAL_RAM
and the memory map looks like this:
SEGMENTS /* here all RAM/ROM areas of the device are listed. Used in PLACEMENT below. */
Z_RAM = READ_WRITE 0x0040 TO 0x00FF;
RAM1 = READ_WRITE 0x0100 TO 0x03CF;
RAM1_SPECIAL = READ_WRITE 0x03D0 TO 0x043F; // part of RAM1 - used for CodeLoader
RAM2 = READ_WRITE 0x0580 TO 0x097F;
FLASH2A = READ_ONLY 0x0462 TO 0x04FF;
FLASH2B = READ_ONLY 0x0980 TO 0x1B7F;
FLASH2C = READ_ONLY 0x1E20 TO 0x7FFF; // used for CodeLoader
FLASH1 = READ_ONLY 0x8000 TO 0xDFFF;
FLASH1_SPECIAL1 = READ_ONLY 0xE000 TO 0xEFFF;
FLASH1_SPECIAL2 = READ_ONLY 0xF000 TO 0xFDFF;
VECTOR1 = READ_ONLY 0xFFCC TO 0xFFFF;
// VECTOR2 = READ_ONLY 0xEFCC TO 0xEFFF;
END
PLACEMENT /* here all predefined and user segments are placed into the SEGMENTS defined above. */
DEFAULT_ROM INTO FLASH1;
SPECIAL_ROM INTO FLASH2C;
SPECIAL_CONST INTO FLASH2B;//FLASH1_SPECIAL2;
DEFAULT_RAM INTO RAM1;
SSTACK INTO RAM1;
SPECIAL_RAM INTO RAM1_SPECIAL;
RAM2 INTO RAM2;
// PARAMETER_FLASH INTO FLASH2B;
_DATA_ZEROPAGE INTO Z_RAM;
VECTOR_TABLE_1 INTO VECTOR1;
// VECTOR_TABLE_2 INTO VECTOR2;
END
Finally the function calling the Mass erase function looks like this:
void ProgramuCCodeBis()
{
#ifdef __HIWARE__ //to disable the interrupts
asm TPA;
asm PSHA;
asm SEI;
#else
#pragma asm
TPA
PSHA
SEI
#pragma endasm
#endif
EraseFlash1(); //calling the mass erase function
//make a led blink to see if the uC resets or not
for (;

{
for (i=0;i<0xFFFF;i++)
{
PTB_PTB1 = 1;
asm sta COPCTL; //Clearing the watchdog
}
for (i=0;i<0xFFFF;i++)
{
PTB_PTB1 = 0;
asm sta COPCTL;
}
}
#ifdef __HIWARE__ //Enabling interrupts
asm PULA;
asm TAP;
#else
#pragma asm
PULA
TAP
#pragma endasm
#endif
}
The next step after the mass erase will be to program flash 1 with a source code received on the CAN Bus previously and stored on a external RAM.
Does anybody see something wrong in the code or give a hint how to debug this? I have been stuck for a week on this .
Thank you very much for your help
Cyril G.
PS: I attached Codeloader.c and codeloader.h containing all the functions and prototypes