Page 1 of 2

STM32F407 flash library

Posted: Mon Dec 10, 2012 2:34 pm
by jeremie.delaitre
Hi,

I finally had time to "finish" a 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 can integrate it easily in your own stuff and never touch to CMake ;)

Comments are welcome!

Re: STM32F407 flash library

Posted: Mon Dec 10, 2012 5:08 pm
by Tectu
Thank you very much for sharing your work!

I guess this will be used by ChibiOS/GFX very soon (I have to check the licensing first) to store calibration data of touchscreens into the internal memory. I guess there's no reason why this shouldn't be possible?


~ Tectu

Re: STM32F407 flash library

Posted: Mon Dec 10, 2012 6:39 pm
by jeremie.delaitre
I am glad it could be helpful to someone else :)

Storing data into internal flash memory is one of my two goals indeed :)
However, a think a slightly higher level API could be use to ease that but it is quite application specific. This is mainly due to the f407 fancy flash memory layout with sectors of different size...

For example, if you want to update only one part of your settings, you will have to read the previous data first, then erase the sector and then write an up to date version of the settings.
To be noted that the f407 has some fairly big sectors (128ko) in the end of its layout, so it could be much more efficient to use some kind of wear leveling on those if your settings do not take a full sector.

Regarding the license, the code was inspired by mabl's work on his P107 bootloader. The original license was not clear but he basically says that we can do whatever we want with it (see: viewtopic.php?f=2&t=388&p=8028#p7637). To be noted that the code contains an IHex file parser (was already in mabl's bootloader but I don't know the license), and a jump function extracted from a thread in the chibios forum. You can find more info in the readme, in ihex.h and in helper.h (I think...).

For my own code, something like LGPL is fine but as the other code license is not quite clear, I did not put it "officially" at the moment.

Re: STM32F407 flash library

Posted: Mon Dec 31, 2012 3:27 pm
by trunet
Hi,

To use this library will I have to alter my linker and startup file? Did you have any example?

Thanks,

Wagner Sartori Junior

Re: STM32F407 flash library

Posted: Fri Jan 04, 2013 1:06 am
by TexZK
Hmmm interesting library 8-) I think I'll try it soon to store some persistent user options for one of my '407 projects ;)

Re: STM32F407 flash library

Posted: Tue Jan 22, 2013 11:57 am
by jeremie.delaitre
trunet wrote:Hi,

To use this library will I have to alter my linker and startup file? Did you have any example?

Thanks,

Wagner Sartori Junior


Sorry for the delay, I didn't noticed your message...
You don't have to modify your linker script to read/write the flash memory, but it could be a good idea though. For example, you can reserve the last sectors for your persistent data. Thus, you can reflect that in your linker script by reducing the size allocated for your program code to make sure it doesn't overlap with your data.

Re: STM32F407 flash library

Posted: Wed Jul 24, 2013 9:16 pm
by russian
jeremie.delaitre wrote:Sorry for the delay, I didn't noticed your message...
You don't have to modify your linker script to read/write the flash memory, but it could be a good idea though. For example, you can reserve the last sectors for your persistent data. Thus, you can reflect that in your linker script by reducing the size allocated for your program code to make sure it doesn't overlap with your data.


Jeremie, would you consider adding an example of how to use your library into the git project and maybe elaborate a bit on how the linker script should be changed? I mean a tiny introduction so that anyone could just simply use this stuff with less research :)

For instance, just an example which saves 1K of data and increments a persistent counter every time the app is rebooted or say every minute

Re: STM32F407 flash library

Posted: Fri Sep 20, 2013 3:51 am
by russian
I've got this code working!

Two issues I have faced:
1) It took me 30 minutes to figure out that I should read the documentation and start invoking 'flashErase'
2) size_t seems to be unsigned in my case and that means that flashErase method was hanging, I had to change it a bit

Code: Select all

int flashErase(flashaddr_t address, size_t size) {
   while (size > 0) {
      flashsector_t sector = flashSectorAt(address);
      int err = flashSectorErase(sector);
      if (err != FLASH_RETURN_SUCCESS)
         return err;
      address = flashSectorEnd(sector);
      size_t sector_size = flashSectorSize(sector);
      if (sector_size >= size)
         break;
      size -= sector_size;
   }

   return FLASH_RETURN_SUCCESS;
}

Re: STM32F407 flash library

Posted: Tue Jul 01, 2014 4:07 pm
by tiko
Hi there,

how do you compile your code?
I would like to see your fork with the cmake build-system, but I cannot find it.
Could anyone gibe an advice or a pointer to the cmake-fork?

Thanks,
Tiko

Re: STM32F407 flash library

Posted: Thu Mar 26, 2015 11:36 am
by deadlyboy
hi

i can easily read from and write into flash in my stm32f4 discovery board , but in flash , i nead to change 50 bytes at runtime , but it seems it's not possible, i suppose ,i should erase whole sector just for overwrite 50 bytes and of course it takes time, is there any possibility to overwrite these value without erasing whole sector ??

thanks in advance