Mbed OS 文档翻译 之 参考(API(存储(NVStore)))

NVStore

                                                                        

                                                                              NVStore 类层次结构

NVStore 是一个轻量级模块,出于安全考虑,可以通过内部闪存中的密钥存储数据。对于每个项目键,NVStore 模块提供设置项目数据或获取项目数据的功能。新添加的值将添加到现有数据的末尾,取代之前相同键的值。NVStore 模块可确保在任何操作期间电源故障不会损害现有数据。

Flash 结构体

NVStore 使用两个 Flash 区域,即活动区域和非活动区域。每个区域必须至少包含一个可擦除单元(扇区)。数据被写入活动区域,直到它变满。如果是,则调用垃圾收集。这将项目从活动区域压缩到非活动区域,并切换区域之间的活动。每个项目都保存在包含标题和数据的条目中,其中标题包含项目键,大小和 CRC。

除非用户特别配置,否则 NVStore 会选择最后两个闪存扇区作为其区域,最小大小为 4KB。这意味着如果扇区较小,NVStore 会为每个区域使用一些连续的扇区。如果自动选择的扇区不适合您的闪存配置,您可以通过为每个板设置 mbed_app.json 中两个区域的地址和大小来覆盖它。

NVStore 类参考

NVStore 类参考

公共成员函数
uint16_t get_max_keys () const
 返回键的数量。更多...
void set_max_keys (uint16_t num_keys)
 设置键数。更多...
uint16_t get_max_possible_keys ()
 返回最大可能的键数(在此闪存配置中)。更多...
int get (uint16_t key, uint16_t buf_size, void *buf, uint16_t &actual_size)
 返回给定键,在 Flash 上编程的一项数据项。更多...
int get_item_size (uint16_t key, uint16_t &actual_size)
 给定键,返回 Flash 上编程的数据大小。更多...
int set (uint16_t key, uint16_t buf_size, const void *buf)
 给定密钥,对 Flash 上的一项数据进行编程。更多...
int allocate_key (uint16_t &key, uint8_t owner=NVSTORE_UNSPECIFIED_OWNER)
 分配一个空闲键(稍后将在设置操作中使用)。更多...
int free_all_keys_by_owner (uint8_t owner)
 释放属于特定所有者的所有已分配密钥。更多...
int set_once (uint16_t key, uint16_t buf_size, const void *buf)
 在给定密钥的情况下对 Flash 上的一项数据进行编程,不允许对此密钥进行任何后续设置。更多...
int remove (uint16_t key)
 从闪光灯中删除项目。更多...
int init ()
 初始化 NVStore 组件。更多...
int deinit ()
 取消初始化 NVStore 组件。警告:此功能不是线程安全的,不应与其他 NVStore 功能同时调用。更多...
int reset ()
 重置 Flash NVStore 区域。警告:此功能不是线程安全的,不应与其他 NVStore 功能同时调用。更多...
size_t size ()
 返回 NVStore 大小(区域大小)。更多...
int get_area_params (uint8_t area, uint32_t &address, size_t &size)
 返回 NVStore 区域的地址和大小。更多...
静态公共成员函数
static NVStoreget_instance ()
 作为单例,返回类的单个实例。此类为单例的原因如下:更多...

NVStore 示例

main.cpp                                                                                                                                                 导入到 Mbed IDE

/*
* Copyright (c) 2018 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "mbed.h"
#include "nvstore.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

// Entry point for the example
int main() {
    printf("\n--- Mbed OS NVStore example ---\n");
#if NVSTORE_ENABLED

    uint16_t actual_len_bytes = 0;

    // NVStore is a sigleton, get its instance
    NVStore &nvstore = NVStore::get_instance();

    int rc;
    uint16_t key;

    // Values read or written by NVStore need to be aligned to a uint32_t address (even if their sizes
    // aren't)
    uint32_t value;

    // Initialize NVstore. Note that it can be skipped, as it is lazily called by all other APIs
    rc = nvstore.init();
    printf("Init NVStore. ");
    printf("Return code is %d\n", rc);

    // Show NVStore size, maximum number of keys and area addresses and sizes
    printf("NVStore size is %d\n", nvstore.size());
    printf("NVStore max number of keys is %d (out of %d possible ones in this flash configuration)\n",
            nvstore.get_max_keys(), nvstore.get_max_possible_keys());
    printf("NVStore areas:\n");
    for (uint8_t area = 0; area < NVSTORE_NUM_AREAS; area++) {
        uint32_t area_address;
        size_t area_size;
        nvstore.get_area_params(area, area_address, area_size);
        printf("Area %d: address 0x%08lx, size %d (0x%x)\n", area, area_address, area_size, area_size);
    }

    // Clear NVStore data. Should only be done once at factory configuration
    rc = nvstore.reset();
    printf("Reset NVStore. ");
    printf("Return code is %d\n", rc);

    // Now set some values to the same key
    key = 1;

    value = 1000;
    rc = nvstore.set(key, sizeof(value), &value);
    printf("Set key %d to value %ld. ", key, value);
    printf("Return code is %d\n", rc);

    value = 2000;
    rc = nvstore.set(key, sizeof(value), &value);
    printf("Set key %d to value %ld. ", key, value);
    printf("Return code is %d\n", rc);

    value = 3000;
    rc = nvstore.set(key, sizeof(value), &value);
    printf("Set key %d to value %ld. ", key, value);
    printf("Return code is %d\n", rc);

    // Get the value of this key (should be 3000)
    rc = nvstore.get(key, sizeof(value), &value, actual_len_bytes);
    printf("Get key %d. Value is %ld. ", key, value);
    printf("Return code is %d\n", rc);

    // Now remove the key
    rc = nvstore.remove(key);
    printf("Delete key %d. ", key);
    printf("Return code is %d\n", rc);

    // Get the key again, now it should not exist
    rc = nvstore.get(key, sizeof(value), &value, actual_len_bytes);
    printf("Get key %d. ", key);
    printf("Return code is %d\n", rc);

    key = 12;

    // Now set another key once (it can't be set again)
    value = 50;
    rc = nvstore.set_once(key, sizeof(value), &value);
    printf("Set key %d once to value %ld. ", key, value);
    printf("Return code is %d\n", rc);

    // This should fail on key already existing
    value = 100;
    rc = nvstore.set(key, sizeof(value), &value);
    printf("Set key %d to value %ld. ", key, value);
    printf("Return code is %d\n", rc);

    // Get the value of this key (should be 50)
    rc = nvstore.get(key, sizeof(value), &value, actual_len_bytes);
    printf("Get key %d. Value is %ld. ", key, value);
    printf("Return code is %d\n", rc);

    // Get the data size for this key (should be 4)
    rc = nvstore.get_item_size(key, actual_len_bytes);
    printf("Data size for key %d is %d. ", key, actual_len_bytes);
    printf("Return code is %d\n", rc);
#else
    printf("NVStore is disabled for this board\n");
#endif

    printf("\n--- Mbed OS NVStore example done. ---\n");
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值