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

rtos task can not work when bluetooth connected.

Hi all,

I creat a main task to process events, when the bluetooth connected, this main task can not work, and the bluetooth can still work.

I try to add the main task's priority, but it still unuseful.

below is my code:

the sdk is nRF5_SDK_15.0.0_a53641a,

work on SEGGER Embedded Studio for ARM 4.12,


//create main task in main()
UNUSED_VARIABLE(xTaskCreate(user_common_task, "COMMON_TASK", 256*3, NULL, 2, common_task_handle));

//the task function
static void user_common_task(void* arg)
{

	common_irq_Semaphore = xSemaphoreCreateBinary();
	
	for(;;)
	{
		if(xSemaphoreTake(common_irq_Semaphore, portMAX_DELAY ) != pdFALSE)
		{
			uint8_t ret  = 0;
			switch (key_evt) 
			{
				case BSP_EVENT_KEY_0:
					DspLog(0xDCCC, 1111);
					break;
				case BSP_EVENT_KEY_0_LONG_PRESS:
					DspLog(0xDeee, 2222);
					if(ble_state == BLE_STATE_IDLE || ble_state == BLE_STATE_INIT)
					{
						hal_user_advertising_start(true);
					}
					break;
				default:
					break;
			}
		}
	}
}

//key event handler, send the semaphore to the main task to process the key event.
static void bsp_evt_handler(bsp_event_t evt)
{

	key_evt = evt;
	BaseType_t xHigherPriorityTaskWoken = pdFALSE;
	xSemaphoreGiveFromISR(common_irq_Semaphore, &xHigherPriorityTaskWoken);

}
--------------------------------------------------------------------------------------
//ble init related called in main(),copy from the example(ble_app_hrs_freertos)
{
	ble_state = BLE_STATE_INIT;
	ble_stack_init();
	gap_params_init();
	gatt_init();
	services_init();
	advertising_init();
	conn_params_init();
	peer_manager_init();

	nrf_sdh_freertos_init(NULL, (void *)0);
}

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",
                                       256*3,
                                       (void *)0,
                                       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 before creating this task.
        vTaskSuspend(NULL);
    }
}

/**@brief Function for handling BLE events.
 *
 * @param[in]   p_ble_evt   Bluetooth stack event.
 * @param[in]   p_context   Unused.
 */
static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
    ret_code_t err_code = NRF_SUCCESS;
#if 1
    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_DISCONNECTED:
			ble_state = BLE_STATE_DISCON;
            NRF_LOG_INFO("Disconnected.");
            break;

        case BLE_GAP_EVT_CONNECTED:
			ble_state = BLE_STATE_CON;
			#if 1
            m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
            err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle);
            APP_ERROR_CHECK(err_code);
			#endif
            break;

        case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
        {
            NRF_LOG_DEBUG("PHY update request.");
            ble_gap_phys_t const phys =
            {
                .rx_phys = BLE_GAP_PHY_AUTO,
                .tx_phys = BLE_GAP_PHY_AUTO,
            };
            err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
            APP_ERROR_CHECK(err_code);
        } break;

        case BLE_GATTC_EVT_TIMEOUT:
            // Disconnect on GATT Client timeout event.
            NRF_LOG_DEBUG("GATT Client Timeout.");
            err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
                                             BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GATTS_EVT_TIMEOUT:
            // Disconnect on GATT Server timeout event.
            NRF_LOG_DEBUG("GATT Server Timeout.");
            err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
                                             BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            APP_ERROR_CHECK(err_code);
            break;

        default:
            // No implementation needed.
            break;
    }
#endif
}

Thanks.

Parents Reply Children
  • Hi,

    When you that the user_common_task does not work, does it mean that it does not run? In other words, it does not get the common_irq_Semaphore semaphore from the bsp_evt_handler? Can you debug and check in more detail what happens and explain here? For instance:

    • Is user_common_task() waiting in the call to xSemaphoreTake ()?
    • Does bsp_evt_handler run and call xSemaphoreGiveFromISR?
    • Is the semaphore received in user_common_task(), or does it wait xSemaphoreTake() "forever"?
    • What happens if you set a lower value for the second parameter, xTicksToWait? In your code it will just call xSemaphoreTake() gain, but I want to know if the task ever has time to run. could it be that there is a problem that the SoftDevice task always runs.

    It is difficult to get any overview of your code snippets in the question. For instance, I don't see where bsp_evt_handler is used, and I am having problems understanding your softdevice_task(). This look similar to the implementation in  nrf_sdh_freertos.c, which is used when you call nrf_sdh_freertos_init(), which you have also included. Can you provide more of y our code to make it easier to get an overview? Which SDK version are you using?

Related