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); 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) diff --git a/ports/rp2/mpbthciport.c b/ports/rp2/mpbthciport.c index d5b3c6073ade2..7722360920916 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(); } @@ -99,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(); 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;