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

Problem with fds: Flash read, write, delete calling that function using android phone.

Hello to all,

I am using nRF52832 DK with sd132, v6.0.0 and sdk 15.0.0, segger embedded studio. We want to achieve change application timer time (APP_TIMER_TICKS(update_time)) using android phone. We have a sensor devices it will wake up as per app timer time defined, we want to change this wake up time using Noridc UART tool box app. For that we are using fds example as referred in given sdk 15.0 and other nRF5-flash-storage-examples .

We are able to flash write read and delete but problem is:

  1.  When i call fds_test_find_and_delete(), fds_test_write(), fds_read() whenever received command from nordic UART app it not able to read and write at this time.
  2. I want to change update_time variable of app timer ticks using android phone and store this time in flash. Whenever want to user need to change this time first delete previous and update latest send time. 
  3. Will you please provide me program snippet for How i call flash fds API through android phone and store received time in that app_timer ticks. Confused regarding this.

I am currently using this following functions for FDS read, write, and delete:

static ret_code_t fds_test_write(void)
{
		
    //static uint32_t const m_deadbeef[2] = {0xDEADBEEF,0xBAADF00D};
    static uint8_t const m_deadbeef[1] = {0x14};
//    memcpy(m_deadbeef, write_data, sizeof(m_deadbeef));
    fds_record_t        record;
    fds_record_desc_t   record_desc;

    // Set up data.
    
    // Set up record.
    record.file_id        = FILE_ID_FDS_TEST;
    record.key            = REC_KEY_FDS_TEST;
    record.data.p_data    = &m_deadbeef;
    //record.data.length_words   = sizeof(m_deadbeef)/sizeof(uint32_t);
    record.data.length_words   = sizeof(m_deadbeef)/sizeof(uint8_t);
                    
    ret_code_t ret = fds_record_write(&record_desc, &record);
    if (ret != FDS_SUCCESS)
    {
                    return ret;
    }
     NRF_LOG_INFO("Writing Record ID = %d \r\n",record_desc.record_id);
    return NRF_SUCCESS;
}

static ret_code_t fds_read(void)
{

    fds_flash_record_t  flash_record;
    fds_record_desc_t   record_desc;
    fds_find_token_t    ftok ={0};//Important, make sure you zero init the ftok token
    //uint32_t *data;
    uint8_t *data;
    uint32_t err_code;
    
    NRF_LOG_INFO("Start searching... \r\n");
    // Loop until all records with the given key and file ID have been found.
    while (fds_record_find(FILE_ID_FDS_TEST, REC_KEY_FDS_TEST, &record_desc, &ftok) == FDS_SUCCESS)
    {
                    err_code = fds_record_open(&record_desc, &flash_record);
                    if ( err_code != FDS_SUCCESS)
                    {
                            return err_code;		
                    }
                    
                    NRF_LOG_INFO("Found Record ID = %d\r\n",record_desc.record_id);
                    NRF_LOG_INFO("Data = ");
                    //data = (uint32_t *) flash_record.p_data;
                    data = (uint8_t *) flash_record.p_data;
                    for (uint8_t i=0;i<flash_record.p_header->length_words;i++)
                    {
                            NRF_LOG_INFO("0x%8x ",data[i]);
                    }
                    NRF_LOG_INFO("\r\n");
                    // Access the record through the flash_record structure.
                    // Close the record when done.
                    err_code = fds_record_close(&record_desc);
                    if (err_code != FDS_SUCCESS)
                    {
                            return err_code;	
                    }
    }
    return NRF_SUCCESS;
		
}


