Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat(ble): Enable BLE for ESP32-P4 through esp-hosted
  • Loading branch information
lucasssvaz committed Sep 5, 2025
commit a099878c0ac56abd851e9660391fd405a5f62ecb
183 changes: 183 additions & 0 deletions cores/esp32/esp32-hal-hosted.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
// Copyright 2015-2025 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "sdkconfig.h"
#if defined(CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE) || defined(CONFIG_ESP_WIFI_REMOTE_ENABLED)

#include "esp32-hal-hosted.h"
#include "esp32-hal-log.h"

#include "esp_hosted_transport_config.h"
extern esp_err_t esp_hosted_init();
extern esp_err_t esp_hosted_deinit();

static bool hosted_initialized = false;
static bool hosted_ble_active = false;
static bool hosted_wifi_active = false;

static sdio_pin_config_t sdio_pin_config = {
#ifdef BOARD_HAS_SDIO_ESP_HOSTED
.pin_clk = BOARD_SDIO_ESP_HOSTED_CLK,
.pin_cmd = BOARD_SDIO_ESP_HOSTED_CMD,
.pin_d0 = BOARD_SDIO_ESP_HOSTED_D0,
.pin_d1 = BOARD_SDIO_ESP_HOSTED_D1,
.pin_d2 = BOARD_SDIO_ESP_HOSTED_D2,
.pin_d3 = BOARD_SDIO_ESP_HOSTED_D3,
.pin_reset = BOARD_SDIO_ESP_HOSTED_RESET
#else
.pin_clk = CONFIG_ESP_SDIO_PIN_CLK,
.pin_cmd = CONFIG_ESP_SDIO_PIN_CMD,
.pin_d0 = CONFIG_ESP_SDIO_PIN_D0,
.pin_d1 = CONFIG_ESP_SDIO_PIN_D1,
.pin_d2 = CONFIG_ESP_SDIO_PIN_D2,
.pin_d3 = CONFIG_ESP_SDIO_PIN_D3,
.pin_reset = CONFIG_ESP_SDIO_GPIO_RESET_SLAVE
#endif
};

// Forward declarations
bool hostedInit();
bool hostedDeinit();

bool hostedInitBLE() {
log_i("Initializing ESP-Hosted for BLE");
hosted_ble_active = true;
return hostedInit();
}

bool hostedInitWiFi() {
log_i("Initializing ESP-Hosted for WiFi");
hosted_wifi_active = true;
return hostedInit();
}

bool hostedDeinitBLE() {
log_i("Deinitializing ESP-Hosted for BLE");
hosted_ble_active = false;
if (!hosted_wifi_active) {
return hostedDeinit();
} else {
log_i("ESP-Hosted is still being used by Wi-Fi. Skipping deinit.");
return true;
}
}

bool hostedDeinitWiFi() {
log_i("Deinitializing ESP-Hosted for WiFi");
hosted_wifi_active = false;
if (!hosted_ble_active) {
return hostedDeinit();
} else {
log_i("ESP-Hosted is still being used by BLE. Skipping deinit.");
return true;
}
}

bool hostedInit() {
if (!hosted_initialized) {
log_i("Initializing ESP-Hosted");
log_d("SDIO pins: clk=%d, cmd=%d, d0=%d, d1=%d, d2=%d, d3=%d, rst=%d", sdio_pin_config.pin_clk, sdio_pin_config.pin_cmd, sdio_pin_config.pin_d0, sdio_pin_config.pin_d1, sdio_pin_config.pin_d2, sdio_pin_config.pin_d3, sdio_pin_config.pin_reset);
hosted_initialized = true;
struct esp_hosted_sdio_config conf = INIT_DEFAULT_HOST_SDIO_CONFIG();
conf.pin_clk.pin = sdio_pin_config.pin_clk;
conf.pin_cmd.pin = sdio_pin_config.pin_cmd;
conf.pin_d0.pin = sdio_pin_config.pin_d0;
conf.pin_d1.pin = sdio_pin_config.pin_d1;
conf.pin_d2.pin = sdio_pin_config.pin_d2;
conf.pin_d3.pin = sdio_pin_config.pin_d3;
conf.pin_reset.pin = sdio_pin_config.pin_reset;
// esp_hosted_sdio_set_config() will fail on second attempt but here temporarily to not cause exception on reinit
if (esp_hosted_sdio_set_config(&conf) != ESP_OK || esp_hosted_init() != ESP_OK) {
log_e("esp_hosted_init failed!");
hosted_initialized = false;
return false;
}
log_i("ESP-Hosted initialized!");
}

// Attach pins to PeriMan here
// Slave chip model is CONFIG_IDF_SLAVE_TARGET
// sdio_pin_config.pin_clk
// sdio_pin_config.pin_cmd
// sdio_pin_config.pin_d0
// sdio_pin_config.pin_d1
// sdio_pin_config.pin_d2
// sdio_pin_config.pin_d3
// sdio_pin_config.pin_reset

return true;
}

