NVOL是持续的闪光注册表,通常称为Eeprom仿真。它提供了一个基本的API,用于读取和从闪存读取索引键/值对,类似于注册表。重要的是要提及的是,密钥和值都存储在Flash中,从而可以在运行时修改注册表。 NVOL的某些功能包括:
该库提供了两个选项来管理数据存储:选项1:每当更改时,立即将条目保存到闪存。选项2:将条目存储在RAM中,并按需保存到闪存。
闪存的缺点是它不能用单个字节删除或编写。闪存只能删除并写入大块。典型的擦除块大小可能是4K或8K字节。对于NVOL,Flash实现应支持部分写作,只要位从“ 1”(删除状态)更改为“ 0”(编程状态),删除的块可能会被多次编写。
对于闪光灯,该页面被认为是可以删除的最小块大小。每个部门都可以由其中一个或多个页面组成。
最初被抹去了第一部门。新的注册表条目将依次添加到闪存中。更新条目后,旧条目将标记为无效,并在下一个可用的闪存地址上写了一个新条目。一旦第一部门达到容量,所有有效的条目将复制到第二部门,然后删除第一部门。这个过程重复自我。
NVOL有效地处理并跟踪有效的条目及其位置在Flash上。这些部门是动态管理的。
宏可用于创建NVOL的静态实例,然后可以与API一起使用。声明的一个例子是:
#define NVOL3_REGISTRY_START 0
#define NVOL3_REGISTRY_SECTOR_SIZE STORAGE_32K
#define NVOL3_REGISTRY_SECTOR_COUNT 2
#define REGISTRY_KEY_LENGTH 24
#define REGISTRY_VALUE_LENGT_MAX 224
NVOL3_INSTANCE_DECL(_regdef_nvol3_entry,
ramdrv_read, ramdrv_write, ramdrv_erase,
NVOL3_REGISTRY_START,
NVOL3_REGISTRY_START + NVOL3_REGISTRY_SECTOR_SIZE,
NVOL3_REGISTRY_SECTOR_SIZE,
REGISTRY_KEY_LENGTH, /*key_size*/
DICTIONARY_KEYSPEC_BINARY(6), /*dictionary key_type (24 char string)*/
53, /*hashsize*/
REGISTRY_VALUE_LENGT_MAX, /*data_size*/
0, /*local_size (no cache in RAM)*/
0, /*tallie*/
NVOL3_SECTOR_VERSION /*version*/
) ;
这在闪光灯的开头创建了一个带有两个32K扇区的NVOL。钥匙的总尺寸(包括入口标头)为256个字节。这不是要求,但应考虑一致。查找字典的哈希尺寸为53。此外,此实例不会在RAM中存储任何值( local_size = 0 ),因此在需要时始终从Flash中读取它们。
在演示中,使用NVRAMDRV驱动程序,以模拟RAM中的闪存,访问功能是为此实例配置的RAMDRV_READ,RAMDRV_WRITE和RAMDRV_ERASE。
现在_regdef_nvol3_entry可以与NVOL API一起使用。 NVOL API略有调整,因此提供了一个简单的注册表示例。
提供的测试示例可以编译并在GitHub代码空间中运行。只需打开此转换的代码空间,然后在打开的终端中make (如果未打开终端使用ctrl + `以打开终端)。现在,您可以通过在终端中键入./build/nvol来启动NVOL示例。所有命令都可以在代码空间的终端执行。
启动示例程序时,将打开命令外壳,并显示以下内容:
@navaro /workspaces/nvol (main) $ ./build/nvol
REG : : resetting _regdef_nvol3_entry
NVOL3 : : '_regdef_nvol3_entry' 0 / 255 records loaded
Navaro corshell Demo v 'Jan 16 2023'
use 'help' or '?' for help.
# >
首先,我们将运行一个简单的脚本来用值填充我们的注册表。在及时类型上:
source ./test/reg.sh
这应该在注册表中充满值。列出测试注册表类型的所有命令? reg ,它应显示以下列表:
# >? reg
reg [key] [value]
regadd <key> <value>
regdel <key>
regerase
regstats
regtest [repeat]
# >
现在,我们可以使用Reg命令查看和更新注册表:
# >reg
player.age: Ancient
player.gender: Otherworldly
player.level: legendary
player.location: Mount Olympus
player.name: cool_cat
player.points: 9000
player.power: invisibility
player.species: Dragon
player.status: awakened
player.weapon: lightning bolt
test: 123
user.address: 123 Main St.
user.age: 30
user.children: 0
user.email: [email protected]
user.favorite_color: blue
user.gender: male
user.marital_status: single
user.name: John Smith
user.occupation: contributor
user.phone_number: 555-555-5555
21 entries found.
# >reg user.favorite_color yellow
OK
# >reg user.favorite_color
user.favorite_color: yellow
# >regdel user.gender
OK
# >reg player
player.age: Ancient
player.gender: Otherworldly
player.level: legendary
player.location: Mount Olympus
player.name: cool_cat
player.points: 9000
player.power: invisibility
player.species: Dragon
player.status: awakened
player.weapon: lightning bolt
10 entries found.
# >regstats
NVOL3 : : '_regdef_nvol3_entry' 20 / 255 records loaded
record : 256 recordsize
: 0x000000 1st sector version 0x0155 flags 0xaaaaffff
: 0x010000 2nd sector version 0x0000 flags 0xffffffff
: 0x010000 sector size
: 20 loaded
: 20 inuse
: 2 invalid
: 0 error
: 640 lookup table bytes
: 53 dict hash size
: dict hash - max 2, empty 34, used 19
这些命令均在src/registry/registrycmd.c中实现。该实施是直观和自我解释的,不应需要进一步的解释。
外壳本身就是一个项目,但仅在此示例中包括出于演示目的。它很容易扩展。使用?查看为此示例实现的命令的完整列表。
在注册表使用字符串索引条目的地方,字符串表使用整数值。这种方法需要更少的RAM资源。
对于此示例,还有一个脚本可以用值填充字符串表。在及时类型上:
source ./test/strtab.sh
同样,我们现在可以使用一个简单命令列出字符串表:
# >strtab
0000 ENGLISH
0001 The end of the world is our playground.
0002 The company is family. The family is the company.
0003 Escape the mundane. Embrace the extraordinary.
0004 Work hard, live easy.
0005 The future is now. Join us.
0006 There is no I in team. But there is in severance.
0007 We're not just a company. We're a movement.
0008 Success is a journey, not a destination.
0009 Innovation starts with you.
0010 Welcome to the beginning of something great.
0100 DUTCH
0101 Het einde van de wereld is onze speeltuin.
0102 Het bedrijf is familie. De familie is het bedrijf.
0103 Ontsnap aan het alledaagse. Omarm het buitengewone.
0104 Hard werken, makkelijk leven.
0105 De toekomst is nu. Doe met ons mee.
0106 Er is geen ik in team. Maar wel in ontslagvergoeding.
0107 We zijn niet alleen een bedrijf. We zijn een beweging.
0108 Succes is een reis, geen bestemming.
0109 Innovatie begint bij jou.
0110 Welkom bij het begin van iets groots.
0999 test
23 entries found.
# >
像注册表一样,字符串表示例用字符串替换库将其自身注册,以替换用字符串表中的字符串划分为<>的索引值。例如:
# >echo "<1>"
The end of the world is our playground.
# >
该演示使用仿真驱动程序在RAM中进行Flash。这是在“ CRC/DRIVERS/RAMDRV.H/C”中实现的。
在同一目录中,在“ spiflash.h/c”文件中,将提供真实闪存芯片的示例驱动程序。使用Chibios/hal Spi驱动程序对此进行了启动,应该是将NVOL移植到您自己的平台的好起点。