TorryLong

博客园 首页 新随笔 联系 订阅 管理
/* Dummy configuration data. */
static configuration_t m_dummy_cfg =
{
    .config1_on  = false,
    .config2_on  = true,
    .boot_count  = 0x0,
    .device_name = "dummy",
};

/* A record containing dummy configuration data. */
static fds_record_t const m_dummy_record =
{
    .file_id           = CONFIG_FILE,
    .key               = CONFIG_REC_KEY,
    .data.p_data       = &ucheck_v,//&m_dummy_cfg,
    /* The length of a record is always expressed in 4-byte units (words). */
    .data.length_words = (sizeof(ucheck_v) + 3) / sizeof(uint32_t),
};

/* Keep track of the progress of a delete_all operation. */
//static struct
//{
//    bool delete_next;   //!< Delete next record.
//    bool pending;       //!< Waiting for an fds FDS_EVT_DEL_RECORD event, to delete the next record.
//} m_delete_all;

void user_data_update(void)
{
    ret_code_t rc;

//    NRF_LOG_INFO("Reading flash usage statistics...");

    fds_stat_t stat = {0};

    rc = fds_stat(&stat);
    APP_ERROR_CHECK(rc);

    NRF_LOG_INFO("Found %d valid records.", stat.valid_records);
    NRF_LOG_INFO("Found %d dirty records (ready to be garbage collected).", stat.dirty_records);

    fds_record_desc_t desc = {0};
    fds_find_token_t  tok    = {0};

    rc = fds_record_find(CONFIG_FILE, CONFIG_REC_KEY, &desc, &tok);

    if (rc == FDS_SUCCESS)
    {
    /* A config file is in flash. Let's update it. */
        fds_flash_record_t config = {0};

        /* Open the record and read its contents. */
        rc = fds_record_open(&desc, &config);
        APP_ERROR_CHECK(rc);

        /* Copy the configuration from flash into m_dummy_cfg. */
        //memcpy(&ucheck_v, config.p_data, sizeof(check_V));//这里是更新数据,所以不能把旧数据复制过来了

        NRF_LOG_INFO("Config file found, updating boot count to %d.", ucheck_v.boot_count);

        /* Update boot count. */
        ucheck_v.boot_count++;

        /* Close the record when done reading. */
        rc = fds_record_close(&desc);
        APP_ERROR_CHECK(rc);

        /* Write the updated record to flash. */
        rc = fds_record_update(&desc, &m_dummy_record);
        APP_ERROR_CHECK(rc);
    }
    else
    {
    /* System config not found; write a new one. */
        NRF_LOG_INFO("Writing config file...");

        rc = fds_record_write(&desc, &m_dummy_record);
        APP_ERROR_CHECK(rc);
    }

    //cli_start();
}
void user_init_data_flash(void)
{
    ret_code_t rc;
    fds_record_desc_t desc = {0};
    fds_find_token_t  tok    = {0};

    rc = fds_record_find(CONFIG_FILE, CONFIG_REC_KEY, &desc, &tok);

    if (rc == FDS_SUCCESS)
    {
    /* A config file is in flash. Let's update it. */
        fds_flash_record_t config = {0};

        /* Open the record and read its contents. */
        rc = fds_record_open(&desc, &config);
        APP_ERROR_CHECK(rc);

        /* Copy the configuration from flash into m_dummy_cfg. */
        memcpy(&ucheck_v, config.p_data, sizeof(check_V));

        NRF_LOG_INFO("Config file found, updating boot count to %d.", ucheck_v.boot_count);

        /* Update boot count. */
        ucheck_v.boot_count++;

        /* Close the record when done reading. */
        rc = fds_record_close(&desc);
        APP_ERROR_CHECK(rc);

        /* Write the updated record to flash. */
        rc = fds_record_update(&desc, &m_dummy_record);
        APP_ERROR_CHECK(rc);
    }

}

 

在增加了DFU之后的52832程序,只要增加如下程序就可以完成FDS的数据保存

 

posted on 2020-09-09 17:34  TorryLong  阅读(381)  评论(0编辑  收藏  举报