From d72dbb822c96c003a202565006a97a2bfdff9210 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Sun, 29 Sep 2019 23:04:58 +1000 Subject: [PATCH] stm32: Provide port-specific implementation for Nimble on STM32. --- .travis.yml | 2 +- ports/stm32/Makefile | 9 ++++ ports/stm32/main.c | 11 ++++ ports/stm32/modnetwork.c | 5 ++ ports/stm32/mpconfigport.h | 9 +++- ports/stm32/nimble_hci_uart.c | 96 +++++++++++++++++++++++++++++++++++ ports/stm32/pendsv.h | 5 +- ports/stm32/systick.h | 3 ++ 8 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 ports/stm32/nimble_hci_uart.c diff --git a/.travis.yml b/.travis.yml index 708329aa12..a72f3bc1a3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -32,7 +32,7 @@ jobs: - sudo apt-get install libnewlib-arm-none-eabi - arm-none-eabi-gcc --version script: - - git submodule update --init lib/lwip lib/mbedtls lib/stm32lib + - git submodule update --init lib/lwip lib/mbedtls lib/stm32lib lib/mynewt-nimble - make ${MAKEOPTS} -C mpy-cross - make ${MAKEOPTS} -C ports/stm32 BOARD=NUCLEO_F091RC - make ${MAKEOPTS} -C ports/stm32 BOARD=PYBV11 MICROPY_PY_WIZNET5K=5200 MICROPY_PY_CC3K=1 diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 8dde851297..0e3b4f8e33 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -428,6 +428,15 @@ CFLAGS_MOD += -DMBEDTLS_CONFIG_FILE='"mbedtls/mbedtls_config.h"' SRC_MOD += mbedtls/mbedtls_port.c endif +ifeq ($(MICROPY_BLUETOOTH_NIMBLE),1) +include $(TOP)/extmod/nimble/nimble.mk +SRC_C += nimble_hci_uart.c +EXTMOD_SRC_C += extmod/modbluetooth_nimble.c +ifeq ($(MICROPY_PY_NETWORK_CYW43),1) +DRIVERS_SRC_C += drivers/cyw43/cywbt.c +endif +endif + OBJ = OBJ += $(PY_O) OBJ += $(addprefix $(BUILD)/, $(SRC_LIB:.c=.o)) diff --git a/ports/stm32/main.c b/ports/stm32/main.c index 48ad3b8be4..2da3626f12 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -43,6 +43,10 @@ #include "drivers/cyw43/cyw43.h" #endif +#if MICROPY_BLUETOOTH_NIMBLE +#include "extmod/modbluetooth.h" +#endif + #include "mpu.h" #include "systick.h" #include "pendsv.h" @@ -500,6 +504,10 @@ void stm32_main(uint32_t reset_mode) { #endif systick_enable_dispatch(SYSTICK_DISPATCH_LWIP, mod_network_lwip_poll_wrapper); #endif + #if MICROPY_BLUETOOTH_NIMBLE + extern void mod_bluetooth_nimble_poll_wrapper(uint32_t ticks_ms); + systick_enable_dispatch(SYSTICK_DISPATCH_NIMBLE, mod_bluetooth_nimble_poll_wrapper); + #endif #if MICROPY_PY_NETWORK_CYW43 { @@ -729,6 +737,9 @@ soft_reset_exit: #endif printf("MPY: soft reboot\n"); + #if MICROPY_BLUETOOTH_NIMBLE + mp_bluetooth_deinit(); + #endif #if MICROPY_PY_NETWORK mod_network_deinit(); #endif diff --git a/ports/stm32/modnetwork.c b/ports/stm32/modnetwork.c index 13ecf444f0..19a60103f8 100644 --- a/ports/stm32/modnetwork.c +++ b/ports/stm32/modnetwork.c @@ -63,6 +63,11 @@ STATIC void pyb_lwip_poll(void) { // Run the lwIP internal updates sys_check_timeouts(); + + #if MICROPY_BLUETOOTH_NIMBLE + extern void nimble_poll(void); + nimble_poll(); + #endif } void mod_network_lwip_poll_wrapper(uint32_t ticks_ms) { diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 20554d9e62..b9c6cdedd7 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -289,6 +289,12 @@ extern const struct _mp_obj_module_t mp_module_onewire; #define MICROPY_PORT_ROOT_POINTER_MBEDTLS #endif +#if MICROPY_BLUETOOTH_NIMBLE +#define MICROPY_PORT_ROOT_POINTER_BLUETOOTH_NIMBLE void **bluetooth_nimble_memory; +#else +#define MICROPY_PORT_ROOT_POINTER_BLUETOOTH_NIMBLE +#endif + #define MICROPY_PORT_ROOT_POINTERS \ const char *readline_hist[8]; \ \ @@ -318,7 +324,8 @@ extern const struct _mp_obj_module_t mp_module_onewire; /* list of registered NICs */ \ mp_obj_list_t mod_network_nic_list; \ \ - MICROPY_PORT_ROOT_POINTER_MBEDTLS + MICROPY_PORT_ROOT_POINTER_MBEDTLS \ + MICROPY_PORT_ROOT_POINTER_BLUETOOTH_NIMBLE \ // type definitions for the specific machine diff --git a/ports/stm32/nimble_hci_uart.c b/ports/stm32/nimble_hci_uart.c new file mode 100644 index 0000000000..104a64b732 --- /dev/null +++ b/ports/stm32/nimble_hci_uart.c @@ -0,0 +1,96 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018-2019 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/mphal.h" +#include "uart.h" +#include "pendsv.h" +#include "drivers/cyw43/cywbt.h" + +#if MICROPY_BLUETOOTH_NIMBLE + +/******************************************************************************/ +// UART +pyb_uart_obj_t bt_hci_uart_obj; +static uint8_t hci_uart_rxbuf[512]; + +extern void nimble_poll(void); + +mp_obj_t mp_uart_interrupt(mp_obj_t self_in) { + pendsv_schedule_dispatch(PENDSV_DISPATCH_NIMBLE, nimble_poll); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(mp_uart_interrupt_obj, mp_uart_interrupt); + +int nimble_hci_uart_set_baudrate(uint32_t baudrate) { + uart_init(&bt_hci_uart_obj, baudrate, UART_WORDLENGTH_8B, UART_PARITY_NONE, UART_STOPBITS_1, UART_HWCONTROL_RTS | UART_HWCONTROL_CTS); + uart_set_rxbuf(&bt_hci_uart_obj, sizeof(hci_uart_rxbuf), hci_uart_rxbuf); + return 0; +} + +int nimble_hci_uart_configure(uint32_t port) { + // bits (8), stop (1), parity (none) and flow (rts/cts) are assumed to match MYNEWT_VAL_BLE_HCI_UART_ constants in syscfg.h. + bt_hci_uart_obj.base.type = &pyb_uart_type; + bt_hci_uart_obj.uart_id = port; + bt_hci_uart_obj.is_static = true; + bt_hci_uart_obj.timeout = 2; + bt_hci_uart_obj.timeout_char = 2; + MP_STATE_PORT(pyb_uart_obj_all)[bt_hci_uart_obj.uart_id - 1] = &bt_hci_uart_obj; + return 0; +} + +int nimble_hci_uart_activate(void) { + // Interrupt on RX chunk received (idle) + // Trigger nimble poll when this happens + mp_obj_t uart_irq_fn = mp_load_attr(&bt_hci_uart_obj, MP_QSTR_irq); + mp_obj_t uargs[] = { + MP_OBJ_FROM_PTR(&mp_uart_interrupt_obj), + MP_OBJ_NEW_SMALL_INT(UART_FLAG_IDLE), + mp_const_true, + }; + mp_call_function_n_kw(uart_irq_fn, 3, 0, uargs); + + #if MICROPY_PY_NETWORK_CYW43 + cywbt_init(); + cywbt_activate(); + #endif + + return 0; +} + +mp_uint_t nimble_hci_uart_rx_any() { + return uart_rx_any(&bt_hci_uart_obj); +} + +int nimble_hci_uart_rx_char() { + return uart_rx_char(&bt_hci_uart_obj); +} + +void nimble_hci_uart_tx_strn(const char *str, uint len) { + uart_tx_strn(&bt_hci_uart_obj, str, len); +} + +#endif // MICROPY_BLUETOOTH_NIMBLE diff --git a/ports/stm32/pendsv.h b/ports/stm32/pendsv.h index 6cbfe8b2ec..9851a5ece7 100644 --- a/ports/stm32/pendsv.h +++ b/ports/stm32/pendsv.h @@ -33,10 +33,13 @@ enum { PENDSV_DISPATCH_CYW43, #endif #endif + #if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE + PENDSV_DISPATCH_NIMBLE, + #endif PENDSV_DISPATCH_MAX }; -#if MICROPY_PY_NETWORK && MICROPY_PY_LWIP +#if (MICROPY_PY_NETWORK && MICROPY_PY_LWIP) || (MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE) #define PENDSV_DISPATCH_NUM_SLOTS PENDSV_DISPATCH_MAX #endif diff --git a/ports/stm32/systick.h b/ports/stm32/systick.h index 6a05a4990a..a70f03e176 100644 --- a/ports/stm32/systick.h +++ b/ports/stm32/systick.h @@ -37,6 +37,9 @@ enum { #if MICROPY_PY_NETWORK && MICROPY_PY_LWIP SYSTICK_DISPATCH_LWIP, #endif + #if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE + SYSTICK_DISPATCH_NIMBLE, + #endif SYSTICK_DISPATCH_MAX };