MCF51QE Flash Erase and Programming

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

MCF51QE Flash Erase and Programming

Jump to solution
1,366 Views
sebasira
Senior Contributor I

Hello everybody! How are you doing?

I hope you're well

 

I'm developing an application that store user data in the internal flash memory. I'm using routines from AN3942, which are:

 

char Flash_SectorErase(unsigned long *FlashPtr)

char Flash_ByteProgram(unsigned long *FlashStartAdd,unsigned long *DataSrcPtr,unsigned long NumberOfBytes)

 

Well, first of all, I've notice that argument NumberOfBytes, should be NumberOfDWord, because the Flash_ByteProgram routine write as many 4bytes as NumberOfBytes... So let's say that you want to write 4 bytes at address 0x2000 then function call should be:

 

Flash_ByteProgram(0x2000, data_ptr, 1)

 

Please, try it your self and let me know. I've notice that because the routine writes 4 times the length I want to write.

 

 

On the other hand, I can't program an address, and then erase it and progam it again... 

 

Flash_Erase(0x2000)Flash_ByteProgram(0x2000, "TEST", 1)then when some event happens, I callFlash_Erase(0x2000)Flash_ByteProgram(0x2000, "LONG", 1)

 

After programming "TEST" I go check the address 0x2000 and there it is, "TEST" is written at that address.

Then I put a breakpoint when the event happens and call Flash_Erase. This routine returns error code 0 (no error), but I look at address 0x2000 and "TEST" is still there... I assume, when erased, 0xFF 0xFF 0xFF 0xFF should be there instead... After Flash_Erase, Flash_ByteProgram is executed, again error code is 0, but the new value it's not written...

 

What could be happenning?

 

Just want to let you know that I do not run it step by step... I set the breakpoint, and the let it run when breakpoint is reached.... It didn't work (flash wasnt re-programmed) so then I ran stepping over the routines just to see what do they returned.

 

Thanks in advance! I hope you can help me here 

 

Labels (1)
0 Kudos
1 Solution
580 Views
kef
Specialist I

Yes, your byte program routine writes not bytes but long words.

 

Regarding debugger caching and updating contents of read only memories. I mentioned above how to make it refreshing flash in case your are using USB-12-ML multilink. If you are using different interface, then I guess there must be similar way to make debugger refreshint the contents of flash.

  

1. Flash sector size (erase size) is mentioned in 4.4.1 Features in QE reference manual. MCF51QE128: 131,072 bytes (128 sectors of 1024 bytes each). Or 256 long words (32bit words) each.

 

2. 900 bytes? I think that's total amount of code. On stack you should have much less than that. To determine if you have enough stack, try 1) resetting MCU with debugger, 2) clear stack space or all the RAM  3) run you code for a while, of coursse including the most stack hungry routines. Now stop and see what has changed since you cleared the RAM. It should be easy to determine how deep did your stack pointer go. 

  

I erased AN3942 from my PC and can't check, but I don't think they are calculating block to be erased.

 

Sector erase command, made to address x, will erase flash from (x & ~(sector_size-1)) to ((x & ~(sector_size-1)) + sector_size-1). Or from ((x / sector_size) * sector_size) to (((x / sector_size) * sector_size) + sector_size-1), all operations integer like in C (remainder is lost, no rounding etc)

 

Yes Mass erase will erase all the flash, provided no parts of flash are write protected. Mass erase should fail if any area is write protected. This command is useful only for flash burners and debuggers.

View solution in original post

0 Kudos
6 Replies
580 Views
kef
Specialist I

Did you rename Page_Erase and Burst_Prog  to Flash_SectorErase and Flash_ByteProgram? Or are we talking about different AN3942's?

AN3942 I downloaded requires size argument of Burst_Prog to be multiple of 4. This is stated in DoOnStack_CFV1.asm.

Specifying for example 2 makes Burst_Prog running not forever, but up to the end of flash :smileyhappy:. This is because that after programming the long word,  Byte_Prog decrements size by 4, checks if lowest order byte of size is 0 and repeats programming next byte it is not zero. Of course -=4 will never give 0 if initial size is not multiple of 4.

  

Page_Erase can fail in the case FCDIV divider is too low. This will probably make erase voltage applied to the flash array for not long enough time to erase.

 

