Skip to content

Commit a099878

Browse files
committed
feat(ble): Enable BLE for ESP32-P4 through esp-hosted
1 parent 7436eab commit a099878

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+479
-273
lines changed

cores/esp32/esp32-hal-hosted.c

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
// Copyright 2015-2025 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "sdkconfig.h"
16+
#if defined(CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE) || defined(CONFIG_ESP_WIFI_REMOTE_ENABLED)
17+
18+
#include "esp32-hal-hosted.h"
19+
#include "esp32-hal-log.h"
20+
21+
#include "esp_hosted_transport_config.h"
22+
extern esp_err_t esp_hosted_init();
23+
extern esp_err_t esp_hosted_deinit();
24+
25+
static bool hosted_initialized = false;
26+
static bool hosted_ble_active = false;
27+
static bool hosted_wifi_active = false;
28+
29+
static sdio_pin_config_t sdio_pin_config = {
30+
#ifdef BOARD_HAS_SDIO_ESP_HOSTED
31+
.pin_clk = BOARD_SDIO_ESP_HOSTED_CLK,
32+
.pin_cmd = BOARD_SDIO_ESP_HOSTED_CMD,
33+
.pin_d0 = BOARD_SDIO_ESP_HOSTED_D0,
34+
.pin_d1 = BOARD_SDIO_ESP_HOSTED_D1,
35+
.pin_d2 = BOARD_SDIO_ESP_HOSTED_D2,
36+
.pin_d3 = BOARD_SDIO_ESP_HOSTED_D3,
37+
.pin_reset = BOARD_SDIO_ESP_HOSTED_RESET
38+
#else
39+
.pin_clk = CONFIG_ESP_SDIO_PIN_CLK,
40+
.pin_cmd = CONFIG_ESP_SDIO_PIN_CMD,
41+
.pin_d0 = CONFIG_ESP_SDIO_PIN_D0,
42+
.pin_d1 = CONFIG_ESP_SDIO_PIN_D1,
43+
.pin_d2 = CONFIG_ESP_SDIO_PIN_D2,
44+
.pin_d3 = CONFIG_ESP_SDIO_PIN_D3,
45+
.pin_reset = CONFIG_ESP_SDIO_GPIO_RESET_SLAVE
46+
#endif
47+
};
48+
49+
// Forward declarations
50+
bool hostedInit();
51+
bool hostedDeinit();
52+
53+
bool hostedInitBLE() {
54+
log_i("Initializing ESP-Hosted for BLE");
55+
hosted_ble_active = true;
56+
return hostedInit();
57+
}
58+
59+
bool hostedInitWiFi() {
60+
log_i("Initializing ESP-Hosted for WiFi");
61+
hosted_wifi_active = true;
62+
return hostedInit();
63+
}
64+
65+
bool hostedDeinitBLE() {
66+
log_i("Deinitializing ESP-Hosted for BLE");
67+
hosted_ble_active = false;
68+
if (!hosted_wifi_active) {
69+
return hostedDeinit();
70+
} else {
71+
log_i("ESP-Hosted is still being used by Wi-Fi. Skipping deinit.");
72+
return true;
73+
}
74+
}
75+
76+
bool hostedDeinitWiFi() {
77+
log_i("Deinitializing ESP-Hosted for WiFi");
78+
hosted_wifi_active = false;
79+
if (!hosted_ble_active) {
80+
return hostedDeinit();
81+
} else {
82+
log_i("ESP-Hosted is still being used by BLE. Skipping deinit.");
83+
return true;
84+
}
85+
}
86+
87+
bool hostedInit() {
88+
if (!hosted_initialized) {
89+
log_i("Initializing ESP-Hosted");
90+
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);
91+
hosted_initialized = true;
92+
struct esp_hosted_sdio_config conf = INIT_DEFAULT_HOST_SDIO_CONFIG();
93+
conf.pin_clk.pin = sdio_pin_config.pin_clk;
94+
conf.pin_cmd.pin = sdio_pin_config.pin_cmd;
95+
conf.pin_d0.pin = sdio_pin_config.pin_d0;
96+
conf.pin_d1.pin = sdio_pin_config.pin_d1;
97+
conf.pin_d2.pin = sdio_pin_config.pin_d2;
98+
conf.pin_d3.pin = sdio_pin_config.pin_d3;
99+
conf.pin_reset.pin = sdio_pin_config.pin_reset;
100+
// esp_hosted_sdio_set_config() will fail on second attempt but here temporarily to not cause exception on reinit
101+
if (esp_hosted_sdio_set_config(&conf) != ESP_OK || esp_hosted_init() != ESP_OK) {
102+
log_e("esp_hosted_init failed!");
103+
hosted_initialized = false;
104+
return false;
105+
}
106+
log_i("ESP-Hosted initialized!");
107+
}
108+
109+
// Attach pins to PeriMan here
110+
// Slave chip model is CONFIG_IDF_SLAVE_TARGET
111+
// sdio_pin_config.pin_clk
112+
// sdio_pin_config.pin_cmd
113+
// sdio_pin_config.pin_d0
114+
// sdio_pin_config.pin_d1
115+
// sdio_pin_config.pin_d2
116+
// sdio_pin_config.pin_d3
117+
// sdio_pin_config.pin_reset
118+
119+
return true;
120+
}
121+
122+
bool hostedSetPins(int8_t clk, int8_t cmd, int8_t d0, int8_t d1, int8_t d2, int8_t d3, int8_t rst) {
123+
if (clk < 0 || cmd < 0 || d0 < 0 || d1 < 0 || d2 < 0 || d3 < 0 || rst < 0) {
124+
log_e("All SDIO pins must be defined");
125+
return false;
126+
}
127+
128+
if (hosted_initialized) {
129+
int8_t current_clk, current_cmd, current_d0, current_d1, current_d2, current_d3, current_rst;
130+
hostedGetPins(&current_clk, &current_cmd, &current_d0, &current_d1, &current_d2, &current_d3, &current_rst);
131+
log_e("SDIO pins must be set before ESP-Hosted is initialized");
132+
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);
133+
return false;
134+
}
135+
136+
sdio_pin_config.pin_clk = clk;
137+
sdio_pin_config.pin_cmd = cmd;
138+
sdio_pin_config.pin_d0 = d0;
139+
sdio_pin_config.pin_d1 = d1;
140+
sdio_pin_config.pin_d2 = d2;
141+
sdio_pin_config.pin_d3 = d3;
142+
sdio_pin_config.pin_reset = rst;
143+
return true;
144+
}
145+
146+
void hostedGetPins(int8_t *clk, int8_t *cmd, int8_t *d0, int8_t *d1, int8_t *d2, int8_t *d3, int8_t *rst) {
147+
*clk = sdio_pin_config.pin_clk;
148+
*cmd = sdio_pin_config.pin_cmd;
149+
*d0 = sdio_pin_config.pin_d0;
150+
*d1 = sdio_pin_config.pin_d1;
151+
*d2 = sdio_pin_config.pin_d2;
152+
*d3 = sdio_pin_config.pin_d3;
153+
*rst = sdio_pin_config.pin_reset;
154+
}
155+
156+
bool hostedIsBLEActive() {
157+
return hosted_ble_active;
158+
}
159+
160+
bool hostedIsWiFiActive() {
161+
return hosted_wifi_active;
162+
}
163+
164+
bool hostedDeinit() {
165+
if (!hosted_initialized) {
166+
log_e("ESP-Hosted is not initialized");
167+
return false;
168+
}
169+
170+
if (esp_hosted_deinit() != ESP_OK) {
171+
log_e("esp_hosted_deinit failed!");
172+
return false;
173+
}
174+
175+
hosted_initialized = false;
176+
return true;
177+
}
178+
179+
bool hostedIsInitialized() {
180+
return hosted_initialized;
181+
}
182+
183+
#endif /* defined(CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE) || defined(CONFIG_ESP_WIFI_REMOTE_ENABLED) */

