Erasing and writing P-Flash on MC9S12XEP100 fails when running the program normally, but not when stepping with debugger

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Erasing and writing P-Flash on MC9S12XEP100 fails when running the program normally, but not when stepping with debugger

Jump to solution
2,958 Views
utwig
Contributor III

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;

}

Labels (1)
Tags (2)
0 Kudos
Reply
1 Solution
2,213 Views
RadekS
NXP Employee
NXP Employee

Illegal breakpoint here probably means that MCU goes into reset.

  1. I guess that issue could be caused by wrong address format. Please check whether you really use global addresses. See attached memory map.
  2. I would like to also recommend check whether addresses are correctly aligned = global address [2:0] = 0b000
  3. You cannot erase or write the same block from where your code runs. I would like to recommend execute at least critical part (FSTAT = FSTAT_CCIF_MASK; while(!FSTAT_CCIF);) from RAM. See attached example code.
  4. This issue could be simply caused by watchdog (time for flash command is too long). Please check whether you used watchdog and how it is with triggering.

View solution in original post

0 Kudos
Reply
3 Replies
2,213 Views
kef2
Senior Contributor V

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);

2,214 Views
RadekS
NXP Employee
NXP Employee

Illegal breakpoint here probably means that MCU goes into reset.

  1. I guess that issue could be caused by wrong address format. Please check whether you really use global addresses. See attached memory map.
  2. I would like to also recommend check whether addresses are correctly aligned = global address [2:0] = 0b000
  3. You cannot erase or write the same block from where your code runs. I would like to recommend execute at least critical part (FSTAT = FSTAT_CCIF_MASK; while(!FSTAT_CCIF);) from RAM. See attached example code.
  4. This issue could be simply caused by watchdog (time for flash command is too long). Please check whether you used watchdog and how it is with triggering.
0 Kudos
Reply
2,213 Views
utwig
Contributor III

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!

0 Kudos
Reply