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

Issue writing in flash using fds

SDK 15, nRF52840, PCA10056

When I read my data in the flash I recover the before last value writen and not the last.

I'm using the es_flash.c source code from eddyston example which use fds.c to write in the flash.

Here is my code to write or read :

__ALIGN(4) static uint8_t nb_charge_buf[sizeof(uint16_t)];

ret_code_t flash_access_nb_charge(uint16_t * p_nb_charge,
                                  es_flash_access_t access_type)
{
      flash_access_params_t params = {.record_key  = RECORD_KEY_NB_CHARGE,
                                      .file_id     = FILE_ID_ES_FLASH,
                                      .p_data_buf  = nb_charge_buf,
                                      .p_data      = (uint8_t *)p_nb_charge,
                                      .size_bytes  = sizeof(uint16_t),
                                      .access_type = access_type};
   
      return access_flash_data(&params);
}

and here is the access_flash_data function :

static ret_code_t access_flash_data(const flash_access_params_t * p_params)
{
    ret_code_t         err_code;
    fds_flash_record_t record = {0};
    fds_record_desc_t  desc   = {0};
    fds_find_token_t   ft     = {0};
    fds_record_t       record_to_write =
    {
        .data.p_data = p_params->p_data_buf,
        .file_id     = p_params->file_id
    };

    err_code = fds_record_find_by_key(p_params->record_key, &desc, &ft);

    // If its a read or clear, we can not accept errors on lookup
    if (p_params->access_type == ES_FLASH_ACCESS_READ)
    {
        RETURN_IF_ERROR(err_code);
    }

    if (p_params->access_type == ES_FLASH_ACCESS_CLEAR && err_code == FDS_ERR_NOT_FOUND)
    {
        return NRF_SUCCESS;
    }

    switch (p_params->access_type)
    {
        case ES_FLASH_ACCESS_READ:
            err_code = fds_record_open(&desc, &record);
            RETURN_IF_ERROR(err_code);

            memcpy(p_params->p_data, record.p_data, p_params->size_bytes);

            err_code = fds_record_close(&desc);
            RETURN_IF_ERROR(err_code);

            break;

        case ES_FLASH_ACCESS_WRITE:
            memcpy(p_params->p_data_buf, p_params->p_data, p_params->size_bytes);

            record_to_write.data.length_words = (p_params->size_bytes +3) / 4;
            record_to_write.key               = p_params->record_key;

            if (err_code == FDS_ERR_NOT_FOUND)
            {
                err_code = fds_record_write(&desc, &record_to_write);
            }

            else
            {
                err_code = fds_record_update(&desc, &record_to_write);
            }

            RETURN_IF_ERROR(err_code);
            m_num_pending_ops++;
            break;

        case ES_FLASH_ACCESS_CLEAR:
            err_code = fds_record_delete(&desc);
            RETURN_IF_ERROR(err_code);
            m_num_pending_ops++;
            break;

        default:
            break;
    }
    return NRF_SUCCESS;
}

Related