diff --git a/extmod/nimble/nimble/hci_uart.c b/extmod/nimble/nimble/hci_uart.c index 801291def4..b4ac4e7388 100644 --- a/extmod/nimble/nimble/hci_uart.c +++ b/extmod/nimble/nimble/hci_uart.c @@ -42,8 +42,6 @@ static void *hal_uart_tx_arg; static hal_uart_rx_cb_t hal_uart_rx_cb; static void *hal_uart_rx_arg; -static uint32_t bt_sleep_ticks; - int hal_uart_init_cbs(uint32_t port, hal_uart_tx_cb_t tx_cb, void *tx_arg, hal_uart_rx_cb_t rx_cb, void *rx_arg) { hal_uart_tx_cb = tx_cb; hal_uart_tx_arg = tx_arg; @@ -76,16 +74,6 @@ void hal_uart_start_tx(uint32_t port) { printf("\n"); #endif - bt_sleep_ticks = mp_hal_ticks_ms(); - - #ifdef pyb_pin_BT_DEV_WAKE - if (mp_hal_pin_read(pyb_pin_BT_DEV_WAKE) == 1) { - //printf("BT WAKE for TX\n"); - mp_hal_pin_low(pyb_pin_BT_DEV_WAKE); // wake up - mp_hal_delay_ms(5); // can't go lower than this - } - #endif - nimble_hci_uart_tx_strn((void*)bt_hci_cmd_buf, len); } @@ -94,29 +82,7 @@ int hal_uart_close(uint32_t port) { } void nimble_uart_process(void) { - int host_wake = 0; - #ifdef pyb_pin_BT_HOST_WAKE - host_wake = mp_hal_pin_read(pyb_pin_BT_HOST_WAKE); - #endif - /* - // this is just for info/tracing purposes - static int last_host_wake = 0; - if (host_wake != last_host_wake) { - printf("HOST_WAKE change %d -> %d\n", last_host_wake, host_wake); - last_host_wake = host_wake; - } - */ - while (nimble_hci_uart_rx_any()) { - uint8_t data = nimble_hci_uart_rx_char(); - //printf("UART RX: %02x\n", data); - hal_uart_rx_cb(hal_uart_rx_arg, data); - } - if (host_wake == 1 && mp_hal_pin_read(pyb_pin_BT_DEV_WAKE) == 0) { - if (mp_hal_ticks_ms() - bt_sleep_ticks > 500) { - //printf("BT SLEEP\n"); - mp_hal_pin_high(pyb_pin_BT_DEV_WAKE); // let sleep - } - } + nimble_hci_uart_rx(hal_uart_rx_cb, hal_uart_rx_arg); } #endif // MICROPY_BLUETOOTH_NIMBLE diff --git a/extmod/nimble/nimble/hci_uart.h b/extmod/nimble/nimble/hci_uart.h index e184210830..3d4a2a9835 100644 --- a/extmod/nimble/nimble/hci_uart.h +++ b/extmod/nimble/nimble/hci_uart.h @@ -26,6 +26,8 @@ #ifndef MICROPY_INCLUDED_EXTMOD_NIMBLE_NIMBLE_HCI_UART_H #define MICROPY_INCLUDED_EXTMOD_NIMBLE_NIMBLE_HCI_UART_H +#include "extmod/nimble/hal/hal_uart.h" + // To be implemented by the port. int nimble_hci_uart_configure(uint32_t port); @@ -35,8 +37,7 @@ int nimble_hci_uart_set_baudrate(uint32_t baudrate); int nimble_hci_uart_activate(void); -mp_uint_t nimble_hci_uart_rx_any(); -int nimble_hci_uart_rx_char(); +void nimble_hci_uart_rx(hal_uart_rx_cb_t rx_cb, void *rx_arg); void nimble_hci_uart_tx_strn(const char *str, uint len); #endif // MICROPY_INCLUDED_EXTMOD_NIMBLE_NIMBLE_HCI_UART_H diff --git a/ports/stm32/nimble_hci_uart.c b/ports/stm32/nimble_hci_uart.c index 104a64b732..251f15e3b4 100644 --- a/ports/stm32/nimble_hci_uart.c +++ b/ports/stm32/nimble_hci_uart.c @@ -28,6 +28,7 @@ #include "py/mphal.h" #include "uart.h" #include "pendsv.h" +#include "extmod/nimble/nimble/hci_uart.h" #include "drivers/cyw43/cywbt.h" #if MICROPY_BLUETOOTH_NIMBLE @@ -37,6 +38,10 @@ pyb_uart_obj_t bt_hci_uart_obj; static uint8_t hci_uart_rxbuf[512]; +#ifdef pyb_pin_BT_DEV_WAKE +static uint32_t bt_sleep_ticks; +#endif + extern void nimble_poll(void); mp_obj_t mp_uart_interrupt(mp_obj_t self_in) { @@ -81,15 +86,47 @@ int nimble_hci_uart_activate(void) { return 0; } -mp_uint_t nimble_hci_uart_rx_any() { - return uart_rx_any(&bt_hci_uart_obj); -} +void nimble_hci_uart_rx(hal_uart_rx_cb_t rx_cb, void *rx_arg) { + #ifdef pyb_pin_BT_HOST_WAKE + int host_wake = 0; + host_wake = mp_hal_pin_read(pyb_pin_BT_HOST_WAKE); + /* + // this is just for info/tracing purposes + static int last_host_wake = 0; + if (host_wake != last_host_wake) { + printf("HOST_WAKE change %d -> %d\n", last_host_wake, host_wake); + last_host_wake = host_wake; + } + */ + #endif -int nimble_hci_uart_rx_char() { - return uart_rx_char(&bt_hci_uart_obj); + while (uart_rx_any(&bt_hci_uart_obj)) { + uint8_t data = uart_rx_char(&bt_hci_uart_obj); + //printf("UART RX: %02x\n", data); + rx_cb(rx_arg, data); + } + + #ifdef pyb_pin_BT_DEV_WAKE + if (host_wake == 1 && mp_hal_pin_read(pyb_pin_BT_DEV_WAKE) == 0) { + if (mp_hal_ticks_ms() - bt_sleep_ticks > 500) { + //printf("BT SLEEP\n"); + mp_hal_pin_high(pyb_pin_BT_DEV_WAKE); // let sleep + } + } + #endif } void nimble_hci_uart_tx_strn(const char *str, uint len) { + #ifdef pyb_pin_BT_DEV_WAKE + bt_sleep_ticks = mp_hal_ticks_ms(); + + if (mp_hal_pin_read(pyb_pin_BT_DEV_WAKE) == 1) { + //printf("BT WAKE for TX\n"); + mp_hal_pin_low(pyb_pin_BT_DEV_WAKE); // wake up + mp_hal_delay_ms(5); // can't go lower than this + } + #endif + uart_tx_strn(&bt_hci_uart_obj, str, len); }