unix: Implement BLE H4 HCI UART for btstack/nimble.
This commit adds support for using Bluetooth on the unix port via a H4 serial interface (distinct from a USB dongle), with both BTstack and NimBLE Bluetooth stacks. Note that MICROPY_PY_BLUETOOTH is now disabled for the coverage variant. Prior to this commit Bluetooth was anyway not being built on Travis because libusb was not detected. But now that bluetooth works in H4 mode it will be built, and will lead to a large decrease in coverage because Bluetooth tests cannot be run on Travis.
This commit is contained in:
parent
feed69aa5c
commit
1b1b22905e
@ -30,10 +30,18 @@ SRC_BTSTACK = \
|
||||
$(addprefix lib/btstack/src/ble/, $(filter-out %_tlv.c, $(SRC_BLE_FILES))) \
|
||||
lib/btstack/platform/embedded/btstack_run_loop_embedded.c
|
||||
|
||||
ifeq ($(MICROPY_BLUETOOTH_BTSTACK_USB),1)
|
||||
ifeq ($(MICROPY_BLUETOOTH_BTSTACK_H4),1)
|
||||
$(error Cannot specifiy both MICROPY_BLUETOOTH_BTSTACK_USB and MICROPY_BLUETOOTH_BTSTACK_H4)
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(MICROPY_BLUETOOTH_BTSTACK_USB),1)
|
||||
SRC_BTSTACK += \
|
||||
lib/btstack/platform/libusb/hci_transport_h2_libusb.c
|
||||
|
||||
CFLAGS_MOD += -DMICROPY_BLUETOOTH_BTSTACK_USB=1
|
||||
|
||||
CFLAGS += $(shell pkg-config libusb-1.0 --cflags)
|
||||
LDFLAGS += $(shell pkg-config libusb-1.0 --libs)
|
||||
endif
|
||||
|
@ -134,24 +134,57 @@ CFLAGS_MOD += -DMICROPY_PY_THREAD=1 -DMICROPY_PY_THREAD_GIL=0
|
||||
LDFLAGS_MOD += $(LIBPTHREAD)
|
||||
endif
|
||||
|
||||
# If the variant enables it and we have libusb, enable BTStack support for USB adaptors.
|
||||
# If the variant enables it, enable modbluetooth.
|
||||
ifeq ($(MICROPY_PY_BLUETOOTH),1)
|
||||
|
||||
HAVE_LIBUSB := $(shell (which pkg-config > /dev/null && pkg-config --exists libusb-1.0) 2>/dev/null && echo '1')
|
||||
ifeq ($(HAVE_LIBUSB),1)
|
||||
|
||||
# Only one stack can be enabled.
|
||||
ifeq ($(MICROPY_BLUETOOTH_NIMBLE),1)
|
||||
ifeq ($(MICROPY_BLUETOOTH_BTSTACK),1)
|
||||
$(error Cannot enable both NimBLE and BTstack at the same time)
|
||||
endif
|
||||
endif
|
||||
|
||||
# Default to btstack, but a variant (or make command line) can set NimBLE
|
||||
# explicitly (which is always via H4 UART).
|
||||
ifneq ($(MICROPY_BLUETOOTH_NIMBLE),1)
|
||||
ifneq ($(MICROPY_BLUETOOTH_BTSTACK),1)
|
||||
MICROPY_BLUETOOTH_BTSTACK ?= 1
|
||||
endif
|
||||
endif
|
||||
|
||||
CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH=1
|
||||
CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE=1
|
||||
# Runs in a thread, cannot make calls into the VM.
|
||||
CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_GATTS_ON_READ_CALLBACK=0
|
||||
|
||||
MICROPY_BLUETOOTH_BTSTACK ?= 1
|
||||
MICROPY_BLUETOOTH_BTSTACK_USB ?= 1
|
||||
|
||||
ifeq ($(MICROPY_BLUETOOTH_BTSTACK),1)
|
||||
|
||||
# Figure out which BTstack transport to use.
|
||||
ifeq ($(MICROPY_BLUETOOTH_BTSTACK_H4),1)
|
||||
ifeq ($(MICROPY_BLUETOOTH_BTSTACK_USB),1)
|
||||
$(error Cannot enable BTstack support for USB and H4 UART at the same time)
|
||||
endif
|
||||
else
|
||||
ifeq ($(HAVE_LIBUSB),1)
|
||||
# Default to btstack-over-usb.
|
||||
MICROPY_BLUETOOTH_BTSTACK_USB ?= 1
|
||||
else
|
||||
# Fallback to HCI controller via a H4 UART (e.g. Zephyr on nRF) over a /dev/tty serial port.
|
||||
MICROPY_BLUETOOTH_BTSTACK_H4 ?= 1
|
||||
endif
|
||||
endif
|
||||
|
||||
# BTstack is enabled.
|
||||
GIT_SUBMODULES += lib/btstack
|
||||
include $(TOP)/extmod/btstack/btstack.mk
|
||||
endif
|
||||
|
||||
else
|
||||
|
||||
# NimBLE is enabled.
|
||||
GIT_SUBMODULES += lib/mynewt-nimble
|
||||
include $(TOP)/extmod/nimble/nimble.mk
|
||||
|
||||
endif
|
||||
|
||||
@ -201,7 +234,9 @@ SRC_C = \
|
||||
alloc.c \
|
||||
coverage.c \
|
||||
fatfs_port.c \
|
||||
mpbthciport.c \
|
||||
mpbtstackport_common.c \
|
||||
mpbtstackport_h4.c \
|
||||
mpbtstackport_usb.c \
|
||||
mpnimbleport.c \
|
||||
$(SRC_MOD) \
|
||||
|
219
ports/unix/mpbthciport.c
Normal file
219
ports/unix/mpbthciport.c
Normal file
@ -0,0 +1,219 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "py/runtime.h"
|
||||
#include "py/mperrno.h"
|
||||
#include "py/mphal.h"
|
||||
|
||||
#if MICROPY_PY_BLUETOOTH && (MICROPY_BLUETOOTH_NIMBLE || (MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_H4))
|
||||
|
||||
#if !MICROPY_PY_THREAD
|
||||
#error Unix HCI UART requires MICROPY_PY_THREAD
|
||||
#endif
|
||||
|
||||
#include "extmod/modbluetooth.h"
|
||||
#include "extmod/mpbthci.h"
|
||||
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <termios.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define DEBUG_printf(...) // printf(__VA_ARGS__)
|
||||
#define DEBUG_HCI_DUMP (0)
|
||||
|
||||
uint8_t mp_bluetooth_hci_cmd_buf[4 + 256];
|
||||
|
||||
// Must be provided by the stack bindings (e.g. mpnimbleport.c or mpbtstackport.c).
|
||||
extern bool mp_bluetooth_hci_poll(void);
|
||||
|
||||
STATIC const useconds_t UART_POLL_INTERVAL_US = 1000;
|
||||
|
||||
STATIC int uart_fd = -1;
|
||||
STATIC pthread_t hci_poll_thread_id;
|
||||
|
||||
STATIC void *hci_poll_thread(void *arg) {
|
||||
(void)arg;
|
||||
|
||||
// This will return false when the stack is shutdown.
|
||||
while (mp_bluetooth_hci_poll()) {
|
||||
usleep(UART_POLL_INTERVAL_US);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
STATIC int configure_uart(void) {
|
||||
struct termios toptions;
|
||||
|
||||
// Get existing config.
|
||||
if (tcgetattr(uart_fd, &toptions) < 0) {
|
||||
DEBUG_printf("Couldn't get term attributes");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Raw mode (disable all processing).
|
||||
cfmakeraw(&toptions);
|
||||
|
||||
// 8N1, no parity.
|
||||
toptions.c_cflag &= ~CSTOPB;
|
||||
toptions.c_cflag |= CS8;
|
||||
toptions.c_cflag &= ~PARENB;
|
||||
|
||||
// Enable receiver, ignore modem control lines
|
||||
toptions.c_cflag |= CREAD | CLOCAL;
|
||||
|
||||
// Blocking, single-byte reads.
|
||||
toptions.c_cc[VMIN] = 1;
|
||||
toptions.c_cc[VTIME] = 0;
|
||||
|
||||
// Enable HW RTS/CTS flow control.
|
||||
toptions.c_iflag &= ~(IXON | IXOFF | IXANY);
|
||||
toptions.c_cflag |= CRTSCTS;
|
||||
|
||||
// 1Mbit (TODO: make this configurable).
|
||||
speed_t brate = B1000000;
|
||||
cfsetospeed(&toptions, brate);
|
||||
cfsetispeed(&toptions, brate);
|
||||
|
||||
// Apply immediately.
|
||||
if (tcsetattr(uart_fd, TCSANOW, &toptions) < 0) {
|
||||
DEBUG_printf("Couldn't set term attributes");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// HCI UART bindings.
|
||||
int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) {
|
||||
(void)port;
|
||||
(void)baudrate;
|
||||
|
||||
DEBUG_printf("mp_bluetooth_hci_uart_init (unix)\n");
|
||||
|
||||
char uart_device_name[256] = "/dev/ttyUSB0";
|
||||
|
||||
char *path = getenv("MICROPYBTUART");
|
||||
if (path != NULL) {
|
||||
strcpy(uart_device_name, path);
|
||||
}
|
||||
DEBUG_printf("Using HCI UART: %s\n", uart_device_name);
|
||||
|
||||
int flags = O_RDWR | O_NOCTTY | O_NONBLOCK;
|
||||
uart_fd = open(uart_device_name, flags);
|
||||
if (uart_fd == -1) {
|
||||
DEBUG_printf("Unable to open port %s", uart_device_name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (configure_uart()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Create a thread to run the polling loop.
|
||||
pthread_attr_t attr;
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
||||
pthread_create(&hci_poll_thread_id, &attr, &hci_poll_thread, NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mp_bluetooth_hci_uart_deinit(void) {
|
||||
DEBUG_printf("mp_bluetooth_hci_uart_deinit\n");
|
||||
|
||||
// Wait for the poll loop to terminate when the state is set to OFF.
|
||||
pthread_join(hci_poll_thread_id, NULL);
|
||||
|
||||
// Close the UART.
|
||||
close(uart_fd);
|
||||
uart_fd = -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mp_bluetooth_hci_uart_set_baudrate(uint32_t baudrate) {
|
||||
(void)baudrate;
|
||||
DEBUG_printf("mp_bluetooth_hci_uart_set_baudrate\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mp_bluetooth_hci_uart_readchar(void) {
|
||||
// DEBUG_printf("mp_bluetooth_hci_uart_readchar\n");
|
||||
|
||||
uint8_t c;
|
||||
ssize_t bytes_read = read(uart_fd, &c, 1);
|
||||
|
||||
if (bytes_read == 1) {
|
||||
#if DEBUG_HCI_DUMP
|
||||
printf("[% 8ld] RX: %02x\n", mp_hal_ticks_ms(), c);
|
||||
#endif
|
||||
return c;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int mp_bluetooth_hci_uart_write(const uint8_t *buf, size_t len) {
|
||||
// DEBUG_printf("mp_bluetooth_hci_uart_write\n");
|
||||
|
||||
#if DEBUG_HCI_DUMP
|
||||
printf("[% 8ld] TX: %02x", mp_hal_ticks_ms(), buf[0]);
|
||||
for (size_t i = 1; i < len; ++i) {
|
||||
printf(":%02x", buf[i]);
|
||||
}
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
return write(uart_fd, buf, len);
|
||||
}
|
||||
|
||||
// No-op implementations of HCI controller interface.
|
||||
int mp_bluetooth_hci_controller_init(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mp_bluetooth_hci_controller_deinit(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mp_bluetooth_hci_controller_sleep_maybe(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool mp_bluetooth_hci_controller_woken(void) {
|
||||
return true;
|
||||
}
|
||||
|
||||
int mp_bluetooth_hci_controller_wakeup(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif // MICROPY_PY_BLUETOOTH && (MICROPY_BLUETOOTH_NIMBLE || (MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_H4))
|
@ -32,6 +32,11 @@
|
||||
|
||||
bool mp_bluetooth_hci_poll(void);
|
||||
|
||||
#if MICROPY_BLUETOOTH_BTSTACK_H4
|
||||
void mp_bluetooth_hci_poll_h4(void);
|
||||
void mp_bluetooth_btstack_port_init_h4(void);
|
||||
#endif
|
||||
|
||||
#if MICROPY_BLUETOOTH_BTSTACK_USB
|
||||
void mp_bluetooth_btstack_port_init_usb(void);
|
||||
#endif
|
||||
|
@ -45,6 +45,9 @@ bool mp_bluetooth_hci_poll(void) {
|
||||
if (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_STARTING || mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_ACTIVE || mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_HALTING) {
|
||||
// Pretend like we're running in IRQ context (i.e. other things can't be running at the same time).
|
||||
mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
|
||||
#if MICROPY_BLUETOOTH_BTSTACK_H4
|
||||
mp_bluetooth_hci_poll_h4();
|
||||
#endif
|
||||
btstack_run_loop_embedded_execute_once();
|
||||
MICROPY_END_ATOMIC_SECTION(atomic_state);
|
||||
|
||||
@ -81,6 +84,10 @@ void mp_bluetooth_btstack_port_init(void) {
|
||||
|
||||
// hci_dump_open(NULL, HCI_DUMP_STDOUT);
|
||||
|
||||
#if MICROPY_BLUETOOTH_BTSTACK_H4
|
||||
mp_bluetooth_btstack_port_init_h4();
|
||||
#endif
|
||||
|
||||
#if MICROPY_BLUETOOTH_BTSTACK_USB
|
||||
mp_bluetooth_btstack_port_init_usb();
|
||||
#endif
|
||||
|
80
ports/unix/mpbtstackport_h4.c
Normal file
80
ports/unix/mpbtstackport_h4.c
Normal file
@ -0,0 +1,80 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#include "py/runtime.h"
|
||||
#include "py/mperrno.h"
|
||||
#include "py/mphal.h"
|
||||
|
||||
#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_H4
|
||||
|
||||
#include "lib/btstack/chipset/zephyr/btstack_chipset_zephyr.h"
|
||||
|
||||
#include "extmod/btstack/btstack_hci_uart.h"
|
||||
#include "extmod/btstack/modbluetooth_btstack.h"
|
||||
|
||||
#include "mpbtstackport.h"
|
||||
|
||||
#define DEBUG_printf(...) // printf(__VA_ARGS__)
|
||||
|
||||
STATIC hci_transport_config_uart_t hci_transport_config_uart = {
|
||||
HCI_TRANSPORT_CONFIG_UART,
|
||||
1000000, // initial baudrate
|
||||
0, // main baudrate
|
||||
1, // flow control
|
||||
NULL, // device name
|
||||
};
|
||||
|
||||
void mp_bluetooth_hci_poll_h4(void) {
|
||||
if (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_STARTING || mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_ACTIVE) {
|
||||
mp_bluetooth_btstack_hci_uart_process();
|
||||
}
|
||||
}
|
||||
|
||||
void mp_bluetooth_btstack_port_init_h4(void) {
|
||||
DEBUG_printf("mp_bluetooth_btstack_port_init_h4\n");
|
||||
|
||||
const hci_transport_t *transport = hci_transport_h4_instance(&mp_bluetooth_btstack_hci_uart_block);
|
||||
hci_init(transport, &hci_transport_config_uart);
|
||||
|
||||
hci_set_chipset(btstack_chipset_zephyr_instance());
|
||||
}
|
||||
|
||||
void mp_bluetooth_btstack_port_deinit(void) {
|
||||
DEBUG_printf("mp_bluetooth_btstack_port_deinit\n");
|
||||
|
||||
hci_power_control(HCI_POWER_OFF);
|
||||
hci_close();
|
||||
}
|
||||
|
||||
void mp_bluetooth_btstack_port_start(void) {
|
||||
DEBUG_printf("mp_bluetooth_btstack_port_start\n");
|
||||
|
||||
hci_power_control(HCI_POWER_ON);
|
||||
}
|
||||
|
||||
#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK && MICROPY_BLUETOOTH_BTSTACK_H4
|
@ -314,6 +314,11 @@ void mp_unix_mark_exec(void);
|
||||
struct _mp_bluetooth_btstack_root_pointers_t;
|
||||
#define MICROPY_BLUETOOTH_ROOT_POINTERS struct _mp_bluetooth_btstack_root_pointers_t *bluetooth_btstack_root_pointers;
|
||||
#endif
|
||||
#if MICROPY_BLUETOOTH_NIMBLE
|
||||
struct _mp_bluetooth_nimble_root_pointers_t;
|
||||
struct _mp_bluetooth_nimble_malloc_t;
|
||||
#define MICROPY_BLUETOOTH_ROOT_POINTERS struct _mp_bluetooth_nimble_malloc_t *bluetooth_nimble_memory; struct _mp_bluetooth_nimble_root_pointers_t *bluetooth_nimble_root_pointers;
|
||||
#endif
|
||||
#else
|
||||
#define MICROPY_BLUETOOTH_ROOT_POINTERS
|
||||
#endif
|
||||
@ -353,7 +358,7 @@ struct _mp_bluetooth_btstack_root_pointers_t;
|
||||
#endif
|
||||
|
||||
#if MICROPY_PY_THREAD
|
||||
#define MICROPY_BEGIN_ATOMIC_SECTION() (mp_thread_unix_begin_atomic_section(), 0)
|
||||
#define MICROPY_BEGIN_ATOMIC_SECTION() (mp_thread_unix_begin_atomic_section(), 0xffffffff)
|
||||
#define MICROPY_END_ATOMIC_SECTION(x) (void)x; mp_thread_unix_end_atomic_section()
|
||||
#endif
|
||||
|
||||
|
84
ports/unix/mpnimbleport.c
Normal file
84
ports/unix/mpnimbleport.c
Normal file
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "py/runtime.h"
|
||||
#include "py/mperrno.h"
|
||||
#include "py/mphal.h"
|
||||
|
||||
#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE
|
||||
|
||||
#include "nimble/nimble_npl.h"
|
||||
|
||||
#include "extmod/nimble/modbluetooth_nimble.h"
|
||||
#include "extmod/nimble/hal/hal_uart.h"
|
||||
|
||||
#define DEBUG_printf(...) // printf(__VA_ARGS__)
|
||||
|
||||
// Called by the UART polling thread in mpbthciport.c.
|
||||
bool mp_bluetooth_hci_poll(void) {
|
||||
// DEBUG_printf("mp_bluetooth_hci_poll (unix nimble) %d\n", mp_bluetooth_nimble_ble_state);
|
||||
|
||||
if (mp_bluetooth_nimble_ble_state == MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF) {
|
||||
DEBUG_printf("mp_bluetooth_hci_poll (unix nimble) -- shutdown\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mp_bluetooth_nimble_ble_state >= MP_BLUETOOTH_NIMBLE_BLE_STATE_WAITING_FOR_SYNC) {
|
||||
|
||||
// Pretend like we're running in IRQ context (i.e. other things can't be running at the same time).
|
||||
mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
|
||||
|
||||
// Ask NimBLE to process UART data.
|
||||
mp_bluetooth_nimble_hci_uart_process();
|
||||
|
||||
// Run pending background operations and events, but only after HCI sync.
|
||||
mp_bluetooth_nimble_os_callout_process();
|
||||
mp_bluetooth_nimble_os_eventq_run_all();
|
||||
|
||||
MICROPY_END_ATOMIC_SECTION(atomic_state);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Extra port-specific helpers.
|
||||
void mp_bluetooth_nimble_hci_uart_wfi(void) {
|
||||
// DEBUG_printf("mp_bluetooth_nimble_hci_uart_wfi\n");
|
||||
// TODO: this should do a select() on the uart_fd.
|
||||
}
|
||||
|
||||
uint32_t mp_bluetooth_nimble_hci_uart_enter_critical(void) {
|
||||
// DEBUG_printf("mp_bluetooth_nimble_hci_uart_enter_critical\n");
|
||||
MICROPY_PY_BLUETOOTH_ENTER
|
||||
return atomic_state; // Always 0xffffffff
|
||||
}
|
||||
|
||||
void mp_bluetooth_nimble_hci_uart_exit_critical(uint32_t atomic_state) {
|
||||
MICROPY_PY_BLUETOOTH_EXIT
|
||||
// DEBUG_printf("mp_bluetooth_nimble_hci_uart_exit_critical\n");
|
||||
}
|
||||
|
||||
#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE
|
33
ports/unix/mpnimbleport.h
Normal file
33
ports/unix/mpnimbleport.h
Normal file
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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_UNIX_NIMBLE_PORT_H
|
||||
#define MICROPY_INCLUDED_UNIX_NIMBLE_PORT_H
|
||||
|
||||
#define MICROPY_HW_BLE_UART_ID (0)
|
||||
#define MICROPY_HW_BLE_UART_BAUDRATE (1000000)
|
||||
|
||||
#endif // MICROPY_INCLUDED_UNIX_NIMBLE_PORT_H
|
@ -17,4 +17,3 @@ MICROPY_ROM_TEXT_COMPRESSION = 1
|
||||
MICROPY_VFS_FAT = 1
|
||||
MICROPY_VFS_LFS1 = 1
|
||||
MICROPY_VFS_LFS2 = 1
|
||||
MICROPY_PY_BLUETOOTH = 1
|
||||
|
@ -6,4 +6,5 @@ MICROPY_ROM_TEXT_COMPRESSION = 1
|
||||
MICROPY_VFS_FAT = 1
|
||||
MICROPY_VFS_LFS1 = 1
|
||||
MICROPY_VFS_LFS2 = 1
|
||||
MICROPY_PY_BLUETOOTH = 1
|
||||
|
||||
MICROPY_PY_BLUETOOTH ?= 1
|
||||
|
Loading…
Reference in New Issue
Block a user