Skip to content

Commit 2b5cf3a

Browse files
facchinmiabdalkader
authored andcommitted
Add Zephyr core support.
1 parent f6f6e10 commit 2b5cf3a

File tree

5 files changed

+174
-2
lines changed

5 files changed

+174
-2
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ sentence=Enables Bluetooth® Low Energy connectivity on the Arduino MKR WiFi 101
66
paragraph=This library supports creating a Bluetooth® Low Energy peripheral & central mode.
77
category=Communication
88
url=https://www.arduino.cc/en/Reference/ArduinoBLE
9-
architectures=samd,megaavr,mbed,apollo3,mbed_nano,mbed_portenta,mbed_nicla,esp32,mbed_giga,renesas,renesas_portenta,mbed_opta,renesas_uno,silabs
9+
architectures=*
1010
includes=ArduinoBLE.h

src/local/BLELocalDevice.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@
2525

2626
#include "BLELocalDevice.h"
2727

28+
#ifdef __ZEPHYR__
29+
#undef ARDUINO_PORTENTA_H7_M7
30+
#undef ARDUINO_OPTA
31+
#undef ARDUINO_GIGA
32+
#undef ARDUINO_NICLA_VISION
33+
#undef ARDUINO_PORTENTA_C33
34+
#endif
35+
2836
#if defined(PORTENTA_H7_PINS) || defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_OPTA)
2937
#ifndef BT_REG_ON
3038
#define BT_REG_ON PJ_12

src/utility/HCIUartTransport.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1818
*/
1919

20-
#if !defined(ARDUINO_ARCH_MBED) && !defined(ESP32) && !defined(ARDUINO_SILABS) && !defined(ARDUINO_UNOR4_WIFI) || defined(TARGET_NANO_RP2040_CONNECT) //|| defined(CORE_CM4)
20+
#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)
2121

2222
#include "HCIUartTransport.h"
2323

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
This file is part of the ArduinoBLE library.
3+
Copyright (c) 2018-2025 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#if defined(__ZEPHYR__)
21+
22+
#include "HCIVirtualTransportZephyr.h"
23+
24+
#include <zephyr/bluetooth/buf.h>
25+
#include <zephyr/bluetooth/hci.h>
26+
#include <zephyr/bluetooth/hci_raw.h>
27+
28+
static K_FIFO_DEFINE(rx_queue);
29+
struct k_fifo* __rx_queue = &rx_queue;
30+
extern "C" int bt_h4_vnd_setup(const struct device *dev);
31+
32+
HCIVirtualTransportZephyrClass::HCIVirtualTransportZephyrClass() {
33+
34+
}
35+
36+
HCIVirtualTransportZephyrClass::~HCIVirtualTransportZephyrClass() {
37+
38+
}
39+
40+
int HCIVirtualTransportZephyrClass::begin() {
41+
bt_enable_raw(__rx_queue);
42+
43+
#if CONFIG_BT_HCI_SETUP
44+
const struct device *uart = DEVICE_DT_GET(DT_PARENT(DT_CHOSEN(zephyr_bt_hci)));
45+
if (!uart) {
46+
return 0;
47+
}
48+
49+
if (bt_h4_vnd_setup(uart)) {
50+
return 0;
51+
}
52+
#endif /* CONFIG_BT_HCI_SETUP */
53+
54+
rxbuf.clear();
55+
56+
return 1;
57+
}
58+
59+
void HCIVirtualTransportZephyrClass::end() {
60+
61+
}
62+
63+
void HCIVirtualTransportZephyrClass::wait(unsigned long timeout) {
64+
delay(timeout);
65+
}
66+
67+
int HCIVirtualTransportZephyrClass::available() {
68+
static struct net_buf *buf = NULL;
69+
70+
if (rxbuf.available()) {
71+
return rxbuf.available();
72+
}
73+
74+
buf = (struct net_buf *) k_fifo_get(__rx_queue, K_MSEC(10));
75+
if (!buf) {
76+
return 0;
77+
}
78+
79+
for (int i=0; i<buf->len; i++) {
80+
rxbuf.store_char((uint8_t)buf->data[i]);
81+
}
82+
83+
net_buf_pull(buf, buf->len);
84+
if (!buf->len) {
85+
net_buf_unref(buf);
86+
buf = NULL;
87+
}
88+
89+
return rxbuf.available();
90+
}
91+
92+
int HCIVirtualTransportZephyrClass::peek() {
93+
return -1;
94+
}
95+
96+
int HCIVirtualTransportZephyrClass::read() {
97+
if (rxbuf.available()) {
98+
return rxbuf.read_char();
99+
}
100+
101+
return -1;
102+
}
103+
104+
size_t HCIVirtualTransportZephyrClass::write(const uint8_t* data, size_t length) {
105+
enum bt_buf_type type = bt_buf_type_from_h4(data[0], BT_BUF_OUT);
106+
struct net_buf *buf = bt_buf_get_tx(type, K_FOREVER, &data[1], length - 1);
107+
108+
if (buf) {
109+
auto err = bt_send(buf);
110+
if (err) {
111+
net_buf_unref(buf);
112+
}
113+
return length;
114+
}
115+
return 0;
116+
}
117+
118+
HCIVirtualTransportZephyrClass HCIVirtualTransportZephyr;
119+
HCITransportInterface& HCITransport = HCIVirtualTransportZephyr;
120+
#endif
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
This file is part of the ArduinoBLE library.
3+
Copyright (c) 2018-2025 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include "HCITransport.h"
21+
#include "api/RingBuffer.h"
22+
#include <stdint.h>
23+
#include <stdio.h>
24+
#include <string.h>
25+
26+
class HCIVirtualTransportZephyrClass : public HCITransportInterface {
27+
public:
28+
HCIVirtualTransportZephyrClass();
29+
virtual ~HCIVirtualTransportZephyrClass();
30+
31+
virtual int begin();
32+
virtual void end();
33+
34+
virtual void wait(unsigned long timeout);
35+
36+
virtual int available();
37+
virtual int peek();
38+
virtual int read();
39+
40+
virtual size_t write(const uint8_t* data, size_t length);
41+
42+
private:
43+
RingBufferN<258> rxbuf;
44+
};

0 commit comments

Comments
 (0)