Skip to content
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

Support esp32, esp32-C3, esp32-S3 #252

Merged
merged 21 commits into from
Aug 23, 2022
Merged
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
33 changes: 33 additions & 0 deletions .github/workflows/compile-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,36 @@ jobs:
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
fqbn: ${{ matrix.fqbn }}

build-for-esp32:
runs-on: ubuntu-latest

strategy:
matrix:
fqbn:
- esp32:esp32:esp32
- esp32:esp32:esp32s3
- esp32:esp32:esp32c3
# future bluetooth chips
#- esp32:esp32:esp32c2
#- esp32:esp32:esp32c6
#- esp32:esp32:esp32h2

steps:
- uses: actions/checkout@v3
- uses: arduino/compile-sketches@v1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
fqbn: ${{ matrix.fqbn }}
platforms: |
- name: esp32:esp32
source-url: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
sketch-paths: |
- examples/Central/Scan
- examples/Central/PeripheralExplorer
- examples/Central/ScanCallback
- examples/Central/SensorTagButton
- examples/Peripheral/Advertising/EnhancedAdvertising
- examples/Peripheral/Advertising/RawDataAdvertising
cli-compile-flags: |
- --warnings="none"
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ sentence=Enables Bluetooth® Low Energy connectivity on the Arduino MKR WiFi 101
paragraph=This library supports creating a Bluetooth® Low Energy peripheral & central mode.
category=Communication
url=https://www.arduino.cc/en/Reference/ArduinoBLE
architectures=samd,megaavr,mbed,apollo3,mbed_nano,mbed_portenta,mbed_nicla
architectures=samd,megaavr,mbed,apollo3,mbed_nano,mbed_portenta,mbed_nicla,esp32
includes=ArduinoBLE.h
4 changes: 2 additions & 2 deletions src/utility/ATT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,7 @@ void ATTClass::writeReqOrCmd(uint16_t connectionHandle, uint16_t mtu, uint8_t op
}
return;
}
// Check permssion
// Check permission
if((characteristic->permissions() &( BLEPermission::BLEEncryption >> 8)) > 0 &&
(getPeerEncryption(connectionHandle) & PEER_ENCRYPTION::ENCRYPTED_AES) == 0){
holdResponse = true;
Expand Down Expand Up @@ -1941,4 +1941,4 @@ int ATTClass::getPeerResolvedAddress(uint16_t connectionHandle, uint8_t resolved
#if !defined(FAKE_ATT)
ATTClass ATTObj;
ATTClass& ATT = ATTObj;
#endif
#endif
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(TARGET_NANO_RP2040_CONNECT)
#if !defined(ARDUINO_ARCH_MBED) && !defined(ESP32) || defined(TARGET_NANO_RP2040_CONNECT)

#include "HCIUartTransport.h"

Expand Down
142 changes: 142 additions & 0 deletions src/utility/HCIVirtualTransport.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
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(ESP32)

#include "HCIVirtualTransport.h"

StreamBufferHandle_t rec_buffer;
StreamBufferHandle_t send_buffer;
TaskHandle_t bleHandle;


static void notify_host_send_available(void)
{
}

static int notify_host_recv(uint8_t *data, uint16_t length)
{
xStreamBufferSend(rec_buffer,data,length,portMAX_DELAY); // !!!potentially waiting forever
return 0;
}

static esp_vhci_host_callback_t vhci_host_cb = {
notify_host_send_available,
notify_host_recv
};

void bleTask(void *pvParameters)
{
esp_vhci_host_register_callback(&vhci_host_cb);
size_t length;
uint8_t mybuf[258];

while(true){
length = xStreamBufferReceive(send_buffer,mybuf,258,portMAX_DELAY);
while (!esp_vhci_host_check_send_available()) {}
esp_vhci_host_send_packet(mybuf, length);
}
}


HCIVirtualTransportClass::HCIVirtualTransportClass()
{
}

HCIVirtualTransportClass::~HCIVirtualTransportClass()
{
}

int HCIVirtualTransportClass::begin()
{
btStarted(); // this somehow stops the arduino ide from initializing bluedroid

rec_buffer = xStreamBufferCreate(258, 1);
send_buffer = xStreamBufferCreate(258, 1);

esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();

#if CONFIG_IDF_TARGET_ESP32
bt_cfg.mode = ESP_BT_MODE_BLE; //original esp32 chip
#else
bt_cfg.bluetooth_mode = ESP_BT_MODE_BLE; //different api for newer models
#endif

esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
esp_bt_controller_init(&bt_cfg);
esp_bt_controller_enable(ESP_BT_MODE_BLE);
xTaskCreatePinnedToCore(&bleTask, "bleTask", 2048, NULL, 5, &bleHandle, 0);
return 1;
}

void HCIVirtualTransportClass::end()
{
vStreamBufferDelete(rec_buffer);
vStreamBufferDelete(send_buffer);
esp_bt_controller_disable();
esp_bt_controller_deinit();
vTaskDelete(bleHandle);
}

void HCIVirtualTransportClass::wait(unsigned long timeout)
{
for (unsigned long start = (esp_timer_get_time() / 1000ULL); ((esp_timer_get_time() / 1000ULL) - start) < timeout;) {
if (available()) {
break;
}
}
}

int HCIVirtualTransportClass::available()
{
size_t bytes = xStreamBufferBytesAvailable(rec_buffer);
return bytes;
}

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

int HCIVirtualTransportClass::read()
{
uint8_t c;
if(xStreamBufferReceive(rec_buffer, &c, 1, portMAX_DELAY)) {
return c;
}
return -1;
}

size_t HCIVirtualTransportClass::write(const uint8_t* data, size_t length)
{
size_t result = xStreamBufferSend(send_buffer,data,length,portMAX_DELAY);
return result;
}

HCIVirtualTransportClass HCIVirtualTransport;

HCITransportInterface& HCITransport = HCIVirtualTransport;

#endif
50 changes: 50 additions & 0 deletions src/utility/HCIVirtualTransport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
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 <stdint.h>
#include <stdio.h>
#include <string.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/stream_buffer.h"

#include "esp_bt.h"
#include "nvs_flash.h"

#include "esp32-hal-bt.h" // this is needed to disable bluedroid


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

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);
};