extmod/btstack: Detect HCI UART init failure.

This commit is contained in:
Jim Mussared 2020-08-18 11:05:34 +10:00 committed by Damien George
parent 6077c63a45
commit 99a29ec705
3 changed files with 19 additions and 6 deletions

View File

@ -50,6 +50,7 @@ STATIC uint8_t *recv_buf;
STATIC size_t recv_len; STATIC size_t recv_len;
STATIC size_t recv_idx; STATIC size_t recv_idx;
STATIC void (*recv_handler)(void); STATIC void (*recv_handler)(void);
STATIC bool init_success = false;
STATIC int btstack_uart_init(const btstack_uart_config_t *uart_config) { STATIC int btstack_uart_init(const btstack_uart_config_t *uart_config) {
(void)uart_config; (void)uart_config;
@ -62,14 +63,21 @@ STATIC int btstack_uart_init(const btstack_uart_config_t *uart_config) {
// Set up the UART peripheral, attach IRQ and power up the HCI controller. // Set up the UART peripheral, attach IRQ and power up the HCI controller.
// We haven't been told the baud rate yet, so defer that until btstack_uart_set_baudrate. // We haven't been told the baud rate yet, so defer that until btstack_uart_set_baudrate.
mp_bluetooth_hci_uart_init(MICROPY_HW_BLE_UART_ID, 0); if (mp_bluetooth_hci_uart_init(MICROPY_HW_BLE_UART_ID, 0)) {
mp_bluetooth_hci_controller_init(); init_success = false;
return -1;
}
if (mp_bluetooth_hci_controller_init()) {
init_success = false;
return -1;
}
init_success = true;
return 0; return 0;
} }
STATIC int btstack_uart_open(void) { STATIC int btstack_uart_open(void) {
return 0; return init_success ? 0 : 1;
} }
STATIC int btstack_uart_close(void) { STATIC int btstack_uart_close(void) {

View File

@ -317,6 +317,9 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint8_t *packet, uint8_t
// Signal that de-initialisation has completed. // Signal that de-initialisation has completed.
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF; mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF;
} }
} else if (event_type == BTSTACK_EVENT_POWERON_FAILED) {
// Signal that initialisation has failed.
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF;
} else if (event_type == HCI_EVENT_TRANSPORT_PACKET_SENT) { } else if (event_type == HCI_EVENT_TRANSPORT_PACKET_SENT) {
DEBUG_printf(" --> hci transport packet sent\n"); DEBUG_printf(" --> hci transport packet sent\n");
} else if (event_type == HCI_EVENT_COMMAND_COMPLETE) { } else if (event_type == HCI_EVENT_COMMAND_COMPLETE) {
@ -644,6 +647,8 @@ int mp_bluetooth_init(void) {
if (mp_bluetooth_btstack_state != MP_BLUETOOTH_BTSTACK_STATE_ACTIVE) { if (mp_bluetooth_btstack_state != MP_BLUETOOTH_BTSTACK_STATE_ACTIVE) {
DEBUG_printf("mp_bluetooth_init: stack startup timed out\n"); DEBUG_printf("mp_bluetooth_init: stack startup timed out\n");
bool timeout = mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_TIMEOUT;
// Required to stop the polling loop. // Required to stop the polling loop.
mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF; mp_bluetooth_btstack_state = MP_BLUETOOTH_BTSTACK_STATE_OFF;
// Attempt a shutdown (may not do anything). // Attempt a shutdown (may not do anything).
@ -652,7 +657,7 @@ int mp_bluetooth_init(void) {
// Clean up. // Clean up.
MP_STATE_PORT(bluetooth_btstack_root_pointers) = NULL; MP_STATE_PORT(bluetooth_btstack_root_pointers) = NULL;
return MP_ETIMEDOUT; return timeout ? MP_ETIMEDOUT : MP_EINVAL;
} }
DEBUG_printf("mp_bluetooth_init: stack startup complete\n"); DEBUG_printf("mp_bluetooth_init: stack startup complete\n");

View File

@ -124,12 +124,12 @@ int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) {
if (path != NULL) { if (path != NULL) {
strcpy(uart_device_name, path); strcpy(uart_device_name, path);
} }
DEBUG_printf("Using HCI UART: %s\n", uart_device_name); DEBUG_printf("mp_bluetooth_hci_uart_init: Using HCI UART: %s\n", uart_device_name);
int flags = O_RDWR | O_NOCTTY | O_NONBLOCK; int flags = O_RDWR | O_NOCTTY | O_NONBLOCK;
uart_fd = open(uart_device_name, flags); uart_fd = open(uart_device_name, flags);
if (uart_fd == -1) { if (uart_fd == -1) {
DEBUG_printf("Unable to open port %s", uart_device_name); printf("mp_bluetooth_hci_uart_init: Unable to open port %s\n", uart_device_name);
return -1; return -1;
} }