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

Not Saving in Flash Memory

Hi,
I am saving data in flash memory but when I read after restarting the device it shows FF. 
How can I read from flash after restarting the device? and how can I check that there is already data?
Thanks 

Parents
  • My initial guess is that you are not waiting for the read finished callback before you print what you have read. Can you share some code showing how you read the data? What are you using to read? Fstorage? FDS? I suggest you check out the examples flash_fstorage or flash_fds, depending on which one you use.

    Best regards,

    Edvin

  • Hi
    I have written these 2 functions to read and write data in flash memory.

    void write_in_flash(uint8_t place)
    {
        ret_code_t rc;
        place=place*16;
    
        sd_flash_page_erase(0x47);
    
        rc = nrf_fstorage_write(&fstorage, flash_start_address+place, save_data, sizeof(save_data), NULL);
        APP_ERROR_CHECK(rc);
    
        wait_for_flash_ready(&fstorage);
        NRF_LOG_INFO("Done.");
    }
    void read_in_flash(uint8_t place)
    {
        ret_code_t rc;
        place=place*16;
    
        rc = nrf_fstorage_read(&fstorage, flash_start_address+place, read_data, sizeof(save_data));
        APP_ERROR_CHECK(rc);
       
        NRF_LOG_INFO("Reading from flash hex:");
        for(uint8_t i=0; i<sizeof(read_data); i++)
        {
            NRF_LOG_RAW_INFO("%02x:", read_data[i]);
        }
        NRF_LOG_RAW_INFO("\r\n");
        NRF_LOG_INFO("done");
    }


    When I connect the device and write any data in flash device didn't respond and also not show logs on the output terminal.

    When I write on characteristics I called the following function.

    void Charcteristics_10(uint8_t test)
    {
        NRF_LOG_INFO("Function Called");
    
        save_data[0]=0x55;
        save_data[1]=0x55;
        write_in_flash(2);
        read_in_flash(2);
    
        if(test==10)
        {
          save_data[0]=0x55;
          save_data[1]=0x55;
          write_in_flash(2);
          read_in_flash(2);
        }
        if(test==20)
        {
         read_in_flash(2);
       }
    
    }
    

    Does any suggestion please?

  • So what do you observe? Have you tried to add a "wait_for_flash_ready(&fstorage);" in read_in_flash as well? What address do you use (flash_start_address)?

    Wha do you write, and what do you read? Did you try to use "nrfjprog --memrd <addr>" to check what the flash actually reads after you have written to it?

  • So what do you observe? Have you tried to add a "wait_for_flash_ready(&fstorage);" in read_in_flash as well?

    After connecting the device read/write didn't give any response.

    What address do you use (flash_start_address)?

    const uint32_t flash_start_address=0x47000;
    const uint32_t flash_end_address=0x4ffff;
    
    static void fstorage_evt_handler(nrf_fstorage_evt_t * p_evt);
    
    
    NRF_FSTORAGE_DEF(nrf_fstorage_t fstorage) =
    {
        /* Set a handler for fstorage events. */
        .evt_handler = fstorage_evt_handler,
    
        /* These below are the boundaries of the flash space assigned to this instance of fstorage.
         * You must set these manually, even at runtime, before nrf_fstorage_init() is called.
         * The function nrf5_flash_end_addr_get() can be used to retrieve the last address on the
         * last page of flash available to write data. */
        .start_addr = flash_start_address,
        .end_addr   = flash_end_address,
    };
    


    Wha do you write, and what do you read?

    I am trying to write this.

        save_data[0]=0x55;
        save_data[1]=0x55;
        write_in_flash(2);
        read_in_flash(2);
    

  • What I meant was what does NRF_LOG_RAW_INFO("%02x:", read_data[i]); print?

    Did you try the nrfjprog --memrd 0x<the address you are writing to>.

    Note that you change the address inside write_in_flash(). plac2 = 2*16 = 32, flash_start_address = 0x47000, actual address = 0x47032. 

    nrfjprog --memrd 0x047032 0x047020

    what does that print?

  • Hi
    Here is the output. When I call the same function before connection it saves and read data from memory BUT when I call the same function after connecting the device it not work.

