1

I am new to embedded systems. I am trying to increase the number of ble peripheral links.

static uint16_t m_connection_handle[NRF_SDH_BLE_PERIPHERAL_LINK_COUNT] = {BLE_CONN_HANDLE_INVALID};
static uint8_t m_num_connections = 0;

static void on_advertising_events(ble_adv_evt_t advertising_event)
{
    NRF_LOG_DEBUG("on_advertising_events - event: %d", advertising_event);

    ret_code_t err_code;

    switch (advertising_event)
    {
        case BLE_ADV_EVT_FAST:
            NRF_LOG_INFO("Fast advertising.");
#ifdef BOARD_PCA10040
            err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
            APP_ERROR_CHECK(err_code);
#endif
            break;

        case BLE_ADV_EVT_IDLE:
            if (m_num_connections < NRF_SDH_BLE_PERIPHERAL_LINK_COUNT)
            {
                err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
                APP_ERROR_CHECK(err_code);
            }
            break;

        default:
            break;
    }
}

static void on_ble_events(ble_evt_t const * p_ble_event, void * p_context)
{
    NRF_LOG_DEBUG("on_ble_events - event: %d", p_ble_event->header.evt_id);

    ret_code_t err_code = NRF_SUCCESS;

    switch (p_ble_event->header.evt_id)
    {
        case BLE_GAP_EVT_DISCONNECTED:
            NRF_LOG_INFO("Disconnected - Reason: %d", p_ble_event->evt.gap_evt.params.disconnected.reason);
            m_mtu_exchanged = false;
            break;

        case BLE_GAP_EVT_CONNECTED:
        {
            NRF_LOG_INFO("Connected.");
#if BOARD_PCA10040
            err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
            APP_ERROR_CHECK(err_code);
#endif
            if (m_num_connections < NRF_SDH_BLE_PERIPHERAL_LINK_COUNT){
                m_connection_handle[m_num_connections++] = p_ble_event->evt.gap_evt.conn_handle;
            }
            // Trigger the MTU exchange timer
            NRF_LOG_INFO("Trigger MTU exchange timer.");
            if (pdPASS != xTimerStart(m_mtu_exchange_timer, OSTIMER_WAIT_FOR_QUEUE))
            {
                NRF_LOG_WARNING("Failed to start MTU exchange timer");
            }

            NRF_LOG_DEBUG("### Configuration:");
            NRF_LOG_DEBUG("### Connection Interval: %d (ms)", MAX_CONN_INTERVAL * 1.25);
            NRF_LOG_DEBUG("### Connection Event Length: %d (ms)", NRF_SDH_BLE_GAP_EVENT_LENGTH * 1.25);
            NRF_LOG_DEBUG("### Connection Event Length Extension: %d", 0);
            NRF_LOG_DEBUG("### ATT MTU: %d", NRF_SDH_BLE_GATT_MAX_MTU_SIZE);
            NRF_LOG_DEBUG("### Data Length: %d", NRF_SDH_BLE_GAP_DATA_LENGTH);
        } 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_event->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_event->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_event->evt.gatts_evt.conn_handle,
                                             BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            APP_ERROR_CHECK(err_code);
            break;

        case BLE_GAP_EVT_CONN_PARAM_UPDATE:
        {
            NRF_LOG_DEBUG("BLE_GAP_EVT_CONN_PARAM_UPDATE");

            ble_gap_conn_params_t const * p_connection = &p_ble_event->evt.gap_evt.params.conn_param_update.conn_params;
            NRF_LOG_DEBUG("max_conn_interval: %d ms", p_connection->max_conn_interval * 1.25);
            NRF_LOG_DEBUG("min_conn_interval: %d ms", p_connection->min_conn_interval * 1.25);
            NRF_LOG_DEBUG("slave_latency: %d", p_connection->slave_latency);
            NRF_LOG_DEBUG("conn_sup_timeout: %d ms",  p_connection->conn_sup_timeout * 10);
        } break;


        default:
            // No implementation needed.
            break;
    }
}
static void disconnect(uint16_t connection_handle, void * p_context)
{
    UNUSED_PARAMETER(p_context);

    ret_code_t err_code = sd_ble_gap_disconnect(connection_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
    if (err_code != NRF_SUCCESS)
    {
        NRF_LOG_WARNING("Failed to disconnect connection. Connection handle: %d Error: %d", connection_handle, err_code);
    }
    else
    {
        NRF_LOG_DEBUG("Disconnected connection handle %d", connection_handle);

        // Find and remove the connection handle from the array
        for (int i = 0; i < NRF_SDH_BLE_PERIPHERAL_LINK_COUNT; ++i)
        {
            if (m_connection_handle[i] == connection_handle)
            {
                m_connection_handle[i] = BLE_CONN_HANDLE_INVALID;
                m_num_connections--;
                break;
            }
        }

        // Restart advertising if necessary
        if (m_num_connections < NRF_SDH_BLE_PERIPHERAL_LINK_COUNT)
        {
            start_ble_advertising(NULL); // Adjust parameters as needed
        }
    }
}

I have increased the nrf_sdh_ble_peripheral_link_count in the sdk.config file that was available at the nordic site but this causes my device to not advertise at all when the number is increased. What are some other things that I need to look out for?

1 Answer 1

1

I can see that you're using the legacy version of the softdevice. With this old version, there are multiple steps needed to increase the number of peripherals. First, you need to increase the NRF_SDH_BLE_PERIPHERAL_LINK_COUNT define in the SDK_config file as you said. You then need to increase the memory allocation to the softdevice via the sd_enable() function as can be seen here.

If the problem persists, I recommend reaching out to Nordic support on the DevZone as they will be able to help. Also, if possible, I recommend migrating your project to Zephyr/nRF Connect SDK as the nordic softdevice is no longer being developed.

Some relevant links:-

2
  • Tried increasing the size of the gap and gatt size but there is no much improvement, am still unable to advertise as per normal.
    – newtotech
    Commented Jul 5 at 7:47
  • Have you tried increasing the RAM allocation as well? What is the error code that you get when you call ble_advertising_start()? Commented Jul 5 at 8:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.