From 6abe3e1714ca32524358e1e1ddde16872d7d7cea Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Mon, 10 Apr 2023 15:07:12 +0200 Subject: [PATCH 1/5] rp2/mpbthciport: Switch to static scheduler nodes. Signed-off-by: iabdalkader --- ports/rp2/mpbthciport.c | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/ports/rp2/mpbthciport.c b/ports/rp2/mpbthciport.c index d5b3c6073ade2..9a24ebdc32a9a 100644 --- a/ports/rp2/mpbthciport.c +++ b/ports/rp2/mpbthciport.c @@ -43,11 +43,9 @@ static alarm_id_t poll_timer_id = 0; uint8_t mp_bluetooth_hci_cmd_buf[4 + 256]; -// Prevent double-enqueuing of the scheduled task. -STATIC volatile bool events_task_is_scheduled; +static mp_sched_node_t mp_bluetooth_hci_sched_node; void mp_bluetooth_hci_init(void) { - events_task_is_scheduled = false; } static int64_t mp_bluetooth_hci_timer_callback(alarm_id_t id, void *user_data) { @@ -65,25 +63,16 @@ void mp_bluetooth_hci_poll_in_ms(uint32_t ms) { // For synchronous mode, we run all BLE stack code inside a scheduled task. // This task is scheduled periodically via a timer, or immediately after UART RX IRQ. -STATIC mp_obj_t run_events_scheduled_task(mp_obj_t none_in) { - (void)none_in; - events_task_is_scheduled = false; - // This will process all HCI data, and run any callouts or events. +STATIC void run_events_scheduled_task(mp_sched_node_t *node) { + (void)node; + // This will process all buffered HCI UART data, and run any callouts or events. 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(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); } #if defined(MICROPY_HW_BLE_UART_ID) @@ -91,7 +80,6 @@ void mp_bluetooth_hci_poll_now(void) { mp_obj_t mp_bthci_uart; STATIC void mp_bluetooth_hci_start_polling(void) { - events_task_is_scheduled = false; mp_bluetooth_hci_poll_now(); } From 1976781d33547d32c72971e8a9d6bb598d6fa93e Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Mon, 10 Apr 2023 18:16:38 +0200 Subject: [PATCH 2/5] rp2/mpbthciport: Fix HCI UART config. Fixes are: - The baudrate argument is a keyword arg, it was passed before as a positional arg. - Use the port and baudrate arguments passed from higher level code instead of the hard-coded port ID and baudrate, which would allow HCI drivers to change baudrates. - Increase UART char timeout and RX buffer size. Signed-off-by: iabdalkader --- ports/rp2/mpbthciport.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ports/rp2/mpbthciport.c b/ports/rp2/mpbthciport.c index 9a24ebdc32a9a..7722360920916 100644 --- a/ports/rp2/mpbthciport.c +++ b/ports/rp2/mpbthciport.c @@ -87,16 +87,18 @@ int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) { debug_printf("mp_bluetooth_hci_uart_init\n"); mp_obj_t args[] = { - MP_OBJ_NEW_SMALL_INT(MICROPY_HW_BLE_UART_ID), - MP_OBJ_NEW_SMALL_INT(MICROPY_HW_BLE_UART_BAUDRATE), + MP_OBJ_NEW_SMALL_INT(port), + MP_OBJ_NEW_QSTR(MP_QSTR_baudrate), MP_OBJ_NEW_SMALL_INT(baudrate), MP_OBJ_NEW_QSTR(MP_QSTR_flow), MP_OBJ_NEW_SMALL_INT((1 | 2)), MP_OBJ_NEW_QSTR(MP_QSTR_timeout), MP_OBJ_NEW_SMALL_INT(1000), + MP_OBJ_NEW_QSTR(MP_QSTR_timeout_char), MP_OBJ_NEW_SMALL_INT(200), + MP_OBJ_NEW_QSTR(MP_QSTR_rxbuf), MP_OBJ_NEW_SMALL_INT(768), }; // This is a statically-allocated UART (see machine_uart.c), and doesn't // contain any heap pointers other than the ringbufs (which are already // root pointers), so no need to track this as a root pointer. - mp_bthci_uart = MP_OBJ_TYPE_GET_SLOT(&machine_uart_type, make_new)((mp_obj_t)&machine_uart_type, 2, 2, args); + mp_bthci_uart = MP_OBJ_TYPE_GET_SLOT(&machine_uart_type, make_new)((mp_obj_t)&machine_uart_type, 1, 5, args); // Start the HCI polling to process any initial events/packets. mp_bluetooth_hci_start_polling(); From 9ea9e04ef63897c8e8349813cfc65d29d7f20078 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Mon, 10 Apr 2023 19:11:53 +0200 Subject: [PATCH 3/5] rp2/mpconfigport: Disable BLE locking when MICROPY_PY_BLUETOOTH enabled. Bluetooth code runs in the scheduler, so no locking/mutex is required. Signed-off-by: iabdalkader --- ports/rp2/mpconfigport.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index df44831662443..056e2df4fe203 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -283,7 +283,7 @@ extern uint32_t rosc_random_u32(void); extern void lwip_lock_acquire(void); extern void lwip_lock_release(void); -#if MICROPY_PY_BLUETOOTH_CYW43 +#if MICROPY_PY_BLUETOOTH || MICROPY_PY_BLUETOOTH_CYW43 // Bluetooth code only runs in the scheduler, no locking/mutex required. #define MICROPY_PY_BLUETOOTH_ENTER uint32_t atomic_state = 0; #define MICROPY_PY_BLUETOOTH_EXIT (void)atomic_state; From d30f61ba0d533f5889d036b24423521e8a2b77a4 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Tue, 11 Apr 2023 13:06:58 +0200 Subject: [PATCH 4/5] drivers/ninaw10/nina_bt_hci: Make some minor fixes to HCI driver. Fixes are: - Reset the module first before changing GPIO1 direction. - Skip spurious bytes received after reset. - Use HCI UART ID and baudrate when reinitializing UART. - Disable all printf output which causes unit-tests to fail. Signed-off-by: iabdalkader --- drivers/ninaw10/nina_bt_hci.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/drivers/ninaw10/nina_bt_hci.c b/drivers/ninaw10/nina_bt_hci.c index 6dc3204471b0e..754e99ed76b45 100644 --- a/drivers/ninaw10/nina_bt_hci.c +++ b/drivers/ninaw10/nina_bt_hci.c @@ -50,7 +50,7 @@ #define OCF_SET_EVENT_MASK (0x0001) #define OCF_RESET (0x0003) -#define error_printf(...) mp_printf(&mp_plat_print, "nina_bt_hci.c: " __VA_ARGS__) +#define error_printf(...) // mp_printf(&mp_plat_print, "nina_bt_hci.c: " __VA_ARGS__) #define debug_printf(...) // mp_printf(&mp_plat_print, "nina_bt_hci.c: " __VA_ARGS__) // Provided by the port, and also possibly shared with the stack. @@ -86,7 +86,7 @@ static int nina_hci_cmd(int ogf, int ocf, size_t param_len, const uint8_t *param buf[i] = mp_bluetooth_hci_uart_readchar(); // There seems to be a sync issue with this fw/module. - if (i == 0 && buf[0] == 0xFF) { + if (i == 0 && (buf[0] == 0xFF || buf[0] == 0xFE)) { continue; } @@ -121,19 +121,19 @@ static int nina_hci_cmd(int ogf, int ocf, size_t param_len, const uint8_t *param int mp_bluetooth_hci_controller_init(void) { // This is called immediately after the UART is initialised during stack initialisation. - mp_hal_pin_output(MICROPY_HW_NINA_GPIO1); mp_hal_pin_output(MICROPY_HW_NINA_RESET); + mp_hal_pin_write(MICROPY_HW_NINA_RESET, 0); + mp_hal_pin_output(MICROPY_HW_NINA_GPIO1); mp_hal_pin_write(MICROPY_HW_NINA_GPIO1, 0); - mp_hal_pin_write(MICROPY_HW_NINA_RESET, 0); - mp_hal_delay_ms(100); + mp_hal_delay_ms(150); mp_hal_pin_write(MICROPY_HW_NINA_RESET, 1); mp_hal_delay_ms(750); - // The UART must be re-initialize here because the GPIO1/RX pin is used initially + // The UART must be re-initialized here because the GPIO1/RX pin is used initially // to reset the module in Bluetooth mode. This will change back the pin to UART RX. - mp_bluetooth_hci_uart_init(0, 0); + mp_bluetooth_hci_uart_init(MICROPY_HW_BLE_UART_ID, MICROPY_HW_BLE_UART_BAUDRATE); // Send reset command return nina_hci_cmd(OGF_HOST_CTL, OCF_RESET, 0, NULL); From 5473200aab492d64866511d96b6f4a7d4cb41393 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Tue, 11 Apr 2023 20:59:56 +0200 Subject: [PATCH 5/5] rp2/boards/ARDUINO_NANO_RP2040_CONNECT: Use standard HCI UART baudrate. Signed-off-by: iabdalkader --- ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/mpconfigboard.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/mpconfigboard.h b/ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/mpconfigboard.h index 98b9c0f6faa72..026702da4519e 100644 --- a/ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/mpconfigboard.h +++ b/ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/mpconfigboard.h @@ -39,7 +39,7 @@ // Bluetooth config. #define MICROPY_HW_BLE_UART_ID (1) -#define MICROPY_HW_BLE_UART_BAUDRATE (119600) +#define MICROPY_HW_BLE_UART_BAUDRATE (115200) // WiFi/NINA-W10 config. #define MICROPY_HW_WIFI_SPI_ID (1)