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

fds write not working FDS_ERR_NOT_INITIALIZED

Hi I am using the custom_bluetooth_service tutorial from https://github.com/bjornspockeli/custom_ble_service_example with SDK v15 and nrf52840 DK.

I am now trying to use fds to write data to flash storage and then access it later to assign the value to characteristics.

.

#include "sdk_common.h"
#include "ble_cus.h"
#include <string.h>
#include "ble_srv_common.h"
#include "nrf_gpio.h"
#include "boards.h"
#include "nrf_log.h"

#include "fds.h"

#define FILE_ID 0x1111
#define REC_KEY 0x2220

static uint8_t eight = 8
static uint8_t nine = 9

static fds_record_t const m_dummy_record =
{
    .file_id = FILE_ID,
    .key = REC_KEY,
    .data.p_data = &eight,
    .data.length_words = (sizeof(eight) + 3) / sizeof(uint32_t),
};

...

static uint32_t custom_value_char_add(ble_cus_t * p_cus, const ble_cus_init_t * p_cus_init)
{
    uint32_t            err_code;
    ble_gatts_char_md_t char_md;
    ble_gatts_attr_md_t cccd_md;
    ble_gatts_attr_t    attr_char_value;
    ble_uuid_t          ble_uuid;
    ble_gatts_attr_md_t attr_md;
    
    ret_code_t ret = fds_init();
    fds_flash_record_t config = {0};
    fds_record_desc_t desc = {0};
    
    ret_code_t rc_write = fds_record_write(&desc, &m_dummy_record);
    
    if (rc_write != FDS_SUCCESS){
        return rc_write;
    }
    
    
    

    // Add Custom Value characteristic
    memset(&cccd_md, 0, sizeof(cccd_md));

    //  Read  operation on cccd should be possible without authentication.
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
    BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);
    
    cccd_md.write_perm = p_cus_init->custom_value_char_attr_md.cccd_write_perm;
    cccd_md.vloc       = BLE_GATTS_VLOC_STACK;

    memset(&char_md, 0, sizeof(char_md));

    char_md.char_props.read   = 1;
    char_md.char_props.write  = 1;
    char_md.char_props.notify = 1; 
    char_md.p_char_user_desc  = NULL;
    char_md.p_char_pf         = NULL;
    char_md.p_user_desc_md    = NULL;
    char_md.p_cccd_md         = &cccd_md; 
    char_md.p_sccd_md         = NULL;
		
    ble_uuid.type = p_cus->uuid_type;
    ble_uuid.uuid = CUSTOM_VALUE_CHAR_UUID;

    memset(&attr_md, 0, sizeof(attr_md));

    attr_md.read_perm  = p_cus_init->custom_value_char_attr_md.read_perm;
    attr_md.write_perm = p_cus_init->custom_value_char_attr_md.write_perm;
    attr_md.vloc       = BLE_GATTS_VLOC_STACK;
    attr_md.rd_auth    = 0;
    attr_md.wr_auth    = 0;
    attr_md.vlen       = 0;

    memset(&attr_char_value, 0, sizeof(attr_char_value));

    attr_char_value.p_uuid    = &ble_uuid;
    attr_char_value.p_attr_md = &attr_md;
    attr_char_value.init_len  = sizeof(uint8_t);
    attr_char_value.init_offs = 0;
    attr_char_value.max_len   = sizeof(uint8_t);

    err_code = sd_ble_gatts_characteristic_add(p_cus->service_handle, &char_md,
                                               &attr_char_value,
                                               &p_cus->custom_value_handles);
    if (err_code != NRF_SUCCESS)
    {
        return err_code;
    }

    return NRF_SUCCESS;
}

But when I try to write a fds record, I have an error "FDS_ERR_NOT_INITIALIZED" which means the module is not initialized. I check other Nordic Devzone post and one post (https://devzone.nordicsemi.com/f/nordic-q-a/23742/fds_err_not_initialized) said the error is due to fs_sys_event_handler() being missing from sys_evt_dispatch(). That post referenced SDK v11 and I am using SDK v15. In the tutorial example code I am using I also cannot find the sys_evt_dispatch() call in main.c.  

The tutorial example code uses peer manager which also uses fds. The peer manager code registers an event handler and initializes the fds module. I am putting my custom code in ble_cus.c and I also do my own initialization of the fds module there. I checked in sdk_config.h and the max users for the fds module is set to 4 so I think I can use the fds_module in ble_cus.c.

I have tried calling peer_manager_init() before service_init() because the peer manage code needs to register the fds event handler before the fds module can be initialized. However, that did not fix the error. I have also added fds event handler code and registered it before I call fds_init() in ble_cus.c, but that also did not change the error.

Interestingly, when I checked the ret_code for my call in fds_init() in ble_cus.c the ret_code was FDS_SUCCESS. I get the same success code from the call that registered the fds event handler.So if my call to fds_init() was successful I am not sure why I am getting the error that the fds module was not successfully initialized. 

How should I fix this? Thank you!

 

 

Parents Reply Children
No Data
Related