cancel
Showing results for 
Search instead for 
Did you mean: 

Storing variable in Flash memory

mani s
Associate III

I can store variables in flash memory location successfully but what is happening is updating that variable in main.c file not happening.I'm posted code what i have done below. 

Linker script file :

MEMORY

{

RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 320K

FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 153

MY_VARS (xrw) : ORIGIN = 0x08005000, LENGTH = 1K // storing in flash memory

}

.my_vars :

{

*(.MY_VARS*);

} >MY_VARS

In main.c file :

__attribute__((section(".MY_VARS"),myvars)) uint32_t *value=8;

int main()

{

int a =sum(); //i have a sum function of two numbers and storing in variable 'a'

value =&a; // value should updated to sum but it is not updating

readaddress=(const volatile uint32_t *)0x08005000;// reading flash memory 

printf("Read flash address %lu\n",*readaddress);// printing the data which is stored in value ,it is printing everytime 8 but it should print variable data which stored in 'a'.

}

int sum(int a)

{

int a=1, b= 34 ;

a=a+b;

return a;

}

Can anybody help me to figure this out,i hope u understood what im asking.

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions

Ok, but you've got some super weird C syntax, and assignment

__attribute__((section(".MY_VARS"),myvars)) uint32_t *value=8;

value =&a; // value should updated to sum but it is not updating

I'm talking about methods to actually write FLASH from an application, ie

STM32Cube_FW_F7_V1.11.0\Projects\STM32746G-Discovery\Examples\FLASH\FLASH_EraseProgram

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

12 REPLIES 12
AvaTar
Lead

> __attribute__((section(".MY_VARS"),myvars)) uint32_t *value=8;

Simply using:

const uint32_t value = 8;

will usually do the trick.

thanks avatar but my concern is below,

1.lets say i want to store in 0x08005000 location in flash.

2.i initilazed some data to value ,value = 8.

3.i have a function called sum() doing addition of two numbers ,this number should be stored in value and should be updated in flash.(see above for ref)

Look at the examples for writing values in to FLASH. They are more complicated than a simple assignment. You will need to enable access to the FLASH and erase prior to a write.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

yes i have read some references to store in flash,below is the link i followed blog.atollic.com/using-gnu-gcc-on-arm-cortex-devices-placing-code-and-data-on-special-memory-addresses-using-the-gnu-ld-linker

Ok, but you've got some super weird C syntax, and assignment

__attribute__((section(".MY_VARS"),myvars)) uint32_t *value=8;

value =&a; // value should updated to sum but it is not updating

I'm talking about methods to actually write FLASH from an application, ie

STM32Cube_FW_F7_V1.11.0\Projects\STM32746G-Discovery\Examples\FLASH\FLASH_EraseProgram

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

ok thanks for that

Is that method im doing wrong approach?

Piranha
Chief II

You can read flash memory like a simple variable, but You can't write it that way. For understanding how a flash memory works, read a FLASH section in Your MCUs reference manual. For understanding how to use FLASH for some data saving, read AN3969.

P.S. Independent of flash Your assignments among value, a and readaddress are logically wrong.

mani s
Associate III

thank you Piranha , will do that and update

mani s
Associate III

can we call a function from flash from another project?

lets say i have created two projects project-1 and project-2. i have stored a function called Sum(

) in the flash memory,so i want to call same Sum() function from the project-2.

I have stored project-1 in 0x08002000 and project-2 in 0x08004000,i stored Sum() function is stored in 0x08003000.

I'm using stm32f413zh board,total flash memory size is 1Mbytes.

Linker script file :

MEMORY

{

RAM (xrw)   : ORIGIN = 0x20000000, LENGTH = 320K

FLASH (rx)   : ORIGIN = 0x8000000, LENGTH = 1536K

MYFUNCTION (xr)  : ORIGIN = 0x08004000 , LENGTH = 1k

}

/* Define output sections */

SECTIONS

{

 /* The startup code goes first into FLASH */

 .isr_vector :

 {

  . = ALIGN(4);

  KEEP(*(.isr_vector)) /* Startup code */

  . = ALIGN(4);

 } >FLASH

/*myfunction memeory section start address is 0x08004000 */

.myfunction :

{

*(.MYFUNCTION*);

} > MYFUNCTION

In main.c file :

 __attribute__((section(".MYFUNCTION"))) int sum(int data);

int main()

{

sum(); //i have a sum function of two numbers and storing in variable 'a'

}

int sum(int a)

{

int a=1, b= 34 ;

a=a+b;

return a;

}