k61 internal flash memory programming from code

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

k61 internal flash memory programming from code

Jump to solution
2,899 Views
augiedog
Contributor II

Hi All;

I am going absolutely nuts trying to write some parameters back to the internal flash. I just want to store a few words for calibration reasons. I have used the demo code functions from flash_kinetis.c and I have problems. Interrupts are off, code is running from RAM.

1. When I try to erase a sector it appears to work as afterwards I look at the memory and it's all FFs...read on

2. I write my logword using the phrase write routines and afterwards it appears that the memory is correct...

3. I reset with the emulator (not reprogram so it's not the emulator erasing anything) and the first longword of the sector is zeroes.

The manual is extremely confusing about using FlexRAM, FlexNV, EEPROM, Program Flash, etc. The K61 I am using only has program flash in two banks. I just want to be able to use a few words of it for storage. Not one-time storage however!

Thanks for ANY help!

Labels (1)
1 Solution
1,429 Views
augiedog
Contributor II

Solved! --- sort of....

All this time I have been testing this with the emulator. If I flash my code and run from standalone it works! I'm guessing the emulation environment must have something to do with it's success or not. Yes - I do run the function that programs the part out of RAM, but I was certainly doing that when debugging from RAM. Strange, but I'll accept it.

Thanks again!

View solution in original post

13 Replies
1,429 Views
augiedog
Contributor II

...oh and I forgot to add that all the status bits looked fine after each command sequence!

0 Kudos
1,429 Views
paulmartin
Contributor III

Which flash address are you programming?

And do you get the same behaviour if you use a different address?

Are you writing only a single longword or more? As far as I remember the phrase routine writes in chunks of

8bytes.

If it works for you after the first longword you could just put your data there.

0 Kudos
1,429 Views
augiedog
Contributor II

address is 0xFF000 which is the last sector of the 1M Flash memory. It never actually works. The only longword it ever seemed to write was 0000 0000, not the intended values. It looks like the initial erase of that sector does not take either. If I just do the erase and then reset, it just has the first longword as zeroes still, and not FFFF FFFF which is what it should erase to, right? The docs say it erases the entire section you are in. Since the FSTAT registers seem completely happy I'm very confused. If the area was protected I would expect an access violation flag or similar. It is performing the function as I can get a completion flag. I made one change to the original code that struck me odd. The K61 manual does not have function 6 for program phrase, instead it uses 7. I tride 6 and got an error status flag, so that must be the right value to use. Why do they change things like that? Makes it less portable. But again, I think not even the erase is working. I did try other locations but not in a different sector...will try that.

thanks!

0 Kudos
1,429 Views
paulmartin
Contributor III

I work with a TWR-K60D100M. It has 512kb prgram flash and should work similar to your MCU.

Attached is my code for accesing the flash. I modified some code I found on the internet to work with Keil uVision. Even if you dont use keil you should be able to crosscheck your code, the register names are straight forward.

there are three functions: erase a sector, write a longword and write N phrases from Flash Acceleration RAM

I hope this helps.

0 Kudos
1,429 Views
augiedog
Contributor II

I used your code...and it did exactly what mine did. same result. I must be missing something extremely simple.

I looked at the flash clock and it seems to be set up correctly. No protection bits on either. No bank swapping. The oddest thing is that the status bits do not reflect that anything went wrong. I know this because your code originally had the code for phrase programming at 6, and I got a bad result. I changed it to 7, and it proceeded without error.

thanks again

0 Kudos
1,429 Views
paulmartin
Contributor III

The fact that the command code is different made me curious and i took a look at the reference manual for a K61 device.

There actually are some differences in comparison with K60.

1. total flash is 1MB instead of 512KB

2. sector size is 4KB instead of 2KB

3. there is a "program phrase" (8byte) command (code = 0x07) instead of "program lond word" (4byte) command (code = 0x06)

4. because you can write 8 bytes the register FCCOB has 4 more fields for data bytes.

So you were right to use code 0x07. I am not sure if you have to write those additional fields in FCCOB or if they can stay empty.

What i don't understand is why it does not erase properly. There is no difference regarding the erase sector command.

0 Kudos
1,429 Views
paulmartin
Contributor III

I had some problems with the TWR-K60N512, because it uses some early versions of the K60.

Certain chip masks worked and others did not. Somehow the integrated flash logic was busted for certain masks and I am not sure if all of that was covered in the errata.

That is why I made an effort to switch to TWR-K60D100.

0 Kudos
1,429 Views
augiedog
Contributor II

Unfortunately the K61 doesn't have any FlexNV memory. It has 1M of program flash in 2 banks only. No EEPROM emulation or any of that. I have turned off interrupts and checked all the protection flags. These parts are their latest offerings, sure seems those early issues wouldn't be there any longer, but you never know.

We needed to use the K61 because of the small package size (5mm x 5mm) and hardware FPU. Using it we have developed a pretty nice SDR on a 1.5" x 1.2" board complete with an RF front end tunable from 50-1800 MHz.This is the last issue I have.

I really appreciate everyone's input!

0 Kudos
1,430 Views
augiedog
Contributor II

Solved! --- sort of....

All this time I have been testing this with the emulator. If I flash my code and run from standalone it works! I'm guessing the emulation environment must have something to do with it's success or not. Yes - I do run the function that programs the part out of RAM, but I was certainly doing that when debugging from RAM. Strange, but I'll accept it.

Thanks again!

1,429 Views
deepakrana
NXP Employee
NXP Employee

Hi Michael

Great to know that you got the problem

The C90 TFS flash driver code examples basically are demo's of how to write to flash. They work in RAM configuration so you can write to flash directly. But it is not possible to execute code out of  and write to the same flash bank. So you need to do a copy to RAM function for the working of the C90TFS flash driver,

if you want to use these drivers then refer the app note

http://cache.freescale.com/files/32bit/doc/app_note/AN4695.pdf

The best and easiest thing i suggest is refer the USB stack and the DFU example. In this they use a simpler flash driver files. You can integerate them to your project and they work much simpler and easier. they already have flash to ram function integrated.

Any further help do let me know.

0 Kudos
1,429 Views
paulmartin
Contributor III

The NVMFlexRam or EEPROM is another topic, i have not used those.

The issue is a program flash only device.

0 Kudos
1,429 Views
dieterteuchert
Contributor IV

Now i seem to remember another complication: Since the flash controller works with 24 bit addresses, they invented some special address decoding to access the extra flash. Maybe you are not writing to program flash, but to the extra flash, although the address you gave points to the last sector of program flash.

0 Kudos
1,429 Views
dieterteuchert
Contributor IV

When i had to do the same (calibration data to flash) in a Kinetis K10 project, i used the extra range of flash, that can also be used as FlexNVRAM etc. Those methods implemented by the flash controller let you use a range of flash together with a range of RAM to simulate something like a disk drive (nonvolatile, long endurance with RAM cache). I read a little, but no time to learn. I found some samples and managed to configure that flash range for use as normal flash, with the erase sector and program longword commands. Also i remember that i had to disable interrupts during flash controller operation to execute the wait loop from instruction cache. It takes time and debugging to get this working, because there are lots of options and protections that all need to be configured correctly. In a 32 bit system nothing comes easy.

0 Kudos