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.
Howto saving Data in Flash in Cortex M3/M4
Moderators: RoccoMarco, lbednarz, utzig, tfAteba, barthess
-
- Posts: 26
- Joined: Fri Oct 05, 2012 3:16 pm
- Been thanked: 1 time
Re: Howto saving Data in Flash in Cortex M3/M4
@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
@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
Re: Howto saving Data in Flash in Cortex M3/M4
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.
-
- 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
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?
Re: Howto saving Data in Flash in Cortex M3/M4
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.
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.
-
- Posts: 26
- Joined: Fri Oct 05, 2012 3:16 pm
- Been thanked: 1 time
Re: Howto saving Data in Flash in Cortex M3/M4
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?)
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?)
-
- 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
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
-
- Posts: 26
- Joined: Fri Oct 05, 2012 3:16 pm
- Been thanked: 1 time
Re: Howto saving Data in Flash in Cortex M3/M4
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!
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!
- 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
Hi,
Thanks for the library, do you mind creating a post under "Contributions", that way people will find it easily.
Giovanni
Thanks for the library, do you mind creating a post under "Contributions", that way people will find it easily.
Giovanni
-
- Posts: 26
- Joined: Fri Oct 05, 2012 3:16 pm
- Been thanked: 1 time
Who is online
Users browsing this forum: No registered users and 18 guests