Posts: 76
Registered: ‎11-03-2016
Accepted Solution

FLASH writes not working properly in EFM32WG



I'm trying to implement some very simple NVM functionality in my code. Since the NVM variables are going to be updated just about 5 or ten times throughout all the life of the product, I don't need the EEPROM emulation. I don't care about FLASH wear.


Instead, I'm using the "User Data (UD)" space in flash indicated in the RM @ address 0x0FE00000 to declare my NVM variables. These variables will be declared with some "default" values and then later on the execution may or may not need to change the value of one, some, or all of them.


This is what I did in the efm32wg.ld file. First, I added another memory section at the top of the file


  FLASH (rx)    : ORIGIN = 0x00000000, LENGTH = 262144
  USER_DATA(rw) : ORIGIN = 0x0FE00000, LENGTH = 2048 <======= H E R E
  RAM (rwx)     : ORIGIN = 0x20000000, LENGTH = 32768

Then, I added the following below the FLASH section declarations



  .userDataBlock :

After that, I just created this initial test code:



#define USER_DATA_SECTION __attribute__((section (".userDataSection")))
#define WORD_ALIGNED      __attribute__ ((aligned (4)))

void moveInterruptVectorToRam(void);

 ***************************   GLOBAL VARIABLES   ******************************
uint32_t USER_DATA_SECTION WORD_ALIGNED var1 = 0x0B00B135;
uint8_t USER_DATA_SECTION WORD_ALIGNED  var3 = 0x12;

 **************************   GLOBAL FUNCTIONS   *******************************

 * @brief  Main function
int main(void)  
  uint32_t value = 0x12345678;

  /* Initialize chip */


  /* Move the interrupt vector table to RAM to safely handle interrupts
   * while performing write/erase operations on flash */

  /* Enables the flash controller for writing. */

  if (0x0B00B135 == var1)

  MSC_WriteWordFast(&var1, &value, sizeof(var1));

  /* Stay in this loop forever. */
  while (1) {


Since the variables var1, var2, and var3 are in this User Data FLASH area, they only get written with the default values when the MCU is flashed which is perfect. Later in the execution, if any variable is changed, it will keep its value across power cycles since the C startup code has no effect on flash declared variables thus obtaining true NVM variables.


Everything is perfect so far but...


Notwithstanding I can change the values of the variables only using either MSC_WriteWordFast or MSC_WriteWord APIs, the values written to the variables are wrong.


For example, you can see in the code that I'm changing the value of var1 from its default value to 0x12345678. Nevertheless, when single-stepping into the code the value gets changed to 0x2001030 and I have no idea why!!


Does anyone know what could be wrong?




Posts: 76
Registered: ‎05-19-2016

Re: FLASH writes not working properly in EFM32WG

Hi. Have not used this feature of the EFM32 processors but:


a) can you review the return code from the flash memory write ? What does this reveal?


b) Silabs notes recommend to erase the target page before the write. Can you try the same?


some references to the same routine and process to write to flash:


Review the example posted at the above URL.


Posts: 76
Registered: ‎11-03-2016

Re: FLASH writes not working properly in EFM32WG

@mon2 thanks for the suggestion.


It turned out that I indeed need to erase the page first. That's very annoying but I guess there is no other option.


Thanks for Helping!

Posts: 3,100
Registered: ‎02-07-2002

Re: FLASH writes not working properly in EFM32WG

The need to erase a full page is common for all flash technologies.


Further, I would expect the variables to need to be declared const to actually be initialized.