Zephyr bluetooth central sample fails with error -EAGAIN

BT central sample has the following code

bt_enable(NULL);
bt_le_scan_start(SCAN_PASSIVE, device_found);    
I ran the sample on my custom board based on nRF52840 and it always returns -EAGAIN even if I put the bt_le_scan_start in a loop and wait 1000 ms between attempts
I looked in the bt_le_scan_start function in scan.c
It returns -EAGAIN if BT_DEV_READY flag is not set:
if (!atomic_test_bit(bt_dev.flags, BT_DEV_READY)) {
        return -EAGAIN;
    }
Any ideas why this flag is never set? bt_enable does not return an error and is called synchronously
Project settings
-------------------------
CONFIG_BT=y
CONFIG_BT_CENTRAL=y

CONFIG_BT_OBSERVER=y
CONFIG_BT_BROADCASTER=y

CONFIG_BT_MESH=y
CONFIG_BT_MESH_RELAY=y
CONFIG_BT_MESH_SUBNET_COUNT=1
CONFIG_BT_MESH_APP_KEY_COUNT=1
CONFIG_BT_MESH_MODEL_GROUP_COUNT=2
CONFIG_BT_MESH_ADV_BUF_COUNT=10
CONFIG_BT_MESH_LABEL_COUNT=0
CONFIG_BT_MESH_PB_ADV=n
CONFIG_BT_MESH_CFG_CLI=y
CONFIG_BT_MESH_LOOPBACK_BUFS=8

CONFIG_BT_SETTINGS=y
CONFIG_FLASH=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_FLASH_MAP=y
CONFIG_NVS=y
CONFIG_SETTINGS=y
CONFIG_BT_MESH_RPL_STORE_TIMEOUT=600
--------------------------------------------------------
Thank you
Andy
  • Update

    1. Calling bt_enable from a thread crashes the program - is this expected?

    2.Enabled logging, called from main thread, see this  message after bt_enable

    [00:01:03.757,263] <inf> sdc_hci_driver: SoftDevice Controller build revision:
    0e e7 c5 66 67 18 3c ac b3 d2 cc 81 a3 dc f1 c0 |...fg.<. ........
    c0 36 02 22 |.6."
    [00:01:03.763,702] <inf> bt_hci_core: No ID address. App must call settings_load()

    Perhaps this is a clue.

    Any ideas?

    Thanks

  • Update 2

    1.Calling settings_load() fixed the scan_start problem. Now scanning works 
    Apparently this is a requirement if CONGFIG_BT_SETTINGS is enabled ( it is )
    Wish documentation was more clear on that

    2. Calling bt_enable from a thread other than main,crashes the app with the following error

    [00:00:00.009,918] <err> os: ***** MPU FAULT *****
    [00:00:00.010,314] <err> os: Stacking error (context area might be not valid)
    [00:00:00.010,803] <err> os: Data Access Violation
    [00:00:00.011,199] <err> os: MMFAR Address: 0x20005230
    [00:00:00.011,657] <err> os: r0/a1: 0xfc71651f r1/a2: 0x72c7d345 r2/a3: 0x263cb4d2
    [00:00:00.012,268] <err> os: r3/a4: 0xa56dccb0 r12/ip: 0x4d7daa1a r14/lr: 0x271198f7
    [00:00:00.012,908] <err> os: xpsr: 0x61000000
    [00:00:00.013,305] <err> os: Faulting instruction address (r15/pc): 0x0002e228
    [00:00:00.013,854] <err> os: >>> ZEPHYR FATAL ERROR 2: Stack overflow on CPU 0
    [00:00:00.014,404] <err> os: Current thread: 0x20001e98 (comm_task)
    [00:00:00.019,897] [1;31m<err> fatal_error: Resetting system[0m]

  • Hi Andy, 

    Could you show how you define the thread that you call bt_enable() ? 

    As far as I know it should be fine to call it from thread with positive priority (and not from a interrupt handler) 

    If you enable CONFIG_SETTINGS, settings_load() should be called. I'm a little bit unsure why not calling it causing the scan problem though. 
    One side question, I assume you are doing mesh at the same time as doing central scanning ? If true this could potentially reduce mesh performance. 

  • Thread was defined as follows

    K_THREAD_DEFINE(comm_task, 1024, comm_task_run, NULL, NULL, NULL,    7, 0, 0);
    I have another thread running that is defined as above and it works fine
    But when I a second thread  I get a crash
    I tried to reduce  the size to 256 but that did not help - same error
    Perhaps that's not the right way - K_THREAD_DEFINE resolves to 
    K_KERNEL_STACK_DEFINE - I'm not sure that I need a supervisor thread in this case. The goal of this task to handle mesh. What's your recommendation ?
  • Hi Andy, 


    Could you provide a simplified example that we can test here ? 
    I tried with the ble_peripheral_lbs_coex and didn't see the issue. 
    I replaced main() function with this: 


    #define STACKSIZE 1024
    void ble_write_thread(void)
    {	int err;
    
    	printk("Thread..\n");
    
    	err = bt_enable(bt_ready);
    	if (err) {
    		printk("Bluetooth init failed (err %d)\n", err);
    	}
    
    }
    void blink_LED(void)
    {	
    
    	for (;;) {
    		printk("LED\n");
    		k_sleep(K_MSEC(1000));
    	}
    
    
    }
    K_THREAD_DEFINE(ble_write_thread_id, STACKSIZE, ble_write_thread, NULL, NULL,
    		NULL, 7, 0, 0);
    K_THREAD_DEFINE(blink_LED_id, STACKSIZE, blink_LED, NULL, NULL,
    		NULL, 7, 0, 0);

Related