Skip to content

ArduinoCore-zephyr support #404

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
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/local/BLELocalDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@

#include "BLELocalDevice.h"

#ifdef __ZEPHYR__
#undef ARDUINO_PORTENTA_H7_M7
#undef ARDUINO_OPTA
#undef ARDUINO_GIGA
#undef ARDUINO_NICLA_VISION
#undef ARDUINO_PORTENTA_C33
#endif

#if defined(PORTENTA_H7_PINS) || defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_OPTA)
#ifndef BT_REG_ON
#define BT_REG_ON PJ_12
Expand Down
2 changes: 1 addition & 1 deletion src/utility/HCIUartTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#if !defined(ARDUINO_ARCH_MBED) && !defined(ESP32) && !defined(ARDUINO_SILABS) && !defined(ARDUINO_UNOR4_WIFI) || defined(TARGET_NANO_RP2040_CONNECT) //|| defined(CORE_CM4)
#if !defined(ARDUINO_ARCH_MBED) && !defined(__ZEPHYR__) && !defined(ESP32) && !defined(ARDUINO_SILABS) && !defined(ARDUINO_UNOR4_WIFI) && !defined(__ZEPHYR__) || defined(TARGET_NANO_RP2040_CONNECT) //|| defined(CORE_CM4)

#include "HCIUartTransport.h"

Expand Down
127 changes: 127 additions & 0 deletions src/utility/HCIVirtualTransportZephyr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
This file is part of the ArduinoBLE library.
Copyright (c) 2018 Arduino SA. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#if defined(__ZEPHYR__)

#include "HCIVirtualTransportZephyr.h"

extern "C" struct k_fifo* __rx_queue;

HCIVirtualTransportZephyrClass::HCIVirtualTransportZephyrClass()
{
}

HCIVirtualTransportZephyrClass::~HCIVirtualTransportZephyrClass()
{
}

extern "C" void set_public_address(struct k_fifo *rx_queue);
extern "C" int bt_h4_vnd_setup(const struct device *dev);
struct h4_config {
const struct device *uart;
k_thread_stack_t *rx_thread_stack;
size_t rx_thread_stack_size;
struct k_thread *rx_thread;
};

int HCIVirtualTransportZephyrClass::begin()
{
bt_enable_raw(__rx_queue);
#if defined(CONFIG_BT_HCI_SETUP)
const struct device *dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_bt_hci));
if (!dev) {
return 0;
}
const struct h4_config *cfg = (const struct h4_config *)dev->config;
bt_h4_vnd_setup(cfg->uart);
#else
set_public_address(__rx_queue);
#endif

rxbuf.clear();
return 1;
}

void HCIVirtualTransportZephyrClass::end()
{
}

void HCIVirtualTransportZephyrClass::wait(unsigned long timeout)
{
delay(timeout);
}

int HCIVirtualTransportZephyrClass::available()
{
if (rxbuf.available()) {
return rxbuf.available();
}

static struct net_buf *buf;
buf = (struct net_buf*)k_fifo_get(__rx_queue, K_MSEC(10));
if (!buf) {
return 0;
}

for (int i = 0; i < buf->len; i++) {
rxbuf.store_char((uint8_t)buf->data[i]);
}
net_buf_pull(buf, buf->len);
if (!buf->len) {
net_buf_unref(buf);
buf = NULL;
}

return rxbuf.available();
}

// never called
int HCIVirtualTransportZephyrClass::peek()
{
return -1;
}

int HCIVirtualTransportZephyrClass::read()
{

if (rxbuf.available()) {
return rxbuf.read_char();
}

return -1;
}

size_t HCIVirtualTransportZephyrClass::write(const uint8_t* data, size_t length)
{
struct net_buf *__net_buf = bt_buf_get_tx(bt_buf_type_from_h4(data[0], BT_BUF_OUT), K_FOREVER, &data[1], length - 1);
if (__net_buf) {
auto err = bt_send(__net_buf);
if (err) {
net_buf_unref(__net_buf);
}
return length;
}
return 0;
}

HCIVirtualTransportZephyrClass HCIVirtualTransportZephyr;

HCITransportInterface& HCITransport = HCIVirtualTransportZephyr;

#endif
49 changes: 49 additions & 0 deletions src/utility/HCIVirtualTransportZephyr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
This file is part of the ArduinoBLE library.
Copyright (c) 2018 Arduino SA. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "HCITransport.h"
#include "api/RingBuffer.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>

extern "C" {
#include <zephyr/bluetooth/hci_raw.h>
#include <zephyr/bluetooth/buf.h>
}

class HCIVirtualTransportZephyrClass : public HCITransportInterface {
public:
HCIVirtualTransportZephyrClass();
virtual ~HCIVirtualTransportZephyrClass();

virtual int begin();
virtual void end();

virtual void wait(unsigned long timeout);

virtual int available();
virtual int peek();
virtual int read();

virtual size_t write(const uint8_t* data, size_t length);

private:
RingBufferN<258> rxbuf;
};
43 changes: 43 additions & 0 deletions src/utility/HCIVirtualZephyrMacros.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#if defined(__ZEPHYR__)

#include <zephyr/bluetooth/hci.h>
#include <zephyr/bluetooth/buf.h>
#include <zephyr/bluetooth/hci_vs.h>

static K_FIFO_DEFINE(rx_queue);
struct k_fifo* __rx_queue = &rx_queue;


// from https://github.com/thomasstenersen/sdk-zephyr/commit/df546a0eb9b96b453373f38483959d2363a83cae#diff-217778ef1d0638c89207786f06425396891f987713120e83db724d26b9813eebR331
// referenced by https://devzone.nordicsemi.com/f/nordic-q-a/73310/incompatibility-between-mcumgr-cli-and-hci_usb-when-running-with-bt_ll_softdevice-enabled

void set_public_address(struct k_fifo *rx_queue)
{
const struct bt_hci_cp_vs_write_bd_addr cmd = {
.bdaddr.val = {0xFA, 0xFA, 0xFA, 0xFA, 0xFA, 0xFA}
};
const struct bt_hci_cmd_hdr cmd_header = {
.opcode = BT_HCI_OP_VS_WRITE_BD_ADDR,
.param_len = sizeof(cmd)
};
struct net_buf *buf;
int err;
buf = bt_buf_get_tx(BT_BUF_CMD, K_NO_WAIT,
&cmd_header, sizeof(cmd_header));
net_buf_add_mem(buf, &cmd, sizeof(cmd));
err = bt_send(buf);
__ASSERT_NO_MSG(err == 0);
/* Pull out the command complete. */
buf = (struct net_buf*)k_fifo_get(rx_queue, K_SECONDS(10)); /* 10s == HCI_CMD_TIMEOUT */
struct bt_hci_evt_hdr *hdr;
__ASSERT_NO_MSG(buf != NULL);
__ASSERT_NO_MSG(buf->len >= sizeof(*hdr));
hdr = net_buf_pull_mem(buf, sizeof(*hdr));
__ASSERT_NO_MSG(hdr->evt == BT_HCI_EVT_CMD_COMPLETE);
struct bt_hci_evt_cmd_complete *evt;
evt = net_buf_pull_mem(buf, sizeof(*evt));
__ASSERT_NO_MSG(sys_le16_to_cpu(evt->opcode) == BT_HCI_OP_VS_WRITE_BD_ADDR);
net_buf_unref(buf);
}

#endif
Loading