bool hostedSetPins(int8_t clk, int8_t cmd, int8_t d0, int8_t d1, int8_t d2, int8_t d3, int8_t rst) {
if (clk < 0 || cmd < 0 || d0 < 0 || d1 < 0 || d2 < 0 || d3 < 0 || rst < 0) {
log_e("All SDIO pins must be defined");
return false;
}

if (hosted_initialized) {
int8_t current_clk, current_cmd, current_d0, current_d1, current_d2, current_d3, current_rst;
hostedGetPins(&current_clk, &current_cmd, &current_d0, &current_d1, &current_d2, &current_d3, &current_rst);
log_e("SDIO pins must be set before ESP-Hosted is initialized");
log_e("Current pins used: clk=%d, cmd=%d, d0=%d, d1=%d, d2=%d, d3=%d, rst=%d", current_clk, current_cmd, current_d0, current_d1, current_d2, current_d3, current_rst);
return false;
}

sdio_pin_config.pin_clk = clk;
sdio_pin_config.pin_cmd = cmd;
sdio_pin_config.pin_d0 = d0;
sdio_pin_config.pin_d1 = d1;
sdio_pin_config.pin_d2 = d2;
sdio_pin_config.pin_d3 = d3;
sdio_pin_config.pin_reset = rst;
return true;
}

void hostedGetPins(int8_t *clk, int8_t *cmd, int8_t *d0, int8_t *d1, int8_t *d2, int8_t *d3, int8_t *rst) {
*clk = sdio_pin_config.pin_clk;
*cmd = sdio_pin_config.pin_cmd;
*d0 = sdio_pin_config.pin_d0;
*d1 = sdio_pin_config.pin_d1;
*d2 = sdio_pin_config.pin_d2;
*d3 = sdio_pin_config.pin_d3;
*rst = sdio_pin_config.pin_reset;
}

bool hostedIsBLEActive() {
return hosted_ble_active;
}

bool hostedIsWiFiActive() {
return hosted_wifi_active;
}

bool hostedDeinit() {
if (!hosted_initialized) {
log_e("ESP-Hosted is not initialized");
return false;
}

if (esp_hosted_deinit() != ESP_OK) {
log_e("esp_hosted_deinit failed!");
return false;
}

hosted_initialized = false;
return true;
}

bool hostedIsInitialized() {
return hosted_initialized;
}

#endif /* defined(CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE) || defined(CONFIG_ESP_WIFI_REMOTE_ENABLED) */
53 changes: 53 additions & 0 deletions cores/esp32/esp32-hal-hosted.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2015-2025 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef MAIN_ESP32_HAL_HOSTED_H_
#define MAIN_ESP32_HAL_HOSTED_H_

#include "sdkconfig.h"
#if defined(CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE) || defined(CONFIG_ESP_WIFI_REMOTE_ENABLED)

#include "stdint.h"
#include "stdbool.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
uint8_t pin_clk;
uint8_t pin_cmd;
uint8_t pin_d0;
uint8_t pin_d1;
uint8_t pin_d2;
uint8_t pin_d3;
uint8_t pin_reset;
} sdio_pin_config_t;

bool hostedInitBLE();
bool hostedInitWiFi();
bool hostedDeinitBLE();
bool hostedDeinitWiFi();
bool hostedIsInitialized();
bool hostedIsBLEActive();
bool hostedIsWiFiActive();
bool hostedSetPins(int8_t clk, int8_t cmd, int8_t d0, int8_t d1, int8_t d2, int8_t d3, int8_t rst);
void hostedGetPins(int8_t *clk, int8_t *cmd, int8_t *d0, int8_t *d1, int8_t *d2, int8_t *d3, int8_t *rst);

#ifdef __cplusplus
}
#endif

#endif /* defined(CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE) || defined(CONFIG_ESP_WIFI_REMOTE_ENABLED) */
#endif /* MAIN_ESP32_HAL_HOSTED_H_ */
1 change: 1 addition & 0 deletions cores/esp32/esp32-hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ void yield(void);
#include "esp32-hal-psram.h"
#include "esp32-hal-rgb-led.h"
#include "esp32-hal-cpu.h"
#include "esp32-hal-hosted.h"

void analogWrite(uint8_t pin, int value);
void analogWriteFrequency(uint8_t pin, uint32_t freq);
Expand Down
5 changes: 2 additions & 3 deletions libraries/BLE/src/BLE2901.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
*/

#include "soc/soc_caps.h"
#if SOC_BLE_SUPPORTED

#include "sdkconfig.h"
#if defined(SOC_BLE_SUPPORTED) || defined(CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE)
#if defined(CONFIG_BLUEDROID_ENABLED) || defined(CONFIG_NIMBLE_ENABLED)

/***************************************************************************
Expand Down Expand Up @@ -56,4 +55,4 @@ void BLE2901::setDescription(const String &userDesc) {
}

#endif /* CONFIG_BLUEDROID_ENABLED || CONFIG_NIMBLE_ENABLED */
#endif /* SOC_BLE_SUPPORTED */
#endif /* SOC_BLE_SUPPORTED || CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE */
6 changes: 3 additions & 3 deletions libraries/BLE/src/BLE2901.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
#define COMPONENTS_CPP_UTILS_BLE2901_H_

#include "soc/soc_caps.h"
#if SOC_BLE_SUPPORTED