static ret_code_t fds_test_find_and_delete (void)
{
    fds_record_desc_t   record_desc;
    fds_find_token_t    ftok;

    ftok.page=0;
    ftok.p_addr=NULL;
    // Loop and find records with same ID and rec key and mark them as deleted. 
    while (fds_record_find(FILE_ID_FDS_TEST, REC_KEY_FDS_TEST, &record_desc, &ftok) == FDS_SUCCESS)
    {
        fds_record_delete(&record_desc);
        NRF_LOG_INFO("Deleted record ID: %d \r\n",record_desc.record_id);
    }
    // call the garbage collector to empty them, don't need to do this all the time, this is just for demonstration
    ret_code_t ret = fds_gc();
    if (ret != FDS_SUCCESS)
    {
        return ret;
    }
    return NRF_SUCCESS;
}

  • While using release config. I defined DEBUG in preprocessor, it worked now able successfully flash data. But problem getting is When i flash first time it successfully flash and also read properly. But when i flash other data on other FileID and rec key got successful flash event gen But when i turn OFF dev board and again check second time flash data get empty record first flash data read properly.

    I am more struggling to work properly for flash read write and update data?

    Here is my update read and write program snippet:

    void fds_read(uint16_t file_ID, uint16_t rec_key)
    {
        ret_code_t err_code = FDS_SUCCESS;
        fds_flash_record_t  flash_record;
        fds_record_desc_t   record_desc;
        fds_find_token_t    ftok;
        memset(&ftok, 0x00, sizeof(fds_find_token_t));
        memset(fdsRead_buff, 0, sizeof(fdsRead_buff));
        
        while (fds_record_find(file_ID, rec_key, &record_desc, &ftok) == FDS_SUCCESS)
        {
            err_code = fds_record_open(&record_desc, &flash_record);
            if (err_code != FDS_SUCCESS)
            {
                // Handle error
                NRF_LOG_INFO("fds_record_open error %d\r\n", err_code);
            }
            uint8_t *data = (uint8_t *) (flash_record.p_data);
            NRF_LOG_INFO("record found:%s\r\n", (uint32_t *)data);
            for (int i=0;i<flash_record.p_header->length_words;i++)
            {
                fdsRead_buff[i] = data[i];
            }
    //        NRF_LOG_INFO("fdsRead_buff:%s\r\n", (uint32_t *)fdsRead_buff);
            // Access the record through the flash_record structure
            // Close the record when done.
            err_code = fds_record_close(&record_desc);
            if(err_code != FDS_SUCCESS)
            {
                // Handle error
                NRF_LOG_INFO("fds_record_close err_code %d\r\n", err_code);
            }       
        }
    }
    
    static ret_code_t fds_write(uint16_t file_ID, uint16_t rec_key)  //char *flashData[]
    {
    		
        NRF_LOG_INFO("In flashing: %s",(uint32_t)fdsWrite_buff);
    
        fds_record_t        record;
        fds_record_desc_t   record_desc;
        fds_find_token_t    ftok;
        ftok.page=0;
        ftok.p_addr=NULL;    
        // Set up record.
        record.file_id        = file_ID;
        record.key            = rec_key;
        record.data.p_data    = &fdsWrite_buff;
        //record.data.length_words   = sizeof(m_deadbeef)/sizeof(uint32_t);
        record.data.length_words   = (sizeof(fdsWrite_buff) + 3) / 4; //sizeof(m_deadbeef)/sizeof(uint8_t);
                        
        while (fds_record_find(file_ID, rec_key, &record_desc, &ftok) == FDS_SUCCESS)
        {
            fds_record_delete(&record_desc);
            NRF_LOG_INFO("Deleted record ID: %d \r\n",record_desc.record_id);
        }
        ret_code_t ret = fds_record_write(&record_desc, &record);
        if (ret != FDS_SUCCESS)
        {
           return ret;
        }
    //     NRF_LOG_INFO("Writing Record ID = %d \r\n",record_desc.record_id);
        return NRF_SUCCESS;
    }
    
    

    For first data file ID= 0x0001 & rec_key= 0x1111

    For second data file ID= 0x0002 & rec_key= 0x2222

    Will you please check my code for where should i am getting wrong What is the other best way or program function for flash read, write, update data.

    Thanks...!!!!!

  • Hello,

    Can you describe what you see?

    Do you get any "fds_record_open error..." in your log?

    Do you get any "Delete record ID:..."?

    I see that you may not actually delete all your records, even though you try. You don't wait for the delete event to finish.

    What does your fds_evt_handler look like?

    I could send you a project, but it would look like the SDK\examples\peripheral\flash_fds example. You can look at this.

    Alternatively, upload your entire project, and I can take a look.

    BR,

    Edvin

  • I am trying to save WiFi configuration in flash for that i am doing Send specific cmd from nordic uart app with included SSID name & password. It successfully stored in flash here is bellow log:

    But now problem getting when reading this stored config in flash getting empty buffer each time:

    As like log bellow:

    The delete record ID is getting correct and increase each write event occur.

    But why getting empty when turn OFF board and call read command.

    I am calling simple fds write and read function once specific command received from nordic uart app.

    as like bellow:

    fds_write(FILE_ID_SSID, REC_KEY_SSID); // when 9 cmd send

    fds_read(FILE_ID_SSID, REC_KEY_SSID); // when 1 cmd send

    my fds_event Handler function is bellow:

    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)
                {
                    // Initialization failed.
                }
                break;
    	case FDS_EVT_WRITE:
                if (p_fds_evt->result == FDS_SUCCESS)
                {
                   m_fds_write_success = true;
                   NRF_LOG_INFO("Flash Write event generated");
                }
                break;
            case FDS_EVT_DEL_RECORD:
                if (p_fds_evt->result == FDS_SUCCESS)
                {
                    NRF_LOG_INFO("Flash data record deleted");
                    m_fds_delete_success = true;
                }
                 break;
            default:
                break;
        }
    }

    Will you please help me whats going wrong in above functions and procedure.

    If you create this case as private then i can share my project to you.

    Thanks...!!!!

  • First, can you try to wait for the FDS_EVT_DEL_RECORD event before you start writing your next record? It doesn't seem like you do this from your snippet in the previous reply.

        while (fds_record_find(file_ID, rec_key, &record_desc, &ftok) == FDS_SUCCESS)
        {
            m_fds_delete_success = false;
            fds_record_delete(&record_desc);
            while(!m_fds_delete_success)
            {
                //wait
            }
            
            NRF_LOG_INFO("Deleted record ID: %d \r\n",record_desc.record_id);
        }

  • I have tried this above method but still getting same problem. One think is in there when i call fds_write(..);

    got successfully flash also getting deleted ID of previous. Before turn off dev board when i send fds_read(...);  getting correct buffer as flashed not getting empty. But when i switch off and ON board that time getting empty read from flash.

    Still i am finding the solution not working. the flash read write method is very irritated sometime work sometime not. Please can you provide other way for fix this issue.

    Thanks you..!!

Related