Skip to content

Commit 1e9f5f6

Browse files
committed
Finished BLE support for arduino-esp32 3.3.0
1 parent 5b6f8aa commit 1e9f5f6

File tree

6 files changed

+46
-21
lines changed

6 files changed

+46
-21
lines changed

doxygen/pages/MIDI-over-BLE.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ backend for the particular board.
2626
| Arduino GIGA R1 WiFi | @ref ArduinoBLEBackend | |
2727
| Raspberry Pi Pico W (RP2040) | @ref BTstackBackgroundBackend | |
2828
| ESP32 | @ref ESP32BluedroidBackend | @ref ESP32NimBLEBackend |
29-
| ESP32-S3 | @ref ESP32BluedroidBackend | @ref ESP32NimBLEBackend |
30-
| ESP32-C3, ESP32-C6, ESP32-H2 | @ref ESP32BluedroidBackend | @ref ESP32NimBLEBackend |
29+
| ESP32-S3 | @ref ESP32NimBLEBackend | @ref ESP32BluedroidBackend |
30+
| ESP32-C3, ESP32-C6, ESP32-H2 | @ref ESP32NimBLEBackend | @ref ESP32BluedroidBackend |
3131

3232
### ArduinoBLEBackend
3333

@@ -56,13 +56,17 @@ buffer from filling up, since there is no BLE flow control.
5656
### ESP32NimBLEBackend
5757