#include "sdkconfig.h"
#if defined(SOC_BLE_SUPPORTED) || defined(CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE)
#if defined(CONFIG_BLUEDROID_ENABLED) || defined(CONFIG_NIMBLE_ENABLED)

/***************************************************************************
Expand All @@ -44,5 +43,6 @@ class BLE2901 : public BLEDescriptor {
}; // BLE2901

#endif /* CONFIG_BLUEDROID_ENABLED || CONFIG_NIMBLE_ENABLED */
#endif /* SOC_BLE_SUPPORTED */
#endif /* SOC_BLE_SUPPORTED || CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE */

#endif /* COMPONENTS_CPP_UTILS_BLE2901_H_ */
5 changes: 2 additions & 3 deletions libraries/BLE/src/BLE2902.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
*/

#include "soc/soc_caps.h"
#if SOC_BLE_SUPPORTED

#include "sdkconfig.h"
#if defined(SOC_BLE_SUPPORTED) || defined(CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE)
#if defined(CONFIG_BLUEDROID_ENABLED) || defined(CONFIG_NIMBLE_ENABLED)

/***************************************************************************
Expand Down Expand Up @@ -126,4 +125,4 @@ void BLE2902::setNotifications(bool flag) {
}

#endif /* CONFIG_BLUEDROID_ENABLED || CONFIG_NIMBLE_ENABLED */
#endif /* SOC_BLE_SUPPORTED */
#endif /* SOC_BLE_SUPPORTED || CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE */
6 changes: 3 additions & 3 deletions libraries/BLE/src/BLE2902.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
#define COMPONENTS_CPP_UTILS_BLE2902_H_

#include "soc/soc_caps.h"
#if SOC_BLE_SUPPORTED

#include "sdkconfig.h"
#if defined(SOC_BLE_SUPPORTED) || defined(CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE)
#if defined(CONFIG_BLUEDROID_ENABLED) || defined(CONFIG_NIMBLE_ENABLED)

/***************************************************************************
Expand Down Expand Up @@ -62,5 +61,6 @@ This class will be removed in a future version.")]] BLE2902 : public BLEDescript
}; // BLE2902

#endif /* CONFIG_BLUEDROID_ENABLED || CONFIG_NIMBLE_ENABLED */
#endif /* SOC_BLE_SUPPORTED */
#endif /* SOC_BLE_SUPPORTED || CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE */

#endif /* COMPONENTS_CPP_UTILS_BLE2902_H_ */
6 changes: 3 additions & 3 deletions libraries/BLE/src/BLE2904.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
* See also:
* https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.characteristic_presentation_format.xml
*/
#include "soc/soc_caps.h"
#if SOC_BLE_SUPPORTED

#include "soc/soc_caps.h"
#include "sdkconfig.h"
#if defined(SOC_BLE_SUPPORTED) || defined(CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE)
#if defined(CONFIG_BLUEDROID_ENABLED) || defined(CONFIG_NIMBLE_ENABLED)

/***************************************************************************
Expand Down Expand Up @@ -85,4 +85,4 @@ void BLE2904::setUnit(uint16_t unit) {
}

#endif /* CONFIG_BLUEDROID_ENABLED || CONFIG_NIMBLE_ENABLED */
#endif /* SOC_BLE_SUPPORTED */
#endif /* SOC_BLE_SUPPORTED || CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE */
6 changes: 3 additions & 3 deletions libraries/BLE/src/BLE2904.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
#define COMPONENTS_CPP_UTILS_BLE2904_H_

#include "soc/soc_caps.h"
#if SOC_BLE_SUPPORTED

#include "sdkconfig.h"
#if defined(SOC_BLE_SUPPORTED) || defined(CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE)
#if defined(CONFIG_BLUEDROID_ENABLED) || defined(CONFIG_NIMBLE_ENABLED)

/***************************************************************************
Expand Down Expand Up @@ -105,5 +104,6 @@ class BLE2904 : public BLEDescriptor {
}; // BLE2904

#endif /* CONFIG_BLUEDROID_ENABLED || CONFIG_NIMBLE_ENABLED */
#endif /* SOC_BLE_SUPPORTED */
#endif /* SOC_BLE_SUPPORTED || CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE */

#endif /* COMPONENTS_CPP_UTILS_BLE2904_H_ */
5 changes: 2 additions & 3 deletions libraries/BLE/src/BLEAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
*/

#include "soc/soc_caps.h"
#if SOC_BLE_SUPPORTED

#include "sdkconfig.h"
#if defined(SOC_BLE_SUPPORTED) || defined(CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE)
#if defined(CONFIG_BLUEDROID_ENABLED) || defined(CONFIG_NIMBLE_ENABLED)

/***************************************************************************
Expand Down Expand Up @@ -227,4 +226,4 @@ BLEAddress::BLEAddress(String stringAddress, uint8_t type) {
#endif

#endif /* CONFIG_BLUEDROID_ENABLED || CONFIG_NIMBLE_ENABLED */
#endif /* SOC_BLE_SUPPORTED */
#endif /* SOC_BLE_SUPPORTED || CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE */
Loading
Loading