My question is:
Any solution how to save data to ATSAMD21 "EEPROM" and not get it erased when uploading a new sketch?
Doesn't have to be with this library obviously.
/******************************************************************************************************************************************
FlashStoreAndRetrieve.ino
For SAMD21/SAMD51 using Flash emulated-EEPROM
The FlashStorage_SAMD library aims to provide a convenient way to store and retrieve user's data using the non-volatile flash memory
of SAMD21/SAMD51. It now supports writing and reading the whole object, not just byte-and-byte.
Based on and modified from Cristian Maglie's FlashStorage (https://github.com/cmaglie/FlashStorage)
Built by Khoi Hoang https://github.com/khoih-prog/FlashStorage_SAMD
Licensed under LGPLv3 license
Orginally written by A. Christian
Copyright (c) 2015-2016 Arduino LLC. All right reserved.
Copyright (c) 2020 Khoi Hoang.
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
as published bythe Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this library.
If not, see (https://www.gnu.org/licenses/)
Version: 1.2.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 28/03/2020 Initial coding to add support to SAMD51 besides SAMD21
1.1.0 K Hoang 26/01/2021 Add supports to put() and get() for writing and reading the whole object. Fix bug.
1.2.0 K Hoang 18/08/2021 Optimize code. Add debug option
******************************************************************************************************************************************/
//#define EEPROM_EMULATION_SIZE (4 * 1024)
// Use 0-2. Larger for more debugging messages
#define FLASH_DEBUG 0
#include <FlashStorage_SAMD.h>
// Note: the area of flash memory reserved for the variable is
// lost every time the sketch is uploaded on the board.
void setup()
{
Serial.begin(115200);
while (!Serial);
delay(200);
Serial.print(F("\nStart FlashStoreAndRetrieve on ")); Serial.println(BOARD_NAME);
Serial.println(FLASH_STORAGE_SAMD_VERSION);
Serial.print("EEPROM length: ");
Serial.println(EEPROM.length());
uint16_t address = 0;
int number;
// Read the content of emulated-EEPROM
EEPROM.get(address, number);
// Print the current number on the serial monitor
Serial.print("Number = 0x"); Serial.println(number, HEX);
// Save into emulated-EEPROM the number increased by 1 for the next run of the sketch
EEPROM.put(address, (int) (number + 1));
if (!EEPROM.getCommitASAP())
{
Serial.println("CommitASAP not set. Need commit()");
EEPROM.commit();
}
Serial.println("Done writing to emulated EEPROM. You can reset now");
}
void loop()
{
// Do nothing...
}
Now I understand your problem. The mentioned library doesn't write to the EEPROM it just imitates the interface of the EEPROM library to make a transition easier. All data you store using the library are stored in the flash memory of the microcontroller. If you upload a new sketch using the bootloader the complete flash memory except the specially flagged bootloader area is erased. So if you store there data it will be deleted by the bootloader. If you want to avoid that you have to write your own bootloader which masks your storage space from been overwritten.