Document

10. MXCUBE HAL STM32F407 SIMULATE EEPROM


  1. Introduce
  2. Hello everyone, today I introduce using simulate EEPROM(using flash memory) instead real EEPROM. In history, i have some doubt how to save data in ECU before power off and load it again after power on again. Data in here we can change during run, not fixed constant value. After search in google, I see it and I think it may be useful if someone need it.

    Reference link

    In STM32, to save cost, manufacturer usually provide flash memory and skip EEPROM. For flash memory, they only support erase follow sector or page and we need erase before write. It difference with EEPROM, we can erase/write/read follow byte and we can overwrite. Below is comparison from my understand between flash memory and EEPROM.

    Advantage and disadvantage use Simulate EEPROM:

    Advantage:

    • To save cost.
    • Hardware for EEPROM not available.
    • To save IO pin or communication pin.

    Disadvantage:

    • Complexity code use for development.
    • If we write over full memory, we need take time to erase and processing – it just small.
    • We write follow sequence, address in flash is not fixed.
  3. Explanation.
    1. Memory use.
    2. Below is example flash memory use, we can select another memory but need take note:

      For code build, we need takes care code need in site range. If it out range, it will overwrite locations simulate EEPROM.

      For simulate we need using 2 sectors with same size and need configuration in library.

    3. Format data in simulate EEPROM.
    4. We use 2 sectors because if first sector is full => we will erase first sector and move to second sector => it will avoid lost or missing data. 4 bytes first sector indication sector is use or not, next 2 bytes is address, more next 2 bytes is data. We will define how many address and data write. Every time trigger write, we need all address and data or apart data (it will more code processing to handle).

      For detail how to read/write/erase, please find in reference link, put debug point and see.

  4. Example and detail code.
  5. Example: using simulate EEPROM library in reference to save number time of reset STM32 with format data: 2 first bytes data is number time reset, 2 next bytes are MSB bytes of Random number generate, 2 end bytes are LSB of Random number generate.

    Hint:

    • Format:
    • Using RGN feature in STM32 to generate random number, we need configuration by STM32CUBEMX and get data.
    1. Open STM32CUBEMX and select STM32F407VGTx, every version of this tool we have UI difference, in here I use 6.10.0. We can search how to use it in google, in this guide line, only mention some main step and main code change.
      1. Configuration RCC are use HSE and LSE external.
      2. Configuration use debugger is Serial wire and System tick timer.
      3. Configuration GPIO for led are PD12, PD13, PD14, PD15.
      4. Configuration RNG feature.
      5. Configuration clock use.
      6. Configuration generate code and click generate code.
    2. Open project by Keil C and start modify code generate
    3. Note: Build project first to confirm no issue for generate code, add library download github or download source code directly in below:

      1. Add some variable use for simulate EERPOM with mandatory address, date read, data write.
      2. Define some function call for Initial, read, write which we call from library for personal purpose.
      3. Process data follow chart: (1)Read first time after reset(If we read failed it mean we don’t initial EEPROM before, it will trigger Inital) => (2) Get data we save via some global variable to view in debug => (3)Get data in current time and write it for next time read => If no error we will toggle 4 led with 1000ms, if failed we toggle with 200ms.
      4. Some note need take care:
        • We need unlock and lock flash every time access read or write => It define by some register control Flash.
        • HAL_FLASH_Unlock(); HAL_FLASH_Lock();

        • Function Protocol_InitialEEPROM(),ReadEEPROM(),WriteEEPROM() are handle by user, we can modify for some personal purpose but need take care of use case
        • We can select option Reset and Run in Keil after first time download, if no select it, you need press button reset or power off/on again. Make sure location memory flash for simulate EEPROM not access by another part. We can use option Erase full chip in configuration and next time disable it.
        • Use debug feature to see data every time reset or STM32-STLINK Utility to view memory of location Simulate EEPROM.
    4. Link source code and reference github.
    5. If you have doubt or concern, you can contact me by contact page or write down comment. I will update ASAP. Sorry to change to English content for some Vietnamses because English is importance with everyone for study and learning new thing easier. Event my English is not good, i will try enhanced and improve it.

      Reference link Source code link