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

Mesh 4.0 SDK Flash Manager - Storing values

I would like to store a simple integer value in the flash write and read after some event. I'm using the mesh example Dimming examples (experimental) in the nRF5 SDK for Mesh v4.0.0 and the nRF5 SDK v16.0.0.

I checked some forum responses and it was recommended to use Mesh's own Flash Manager, I took an example from the - Storing values using Flash-manager - forum and I'm trying to adapt it to the Mesh 4.0.0 Experimental Dimming example.
It seems that the variables ACCESS_FLASH_PAGE_COUNT and NET_FLASH_PAGE_COUNT are no longer used, and the dsm_flash_area_get() function as well.

This is the code that I'm trying to adapt:

/*****************************************************************************
* Custom data in flash
 *****************************************************************************/
 #define FLASH_CUSTOM_DATA_GROUP_ELEMENT 0x1500 // A number in the range 0x0000 - 0x7EFF (flash_manager.h)
 #define CUSTOM_DATA_FLASH_PAGE_COUNT 1

 typedef struct 
 {
   uint32_t data[2];
 } custom_data_format_t; // Format for the custom data

 static flash_manager_t m_custom_data_flash_manager; // flash manager instance

int main(void)
{
    uint32_t ret_code;

    __LOG_INIT(LOG_SRC_APP | LOG_SRC_ACCESS, LOG_LEVEL_INFO, LOG_CALLBACK_DEFAULT);
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "----- BLE Mesh Light Switch Client Demo -----\n");

//    mesh_core_setup();
//    access_setup();

    initialize();
//    start();

    rtt_input_enable(rtt_input_handler, RTT_INPUT_POLL_PERIOD_MS);

    /* Adding custom data to flash for persistent storage */

    // 1) Flash manager is already initialized

    // 2) Add a new flash manager instance. NB: should not overlap (in region) the instance used by mesh.  

    flash_manager_config_t custom_data_manager_config;
    custom_data_manager_config.write_complete_cb = NULL; 
    custom_data_manager_config.invalidate_complete_cb = NULL; 
    custom_data_manager_config.remove_complete_cb = NULL; 
    custom_data_manager_config.min_available_space = WORD_SIZE;

    // The new instance of flash manager should use an unused region of flash:
//    custom_data_manager_config.p_area = (const flash_manager_page_t *) (((const uint8_t *) dsm_flash_area_get()) - (ACCESS_FLASH_PAGE_COUNT * PAGE_SIZE) - (NET_FLASH_PAGE_COUNT * PAGE_SIZE) );
//    custom_data_manager_config.p_area = (const flash_manager_page_t *) (((const uint8_t *) flash_manager_recovery_page_get()) - (ACCESS_FLASH_PAGE_COUNT * PAGE_SIZE) - (NET_FLASH_PAGE_COUNT * PAGE_SIZE) );
    custom_data_manager_config.p_area = (const flash_manager_page_t *) ((const uint8_t *) flash_manager_recovery_page_get());
    
    custom_data_manager_config.page_count = CUSTOM_DATA_FLASH_PAGE_COUNT;
   
    ret_code = flash_manager_add(&m_custom_data_flash_manager, &custom_data_manager_config);
   
    if (NRF_SUCCESS != ret_code) {
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "Flash error: no memory\n",ret_code);
    
    }
         
    // 3) Write to Flash

    // a) allocate flash 
    fm_entry_t * p_entry = flash_manager_entry_alloc(&m_custom_data_flash_manager, FLASH_CUSTOM_DATA_GROUP_ELEMENT, sizeof(custom_data_format_t));
    if (p_entry == NULL)
    {
       __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "IF \n");
      return NRF_ERROR_BUSY;
    } 
    else
    {
       __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "ELSE \n");

      custom_data_format_t * p_custom_data = (custom_data_format_t *) p_entry->data;
      p_custom_data->data[0] = 5;
      p_custom_data->data[1] = 9;
   
      // b) write to flash
      flash_manager_entry_commit(p_entry);
      __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "write:%x, %x\n",p_entry->data[0], p_entry->data[1]);
      
    }
    
    
    // 4) Wait for flash manager to finish.
    flash_manager_wait();
   
   
    // 5) Read from Flash
    const fm_entry_t * p_read_raw = flash_manager_entry_get(&m_custom_data_flash_manager, FLASH_CUSTOM_DATA_GROUP_ELEMENT);
    
    const custom_data_format_t * p_read_data = (const custom_data_format_t *) p_read_raw->data;
    __LOG(LOG_SRC_APP, LOG_LEVEL_INFO, "read:%x, %x\n",p_read_data->data[0], p_read_data->data[1]);

    nrf_delay_ms(5000);
//    initialize();
    start();

    for (;;)
    {
        app_sched_execute();
//        NRF_LOG_PROCESS();
//        NRF_LOG_INFO("BSP example started.");
        NRF_LOG_FLUSH();
        __WFE();
        (void)sd_app_evt_wait();
    }
}

When I execute this code it goes through line 49 and goes in Loop Forever of the file thumb_crt0.s line 332

Could you help me adapt this code to the example of Mesh 4.0?

Thank you in advance.

Related