Put the BLE block in sleep to reduce consumption in a nRF52832 using Softdevice

Hello,

In a project using the NRF52832. Softdevice SDK 19.1.0 and Segger IDE, I inizialize the BLE for having NUS and DFU services actives. I try to reproduce the code here.

void ble_init(void)
{
    ret_code_t err_code;


    // Initialize the async SVCI interface to bootloader before any interrupts are enabled.
    err_code = ble_dfu_buttonless_async_svci_init();    
    APP_ERROR_CHECK(err_code);  
 
    ble_stack_init();   
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();
    peer_manager_init();  
    advertising_start();

    ble_gap_addr_t my_addr;
    err_code = sd_ble_gap_addr_get(&my_addr);
    APP_ERROR_CHECK(err_code);
    NRF_LOG_INFO("addr %02x:%02x:%02x:%02x:%02x:%02x", my_addr.addr[0],
                                                      my_addr.addr[1],
                                                      my_addr.addr[2],
                                                      my_addr.addr[3],
                                                      my_addr.addr[4],
                                                      my_addr.addr[5]);
}

static void advertising_init(void)
{
    uint32_t               err_code;
    ble_advertising_init_t init;

    memset(&init, 0, sizeof(init));

    ble_advdata_manuf_data_t manuf_data; //Variable to hold manufacturer specific data
    
    uint8_t data[12];
    sprintf(&data[0],"%04X%08X", (NRF_FICR->DEVICEADDR1)&0x0000FFFF, NRF_FICR->DEVICEADDR0); // get MAC address
    
    //sprintf(&data[0],"%12", NRF_FICR->DEVICEADDR1); // get MAC address
    manuf_data.company_identifier             =  0x0059; //Nordics company ID
    manuf_data.data.p_data                    = data;
    manuf_data.data.size                      = sizeof(data);
    init.advdata.p_manuf_specific_data = &manuf_data;
    init.advdata.short_name_len = 6; // Advertise only first 6 letters of name.
    //Fi de Prova 20221105

    init.advdata.name_type          = BLE_ADVDATA_FULL_NAME;
    init.advdata.include_appearance = false;
    init.advdata.flags              = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;

    init.srdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
    init.srdata.uuids_complete.p_uuids  = m_adv_uuids;

    init.config.ble_adv_fast_enabled  = true;
    init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
    init.config.ble_adv_fast_timeout  = APP_ADV_DURATION;
    init.evt_handler = on_adv_evt;
    

    err_code = ble_advertising_init(&m_advertising, &init);
    APP_ERROR_CHECK(err_code);

    ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
}

Using this code I can see the device advertised in the nRFConnect application for Smartphone.

One of the functions of this application is put BLE in sleep to reduce the consumption when some conditions are met. I have to highlight that I need to put only BLE in sleep, but the rest of operations of the device must be maintained in active mode. To do this I use the next code. However, I cannot see a reduction in the consumtpion.

void ble_stop (void)  
{
    uint32_t err_code= 0;
    
    // Disconnect any active connections
    for (uint16_t conn_handle = 0; conn_handle < BLE_CONN_STATE_MAX_CONNECTIONS; conn_handle++)
    {
        if (ble_conn_state_status(conn_handle) == BLE_CONN_STATUS_CONNECTED)
        {
            err_code = sd_ble_gap_disconnect(conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            APP_ERROR_CHECK(err_code);
        }
    }
    
    // Stop advertising
    //err_code = sd_ble_gap_adv_stop(&m_advertising);

    //APP_ERROR_CHECK(err_code);
    
}

I have had to comment the line "err_code = sd_ble_gap_adv_stop(&m_advertising);" because it produces an uncontrolled break in the program (it returns error_code= 0x00003004.

What other way to reduce the consumtpion by putting BLE block to sleep or standby could I use?

Thank you very much in advance.


Regards,

Joel

Parents Reply
  • Hi Joel,

    I have modified the code using

    err_code = sd_ble_gap_adv_stop(m_advertising.adv_handle);

    and it looked as if this has worked well.

    Note that the function will return 'invalid state' if the stack is not advertising at that point. You may want to ignore that error.

    Anyway, is the process mentioned the right one to disable BLE and let the Softdevice to reduce the consumption of the nRF52832?

    Yes, you the stack will not draw any additional current if you stop any ongoing BLE activities such as active connections, advertising, or scanning.

Children
Related