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

NRF52832 BLE scanning event not triggered after some time(randomly like 1hour, 3 hour)

Thanks in advance.

Hi guys am working with nrf5282 (softdevice s132 V1.6.0 and SDK version 15.2.0), also enabled freertos, and also make softdevice changes rleated freertos,

My functioanlity is nrf52832 put as a BLE central mode need to scan the nearest beacon devices. for led indication There are another two task running parallel with the above task(nrf softdevice task).

my problem is I could get the scan data continuously. for testing i using one beacons it send advertising packets every 100ms interval. The firmware is running fine able to get the ble scanning event(BLE_GAP_EVT_ADV_REPORT) some time(nearly 3hrs). After that i could not get the ble scan event. mean time other 2 tasks are working fine. But the softdevice task is some where stucked. the nrf_sdh_evts_poll function not called continuously.

I also tried to disable the softdevice only but the device is reset when disable the softdevice. even i check the all return value of softdevice API like nrf_sdh_is_enabled(), nrf_sdh_is_suspended(). All are fine it give positive status only.

I'm stucked with this and i have been looking this issue for past one week.

In this i also attached my code.

//softdevice task creation

void nrf_sdh_freertos_init(nrf_sdh_freertos_task_hook_t hook_fn, void * p_context)
{
	NRF_LOG_DEBUG("Creating a SoftDevice task.");

	m_task_hook = hook_fn;

	BaseType_t xReturned = xTaskCreate(softdevice_task,
										"BLE",
										NRF_BLE_FREERTOS_SDH_TASK_STACK,
										p_context,
										2,
										&m_softdevice_task);
	
	if (xReturned != pdPASS)
	{
		NRF_LOG_ERROR("SoftDevice task not created.");
		APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
	}
}



/* This function gets events from the SoftDevice and processes them. */
static void softdevice_task(void * pvParameter)
{
    NRF_LOG_DEBUG("Enter softdevice_task.");

    if (m_task_hook != NULL)
    {
        m_task_hook(pvParameter);
    }

    while (true)
    {
        nrf_sdh_evts_poll(); // Let the handlers run first, in case the EVENT occured 
        vTaskSuspend(NULL);
    }
}



// init the ble stack
void ble_m_init(void)
{
    // Initialization of required BLE components.
    ble_stack_init();
    scan_init();
    gap_params_init();
    gatt_init();
    conn_params_init();
}



/**@brief Function for initialization Scanning Module.
 */
static void scan_init(void)
{
    ret_code_t err_code;
    err_code = nrf_ble_scan_init(&m_scan, NULL, NULL);
    APP_ERROR_CHECK(err_code);
}



/**@brief Function for initiating scanning.
 */
void scan_start(void)
{
    ret_code_t err_code;

    //Clear the current device address.
    memset(m_addr_str_for_connection, 0, sizeof(m_addr_str_for_connection));

   // connect_addr_clear();

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

    //bsp_board_led_on(CENTRAL_SCANNING_LED);

    printf("Scanning\r\n");
    m_scanning = true;
}


/**@brief Function for stopping scanning.
 */
void scan_stop(void)
{
    nrf_ble_scan_stop();
    //bsp_board_led_off(CENTRAL_SCANNING_LED);
    m_scanning = false;
}



//ble event processing

void nrf_ble_scan_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_contex)
{
    nrf_ble_scan_t                 * p_scan_data  = (nrf_ble_scan_t *)p_contex;
    ble_gap_evt_adv_report_t const * p_adv_report = &p_ble_evt->evt.gap_evt.params.adv_report;
   
    ble_gap_evt_t const            * p_gap_evt    = &p_ble_evt->evt.gap_evt;

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_ADV_REPORT:
            nrf_ble_scan_on_adv_report(p_scan_data, p_adv_report);
            break;

        default:
            break;
    }
}

  • This was fixed in SDK15.3, there was a corner case in the below file

    nRF5_SDK_15.2.0_9412b96\components\softdevice\common\nrf_sdh_freertos.c

    Please replace contents of SD_EVT_IRQHandler() and softdevice_task() functions to below

    void SD_EVT_IRQHandler(void)
    {
        BaseType_t yield_req = pdFALSE;
    
        vTaskNotifyGiveFromISR(m_softdevice_task, &yield_req);
    
        /* Switch the task if required. */
        portYIELD_FROM_ISR(yield_req);
    }
    
    
    /* This function gets events from the SoftDevice and processes them. */
    static void softdevice_task(void * pvParameter)
    {
        NRF_LOG_DEBUG("Enter softdevice_task.");
    
        if (m_task_hook != NULL)
        {
            m_task_hook(pvParameter);
        }
    
        while (true)
        {
            nrf_sdh_evts_poll();                    /* let the handlers run first, incase the EVENT occured before creating this task */
    
            (void) ulTaskNotifyTake(pdTRUE,         /* Clear the notification value before exiting (equivalent to the binary semaphore). */
                                    portMAX_DELAY); /* Block indefinitely (INCLUDE_vTaskSuspend has to be enabled).*/
        }
    }

    In SDK15.2, softdevice_task was calling vTaskSuspend unconditionally. which could cause deadlocks. This is fixed in SDK15.3

    Left in the picture above is from SDK15.3 and right is SDK15.2

  • Thanks susheel.

    i will make changes in my code base and update you later

  • hi

    nRF52832 is new for me and i want to start working on this board

    So, can you please guide me, form where i can start writing code for scanning program using FreeRtos.

    Or please share your scanning project code with me, so it will be very helpful for me 

Related