Central resets randomly during scanning

Hello and thanks for reading. Our device needs to act as both a central and peripheral. I have written all the peripheral code, and its all working perfectly. However, once I started writing code for the central role, I discovered a bug that I cant quite figure out.

Our device seems to start scanning fine, but then randomly resets, typically after about ten seconds of scanning. I have tried commenting out most, or even all, of the peripheral code, in order try and isolate the problem, but without success. I have also reduced the central code to just scan indefinitely, it doesnt even try to connect.

Is there anything obviously wrong you can see? All of the relevant code is below as follows:

prj.config:

#GPIO
CONFIG_GPIO=y
CONFIG_PINCTRL=y

#UART & SERIAL
CONFIG_SERIAL=y
CONFIG_NEWLIB_LIBC=y
CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y


CONFIG_THREAD_STACK_INFO=y
CONFIG_THREAD_MONITOR=y
CONFIG_INIT_STACKS=y
CONFIG_STACK_SENTINEL=y



CONFIG_MAIN_STACK_SIZE=16384
CONFIG_LOG=y
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=8192
CONFIG_HEAP_MEM_POOL_SIZE=8192

#CPP
CONFIG_CPP=y
CONFIG_REQUIRES_FULL_LIBCPP=y
CONFIG_STD_CPP20=y

#i2C
CONFIG_I2C=y

#spi
CONFIG_SPI=y

#flash
CONFIG_SETTINGS=y
CONFIG_FLASH=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_NVS=y
CONFIG_FLASH_MAP=y
CONFIG_MPU_ALLOW_FLASH_WRITE=y
CONFIG_FILE_SYSTEM=y
CONFIG_SOC_FLASH_NRF=y

#math
CONFIG_CMSIS_DSP=y
CONFIG_NEWLIB_LIBC=y
CONFIG_FPU=y
CONFIG_CMSIS_DSP_FILTERING=y

#BLE
CONFIG_BT=y
CONFIG_BT_SETTINGS=y
CONFIG_BT_GATT_CLIENT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_DEBUG_LOG=y
CONFIG_BT_CENTRAL=y
CONFIG_BT_BROADCASTER=y
CONFIG_BT_OBSERVER=y

CONFIG_BT_DEVICE_NAME_DYNAMIC=y
CONFIG_BT_DEVICE_NAME_MAX=30

#NUS
CONFIG_BT_NUS=y
CONFIG_BT_NUS_AUTHEN=n

#BT BATTERY SERVICE (BAS)
CONFIG_BT_BAS=y

#DIS
CONFIG_BT_DIS=y #all non-pnp DIS settings are done dynamically
CONFIG_BT_DIS_PNP=n
CONFIG_SETTINGS_RUNTIME=y
CONFIG_BT_DIS_SETTINGS=y

CONFIG_BT_DIS_MODEL="GENERIC"
CONFIG_BT_DIS_MANUF=""
CONFIG_BT_DIS_SERIAL_NUMBER=y
CONFIG_BT_DIS_FW_REV=y
CONFIG_BT_DIS_HW_REV=y
CONFIG_BT_DIS_SW_REV=y
CONFIG_BT_DIS_SERIAL_NUMBER_STR="xxxxxxxx"
CONFIG_BT_DIS_FW_REV_STR="0"
CONFIG_BT_DIS_HW_REV_STR="0"
CONFIG_BT_DIS_SW_REV_STR="0"

#BT HID
CONFIG_NCS_SAMPLES_DEFAULTS=y

# CONFIG_BT_CTLR_SDC_PERIPHERAL_COUNT=3
CONFIG_BT_MAX_CONN=6
# CONFIG_BT_MAX_PAIRED=5
CONFIG_BT_SMP=y
CONFIG_BT_L2CAP_TX_BUF_COUNT=5
CONFIG_BT_DEVICE_APPEARANCE=962

CONFIG_BT_HIDS=y
CONFIG_BT_HIDS_MAX_CLIENT_COUNT=2
CONFIG_BT_HIDS_DEFAULT_PERM_RW_ENCRYPT=y
CONFIG_BT_GATT_UUID16_POOL_SIZE=40
CONFIG_BT_GATT_CHRC_POOL_SIZE=20

