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 

  • 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?

Related