Skip to main content
mani s
Associate III
February 8, 2019
Solved

Storing variable in Flash memory

  • February 8, 2019
  • 12 replies
  • 4236 views

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

    This topic has been closed for replies.
    Best answer by Tesla DeLorean

    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

    12 replies

    AvaTar
    Senior III
    February 8, 2019

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

    Simply using:

    const uint32_t value = 8;

    will usually do the trick.

    mani s
    mani sAuthor
    Associate III
    February 8, 2019

    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)

    Tesla DeLorean
    Guru
    February 8, 2019

    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Piranha
    Principal III
    February 9, 2019

    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
    mani sAuthor
    Associate III
    February 11, 2019

    thank you Piranha , will do that and update

    mani s
    mani sAuthor
    Associate III
    February 18, 2019

    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;

    }

    Tesla DeLorean
    Guru
    February 18, 2019

    You could use a function pointer. The address of the function should be ODD. Confirm it's placement by reviewing the .MAP file

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    mani s
    mani sAuthor
    Associate III
    February 18, 2019

    @Community member​  can you elaborate the answer ?

    My understand is ,to create a fun pointer in the flash and excess from the project-2.

    what do you mean by ODD ? why it should be odd.