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

BLE with SPI doesn´t reconnect once bluetooth has been disabled and enabled again.

I'm currently working with nrf52832 implementing bluetooth and spi functionalities. The bluetooth connection to PC is working fine, it connects and after that starts transfering and receiving data from SPI slave. The problem comes once I decide to turn bluetooth off on the PC. It connects and disconnects constantly. It also happens once I turn off/on the power button on the SDK.

Here it is the main code and the spi configuration:

int main(void)
{
    bool erase_bonds;


    ret_code_t err_code;

    spi_configuration();

    APP_ERROR_CHECK(nrf_drv_clock_init());
    nrf_drv_clock_lfclk_request(NULL);
    
    log_init();
    

    err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);


    motion_timer_init();

 
    // Initialize.
    buttons_leds_init(&erase_bonds);
    power_management_init();
    ble_stack_init();
    scheduler_init();
    gap_params_init();
    gatt_init();
    advertising_init();
    services_init();
    conn_params_init();
    peer_manager_init();
    bsp_board_init(BSP_INIT_LEDS);

    // Start execution.
    advertising_start(erase_bonds);

    // Enter main loop.
    for (;;)
    {
      if(connected){
        NRF_LOG_INFO("Connected.");

        laser_frame();  //transfer and receive spi data
       
      }

      idle_state_handle();
      nrf_delay_ms(2000);
    }
}


void spi_configuration(void){
  /* SPI Configuration */
    nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
    spi_config.ss_pin   = SPI_SS_PIN;
    spi_config.miso_pin = SPI_MISO_PIN;
    spi_config.mosi_pin = SPI_MOSI_PIN;
    spi_config.sck_pin  = SPI_SCK_PIN;

    APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));

}

If spi_configuration() and laser_frame() are commented, the problem when reconnecting disappears. Any idea? Thanks!

Parents
  • That nrf_delay_ms(2000); looked scary. 

    Can you try to allow re-pairing by adding the following in pm_evt_handler() in main.c

    case PM_EVT_CONN_SEC_CONFIG_REQ:
    {
    // Allow or reject pairing request from an already bonded peer.
    pm_conn_sec_config_t conn_sec_config = {.allow_repairing = true};
    pm_conn_sec_config_reply(p_evt->conn_handle, &conn_sec_config);
    } break;

  • Hey, sorry for the late reply. I have been trying what you propossed and a few more things and this is what I get:

    1. (nrf_delay_ms(2000); deleted) After adding your code I realized that it never goes into that case so it keeps disconnecting and connecting when attempting to reconnect. Still getting error 4102.
    2.  I though it could be because it deletes the bonds in the line 37 so I changed it with advertising_start(false);
    3. Now the sequence I get is:

    <info> app:     m_whitelist_peer_cnt 2, MAX_PEERS_WLIST 8
    <info> app: Fast advertising with whitelist.
    <info> app: Connected
    <info> app: Connected.

    (gets one data buffer from spi slave)

    <error> app: Fatal error
    <warning> app: System reset

    Also I keep getting the following (3 times in a row) while BLE configuration but it connects after all:

    <error> nrf_ble_gatt: sd_ble_gatts_exchange_mtu_reply() returned NRF_ERROR_INVALID_STATE.

Reply
  • Hey, sorry for the late reply. I have been trying what you propossed and a few more things and this is what I get:

    1. (nrf_delay_ms(2000); deleted) After adding your code I realized that it never goes into that case so it keeps disconnecting and connecting when attempting to reconnect. Still getting error 4102.
    2.  I though it could be because it deletes the bonds in the line 37 so I changed it with advertising_start(false);
    3. Now the sequence I get is:

    <info> app:     m_whitelist_peer_cnt 2, MAX_PEERS_WLIST 8
    <info> app: Fast advertising with whitelist.
    <info> app: Connected
    <info> app: Connected.

    (gets one data buffer from spi slave)

    <error> app: Fatal error
    <warning> app: System reset

    Also I keep getting the following (3 times in a row) while BLE configuration but it connects after all:

    <error> nrf_ble_gatt: sd_ble_gatts_exchange_mtu_reply() returned NRF_ERROR_INVALID_STATE.

Children
Related