diff --git a/extmod/mpbthci.h b/extmod/mpbthci.h index acb5b832bab11..699723803357b 100644 --- a/extmod/mpbthci.h +++ b/extmod/mpbthci.h @@ -45,6 +45,7 @@ int mp_bluetooth_hci_controller_wakeup(void); int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate); int mp_bluetooth_hci_uart_deinit(void); int mp_bluetooth_hci_uart_set_baudrate(uint32_t baudrate); +int mp_bluetooth_hci_uart_any(void); int mp_bluetooth_hci_uart_readchar(void); int mp_bluetooth_hci_uart_write(const uint8_t *buf, size_t len); diff --git a/extmod/nimble/nimble.cmake b/extmod/nimble/nimble.cmake new file mode 100644 index 0000000000000..658beb9f2a8dc --- /dev/null +++ b/extmod/nimble/nimble.cmake @@ -0,0 +1,81 @@ +set(NIMBLE_LIB_DIR "${MICROPY_DIR}/lib/mynewt-nimble") +set(NIMBLE_EXTMOD_DIR "${MICROPY_DIR}/extmod/nimble") + +add_library(micropy_extmod_nimble INTERFACE) + +target_include_directories(micropy_extmod_nimble INTERFACE + ${MICROPY_DIR}/ + ${MICROPY_PORT_DIR}/ + ${NIMBLE_EXTMOD_DIR}/ + ${NIMBLE_LIB_DIR}/ + ${NIMBLE_LIB_DIR}/ext/tinycrypt/include + ${NIMBLE_LIB_DIR}/nimble/host/include + ${NIMBLE_LIB_DIR}/nimble/host/services/gap/include + ${NIMBLE_LIB_DIR}/nimble/host/services/gatt/include + ${NIMBLE_LIB_DIR}/nimble/host/store/ram/include + ${NIMBLE_LIB_DIR}/nimble/host/util/include + ${NIMBLE_LIB_DIR}/nimble/include + ${NIMBLE_LIB_DIR}/nimble/transport/uart/include + ${NIMBLE_LIB_DIR}/porting/nimble/include +) + +target_sources(micropy_extmod_nimble INTERFACE + ${NIMBLE_LIB_DIR}/ext/tinycrypt/src/aes_encrypt.c + ${NIMBLE_LIB_DIR}/ext/tinycrypt/src/cmac_mode.c + ${NIMBLE_LIB_DIR}/ext/tinycrypt/src/ecc.c + ${NIMBLE_LIB_DIR}/ext/tinycrypt/src/ecc_dh.c + ${NIMBLE_LIB_DIR}/ext/tinycrypt/src/utils.c + ${NIMBLE_LIB_DIR}/nimble/host/services/gap/src/ble_svc_gap.c + ${NIMBLE_LIB_DIR}/nimble/host/services/gatt/src/ble_svc_gatt.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_att.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_att_clt.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_att_cmd.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_att_svr.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_eddystone.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_gap.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_gattc.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_gatts.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_hs_adv.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_hs_atomic.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_hs.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_hs_cfg.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_hs_conn.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_hs_flow.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_hs_hci.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_hs_hci_cmd.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_hs_hci_evt.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_hs_hci_util.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_hs_id.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_hs_log.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_hs_mbuf.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_hs_misc.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_hs_mqueue.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_hs_pvcy.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_hs_startup.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_hs_stop.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_ibeacon.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_l2cap.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_l2cap_coc.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_l2cap_sig.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_l2cap_sig_cmd.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_monitor.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_sm_alg.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_sm.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_sm_cmd.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_sm_lgcy.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_sm_sc.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_store.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_store_util.c + ${NIMBLE_LIB_DIR}/nimble/host/src/ble_uuid.c + ${NIMBLE_LIB_DIR}/nimble/host/util/src/addr.c + ${NIMBLE_LIB_DIR}/nimble/transport/uart/src/ble_hci_uart.c + ${NIMBLE_LIB_DIR}/porting/nimble/src/endian.c + ${NIMBLE_LIB_DIR}/porting/nimble/src/mem.c + ${NIMBLE_LIB_DIR}/porting/nimble/src/nimble_port.c + ${NIMBLE_LIB_DIR}/porting/nimble/src/os_mbuf.c + ${NIMBLE_LIB_DIR}/porting/nimble/src/os_mempool.c + ${NIMBLE_LIB_DIR}/porting/nimble/src/os_msys_init.c + ${NIMBLE_EXTMOD_DIR}/nimble/nimble_npl_os.c + ${NIMBLE_EXTMOD_DIR}/hal/hal_uart.c + +) diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index 2e7347fd53055..40b10434b0146 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -159,6 +159,32 @@ set(PICO_SDK_COMPONENTS tinyusb_device ) +if(MICROPY_PY_BLUETOOTH) + list(APPEND MICROPY_SOURCE_PORT mpbthciport.c) + target_compile_definitions(${MICROPY_TARGET} PRIVATE + MICROPY_PY_BLUETOOTH=1 + MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE=1 + MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING=1 + MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS=1 + ) +endif() + +if(MICROPY_BLUETOOTH_NIMBLE) + list(APPEND MICROPY_SOURCE_PORT mpnimbleport.c) + target_compile_definitions(${MICROPY_TARGET} PRIVATE + MICROPY_BLUETOOTH_NIMBLE=1 + MICROPY_BLUETOOTH_NIMBLE_BINDINGS_ONLY=0 + ) + target_compile_options(${MICROPY_TARGET} PRIVATE + # TODO: This flag is currently needed to make nimble build. + -Wno-unused-but-set-variable + ) + include(${MICROPY_DIR}/extmod/nimble/nimble.cmake) + target_link_libraries(${MICROPY_TARGET} micropy_extmod_nimble) + get_target_property(NIMBLE_INCLUDE micropy_extmod_nimble INTERFACE_INCLUDE_DIRECTORIES) + list(APPEND MICROPY_INC_CORE ${NIMBLE_INCLUDE}) +endif() + # Define mpy-cross flags and frozen manifest set(MICROPY_CROSS_FLAGS -march=armv7m) if (NOT MICROPY_FROZEN_MANIFEST) diff --git a/ports/rp2/main.c b/ports/rp2/main.c index d0b89c8d6dc75..4bfed391de439 100644 --- a/ports/rp2/main.c +++ b/ports/rp2/main.c @@ -40,6 +40,10 @@ #include "modmachine.h" #include "modrp2.h" #include "genhdr/mpversion.h" +#if MICROPY_PY_BLUETOOTH +#include "extmod/modbluetooth.h" +#include "mpbthciport.h" +#endif #include "pico/stdlib.h" #include "pico/binary_info.h" @@ -107,6 +111,10 @@ int main(int argc, char **argv) { machine_pin_init(); rp2_pio_init(); + #if MICROPY_PY_BLUETOOTH + mp_bluetooth_hci_init(); + #endif + // Execute _boot.py to set up the filesystem. pyexec_frozen_module("_boot.py"); @@ -137,6 +145,9 @@ int main(int argc, char **argv) { soft_reset_exit: mp_printf(MP_PYTHON_PRINTER, "MPY: soft reboot\n"); rp2_pio_deinit(); + #if MICROPY_PY_BLUETOOTH + mp_bluetooth_deinit(); + #endif machine_pin_deinit(); #if MICROPY_PY_THREAD mp_thread_deinit(); diff --git a/ports/rp2/mpbthciport.c b/ports/rp2/mpbthciport.c new file mode 100644 index 0000000000000..1ebb3f178095c --- /dev/null +++ b/ports/rp2/mpbthciport.c @@ -0,0 +1,199 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 Ibrahim Abdelkader + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/stream.h" +#include "py/mphal.h" +#include "extmod/mpbthci.h" +#include "extmod/modbluetooth.h" +#include "modmachine.h" +#include "mpbthciport.h" +#include "pico/stdlib.h" + +#if MICROPY_PY_BLUETOOTH + +#define debug_printf(...) // mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__) +#define error_printf(...) mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__) + +// Poll timer ID. +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; + +void mp_bluetooth_hci_init(void) { + events_task_is_scheduled = false; +} + +STATIC void mp_bluetooth_hci_start_polling(void) { + events_task_is_scheduled = false; + mp_bluetooth_hci_poll_now(); +} + +static int64_t mp_bluetooth_hci_timer_callback(alarm_id_t id, void *user_data) { + poll_timer_id = 0; + mp_bluetooth_hci_poll_now(); + return 0; +} + +void mp_bluetooth_hci_poll_in_ms(uint32_t ms) { + poll_timer_id = add_alarm_in_ms(ms, mp_bluetooth_hci_timer_callback, NULL, true); +} + +// 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 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_obj_t mp_bthci_uart; + +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_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_bthci_uart = machine_uart_type.make_new((mp_obj_t)&machine_uart_type, 2, 2, args); + MP_STATE_PORT(mp_bthci_uart) = mp_bthci_uart; + + // Start the HCI polling to process any initial events/packets. + mp_bluetooth_hci_start_polling(); + return 0; +} + +int mp_bluetooth_hci_uart_deinit(void) { + debug_printf("mp_bluetooth_hci_uart_deinit\n"); + + // If a poll callback is set cancel it now. + if (poll_timer_id > 0) { + cancel_alarm(poll_timer_id); + } + poll_timer_id = 0; + return 0; +} + +int mp_bluetooth_hci_uart_set_baudrate(uint32_t baudrate) { + debug_printf("mp_bluetooth_hci_uart_set_baudrate(%lu)\n", baudrate); + return 0; +} + +int mp_bluetooth_hci_uart_any(void) { + int errcode = 0; + const mp_stream_p_t *proto = (mp_stream_p_t *)machine_uart_type.protocol; + + mp_uint_t ret = proto->ioctl(mp_bthci_uart, MP_STREAM_POLL, MP_STREAM_POLL_RD, &errcode); + if (errcode != 0) { + error_printf("Uart ioctl failed to poll UART %d\n", errcode); + return -1; + } + return ret & MP_STREAM_POLL_RD; +} + +int mp_bluetooth_hci_uart_write(const uint8_t *buf, size_t len) { + debug_printf("mp_bluetooth_hci_uart_write\n"); + + int errcode = 0; + const mp_stream_p_t *proto = (mp_stream_p_t *)machine_uart_type.protocol; + + mp_bluetooth_hci_controller_wakeup(); + + if (proto->write(mp_bthci_uart, (void *)buf, len, &errcode) < 0) { + error_printf("mp_bluetooth_hci_uart_write: failed to write to UART %d\n", errcode); + } + return 0; +} + +// This function expects the controller to be in the wake state via a previous call +// to mp_bluetooth_hci_controller_woken. +int mp_bluetooth_hci_uart_readchar(void) { + debug_printf("mp_bluetooth_hci_uart_readchar\n"); + if (mp_bluetooth_hci_uart_any()) { + int errcode = 0; + uint8_t buf = 0; + const mp_stream_p_t *proto = (mp_stream_p_t *)machine_uart_type.protocol; + if (proto->read(mp_bthci_uart, (void *)&buf, 1, &errcode) < 0) { + error_printf("mp_bluetooth_hci_uart_readchar: failed to read UART %d\n", errcode); + return -1; + } + return buf; + } else { + debug_printf("mp_bluetooth_hci_uart_readchar: not ready\n"); + return -1; + } +} + +// Default (weak) implementation of the HCI controller interface. +// A driver (e.g. cywbt43.c) can override these for controller-specific +// functionality (i.e. power management). +MP_WEAK int mp_bluetooth_hci_controller_init(void) { + debug_printf("mp_bluetooth_hci_controller_init (default)\n"); + return 0; +} + +MP_WEAK int mp_bluetooth_hci_controller_deinit(void) { + debug_printf("mp_bluetooth_hci_controller_deinit (default)\n"); + return 0; +} + +MP_WEAK int mp_bluetooth_hci_controller_sleep_maybe(void) { + debug_printf("mp_bluetooth_hci_controller_sleep_maybe (default)\n"); + return 0; +} + +MP_WEAK bool mp_bluetooth_hci_controller_woken(void) { + debug_printf("mp_bluetooth_hci_controller_woken (default)\n"); + return true; +} + +MP_WEAK int mp_bluetooth_hci_controller_wakeup(void) { + debug_printf("mp_bluetooth_hci_controller_wakeup (default)\n"); + return 0; +} +#endif // MICROPY_PY_BLUETOOTH diff --git a/ports/rp2/mpbthciport.h b/ports/rp2/mpbthciport.h new file mode 100644 index 0000000000000..ac5263aa11a8f --- /dev/null +++ b/ports/rp2/mpbthciport.h @@ -0,0 +1,42 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#ifndef MICROPY_INCLUDED_RP2_MPBTHCIPORT_H +#define MICROPY_INCLUDED_RP2_MPBTHCIPORT_H + +// Initialise the HCI subsystem (should be called once, early on). +void mp_bluetooth_hci_init(void); + +// Poll the HCI now, or after a certain timeout. +void mp_bluetooth_hci_poll_now(void); +void mp_bluetooth_hci_poll_in_ms(uint32_t ms); + +// Must be provided by the stack bindings (e.g. mpnimbleport.c or mpbtstackport.c). +// Request new data from the uart and pass to the stack, and run pending events/callouts. +// This is a low-level function and should not be called directly, use +// mp_bluetooth_hci_poll_now/mp_bluetooth_hci_poll_in_ms instead. +void mp_bluetooth_hci_poll(void); + +#endif // MICROPY_INCLUDED_RP2_MPBTHCIPORT_H diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index bb2f505b6e9d2..10d272e48ac99 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -171,6 +171,19 @@ extern const struct _mp_obj_module_t mp_module_rp2; extern const struct _mp_obj_module_t mp_module_uos; extern const struct _mp_obj_module_t mp_module_utime; +#if MICROPY_PY_BLUETOOTH +#define MICROPY_PORT_ROOT_POINTER_BLUETOOTH struct _machine_uart_obj_t *mp_bthci_uart; +#else +#define MICROPY_PORT_ROOT_POINTER_BLUETOOTH +#endif +#if MICROPY_BLUETOOTH_NIMBLE +struct _mp_bluetooth_nimble_root_pointers_t; +struct _mp_bluetooth_nimble_malloc_t; +#define MICROPY_PORT_ROOT_POINTER_BLUETOOTH_NIMBLE struct _mp_bluetooth_nimble_malloc_t *bluetooth_nimble_memory; struct _mp_bluetooth_nimble_root_pointers_t *bluetooth_nimble_root_pointers; +#else +#define MICROPY_PORT_ROOT_POINTER_BLUETOOTH_NIMBLE +#endif + #define MICROPY_PORT_BUILTIN_MODULES \ { MP_OBJ_NEW_QSTR(MP_QSTR_machine), (mp_obj_t)&mp_module_machine }, \ { MP_OBJ_NEW_QSTR(MP_QSTR__onewire), (mp_obj_t)&mp_module_onewire }, \ @@ -190,6 +203,8 @@ extern const struct _mp_obj_module_t mp_module_utime; void *rp2_uart_rx_buffer[2]; \ void *rp2_uart_tx_buffer[2]; \ MICROPY_BOARD_ROOT_POINTERS \ + MICROPY_PORT_ROOT_POINTER_BLUETOOTH \ + MICROPY_PORT_ROOT_POINTER_BLUETOOTH_NIMBLE \ #define MP_STATE_PORT MP_STATE_VM diff --git a/ports/rp2/mpnimbleport.c b/ports/rp2/mpnimbleport.c new file mode 100644 index 0000000000000..63c747284263e --- /dev/null +++ b/ports/rp2/mpnimbleport.c @@ -0,0 +1,78 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Jim Mussared + * Copyright (c) 2020 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#include "py/runtime.h" +#include "py/mperrno.h" +#include "py/mphal.h" +#include "py/stream.h" + +#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE +#define DEBUG_printf(...) // printf("mpnimbleport.c: " __VA_ARGS__) + +#include "host/ble_hs.h" +#include "nimble/nimble_npl.h" + +#include "extmod/mpbthci.h" +#include "extmod/modbluetooth.h" +#include "extmod/nimble/modbluetooth_nimble.h" +#include "extmod/nimble/hal/hal_uart.h" +#include "mpbthciport.h" + +// Get any pending data from the UART and send it to NimBLE's HCI buffers. +// Any further processing by NimBLE will be run via its event queue. +void mp_bluetooth_hci_poll(void) { + if (mp_bluetooth_nimble_ble_state >= MP_BLUETOOTH_NIMBLE_BLE_STATE_WAITING_FOR_SYNC) { + // DEBUG_printf("mp_bluetooth_hci_poll_uart %d\n", mp_bluetooth_nimble_ble_state); + + // Run any timers. + mp_bluetooth_nimble_os_callout_process(); + + // Process incoming UART data, and run events as they are generated. + mp_bluetooth_nimble_hci_uart_process(true); + + // Run any remaining events (e.g. if there was no UART data). + mp_bluetooth_nimble_os_eventq_run_all(); + } + + if (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF) { + // Call this function again in 128ms to check for new events. + // TODO: improve this by only calling back when needed. + mp_bluetooth_hci_poll_in_ms(128); + } +} + +// --- Port-specific helpers for the generic NimBLE bindings. ----------------- + +void mp_bluetooth_nimble_hci_uart_wfi(void) { + #if defined(__WFI) + __WFI(); + #endif + // This is called while NimBLE is waiting in ble_npl_sem_pend, i.e. waiting for an HCI ACK. + // Do not need to run events here (it must not invoke Python code), only processing incoming HCI data. + mp_bluetooth_nimble_hci_uart_process(false); +} + +#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE diff --git a/ports/rp2/mpnimbleport.h b/ports/rp2/mpnimbleport.h new file mode 100644 index 0000000000000..9f246b99329c4 --- /dev/null +++ b/ports/rp2/mpnimbleport.h @@ -0,0 +1,30 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2020 Jim Mussared + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_RP2_NIMBLE_PORT_H +#define MICROPY_INCLUDED_RP2_NIMBLE_PORT_H + +#endif // MICROPY_INCLUDED_RP2_NIMBLE_PORT_H