Read/Write to internal EEPROM on Nucleo L152RE

13 May 2014

Hi all, I was unable to find anything on the forum about reading and writing to the internal EEPROM on the Nucleo L152RE or anywhere else on the site so thought I'd give it a shot and have something that works.

Many thanks to 'bco stm' for pointing me in the right direction.

Note that this is by no way a complete solution, I've only worked on bytes but it should be easily adapted to half words and words. Also note that if you are trying to adapt it to other Nucleo boards you'll need to make sure you're writing to the correct memory region. On the L152RE the EEPROM starts at 0x08080000 other devices may/will be different. Also there is no testing to see if you are going past the EEPROM region so be careful not to over shoot the EEPROM or else bad things may happen.

With all those caveats out of the way, here it is:

#include "mbed.h"
#include "stm32l1xx_flash.h"

DigitalOut myled(LED1);
bool ledState = false;
FLASH_Status FLASHStatus;
Serial pc(SERIAL_TX, SERIAL_RX);



FLASH_Status writeEEPROMByte(uint32_t address, uint8_t data) {
    FLASH_Status status = FLASH_COMPLETE;
    address = address + 0x08080000;
    DATA_EEPROM_Unlock();  //Unprotect the EEPROM to allow writing
    status = DATA_EEPROM_ProgramByte(address, data);
    DATA_EEPROM_Lock();  // Reprotect the EEPROM
    return status;
}

uint8_t readEEPROMByte(uint32_t address) {
    uint8_t tmp = 0;
    address = address + 0x08080000;
    tmp = *(__IO uint32_t*)address;
    
    return tmp;
}


int main() {
    //while(1) {
        pc.printf("Attempting to write to EEPROM...");
        for (uint32_t i = 0; i < 256; i++) {
            FLASHStatus = writeEEPROMByte(i, (uint8_t)i);
            }
        if(FLASHStatus == FLASH_COMPLETE) 
            {pc.printf("Success!!\r\n");}
        else
            {pc.printf("Failed!!\r\n");}
           
        for (uint32_t i = 0; i < 256; i++) {    
            uint8_t storedValue = readEEPROMByte(i);
            pc.printf("Stored value: %d \n\r", storedValue);
            }
            
        //Flash LED to let us know something is happening
        ledState= !ledState;
        myled = ledState; 
        wait(1.0); // Wait a bit so we're not hammering the EEPROM
    //}
}

TheCageybee

15 Jun 2016

Now, mbed provides you the abstraction for "stm32l1xx_flash.h" and you don't need to add it separately. Although you may need to update the code because they uses different names. e.g the above function will change like this:

include the mbed library with this snippet

HAL_StatusTypeDef writeEEPROMByte(uint32_t address, uint8_t data)
 {
    HAL_StatusTypeDef  status;
    address = address + 0x08080000;
    HAL_FLASHEx_DATAEEPROM_Unlock();  //Unprotect the EEPROM to allow writing
    status = HAL_FLASHEx_DATAEEPROM_Program(TYPEPROGRAMDATA_BYTE, address, data);
    HAL_FLASHEx_DATAEEPROM_Lock();  // Reprotect the EEPROM
    return status;
}

I have tried it on uVision v5 and it's working fine, I'm not sure about the online compiler. For complete documentation: http://developer.mbed.org/users/mbed_official/code/mbed-src/docs/7f9d41292847/stm32l1xx__hal__flash__ex_8c.html

30 Jul 2017

Thank you!