Help with Simple Flash Sector Erase

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

Help with Simple Flash Sector Erase

Jump to solution
1,735 Views
SecondTechCo
Contributor IV


I am writing a simple Flash sector erase for MCF series.  The program is below.  It runs fine when I single step through it and I can see on the cyclone pro that the proper locations in memory get erased so i beleive this tells me that I have FCDIV set up right and am exectuing the flash commands properly.    FCDIV ( clock 24 mHZ, FCDIV = 0x4C or FCDIV =0x4F)  is setup before entering the function and I have verfied it several times.  When I try to just run the function instead of single step, it somehow jumps out to a memory location ( 0x400c2700 ) I dont recognize at all.  It has nothing to do with the looping as far as I can see because I have tried to just erase one sector and again same results.  The flash erase function is located in memory in flash below the range that I am trying to erase.  The execution chain is attached.

 

1) Can any one point me in the right direction ?

 

2) I see on several examples including ones done by freescale that they use memcpy or another method to execute the sector flash erase out of RAM memory.   Not sure if this is the reason I am having problems.  Why do they execute out of flash ?  Is that somehow related to the problem I have.

 

void EraseFlash(void)
{
unsigned long TempLong;
TempLong = 0x1200;
FSTAT = 0XB0; //Clear all commands error and command in progress flags
while (TempLong < 0x20000) // 128 k
{
  if (FSTAT_FCCF == 1) // Command is complete
  {
   *(unsigned long *) TempLong = 0x55; // Write any byte
   FCMD = 0x40; // Sector erase of 1k of data;
   FSTAT = 0x80;
   while (FSTAT_FCCF == 0) // Wait for command to complete
   {
    asm
    {
     nop;
    }
   }
   TempLong = TempLong + 0x400;
  }

  if ((FSTAT_FACCERR == 1) || (FSTAT_FPVIOL == 1))
  {
   FSTAT = 0xB0;
  }
}

}

 

Thank you for the input.

 

 

 

 

 


 

 

Labels (1)
1 Solution
988 Views
kef
Specialist I

2) I see on several examples including ones done by freescale that they use memcpy or another method to execute the sector flash erase out of RAM memory.   Not sure if this is the reason I am having problems. 

Yes, you need to run your flash routine out of RAM. While FCCF bit is 0, flash array is not readable, but CPU has to read some code to execute. So you launch flash command and CPU runs away.

FSTAT = 0XB0; //Clear all commands error and command in progress flags

You should not clear FCBEF bit at this point. It is attempt to launch flash command! You should clear just ACCERR and PVIOL . (You have two such lines in your code).

View solution in original post

0 Kudos
4 Replies
989 Views
kef
Specialist I

2) I see on several examples including ones done by freescale that they use memcpy or another method to execute the sector flash erase out of RAM memory.   Not sure if this is the reason I am having problems. 

Yes, you need to run your flash routine out of RAM. While FCCF bit is 0, flash array is not readable, but CPU has to read some code to execute. So you launch flash command and CPU runs away.

FSTAT = 0XB0; //Clear all commands error and command in progress flags

You should not clear FCBEF bit at this point. It is attempt to launch flash command! You should clear just ACCERR and PVIOL . (You have two such lines in your code).

0 Kudos
988 Views
SecondTechCo
Contributor IV

I will try moving the function over to RAM.  So I need to stay in the function until the FCCF bit is cleared ?

Thank you.

0 Kudos
988 Views
kef
Specialist I

You need to clear FCBEF when executing code in RAM, and not exit to code in flash while FCCF==0.

BTW you also need to care about interrupts. Since flash is not readable while FCCF==0, you can't keep interrupts enabled while erasing/programming flash, unless you move interrupt vectors table and ISR's to RAM.

988 Views
SecondTechCo
Contributor IV

Theis worked out very Well.   Thanks you for the solution.   Moving the function To Ram solved my problems

0 Kudos