8000 ports/rp2: Bluetooth HCI fixes. by iabdalkader · Pull Request #11234 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

ports/rp2: Bluetooth HCI fixes. #11234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions drivers/ninaw10/nina_bt_hci.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 10 additions & 20 deletions ports/rp2/mpbthciport.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -65,50 +63,42 @@ 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)

mp_obj_t mp_bthci_uart;

STATIC void mp_bluetooth_hci_start_polling(void) {
events_task_is_scheduled = false;
mp_bluetooth_hci_poll_now();
}

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();
Expand Down
2 changes: 1 addition & 1 deletion ports/rp2/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ extern uint32_t rosc_random_u32(void);
extern void lwip_lock_ 5362 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;
Expand Down
0