cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F40x flash storage

Posted on December 07, 2015 at 15:33

Hello there,

I am using stm32f407vtg (1MB flash). Apart from user program, I need to store some additional data in the flash memory. I need 3 banks: 1kB, 16 kB and 64 kB. I just expirienced that I need to erase whole flash sector in order to write to it (please correct me if I am wrong). In that case, the situation gets a bit complicated If I wanted to write certain banks without touching the others. This is the STM32F40x flash memory map:

https://www.dropbox.com/s/uge36boeomwuw6t/memoryMap.png?dl=0

If I wanted to use 3 last sectors, I would waste a lot of flash memory. I think It would be better to use sectors 2, 3 and 4 and start user application from sector 5. The question is, how can I tell the debugger, linker and everyone else that needs to know it that my program should start from address 0x08020000 instead of 0x08000000?

I would apreciate all help regarding this topic!
8 REPLIES 8
Posted on December 07, 2015 at 16:49

You use your linker script or scatter file to describe where you want it to place your code. For IAR and Keil there are GUI base options to describe the memories also.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on December 07, 2015 at 18:08

I dont have access to IAR ot KEIL. I am using eclipse and gcc toolchain with openSTM I have a linker script generated by my ide called STM32F407VGTx_FLASH.ld (I am loading program to flash). In there I am looking for ''text entry''

While file in here:https://pastebin.com/i4CKQApy all text entries i found are:

/* The program code and other data goes into FLASH */
.text :
{
. = ALIGN(4);
*(.text) /* .text sections (code) */
*(.text*) /* .text* sections (code) */
*(.glue_7) /* glue arm to thumb code */
*(.glue_7t) /* glue thumb to arm code */
*(.eh_frame)
KEEP (*(.init))
KEEP (*(.fini))
. = ALIGN(4);
_etext = .; /* define a global symbols at end of code */
} >FLASH

Maybe I am on a wrong track but how can I change the user flash address start from 0x0 to another one? Also, even If I manage to do it, will the startup code know where to jump?
Posted on December 07, 2015 at 19:44

...
/* Specify the memory areas */
MEMORY
{
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 1024K /* CHANGE THE START AND SIZE OF THIS REGION */
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 64K
}
..

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on December 07, 2015 at 19:48

Also, even If I manage to do it, will the startup code know where to jump?

You will need to place a loader at the usual 0x08000000 start address for the PROCESSOR. The loader would define the FLASH region as 0x08000000/0x4000 for its linker script, and you'd transfer control to your application code at 0x08020000/0xE0000

This stuff has been covered in numerous other threads, PLEASE review them.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on December 07, 2015 at 20:14

Clive, I have tried searching the forums before posting. I have found some threads that more or less adress my issue but after reading the posts they dont specifically solve my problem. Most of the only give some scraps of informations. In theory I know what has to be done but I dont know how to do that technically.

If I understand corretly, the startup code will start from address 0x0 no matter what yes? And I have to jump to my application from there in addition to linker script modification? Also, the vector table would have to be moved?

I really dont want to bother you, but I really apreciate your help. 
Posted on December 07, 2015 at 21:59

I have found some threads that more or less address my issue but after reading the posts they dont specifically solve my problem. Most of the only give some scraps of informations. In theory I know what has to be done but I dont know how to do that technically.

Ok, but you need to be able to make your own logical deductions, and review the documentation for the tools and parts you have chosen. My goal here is not to hold your hand through the whole process, but to point you in the right direction. A lot of the concepts are universally applicable, and just require adaption to specific circumstances.

If you don't want to use a loader, then your other option is to use the linker script to make two regions for the FLASH, where you have put a hole in the memory to accommodate your use of 3x 16KB and 1x 64KB blocks for configuration/storage, which you want to hide from the linker. The easiest probably being to use a section called BOOT, or something, which situates at 0x08000000/0x4000, and where you direct the Vector Table from startup.s

My personal preference would be to use the first 16KB for a loader, which allows me to update, and validate, the application code placed up at the 128KB mark. This also allows you to build something that is significantly hard to brick in the field. And per previous conversations, pull updates, scripts, or whatever, from a MicroSD card.

The processor is going to want to load it's initial SP/PC from the first two entries at 0x08000000, which is mapped also at zero based on the state of the BOOTx pins.

Joseph Yiu's book on the Cortex-M3/4 parts would be a good starting point for the CPU

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on December 07, 2015 at 22:04

Here a 16KB hole, but could be quite easily changed to 112KB one.

[DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/How%20to%20allocate%20variable%20in%20Flash%20to%20desired%20address&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=13030]https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2FHow%20to%20allocate%20variable%20in%20Flash%20to%20desired%20address&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=13030
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on December 07, 2015 at 22:21

Thank you for answer. I would also really like to make an aproach like you suggested- the bootloader always starts first so he can validate the user flash, no way to brick the device and etc. Thats really great. Because of that I dont have enough time I need I cant learn everything i wish to and I am forced to find half-solutions or ready examples. 

I will try to find a half way, I would eventually like to write a loader instead of using the build in one. Thank you for the provided example, i will try to make it run.