This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Storing values using Flash-manager

Hi Team

I would like to store a simple integer value in flash and read it back after a power cycle. I already studied the library Flash-manager of the nRF5 SDK for Mesh v1.0.1. Since I found no example code this seems quite hard to do. Can you give us some example lines of code to write and read simple values to/from flash?

kind regards

Gerry

 

Parents Reply Children
  • Hi Kristin

    I think it would be good to have an example for that, since it makes not much sense to write a mesh application without beeng able to persistantly store values or settings and its not an easy task to do so with the information given in the infocenter. So I made some example code to store and read back from flash. Maybe you can fix it and then other developers can use it also.

    Kind regards Gerry

    // Init Flash
    flash_manager_init ();
    #define FLASH_HANDLE_METADATA 0x0002
    const fm_entry_t * p_metadata_entry = flash_manager_entry_get(&m_flash_manager, FLASH_HANDLE_METADATA);
    if (p_metadata_entry == NULL)
    {
    flash_manager_config_t manager_config;
    manager_config.write_complete_cb = NULL; //flash_write_complete;
    manager_config.invalidate_complete_cb = NULL; //flash_invalidate_complete;
    manager_config.remove_complete_cb = NULL; //flash_remove_complete;
    manager_config.min_available_space = WORD_SIZE;
    manager_config.p_area = (const flash_manager_page_t *) (((const uint8_t *) dsm_flash_area_get()) - (ACCESS_FLASH_PAGE_COUNT * PAGE_SIZE));
    manager_config.page_count = ACCESS_FLASH_PAGE_COUNT;
    uint32_t status = flash_manager_add(&m_flash_manager, &manager_config);
    if (NRF_SUCCESS != status) {
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Flash error: no memory\n",status);
    }
    }

    //Write to Flash
    #define FLASH_GROUP_ELEMENT 0x1500
    uint16_t element_index = 1;
    fm_entry_t * p_entry = flash_manager_entry_alloc(&m_flash_manager, FLASH_GROUP_ELEMENT | element_index, sizeof(uint16_t));
    if (p_entry == NULL)
    {
    return NRF_ERROR_BUSY;
    }
    else
    {
    uint16_t fstore = 7;
    uint32_t data_[] = {1,2};
    uint16_t * p_element_location = (uint16_t *) p_entry->data;
    *p_element_location = 7;
    flash_manager_entry_commit(p_entry);
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "write:%u\n",p_entry);
    }

    waitevent();


    //Read from Flash
    const fm_entry_t * fread = flash_manager_entry_get(&m_flash_manager, FLASH_GROUP_ELEMENT);
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "read:%u\n",*fread);

  • FormerMember
    0 FormerMember in reply to Gerry

    Yes, I agree that there should be a mesh example showing how to store persistent data. 

    Thanks for sharing your code! I will add it to an example so that it easily can be tested. 

Related