cores/esp32/esp32-hal-hosted.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2015-2025 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef MAIN_ESP32_HAL_HOSTED_H_
16+
#define MAIN_ESP32_HAL_HOSTED_H_
17+
18+
#include "sdkconfig.h"
19+
#if defined(CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE) || defined(CONFIG_ESP_WIFI_REMOTE_ENABLED)
20+
21+
#include "stdint.h"
22+
#include "stdbool.h"
23+
24+
#ifdef __cplusplus
25+
extern "C" {
26+
#endif
27+
28+
typedef struct {
29+
uint8_t pin_clk;
30+
uint8_t pin_cmd;
31+
uint8_t pin_d0;
32+
uint8_t pin_d1;
33+
uint8_t pin_d2;
34+
uint8_t pin_d3;
35+
uint8_t pin_reset;
36+
} sdio_pin_config_t;
37+
38+
bool hostedInitBLE();
39+
bool hostedInitWiFi();
40+
bool hostedDeinitBLE();
41+
bool hostedDeinitWiFi();
42+
bool hostedIsInitialized();
43+
bool hostedIsBLEActive();
44+
bool hostedIsWiFiActive();
45+
bool hostedSetPins(int8_t clk, int8_t cmd, int8_t d0, int8_t d1, int8_t d2, int8_t d3, int8_t rst);
46+
void hostedGetPins(int8_t *clk, int8_t *cmd, int8_t *d0, int8_t *d1, int8_t *d2, int8_t *d3, int8_t *rst);
47+
48+
#ifdef __cplusplus
49+
}
50+
#endif
51+
52+
#endif /* defined(CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE) || defined(CONFIG_ESP_WIFI_REMOTE_ENABLED) */
53+
#endif /* MAIN_ESP32_HAL_HOSTED_H_ */

