EnglishSvenska

This post is automatically translated to English by Google Translate.

Ersättare för EEPROM på Arduino Due

När jag fick min Arduino Due upptäckte jag att den inte har EEPROM vilket kan användas för att spara icke-flyktig data. Vanligtvis på en mikroprocessor så arbetar man med RAM-minnet vilket fungerar bra så länge man inte vill spara data mellan gångerna som processorn startar. Om man vill spara en variabel som ska vara kvar även när processorn förlorar ström behöver man en annan lösning där den vanligaste är att spara ner till EEPROM. På Arduino Due finns tyvärr inte detta minne men den har däremot ett riktigt stort flash-minne som kan konfigureras till att fungera på ett liknande sätt. Jag skrev därför ett bibliotek som gör just detta. Det är tänkt att kunna användas som en rak ersättare till EEPROM. DueFlashStorage är publicerad på Github.
Exempel:

// write the value 123 to address 0
dueFlashStorage.write(0,123);

// read byte at address 0
byte b = dueFlashStorage.read(0);

Eller hela exempelkoden:

/* This example will write 3 bytes to 3 different addresses and print them to the serial monitor.
Try resetting the Arduino Due or unplug the power to it. The values will stay stored. */

#include DueFlashStorage dueFlashStorage;

void setup() {
Serial.begin(115200);
byte b1 = 3;
uint8_t b2 = 1;
dueFlashStorage.write(0,b1);
dueFlashStorage.write(1,b2);
//dueFlashStorage.write(2,b2);
}

void loop() {
  /* read from flash at address 0 and 1 and print them */
  Serial.print("0:");
  Serial.print(dueFlashStorage.read(0));
  Serial.print(" 1:");
  Serial.print(dueFlashStorage.read(1));  

  /* read from address 2, increment it, print and then write incremented value back to flash storage */
  uint8_t i = dueFlashStorage.read(2)+1;
  Serial.print(" 2:");
  Serial.print(dueFlashStorage.read(2)); 
  dueFlashStorage.write(2,i);

  Serial.println();
  delay(1000);
}

Serial monitor:
arduino due flash example serial monitor 1

Men säg att man vill spara undan en hel konfiguration och inte bara några få bytes. Jag skrev biblioteket för att kunna hantera detta också:

// say you want to store a struct with parameters:
struct Configuration {
  uint8_t a;
  uint8_t b;
  int32_t bigInteger;
  char* message;
  char c;
};
Configuration configuration;

/* then write it to flash like this: */
byte b2[sizeof(Configuration)]; // create byte array to store the struct
memcpy(b2, &configuration, sizeof(Configuration)); // copy the struct to the byte array
dueFlashStorage.write(4, b2, sizeof(Configuration)); // write byte array to flash at address 4

/* and read from flash like this: */
byte* b = dueFlashStorage.readAddress(4); // byte array which is read from flash at adress 4
Configuration configurationFromFlash; // create a temporary struct
memcpy(&configurationFromFlash, b, sizeof(Configuration)); // copy byte array to temporary struct

Se hela koden på Github för ett fungerande exempel.