Another possibility is that debugger is fooling you by caching contents of "must be const and readonly" flash memory and never refreshing it. If you are using P&E Multilink, in CW for MCUs V6.3 debugger go to CFMultilinkCyclonePro-> Debugging Memory Map..., select FLASH, click Modify/Details and check the Refresh Memory When Halting checkbox. Now flash should update after erase and program.

 

I found the bug in DoOnStack_CFV1.asm. In _Burst_Prog: routine, instead of clearing FACCERR and FPVIOL error flags, one is written to FCCF flag. Flash command is not specified at this point and code is trying to launch old flash command. This shoud make FACCERR set. Burst_Prog shouldn't be working at all. See attached fixed asm file.

 

0 Kudos
580 Views
sebasira
Senior Contributor I

Hi kef!

 

I'm sorry, you were right.... I was not using code from AN3942... I was using it before... What I'm using now is here in this post:

(almost at the end of it )

http://208.74.204.137/t5/68K-ColdFire-reg-Microprocessors/MCF51QE128-Problem-using-Flash-errors-in-C... 

 

 

or you can also find it here.... I'ts develop by freescale so I guess some one could give some support

 

flash.c

flash.h

 

Thanks!!

 

PS: I'll also try with AN3942 and let you know 

0 Kudos
580 Views
sebasira
Senior Contributor I

I'm here again!

 

Well, kef, I've got to say you were right again. CONGRATULATIONS! Debugger was fooling me.... I try back the routines from AN3942 and same symptom happens.

So I decided to read the memory out with a buffer and the flash was indeed erased and reprogammed with the new value....

 

So now the question becomes..... How can I avoid the debugger fooling me? I'm asking that because I'm developping this application, and I'm testing the performance of flash erasing/programming. If the debugger fools me it could mess me around...

 

Kef, as always, every reply of yours are very helpfull... I really really thank you very much!! :robothappy:

 

 

I've got some other questions that you may be able to ask... There are some conceptual question.

 

1- I'm working with MCF51QE as stated before... When erasing flash, how many long words (4 bytes) am I erasing? And where I can find that information (I can't seem to find it in the Ref Manual)?

 

2- When using the DoOnStack routines, how can I know if I've got enough stack? AN3942 says that sources code for CFv1 is about 900bytes... that's the total of bytes running on stack?

 

 

Thanks to all of you for taking the time to read it

0 Kudos
580 Views
sebasira
Senior Contributor I

I realize I've ask the wrong question, in question #1.... If I'm not wrong, SECTOR ERASE, erase a sector of a 1Kb (1024 bytes)... What I want to know are the start addresses of each sector.... And I'm assuming routines from AN3942 calculates the block to be erased when I pass the address I want to erase, right?

 

What about MASS ERASE? Does it erase the whole flash? or what?

 

Sorry for asking so much 

0 Kudos
581 Views
kef
Specialist I

Yes, your byte program routine writes not bytes but long words.

 

Regarding debugger caching and updating contents of read only memories. I mentioned above how to make it refreshing flash in case your are using USB-12-ML multilink. If you are using different interface, then I guess there must be similar way to make debugger refreshint the contents of flash.

  

1. Flash sector size (erase size) is mentioned in 4.4.1 Features in QE reference manual. MCF51QE128: 131,072 bytes (128 sectors of 1024 bytes each). Or 256 long words (32bit words) each.

 

2. 900 bytes? I think that's total amount of code. On stack you should have much less than that. To determine if you have enough stack, try 1) resetting MCU with debugger, 2) clear stack space or all the RAM  3) run you code for a while, of coursse including the most stack hungry routines. Now stop and see what has changed since you cleared the RAM. It should be easy to determine how deep did your stack pointer go. 

  

I erased AN3942 from my PC and can't check, but I don't think they are calculating block to be erased.

 

Sector erase command, made to address x, will erase flash from (x & ~(sector_size-1)) to ((x & ~(sector_size-1)) + sector_size-1). Or from ((x / sector_size) * sector_size) to (((x / sector_size) * sector_size) + sector_size-1), all operations integer like in C (remainder is lost, no rounding etc)

 

Yes Mass erase will erase all the flash, provided no parts of flash are write protected. Mass erase should fail if any area is write protected. This command is useful only for flash burners and debuggers.

0 Kudos
580 Views
sebasira
Senior Contributor I

That method works for refreshing the Memory windows... Also, double-clicking the data block updates its value, only that block value...

 

Thanks!!! 

0 Kudos