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 Reply Children
  •   If i set the scan parameters like this     m_scan.scan_params = scan_param; They set correctly...

  • But the scan does not start due to Invalid Params Error being thrown in nrf_ble_scan_start.

     

    When using the &init_scan to set the scan paramters, everything is offset by 2 bytes. The address of m_scan.scan_params starts at 0x20004992, but from that address to 0x20004993 is never written to which is where scan_params.extended, report incomplete, active, filter policy, and scan_phys. The value I have for extended, report incomplete, active and filter policy (which are bits 0-3 of the first byte are 0x04) and scan_phys is 0x01. The value written to interval is 0x0104 which shows these bits getting put in the wrong address. The interval hex value is 0xc8 (Which I have set my define value to 200). This value is being written to the window address. This continues on for the window which is set to the define value for interval and the timeout value is set as the define for the window. What could be causing those first two addresses to not being written to?

    This is also causing garbage data to appear in the first 4 bytes of my scan buffer data array, making the first values to start at scan_buffer_data[4]  vs scan_buffer_data[0]. I hope you can access the project in the private case to maybe find what could be happening?

  •   Also, weird behavior. The pointer to the scan buffer data is constantly changing addresses.....

    When I open up the variable to watch, the watch window crashes

  • 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
    };

Related