|
| 1 | +# NVStore |
| 2 | + |
| 3 | +NVStore is a lightweight module that stores data by keys in the internal flash for security purposes. |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +NVStore provides the ability to store a minimal set of system critical items in the internal flash. |
| 8 | +For each item key, the NVStore module provides the ability to set the item data or get it. |
| 9 | +Newly added values are added to the end of the existing data, superseding the previous value that was there for the same key. |
| 10 | +The NVStore module ensures that power failures don't harm existing data during any operation. |
| 11 | +The full interface can be found under `nvstore.h`. |
| 12 | + |
| 13 | +### Flash structure |
| 14 | +NVStore uses two Flash areas, active and nonactive. Each area must consist of at least one erasable unit (sector). |
| 15 | +Data is written to the active area until it becomes full. When it does, garbage collection is invoked. |
| 16 | +This compacts items from the active area to the nonactive one and switches activity between areas. |
| 17 | +Each item is kept in an entry containing a header and data, where the header holds the item key, size and CRC. |
| 18 | + |
| 19 | +### APIs |
| 20 | +- init: Initialize NVStore (also lazily called by get, set, set_once and remove APIs). |
| 21 | +- deinit: Deinitialize NVStore. |
| 22 | +- get: Get the value of an item, given key. |
| 23 | +- set: Set the value of an item, given key and value. |
| 24 | +- set_once: Like set, but allows only a one time setting of this item (and disables deleting of this item). |
| 25 | +- remove: Remove an item, given key. |
| 26 | +- get_item_size: Get the item value size (in bytes). |
| 27 | +- set_max_keys: Set maximal value of unique keys. Overriding the default of NVSTORE_MAX_KEYS. This affects RAM consumption, |
| 28 | + as NVStore consumes 4 bytes per unique key. Reinitializes the module. |
| 29 | + |
| 30 | + |
| 31 | +## Usage |
| 32 | + |
| 33 | +### Enabling NVStore and configuring it for your board |
| 34 | +NVStore is enabled by default for all devices with the internal flash driver (have "FLASH" in the device_has attribute). |
| 35 | +One can disable it by setting its "enabled" attribute to false. |
| 36 | +Unless specifically configured by the user, NVStore selects the last two flash sectors as its areas, with the mininum size of 4KBs, |
| 37 | +meaning that if the sectors are smaller, few continuous ones will be used for each area. |
| 38 | +The user can override this by setting the addresses and sizes of both areas in` mbed_lib.json` on a per board basis. |
| 39 | +In this case, all following four attributes need to be set: |
| 40 | +- area_1_address |
| 41 | +- area_1_size |
| 42 | +- area_2_address |
| 43 | +- area_2_size |
| 44 | + |
| 45 | +In addition, the `num_keys` value should be modified to change the default number of different keys. |
| 46 | + |
| 47 | +### Using NVStore |
| 48 | +NVStore is a singleton class, meaning that the system can have only a single instance of it. |
| 49 | +To instantiate NVStore, one needs to call its get_instance member function as following: |
| 50 | +``` c++ |
| 51 | + NVStore &nvstore = NVStore::get_instance(); |
| 52 | +``` |
| 53 | +After the NVStore instantiation, one can call the init API, but it is not necessary because all |
| 54 | +NVStore APIs (get, set and so on) perform a "lazy initialization". |
| 55 | + |
| 56 | +### Testing NVStore |
| 57 | +Run the NVStore functionality test with the `mbed` command as following: |
| 58 | +```mbed test -n features-nvstore-tests-nvstore-functionality``` |
0 commit comments