diff options
| author | Damien George | 2020-02-20 14:35:07 +1100 |
|---|---|---|
| committer | Damien George | 2020-03-10 01:53:42 +1100 |
| commit | 894c550c866211c9f176875d40c15fcf3bf74149 (patch) | |
| tree | 40c3f0f411c5065c06d8d38e4c126549fde8364d /extmod | |
| parent | c44d52f33e4ec626c7f6293323300baaeb55e856 (diff) | |
stm32: Refactor bluetooth stack/hci/driver bindings.
This makes a cleaner separation between the: driver, HCI UART and BT stack.
Also updated the naming to be more consistent (mp_bluetooth_hci_*).
Work done in collaboration with Jim Mussared aka @jimmo.
Diffstat (limited to 'extmod')
| -rw-r--r-- | extmod/modbluetooth_hci.h | 55 | ||||
| -rw-r--r-- | extmod/nimble/hal/hal_uart.c (renamed from extmod/nimble/nimble/hci_uart.c) | 27 | ||||
| -rw-r--r-- | extmod/nimble/hal/hal_uart.h | 1 | ||||
| -rw-r--r-- | extmod/nimble/nimble.mk | 2 | ||||
| -rw-r--r-- | extmod/nimble/nimble/nimble_hci_uart.h (renamed from extmod/nimble/nimble/hci_uart.h) | 23 | ||||
| -rw-r--r-- | extmod/nimble/nimble/npl_os.c | 4 |
6 files changed, 81 insertions, 31 deletions
diff --git a/extmod/modbluetooth_hci.h b/extmod/modbluetooth_hci.h new file mode 100644 index 000000000..6d44761a4 --- /dev/null +++ b/extmod/modbluetooth_hci.h @@ -0,0 +1,55 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 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. + */ + +#ifndef MICROPY_INCLUDED_EXTMOD_MODBLUETOOTH_HCI_H +#define MICROPY_INCLUDED_EXTMOD_MODBLUETOOTH_HCI_H + +#include "uart.h" + +// Optionally can be implemented by the driver. +int mp_bluetooth_hci_controller_init(void); +int mp_bluetooth_hci_controller_activate(void); +int mp_bluetooth_hci_controller_deactivate(void); + +// Tell the controller to go to sleep (e.g. on RX if we don't think we're expecting anything more). +int mp_bluetooth_hci_controller_sleep_maybe(void); +// True if the controller woke us up. +bool mp_bluetooth_hci_controller_woken(void); +// Wake up the controller (e.g. we're about to TX). +int mp_bluetooth_hci_controller_wakeup(void); + +// Storage and bindings that need to be implemented by the port. +// These are used by the stack bindings (e.g. nimble/hal_uart.c) +// as well as potentially the driver (e.g. cywbt.c). +extern uint8_t mp_bluetooth_hci_cmd_buf[4 + 256]; +extern pyb_uart_obj_t mp_bluetooth_hci_uart_obj; + +int mp_bluetooth_hci_uart_init(uint32_t port); +int mp_bluetooth_hci_uart_activate(void); +int mp_bluetooth_hci_uart_set_baudrate(uint32_t baudrate); +int mp_bluetooth_hci_uart_write(const uint8_t *buf, size_t len); + +#endif // MICROPY_INCLUDED_EXTMOD_MODBLUETOOTH_HCI_H diff --git a/extmod/nimble/nimble/hci_uart.c b/extmod/nimble/hal/hal_uart.c index b4ac4e738..fba82b830 100644 --- a/extmod/nimble/nimble/hci_uart.c +++ b/extmod/nimble/hal/hal_uart.c @@ -28,14 +28,11 @@ #include "py/mphal.h" #include "pin_static_af.h" #include "nimble/ble.h" -#include "hal/hal_uart.h" -#include "hci_uart.h" +#include "extmod/nimble/hal/hal_uart.h" +#include "extmod/modbluetooth_hci.h" +#include "extmod/nimble/nimble/nimble_hci_uart.h" -#if MICROPY_BLUETOOTH_NIMBLE - -/******************************************************************************/ -// Bindings Uart to Nimble -uint8_t bt_hci_cmd_buf[4 + 256]; +#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE static hal_uart_tx_cb_t hal_uart_tx_cb; static void *hal_uart_tx_arg; @@ -51,9 +48,9 @@ int hal_uart_init_cbs(uint32_t port, hal_uart_tx_cb_t tx_cb, void *tx_arg, hal_u } int hal_uart_config(uint32_t port, uint32_t baudrate, uint32_t bits, uint32_t stop, uint32_t parity, uint32_t flow) { - nimble_hci_uart_configure(port); - nimble_hci_uart_set_baudrate(baudrate); - return nimble_hci_uart_activate(); + mp_bluetooth_hci_uart_init(port); + mp_bluetooth_hci_uart_set_baudrate(baudrate); + return mp_bluetooth_hci_uart_activate(); } void hal_uart_start_tx(uint32_t port) { @@ -63,7 +60,7 @@ void hal_uart_start_tx(uint32_t port) { if (data == -1) { break; } - bt_hci_cmd_buf[len++] = data; + mp_bluetooth_hci_cmd_buf[len++] = data; } #if 0 @@ -74,15 +71,15 @@ void hal_uart_start_tx(uint32_t port) { printf("\n"); #endif - nimble_hci_uart_tx_strn((void*)bt_hci_cmd_buf, len); + mp_bluetooth_nimble_hci_uart_tx_strn((void*)mp_bluetooth_hci_cmd_buf, len); } int hal_uart_close(uint32_t port) { return 0; // success } -void nimble_uart_process(void) { - nimble_hci_uart_rx(hal_uart_rx_cb, hal_uart_rx_arg); +void mp_bluetooth_nimble_hci_uart_process(void) { + mp_bluetooth_nimble_hci_uart_rx(hal_uart_rx_cb, hal_uart_rx_arg); } -#endif // MICROPY_BLUETOOTH_NIMBLE +#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE diff --git a/extmod/nimble/hal/hal_uart.h b/extmod/nimble/hal/hal_uart.h index 294dfb50d..0ef04bc81 100644 --- a/extmod/nimble/hal/hal_uart.h +++ b/extmod/nimble/hal/hal_uart.h @@ -10,6 +10,7 @@ typedef int (*hal_uart_tx_cb_t)(void *arg); typedef int (*hal_uart_rx_cb_t)(void *arg, uint8_t data); +// Called by NimBLE, implemented in hal_uart.c. 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); int hal_uart_config(uint32_t port, uint32_t baud, uint32_t bits, uint32_t stop, uint32_t parity, uint32_t flow); void hal_uart_start_tx(uint32_t port); diff --git a/extmod/nimble/nimble.mk b/extmod/nimble/nimble.mk index 4d2c6637a..60dbe60b0 100644 --- a/extmod/nimble/nimble.mk +++ b/extmod/nimble/nimble.mk @@ -84,7 +84,7 @@ SRC_LIB += $(addprefix $(NIMBLE_LIB_DIR)/, \ EXTMOD_SRC_C += $(addprefix $(NIMBLE_EXTMOD_DIR)/, \ nimble/npl_os.c \ - nimble/hci_uart.c \ + hal/hal_uart.c \ ) INC += -I$(TOP)/$(NIMBLE_EXTMOD_DIR) diff --git a/extmod/nimble/nimble/hci_uart.h b/extmod/nimble/nimble/nimble_hci_uart.h index 3d4a2a983..646a12dac 100644 --- a/extmod/nimble/nimble/hci_uart.h +++ b/extmod/nimble/nimble/nimble_hci_uart.h @@ -23,21 +23,18 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_EXTMOD_NIMBLE_NIMBLE_HCI_UART_H -#define MICROPY_INCLUDED_EXTMOD_NIMBLE_NIMBLE_HCI_UART_H +#ifndef MICROPY_INCLUDED_EXTMOD_NIMBLE_NIMBLE_NIMBLE_HCI_UART_H +#define MICROPY_INCLUDED_EXTMOD_NIMBLE_NIMBLE_NIMBLE_HCI_UART_H -#include "extmod/nimble/hal/hal_uart.h" - -// To be implemented by the port. +// Extensions to extmod/modbluetooth_hci.h specific to NimBLE. -int nimble_hci_uart_configure(uint32_t port); - -// This will default to MICROPY_HW_BLE_UART_BAUDRATE, but can be updated later. -int nimble_hci_uart_set_baudrate(uint32_t baudrate); +#include "extmod/nimble/hal/hal_uart.h" -int nimble_hci_uart_activate(void); +// Helpers called from ports. +void mp_bluetooth_nimble_hci_uart_process(void); -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); +// Must be provided by the port. +void mp_bluetooth_nimble_hci_uart_rx(hal_uart_rx_cb_t rx_cb, void *rx_arg); +void mp_bluetooth_nimble_hci_uart_tx_strn(const char *str, uint len); -#endif // MICROPY_INCLUDED_EXTMOD_NIMBLE_NIMBLE_HCI_UART_H +#endif // MICROPY_INCLUDED_EXTMOD_NIMBLE_NIMBLE_NIMBLE_HCI_UART_H diff --git a/extmod/nimble/nimble/npl_os.c b/extmod/nimble/nimble/npl_os.c index 4875d2d1a..57ab689e6 100644 --- a/extmod/nimble/nimble/npl_os.c +++ b/extmod/nimble/nimble/npl_os.c @@ -29,6 +29,7 @@ #include "py/runtime.h" #include "nimble/ble.h" #include "nimble/nimble_npl.h" +#include "nimble/nimble_hci_uart.h" #define DEBUG_OS_printf(...) //printf(__VA_ARGS__) #define DEBUG_MALLOC_printf(...) //printf(__VA_ARGS__) @@ -234,8 +235,7 @@ ble_npl_error_t ble_npl_sem_pend(struct ble_npl_sem *sem, ble_npl_time_t timeout if (sem->count == 0) { uint32_t t0 = mp_hal_ticks_ms(); while (sem->count == 0 && mp_hal_ticks_ms() - t0 < timeout) { - extern void nimble_uart_process(void); - nimble_uart_process(); + mp_bluetooth_nimble_hci_uart_process(); if (sem->count != 0) { break; } |
