NRF 5340 resets automatically in between device scanning

hi,

                  i am trying to connect  peripherials ( blood pressure, pulseox meter) with nrf5340 using uuid and mac, initially using uuid filter are used to scan the devices around and store the mac of the device. once the mac is stored we use address filter to  filter the device based on the mac address that was stored earlier.

once the mac address matches with the stored mac address  the connect with the device is established and gatt discovery is initialed and the handles are collected and data is received and subscribing to them. everything works as expected.  and we could get the vitals from the blood pressure and pulse oximeter.

but we face an unexpected controller reset all of sudden in between in a random pattern whenever the scan_start function is initialed. since we use more than one device we initial Scan_start to initiate the filers to scan for other devices this is done during the connected, disconnected  and once gatt_ discovery call back function is succeeded. the nrf controller gets reset automatically during the scan_start function at any on the given call back function. 

static void set_scan_filters(void)
{
	// scan filters are determined based on the current mode (pairing or connecting)
	// and based on which (if any) connections are currently active

	int err;

	// remove existing filters
	//printk("Resetting scan filters\n");
	bt_scan_filter_remove_all();


	if(hub_mode == HUB_MODE_PAIRING_MODE)
	{
		// in Pairing mode, we want to just filter by UUID
		printk("Setting new scan filters for pairing mode based on UUIDs and current active connections\n");

		if(p_blood_pres_conn == NULL)
		{
			// not currently connected to any BP sensor, so add that filter
			//printk("Setting scan filter for BP UUID\n");
			err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_UUID, BT_UUID_BPS);
		}
		if(p_pulse_ox_conn == NULL)
		{
			// not currently connected to any Pulse Ox sensor, so add that filter
			//printk("Setting scan filter for Pulse Ox UUID\n");
			err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_UUID, BT_UUID_PUL_SERVICE);
		}
		if(p_therm_conn == NULL)
		{
			// not currently connected to any Thermometer, so add that filter
			//printk("Setting scan filter for Thermometer UUID\n");
			err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_UUID, BT_UUID_TEMP_SERVICE);
		}

		err = bt_scan_filter_enable(BT_SCAN_UUID_FILTER, false);
		if (err)
		{
			printk("Filters cannot be turned on (err %d)\n", err);
		}

	}
	else if(hub_mode == HUB_MODE_CONNECT_MODE)
	{
		// in Connect mode, we want to connect only if we have been given a target address and are 
		// not currently connected to that type of device

		printk("Setting new scan filters for connecting mode based on target addresses and current active connections\n");

		if(p_blood_pres_conn == NULL)
		{
			// not currently connected to any BP sensor

			// check if we have a specific address to connect to
			if(have_target_blood_pres_address == true)
			{
				// Have a target address - set filter
				//printk("Setting scan filter for target BP address\n");
				bt_addr_le_t addr_le_bps;
				memset(&addr_le_bps, 0x00, sizeof(addr_le_bps));
				memcpy(addr_le_bps.a.val, target_blood_pres_address, sizeof(target_blood_pres_address));
				addr_le_bps.type = BT_ADDR_LE_PUBLIC;
				err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_ADDR, &addr_le_bps);
			}
			printk( "Not reseted yet in Bp condition");
		}

		if(p_pulse_ox_conn == NULL)
		{
			// not currently connected to any pulse ox

			// check if we have a specific address to connect to
			if(have_target_pulse_ox_address == true)
			{
				// Have a target address - set filter
				//printk("Setting scan filter for target Pulse Ox address\n");
				bt_addr_le_t addr_le_pulseox;
				memset(&addr_le_pulseox, 0x00, sizeof(addr_le_pulseox));
				memcpy(addr_le_pulseox.a.val, target_pulse_ox_address, sizeof(target_pulse_ox_address));
				addr_le_pulseox.type = BT_ADDR_LE_PUBLIC;
				err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_ADDR, &addr_le_pulseox);
			}
			printk( "Not reseted yet in plus condition");
		}

		if(p_therm_conn == NULL)
		{
			// not currently connected to any thermometer

			// check if we have a specific address to connect to
			if(have_target_therm_address == true)
			{
				// Have a target address - set filter
				//printk("Setting scan filter for target Thermometer address\n");
				bt_addr_le_t addr_le_therm;
				memset(&addr_le_therm, 0x00, sizeof(addr_le_therm));
				memcpy(addr_le_therm.a.val, target_therm_address, sizeof(target_therm_address));
				addr_le_therm.type = BT_ADDR_LE_PUBLIC;
				err = bt_scan_filter_add(BT_SCAN_FILTER_TYPE_ADDR, &addr_le_therm);
			}
			printk( "Not reseted yet in threm condition");
		}

		err = bt_scan_filter_enable(BT_SCAN_ADDR_FILTER, false);
		if (err)
		{
			printk("Filters cannot be turned on (err %d)\n", err);
		}
		printk( "Not reseted yet in connetcion function");
	}
}


static int scan_start(void)
{
	int err;
	uint8_t chan_map[5] = { 0x55, 0x55, 0, 0, 0};

	err = bt_le_set_chan_map(chan_map);
	if (err)
	{
		printk("Bad Channel (err %d)\n", err);
	}

	// always refresh scan filters before starting scanning
	set_scan_filters();

	printk("Starting scanning\n");
	err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE);

	if (err)
	{
		// error
		printk("Scanning failed to start (err %d)\n", err);

		//Scan Error
		uint16_t scan_error_payload = SCAN_ERROR_PAYLOAD;
    	send_message_data(SCAN_ERROR, &scan_error_payload, sizeof(scan_error_payload));
	}
	else
	{
		// success
		hub_scan_init_state = HUB_SCANNING;
	}

	return err;
}
  

