How to write my own data to the flash of K64 ( above the program)

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

How to write my own data to the flash of K64 ( above the program)

Jump to solution
4,890 Views
eliar
Contributor II

Hi

Is it possible to write some userdata( my own data) at the end of the flash for later on to read it back?

I tried to do so with the flash driver API and i cannot read back using the ReadResource function.

I tried both k64 flash driver demo  and the bootloader flash driver demo ( they are almost the same, Note that the enum defined there with small and capital letter) 

In both i am getting invalid argument.

1. is it possible to write and read user data to the flash?

2. can someone test and and show a working example?

3.If it is not possible , where can i store user data for my firmware without using sdcard?

Thanks!

1 Solution
3,535 Views
DavidS
NXP Employee
NXP Employee

Hi Eli,

Attached is my source file (flash_erase_program_verify.c) that has been edited to test for wheter 0xff000 has been flashed or not.  If not flashed, then code continues as normal.  If 0xff000 has been flashed, then I memcpy to get data from 0xff000 and PRINTF it to terminal.

So on first pass of program where 0xff000 has not been flashed I get following:

Flash Example Start

Flash Information:

 Total Program Flash Size:1024 KB, Hex: (0x100000)

Program Flash Sector Size:4 KB, Hex: (0x1000)

 There is no D-Flash (FlexNVM) on this Device.

There is no Enhanced EEPROM (EEE) on this Device.

Flash is UNSECURE!

Erase a sector of flash

Successfully Erased Sector 0xff000 -> 0x100000

Program a buffer to a sector of flash

 Successfully Programmed and Verified Location 0xff000 -> 0xff010

End of Flash Example

On second pass by pressing the reset button on FRDM-K64F, I get following:

flash_buffer_contents preserved in flash

flash_buffer_contents[0] = 0x0

flash_buffer_contents[1] = 0x1

flash_buffer_contents[2] = 0x2

flash_buffer_contents[3] = 0x3

This matches what is in flash memory 0xff000 when I look at it in the Memory Browsers too.

Hope source helps.  Look for "//DES" to see locations modified.

Regards,

David

View solution in original post

12 Replies
3,535 Views
eliar
Contributor II

Hi Mark

I saw the the code is author by you, but i am still very much confused.

You send me a code that does not seems to be compatible with kinetis SDK 2.0 and the flash driver.

let me please ask the following:

can you do a write of user data to the flash using flash driver API in the kinetis 2.0  and read it back?

The example used FLASh_Program function , which i assume it write a buffer , but i cannot read it back.

We need to store our own data after the program code and the software need to read it when it start.

0 Kudos
Reply
3,535 Views
DavidS
NXP Employee
NXP Employee

Hi Eli,

Have you tried running the KSDK_v2 for FRDM-K64F Freedom board flash programming example at following path?

C:\NXP\KSDK_v2\SDK_2.0_FRDM-K64F_KDS\boards\frdmk64f\driver_examples\flash\flash_erase_program_verify\kds

When I run it, the terminal output is as follows:

Flash Example Start

Flash Information:
Total Program Flash Size:1024 KB, Hex: (0x100000)
Program Flash Sector Size:4 KB, Hex: (0x1000)
There is no D-Flash (FlexNVM) on this Device.
There is no Enhanced EEPROM (EEE) on this Device.
Flash is UNSECURE!

Erase a sector of flash
Successfully Erased Sector 0xff000 -> 0x100000

Program a buffer to a sector of flash
Successfully Programmed and Verified Location 0xff000 -> 0xff010

End of Flash Example

After erasing the flash the Memory Browser showed the 0xff000 flash location to be erased:

pastedImage_2.png

After FLASH_program Memory Browser showed:

pastedImage_3.png

The data set size was 16 bytes.

Note the minimum size data set to write/program to flash is 8-bytes.

Regards,

David 

3,535 Views
eliarad2
Contributor II

Hi David,

yes , i just run this exact example.

But lets continue:

I want to read back the value.

So i just did memcpy , ( before i reset the board)

and i do see that i can read using direct access to the flash.

as i understood, i dont need special function to read from the flash because it is already mapped.

BUT here is a problem.

When i reset the device ( just press the reset , or using putty doing a break command) without reprogramming the flash again ( i am using mbed drag and drop)

When the program is starting again and i am doing the read operation only ( because i though the data will be persistance)

The value i am reading is garbage and not the one the example wrote before.

Can you please explain why?

How do i read the data and make sure it stay there afer reset?

Thanks!

0 Kudos
Reply
3,535 Views
mjbcswitzerland
Specialist V

Eli

Data that has been programmed to flash will not be lost after a reset.

Make sure that your code is not (by mistake) re-writing to the Flash the next time that it runs (without first erasing it) since a second write to a phrase, without first erasing the sector that is in, is an invalid operation that can damage the flash and cause it to behave strangely (often causing hard-faults to occur when the corrupted area is read, where debuggers will tend to display the content as "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ").

Regards

Mark

0 Kudos
Reply
3,536 Views
DavidS
NXP Employee
NXP Employee

Hi Eli,