Tagged with:
Posted in Blog
22 Comments »Ersättare för EEPROM på Arduino Due
  1. Sam says:

    I see you are writing to address zero. Is this address offset internally somehow? Otherwise, wouldn't it overwrite the software which is also stored in flash memory?

  2. Peter Schmidt says:

    Very nice work here. I'm a little confused about how this works on a lower level - is address zero a fixed location regardless of program size, or does it start from the next page available after the program? More importantly, is there a limit of how many addresses you can have, or do you have to calculate that yourself based on the size of your program? Currently I'm only using about 16% of my Due's storage and I only have a few variables I intend to store. I'm just curious what the limits are.

    Also just to be clear, each address can store up to 256 bytes, correct?

    Thanks for your work!

    • sebnil says:

      Fixed location and you have to calculate all by yourself since the library doesn't do it for you. For my projects this has not been an issue and I have skipped any calculations. Hope you will find this library useful.

      /Seb

    • Bill says:

      Peter, Did you figure this out? I have the same concern too. How to calculate the offset and stuff.

  3. BARTOLOMÉ says:

    Hola quetal la libreria DueFlashStorage es buena pero no se puede poner un valor de mas de 255 en la libreria de EEPROMex que es la que utilizo si que se puede tiene varias funciones como (read ,readByte,readInt,readLong,readFloat,readDouble,readBlock)tanto para leer como escribir.

  4. maurizio says:

    Hi sebnil, do you think is possible to use your library to write in a SAM3A4C flash instead SAM3A8 mounted on Arduino Due?

    • sebnil says:

      I don't know but I would assume so. After some modifications. Make a pull request on github if you manage to modify it and get it working.

  5. Doug says:

    Any thoughts on write cycle limits? I'm using this library to increment 4 bytes (uint32_t) every second.

  6. Doug says:

    Well I let it run all weekend and now after 247382 writes (68 hours) I tried to flash some code changes and I get 'Flash page is locked'. I tired holding down the erase button while pressing the reset button but no help.

  7. Doug says:

    Well I rewrote my counter to only write to flash when the counter has either been commanded to stop, or CAN activity stops for more than 5 seconds. That along with a reliable power supply should make for a reliable hourmeter that only writes once per day at the most.

    Meanwhile, I tried to find definitive info on flash lifetime for the ATSAM8XE controllers with no luck. Lots of info on AVR based controllers, most of which agree to 10,000 writes for flash, 100,000 writes for EEPROM.

  8. alan says:

    This sounds great but if the position of the storage is offset from the end of the program presumably you lose the data if you make a change to your program.

  9. alan says:

    Sorry just seen in the readme.md that you mention that uploading a program cleans the memory.

    Alan

  10. armin says:

    can you give me an example? how can i read and write float or long data with your library?tnx

  11. MESSIAS DA SILVA says:

    Hello everybody.

    First, Sebastian Nilsson congratulations for the initiative.
    I wonder if beyond these two examples provided along with the library, you or some of the blog mates, have another data storage example in Flash Arduino DUE. I ran several tests with the library in my project. But unfortunately the data appear to have saved, but when turned off and on twice, it will be erased.

    Thank you for your attention

    Messias da Silva

  12. Mahmood Sabooni says:

    Hi,
    I have used this program (DueFlashStorage) but afterward, always I get the error:
    "Flash page is locked
    An error occurred while uploading the sketch"
    and always I need to press erase during the uploading any Arduino program to be able to run it.
    May I ask you to help me to resolve the issue?

    Best wishes,

    Mahmood Sabooni,

  13. Stimpy says:

    Hi;
    Also having some problems using this library. Along with the occasional page locking problem, I also see a problem with my timer interrupts. I have a timer setup to have the DUE emit serial data at around 2 kHz on Serial 2. If I instantiate the Flash library in the sketch preamble, I instantly lose the timer interrupt. My thinking is that the flash lib constructor is overwriting a configuration register instead of 'ORing' into it. Thus the flash lib works but nothing else does.

  14. hamid says:

    Hi
    Thank you for your practical library
    i have a question
    how to save a decimal number like 12.5 by your library to Dueflashmemory
    could you make an example?

    • sebnil says:

      Hi mate,
      I think there is an example for that. Put it in a struct and unpack using union. If I remember correctly. Good luck!

  15. Franz48 says:

    Hi
    I am reading your comments about DueFlashStorage because I coud get it to work at the beginning with simple DueFashStorage.write and DueFlashStorage.read but when I tried to work with sruct I got into a proble tha my sketch is not loaded and get the error of flash storage looked.
    Now I cannot use the board any more. I hwen throug all comment n this regard but nothig changed.
    Is anyoune able to give a suggestion ? At at least how to reset Arduino Due board and make it work without using DueFlashStorage.

Leave a Reply

Your email address will not be published. Required fields are marked *

*