EEPROM Emulation with Arduino API

arduino m0 samd21 robotdyn eeprom flash

Pre-requisites

Read and write to the flash memory of a SAMD21 board (E.g. Arduino M0 or RobotDyn SAMD21 M0-Mini) with the EEPROM Arduino API.

The following APIs are used in the example:

  1. EEPROM.isValid()
  2. EEPROM.length()
  3. EEPROM.commit()
  4. EEPROM.write
  5. EEPROM.read

Code

Download code
#include <FlashAsEEPROM.h>

void setup() {
  SerialUSB.begin(115200);
  delay(100);
}

void loop() {
  if (!EEPROM.isValid()) {
    SerialUSB.println("EEPROM is empty, writing some example data:");

    storeInFlash();
    SerialUSB.print("");
  } else {
    SerialUSB.println("EEPROM has been written.");
    SerialUSB.print("Total length of EEPROM is ");
    SerialUSB.println(EEPROM.length());
    SerialUSB.println("Here is the content of the first 20 bytes:");
    SerialUSB.print("->");

    readFromFlash();
    SerialUSB.println("");
  }

  delay(5000);
}

void storeInFlash() {
  for (int i = 0; i < 20; i++) {
    EEPROM.write(i, 100+i);
    SerialUSB.print(" ");
    SerialUSB.print(100+i);
  }

  EEPROM.commit();
}

void readFromFlash() {
  for (int i = 0; i < 20; i++) {
    SerialUSB.print(" ");
    SerialUSB.print(EEPROM.read(i));
  }

  SerialUSB.println();
}

References