rp2/mpbthciport: Rework HCI polling timer to use soft_timer.

Signed-off-by: Damien George <damien@micropython.org>
pull/12901/head
Damien George 2023-11-14 13:23:46 +11:00
rodzic c9a9b2e682
commit 633c604722
2 zmienionych plików z 18 dodań i 20 usunięć

Wyświetl plik

@ -30,36 +30,37 @@
#include "extmod/modbluetooth.h"
#include "extmod/modmachine.h"
#include "extmod/mpbthci.h"
#include "shared/runtime/softtimer.h"
#include "modmachine.h"
#include "mpbthciport.h"
#include "pico/stdlib.h"
#if MICROPY_PY_BLUETOOTH
#define debug_printf(...) // mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__)
#define error_printf(...) mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__)
// Poll timer ID.
static alarm_id_t poll_timer_id = 0;
uint8_t mp_bluetooth_hci_cmd_buf[4 + 256];
// Soft timer and scheduling node for scheduling a HCI poll.
static soft_timer_entry_t mp_bluetooth_hci_soft_timer;
static mp_sched_node_t mp_bluetooth_hci_sched_node;
void mp_bluetooth_hci_init(void) {
// This is called by soft_timer and executes at PendSV level.
static void mp_bluetooth_hci_soft_timer_callback(soft_timer_entry_t *self) {
mp_bluetooth_hci_poll_now();
}
static int64_t mp_bluetooth_hci_timer_callback(alarm_id_t id, void *user_data) {
poll_timer_id = 0;
mp_bluetooth_hci_poll_now();
return 0;
void mp_bluetooth_hci_init(void) {
soft_timer_static_init(
&mp_bluetooth_hci_soft_timer,
SOFT_TIMER_MODE_ONE_SHOT,
0,
mp_bluetooth_hci_soft_timer_callback
);
}
void mp_bluetooth_hci_poll_in_ms(uint32_t ms) {
if (poll_timer_id != 0) {
cancel_alarm(poll_timer_id);
}
poll_timer_id = add_alarm_in_ms(ms, mp_bluetooth_hci_timer_callback, NULL, true);
soft_timer_reinsert(&mp_bluetooth_hci_soft_timer, ms);
}
// For synchronous mode, we run all BLE stack code inside a scheduled task.
@ -110,10 +111,8 @@ int mp_bluetooth_hci_uart_deinit(void) {
debug_printf("mp_bluetooth_hci_uart_deinit\n");
// If a poll callback is set cancel it now.
if (poll_timer_id > 0) {
cancel_alarm(poll_timer_id);
}
poll_timer_id = 0;
soft_timer_remove(&mp_bluetooth_hci_soft_timer);
return 0;
}

Wyświetl plik

@ -69,9 +69,8 @@ void mp_bluetooth_hci_poll(void) {
// --- Port-specific helpers for the generic NimBLE bindings. -----------------
void mp_bluetooth_nimble_hci_uart_wfi(void) {
#if defined(__WFI)
__WFI();
#endif
best_effort_wfe_or_timeout(make_timeout_time_ms(1));
// 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 (it must not invoke Python code), only processing incoming HCI data.
mp_bluetooth_nimble_hci_uart_process(false);