|
| 1 | +# NVStore |
| 2 | + |
| 3 | +A lightweight module providing the functionality of storing data by keys in the internal flash, for security purpose. |
| 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, NVStore module provides the ability to set the item data or get it. |
| 9 | +Newly set values are added to the flash (as in journal), with the effect of overriding the previous value for this key. |
| 10 | +NVStore module ensures that existing data isn't harmed by power failures, 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 non-active. Data is written to the active area, until it gets full. |
| 15 | +When it does, garbage collection is invoked, compacting items from the active area to the non-active one, |
| 16 | +and switching activity between areas. |
| 17 | +Each item is kept in an entry, containing header and data, where the header holds the item key, size and CRC. |
| 18 | +Each area must consist of one or more erasable units (sectors). |
| 19 | + |
| 20 | +### APIs |
| 21 | +- init: Initialize NVStore (also lazily called by get, set, set_once and remove APIs). |
| 22 | +- deinit: Deinitialize NVStore. |
| 23 | +- get: Get the value of an item, given key. |
| 24 | +- set: Set the value of an item, given key and value. |
| 25 | +- set_once: Like set, but allows only a one time setting of this item (and disables deleting of this item). |
| 26 | +- remove: Remove an item, given key. |
| 27 | +- get_item_size: Get the item value size (in bytes). |
| 28 | +- set_max_keys: Set maximal value of unique keys. Overriding the default of NVSTORE_MAX_KEYS. This affects RAM consumption, |
| 29 | + as NVStore consumes 4 bytes per unique key. Reinitializes the module. |
| 30 | + |
| 31 | + |
| 32 | +## Usage |
| 33 | + |
| 34 | +### Configuring NVStore for your board |
| 35 | +NVStore requires the addresses and sizes of both areas in flash. For this purpose, the following values should be defined in |
| 36 | +configuration: |
| 37 | +NVSTORE_AREA_1_ADDRESS |
| 38 | +NVSTORE_AREA_1_SIZE |
| 39 | +NVSTORE_AREA_2_ADDRESS |
| 40 | +NVSTORE_AREA_2_SIZE |
| 41 | +This is currently supported by the K64F and K82F boards, while these values are configured in the targets.json file. Any new board |
| 42 | +supporting this feature should have these values added there. |
| 43 | + |
| 44 | +### Building NVStore |
| 45 | +The default value of number of different keys can be set by the NVSTORE_MAX_KEYS define in mbed_lib.json . |
| 46 | + |
| 47 | +### Using NVStore |
| 48 | +NVStore is a singleton class, meaning that the system can have only a single instance of it. |
| 49 | +To instanciate NVStore, one needs to call its get_instance member function as following: |
| 50 | + NVStore &nvstore = NVStore::get_instance(); |
| 51 | +After the NVStore instantiation, one can call the init API, but it is not necessary, as all |
| 52 | +NVStore APIs (get, set et al.) perform a "lazy initialization". |
| 53 | + |
| 54 | +### Testing NVStore |
| 55 | +Run the NVStore functionality test with the mbed command as following: |
| 56 | +mbed test -n features-nvstore-tests-nvstore-functionality |
0 commit comments