cores/esp32/esp32-hal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ void yield(void);
100100
#include "esp32-hal-psram.h"
101101
#include "esp32-hal-rgb-led.h"
102102
#include "esp32-hal-cpu.h"
103+
#include "esp32-hal-hosted.h"
103104

104105
void analogWrite(uint8_t pin, int value);
105106
void analogWriteFrequency(uint8_t pin, uint32_t freq);

libraries/BLE/src/BLE2901.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
*/
1717

1818
#include "soc/soc_caps.h"
19-
#if SOC_BLE_SUPPORTED
20-
2119
#include "sdkconfig.h"
20+
#if defined(SOC_BLE_SUPPORTED) || defined(CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE)
2221
#if defined(CONFIG_BLUEDROID_ENABLED) || defined(CONFIG_NIMBLE_ENABLED)
2322

2423
/***************************************************************************
@@ -56,4 +55,4 @@ void BLE2901::setDescription(const String &userDesc) {
5655
}
5756

5857
#endif /* CONFIG_BLUEDROID_ENABLED || CONFIG_NIMBLE_ENABLED */
59-
#endif /* SOC_BLE_SUPPORTED */
58+
#endif /* SOC_BLE_SUPPORTED || CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE */

libraries/BLE/src/BLE2901.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
#define COMPONENTS_CPP_UTILS_BLE2901_H_
2020

2121
#include "soc/soc_caps.h"
22-
#if SOC_BLE_SUPPORTED
23-
2422
#include "sdkconfig.h"
23+
#if defined(SOC_BLE_SUPPORTED) || defined(CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE)
2524
#if defined(CONFIG_BLUEDROID_ENABLED) || defined(CONFIG_NIMBLE_ENABLED)
2625

2726
/***************************************************************************
@@ -44,5 +43,6 @@ class BLE2901 : public BLEDescriptor {
4443
}; // BLE2901
4544

4645
#endif /* CONFIG_BLUEDROID_ENABLED || CONFIG_NIMBLE_ENABLED */
47-
#endif /* SOC_BLE_SUPPORTED */
46+
#endif /* SOC_BLE_SUPPORTED || CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE */
47+
4848
#endif /* COMPONENTS_CPP_UTILS_BLE2901_H_ */

libraries/BLE/src/BLE2902.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
*/
1616

1717
#include "soc/soc_caps.h"
18-
#if SOC_BLE_SUPPORTED
19-
2018
#include "sdkconfig.h"
19+
#if defined(SOC_BLE_SUPPORTED) || defined(CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE)
2120
#if defined(CONFIG_BLUEDROID_ENABLED) || defined(CONFIG_NIMBLE_ENABLED)
2221

