Hi,
I'm using the Bluetooth sample called Central HIDS and want to develop a feature that stops BT scanning after a few minutes.
However, during development, I encountered that when BT started scanning, the one shot timer entered the expiration function after reaching a specific time. Executing the bt_scan_stop() API to stop BT scanning in the expiration function would cause a kernel panic and system restart.
Here is an excerpt of some modified code. The rest of the code is the same as the sample code.
/* other defines */ ... #define AUTO_SCAN_TIME K_MINUTES(2) struct k_timer scan_timer; /* other announcements */ ... /* other functions */ ... static void scan_timer_expired(struct k_timer *timer_id) { int err; printk("Stop scanning\n"); err = bt_scan_stop(); if (err) { printk("Stop LE scan failed (err %d)\n", err); } printk("Scanning successfully stopped\n"); } static void scan_init(void) { int err; struct bt_scan_init_param scan_init = { .connect_if_match = 1, .scan_param = NULL, .conn_param = BT_LE_CONN_PARAM_DEFAULT }; bt_scan_init(&scan_init); bt_scan_cb_register(&scan_cb); /* do other work */ ... k_timer_init(&scan_timer, scan_timer_expired, NULL); } void main(void) { int err; printk("Starting Bluetooth Central HIDS example\n"); /* do other work */ ... scan_init(); /* do other work */ ... err = bt_scan_start(BT_SCAN_TYPE_SCAN_ACTIVE); if (err) { printk("Scanning failed to start (err %d)\n", err); return; } k_timer_start(&scan_timer, AUTO_SCAN_TIME, K_NO_WAIT); printk("Scanning successfully started\n"); }
First, I defined a duration called AUTO_SCAN_TIME and an expiry function called scan_timer_expired() for the one shot timer.
The stop BT scan function is defined in the scan_timer_expired() function.
Next, initialize the one shot timer in the scan_init() function.
Finally, after BT starts scanning, start the one shot timer in the main() function.
This is the UART console log.
*** Booting Zephyr OS build vasco_phase1_20230727 *** Starting Bluetooth Central HIDS example I: 8 Sectors of 4096 bytes I: alloc wra: 0, fe8 I: data wra: 0, 0 I: SoftDevice Controller build revision: I: 6d 90 41 2a 38 e8 ad 17 |m.A*8... I: 29 a5 03 38 39 27 d7 85 |)..89'.. I: 1f 85 d8 e1 |.... I: HW Platform: Nordic Semiconductor (0x0002) I: HW Variant: nRF52x (0x0002) I: Firmware: Standard Bluetooth controller (0x00) Version 109.16784 Build 2917677098 I: No ID address. App must call settings_load() Bluetooth initialized I: Identity: DA:3F:22:39:70:D5 (random) I: HCI: version 5.3 (0x0c) revision 0x11fa, manufacturer 0x0059 I: LMP: version 5.3 (0x0c) subver 0x11fa Scanning successfully started Stop scanning ASSERTION FAIL @ WEST_TOPDIR/zephyr/kernel/sem.c:121 E: r0/a1: 0x00000004 r1/a2: 0x00000079 r2/a3: 0x00000001 E: r3/a4: 0x0001f891 r12/ip: 0x00000000 r14/lr: 0x00024953 E: xpsr: 0x41000021 E: Faulting instruction address (r15/pc): 0x0002a622 E: >>> ZEPHYR FATAL ERROR 4: Kernel panic on CPU 0 E: Fault during interrupt handling E: Current thread: 0x20002060 (unknown) *** Booting Zephyr OS build vasco_phase1_20230727 *** Starting Bluetooth Central HIDS example I: 8 Sectors of 4096 bytes I: alloc wra: 0, fd0 I: data wra: 0, 1c I: SoftDevice Controller build revision: I: 6d 90 41 2a 38 e8 ad 17 |m.A*8... I: 29 a5 03 38 39 27 d7 85 |)..89'.. I: 1f 85 d8 e1 |.... I: HW Platform: Nordic Semiconductor (0x0002) I: HW Variant: nRF52x (0x0002) I: Firmware: Standard Bluetooth controller (0x00) Version 109.16784 Build 2917677098 I: No ID address. App must call settings_load() Bluetooth initialized I: Identity: DA:3F:22:39:70:D5 (random) I: HCI: version 5.3 (0x0c) revision 0x11fa, manufacturer 0x0059 I: LMP: version 5.3 (0x0c) subver 0x11fa Scanning successfully started
Environment and tools I use:
nRF Connect SDK version: v2.2.0
Hardware: nRF52840 DK
Could anyone help with this?
Please feel free to let me know if you need more information.