| 
 | 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(¤t_clk, ¤t_cmd, ¤t_d0, ¤t_d1, ¤t_d2, ¤t_d3, ¤t_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) */  | 
0 commit comments