2322
/***************************************************************************
@@ -126,4 +125,4 @@ void BLE2902::setNotifications(bool flag) {
126125
}
127126

128127
#endif /* CONFIG_BLUEDROID_ENABLED || CONFIG_NIMBLE_ENABLED */
129-
#endif /* SOC_BLE_SUPPORTED */
128+
#endif /* SOC_BLE_SUPPORTED || CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE */

libraries/BLE/src/BLE2902.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
#define COMPONENTS_CPP_UTILS_BLE2902_H_
1414

1515
#include "soc/soc_caps.h"
16-
#if SOC_BLE_SUPPORTED
17-
1816
#include "sdkconfig.h"
17+
#if defined(SOC_BLE_SUPPORTED) || defined(CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE)
1918
#if defined(CONFIG_BLUEDROID_ENABLED) || defined(CONFIG_NIMBLE_ENABLED)
2019

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

6463
#endif /* CONFIG_BLUEDROID_ENABLED || CONFIG_NIMBLE_ENABLED */
65-
#endif /* SOC_BLE_SUPPORTED */
64+
#endif /* SOC_BLE_SUPPORTED || CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE */
65+
6666
#endif /* COMPONENTS_CPP_UTILS_BLE2902_H_ */

libraries/BLE/src/BLE2904.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
* See also:
1414
* https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.characteristic_presentation_format.xml
1515
*/
16-
#include "soc/soc_caps.h"
17-
#if SOC_BLE_SUPPORTED
1816

17+
#include "soc/soc_caps.h"
1918
#include "sdkconfig.h"
19+
#if defined(SOC_BLE_SUPPORTED) || defined(CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE)
2020
#if defined(CONFIG_BLUEDROID_ENABLED) || defined(CONFIG_NIMBLE_ENABLED)
2121

2222
/***************************************************************************
@@ -85,4 +85,4 @@ void BLE2904::setUnit(uint16_t unit) {
8585
}
8686

8787
#endif /* CONFIG_BLUEDROID_ENABLED || CONFIG_NIMBLE_ENABLED */
88-
#endif /* SOC_BLE_SUPPORTED */
88+
#endif /* SOC_BLE_SUPPORTED || CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE */

libraries/BLE/src/BLE2904.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
#define COMPONENTS_CPP_UTILS_BLE2904_H_
1414

1515
#include "soc/soc_caps.h"
16-
#if SOC_BLE_SUPPORTED
17-
1816
#include "sdkconfig.h"
17+
#if defined(SOC_BLE_SUPPORTED) || defined(CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE)
1918
#if defined(CONFIG_BLUEDROID_ENABLED) || defined(CONFIG_NIMBLE_ENABLED)
2019

2120
/***************************************************************************
@@ -105,5 +104,6 @@ class BLE2904 : public BLEDescriptor {
105104
}; // BLE2904
106105

107106
#endif /* CONFIG_BLUEDROID_ENABLED || CONFIG_NIMBLE_ENABLED */
108-
#endif /* SOC_BLE_SUPPORTED */
107+
#endif /* SOC_BLE_SUPPORTED || CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE */
108+
109109
#endif /* COMPONENTS_CPP_UTILS_BLE2904_H_ */

libraries/BLE/src/BLEAddress.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
*/
1111

1212
#include "soc/soc_caps.h"
13-
#if SOC_BLE_SUPPORTED
14-
1513
#include "sdkconfig.h"
14+
#if defined(SOC_BLE_SUPPORTED) || defined(CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE)
1615
#if defined(CONFIG_BLUEDROID_ENABLED) || defined(CONFIG_NIMBLE_ENABLED)
1716

1817
/***************************************************************************
@@ -227,4 +226,4 @@ BLEAddress::BLEAddress(String stringAddress, uint8_t type) {
227226
#endif
228227

229228
#endif /* CONFIG_BLUEDROID_ENABLED || CONFIG_NIMBLE_ENABLED */
230-
#endif /* SOC_BLE_SUPPORTED */
229+
#endif /* SOC_BLE_SUPPORTED || CONFIG_ESP_HOSTED_ENABLE_BT_NIMBLE */

0 commit comments

Comments
 (0)