cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F2xx Save User Data (ie virtual eeprom)

dj
Associate II
Posted on April 01, 2014 at 02:45

I have been attempting to store a few bytes in some non-volatile memory on the STM32fxx. From what I understand, there are two places to save data that does not require vbat battery backup... I may be out to lunch, but this is what I understand?

1) rtc backup (RTC->BKP0R)

2) Flash program memory

I have attempted to store user data in both of these locations without success. Here are the functions that I used which only seem to work while the chip is powered. Once the power is cycled, their values are lost.

1) Store the variable in the RTC Backup memory

void ee_write_to_backup_rtc(uint32_t data, uint32_t offset) {

 PWR_BackupAccessCmd(ENABLE);

 uint32_t tmp = (uint32_t)&(RTC->BKP0R);

 tmp += offset;

 *(__IO uint32_t *)tmp = (uint32_t)data;

 PWR_BackupAccessCmd(DISABLE);

 }

uint32_t ee_read_from_backup_rtc(uint16_t offset) {

 uint32_t tmp = (uint32_t)&(RTC->BKP0R);

 tmp += offset;

 return (*(__IO uint32_t *)tmp);

}

2) Store the variable in the ROM by modifying the value using the Flash functions

const uint32_t _EEPROM[6] = { 0, 0, 0, 0, 0, 0 };

void ee_write_to_backup_rtc(uint32_t data, uint32_t offset) {

 FLASH_Unlock();

 FLASH_ProgramWord((uint32_t)&_EEPROM + offset, data);

 FLASH_Lock();

}

uint32_t ee_read_from_backup_rtc(uint16_t offset) {

  return _EEPROM[offset];

}

 
6 REPLIES 6
dj
Associate II
Posted on April 01, 2014 at 04:01

I tried making this... and it seems to work occasionally, but not always. It's as if the FLASH_ProgramWord() isn't always setting the data

eeprom.h

-------------------------------------------------------------------

#ifndef __EEPROM_H

#define __EEPROM_H

#include ''stm32f2xx.h''

#include ''stm32f2xx_flash.h''

#define EEPROM_START_ADDRESS  ((uint32_t)0x08070000)

void ee_write_to_backup_rtc(uint32_t data, uint8_t offset);

uint32_t ee_read_from_backup_rtc(uint8_t offset);

#endif

eeprom.c

-------------------------------------------------------------------

#include ''eeprom.h''

void ee_write_to_backup_rtc(uint8_t data, uint8_t offset) {

  FLASH_Unlock();

  FLASH_ProgramWord(EEPROM_START_ADDRESS + (uint32_t)offset, data);

  FLASH_Lock();

}

uint8_t ee_read_from_backup_rtc(uint8_t offset) {

  uint32_t *uid = (uint32_t *)EEPROM_START_ADDRESS;

  return (uint8_t)uid[offset];

}
Posted on April 01, 2014 at 04:51

BKP require some power

FLASH can only be written when it is blank, so you'll have to erase a whole sector, or journal write across an erased region. Erased FLASH on the F2 will contain 0xFF

Also observe that 32-bit pointer's advance 4 bytes are a time, so you're not reading and writing the same location. ie (uint32_t)&_EEPROM + offset != &_EEPROM[offset]
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
dj
Associate II
Posted on April 01, 2014 at 06:48

Is there a reference to the sector memory map somewhere?

Also, when you say journal write across a region - are you referring to a Word? How large is a Region - or is a region (for example) the size of ''Flash'' 0x0800 0000 to 0x080F FFFF?

My program is 379,100 Bytes, and i'm using the 512k STM32fxx - so if I can identify the sector that resides above 400k, i'll be golden

dj
Associate II
Posted on April 01, 2014 at 07:39

Thanks clive1 (As usual you're an allstar)

I was wondering what sector 0x0807 0000 would be - and I couldn't find a document. I was browsing the STM32 ST-Link software and found a button Erase Sectors, which showed me the sector map. Here it is for reference for future people...

sector 0 0x800 0000 (16k)

sector 1 0x800 4000 (16k)

sector 2 0x800 8000 (16k)

sector 3 0x800 C000 (16k)

sector 4 0x801 0000 (64k)

sector 5 0x802 0000 (128k)

sector 6 0x804 0000 (128k)

sector 7 0x806 0000 (128k)

I fixed my eeprom emulator (As per your suggestions of the word size) and it's working great! Thanks again

Posted on April 01, 2014 at 18:07

Sector 7 spans 0x08060000..0x0807FFFF

Usually one would floor-plan the flash usage to place a boot loader, and configuration/calibrate/eeprom type data in the forward 16 KB sectors. The boot loader would need to be placed at 0x08000000 to manage the initial execution, and you'd push the app code downstream to the 64KB (0x08010000) or 128KB (0x08020000) boundary.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..