aboutsummaryrefslogtreecommitdiff
path: root/ports/unix/mpnimbleport.c
diff options
context:
space:
mode:
authorJim Mussared2020-11-03 23:27:47 +1100
committerDamien George2020-11-13 17:19:05 +1100
commit61d1e4b01b1bf77e5ca478e18065f0691ae274a7 (patch)
treef7b5d2b0d460473e5ff87ece96272073a5ce02e7 /ports/unix/mpnimbleport.c
parent81e92d3d6e1a605a6115821ac24dcbc2546ba0f9 (diff)
extmod/nimble: Make stm32 and unix NimBLE ports use synchronous events.
This changes stm32 from using PENDSV to run NimBLE to use the MicroPython scheduler instead. This allows Python BLE callbacks to be invoked directly (and therefore synchronously) rather than via the ringbuffer. The NimBLE UART HCI and event processing now happens in a scheduled task every 128ms. When RX IRQ idle events arrive, it will also schedule this task to improve latency. There is a similar change for the unix port where the background thread now queues the scheduled task. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Diffstat (limited to 'ports/unix/mpnimbleport.c')
-rw-r--r--ports/unix/mpnimbleport.c34
1 files changed, 12 insertions, 22 deletions
diff --git a/ports/unix/mpnimbleport.c b/ports/unix/mpnimbleport.c
index 896191009..29f558f74 100644
--- a/ports/unix/mpnimbleport.c
+++ b/ports/unix/mpnimbleport.c
@@ -47,38 +47,28 @@ bool mp_bluetooth_hci_poll(void) {
}
if (mp_bluetooth_nimble_ble_state >= MP_BLUETOOTH_NIMBLE_BLE_STATE_WAITING_FOR_SYNC) {
+ // Run any timers.
+ mp_bluetooth_nimble_os_callout_process();
- // Pretend like we're running in IRQ context (i.e. other things can't be running at the same time).
- mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
-
- // Ask NimBLE to process UART data.
- mp_bluetooth_nimble_hci_uart_process();
+ // Process incoming UART data, and run events as they are generated.
+ mp_bluetooth_nimble_hci_uart_process(true);
- // Run pending background operations and events, but only after HCI sync.
- mp_bluetooth_nimble_os_callout_process();
+ // Run any remaining events (e.g. if there was no UART data).
mp_bluetooth_nimble_os_eventq_run_all();
-
- MICROPY_END_ATOMIC_SECTION(atomic_state);
}
return true;
}
-// Extra port-specific helpers.
-void mp_bluetooth_nimble_hci_uart_wfi(void) {
- // DEBUG_printf("mp_bluetooth_nimble_hci_uart_wfi\n");
- // TODO: this should do a select() on the uart_fd.
-}
-
-uint32_t mp_bluetooth_nimble_hci_uart_enter_critical(void) {
- // DEBUG_printf("mp_bluetooth_nimble_hci_uart_enter_critical\n");
- MICROPY_PY_BLUETOOTH_ENTER
- return atomic_state; // Always 0xffffffff
+bool mp_bluetooth_hci_active(void) {
+ return mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF;
}
-void mp_bluetooth_nimble_hci_uart_exit_critical(uint32_t atomic_state) {
- MICROPY_PY_BLUETOOTH_EXIT
- // DEBUG_printf("mp_bluetooth_nimble_hci_uart_exit_critical\n");
+// Extra port-specific helpers.
+void mp_bluetooth_nimble_hci_uart_wfi(void) {
+ // This is called while NimBLE is waiting in ble_npl_sem_pend, i.e. waiting for an HCI ACK.
+ // Do not need to run events here, only processing incoming HCI data.
+ mp_bluetooth_nimble_hci_uart_process(false);
}
#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE