diff --git a/ports/stm32/mpbthciport.c b/ports/stm32/mpbthciport.c index a1ab3258b6..89b04a5405 100644 --- a/ports/stm32/mpbthciport.c +++ b/ports/stm32/mpbthciport.c @@ -42,20 +42,12 @@ uint8_t mp_bluetooth_hci_cmd_buf[4 + 256]; // Soft timer for scheduling a HCI poll. STATIC soft_timer_entry_t mp_bluetooth_hci_soft_timer; -#if MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS -// Prevent double-enqueuing of the scheduled task. -STATIC volatile bool events_task_is_scheduled; -#endif - // This is called by soft_timer and executes at IRQ_PRI_PENDSV. STATIC void mp_bluetooth_hci_soft_timer_callback(soft_timer_entry_t *self) { mp_bluetooth_hci_poll_now(); } void mp_bluetooth_hci_init(void) { - #if MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS - events_task_is_scheduled = false; - #endif soft_timer_static_init( &mp_bluetooth_hci_soft_timer, SOFT_TIMER_MODE_ONE_SHOT, @@ -65,9 +57,6 @@ void mp_bluetooth_hci_init(void) { } STATIC void mp_bluetooth_hci_start_polling(void) { - #if MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS - events_task_is_scheduled = false; - #endif mp_bluetooth_hci_poll_now(); } @@ -77,29 +66,21 @@ void mp_bluetooth_hci_poll_in_ms_default(uint32_t ms) { #if MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS +STATIC mp_sched_node_t mp_bluetooth_hci_sched_node; + // For synchronous mode, we run all BLE stack code inside a scheduled task. // This task is scheduled periodically via a soft timer, or // immediately on HCI UART RXIDLE. - -STATIC mp_obj_t run_events_scheduled_task(mp_obj_t none_in) { - (void)none_in; - events_task_is_scheduled = false; +STATIC void run_events_scheduled_task(mp_sched_node_t *node) { // This will process all buffered HCI UART data, and run any callouts or events. + (void)node; mp_bluetooth_hci_poll(); - return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(run_events_scheduled_task_obj, run_events_scheduled_task); // Called periodically (systick) or directly (e.g. UART RX IRQ) in order to // request that processing happens ASAP in the scheduler. void mp_bluetooth_hci_poll_now_default(void) { - if (!events_task_is_scheduled) { - events_task_is_scheduled = mp_sched_schedule(MP_OBJ_FROM_PTR(&run_events_scheduled_task_obj), mp_const_none); - if (!events_task_is_scheduled) { - // The schedule queue is full, set callback to try again soon. - mp_bluetooth_hci_poll_in_ms(5); - } - } + mp_sched_schedule_node(&mp_bluetooth_hci_sched_node, run_events_scheduled_task); } #else // !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index a52b2cc505..b1bf6da22a 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -75,6 +75,7 @@ #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) #endif #define MICROPY_USE_INTERNAL_ERRNO (1) +#define MICROPY_SCHEDULER_STATIC_NODES (1) #define MICROPY_SCHEDULER_DEPTH (8) #define MICROPY_VFS (1)