Attached is my source file (flash_erase_program_verify.c) that has been edited to test for wheter 0xff000 has been flashed or not.  If not flashed, then code continues as normal.  If 0xff000 has been flashed, then I memcpy to get data from 0xff000 and PRINTF it to terminal.

So on first pass of program where 0xff000 has not been flashed I get following:

Flash Example Start

Flash Information:

 Total Program Flash Size:1024 KB, Hex: (0x100000)

Program Flash Sector Size:4 KB, Hex: (0x1000)

 There is no D-Flash (FlexNVM) on this Device.

There is no Enhanced EEPROM (EEE) on this Device.

Flash is UNSECURE!

Erase a sector of flash

Successfully Erased Sector 0xff000 -> 0x100000

Program a buffer to a sector of flash

 Successfully Programmed and Verified Location 0xff000 -> 0xff010

End of Flash Example

On second pass by pressing the reset button on FRDM-K64F, I get following:

flash_buffer_contents preserved in flash

flash_buffer_contents[0] = 0x0

flash_buffer_contents[1] = 0x1

flash_buffer_contents[2] = 0x2

flash_buffer_contents[3] = 0x3

This matches what is in flash memory 0xff000 when I look at it in the Memory Browsers too.

Hope source helps.  Look for "//DES" to see locations modified.

Regards,

David

3,535 Views
eliar
Contributor II

Hi David, 

Thanks you so much , it is working for me also now.

I have a continues question.

Please tell me if i need to open a new thread or here it is ok.

I am using MBED ( disk drive D) to drag and drop binary file to burn(flash) the program i am writing.

every time i will do that , all the flash will be erase , including my personal data that we just wrote there.

How can i tell , if possible , to KDS not to overwrite sections in flash?

Or should it be only when using bootloader?

I assume i need to do something in the linker file, or?

If you dont understand my question please let me know.

Thanks

Eli

0 Kudos
Reply
3,535 Views
mjbcswitzerland
Specialist V

Eli

There are several mbed versions and newer ones only delete the blocks required by the new program. The version from Rev. 220 should stop your parameters being erased each time -you can get it from https://developer.mbed.org/handbook/Firmware-FRDM-K64F

If not, you can use a boot loader to take over the function of the mbed drag-and-drop loader - available at

http://www.utasker.com/kinetis/FRDM-K64F.html

Regards


Mark
Kinetis for professionals: http://www.utasker.com/kinetis.html

0 Kudos
Reply
3,535 Views
eliar
Contributor II

Thanks for your quick reply.

I am not think i am going to use the uTasker and its relevent system file.

i am using the Kinetis SDK and everything that NXP provide because this is how i started my project.

Thanks and sorry :smileyhappy:

Eli

0 Kudos
Reply
3,535 Views
mjbcswitzerland
Specialist V

Eli

I gave a link to the mdeb web site, which should solve your drag-and-drop problem. [I did try attaching the file for you but it looks like file attachment is broken since it says a 40k file is TOO LARGE....]

The second link is just in case it doesn't solve your problem and can be used with any SW - not just uTasker.

Regards

Mark
Kinetis for professionals: http://www.utasker.com/kinetis.html

P.S. If you had started with uTasker you may have finished the project by now :smileylaugh:

3,535 Views
eliar
Contributor II

Hi Mark,

Thank you so  much it is working with MBed now 

(for people that need to replace your file , just push the reset button and plug in the USB , the MBED will start as bootloader) and then it possible to replace the 220 version )

i did not heard before about uTasker , next project i will get to know with it.

Thanks for your quick replay.

Eli

0 Kudos
Reply
3,535 Views
mjbcswitzerland
Specialist V

Hi Eli

The code that I referenced to is a higher performance solution than the SDK (works with 9 IDEs, is Kinetis part independent, with many additional functions, including simulation), and so doesn't use the SDK components. It is a complete. industrially proven solution rather than just examples that you need to piece together for your final operation.

If you need to use SDK I can't help more that to say that it
- does include the functions that you need to write to Flash
- you can read back probably using function but it is simpler to just do memcpy(buffer, FlashAddress, size) since program Flash is memory-mapped
- there are many people who use such flash writing with SDK so it should basically be no big problem
- search the forum (also the SDK forum at https://community.nxp.com/community/kinetis/kinetis-software-development-kit )  for similar topics where potential problems with use of the SDK APIs are discussed and solutions given.

Regards

Mark

0 Kudos
Reply
3,535 Views
mjbcswitzerland
Specialist V

Hi

Write/read of non-used-program flash is possible (8 byte phrase writes required on the K64).

See the following, which has a binary for the FRDM-K64F that allows you to do this at any location (with description/example): https://community.nxp.com/message/831848?commentID=831848#comment-831848 

For various K64 boards see:
- http://www.utasker.com/kinetis/FRDM-K64F.html
- http://www.utasker.com/kinetis/TWR-K64F120M.html
where full code is available as well as a Kinetis simulator that allows the operation to be tested and analysed.

Regards

Mark
Kinetis for professionals: http://www.utasker.com/kinetis.html

0 Kudos
Reply