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
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:
TheCageybee