cmaglie / FlashStorage

A convenient way to store data into Flash memory on the ATSAMD21 and ATSAMD51 processor family
202 stars 69 forks source link

trying to write/read a string (variable length) to persistent storage on Nano 33 IOT #55

Closed sdetweil closed 1 year ago

sdetweil commented 1 year ago

i am trying to the the eeprom emulation (I guess?) https://github.com/cmaglie/FlashStorage/blob/master/examples/EmulateEEPROM/EmulateEEPROM.ino

EEProm.read/write/isValid()

compiles ok, just always reports isValid = false

is this the right function to use?

I REALLY don't CARE HOW its done... name, address, location,, whatever flash, eeprom, stones in a cup.

the example doesn't say it wont work on this board.. and the lib says it supports samd21 and 51

String fileName="something";
          int i=0;
          for(;i<fileName.length();i++){
            EEPROM.write(i, fileName.charAt(i));  // write out 1 char of data at a time
          }
          EEPROM.write(i,'\0');  // add on a string terminator
          EEPROM.commit();   // flush 

this is a json string with variable length elements, I can imagine a max length..

sdetweil commented 1 year ago

well, part 1, DOAH, READ ... it uses flash and flash is wiped on firmware upload.. ...

now.. part 2, I'm not getting out what I put in..

do I have to commit after each write?, example says no

my read code

char xx[]="some string of dummy data";
if(EEPROM.isValid()){
    Serial.println("config present");
    Serial.println("using stored data");
    //  create a buffer
    char x[sizeof(xx)+10];
    // clear it, expect we will read less data than buffer size, most of the time.
    memset(x,0,sizeof(x));
    // loop thru the eeprom (whatever it is) 
    for (int i=0; i<sizeof(xx)+10; i++) {
        Serial.print(" ");
        // get a character
        unsigned char y = EEPROM.read(i);
        // if its not a null terminator (c stringish)
        if( y != 0){
          // save it in the buffer  
          x[i]=y;
        }
        // done with the read loop
        else
          break;
    }
}
sdetweil commented 1 year ago

only 25 chars of my 110 chars are retrievable from flash on my Nano 33 IOT i write ascii chars and then '\0', and then commit() I read til I see the '\0'

sdetweil commented 1 year ago

ok, don't know why, but it thinks there is a '\0' embedded in the data, but not..

anyhow, now write 1 byte length, rest of data read 1 byte length, rest of data..

all ok