4
votes

I am implementing a emulated EEPROM in flash memory on a STM32 microprocessor, mostly based on the Application Note by ST (AN2594 - EEPROM emulation in STM32F10x microcontrollers).

The basics outline there and in the respective Datasheet and Programming manual (PM0075) are quite clear. However, I am unsure regarding the implications of power-out/system reset on flash programming and page erasure operations. The AppNote considers this case, too but does not clarify what exactly happens when a programming (write) operations is interrupted:

  1. Does the address have a arbitrary (random) value? OR
  2. Are only part of the bits written? OR
  3. Does it have the default erase value 0xFF?

Thanks for hints or pointers to the relevant documentation.

Arne

4
I don't have a reference to back me up, but I'd think that if a power outage occurred after a write or erase operation has started and before the operation has completed then you can't rely on any particular state for the location (or page).Michael Burr
Migrated by author to Electrical Engineering StackExchange site. electronics.stackexchange.com/questions/25501/…Potatoswatter

4 Answers

7
votes

This is not really a software question (much less C++). It belongs on electronics.se, but there does not seem to be an option to migrate questions there… only to sites such as superuser or webmasters.se.

The short answer is that hardware is inherently unreliable. Something can always in theory go wrong that interrupts the write process or causes the wrong bit to be written.

The long answer is that Flash circuits are usually designed for maximum reliability. A sudden power loss on write will probably not cause corruption because the driver circuit may have enough capacitance or the capability to operate under a low-voltage condition long enough to finish draining the charge as necessary. A power loss on erasure might be trickier. You really need to consult the manufacturer.

For a "soft" system reset with no power interruption, it would be pretty surprising if the hardware didn't always completely erase whatever bytes it was immediately working on. Usually the bytes are erased in a predefined order, so you can use the first or last ones to indicate whether a page is full or empty.

3
votes
#include "stm32f10x.h"
#define FLASH_KEY1      ((uint32_t)0x45670123)
#define FLASH_KEY2      ((uint32_t)0xCDEF89AB)
#define Page_127        0x0801FC00
uint16_t i;
int main()
{
  //FLASH_Unlock
  FLASH->KEYR = FLASH_KEY1;
  FLASH->KEYR = FLASH_KEY2;
  //FLASH_Erase Page
  while((FLASH->SR&FLASH_SR_BSY));
  FLASH->CR |= FLASH_CR_PER; //Page Erase Set
  FLASH->AR = Page_127; //Page Address
  FLASH->CR |= FLASH_CR_STRT; //Start Page Erase
  while((FLASH->SR&FLASH_SR_BSY));
  FLASH->CR &= ~FLASH_CR_PER; //Page Erase Clear
  //FLASH_Program HalfWord
  FLASH->CR |= FLASH_CR_PG;
  for(i=0; i<1024; i+=2)
  {
    while((FLASH->SR&FLASH_SR_BSY));
    *(__IO uint16_t*)(Page_127 + i) = i;
  }
  FLASH->CR &= ~FLASH_CR_PG;
  FLASH->CR |= FLASH_CR_LOCK;
  while(1);
}
1
votes

If you are using the EEProm Emulation driver, you shouldn't worry too much about the flash corruption issues as the EEProm emulation driver always keeps a shadow copy in another page. Worst come worst, you will lose the most recent values that are being written into the flash. If you look closely on the emulation driver, you will notice that it is nothing but essentially a wrapper to stm32fxx_flash.c in the standard peripheral library. If you look at the application note, you will see the times that the emulation library take for the flash operations. Erasing a page typically takes the longest time (tens of milliseconds on M0 core - this depends on the clock frequency).

0
votes

If you are using the EEProm Emulation driver, you had bettern add a function such as check the data after write finished. For example, if you have 10 data to save, so you need write 11 bytes to flash. The last byte is checksum. And check the data after read from flash.