CONFIG_BT_CONN_CTX=y

CONFIG_BT_USER_DATA_LEN_UPDATE=y
CONFIG_BT_BUF_ACL_RX_SIZE=251
CONFIG_BT_BUF_ACL_TX_SIZE=251
CONFIG_BT_CTLR_DATA_LENGTH_MAX=251
CONFIG_BT_L2CAP_TX_MTU=247

CONFIG_BT_ATT_PREPARE_COUNT=4
CONFIG_BT_CONN_TX_MAX=10
CONFIG_BT_L2CAP_DYNAMIC_CHANNEL=y


CONFIG_BT_PERIPHERAL_PREF_MIN_INT=6
CONFIG_BT_PERIPHERAL_PREF_MAX_INT=24
CONFIG_BT_PERIPHERAL_PREF_LATENCY=0
CONFIG_BT_PERIPHERAL_PREF_TIMEOUT=400

#ADC
CONFIG_ADC=y
CONFIG_ADC_ASYNC=y

#timer
CONFIG_COUNTER=y

#52840 specific
CONFIG_BT_CTLR_SDC_PERIPHERAL_COUNT=3 
CONFIG_BT_MAX_PAIRED=5 
CONFIG_CONSOLE=y 
CONFIG_UART_CONSOLE=y 
CONFIG_UART_LINE_CTRL=y 
CONFIG_USB_DEVICE_STACK=y 
CONFIG_USB_DEVICE_PRODUCT="console" 
CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n 

And the relevant code:

//std libraries
#include <stddef.h>
#include <errno.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

//zephyr basic libraries
#include <zephyr/types.h>
#include <zephyr/sys/printk.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/logging/log.h>


#include <zephyr/drivers/gpio.h>

//zephyr BLE
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/hci.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/gatt.h>
#include <zephyr/bluetooth/uuid.h>
#include <zephyr/bluetooth/att.h>
#include <bluetooth/services/nus.h>
#include <zephyr/sys/byteorder.h>

#include "BLE_Central.h"
#include "BLE.h"
#include "BLE_Commands.h"
#include "NTP_Time.h"
#include "NUS.h"


static void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,struct net_buf_simple *ad)
{
	char addr_str[BT_ADDR_LE_STR_LEN];
	int err;

	if (default_conn) {
		return;
	}

	/* We're only interested in connectable events */
	if (type != BT_GAP_ADV_TYPE_ADV_IND &&
	    type != BT_GAP_ADV_TYPE_ADV_DIRECT_IND) {
		return;
	}

	bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
	printk("Device found: %s (RSSI %d)\n", addr_str, rssi);
}

int Central_Connect_And_Sync()
{
	bt_enable(NULL);
	settings_load();
	int err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found);
	if (err) 
	{
		printk("Scanning failed to start (err %d)\n", err);
		return;
	}

	return false;
}

  • Hello,

    Does the log say anything when it resets? What HW are you running this on? The nRF52840 DK, or a custom board?

    Best regards,

    Edvin

  • Hello,

    I am running this on the 52840 dongle; I dont get any log messages, the system just resets.

    The lack of log messages might be because with the dongle it uses the USB port for uart/logging and when it crashes we lose USB functionality too.

    I dont think it matters, but I should also mention we are using SDK 2.5.0, and are using a mix of C and C++.

  • I see. The dongle is not great for development, because it doesn't have a debugger. This means that it is possible to output log messages over USB, but as you say, when the application crashes, the USB functionality goes down with it, so the log disappears as well. 

    If you have an external UART -> USB thing, you can try to monitor the log via UART, but if not, I recommend that you try to run the application on a DK, and see if you can replicate the issue there. In general, application development on the dongle is not recommended. If you want to run the application on a dongle, write it on a DK first, and when you are up and running, you can port the application over to the dongle.

    Best regards,

    Edvin

  • I switched to my NRF5340 dev board and the problem seems to have gone away, so Im going to assume the problem was somehow related to pushing too much data through the USB driver too fast or something. In any case we dont plan to continue working with the nrf52840, I was just using the dongle for conveience, so I think im just going to move forward and call it closed. Thanks!

Related