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

fds_record_update write the flash in a random way

Hello,

I'm having an issue about the FDS. Indeed, i want to update some structures already saved in flash so i call the following function : fds_record_update. I can see the event FDS_EVT_UPDATE that passes through my handler. I reset my device a few seconds after having updated the flash but sometime my data seems to be not registered as it should be whereas other time all is working fine.

I don't know what i'm missing. I try to save a value just before reseting. To do that, i call system_reset, here is my code :

void system_reset(eRESET_ID reset_id, uint32_t timeout_ms) {
    uint32_t err_code;
    uint8_t _reset_id = (uint8_t)reset_id;

    NRF_LOG_INFO("");
    NRF_LOG_INFO("*** System reset asked in %d ms ***",timeout_ms);
    NRF_LOG_INFO("\r\n");


    // Check reset_id before reseting
    if (reset_id < RESET_ID_NUMBER) {
        ret_code_t ret = update_PERIPH_CFG(ID_RESET_ID,&_reset_id);

        if (timeout_ms == 0) {
            // Reset immediatly
            NVIC_SystemReset();
        }
        else {
            // Reset in the timer callback
            err_code = app_timer_start(reset_timer,APP_TIMER_TICKS(timeout_ms),NULL);
            APP_ERROR_CHECK(err_code);
        }
    }
}

ret_code_t update_PERIPH_CFG(int target_data, void * data)
{
    fds_record_desc_t desc = {0};
    fds_find_token_t  tok  = {0};
    ret_code_t rc;
    int time_out = 0;

    if (fds_record_find(ACTIVATED_CFG_FILE, ACTIVATED_CFG_REC_KEY, &desc, &tok) == FDS_SUCCESS)
    {
        // modication is possible here/
        switch ( target_data )
        {
            case ID_ADHERENCE_SENSOR: //Activated adherence sensor
                periph_cfg.adherence_activated = *((bool *)data);
                NRF_LOG_INFO("save:%d",periph_cfg.adherence_activated);
                break;
            case ID_ALS_MAX44009:  //Activated ALS
                periph_cfg.ALS_MAX44009_activated = *((bool *)data);
                break;
            case ID_CLEAR_STATE:
                //periph_cfg.clear_state_of_lens = *((uint8_t *)data);
                memcpy(&periph_cfg.clear_state_of_lens,data,sizeof(periph_cfg.clear_state_of_lens));
                break;
            case ID_RST_NUMB_WTH_BT:
                //periph_cfg.ResetNumberWithButton = *((uint8_t *)data);
                memcpy(&periph_cfg.ResetNumberWithButton,data,sizeof(periph_cfg.ResetNumberWithButton));
                break;
            case ID_RESET_ID:
                //periph_cfg.reset_id = *((uint8_t *)data);
                memcpy(&periph_cfg.reset_id,data,sizeof(periph_cfg.reset_id));
                break;
            default:
                break;
        }

        // Write the updated record to flash. 
        do
        {
            rc = fds_record_update(&desc, &m_periph_cfg);
            time_out++;
        } while ((rc != FDS_SUCCESS) && (time_out < 10));

        //APP_ERROR_CHECK(rc);
        fds_gc();
        return rc;
    }
    else
    {
        return FDS_ERR_NOT_FOUND;
    }
}

At startup i try to get the same structure and sometime the value has not been saved as it should be ...

If you have any idea feel free to share.

Best regards,

Aurélien

Related