Skip to content

Commit 991f076

Browse files
authored
Support esp32, esp32-C3, esp32-S3 (#252)
* add esp32 support * Update HCIVirtualTransport.cpp * Update HCIVirtualTransport.cpp add ESP32-C3 support * Revert "Update HCIVirtualTransport.cpp" This reverts commit 9af9bea. * Update HCIVirtualTransport.cpp * Update library.properties * fix crash on restart * Update ATT.cpp spelling * support more espressif chips * Create compile-for-esp.yaml don't know what I'm doing * skip failing examples * Update compile-for-esp.yaml * Update compile-for-esp.yaml * Update compile-for-esp.yaml * Update compile-for-esp.yaml * Update compile-for-esp.yaml * Update compile-examples.yml * Delete compile-for-esp.yaml
1 parent fae1e36 commit 991f076

File tree

5 files changed

+227
-2
lines changed

5 files changed

+227
-2
lines changed

.github/workflows/compile-examples.yml

+33
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,36 @@ jobs:
2222
with:
2323
github-token: ${{ secrets.GITHUB_TOKEN }}
2424
fqbn: ${{ matrix.fqbn }}
25+
26+
build-for-esp32:
27+
runs-on: ubuntu-latest
28+
29+
strategy:
30+
matrix:
31+
fqbn:
32+
- esp32:esp32:esp32
33+
- esp32:esp32:esp32s3
34+
- esp32:esp32:esp32c3
35+
# future bluetooth chips
36+
#- esp32:esp32:esp32c2
37+
#- esp32:esp32:esp32c6
38+
#- esp32:esp32:esp32h2
39+
40+
steps:
41+
- uses: actions/checkout@v3
42+
- uses: arduino/compile-sketches@v1
43+
with:
44+
github-token: ${{ secrets.GITHUB_TOKEN }}
45+
fqbn: ${{ matrix.fqbn }}
46+
platforms: |
47+
- name: esp32:esp32
48+
source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
49+
sketch-paths: |
50+
- examples/Central/Scan
51+
- examples/Central/PeripheralExplorer
52+
- examples/Central/ScanCallback
53+
- examples/Central/SensorTagButton
54+
- examples/Peripheral/Advertising/EnhancedAdvertising
55+
- examples/Peripheral/Advertising/RawDataAdvertising
56+
cli-compile-flags: |
57+
- --warnings="none"

library.properties

+1-1
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
9+
architectures=samd,megaavr,mbed,apollo3,mbed_nano,mbed_portenta,mbed_nicla,esp32
1010
includes=ArduinoBLE.h

src/utility/HCIUartTransport.cpp

+1-1
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

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

src/utility/HCIVirtualTransport.h

+50
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)