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

Can't manage to read from flash

Hi, Following the docs I think I am able to write to flash but it will not read anything .

This is how I set it up, write and read :

 static void sys_evt_dispatch(uint32_t sys_evt)
{
    fs_sys_event_handler(sys_evt);
    ble_advertising_on_sys_evt(sys_evt);
}


static void fds_evt_handler(fds_evt_t const * const p_fds_evt)
{
    switch (p_fds_evt->id)
    {
        case FDS_EVT_INIT:
            if (p_fds_evt->result != FDS_SUCCESS)
            {
                 NRF_LOG_INFO("failed initializing\r\n"); 
            }
            
            break;

       case FDS_EVT_WRITE:

       if (p_fds_evt->result == FDS_SUCCESS)
           {
               write_flag=1;
                NRF_LOG_INFO("finish write\r\n"); 
           }
      break;

        default:
            break;
    }
}



void initStorage()
{

        ret_code_t ret = fds_register(fds_evt_handler);
        if (ret != FDS_SUCCESS)
        {
            NRF_LOG_INFO("FAILED TO INIT STORAGE\r\n"); 
        }
        ret = fds_init();
        if (ret != FDS_SUCCESS)
        {
            NRF_LOG_INFO("ERROR INIT\r\n"); 
        }
        else
        NRF_LOG_INFO("INIT IT \r\n"); 

     


}


  void  write()
{
      #define FILE_ID     0x1111
      #define REC_KEY     0x2222
      static uint32_t const m_deadbeef = 0xDEADBEEF;
      fds_record_t        record;
      fds_record_desc_t   record_desc;
      fds_record_chunk_t  record_chunk;
      // Set up data.
      record_chunk.p_data         = &m_deadbeef;
      record_chunk.length_words   = 1;
      // Set up record.
      record.file_id                  = FILE_ID;
      record.key               = REC_KEY;
      record.data.p_chunks       = &record_chunk;
      record.data.num_chunks   = 1;
    
      ret_code_t ret = fds_record_write(&record_desc, &record);
      if (ret != FDS_SUCCESS)
      {
           NRF_LOG_INFO("FAILED TO SAVE\r\n"); 
      }

          else
 NRF_LOG_INFO("Writing Record ID = %d \r\n",record_desc.record_id);



}


void read ()
{
      NRF_LOG_INFO("START READING\r\n"); 

      #define FILE_ID     0x1111
      #define REC_KEY     0x2222
      fds_flash_record_t  flash_record;
      fds_record_desc_t   record_desc;
      fds_find_token_t    ftok={0};
      // Loop until all records with the given key and file ID have been found.
      while (fds_record_find(FILE_ID, REC_KEY, &record_desc, &ftok) == FDS_SUCCESS)
      {
         NRF_LOG_INFO("FOUND SOMETHING\r\n");
          if (fds_record_open(&record_desc, &flash_record) != FDS_SUCCESS)
          {
             NRF_LOG_INFO("ERROR OPEN\r\n"); 
          }
          // Access the record through the flash_record structure.

          NRF_LOG_INFO("Found Record ID = %d\r\n",record_desc.record_id);
          //NRF_LOG_INFO("Found data = %s\r\n",flash_record.p_data);

           


          // Close the record when done.
          if (fds_record_close(&record_desc) != FDS_SUCCESS)
          {
              NRF_LOG_INFO("ERROR CLOSE\r\n"); 
          }
      }


}

I can see that there are no errors so it means I am able to init and write with :

   initStorage();


     nrf_delay_ms(1000);
     write();
     nrf_delay_ms(1000);
     read ();

You can see on the log that the FDS_EVT_WRITE will not be called but I do get a record write ID ! The log :

:INFO:PROGRAM STARTED
:INFO:INIT IT 
:INFO:Writing Record ID = 1 
:INFO:START READING
Related