1.. _nvs_api:
2
3Non-Volatile Storage (NVS)
4##########################
5
6Elements, represented as id-data pairs, are stored in flash using a
7FIFO-managed circular buffer. The flash area is divided into sectors. Elements
8are appended to a sector until storage space in the sector is exhausted. Then a
9new sector in the flash area is prepared for use (erased). Before erasing the
10sector it is checked that identifier - data pairs exist in the sectors in use,
11if not the id-data pair is copied.
12
13The id is a 16-bit unsigned number. NVS ensures that for each used id there is
14at least one id-data pair stored in flash at all time.
15
16NVS allows storage of binary blobs, strings, integers, longs, and any
17combination of these.
18
19Each element is stored in flash as metadata (8 byte) and data. The metadata is
20written in a table starting from the end of a nvs sector, the data is
21written one after the other from the start of the sector. The metadata consists
22of: id, data offset in sector, data length, part (unused), and a CRC. This CRC is
23only calculated over the metadata and only ensures that a write has been
24completed. The actual data of the element can be protected by a different (and optional)
25CRC-32. Use the :kconfig:option:`CONFIG_NVS_DATA_CRC` configuration item to enable
26the data part CRC.
27
28.. note:: The data CRC is checked only when the whole data of the element is read.
29  The data CRC is not checked for a partial read, as it is stored at the end of the
30  element data area.
31
32.. note:: Enabling the data CRC feature on a previously existing NVS content without
33  data CRC will make all existing data invalid.
34
35A write of data to nvs always starts with writing the data, followed by a write
36of the metadata. Data that is written in flash without metadata is ignored
37during initialization.
38
39During initialization NVS will verify the data stored in flash, if it
40encounters an error it will ignore any data with missing/incorrect metadata.
41
42NVS checks the id-data pair before writing data to flash. If the id-data pair
43is unchanged no write to flash is performed.
44
45To protect the flash area against frequent erases it is important that there is
46sufficient free space. NVS has a protection mechanism to avoid getting in a
47endless loop of flash page erases when there is limited free space. When such
48a loop is detected NVS returns that there is no more space available.
49
50For NVS the file system is declared as:
51
52.. code-block:: c
53
54	static struct nvs_fs fs = {
55	.flash_device = NVS_FLASH_DEVICE,
56	.sector_size = NVS_SECTOR_SIZE,
57	.sector_count = NVS_SECTOR_COUNT,
58	.offset = NVS_STORAGE_OFFSET,
59	};
60
61where
62
63- ``NVS_FLASH_DEVICE`` is a reference to the flash device that will be used. The
64  device needs to be operational.
65- ``NVS_SECTOR_SIZE`` is the sector size, it has to be a multiple of
66  the flash erase page size and a power of 2.
67- ``NVS_SECTOR_COUNT`` is the number of sectors, it is at least 2, one
68  sector is always kept empty to allow copying of existing data.
69- ``NVS_STORAGE_OFFSET`` is the offset of the storage area in flash.
70
71
72Flash wear
73**********
74
75When writing data to flash a study of the flash wear is important. Flash has a
76limited life which is determined by the number of times flash can be erased.
77Flash is erased one page at a time and the pagesize is determined by the
78hardware. As an example a nRF51822 device has a pagesize of 1024 bytes and each
79page can be erased about 20,000 times.
80
81Calculating expected device lifetime
82====================================
83
84Suppose we use a 4 bytes state variable that is changed every minute and
85needs to be restored after reboot. NVS has been defined with a sector_size
86equal to the pagesize (1024 bytes) and 2 sectors have been defined.
87
88Each write of the state variable requires 12 bytes of flash storage: 8 bytes
89for the metadata and 4 bytes for the data. When storing the data the
90first sector will be full after 1024/12 = 85.33 minutes. After another 85.33
91minutes, the second sector is full.  When this happens, because we're using
92only two sectors, the first sector will be used for storage and will be erased
93after 171 minutes of system time.  With the expected device life of 20,000
94writes, with two sectors writing every 171 minutes, the device should last
95about 171 * 20,000 minutes, or about 6.5 years.
96
97More generally then, with
98
99- ``NS`` as the number of storage requests per minute,
100- ``DS`` as the data size in bytes,
101- ``SECTOR_SIZE`` in bytes, and
102- ``PAGE_ERASES`` as the number of times the page can be erased,
103
104the expected device life (in minutes) can be calculated as::
105
106   SECTOR_COUNT * SECTOR_SIZE * PAGE_ERASES / (NS * (DS+8)) minutes
107
108From this formula it is also clear what to do in case the expected life is too
109short: increase ``SECTOR_COUNT`` or ``SECTOR_SIZE``.
110
111Flash write block size migration
112********************************
113It is possible that during a DFU process, the flash driver used by the NVS
114changes the supported minimal write block size.
115The NVS in-flash image will stay compatible unless the
116physical ATE size changes.
117Especially, migration between 1,2,4,8-bytes write block sizes is allowed.
118
119Sample
120******
121
122A sample of how NVS can be used is supplied in ``samples/subsys/nvs``.
123
124Troubleshooting
125***************
126
127MPU fault while using NVS, or ``-ETIMEDOUT`` error returned
128   NVS can use the internal flash of the SoC.  While the MPU is enabled,
129   the flash driver requires MPU RWX access to flash memory, configured
130   using :kconfig:option:`CONFIG_MPU_ALLOW_FLASH_WRITE`.  If this option is
131   disabled, the NVS application will get an MPU fault if it references
132   the internal SoC flash and it's the only thread running.  In a
133   multi-threaded application, another thread might intercept the fault
134   and the NVS API will return an ``-ETIMEDOUT`` error.
135
136
137API Reference
138*************
139
140The NVS subsystem APIs are provided by ``nvs.h``:
141
142.. doxygengroup:: nvs_data_structures
143
144.. doxygengroup:: nvs_high_level_api
145
146.. comment
147   not documenting
148   .. doxygengroup:: nvs
149