hci skeleton done; not working yet

This commit is contained in:
Dan Halbert 2020-06-30 23:19:40 -04:00
parent f879114c43
commit 11cb3e3b4b
7 changed files with 587 additions and 549 deletions

View File

@ -31,6 +31,8 @@
#include <stdio.h>
#include <string.h>
#include "hci.h"
#include "py/gc.h"
#include "py/mphal.h"
#include "py/objstr.h"
@ -178,10 +180,10 @@ char default_ble_name[] = { 'C', 'I', 'R', 'C', 'U', 'I', 'T', 'P', 'Y', 0, 0, 0
// }
void common_hal_bleio_adapter_hci_init(bleio_adapter_obj_t *self, const mcu_pin_obj_t *tx, const mcu_pin_obj_t *rx, const mcu_pin_obj_t *rts, const mcu_pin_obj_t *cts, uint32_t baudrate, uint32_t buffer_size) {
self->tx = tx;
self->rx = rx;
self->rts = rts;
self->cts = cts;
self->tx_pin = tx;
self->rx_pin = rx;
self->rts_pin = rts;
self->cts_pin = cts;
self->baudrate = baudrate;
self->buffer_size = buffer_size;
self->enabled = false;
@ -195,6 +197,35 @@ void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enable
return;
}
if (enabled) {
common_hal_busio_uart_construct(
&self->hci_uart,
self->tx_pin, // tx pin
self->rx_pin, // rx pin
NULL, // rts pin
NULL, // cts pin
NULL, // rs485 dir pin
false, // rs485 invert
0, // timeout
self->baudrate, // baudrate
8, // nbits
PARITY_NONE, // parity
1, // stop bits
self->buffer_size, // buffer size
NULL, // buffer
false // sigint_enabled
);
common_hal_digitalio_digitalinout_construct(&self->rts_digitalinout, self->rts_pin);
common_hal_digitalio_digitalinout_construct(&self->cts_digitalinout, self->cts_pin);
hci_init(self);
} else {
common_hal_busio_uart_deinit(&self->hci_uart);
common_hal_digitalio_digitalinout_deinit(&self->rts_digitalinout);
common_hal_digitalio_digitalinout_deinit(&self->cts_digitalinout);
}
//FIX enable/disable HCI adapter, but don't reset it, since we don't know how.
self->enabled = enabled;
}
@ -206,13 +237,14 @@ bool common_hal_bleio_adapter_get_enabled(bleio_adapter_obj_t *self) {
bleio_address_obj_t *common_hal_bleio_adapter_get_address(bleio_adapter_obj_t *self) {
common_hal_bleio_adapter_set_enabled(self, true);
// ble_gap_addr_t local_address;
// get_address(self, &local_address);
uint8_t addr[6];
hci_readBdAddr(addr);
bleio_address_obj_t *address = m_new_obj(bleio_address_obj_t);
address->base.type = &bleio_address_type;
// common_hal_bleio_address_construct(address, local_address.addr, local_address.addr_type);
// 0 is the type designating a public address.
common_hal_bleio_address_construct(address, addr, 0);
return address;
}

View File

@ -52,15 +52,15 @@ typedef struct {
bleio_scanresults_obj_t* scan_results;
mp_obj_t name;
mp_obj_tuple_t *connection_objs;
const mcu_pin_obj_t* tx;
const mcu_pin_obj_t* rx;
const mcu_pin_obj_t* rts;
const mcu_pin_obj_t* cts;
const mcu_pin_obj_t* tx_pin;
const mcu_pin_obj_t* rx_pin;
const mcu_pin_obj_t* rts_pin;
const mcu_pin_obj_t* cts_pin;
uint32_t baudrate;
uint16_t buffer_size;
busio_uart_obj_t hci_uart;
digitalio_digitalinout_obj_t rts_digitalio;
digitalio_digitalinout_obj_t cts_digitalio;
digitalio_digitalinout_obj_t rts_digitalinout;
digitalio_digitalinout_obj_t cts_digitalinout;
bool enabled;
} bleio_adapter_obj_t;

File diff suppressed because it is too large Load Diff

View File

@ -248,7 +248,7 @@ endif
SRC_ASF := $(addprefix asf4/$(CHIP_FAMILY)/, $(SRC_ASF))
SRC_C = \
SRC_C += \
audio_dma.c \
background.c \
bindings/samd/Clock.c \

View File

@ -318,8 +318,8 @@ SRC_COMMON_HAL_ALL = \
watchdog/__init__.c \
ifeq ($(CIRCUITPY_BLEIO_HCI),1)
SRC_C +=\
common_hal/_bleio/hci.c \
SRC_C += \
common-hal/_bleio/hci.c \
endif

View File

@ -64,20 +64,20 @@
//| advertisements and it can advertise its own data. Furthermore, Adapters can accept incoming
//| connections and also initiate connections."""
//|
//| def __init__(self, *, tx: Pin, rx: Pin, rts: Pin, cts: Pin, baudrate: int = 115200, buffer_size: int = 256):
//| You cannot create an instance of `_bleio.Adapter`.
//| Use `_bleio.adapter` to access the sole instance available."""
//|
//| On boards that do not have native BLE. You can use HCI co-processor.
//| def hci_init(self, *, tx: Pin, rx: Pin, rts: Pin, cts: Pin, baudrate: int = 115200, buffer_size: int = 256):
//| On boards that do not have native BLE, you can an use HCI co-processor.
//| Call `_bleio.adapter.hci_init()` passing it the pins used to communicate
//| with the co-processor, such as an Adafruit AirLift.
//| The co-processor must have been reset and put into BLE mode beforehand
//| by the appropriate pin manipulation.
//| The `tx`, `rx`, `rts`, and `cs` pins are used to communicate with the HCI co-processor in HCI mode.
//|
#if CIRCUITPY_BLEIO_HCI
mp_obj_t bleio_adapter_hci_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
#if CIRCUITPY_BLEIO_HCI
bleio_adapter_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
if (self->enabled) {
@ -114,10 +114,14 @@ mp_obj_t bleio_adapter_hci_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_m
common_hal_bleio_adapter_hci_init(&common_hal_bleio_adapter_obj, tx, rx, rts, cts,
baudrate, buffer_size);
return mp_const_none;
#else
mp_raise_RuntimeError(translate("hci_init not available"));
return mp_const_none;
#endif // CIRCUITPY_BLEIO_HCI
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_adapter_hci_init_obj, 1, bleio_adapter_hci_init);
#endif // CIRCUITPY_BLEIO_HCI
//|
//| enabled: Any = ...

View File

@ -112,7 +112,10 @@ NORETURN void mp_raise_bleio_SecurityError(const compressed_string_t* fmt, ...)
// Called when _bleio is imported.
STATIC mp_obj_t bleio___init__(void) {
#if !CIRCUITPY_BLEIO_HCI
// HCI cannot be enabled on import, because we need to setup the HCI adapter first.
common_hal_bleio_adapter_set_enabled(&common_hal_bleio_adapter_obj, true);
#endif
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(bleio___init___obj, bleio___init__);