Multiple Definitions Linker ErrorPlease have a look at HOWTO Fix Multiple Definitions Linker Error
The FlashStorage_SAMD library, based on and modified from Cristian Maglie's FlashStorage, provides a convenient way to store and retrieve user's data using emulated-EEPROM, from the non-volatile flash memory of SAMD21/SAMD51 boards.
The flash memory, generally used to store the firmware code, can also be used to store / retrieve more user's data and faster than from EEPROM. Thanks to the buffered data writing and reading, the flash access time is greatly reduced to increase the life of the flash.
Arduino IDE 1.8.19+ for Arduino. Arduino SAMD core 1.8.13+ for SAMD ARM Cortex-M0+ boards. Adafruit SAMD core 1.7.11+ for SAMD ARM Cortex-M0+ and M4 boards (Nano 33 IoT, etc.). Seeeduino SAMD core 1.8.3+ for SAMD21/SAMD51 boards (XIAO M0, Wio Terminal, etc.). IndustruinoSAMD core 1.0.3+ for SAMD21 boards (Industruino DG21, etc.).IndustruinoSAML core 1.0.0+ for SAML21 boards (Industruino 420MAKER, etc.).Sparkfun SAMD core 1.8.4+ for SAMD21/SAMD51 boards (SparkFun_RedBoard_Turbo, SparkFun_SAMD51_Thing_Plus, etc.).The best and easiest way is to use Arduino Library Manager. Search for FlashStorage_SAMD, then select / install the latest version.
Another way to install is to:
FlashStorage_SAMD-master.zip.FlashStorage_SAMD-master directoryFlashStorage_SAMD-master folder to Arduino libraries' directory such as ~/Arduino/libraries/.To be able to compile without error and automatically detect and display BOARD_NAME on Arduino SAMD (Nano-33-IoT, etc) boards, you have to copy the whole Arduino SAMD Packages_Patches directory into Arduino SAMD directory (~/.arduino15/packages/arduino/hardware/samd/1.8.13).
Supposing the Arduino SAMD version is 1.8.13. Now only one file must be copied into the directory:
~/.arduino15/packages/arduino/hardware/samd/1.8.13/platform.txtWhenever a new version is installed, remember to copy this files into the new version directory. For example, new version is x.yy.zz
This file must be copied into the directory:
~/.arduino15/packages/arduino/hardware/samd/x.yy.zz/platform.txtSupposing the Arduino SAMD version is 1.8.9. These files must be copied into the directory:
~/.arduino15/packages/arduino/hardware/samd/1.8.9/platform.txt~/.arduino15/packages/arduino/hardware/samd/1.8.9/cores/arduino/Arduino.hWhenever a new version is installed, remember to copy these files into the new version directory. For example, new version is x.yy.z
These files must be copied into the directory:
~/.arduino15/packages/arduino/hardware/samd/x.yy.z/platform.txt~/.arduino15/packages/arduino/hardware/samd/x.yy.z/cores/arduino/Arduino.hThis is mandatory to fix the notorious Arduino SAMD compiler error. See Improve Arduino compatibility with the STL (min and max macro)
...arm-none-eabiincludec++7.2.1bitsstl_algobase.h:243:56: error: macro "min" passed 3 arguments, but takes just 2
min(const _Tp& __a, const _Tp& __b, _Compare __comp)
Whenever the above-mentioned compiler error issue is fixed with the new Arduino SAMD release, you don't need to copy the Arduino.h file anymore.
To be able to compile without error and automatically detect and display BOARD_NAME on Adafruit SAMD (Itsy-Bitsy M4, etc) boards, you have to copy the files in Adafruit SAMD Packages_Patches into Adafruit samd directory (~/.arduino15/packages/adafruit/hardware/samd/1.7.11).
Supposing the Adafruit SAMD core version is 1.7.11. This file must be copied into the directory:
~/.arduino15/packages/adafruit/hardware/samd/1.7.11/platform.txt~/.arduino15/packages/adafruit/hardware/samd/1.7.11/cores/arduino/Print.h~/.arduino15/packages/adafruit/hardware/samd/1.7.11/cores/arduino/Print.cppWhenever a new version is installed, remember to copy this file into the new version directory. For example, new version is x.yy.zz This file must be copied into the directory:
~/.arduino15/packages/adafruit/hardware/samd/x.yy.zz/platform.txt~/.arduino15/packages/adafruit/hardware/samd/x.yy.zz/cores/arduino/Print.h~/.arduino15/packages/adafruit/hardware/samd/x.yy.zz/cores/arduino/Print.cppTo be able to compile without error and automatically detect and display BOARD_NAME on Seeeduino SAMD (XIAO M0, Wio Terminal, etc) boards, you have to copy the files in Seeeduino SAMD Packages_Patches into Seeeduino samd directory (~/.arduino15/packages/Seeeduino/hardware/samd/1.8.3).
Supposing the Seeeduino SAMD core version is 1.8.3. This file must be copied into the directory:
~/.arduino15/packages/Seeeduino/hardware/samd/1.8.3/platform.txt~/.arduino15/packages/Seeeduino/hardware/samd/1.8.3/cores/arduino/Arduino.h~/.arduino15/packages/Seeeduino/hardware/samd/1.8.3/cores/arduino/Print.h~/.arduino15/packages/Seeeduino/hardware/samd/1.8.3/cores/arduino/Print.cppWhenever a new version is installed, remember to copy this file into the new version directory. For example, new version is x.yy.zz This file must be copied into the directory:
~/.arduino15/packages/Seeeduino/hardware/samd/x.yy.zz/platform.txt~/.arduino15/packages/Seeeduino/hardware/samd/x.yy.zz/cores/arduino/Arduino.h~/.arduino15/packages/Seeeduino/hardware/samd/x.yy.zz/cores/arduino/Print.h~/.arduino15/packages/Seeeduino/hardware/samd/x.yy.zz/cores/arduino/Print.cppTo be able to compile without error and automatically detect and display BOARD_NAME on SparkFun SAMD (XIAO SparkFun_RedBoard_Turbo, SparkFun_SAMD51_Thing_Plus, etc) boards, you have to copy the file SparkFun SAMD Packages_Patches into SparkFun samd directory (~/.arduino15/packages/SparkFun/hardware/samd/1.8.3).
Supposing the SparkFun SAMD core version is 1.8.3. This file must be copied into the directory:
~/.arduino15/packages/SparkFun/hardware/samd/1.8.3/cores/arduino/Print.h~/.arduino15/packages/SparkFun/hardware/samd/1.8.3/cores/arduino/Print.cpp~/.arduino15/packages/SparkFun/hardware/samd/1.8.3/cores/arduino51/Print.h~/.arduino15/packages/SparkFun/hardware/samd/1.8.3/cores/arduino51/Print.cppWhenever a new version is installed, remember to copy this file into the new version directory. For example, new version is x.yy.zz This file must be copied into the directory:
~/.arduino15/packages/SparkFun/hardware/samd/x.yy.zz/cores/arduino/Print.h~/.arduino15/packages/SparkFun/hardware/samd/x.yy.zz/cores/arduino/Print.cpp~/.arduino15/packages/SparkFun/hardware/samd/x.yy.zz/cores/arduino51/Print.h~/.arduino15/packages/SparkFun/hardware/samd/x.yy.zz/cores/arduino51/Print.cppMultiple Definitions Linker ErrorThe current library implementation, using xyz-Impl.h instead of standard xyz.cpp, possibly creates certain Multiple Definitions Linker error in certain use cases.
You can include this .hpp file
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
#include "FlashStorage_SAMD.hpp" //https://github.com/khoih-prog/FlashStorage_SAMDin many files. But be sure to use the following .h file in just 1 .h, .cpp or .ino file, which must not be included in any other file, to avoid Multiple Definitions Linker Error
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "FlashStorage_SAMD.h" //https://github.com/khoih-prog/FlashStorage_SAMDCheck the new multiFileProject example for a HOWTO demo.
Have a look at the discussion in Different behaviour using the src_cpp or src_h lib #80
The flash memory has a limited amount of write cycles. Typical flash memories can perform about 10000 writes cycles to the same flash block before starting to "wear out" and begin to lose the ability to retain data.
So BEWARE: IMPROPER USE OF THIS LIBRARY CAN QUICKLY AND PERMANENTLY DESTROY THE FLASH MEMORY OF YOUR MICRO, in particular you should avoid to call the write() function too often and make sure that in the entire life of the micro the number of calls to write stay well below the above limit of 10000 (it's a good rule-of-thumb to keep that number in mind even if the
manufacturer of the micro guarantees a bigger number of cycles).
The same caution must be taken if you're using the EEPROM API emulation (see below) with the EEPROM.commit() function.
First of all you must declare a global FlashStorage object for each data you intend to store in the flash memory.
For example if you want to store the age of a person, you must declare an age_storage like this:
FlashStorage(age_storage, int);this instruction means "create a FlashStorage to store an int variable and call it age_storage". Now you can use age_storage as a place to safely store an integer:
void readAndStoreUserAge()
{
Serial.println("Please enter your age:");
String age = Serial.readStringUntil('n');
age_storage.write(age.toInt()); // <-- save the age
}after a reset of the microcontroller to retrieve the stored age you can use:
int user_age = age_storage.read();Include FlashStorage_SAMD.h to get an EEPROM emulation with the internal flash memory.
See EmulateEEPROM sketch for an example.
The API is very similar to the well-known Arduino EEPROM library API, but with 4 additional functions:
bool isValid() returns true if data in the emulated-EEPROM is valid (the data written to flash at least once by EEPROM.commit() or EEPROM.put()). Otherwise emulated-EEPROM data is "undefined" and the function returns false.void commit() store the EEPROM data in flash. Use this with care: Every call writes the complete emulated-EEPROM data to flash. This will reduce the remaining flash-write-cycles. Don't call this method in a loop or you will kill your flash soon.void setCommitASAP(bool value = true) to set or clear the _commitASAP private variable (default is true to be safe). If _commitASAP is false, the call to EEPROM.put() won't force the EEPROM.commit() to extend the flash life. You'll have to remember to call EEPROM.commit() manually to save the emulated-EEPROM data into flash or data will be lost.bool getCommitASAP() to return the current value of _commitASAP.FlashStorage_SAMD/examples/StoreNameAndSurname/StoreNameAndSurname.ino
Lines 26 to 128 in 102e13e
The following is the sample terminal output when running example W5500_Blynk on Adafruit SAMD51 ITSYBITSY_M4 using W5500 Ethernet shield
Start W5500_Blynk on ITSYBITSY_M4
[936] ChkCrR:CrCCSum=0xaf50,CrRCSum=0xffffffff
[936] CCSum=0x0,RCSum=0x0
[936] Invalid Stored Dynamic Data. Load default from Sketch
[937] SaveEEPROM,Sz=1024,DataSz=0,WCSum=0x1d4d
[944] CrCCSum=0x29a6
[944] MAC:FE-A8-80-C6-CE-A3
_pinCS = 0
W5100 init, using SS_PIN_DEFAULT = 10, new ss_pin = 10, W5100Class::ss_pin = 1
W5100::init: W5500, SSIZE =8192
[2632] IP:192.168.2.153
[2633] b:Stay in CfgPortal:No CfgDat
[2633] CfgIP=192.168.2.153
F
Your stored Credentials :
MQTT Server = default-mqtt-server
Port = 1883
MQTT UserName = default-mqtt-username
MQTT PWD = default-mqtt-password
Subs Topics = default-mqtt-SubTopic
Pubs Topics = default-mqtt-PubTopic
FFFFF
[339285] SaveEEPROM,Sz=1024,DataSz=0,WCSum=0x2e89
[339292] CrCCSum=0x219fSave => rebootStart W5500_Blynk on ITSYBITSY_M4
[1547] ChkCrR:CrCCSum=0x219f,CrRCSum=0x219f
[1547] CCSum=0x0,RCSum=0x0
[1548] CrCCSum=0x219f,CrRCSum=0x219f
[1548] ======= Start Stored Config Data =======
[1548] Hdr=W5X00,BName=Seeeduino_W5500_BlynkWM
[1548] Svr=account.duckdns.org,Tok=new_token1
[1549] Svr1=account.ddns.net,Tok1=new_token2
[1549] Prt=8080,SIP=192.168.2.220
[1549] connectEthernet: Use static_IP=192.168.2.220
[1549] MAC:FE-A1-D4-BC-E8-CB
W5100 init, using SS_PIN_DEFAULT = 10, new ss_pin = 10, W5100Class::ss_pin = 1
W5100::init: W5500, SSIZE =8192
[3131] IP:192.168.2.220
[3131] bg:ECon.TryB
[3131]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ / '_/
/____/_/_, /_//_/_/_
/___/ v0.6.1 on Arduino Zero
[3132] BlynkArduinoClient.connect: Connecting to account.duckdns.org:8080
[3244] Ready (ping: 6ms).
[3311] Connected to BlynkServer=account.duckdns.org,Token=new_token1
[3311] bg:EBCon
Conn2Blynk: server = account.duckdns.org, port = 8080
Token = new_token1, IP = 192.168.2.220
B
Your stored Credentials :
MQTT Server = new-mqtt-server
Port = 1883
MQTT UserName = new-mqtt-username
MQTT PWD = new-mqtt-password
Subs Topics = new-mqtt-SubTopic
Pubs Topics = new-mqtt-PubTopic
BBBBBBBBB BBBBBBBBBB BBBBBBBBBB BBBBBBBBBB BBBBBBBBBB BBBBBBBBBB BBBBBBBBBBStart StoreNameAndSurname on SEEED_XIAO_M0
FlashStorage_SAMD v1.3.2
EEPROM length: 1024
EEPROM is empty, writing WRITTEN_SIGNATURE and some example data:
Insert your name : John
Insert your surname : Doe
<< Your name: John. Your surname: Doe >> have been saved. Thank you!
You can reset to check emulated-EEPROM data retention.Start StoreNameAndSurname on SEEED_XIAO_M0
FlashStorage_SAMD v1.3.2
EEPROM length: 1024
Hi John Doe, nice to see you again :-)
Clearing WRITTEN_SIGNATURE for next try
Done clearing signature in emulated EEPROM. You can reset nowStart EEPROM_Clear on SEEED_XIAO_M0
FlashStorage_SAMD v1.3.2
Emulated EEPROM length (bytes) = 1024
Done clearing emulated EEPROM. Time spent (ms) = 11
Start EEPROM_Clear on SEEED_XIAO_M0
FlashStorage_SAMD v1.3.2
Emulated EEPROM length (bytes) = 2048
Done clearing emulated EEPROM. Time spent (ms) = 22Start EEPROM_Clear on SEEED_XIAO_M0
FlashStorage_SAMD v1.3.2
Emulated EEPROM length (bytes) = 4096
Done clearing emulated EEPROM. Time spent (ms) = 42Start EEPROM_get on SEEED_XIAO_M0
FlashStorage_SAMD v1.3.2
EEPROM length: 1024
EEPROM doesn't store valid data, writing WRITTEN_SIGNATURE and some example data
Float written to EEPROM: 123.456
Done writing custom object to EEPROM:
===============
Field1: 3.14159
Field2: 65
Name: Working!
===============
Reset to see how you can retrieve the values by using EEPROM_get!Start EEPROM_get on SEEED_XIAO_M0
FlashStorage_SAMD v1.3.2
EEPROM length: 1024
EEPROM has valid data with WRITTEN_SIGNATURE. Now read some example data
Read float from EEPROM: 123.456
Read custom object from EEPROM:
===============
Field1: 3.14159
Field2: 65
Name: Working!
===============Yes, you can declare a struct with more fields and call a EEPROM.put() to store the entire structure. See the StoreNameAndSurname for how to do it.
Yes, every time you upload a new sketch, the previous content of the FlashStorage is erased.
No. If your board provides an integrated-EEPROM, it's advisable to use that because EEPROM has longer lifetime, number of write cycles, etc.).
In the absence of an integrated-EEPROM or its size is too small for your use-case, you can use this library to use a small portion flash memory as emulated-EEPROM, provided that you keep in mind the limits as in Limited number of writes
If you get compilation errors, more often than not, you may need to install a newer version of the core for Arduino boards.
Sometimes, the library will only work if you update the board core to the latest version because I am using newly added functions.
Submit issues to: FlashStorage_SAMD issues
EEPROM.put() and EEPROM.get() functions to read/write the whole struct in emulated-EEPROMmultiple-definitions linker error.SAMD21E1xA, SAMD21G1xA andSAMD21J1xA
![]() Cristian Maglie |
If you want to contribute to this project: