The nvs module has some disadvantages for larger block size. The data header and slot are taking up to much space. A rewrite is proposed that reduces the used storage space for systems with write block size > 4. The data storage in flash is now one unit consisting of: data_length, data_id, data and data_length again in a multiple of the write block size. The data_length at the end is used to validate the correctness of the flash write and also allows to travel backwards in the filesystem. As a comparison, on a system with block size 8 byte, a 32 bit values now fits 1 block including the metadata (length and id). This used to be 3 blocks. The data_length will occupy 1 byte if the data length is less than 128 byte, it will occupy 2 byte if the data length is 128 byte or more. The data length is limited to 16383 byte. Each write to flash is verified by a read back of the data. The read performance is improved because reading is done backwards so the latest items are found first. When the filesystem is locked it can be unlocked by calling reinit(), this will clear flash and setup everything for storage. add sample documentation - README.rst Update dtsi to include erase_block_size, use erase_block_size in sample Update prj.conf to include CONFIG_MPU_ALLOW_FLASH_WRITE Signed-off-by: Laczen JMS <laczenjms@gmail.com>
69 lines
2.1 KiB
ReStructuredText
69 lines
2.1 KiB
ReStructuredText
.. _nvs-sample:
|
|
|
|
NVS: Non-Volatile Storage
|
|
#########################
|
|
|
|
Overview
|
|
********
|
|
|
|
This is a simple application demonstrating use of the NVS
|
|
module for non-volatile (flash) storage. In this application,
|
|
a counter is incremented on every reboot and stored in flash,
|
|
the application reboots, and the reboot counter data is retrieved.
|
|
|
|
Requirements
|
|
************
|
|
|
|
* A board with flash support
|
|
|
|
Building and Running
|
|
********************
|
|
|
|
This sample can be found under :file:`samples/subsys/nvs` in the Zephyr tree.
|
|
|
|
The sample can be build for several platforms, the following commands build the
|
|
application for the nrf51_pca10028 board.
|
|
|
|
.. zephyr-app-commands::
|
|
:zephyr-app: samples/subsys/nvs
|
|
:board: nrf51_pca10028
|
|
:goals: build flash
|
|
:compact:
|
|
|
|
After flashing the image to the board the output on the console shows the
|
|
reboot counter and the boards reboots several times to show the reboot counter
|
|
is incremented.
|
|
|
|
Sample Output
|
|
=============
|
|
|
|
.. code-block:: console
|
|
|
|
***** Booting Zephyr OS v1.12.0-rc1-176-gf091be783 *****
|
|
[fs/nvs] [DBG] nvs_reinit: (Re)Initializing sectors
|
|
[fs/nvs] [DBG] _nvs_sector_init: f->write_location set to c
|
|
[fs/nvs] [INF] nvs_init: maximum storage length 256 byte
|
|
[fs/nvs] [INF] nvs_init: write-align: 1, write-addr: c
|
|
[fs/nvs] [INF] nvs_init: entry sector: 0, entry sector ID: 1
|
|
No address found, adding 192.168.1.1 at id 1
|
|
No key found, adding it at id 2
|
|
No Reboot counter found, adding it at id 3
|
|
Id: 4 not found, adding it
|
|
Longarray not found, adding it as id 4
|
|
Reboot counter history: ...0
|
|
Oldest reboot counter: 0
|
|
Rebooting in ...5...4...3...2...1
|
|
***** Booting Zephyr OS v1.12.0-rc1-176-gf091be783 *****
|
|
[fs/nvs] [INF] nvs_init: maximum storage length 256 byte
|
|
[fs/nvs] [INF] nvs_init: write-align: 1, write-addr: c7
|
|
[fs/nvs] [INF] nvs_init: entry sector: 0, entry sector ID: 1
|
|
Entry: 1, Address: 192.168.1.1
|
|
Id: 2, Key: ff fe fd fc fb fa f9 f8
|
|
Id: 3, Reboot_counter: 1
|
|
Id: 4, Data: DATA
|
|
Id: 5, Longarray: 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13 14 15 16 17 18
|
|
Reboot counter history: ...1...0
|
|
Oldest reboot counter: 0
|
|
Rebooting in ...5...4...3...2...1
|
|
...
|