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

app timer causing assertion after disconnection event

SoC: NRF51822 xxAC Toolchain: MDK-Lite Version 5.15 Softdevice: S110 SDK Version: 8.1.0_b6ed55f

I'm using a custom board which is functioning well. The app is functioning normally during advertisement and connection, but I am getting an assertion a couple seconds after a client has been disconnected (I don't know how to tell what the assertion is since I get no error code, line number or filename in the error handler).

I have traced the problem to an application timer. If I stop the app timer in the event handler for the disconnect, then I will not have an assertion. The app timer is working as expected while the system is connected, and only causes an exception after the disconnection. The assertion occurs even if I use an empty timeout handler for the app timer. The timer's timeout value is every 200ms, and the assertion occurs around 2-4 seconds after the disconnection event. Any ideas on what could be causing this problem would be appreciated. I've spent a day trying to track down where the problem is occurring.

Timer initialization routine (problem timer is the sample timer):

static void init_timers(void)
{
  uint32_t err_code;

  // INITIALIZE TIMER MODULE, MAKING IT USE THE SCHEDULER
  APP_TIMER_APPSH_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, true);

  err_code = app_timer_create(&m_timers.battery, APP_TIMER_MODE_REPEATED,    timer_handler_battery);
  APP_ERROR_CHECK(err_code);
  err_code = app_timer_create(&m_timers.sample,  APP_TIMER_MODE_REPEATED,    timer_handler_sample);
  APP_ERROR_CHECK(err_code);
}

Event handler code for disconnect (if app_timer_stop() is commented out I get the assertion):

case BLE_GAP_EVT_DISCONNECTED:
  m_conn_handle = BLE_CONN_HANDLE_INVALID;
  stop_battery_measurement();
  leds_start_event(LED_STATE_SHUTDOWN);
  app_timer_stop(m_timers.sample);
  m_system.test = true;
  break;

The sample timer's handler (the conditional statement just makes it do nothing after the disconnect event--I have also verified the problem is still present if this function is completely empty):

void timer_handler_sample(void * p_context)
{
  if(m_system.test == false)
  {
    system_sample_timer_handler();
  }
}
Related