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

Flash handling after sdk 14 migration unclear

Edit: Rephrased the question to make it easier to answer.

I used flash storage to store a couple of variables between resets in a app made from SDK 12.2, now I am attempting to migrate to SDK 14, but I can't figure out how to make the fstorage migration correct, in particular the initialization and then how to read from the flash is unclear..

I was thinking of adding DFU later, so if I should handle this differently alltogether, I am totally open for suggestions but i struggle with applying the same principle from the other examples (hrs etc)

I get this err_code = 0x00000010 which I guess translates to NRF_ERROR_INVALID_ADDR (NRF_ERROR_BASE_NUM + 16) ///< Bad Memory Address when calling nrf_fstorage_erase(&m_fs, m_fs.start_addr, 4, NULL);

I get that I am using start_addr wrong but I can't figure how to fix it..

The project is built on peripheral examples\ble_app_uart

main routine:

NRF_FSTORAGE_DEF(nrf_fstorage_t m_fs) =
{
    .evt_handler = fs_event_handler,
};


/**@brief Application main function.
 */
int main(void)
{
    uint32_t err_code;
    bool     erase_bonds;
    
    uint32_t data;
    uint32_t flash_data[4];
    

    // Initialize.
    err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);

    uart_init();
    log_init();
    
    err_code = nrf_fstorage_init(&m_fs, &nrf_fstorage_sd, NULL);
    APP_ERROR_CHECK(err_code);
    
    buttons_leds_init(&erase_bonds);
    ble_stack_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();

    printf("\r\nUART Start!");
    NRF_LOG_INFO("UART Start!");
    err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);
    NRF_LOG_FLUSH();
    // Enter main loop.
    for (;;)
    {
        if(do_flash_operation)
        {   
            do_flash_operation = false;
            // Erase one page
            NRF_LOG_INFO("Erasing a flash page at address 0x%X\r\n", (uint32_t)m_fs.start_addr);
            fs_callback_flag = 1;
            err_code = nrf_fstorage_erase(&m_fs, m_fs.start_addr, 4, NULL);
            APP_ERROR_CHECK(err_code);
            if (err_code != NRF_SUCCESS)
            {
                bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
                NRF_LOG_ERROR("erase error");
            }
            while(fs_callback_flag == 1)  { power_manage(); }
            
            //Read the first 4 words of the page
            NRF_LOG_INFO("Data read from flash address 0x%X: \r\n", (uint32_t)m_fs.start_addr);
            err_code = nrf_fstorage_read(&m_fs, m_fs.start_addr, flash_data, 32);
            APP_ERROR_CHECK(err_code);
            for(int i=0; i<4; i++)
            {                  
                NRF_LOG_INFO("%X ", flash_data[i]);
            }
            NRF_LOG_INFO("\r\n");
            
            data = 0xAAAAAAAA;
            NRF_LOG_INFO("Writing data 0x%X to address 0x%X\r\n", data, (uint32_t)m_fs.start_addr);
            fs_callback_flag = 1;
            err_code = nrf_fstorage_write(&m_fs, m_fs.start_addr, &data, 4, NULL);      //Write data to memory address. Check it with command: nrfjprog --memrd addr --n 32
            APP_ERROR_CHECK(err_code);
            if (err_code != NRF_SUCCESS)
            {
                bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
                NRF_LOG_ERROR("write error");
            }
            while(fs_callback_flag == 1)  { power_manage(); }
            
            data = 0xBBBBBBBB;
            NRF_LOG_INFO("Writing data 0x%X to address 0x%X\r\n", data, (uint32_t)m_fs.start_addr + 4);
            fs_callback_flag = 1;
            err_code = nrf_fstorage_write(&m_fs, m_fs.start_addr + 1, &data, 4, NULL);
            APP_ERROR_CHECK(err_code);
            if (err_code != NRF_SUCCESS)
            {
                bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
                NRF_LOG_ERROR("write error");
            }
            while(fs_callback_flag == 1)  { power_manage(); }
            
            //Read the first 4 words of the page
            NRF_LOG_INFO("Data read from flash address 0x%X: \r\n", (uint32_t)m_fs.start_addr);
            err_code = nrf_fstorage_read(&m_fs, m_fs.start_addr, flash_data, 32);
            APP_ERROR_CHECK(err_code);
            for(int i=0; i<4; i++)
            {
                NRF_LOG_INFO("%X ", flash_data[i]);
            }
            NRF_LOG_INFO("\r\n");
        }
        
        if (NRF_LOG_PROCESS() == false)
        {
            NRF_LOG_FLUSH();
            power_manage();
        }
        NRF_LOG_FLUSH();
    }
}

temp.link to project (upload file doesn't work??)

Related