Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

NRF52 reconnection problem

Hello. I am using ble_app_uart from sdk14.2. I use pca100401, softdevice s132.

Advertising is generated using timer RTC. Board advertises and stops advertising without errors if I it starts and I do not connect to it. However, when trying to reconnect after one connection and disconnection event with android device I receive an error. Error message in J-link RTT viewer states that error occurs at ..\..\..\main.c:1416 which is:

This is my main.c code:

/**@brief Application main function.
 */
int main(void)
 {
    uint32_t err_code;
    //bool     erase_bonds;
	timers_init();
    uart_init();
    log_init();
   // buttons_leds_init(&erase_bonds);
    ble_stack_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();

   
 SEGGER_RTT_WriteString(0, "Hello World_SEGGER!\n");
    //NRF_LOG_INFO("UART Start_NRF_LOG_INFO!");


/******************* MY CHANGES ****************/
gpio_config();
gpiote_init();
fstorage_init();

my_flash_begin();
timers_start();
nrf_gpio_pin_write(LED_2,0);
nrf_gpio_pin_write(LED_1,1);
/******************* MY CHANGES ****************/
					
    while(1)
    {
			if(begin_flag == 1)
			{
				if(advertising_started==0)
				 {
					 advertising_started =1;
					 device_beginning_service();
					 
					 sprintf((char*)string18,"advertising started\r\n");
					 SEGGER_RTT_WriteString(0,(char*)string18);
					
					 err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
					
					 APP_ERROR_CHECK(err_code);

				 }
		 
			}

This is the error I receive in J-link RTT viewer:

ble_advertising.h states that error means: the module is not initialized. 

I am thinking that it might be problem with connection or disconnection events. Here is how I describe them:

       case BLE_GAP_EVT_CONNECTED:
						 SEGGER_RTT_WriteString(0,"CONNECTION HAPPENED\n\r");
						 nrf_gpio_pin_write(LED_3, 0);
						 nrf_gpio_pin_write(LED_2, 1);
						 nrf_gpio_pin_write(LED_1, 1);
//            NRF_LOG_INFO("Connected");
						 app_timer_stop_all();
           /* err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
            APP_ERROR_CHECK(err_code);*/
            m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
					
            break;

case BLE_GAP_EVT_DISCONNECTED:
						 SEGGER_RTT_WriteString(0,"DISCONNECTION HAPPENED\n\r");
						 nrf_gpio_pin_write(LED_2, 0);
						 nrf_gpio_pin_write(LED_1, 1);
						 nrf_gpio_pin_write(LED_3, 1);
						 begin_flag = 0;
				     advertising_started = 0;
						 app_timer_stop_all();
				     err_code = app_timer_start(m_sleep_timer_id, SLEEP_TIMER_INTERVAL, NULL);  		  //start sleeping timer
				     APP_ERROR_CHECK(err_code);
            // LED indication will be changed when advertising starts.
            m_conn_handle = BLE_CONN_HANDLE_INVALID;
            break;

Would like to receive any suggestions why this is happening. Thank you.

  • Hello,

    I think you posted the wrong screenshot after:

    "This is the error I receive in J-link RTT viewer"

    Can you please try to check the return on app_timer_stop_all();

    err_code = app_timer_stop_all();
    APP_ERROR:CHECK(err_code);

    Aslo, what is the RTT printing on line 1416 in main?

    Best regards,

    Edvin

  • Thank you for taking time to investigate my problem. I have deleted few line of code that is why RTT shows error in main.c 1418. This is the same error that occurs at 1416. They both bring this to RTT: <error> app: ERROR 8 [NRF_ERROR_INVALID_STATE] at ..\..\..\main.c:1418

    Moreover, I have tried to change app_timer_stop_all();  to just one timer that I use. This change have not solved my problem. 

    I have tried your proposal to use:

    err_code = app_timer_stop_all();
    APP_ERROR_CHECK(err_code);

    The result is same as it was before. RTT says that error stems from main.c 1418.

  • Hello,

    Ok, so the function ble_advertising_start() returns 8 [NRF_ERROR_INVALID_STATE].

    The app_timer_stop_all() function does not seem to stop the timer that the softdevice uses, only the ones used in app_timer, so that should not be the problem. I tried to use app_timer_stop_all() right before ble_advertising_start(), and that works fine.

    NRF_ERROR_INVALID_STATE usually indicates that ble_advertising_init() has not been called before the ble_advertising_start() function. Since you are able to start advertising before the connection, this is obviously not the case here. Have you changed anything in your m_advertising variable while being in the connection?

    It is a bit difficult to say only from the code snippets, but is the ble_advertising_start() that is called and returns the error the one in your main() function, or is it called from somewhere else? Maybe it is being called twice?

    Best regards,

    Edvin

  • Thank you for suggestions. m_advertising  is defined only in main.c like this:  BLE_ADVERTISING_DEF(m_advertising); 

    Function err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code); is being used only in main() function.

    Would it be easier if I upload it to a private case study?

    Best regards

  • And you only use ble_advertising_init() once? Yes. You could do that. It would be great if you link to this case in the private case, so that I can pick it up.

    Best regards,

    Edvin

Related