Soft Device Scan Parms not being set correctly from struct

I set my scan params in the following init code.

 

/**@brief Function for initializing the scanning and setting the filters.
 */
static void scan_init(void)
{   
    ret_code_t          err_code;
    nrf_ble_scan_init_t init_scan;
    ble_gap_scan_params_t const m_scan_param =
    {
      .active        = 0x01,
      .interval      = NRF_BLE_SCAN_SCAN_INTERVAL,
      .window        = NRF_BLE_SCAN_SCAN_WINDOW,
      .filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL, //BLE_GAP_SCAN_FP_WHITELIST,
      .timeout       = 0,
      .scan_phys     = BLE_GAP_PHY_1MBPS,
    };

    memset(&init_scan, 0, sizeof(init_scan));
    /**< Scan parameters requested for scanning and connection. */
    init_scan.p_scan_param     = &m_scan_param;
    init_scan.connect_if_match = false;
    init_scan.conn_cfg_tag     = APP_BLE_CONN_CFG_TAG;

    err_code = nrf_ble_scan_init(&m_scan, &init_scan, scan_evt_handler);
    APP_ERROR_CHECK(err_code);

}

The interval and scan window are both set to 100. However, when I watch the variables in debug mode, the variables are as follows. 

Any idea how they are being set so wrong? No other references to these variables in the code anywhere except here and when I start scanning following the advertising period ending (60 seconds).

Parents
  • Hi,

    Any idea how they are being set so wrong? No other references to these variables in the code anywhere except here and when I start scanning following the advertising period ending (60 seconds).

    You should use a static variable for the ble_gap_scan_params_t structure used for the p_scan_param pointer in the p_init argument to nrf_ble_scan_init().

    In your implementation, the variable is not static, so when your function scan_init() returns the pointer points to a location in stack memory that will later be overwritten by the local variables of other functions.

    For an example of working initialization with custom scan parameters, see the Advanced initialization section of the Scanning Module documentation.

    Regards,
    Terje

  • I changed it to a static and still no luck. I copied over the strict from the working example. 

  • Hi,

    I am starting to think this may be a memory alignment issue. Can you try replacing the line with the following?

    __ALIGN(4) ble_gap_scan_params_t const m_scan_param =

    That is, prefix the line with the __ALIGN(4) macro. Possibly also for the nrf_ble_scan_init_t init_scan; line.

    It may actually be as simple as the pointer not being word aligned. I don't know why this should matter, but something is definitely going on with addressing and word boundaries here, so it should be well worth experimenting with word aligning the data structures.

    Regards,
    Terje

  •  

    That did not work. I am imaging it has to be with addressing. I have it set as such now.

    __ALIGN(4) static ble_gap_scan_params_t const scan_param =
    {
    .active = 0x01,
    .interval = NRF_BLE_SCAN_SCAN_INTERVAL, //=200
    .window = NRF_BLE_SCAN_SCAN_WINDOW, // =50
    .filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL, //BLE_GAP_SCAN_FP_WHITELIST,
    .timeout = 0,
    .scan_phys = BLE_GAP_PHY_1MBPS, // =1
    };

  • Hi,

    And the pointers are now all aligned to four bytes?

    I am afraid there isn't more time today to look into this, but I will have a closer look at the project tomorrow.

    Regards,
    Terje

  • They are not. Thank you for the current help though! I will continue to work through this today and see if I find any solution  

  •   I made this change to nrf_ble_scan.h line 289 - 306 by adding __attribute__((packed))

    /**@brief Scan module instance. Options for the different scanning modes.
    *
    * @details This structure stores all module settings. It is used to enable or disable scanning modes
    * and to configure filters.
    */
    typedef struct
    {
    #if (NRF_BLE_SCAN_FILTER_ENABLE == 1)
    nrf_ble_scan_filters_t scan_filters; /**< Filter data. */
    #endif
    bool connect_if_match; /**< If set to true, the module automatically connects after a filter match or successful identification of a device from the whitelist. */
    ble_gap_conn_params_t conn_params; /**< Connection parameters. */
    uint8_t conn_cfg_tag; /**< Variable to keep track of what connection settings will be used if a filer match or a whitelist match results in a connection. */
    ble_gap_scan_params_t scan_params; /**< GAP scanning parameters. */
    nrf_ble_scan_evt_handler_t evt_handler; /**< Handler for the scanning events. Can be initialized as NULL if no handling is implemented in the main application. */
    uint8_t scan_buffer_data[NRF_BLE_SCAN_BUFFER]; /**< Buffer where advertising reports will be stored by the SoftDevice. */
    ble_data_t scan_buffer; /**< Structure-stored pointer to the buffer where advertising reports will be stored by the SoftDevice. */
    } __attribute__((__packed__)) nrf_ble_scan_t;

    This allowed the parameters to be set correctly. Now when I call  my scan start function

    err_code = nrf_ble_scan_start(&my_scanner);
    APP_ERROR_CHECK(err_code); 

    The program stops running at an unknown function at 0x00004AAA (which I assume lies somewhere in the Soft Device binary, hence why it is unknown). 

Reply
  •   I made this change to nrf_ble_scan.h line 289 - 306 by adding __attribute__((packed))

    /**@brief Scan module instance. Options for the different scanning modes.
    *
    * @details This structure stores all module settings. It is used to enable or disable scanning modes
    * and to configure filters.
    */
    typedef struct
    {
    #if (NRF_BLE_SCAN_FILTER_ENABLE == 1)
    nrf_ble_scan_filters_t scan_filters; /**< Filter data. */
    #endif
    bool connect_if_match; /**< If set to true, the module automatically connects after a filter match or successful identification of a device from the whitelist. */
    ble_gap_conn_params_t conn_params; /**< Connection parameters. */
    uint8_t conn_cfg_tag; /**< Variable to keep track of what connection settings will be used if a filer match or a whitelist match results in a connection. */
    ble_gap_scan_params_t scan_params; /**< GAP scanning parameters. */
    nrf_ble_scan_evt_handler_t evt_handler; /**< Handler for the scanning events. Can be initialized as NULL if no handling is implemented in the main application. */
    uint8_t scan_buffer_data[NRF_BLE_SCAN_BUFFER]; /**< Buffer where advertising reports will be stored by the SoftDevice. */
    ble_data_t scan_buffer; /**< Structure-stored pointer to the buffer where advertising reports will be stored by the SoftDevice. */
    } __attribute__((__packed__)) nrf_ble_scan_t;

    This allowed the parameters to be set correctly. Now when I call  my scan start function

    err_code = nrf_ble_scan_start(&my_scanner);
    APP_ERROR_CHECK(err_code); 

    The program stops running at an unknown function at 0x00004AAA (which I assume lies somewhere in the Soft Device binary, hence why it is unknown). 

Children
Related