Reply Children
  • How many bytes are you writing? And what values do you write? Remember that I don't know anything apart from what you have written here. How am I supposed to know what your log is supposed to write?

    According to your snippets so far, the save_data[] array is two bytes long (but it doesn't say). And the values are {0x55, 0x55}.

    This is what you read, but the save_data[] array looks like it is 8 bytes long.

     

    Muqarrab said:
    BUT when I call the same function after connecting the device it not work.

     So what do you read then? It seems you forgot to mention that in the log? Or do you not call the function after you have connected?

  • Hi ,

    1-These are two functions to save/read data from flash. These are okay?

    void write_in_flash(uint8_t place)
    {
        ret_code_t rc;
        place=place*16;
    
        sd_flash_page_erase(0x47);
    
        rc = nrf_fstorage_write(&fstorage, flash_start_address+place, save_data, sizeof(save_data), NULL);
        APP_ERROR_CHECK(rc);
    
        wait_for_flash_ready(&fstorage);
        NRF_LOG_INFO("Done.");
    }
    void read_in_flash(uint8_t place)
    {
        ret_code_t rc;
        place=place*16;
    
        rc = nrf_fstorage_read(&fstorage, flash_start_address+place, read_data, sizeof(save_data));
        APP_ERROR_CHECK(rc);
    
        wait_for_flash_ready(&fstorage);
       
        NRF_LOG_INFO("Reading from flash hex:");
        for(uint8_t i=0; i<sizeof(read_data); i++)
        {
            NRF_LOG_RAW_INFO("%02x:", read_data[i]);
        }
        NRF_LOG_RAW_INFO("\r\n");
        NRF_LOG_INFO("done");
    }
    

    2-When I called these functions without connection it works.

    void Charcteristics_10(uint8_t test)
    {
        NRF_LOG_INFO("Function Called");
    
        save_data[0]=0x55;
        save_data[1]=0x55;
        write_in_flash(2);
        read_in_flash(2);
    
    
    }
    






    3-When I called the same function after connecting the device I get no response also device does not connect. (Stuck in connecting) 

            case BLE_GAP_EVT_CONNECTED:
                NRF_LOG_INFO("Connected.");
                err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
                APP_ERROR_CHECK(err_code);
                m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
                err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle);
                APP_ERROR_CHECK(err_code);
    
               connection_state=1;
               NRF_LOG_INFO("Connected LED Set");
    
               nrf_gpio_pin_set(State_LED);
    
               Charcteristics_10(5);
    
    
                break;
    



  • Muqarrab said:
    3-When I called the same function after connecting the device I get no response also device does not connect. (Stuck in connecting) 

     From where are you calling the flash read and write functions? I don't think you included that in your snippets.

    What is your connection interval? What are you connected to? A phone or another nRF? What is your role? Central or Peripheral?

    Perhaps you can upload the entire project, so that I don't have to ask for all these things, in case there is some information missing? At least all the relevant functions.

  • Hi





    A phone or another nRF?

    With a mobile phone.

    What is your role?

    Peripheral.

    I have attached the project kindly check. These are the functions I have written for read/write data in flash memory. 

    void write_in_flash(uint8_t place)
    void read_in_flash(uint8_t place)


    void Charcteristics_10(uint8_t test) //
    In this function I called above functions.





    Flash(PR.Y.MK.5.0.1.X).zip

  • Edvin said:
     From where are you calling the flash read and write functions? I don't think you included that in your snippets.

     Ok. So I asked where you called this from. I meant when it is not working, and the answer is: "From the BLE_GAP_EVT_CONNECTED" event.

    You can't do that. That is, you can, but you can't use the wait_for_flash_ready() from this interrupt. The reason for this is that this will wait for an event in the fstorage module, but this interrupt priority is the same as the interrupt you are currently in. Therefore the interrupt it is waiting for will not be able to be processed before you release the ble_evt_handler() interrupt, and you are in what we call a deadlock. (both threads are waiting for the other to finish).

    For easier debugging using the log, you can set #define NRF_LOG_DEFERRED 0 in sdk_config.h. 

    So you need to move the flash write function (or at least the wait_for_flash_ready()) to outside the interrupt.

    Best regards,

    Edvin

Related