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!
STM32F407 flash library
-
- Posts: 26
- Joined: Fri Oct 05, 2012 3:16 pm
- Been thanked: 1 time
Re: STM32F407 flash library
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
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
-
- Posts: 26
- Joined: Fri Oct 05, 2012 3:16 pm
- Been thanked: 1 time
Re: STM32F407 flash library
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.
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
Hi,
To use this library will I have to alter my linker and startup file? Did you have any example?
Thanks,
Wagner Sartori Junior
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
Hmmm interesting library I think I'll try it soon to store some persistent user options for one of my '407 projects
-
- Posts: 26
- Joined: Fri Oct 05, 2012 3:16 pm
- Been thanked: 1 time
Re: STM32F407 flash library
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.
- russian
- Posts: 364
- Joined: Mon Oct 29, 2012 3:17 am
- Location: Jersey City, USA
- Has thanked: 16 times
- Been thanked: 14 times
Re: STM32F407 flash library
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
- russian
- Posts: 364
- Joined: Mon Oct 29, 2012 3:17 am
- Location: Jersey City, USA
- Has thanked: 16 times
- Been thanked: 14 times
Re: STM32F407 flash library
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
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
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
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
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
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
Who is online
Users browsing this forum: No registered users and 19 guests