MCF51AG128 - Problem to program the flash memory

Showing results for 
Search instead for 
Did you mean: 

MCF51AG128 - Problem to program the flash memory

Contributor I


I am working on the MCF51AG128 microcontroller with codewarrior.
The final aim of my project is to make a bootloader, but first I want to succed to write correctly on the flash memory but I have some troubles to do it.

What I did :
- I configured the flash clock between 150kHz and 200kHz.
- I did the correct algorithm explained on the documentation (MCF51AG128RM.pdf p.88) to program the flash.

My problem :
I succed to write on a block of the memory but instantly after there is and interruption because of an illegal address access.

What I tried :
- I tried differents flash clock value.
- I tried to declare the flash block memory differently.
- I tried to change the CPUCR to not reset the microcontroler while there is an illegal address access error. But the MCP is constantly in the exception_handler interruption.

My code :
Error 1 illegal adress access :


Error 2 by changing the CPUCR to not reset the MCU :

Do you have any idea how to fix this problem ?

Thanks for your help,

0 Kudos
4 Replies

Specialist I

I haven't programmed this chip. I'd suggest the first thing to do would be to find working example code and copy that. But looking at Page 88 in the manual:

Before starting a command write sequence, the FACCERR and FPVIOL flags in the FSTAT register must be clear and the FCBEF flag must be set

You're not doing that. You're setting the FCBEF bit, but you're not clearing the other ones. If they're set the command won't run, and note you have to write a "1" to those bits to clear them.

Are you running that programming command from RAM? Or is the IDE you're using burning it to Flash and you're running it from there? That can't work. Have you erased the Flash before you try to program it?

I'd suggest you search this forum for others that have had problems with this, like this one from 2008, yes 13 years ago. See if they had similar problems, and look at any code they mention.

I found that by searching this forum for "FCBEF" - that's the easiest way to find things related to a particular prepheral - search for a register or register bit name. The code given there does a few more things than yours does:





/* The opcode above represents this set of instructions */
if (FSTAT&0x10){ //Check to see if FACCERR is set
    FSTAT = FSTAT | 0x10; //write a 1 to FACCERR to clear
(*((volatile unsigned char *)(Address))) = data; //write to somewhere in flash
FCMD = 0x20; //set command type.
FSTAT = FSTAT | 0x80; //Put FCBEF at 1.
_asm NOP; //Wait 4 cycles
_asm NOP;
_asm NOP;
_asm NOP;
if (FSTAT&0x30){ //check to see if FACCERR or FVIOL are set
    return 0xFF; //if so, error.
while ((FSTAT&0x40)==0){ //else wait for command to complete





Note the "wait 4 cycles". That was necessary 13 years ago with the MC9S08AW16. The Coldfire chip might need the same, less or more. Not having those waits might explain why single-stepping the programming works for you. The single-stepping is adding the delays. Why are they there? The FCCF bit may not be able to clear immediately when you write FCBEF. It may take a few Flash clock cycles before it can do that. This is very common in these peripherals. Since the Coldfire is probably running at a higher clock rate than the MC908 it might need a longer delay too. Or maybe that's only there because it takes a while for the error bits to set and the delay is needed for that. In any event, programming code should check for errors and should tell you about any errors it found so you know.

Yes, I know. I can't find anything in the manual that says that any delays are necessary, and none of the flow charts show them either. Guess what? The instructions and flow charts in the 2007 MCS908 manual don't show any delays either. The Flash chapter in the MCF manual looks almost identical the the MC908 one as they're using the same Flash module. That's why you have to look at sample code, to find the stuff the manual doesn't mention.




0 Kudos

Contributor I

Hi TomE,

Thanks a lot for your clear awnser!

Conserning what you are asking first, I was clearing the FACCERR and FPVIOL flags in the FSTAT register.
And the error was the same when I tried to erase the flash.

But I found a dirty solution:
When there is the address error, I just jump into the main (from the exception) to "skip" the error.
And this solution work fine, I can do every operation on the flash without any problems.

I'll try to put delays between commands as you suggest to have a safer solution.
I tried like the code you sent but I still have this error.
I know I need to do more testing.

Thanks again,

0 Kudos

Contributor I


Tanks for your response.
Unfortunately those documents can not help me for my problem which is writing to the flash and not about the bootloader.

Update of my probelm :
The protocol to write on the flash succed only when I use the debugg mode in "step by step".

If anyone has any other idea where the problem might come from it would be much appreciated.
Thanks for your help,
0 Kudos

NXP TechSupport
NXP TechSupport

 you can check the following applications note for the bootloader

I hope this will help you




0 Kudos