cancel
Showing results for 
Search instead for 
Did you mean: 

Storing block of memory (struct data) on microcontrollers non-volatile memory (STM32F103)

cnhx27
Associate III
Posted on November 04, 2014 at 18:22

I want to be able to store values in a non-volatile memory so that the microcontroller can load these settings at start up.

These settings might change at run-time and it is important for the systems functionality that these settings are stored.

I know the only non-volatile memory on the STM32F103 is the flash and it is possible to save values on flash(emulated as EEPROM for ease of use) according to this Appnote ''EEPROM emulation in STM32F101xx and STM32F103xx microcontrollers (AN2594)''(

http://www.st.com/web/en/catalog/tools/FM147/CL1794/SC961/SS1743/LN1734/PF257846

)

How can I store block of memory (struct data) in flash memory?

One idea is to write this struct into flash half word by half word. Bad idea?

Thanks a lot for your answers 😉

#stm32f103-flash-memory
2 REPLIES 2
Posted on November 04, 2014 at 18:27

The EEPROM Emulation is a bit ugly.

You'd just want to define a structure, a copy the new one in by erasing the sector/page and writing the flash a word at a time. You can access (read) the data in place. You could also journal copies of the structure and pick the most recent, as a method to reduce wear on the flash cells.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
cnhx27
Associate III
Posted on November 05, 2014 at 07:56

For example I have this struc

struct

t_mastruct

{

char

ch

;

int

nb

;

float

pi

;

}

;

t_mastruct variable

;

variable.

ch

=

'a'

;

variable.

nb

=

12345

;

variable.

pi

=

3.141592

;

To write this on EEPROM, I do

#define SETTINGSSAVED_FLASHADDR 0x0801FC00

    FLASH_Unlock();

    FLASH_ErasePage(SETTINGSSAVED_FLASHADDR);

   

    // How to write 'variabler' on EEPROM ?   

    // Perhaps this ?

/*

 uint32_t *p = (uint32_t*)&variable;

 uint32_t Address;

 for ( Address = 0; Address < sizeof(t_mastruct); Address++ ){

     FLASH_ProgramHalfWord(SETTINGSSAVED_FLASHADDR, (volatile uint32_t)(*p));

     SETTINGSSAVED_FLASHADDR += 2;

 }

*/

    FLASH_Lock();