Hi,
I am trying to erase and write P-Flash with my MC9S12XEP100 application. I am able to successfully step through the subroutines performing the erasure and write. But if I try to run the application normally, an illegal breakpoint stops the program execution.
My P-Flash handling subroutines are derived from the subroutines I use to handle D-Flash in the same application. D-Flash handling works fine. And I have set the FCLKDIV correctly. Below is my code for erasing and writing P-Flash:
void erasePflash(unsigned long sector_address){
unsigned int gpage;
DisableInterrupts;
//Wait for the previous flash command to complete
while(!FSTAT_CCIF);
//Clear ACCERR or FPVIOL if set
if ((FSTAT & 0x30) != 0) {
FSTAT = 0x30;
}
//Erase D-flash command, set global page
gpage = (unsigned int) (sector_address >> 16U);
gpage |= 0x0A00;
FCCOBIX = 0x00;
FCCOB = gpage;
//Address within sector
FCCOBIX = 0x01;
FCCOB = (unsigned int) (sector_address);
//Initiate flash command
FSTAT = FSTAT_CCIF_MASK; //THE APPLICATION CRASHES IF I TRY TO RUN THE PROGRAM NORMALLY PAST THIS LINE
//Wait for the flash command to complete
while(!FSTAT_CCIF);
EnableInterrupts;
}
void writePflashWords(unsigned long address, unsigned int *__far ptr, unsigned char length){
unsigned int gpage;
unsigned int i;
DisableInterrupts;
//Wait for the previous flash command to complete
while(!FSTAT_CCIF);
//Clear ACCERR or FPVIOL if set
if ((FSTAT & 0x30) != 0) {
FSTAT = 0x30;
}
//Data length out of range
if((length < 1) || (length > 4)) {
return;
}
//Program D-flash command, set global page
gpage = (unsigned int) (address >> 16U);
gpage |= 0x0600;
FCCOBIX = 0x00;
FCCOB = gpage;
FCCOBIX = 0x01;
FCCOB = (unsigned int) (address);
//Fill appropriate number of words to FCCOB
for(i=1; i<=length; i++) {
FCCOBIX = i+1;
FCCOB = *ptr;
ptr++;
}
//Initiate flash command
FSTAT = FSTAT_CCIF_MASK; //THE APPLICATION CRASHES IF I TRY TO RUN THE PROGRAM NORMALLY PAST THIS LINE
//Wait for the flash command to complete
while(!FSTAT_CCIF);
EnableInterrupts;
}
Solved! Go to Solution.
Illegal breakpoint here probably means that MCU goes into reset.
Flash and EEPROM are not readable while being programmed, erased or while any other flash command is executing. When you launch flash command CPU just can't read program code and runaways. Single stepping makes code paused, you continue when erase/program is complete. It is mentioned zilion times in forums that you need to move part of your flash erase/program code to RAM so that flash command is launched (FSTAT = FSTAT_CCIF_MASK;)when program counter points to RAM and not return from RAM while(!FSTAT_CCIF);. Interrupts also have to be disabled while(!FSTAT_CCIF);
Illegal breakpoint here probably means that MCU goes into reset.
The problem was related your point 3. Somehow I understood that it is possible to stay executing in P-Flash as long as the application does not try to erase or write the same sector where the execution is happening. But this concerns the whole block, as you said.
Thank you for the good example with the machine code for performing the Flash command while executing from RAM!