forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 4
Extmod bluetooth addr mode unix h4 reliability #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jimmo
wants to merge
18
commits into
master
Choose a base branch
from
extmod-bluetooth-addr-mode-unix-h4-reliability
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
0a07567
stm32/uart: Allow static IRQ handler registration.
jimmo 8f0ecf7
stm32/modbluetooth_hci.c: Use a static mp_irq_obj_t for HCI UART.
jimmo 9701004
extmod/modbluetooth: Rename logging macro.
jimmo 547d457
bluetooth: Refactor stack/hci/driver/port bindings.
jimmo 223c9b5
extmod/nimble: Set struct alignment correctly on 64-bit arch.
jimmo 9b5344b
extmod/nimble: Make nimble_malloc work with allocated size.
jimmo 895f64b
extmod/nimble: Implement NimBLE mutex.
jimmo 8bb3fde
ports/unix: Always enable -f*-sections.
jimmo 0fa6329
ports/unix: Implement BLE H4 HCI UART for btstack/nimble.
jimmo a5b7c51
extmod/modbluetooth: Implement configuration of address modes.
jimmo a776cf7
tests/multi_bluetooth: Update to new config('mac') behaviour.
jimmo eaaf1b6
docs/library/ubluetooth.rst: Add address modes.
jimmo aa85908
extmod/modbluetooth: Allow using mp_hal_get_mac as a static address.
jimmo 8353f69
extmod/bluetooth: Add btstack support for _IRQ_GATTS_READ_REQUEST.
pi-anl eea2e09
ports/stm32/mphciport.c: Increase UART timeout.
pi-anl 7578582
extmod/btstack: Detect HCI UART init failure.
jimmo 4b933a7
ports/esp32: Pin MP and NimBLE to core 0.
jimmo 010ef44
extmod/nimble: Add timeout for HCI sync on startup.
jimmo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
ports/unix: Implement BLE H4 HCI UART for btstack/nimble.
- Loading branch information
commit 0fa632975f0013bafc9a12d534231ff97b71aa47
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this duplicates the line just below