STM32F407 flash library

This forum is about you. Feel free to discuss anything is related to embedded and electronics, your awesome projects, your ideas, your announcements, not necessarily related to ChibiOS but to embedded in general.
jeremie.delaitre
Posts: 26
Joined: Fri Oct 05, 2012 3:16 pm
Been thanked: 1 time

STM32F407 flash library

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

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!

User avatar
Tectu
Posts: 1226
Joined: Thu May 10, 2012 9:50 am
Location: Switzerland
Contact:

Re: STM32F407 flash library

Postby Tectu » Mon Dec 10, 2012 5:08 pm

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

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

Re: STM32F407 flash library

Postby jeremie.delaitre » Mon Dec 10, 2012 6:39 pm

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.

trunet
Posts: 54
Joined: Fri Apr 06, 2012 7:18 pm

Re: STM32F407 flash library

Postby trunet » Mon Dec 31, 2012 3:27 pm

Hi,

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

Thanks,

Wagner Sartori Junior

User avatar
TexZK
Posts: 57
Joined: Sat Sep 22, 2012 7:06 pm
Location: Lodi, Italy
Contact:

Re: STM32F407 flash library

Postby TexZK » Fri Jan 04, 2013 1:06 am

Hmmm interesting library 8-) I think I'll try it soon to store some persistent user options for one of my '407 projects ;)

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

Re: STM32F407 flash library

Postby jeremie.delaitre » Tue Jan 22, 2013 11:57 am

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.

User avatar
russian
Posts: 310
Joined: Mon Oct 29, 2012 3:17 am
Location: Jersey City, USA
Has thanked: 12 times
Been thanked: 5 times

Re: STM32F407 flash library

Postby russian » Wed Jul 24, 2013 9:16 pm

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

User avatar
russian
Posts: 310
Joined: Mon Oct 29, 2012 3:17 am
Location: Jersey City, USA
Has thanked: 12 times
Been thanked: 5 times

Re: STM32F407 flash library

Postby russian » Fri Sep 20, 2013 3:51 am

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;
}

tiko
Posts: 2
Joined: Tue Jul 01, 2014 4:01 pm

Re: STM32F407 flash library

Postby tiko » Tue Jul 01, 2014 4:07 pm

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

deadlyboy
Posts: 27
Joined: Wed Nov 26, 2014 9:27 am

Re: STM32F407 flash library

Postby deadlyboy » Thu Mar 26, 2015 11:36 am

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


Return to “User Projects”

Who is online

Users browsing this forum: No registered users and 2 guests