cancel
Showing results for 
Search instead for 
Did you mean: 

How to config sct file in Keil MDK to store user data in a section of STM32's flash?

ZYu.1
Associate III

I want to store my software version and data at a pointed section of STM32's Flash.I have tried to config the stc file in keil mdk,but it seems not working.So could anyone help me to find which step not right,thanks.

1. I set in keil to use user configured stc file;

0693W000007ZYpHQAW.png

2. I set the sct file like this:

;Add 0x0x0801B000 start address,2k=0x800 length to define as myram section to store user data

0693W000007ZYt3QAG.png

3.I define MYRAM as __attribute__((section("myram"))):

0693W000007ZYufQAG.png

4.I use MYRAM to define user data const variable:

0693W000007ZYwCQAW.pngthen i rebuild this program and download the hex to STM32G4,but the sct file defined style

variable could not be found at MYRAM section at 0x0801B000;

0693W000007ZYz6QAG.png

But when I use the style liking attribute at + address,it is useful;

0693W000007ZZ0JQAW.png

So the attribute at + address method is useful,but if I want to define another const variable behind the UserData_BlockValue2, I have to calculate the address because of the length of UserData_BlockValue2, this seems a little tedious.

I want to know is it possible to use sct file to define a section of flash to store my const variables Value1,Value2,Valu3...and so on,these variables' address are automaticlly in turn loacted at the section? Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions

Hello

define the version string as const

static const char xxxx[]={"version string"};// ensure will COMPILE as RO DATA

Make a new REGION and write to LOAD adress *(.rodata.xxxx)

LR_IROM2 0x0801B000 0x00000800 {   ; load region size_region

 ER_IROM2 0x0801B000 0x00000800 { ; load address = execution address

  *(.rodata.xxxx)

 }

}

Note that, LR_IROM2 region must be outside of the default region.

View solution in original post

4 REPLIES 4

Hello

define the version string as const

static const char xxxx[]={"version string"};// ensure will COMPILE as RO DATA

Make a new REGION and write to LOAD adress *(.rodata.xxxx)

LR_IROM2 0x0801B000 0x00000800 {   ; load region size_region

 ER_IROM2 0x0801B000 0x00000800 { ; load address = execution address

  *(.rodata.xxxx)

 }

}

Note that, LR_IROM2 region must be outside of the default region.

Thanks, combine your advice and my searching result by Google,I find a more useful method:

1.Revise sct file and define a section for userdata;

Here I define myram form 0x0801C000 with a length of 2K(0x00000800),this section is put at outside of the default region;

0693W000007ZeCPQA0.png2.Use macro definition in your program;

Here use keyword attribute to locate at the pointed section, keyword used will prevent the defined const variable from being ignored by MDK compiler even though it is not referenced at anywhere in your program code.This will be helpful when you want to store your software version at a pionted section at STM32's FLASH,maybe you will never read or write this software version until the product go back home for repairing,then you could use STLINK to read the software verion at the pointed address.So the keyword used is useful.If not used this keyword,you will not find the initial data for your const variable when this variable is not referenced at any place in your code.

0693W000007ZeCoQAK.png3.Use macro definition to define the const variable;

Here I jsut define the const variables for user data at the pointed section,they will be stored one by one at the pointed address without my own calculating starting address for each varioable,so it is convenient.

0693W000007ZeDmQAK.png4.At last, let me check the falsh data at my defined section at the pointed address,the data is as what I want to be stored.

0693W000007ZeE6QAK.png 

Thanks for Vangelis Fortounas's answering and advice,here this article is also helpful for this solution:

https://www.amobbs.com/thread-5724606-1-1.html.

So this question has a good result,may it will help someone else.

Hello

@ZYu.1​ 

Note also the macros __DATE__ and __TIME__ that are available at compile time . Usefull to embed compilation date and time to flash memmory

Thank you,that's a good advice.