we suspected it might be due to the stack overflow and we tried to increase the size however stil the issue sustained. we are not sure what the issue is since we could not find any debug error in the debugger kindly help us how to identify the issue related to the rest.

# disable DCDC

CONFIG_BOARD_ENABLE_DCDC_APP=n
CONFIG_BOARD_ENABLE_DCDC_NET=n
CONFIG_BOARD_ENABLE_DCDC_HV=n

# Enable the UART driver
# CONFIG_UART_ASYNC_API=y
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_NRFX_UARTE0=y
CONFIG_SERIAL=y
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y

# Enable the BLE stack with GATT Client configuration
CONFIG_BT=y
CONFIG_BT_CENTRAL=y
CONFIG_BT_SMP=y
CONFIG_BT_GATT_CLIENT=y
CONFIG_BT_MAX_CONN=20
CONFIG_BT_MAX_PAIRED=20
CONFIG_BT_CONN_CTX=y
CONFIG_BT_SMP_APP_PAIRING_ACCEPT=y


# Enable the BLE modules from NCS
CONFIG_BT_NUS_CLIENT=n
CONFIG_BT_SCAN=y
CONFIG_BT_SCAN_FILTER_ENABLE=y
CONFIG_BT_SCAN_UUID_CNT=3
CONFIG_BT_SCAN_ADDRESS_CNT=3
CONFIG_BT_GATT_DM=y
CONFIG_BT_GATT_DM_MAX_ATTRS=60
CONFIG_HEAP_MEM_POOL_SIZE=8192

# This example requires more stack
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=16384
CONFIG_MAIN_STACK_SIZE= 4096

# Enable bonding
CONFIG_BT_SETTINGS=y
CONFIG_FLASH=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_FLASH_MAP=y
CONFIG_NVS=y
CONFIG_SETTINGS=y

# Config logger
CONFIG_RTT_CONSOLE=y
CONFIG_LOG=y
CONFIG_USE_SEGGER_RTT=y
CONFIG_LOG_BACKEND_RTT=y
CONFIG_LOG_BACKEND_UART=n
CONFIG_LOG_PRINTK=n

CONFIG_ASSERT=y
CONFIG_BT_DEBUG_LOG=y

CONFIG_DK_LIBRARY=y

CONFIG_NO_OPTIMIZATIONS=y

#Debug Keys. MAKE SURE TO SET THESE TO n FOR PRODUCTION
CONFIG_BT_USE_DEBUG_KEYS=y
CONFIG_BT_STORE_DEBUG_KEYS=y

CONFIG_WATCHDOG=n

  • Hi Aravind, I think I have seen before, just do not remember the whole context but I remember that it had to do with the multiple attempts of scanning while the scanner is still on. Try to stop the scanner before you start it (if the bt_scan_stop returns error, it is normal, you do not need to handle the error). Do something like bt_scan_stop, wait few milliseconds and scan_start again. 

    The other reason I can think of is that you might be seeing a side effect of stack overflow, Make sure that all your contexts have enough stack memory allocated  
    Some of the configs that are generally useful to enable in prj.conf for such debugging is mentioned here. Enable those and pristine build your project, flash and see what you see on the serial logs.

  • hi Susheel, thank you for the reply i tried using the configs you suggested, and i could visualize the stack usage and i guess the issue is not due to stack over flow, since mostly 50 to 70 percent of the stack is unused. I guess the issue is due to the scan attempts as you told. i tried to add the scan_stop function before starting the scan_start function with a delay  200ms however, the issue is not solved and it can be clearly noticed that the reset happens during the execution of the scan_start function. is there any way we can identify the exact cause further. 

       

  • Aravind, sorry for late response.

    Either this is a brownout reset or a software reset. A software reset is trackable.

    put a breakpoint in zephyr\lib\os\reboot.c->sys_reboot function and recompile your project enabling debug_optimizations as compiler flag and start the debugger and run the code. When the breakpoint hits, look at the function call trace and post it here. We will get a full context of the reset then and we can go from there to attempt to solve it.

  • hi Susheel, thank you for the mail, i tried as per your suggestion by providing the break point in the sys_reboot function and check if the debugger breaks during reset. However the debugger did not break during the reset. i tried to log what causes the reset and came across a function in devzone which helps finding the reason.

     nrf52840 soft reset cause in this they provide a function to log the reset cause i tried to add it in my code to determine which causes the fault.

     

    and i got the reset is due to software Reset_Software. 

    i'm quite confused, if its due to software then why during the debugging face the debugger did not break at the point. and how can we further know what the exact cause of the issue. im using the nordic nrf5340_DK development kit and im powering it up using the usb of my laptop. so i dont guess its due to brownout. kindly let me know how i can continue further 

  • Aravibala said:
    i'm quite confused, if its due to software then why during the debugging face the debugger did not break at the point.

    I have seen this behavior many times and most of the times it happens due to the compiler settings of debug optimizations. If you are compiling your program for performance or code size, then there is minimal debug info and the debugger wont function as it should. Please recompile your program (pristine build) after you enable optimizations for debugger. 

    In VSC you can find it like this


    If you are compiling from the command line 

Related