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
Parents
  • 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]

Reply
  • 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]

Children
  • 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. 

Related