Skip to content

Commit b96bc7c

Browse files
committed
add esp32 support
1 parent 94ffe05 commit b96bc7c

File tree

4 files changed

+199
-2
lines changed

4 files changed

+199
-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 BLE connectivity on the Arduino MKR WiFi 1010, Arduino UNO WiFi
66
paragraph=This library supports creating a BLE peripheral and BLE central mode.
77
category=Communication
88
url=https://www.arduino.cc/en/Reference/ArduinoBLE
9-
architectures=samd,megaavr,mbed,apollo3,mbed_nano,mbed_portenta
9+
architectures=samd,megaavr,mbed,apollo3,mbed_nano,mbed_portenta,esp32
1010
includes=ArduinoBLE.h

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

2222
#include "HCIUartTransport.h"
2323

src/utility/HCIVirtualTransport.cpp

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
This file is part of the ArduinoBLE library.
3+
Copyright (c) 2018 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(ESP32)
21+
22+
#include "HCIVirtualTransport.h"
23+
24+
StreamBufferHandle_t rec_buffer;
25+
StreamBufferHandle_t send_buffer;
26+
27+
28+
static void notify_host_send_available(void)
29+
{
30+
}
31+
32+
static int notify_host_recv(uint8_t *data, uint16_t length)
33+
{
34+
for (uint8_t i = 0; i < length; i++) {
35+
char b = data[i];
36+
}
37+
xStreamBufferSend(rec_buffer,data,length,portMAX_DELAY); // !!!potentially waiting forever
38+
return 0;
39+
}
40+
41+
static esp_vhci_host_callback_t vhci_host_cb = {
42+
notify_host_send_available,
43+
notify_host_recv
44+
};
45+
46+
void bleTask(void *pvParameters)
47+
{
48+
esp_vhci_host_register_callback(&vhci_host_cb);
49+
size_t length;
50+
uint8_t mybuf[256];
51+
52+
while(true){
53+
length = xStreamBufferReceive(send_buffer,mybuf,256,portMAX_DELAY);
54+
while (!esp_vhci_host_check_send_available()) {}
55+
for (uint8_t i = 0; i < length; i++) {
56+
char b = mybuf[i];
57+
}
58+
esp_vhci_host_send_packet(mybuf, length);
59+
}
60+
}
61+
62+
63+
HCIVirtualTransportClass::HCIVirtualTransportClass()
64+
{
65+
}
66+
67+
HCIVirtualTransportClass::~HCIVirtualTransportClass()
68+
{
69+
}
70+
71+
int HCIVirtualTransportClass::begin()
72+
{
73+
btStarted(); // this somehow stops the arduino ide from initializing bluedroid
74+
75+
rec_buffer = xStreamBufferCreate(258, 1);
76+
send_buffer = xStreamBufferCreate(258, 1);
77+
78+
esp_err_t ret = nvs_flash_init();
79+
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
80+
ESP_ERROR_CHECK(nvs_flash_erase());
81+
ret = nvs_flash_init();
82+
}
83+
ESP_ERROR_CHECK( ret );
84+
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
85+
bt_cfg.mode = ESP_BT_MODE_BLE;
86+
ret = esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
87+
if (ret) {
88+
return 0;
89+
}
90+
if ((ret = esp_bt_controller_init(&bt_cfg)) != ESP_OK) {
91+
return 0;
92+
}
93+
if ((ret = esp_bt_controller_enable(ESP_BT_MODE_BLE)) != ESP_OK) {
94+
return 0;
95+
}
96+
xTaskCreatePinnedToCore(&bleTask, "bleTask", 2048, NULL, 5, NULL, 0);
97+
return 1;
98+
}
99+
100+
void HCIVirtualTransportClass::end()
101+
{
102+
vStreamBufferDelete(rec_buffer);
103+
vStreamBufferDelete(send_buffer);
104+
esp_bt_controller_deinit();
105+
}
106+
107+
void HCIVirtualTransportClass::wait(unsigned long timeout)
108+
{
109+
for (unsigned long start = (esp_timer_get_time() / 1000ULL); ((esp_timer_get_time() / 1000ULL) - start) < timeout;) {
110+
if (available()) {
111+
break;
112+
}
113+
}
114+
}
115+
116+
int HCIVirtualTransportClass::available()
117+
{
118+
size_t bytes = xStreamBufferBytesAvailable(rec_buffer);
119+
return bytes;
120+
}
121+
122+
// never called
123+
int HCIVirtualTransportClass::peek()
124+
{
125+
return -1;
126+
}
127+
128+
int HCIVirtualTransportClass::read()
129+
{
130+
uint8_t c;
131+
if(xStreamBufferReceive(rec_buffer, &c, 1, portMAX_DELAY)) {
132+
return c;
133+
}
134+
return -1;
135+
}
136+
137+
size_t HCIVirtualTransportClass::write(const uint8_t* data, size_t length)
138+
{
139+
size_t result = xStreamBufferSend(send_buffer,data,length,portMAX_DELAY);
140+
return result;
141+
}
142+
143+
HCIVirtualTransportClass HCIVirtualTransport;
144+
145+
HCITransportInterface& HCITransport = HCIVirtualTransport;
146+
147+
#endif

src/utility/HCIVirtualTransport.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
This file is part of the ArduinoBLE library.
3+
Copyright (c) 2018 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 <stdint.h>
22+
#include <stdio.h>
23+
#include <string.h>
24+
25+
#include "freertos/FreeRTOS.h"
26+
#include "freertos/task.h"
27+
#include "freertos/stream_buffer.h"
28+
29+
#include "esp_bt.h"
30+
#include "nvs_flash.h"
31+
32+
#include "esp32-hal-bt.h" // this is needed to disable bluedroid
33+
34+
35+
class HCIVirtualTransportClass : public HCITransportInterface {
36+
public:
37+
HCIVirtualTransportClass();
38+
virtual ~HCIVirtualTransportClass();
39+
40+
virtual int begin();
41+
virtual void end();
42+
43+
virtual void wait(unsigned long timeout);
44+
45+
virtual int available();
46+
virtual int peek();
47+
virtual int read();
48+
49+
virtual size_t write(const uint8_t* data, size_t length);
50+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy