5 years, 11 months ago.

Read/Write to the EEPROM of Nucléo-L476RG

Hello all,

I'm a beginner in programming and I'm actually trying to write and read to the EEPROM memory of my nucléo card with an STM32L476RG microcontroller (to save data of a measuring tool). I saw lots of code and one of them looks good, it is on a post of this forum : https://os.mbed.com/forum/mbed/topic/4912/ I changed the memory address to correspond with the datasheet of my nucléo (0x40005400) and updated the code like someone who answered suggested. I obtained this :

include the mbed library with this snippet

#include "mbed.h"
 
DigitalOut myled(LED1);
bool ledState = false;
FLASH_Status FLASHStatus;
Serial pc(SERIAL_TX, SERIAL_RX);
 
 
 
HAL_StatusTypeDef writeEEPROMByte(uint32_t address, uint8_t data) {
     HAL_StatusTypeDef  status;
    address = address + 0x40005400; // début de l'adresse de I2C1
    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;
}
 
uint8_t readEEPROMByte(uint32_t address) {
    uint8_t tmp = 0;
    address = address + 0x40005400;
    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
    //}
}

But I still obtained lots of errors so I'm ask myself some questions : Must I have a specific library ? Are functions used compatible with my nucléo card ?

Thank you

Be the first to answer this question.