Dirk- / NanoBLEFlashPrefs

Substitute for the missing EEPROM storage on Arduino Nano 33 BLE and BLE Sense.
GNU General Public License v2.0
16 stars 1 forks source link

Garbage collection necessary? #9

Closed mhodder closed 7 months ago

mhodder commented 1 year ago

Hello, thank you for the library. I am using it on a project where I will save two calibration values and recall at startup. I will also record them any time the position of a servo motor changes. I’m not expecting too many writes, but probably more then 1k over the life of the device. I don’t fully understand the garbage collection feature, if I let the the device continually write to flash without garbage collection will it eventually fill up, if so is there any downside to running the garbage collection after every write?

So far the library is working great I just don’t fully understand everything and I’m worried my device is going to crash after 1000 writes to flash.

Dirk- commented 1 year ago

Hi, glad you like it.

Please take a look at the sample sketch NanoBLEFlashPrefsUtils from version 1.2 of the library. If you let your own sketch run for a couple of times and then run NanoBLEFlashPrefsUtils, you will see that you end up with more "dirty" records each time your sketch writes out the preferences. If you do a garbage collection, the number of dirty records will go back to zero. So yes, there is a chance that your sketch will not be able to write the preferences if you do not do garbage collection from time to time. You could check the return result of writePrefs and if it is FDS_ERR_NO_SPACE_IN_FLASH, do a garbage collection and try again:

  int rc = myFlashPrefs.writePrefs(&prefs, sizeof(prefs));
  if (rc == FDS_ERR_NO_SPACE_IN_FLASH) {
    rc = myFlashPrefs.garbageCollection();
    rc = myFlashPrefs.writePrefs(&prefs, sizeof(prefs));
    if (rc != FDS_SUCCESS) {
      Serial.print("Now we got some serious problems: ");
      Serial.println(myFlashPrefs.errorString(rc));
    }
  }

See nRF5 SDK documentation for more info.