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

uart disable problem

Hi,

I have a problem with uart disable function (app_uart_close();). I started from ble_app_uart example, and I want to disable uart when nobody is connected to the Ble device. 
First I removed uart_init(); and printf from main and then added next to the ble_evt_handler function:

case BLE_GAP_EVT_CONNECTED:
            NRF_LOG_INFO("Connected");
            err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
            APP_ERROR_CHECK(err_code);
            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);
            uart_init();
            break;

case BLE_GAP_EVT_DISCONNECTED:
    NRF_LOG_INFO("Disconnected");
    // LED indication will be changed when advertising starts.
    NRF_UART0->TASKS_STOPRX = 1;
    app_uart_close();
    m_conn_handle = BLE_CONN_HANDLE_INVALID;
    break;

These are just two cases of the function, so on connect I enable the uart and on disconnect, I disable the uart. Problem is when I disable the uart power consumption does not drop and the NRF52 is not low power. In the picture is the part of the power consumption diagram when the device is connected and disconnected:


Is there something I am missing when disabling the uart?

Parents
  • Hi again,

    I have looked into this issue today, and I am awaiting your answers to the questions in my previous reply.

    Best regards
    Karl

  • Hi Karl,

    Thank you for your support, I am currently out of the office, and will get back to you with the results early next week. 

    Best regards,
    Vojislav.

  • Hi Vojislav,

    That is no problem at all. I look forward to hearing from you!

    Best regards,
    Karl

  • Hi Karl,

    Sorry for keeping you waiting for my answer. 
    It seems that I had something wrong in my hardware setup. Now I do not have the same problem anymore. 

    Here is the power diagram:

    and here is the main function of the code and ble_event_handler:

    /**@brief Application main function.
     */
    int main(void)
    {
        bool erase_bonds;
    
        // Initialize.
        //uart_init();
        log_init();
        timers_init();
        
        //buttons_leds_init(&erase_bonds);
        power_management_init();
        ble_stack_init();
        gap_params_init();
        gatt_init();
        services_init();
        advertising_init();
        conn_params_init();
    
        // Start execution.
        //printf("\r\nUART started.\r\n");  
        NRF_LOG_INFO("Debug logging for UART over RTT started.");
        advertising_start();
    
        // Enter main loop.
        for (;;)
        {
            idle_state_handle();
        }
    }
    

    /**@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)
    {
        uint32_t err_code;
    
        switch (p_ble_evt->header.evt_id)
        {
            case BLE_GAP_EVT_CONNECTED:
                NRF_LOG_INFO("Connected");
                //err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
                //APP_ERROR_CHECK(err_code);
                uart_init();
                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);
                break;
    
            case BLE_GAP_EVT_DISCONNECTED:
                NRF_LOG_INFO("Disconnected");
                err_code = app_uart_close();
                NRF_LOG_INFO("error: %d", err_code);
                // LED indication will be changed when advertising starts.
                m_conn_handle = BLE_CONN_HANDLE_INVALID;
                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_GAP_EVT_SEC_PARAMS_REQUEST:
                // Pairing not supported
                err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
                APP_ERROR_CHECK(err_code);
                break;
    
            case BLE_GATTS_EVT_SYS_ATTR_MISSING:
                // No system attributes have been stored.
                err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
                APP_ERROR_CHECK(err_code);
                break;
    
            case BLE_GATTC_EVT_TIMEOUT:
                // Disconnect on GATT Client timeout event.
                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.
                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;
        }
    }


    If you have some ideas about what can be better or some cases I did not cover, please let me know>

    I will keep issue open a bit more, while I do some more tests. 

    Best regards,
    Vojislav

Reply
  • Hi Karl,

    Sorry for keeping you waiting for my answer. 
    It seems that I had something wrong in my hardware setup. Now I do not have the same problem anymore. 

    Here is the power diagram:

    and here is the main function of the code and ble_event_handler:

    /**@brief Application main function.
     */
    int main(void)
    {
        bool erase_bonds;
    
        // Initialize.
        //uart_init();
        log_init();
        timers_init();
        
        //buttons_leds_init(&erase_bonds);
        power_management_init();
        ble_stack_init();
        gap_params_init();
        gatt_init();
        services_init();
        advertising_init();
        conn_params_init();
    
        // Start execution.
        //printf("\r\nUART started.\r\n");  
        NRF_LOG_INFO("Debug logging for UART over RTT started.");
        advertising_start();
    
        // Enter main loop.
        for (;;)
        {
            idle_state_handle();
        }
    }
    

    /**@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)
    {
        uint32_t err_code;
    
        switch (p_ble_evt->header.evt_id)
        {
            case BLE_GAP_EVT_CONNECTED:
                NRF_LOG_INFO("Connected");
                //err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
                //APP_ERROR_CHECK(err_code);
                uart_init();
                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);
                break;
    
            case BLE_GAP_EVT_DISCONNECTED:
                NRF_LOG_INFO("Disconnected");
                err_code = app_uart_close();
                NRF_LOG_INFO("error: %d", err_code);
                // LED indication will be changed when advertising starts.
                m_conn_handle = BLE_CONN_HANDLE_INVALID;
                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_GAP_EVT_SEC_PARAMS_REQUEST:
                // Pairing not supported
                err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
                APP_ERROR_CHECK(err_code);
                break;
    
            case BLE_GATTS_EVT_SYS_ATTR_MISSING:
                // No system attributes have been stored.
                err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
                APP_ERROR_CHECK(err_code);
                break;
    
            case BLE_GATTC_EVT_TIMEOUT:
                // Disconnect on GATT Client timeout event.
                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.
                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;
        }
    }


    If you have some ideas about what can be better or some cases I did not cover, please let me know>

    I will keep issue open a bit more, while I do some more tests. 

    Best regards,
    Vojislav

Children
  • Hello Vojislav,

    Vojislav said:
    Sorry for keeping you waiting for my answer.

    No problem at all :)

    Vojislav said:
    It seems that I had something wrong in my hardware setup. Now I do not have the same problem anymore. 

    I am happy to hear that the issue is resolved!
    Do you remember what changes you made to your hardware, to eliminate the extra current draw?

    Vojislav said:
    If you have some ideas about what can be better or some cases I did not cover, please let me know>

    Since I do not know what your application will do, or where/how it will operate, it is hard for me to pinpoint any specific changes to your code.
    It looks to me that you have covered the common events, if that is what you are asking.
    Is there anything specific you would like me to take a closer look at?

    Vojislav said:
    I will keep issue open a bit more, while I do some more tests. 

    Great! I would love to know what caused your initial issue.

    Best regards,
    Karl

Related