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

How can I prevent system reset?

Hello, I created a timer that runs the scan_start function at certain intervals.And I put this timer in the for loop.

static void scan_timeout_handler(void * p_context)
{
	scan_start();
}

static void timers_init(void)
{
	uint32_t err_code;

  // Create timers.   
	err_code = app_timer_create(&start_scan_timer_id,
															APP_TIMER_MODE_SINGLE_SHOT,
															scan_timeout_handler);
	APP_ERROR_CHECK(err_code);
}

When the timer completes the time I have defined, the system restarts each time the program flow arrives at the end of the For loop.

int main(void)
{
    uint32_t err_code;
	
		// Initialize the application timer module.
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
	
    ble_stack_init();
    
    err_code = ble_db_discovery_init();
    APP_ERROR_CHECK(err_code);
    
    nus_c_init();
	timers_init();

	nrf_gpio_cfg_output(LED1);
	nrf_gpio_cfg_output(LED2);
	
	nrf_gpio_pin_set(LED1);			
	nrf_gpio_pin_set(LED2);			
	nrf_delay_ms(1000);
	nrf_gpio_pin_clear(LED1);
	nrf_gpio_pin_clear(LED2);
		
    for (;;)
    {	
		err_code = app_timer_start(start_scan_timer_id, APP_TIMER_TICKS(START_SCAN_INTERVAL, APP_TIMER_PRESCALER), NULL);
		APP_ERROR_CHECK(err_code);
			
    }
}

My aim is to constantly measure rssi on the backplane. Then connect to the device when it reaches a certain rssi value and do not reconnect until the rssi value is below a certain value.

case BLE_GAP_EVT_ADV_REPORT:
    {
        const ble_gap_evt_adv_report_t *p_adv_report = &p_gap_evt->params.adv_report;    					
					
	    if(p_adv_report->rssi >= -70 && enter_region==false)	
	    {
	       if (is_uuid_present(&m_normalMode_uuids, p_adv_report))	
	       {		
		   err_code = sd_ble_gap_connect(&p_adv_report->peer_addr,&m_scan_params,&m_connection_param);	
		  }					
	  }    											
           break;	

case BLE_GAP_EVT_CONNECTED:
		APPL_LOG("Connected to target\r\n");
		err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
		APP_ERROR_CHECK(err_code);
		
		m_ble_nus_c.conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
		
		// start discovery of services. The NUS Client waits for a discovery result
		err_code = ble_db_discovery_start(&m_ble_db_discovery, p_ble_evt->evt.gap_evt.conn_handle);
		APP_ERROR_CHECK(err_code);
		sd_ble_gap_rssi_start(p_ble_evt->evt.gap_evt.conn_handle, 0 , 0);

		break;

    case BLE_GAP_EVT_RSSI_CHANGED:						 
    	rssi_value = p_ble_evt->evt.gap_evt.params.rssi_changed.rssi;
    				
    	if(rssi_value>-70 && rssi_value < -61)
    	{
    		enter_region=false;
    	}
    	else if(rssi_value > -60 && rssi_value < -30)		
    	{
    		enter_region=true;
    	}
    break;

The problem is that the system restarts itself every time the timer expires(According to my prediction SoftDeviceHandler is causing this). Thus, all variables are reset to default regardless of program flow(such as rssi_value, enter_region).

How can I always stay in the for loop? Or is there an alternative solution?

Related