|
| 1 | +# FlashStorage library for Arduino |
| 2 | + |
| 3 | +The FlashStorage library aims to provide a convenient way to store and retrieve |
| 4 | +user's data using the non-volatile flash memory of microcontrollers. |
| 5 | + |
| 6 | +The flash memory, due to it's properties, is generally used to store the firmware |
| 7 | +code, but it can also be used to store user data. |
| 8 | + |
| 9 | +## Supported hardware |
| 10 | + |
| 11 | +Currently, only ATSAMD21 cpu is supported (and consequently every board based |
| 12 | +on this cpu like the Arduino Zero or Aduino MKR1000). |
| 13 | + |
| 14 | +## Limited number of writes |
| 15 | + |
| 16 | +Flash memory has a limited amount of write cycles. Typical flash |
| 17 | +memory can perform about 10000 writes cycles to the same flash block |
| 18 | +before starting to "wear out" and begin to lose the ability to retain data. |
| 19 | + |
| 20 | +So **BEWARE: IMPROPER USE OF THIS LIBRARY CAN QUICKLY AND PERMANENTLY |
| 21 | +DESTROY THE FLASH MEMORY OF YOUR MICRO**, in particular you should avoid to |
| 22 | +call the `write()` function too often and make sure that in the entire life |
| 23 | +of the micro the number of calls to `write` stay well below the above limit |
| 24 | +of 10000 (it's a good rule-of-thumb to keep that number in mind even if the |
| 25 | +manufacturer of the micro guarantees a bigger number of cycles). |
| 26 | + |
| 27 | +The same caution must be taken if you're using the EEPROM API emulation (see |
| 28 | +below) with the `EEPROM.commit()` function. |
| 29 | + |
| 30 | +## Usage |
| 31 | + |
| 32 | +First of all you must declare a global `FlashStorage` object for each piece of |
| 33 | +data you intend to store in the flash memory. |
| 34 | +For example if you want to store the age of a person you must declare an |
| 35 | +`age_storage` like this: |
| 36 | + |
| 37 | +```c++ |
| 38 | +FlashStorage(age_storage, int); |
| 39 | +``` |
| 40 | +
|
| 41 | +this instruction means "create a `FlashStorage` to store an `int` variable and call |
| 42 | +it `age_storage`". Now you can use `age_storage` as a place to safely store an integer: |
| 43 | +
|
| 44 | +```c++ |
| 45 | +void readAndStoreUserAge() { |
| 46 | + Serial.println("Please enter your age:"); |
| 47 | + String age = Serial.readStringUntil('\n'); |
| 48 | +
|
| 49 | + age_storage.write(age.toInt()); // <-- save the age |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +after a reset of the microcontroller you can retrieve the stored age by using: |
| 54 | + |
| 55 | +```c++ |
| 56 | +int user_age = age_storage.read(); |
| 57 | +``` |
| 58 | + |
| 59 | +### Using the alternative EEPROM-like API |
| 60 | + |
| 61 | +If you include `FlashAsEEPROM.h` you'll get an EEPROM emulation with the internal flash memory. |
| 62 | +See [EmulateEEPROM](https://github.com/cmaglie/FlashStorage/tree/master/examples/EmulateEEPROM/EmulateEEPROM.ino) sketch for an example. |
| 63 | + |
| 64 | +The API is very similar to the well known Arduino EEPROM.h API but with two additional functions: |
| 65 | + |
| 66 | +* `EEPROM.isValid()` returns `true` if data in the EEPROM is valid or, in other words, if the data has been written at least once, otherwise EEPROM data is "undefined" and the function returns `false`. |
| 67 | +* `EEPROM.commit()` store the EEPROM data in flash. Use this with care: Every call writes the complete 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](https://github.com/cmaglie/FlashStorage#limited-number-of-writes). |
| 68 | + |
| 69 | +## License |
| 70 | + |
| 71 | +This library is released under LGPL-2.1. |
| 72 | + |
| 73 | +## FAQ |
| 74 | + |
| 75 | +### Can I use a single FlashStorage object to store more stuff? |
| 76 | + |
| 77 | +Yes, you can declare a `struct` with more fields and create a `FlashStorage` object to |
| 78 | +store the entire structure. See the [StoreNameAndSurname](https://github.com/cmaglie/FlashStorage/tree/master/examples/StoreNameAndSurname/StoreNameAndSurname.ino) |
| 79 | +sketch for an example on how to do it. |
| 80 | + |
| 81 | +### The content of the FlashStorage is erased each time a new sketch is uploaded? |
| 82 | + |
| 83 | +Yes, every time you upload a new sketch, the previous content of the FlashStorage is erased. |
| 84 | + |
| 85 | +### Do you recommend to use FLASH instead of EEPROM? |
| 86 | + |
| 87 | +No. If your micro provides an EEPROM it's almost always better to use that because |
| 88 | +it's a kind of memory designed with the specific purpose to store user data (it has a |
| 89 | +longer lifetime, number of write cycles, etc...). |
| 90 | + |
| 91 | +In the absence of an EEPROM you can use this library to use a piece of the flash memory |
| 92 | +as an alternative to EEPROM. However, you must always keep in mind of it's limits. |
| 93 | + |
0 commit comments