Howto saving Data in Flash in Cortex M3/M4

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, utzig, tfAteba, barthess

mobyfab
Posts: 483
Joined: Sat Nov 19, 2011 6:47 pm
Location: Le Mans, France
Has thanked: 21 times
Been thanked: 30 times

Re: Howto saving Data in Flash in Cortex M3/M4

Postby mobyfab » Mon Nov 12, 2012 4:38 pm

You can't write a flash page that has not been previously erased.

Also you have to unlock the flash page before writing it.

If I were you I would write at the end of the flash and do some wear leveling.
Like if you need 16K use a 128K page and divide it in 8 areas. You only erase the page once every 8 writes. (so 8 times less flash wear)
You just need to check in which of the 8 areas the current data is. simply check which is the last area that is not empty.

jeremie.delaitre
Posts: 26
Joined: Fri Oct 05, 2012 3:16 pm
Been thanked: 1 time

Re: Howto saving Data in Flash in Cortex M3/M4

Postby jeremie.delaitre » Mon Nov 19, 2012 4:26 pm

@mobyfab: thanks for the tips :) Using wear leveling seems to be a good idea even if the user should not change the settings that often!

@mabl: I've looked at your code and it seems quite clear indeed. I'm trying to port it to the STM32F407 but some registers are gone (for example the FLASH->AR) and the flash memory is more complicated that in the F107 (there are different page sizes...) so it need real rework. My question is where did you find the documentation and all the information for the F107? I looked in the reference manual without success (the same in the F407 manual) and I did not find any information on the Internet for either controllers. I thought I can maybe take a look at the openocd code but I never did it (so it may be a bit complicated) so I'm wondering if there is a real document somewhere before digging into openocd mysteries :)

Abhishek
Posts: 266
Joined: Wed May 23, 2012 3:15 pm
Location: India

Re: Howto saving Data in Flash in Cortex M3/M4

Postby Abhishek » Mon Nov 19, 2012 6:04 pm

You would need to check in a different datasheet called the 'STM32 F2/F4 Flash programming manual' in the section on the ST website where you get the datasheet. They describe it there.

mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Location: Karlsruhe, Germany
Been thanked: 1 time
Contact:

Re: Howto saving Data in Flash in Cortex M3/M4

Postby mabl » Wed Nov 21, 2012 7:35 am

Abhishek wrote:You would need to check in a different datasheet called the 'STM32 F2/F4 Flash programming manual' in the section on the ST website where you get the datasheet. They describe it there.

Correct.

I'm also going to replace my F10x with F4xx mcus so I'd be interested in such a driver, too. Will you publish your code Jeremie?

Abhishek
Posts: 266
Joined: Wed May 23, 2012 3:15 pm
Location: India

Re: Howto saving Data in Flash in Cortex M3/M4

Postby Abhishek » Wed Nov 21, 2012 8:11 am

In the latest revision of the STM32F4XX reference manual, they have moved the Flash programming manual there, and removed all references to that document.

So it would be better to get yourself a copy of the latest reference manual from the ST website instead.
Also, Have a look at the topic in Dev & Feedback: New STM32F42X/43X Devices, they have upto 2 MB ram, 256 + 4 KB SRAM and 6 SPIs.

jeremie.delaitre
Posts: 26
Joined: Fri Oct 05, 2012 3:16 pm
Been thanked: 1 time

Re: Howto saving Data in Flash in Cortex M3/M4

Postby jeremie.delaitre » Wed Nov 21, 2012 9:28 am

I found the flash programming manual yesterday with all the needed info.
I've started to port the p107 bootloader to the p407 but it still a work in progress.

I'll publish the code when it'll be ready as I will appreciate any comment on the code since I'm not used to embedded programming yet :)
Moreover, as the code is based on mabl's one it is just normal to publish it (I did not find a license in the project but as it is ChibiOS based I imagine it is under LGPL and/or GPL?)

mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Location: Karlsruhe, Germany
Been thanked: 1 time
Contact:

Re: Howto saving Data in Flash in Cortex M3/M4

Postby mabl » Wed Nov 21, 2012 9:51 am

jeremie.delaitre wrote:I'll publish the code when it'll be ready as I will appreciate any comment on the code since I'm not used to embedded programming yet :)
Moreover, as the code is based on mabl's one it is just normal to publish it (I did not find a license in the project but as it is ChibiOS based I imagine it is under LGPL and/or GPL?)

You could use it with ChibiOS stable and GPL exceptions - so it *could* be a commercial licence. In any case, I don't care much on what you do with it ;-) It only took me less than a day to code. Feedback and improvements are always welcome of course.

Btw. You can easily enable debugging features and stuff - but make sure you move the start of the user flash past your bootloader :D

jeremie.delaitre
Posts: 26
Joined: Fri Oct 05, 2012 3:16 pm
Been thanked: 1 time

Re: Howto saving Data in Flash in Cortex M3/M4

Postby jeremie.delaitre » Mon Dec 10, 2012 1:38 pm

Hi !

I finally had time to "finish" the flash library for the STM32F407. The code is available at: https://github.com/tegesoft/flash-stm32f407

The library allows to erase sectors and read/write data. A helper function allows to flash an IHex file (almost like in the original mabl's work).
There is no bootloader in the repository because I wanted to have a "flash library" with some helpers allowing the use in a bootloader AND in a traditional firmware too (e.g. to read/write "persistent data" in flash for example).

There are a lot of unit tests provided though.

To be noted that the build system is based on CMake and uses our fork of ChibiOS (https://github.com/tegesoft/ChibiOS, which basically just add a CMake build system for what we are using).
As the "library" is just formed by 3 headers and 3 C files, you canintegrate it easily in your own stuff and never touch to CMake ;)

Comments are welcome!

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Howto saving Data in Flash in Cortex M3/M4

Postby Giovanni » Mon Dec 10, 2012 1:50 pm

Hi,

Thanks for the library, do you mind creating a post under "Contributions", that way people will find it easily.

Giovanni

jeremie.delaitre
Posts: 26
Joined: Fri Oct 05, 2012 3:16 pm
Been thanked: 1 time

Re: Howto saving Data in Flash in Cortex M3/M4

Postby jeremie.delaitre » Mon Dec 10, 2012 2:36 pm



Return to “General Support”

Who is online

Users browsing this forum: No registered users and 16 guests