8000 ports/esp32: Pin MP and NimBLE to core 0. · micropython/micropython@6546f70 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6546f70

Browse files
committed
ports/esp32: Pin MP and NimBLE to core 0.
MP and NimBLE must be on the same core (for synchronisation of the BLE ringbuf and the MP scheduler). However, in the current IDF versions (3.3 and 4.0) there are issues (e.g. #6343) with running NimBLE on core 1. This change makes it possible to reliably run the BLE multitests.
1 parent d466483 commit 6546f70

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

extmod/nimble/modbluetooth_nimble.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ STATIC void sync_cb(void) {
183183
int rc;
184184
(void)rc;
185185

186+
DEBUG_printf("sync_cb: state=%d\n", mp_bluetooth_nimble_ble_state);
187+
188+
if (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_WAITING_FOR_SYNC) {
189+
return;
190+
}
191+
186192
if (has_public_address()) {
187193
nimble_address_mode = BLE_OWN_ADDR_PUBLIC;
188194
} else {
@@ -390,14 +396,16 @@ void mp_bluetooth_deinit(void) {
390396
return;
391397
}
392398

393-
mp_bluetooth_gap_advertise_stop();
394-
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
395-
mp_bluetooth_gap_scan_stop();
396-
#endif
397-
398399
// Must call ble_hs_stop() in a port-specific way to stop the background
399400
// task. Default implementation provided above.
400401
if (mp_bluetooth_nimble_ble_state == MP_BLUETOOTH_NIMBLE_BLE_STATE_ACTIVE) {
402+
mp_bluetooth_gap_advertise_stop();
403+
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
404+
mp_bluetooth_gap_scan_stop();
405+
#endif
406+
407+
DEBUG_printf("trying port shutdown\n");
408+
401409
mp_bluetooth_nimble_port_shutdown();
402410
assert(mp_bluetooth_nimble_ble_state == MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF);
403411
} else {

ports/esp32/boards/sdkconfig.ble

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ CONFIG_BT_NIMBLE_ENABLED=y
99
CONFIG_BT_NIMBLE_MAX_CONNECTIONS=4
1010

1111
# Pin to the same core as MP.
12-
CONFIG_BT_NIMBLE_PINNED_TO_CORE_0=n
13-
CONFIG_BT_NIMBLE_PINNED_TO_CORE_1=y
14-
CONFIG_BT_NIMBLE_PINNED_TO_CORE=1
12+
# Until we move to IDF 4.2+, we need NimBLE on core 0, and for synchronisation
13+
# with the ringbuffer and scheduler MP needs to be on the same core.
14+
# See https://github.com/micropython/micropython/issues/5489
15+
CONFIG_BT_NIMBLE_PINNED_TO_CORE_0=y
16+
CONFIG_BT_NIMBLE_PINNED_TO_CORE_1=n
17+
CONFIG_BT_NIMBLE_PINNED_TO_CORE=0
1518

1619
# v3.3-only (renamed in 4.0)
1720
CONFIG_BTDM_CONTROLLER_MODE_BLE_ONLY=y

ports/esp32/mphalport.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@
3535
#include "freertos/FreeRTOS.h"
3636
#include "freertos/task.h"
3737

38-
// The core that the MicroPython task(s) are pinned to
39-
#define MP_TASK_COREID (1)
38+
// The core that the MicroPython task(s) are pinned to.
39+
// Until we move to IDF 4.2+, we need NimBLE on core 0, and for synchronisation
40+
// with the ringbuffer and scheduler MP needs to be on the same core.
41+
// See https://github.com/micropython/micropython/issues/5489
42+
#define MP_TASK_COREID (0)
4043

4144
extern TaskHandle_t mp_main_task_handle;
4245

ports/esp32/mpnimbleport.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,43 @@
3030

3131
#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE
3232

33+
#define DEBUG_printf(...) // printf("nimble (esp32): " __VA_ARGS__)
34+
3335
#include "esp_nimble_hci.h"
3436
#include "nimble/nimble_port.h"
3537
#include "nimble/nimble_port_freertos.h"
3638

3739
#include "extmod/nimble/modbluetooth_nimble.h"
3840

3941
STATIC void ble_host_task(void *param) {
42+
DEBUG_printf("ble_host_task\n");
4043
nimble_port_run(); // This function will return only when nimble_port_stop() is executed.
4144
nimble_port_freertos_deinit();
4245
}
4346

4447
void mp_bluetooth_nimble_port_hci_init(void) {
48+
DEBUG_printf("mp_bluetooth_nimble_port_hci_init\n");
4549
esp_nimble_hci_and_controller_init();
4650
}
4751

4852
void mp_bluetooth_nimble_port_hci_deinit(void) {
53+
DEBUG_printf("mp_bluetooth_nimble_port_hci_deinit\n");
54+
4955
esp_nimble_hci_and_controller_deinit();
5056
}
5157

5258
void mp_bluetooth_nimble_port_start(void) {
59+
DEBUG_printf("mp_bluetooth_nimble_port_start\n");
5360
nimble_port_freertos_init(ble_host_task);
5461
}
5562

5663
void mp_bluetooth_nimble_port_shutdown(void) {
64+
DEBUG_printf("mp_bluetooth_nimble_port_shutdown\n");
65+
5766
// Despite the name, these is an ESP32-specific (no other NimBLE ports have these functions).
5867
// Calls ble_hs_stop() and waits for stack shutdown.
5968
nimble_port_stop();
69+
6070
// Shuts down the event queue.
6171
nimble_port_deinit();
6272

0 commit comments

Comments
 (0)
0