Subsys: Settings¶
1 功能概述¶
Zephyr 提供了一个名为 Settings 的键值对(Key-Value Pair)字符串数据存储子系统,其实现基于 NVS 或文件系统提供的底层接口。本例程演示 Settings 相关接口的基本用法,包括 NVS Backend 与 Runtime Backend。
2 环境要求¶
Board: pan108xxa1_evb / pan108xxa3_evb / pan108xxb1_evb / pan108xxb5_evb
UART: 将 P00 与 TX0 引脚接在一起,P01 与 RX0 引脚接在一起,在 PC 上打开终端工具(如 SecureCRT),波特率设置为 921600
3 编译和烧录¶
例程位置:zephyr\samples_panchip\subsys\settings
使用 ZAL 工具可以对其进行编译、烧录、打开 VS Code 调试等操作。关于 ZAL 工具的详细介绍请参考:Zephyr APP Launcher 工具介绍。
4 演示说明¶
程序下载后,在终端工具(SecureCRT)中可以看到如下的 Log 打印信息:
Try to load HW calibration data.. DONE.
- Chip Type : 0x80
- Chip CP Version : None
- Chip FT Version : 5
- Chip MAC Address : D0000C029382
- Chip Flash UID : 31373237300A44594330FFFFFFFFFFFF
- Chip Flash Size : 1024 KB
*** Booting Zephyr OS build zephyr-v2.7.0-1633-gc485e109 ***
*** Settings usage example ***
settings subsys initialization: OK.
subtree <alpha> handler registered: OK
subtree <alpha/beta> has static handler
##############
# iteration 0
##############
=================================================
basic load and save using registered handlers
load all key-value pairs using registered handlers
<alpha/length/2> = 42
<alpha/length/1> = 58
<alpha/angle/1> = 18
<alpha/beta/source> =
<alpha/beta/voltage> = -3450
loading all settings under <beta> handler is done
loading all settings under <alpha> handler is done
save <alpha/beta/voltage> key directly: OK.
load <alpha/beta> key-value pairs using registered handlers
<alpha/beta/source> =
<alpha/beta/voltage> = -3475
loading all settings under <beta> handler is done
save all key-value pairs using registered handlers
export keys under <beta> handler
export keys under <alpha> handler
load all key-value pairs using registered handlers
export keys under <beta> handler
export keys under <alpha> handler
=================================================
loading subtree to destination provided by the caller
direct load: <alpha/length>
direct load: <alpha/length/2>
direct load: <alpha/length/1>
direct.length = 100
direct.length_1 = 59
direct.length_2 = 41
=================================================
Delete a key-value pair
immediate load: OK.
<alpha/length> value exist in the storage
delete <alpha/length>: OK.
Can't to load the <alpha/length> value as expected
=================================================
Service a key-value pair without dedicated handlers
immediate load: OK.
<gamma> = 18
save <gamma> key directly: OK.
##############
# iteration 1
##############
=================================================
basic load and save using registered handlers
load all key-value pairs using registered handlers
<alpha/length/2> = 41
<alpha/length/1> = 59
<alpha/angle/1> = 19
<alpha/beta/source> = a
<alpha/beta/voltage> = -3475
loading all settings under <beta> handler is done
loading all settings under <alpha> handler is done
save <alpha/beta/voltage> key directly: OK.
load <alpha/beta> key-value pairs using registered handlers
<alpha/beta/source> = a
<alpha/beta/voltage> = -3500
loading all settings under <beta> handler is done
save all key-value pairs using registered handlers
export keys under <beta> handler
export keys under <alpha> handler
load all key-value pairs using registered handlers
export keys under <beta> handler
export keys under <alpha> handler
=================================================
loading subtree to destination provided by the caller
direct load: <alpha/length>
direct load: <alpha/length/2>
direct load: <alpha/length/1>
direct.length = 100
direct.length_1 = 60
direct.length_2 = 40
=================================================
Delete a key-value pair
immediate load: OK.
<alpha/length> value exist in the storage
delete <alpha/length>: OK.
Can't to load the <alpha/length> value as expected
=================================================
Service a key-value pair without dedicated handlers
immediate load: OK.
<gamma> = 19
save <gamma> key directly: OK.
##############
# iteration 2
##############
=================================================
basic load and save using registered handlers
load all key-value pairs using registered handlers
<alpha/length/2> = 40
<alpha/length/1> = 60
<alpha/angle/1> = 20
<alpha/beta/source> = ab
<alpha/beta/voltage> = -3500
loading all settings under <beta> handler is done
loading all settings under <alpha> handler is done
save <alpha/beta/voltage> key directly: OK.
load <alpha/beta> key-value pairs using registered handlers
<alpha/beta/source> = ab
<alpha/beta/voltage> = -3525
loading all settings under <beta> handler is done
save all key-value pairs using registered handlers
export keys under <beta> handler
export keys under <alpha> handler
load all key-value pairs using registered handlers
export keys under <beta> handler
export keys under <alpha> handler
=================================================
loading subtree to destination provided by the caller
direct load: <alpha/length>
direct load: <alpha/length/2>
direct load: <alpha/length/1>
direct.length = 100
direct.length_1 = 61
direct.length_2 = 39
=================================================
Delete a key-value pair
immediate load: OK.
<alpha/length> value exist in the storage
delete <alpha/length>: OK.
Can't to load the <alpha/length> value as expected
=================================================
Service a key-value pair without dedicated handlers
immediate load: OK.
<gamma> = 20
save <gamma> key directly: OK.
##############
# iteration 3
##############
=================================================
basic load and save using registered handlers
load all key-value pairs using registered handlers
<alpha/length/2> = 39
<alpha/length/1> = 61
<alpha/angle/1> = 21
<alpha/beta/source> = abc
<alpha/beta/voltage> = -3525
loading all settings under <beta> handler is done
loading all settings under <alpha> handler is done
save <alpha/beta/voltage> key directly: OK.
load <alpha/beta> key-value pairs using registered handlers
<alpha/beta/source> = abc
<alpha/beta/voltage> = -3550
loading all settings under <beta> handler is done
save all key-value pairs using registered handlers
export keys under <beta> handler
export keys under <alpha> handler
load all key-value pairs using registered handlers
export keys under <beta> handler
export keys under <alpha> handler
=================================================
loading subtree to destination provided by the caller
direct load: <alpha/length>
direct load: <alpha/length/2>
direct load: <alpha/length/1>
direct.length = 100
direct.length_1 = 62
direct.length_2 = 38
=================================================
Delete a key-value pair
immediate load: OK.
<alpha/length> value exist in the storage
delete <alpha/length>: OK.
Can't to load the <alpha/length> value as expected
=================================================
Service a key-value pair without dedicated handlers
immediate load: OK.
<gamma> = 21
save <gamma> key directly: OK.
##############
# iteration 4
##############
=================================================
basic load and save using registered handlers
load all key-value pairs using registered handlers
<alpha/length/2> = 38
<alpha/length/1> = 62
<alpha/angle/1> = 22
<alpha/beta/source> = abcd
<alpha/beta/voltage> = -3550
loading all settings under <beta> handler is done
loading all settings under <alpha> handler is done
save <alpha/beta/voltage> key directly: OK.
load <alpha/beta> key-value pairs using registered handlers
<alpha/beta/source> = abcd
<alpha/beta/voltage> = -3575
loading all settings under <beta> handler is done
save all key-value pairs using registered handlers
export keys under <beta> handler
export keys under <alpha> handler
load all key-value pairs using registered handlers
export keys under <beta> handler
export keys under <alpha> handler
=================================================
loading subtree to destination provided by the caller
direct load: <alpha/length>
direct load: <alpha/length/2>
direct load: <alpha/length/1>
direct.length = 100
direct.length_1 = 63
direct.length_2 = 37
=================================================
Delete a key-value pair
immediate load: OK.
<alpha/length> value exist in the storage
delete <alpha/length>: OK.
Can't to load the <alpha/length> value as expected
=================================================
Service a key-value pair without dedicated handlers
immediate load: OK.
<gamma> = 22
save <gamma> key directly: OK.
##############
# iteration 5
##############
=================================================
basic load and save using registered handlers
load all key-value pairs using registered handlers
<alpha/length/2> = 37
<alpha/length/1> = 63
<alpha/angle/1> = 23
<alpha/beta/source> is not compatible with the application
<alpha/beta/voltage> = -3575
loading all settings under <beta> handler is done
loading all settings under <alpha> handler is done
save <alpha/beta/voltage> key directly: OK.
load <alpha/beta> key-value pairs using registered handlers
<alpha/beta/source> is not compatible with the application
<alpha/beta/voltage> = -3600
loading all settings under <beta> handler is done
save all key-value pairs using registered handlers
export keys under <beta> handler
export keys under <alpha> handler
load all key-value pairs using registered handlers
export keys under <beta> handler
export keys under <alpha> handler
=================================================
loading subtree to destination provided by the caller
direct load: <alpha/length>
direct load: <alpha/length/2>
direct load: <alpha/length/1>
direct.length = 100
direct.length_1 = 64
direct.length_2 = 36
=================================================
Delete a key-value pair
immediate load: OK.
<alpha/length> value exist in the storage
delete <alpha/length>: OK.
Can't to load the <alpha/length> value as expected
=================================================
Service a key-value pair without dedicated handlers
immediate load: OK.
<gamma> = 23
save <gamma> key directly: OK.
=================================================
Inject the value to the setting destination in runtime
<alpha/beta/source> = RT
injected <alpha/beta/source>: OK.
The settings destination off the key <alpha/beta/source> has got value: "RT"
=================================================
Read a value from the setting destination in runtime
fetched <alpha/beta/source>: OK.
String value "rtos" was retrieved from the settings destination off the key <alpha/beta/source>
*** THE END ***
5 开发说明¶
本例程演示的 Settings 功能需要开启如下的 Kconfig 选项:
CONFIG_FLASH=y CONFIG_FLASH_PAGE_LAYOUT=y CONFIG_FLASH_MAP=y CONFIG_SETTINGS=y CONFIG_SETTINGS_RUNTIME=y CONFIG_NVS=y CONFIG_SETTINGS_NVS=y
settings 相关接口定义及介绍请参考:
zephyr\include\settings\settings.h
6 RAM/Flash资源使用情况¶
Memory region Used Size Region Size %age Used
FLASH: 26592 B 384 KB 6.76%
SRAM: 4344 B 64 KB 6.63%