-
Notifications
You must be signed in to change notification settings - Fork 7.7k
feat(ble): Enable BLE for ESP32-P4 through esp-hosted #11804
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a099878
feat(ble): Enable BLE for ESP32-P4 through esp-hosted
lucasssvaz e038f4f
fix(core): Add missing hosted file
lucasssvaz f794498
fix(hosted): Make functions static
lucasssvaz 33b959f
Merge branch 'master' into feat/p4_ble
me-no-dev 2e1019f
feat(ble): Enable BLE examples for P4
lucasssvaz 4e510de
fix(ble): Add missing definition when using P4
lucasssvaz 99c7ceb
ci(pre-commit): Apply automatic fixes
pre-commit-ci-lite[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(¤t_clk, ¤t_cmd, ¤t_d0, ¤t_d1, ¤t_d2, ¤t_d3, ¤t_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) */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.