5858
This backend uses the newer [Apache MyNewt NimBLE](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/bluetooth/nimble/index.html)
59-
stack.
59+
stack. Starting from version 3.3.0 of the [arduino-esp32](https://github.com/espressif/arduino-esp32) core,
60+
it is the default for the ESP32-S3 and other ESP32-based boards except the
61+
original ESP32 (which still uses Bluedroid).
62+
See https://github.com/espressif/arduino-esp32/discussions/10991 for details.
6063

6164
Like the @ref ESP32BluedroidBackend, the NimBLE backend uses its own threads and
6265
does not require polling.
6366

64-
The NimBLE ESP-IDF component is disabled by default in the [arduino-esp32](https://github.com/espressif/arduino-esp32)
65-
core, so you'll need to install the [h2zero/NimBLE-Arduino](https://github.com/h2zero/NimBLE-Arduino)
67+
If you're using a version of the arduino-esp32 core that's older than 3.3.0,
68+
or if you're using an original ESP32, the NimBLE ESP-IDF component is disabled,
69+
and you'll need to install the [h2zero/NimBLE-Arduino](https://github.com/h2zero/NimBLE-Arduino)
6670
library in order to use this backend.
6771

6872
Defining @ref CS_USE_NIMBLE before including Control Surface changes the default

examples/3. MIDI Interfaces/BLEMIDI-Adapter/BLEMIDI-Adapter.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
* - If you're using an ESP32, you can optionally switch to the NimBLE backend
1212
* by installing the [h2zero/NimBLE-Arduino](https://github.com/h2zero/NimBLE-Arduino)
1313
* library, and adding `#define CS_USE_NIMBLE` at the top of this sketch.
14+
* - If you're using a newer ESP32-based chip like the ESP32-S3, with
15+
* arduino-esp32 version 3.3.0 or later, the NimBLE backend is enabled by
16+
* default, without requiring the h2zero/NimBLE-Arduino library.
1417
* - If you're not using a Pico or an ESP32, you'll have to install the
1518
* [ArduinoBLE](https://github.com/arduino-libraries/ArduinoBLE) library.
1619
*

src/MIDI_Interfaces/BLEMIDI/ESP32-NimBLE/ble.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@
88
#define CS_MIDI_BLE_ESP_IDF_NIMBLE 1
99
#else
1010
#include <nimconfig.h>
11+
#pragma message( \
12+
"arduino-esp32 installation does not include NimBLE. Using https://github.com/h2zero/NimBLE-Arduino as a fallback")
1113
#define CS_MIDI_BLE_ESP_IDF_NIMBLE 0
1214
#endif

src/MIDI_Interfaces/BLEMIDI/ESP32-NimBLE/events.ipp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ namespace {
2424
[[maybe_unused]] std::string fmt_address(const void *addr) {
2525
std::string str {"XX:XX:XX:XX:XX:XX"};
2626
auto *u8p = reinterpret_cast<const uint8_t *>(addr);
27-
snprintf(str.data(), str.size() + 1, "%02x:%02x:%02x:%02x:%02x:%02x",
28-
u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]);
27+
#if __cplusplus >= 201703L
28+
auto *buf = str.data();
29+
#else
30+
auto *buf = &str[0];
31+
#endif
32+
snprintf(buf, str.size() + 1, "%02x:%02x:%02x:%02x:%02x:%02x", u8p[5],
33+
u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]);
2934
return str;
3035
}
3136

@@ -49,7 +54,7 @@ void print_conn_desc(struct ble_gap_conn_desc *desc) {
4954
desc->sec_state.bonded);
5055
}
5156

52-
const char *own_address_type_to_string(uint8_t own_addr_type) {
57+
[[maybe_unused]] const char *own_address_type_to_string(uint8_t own_addr_type) {
5358
switch (own_addr_type) {
5459
case BLE_OWN_ADDR_RANDOM: return "BLE_OWN_ADDR_RANDOM";
5560
case BLE_OWN_ADDR_RPA_RANDOM_DEFAULT:

src/MIDI_Interfaces/BLEMIDI/ESP32-NimBLE/gatt.c.ipp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ inline const struct ble_gatt_chr_def midi_ble_characteristic[] = {
4343
BLE_GATT_CHR_F_WRITE_NO_RSP | // BLE_GATT_CHR_F_WRITE_ENC |
4444
BLE_GATT_CHR_F_NOTIFY,
4545
.min_key_size = 0,
46-
.val_handle = nullptr},
46+
.val_handle = nullptr,
47+
#ifdef BLE_GATT_DSC_CLT_PRE_FMT16 // https://github.com/apache/mynewt-nimble/pull/1667
48+
.cpfd = nullptr
49+
#endif
50+
},
4751
{}, // sentinel
4852
};
4953

src/MIDI_Interfaces/BLEMIDI/ESP32-NimBLE/init.ipp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
#include <nimble/porting/nimble/include/nimble/nimble_port.h>
1212
#endif
1313
#ifdef ESP_PLATFORM
14+
#ifndef CONFIG_IDF_TARGET_ESP32P4
1415
#include <esp_bt.h>
16+
#endif
1517
#include <nvs_flash.h>
18+
#include <soc/soc_caps.h>
1619
#endif
1720

1821
#include "ble-macro-fix.h"
@@ -21,8 +24,11 @@
2124
namespace cs::midi_ble_nimble {
2225

2326
inline bool init_hardware() {
27+
#if CS_MIDI_BLE_ESP_IDF_NIMBLE
28+
CS_CHECK_ESP(nimble_port_init());
29+
#else // fall back to the h2zero/NimBLE-Arduino library
2430
#ifdef ESP_PLATFORM
25-
#ifdef CONFIG_ENABLE_ARDUINO_DEPENDS
31+
#if defined(CONFIG_ENABLE_ARDUINO_DEPENDS) && SOC_BT_SUPPORTED
2632
// make sure the linker includes esp32-hal-bt.c so Arduino init doesn't release BLE memory.
2733
btStarted();
2834
#endif
@@ -33,26 +39,27 @@ inline bool init_hardware() {
3339
nvs_flash_init_rc = nvs_flash_init();
3440
}
3541
ESP_ERROR_CHECK(nvs_flash_init_rc);
36-
3742
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
38-
#if defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32S3)
39-
bt_cfg.bluetooth_mode = ESP_BT_MODE_BLE;
40-
#else
43+
#if defined(CONFIG_IDF_TARGET_ESP32)
4144
bt_cfg.mode = ESP_BT_MODE_BLE;
45+
bt_cfg.ble_max_conn = CONFIG_BT_NIMBLE_MAX_CONNECTIONS;
46+
#elif defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32S3)
47+
bt_cfg.ble_max_act = CONFIG_BT_NIMBLE_MAX_CONNECTIONS;
48+
#else
49+
bt_cfg.nimble_max_connections = CONFIG_BT_NIMBLE_MAX_CONNECTIONS;
4250
#endif
51+
#if CONFIG_IDF_TARGET_ESP32
4352
ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
53+
#endif
4454
ESP_ERROR_CHECK(esp_bt_controller_init(&bt_cfg));
4555
ESP_ERROR_CHECK(esp_bt_controller_enable(ESP_BT_MODE_BLE));
46-
#if ESP_ARDUINO_VERSION < ESP_ARDUINO_VERSION_VAL(3, 3, 0)
56+
#if CONFIG_BT_NIMBLE_LEGACY_VHCI_ENABLE
4757
ESP_ERROR_CHECK(esp_nimble_hci_init());
48-
#endif
4958
#endif
50-
51-
#if CS_MIDI_BLE_ESP_IDF_NIMBLE
52-
CS_CHECK_ESP(nimble_port_init());
53-
#else
54-
nimble_port_init();
5559
#endif
60+
nimble_port_init();
61+
#endif // #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
62+
5663
return true;
5764
}
5865

0 commit comments

Comments
 (0)