|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + */ |
| 6 | +#include "integration/wifi_remote/hosted_safe.h" |
| 7 | + |
| 8 | +#include "driver/gpio.h" |
| 9 | +#include "esp_log.h" |
| 10 | +#include "freertos/FreeRTOS.h" |
| 11 | +#include "freertos/task.h" |
| 12 | +#include "sdkconfig.h" |
| 13 | + |
| 14 | +#if CONFIG_M5TAB5_USE_ESP_HOSTED |
| 15 | +# include "esp_hosted_api.h" |
| 16 | +# include "esp_wifi.h" |
| 17 | +# include "esp_wifi_remote.h" |
| 18 | +#endif |
| 19 | + |
| 20 | +static const char* TAG = "hosted_safe"; |
| 21 | +static bool s_hosted_ready = false; |
| 22 | + |
| 23 | +#if CONFIG_M5TAB5_USE_ESP_HOSTED |
| 24 | +static void slave_pulse_reset(void) |
| 25 | +{ |
| 26 | + const gpio_num_t rst = 54; |
| 27 | + const gpio_config_t io = { |
| 28 | + .pin_bit_mask = 1ULL << rst, |
| 29 | + .mode = GPIO_MODE_OUTPUT, |
| 30 | + .pull_up_en = 0, |
| 31 | + .pull_down_en = 0, |
| 32 | + .intr_type = GPIO_INTR_DISABLE, |
| 33 | + }; |
| 34 | + gpio_config(&io); |
| 35 | + gpio_set_level(rst, 0); |
| 36 | + vTaskDelay(pdMS_TO_TICKS(5)); |
| 37 | + gpio_set_level(rst, 1); |
| 38 | +} |
| 39 | +#endif |
| 40 | + |
| 41 | +bool hosted_try_init_with_retries(void) |
| 42 | +{ |
| 43 | +#if !CONFIG_M5TAB5_USE_ESP_HOSTED |
| 44 | + ESP_LOGI(TAG, "ESP-Hosted disabled via Kconfig."); |
| 45 | + return false; |
| 46 | +#else |
| 47 | + if (s_hosted_ready) |
| 48 | + { |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + int retries = CONFIG_M5TAB5_HOSTED_BOOT_RETRIES; |
| 53 | + while (retries-- >= 0) |
| 54 | + { |
| 55 | + ESP_LOGI( |
| 56 | + TAG, "Resetting SDIO slave and waiting %d ms ...", CONFIG_M5TAB5_HOSTED_BOOT_DELAY_MS); |
| 57 | + slave_pulse_reset(); |
| 58 | + vTaskDelay(pdMS_TO_TICKS(CONFIG_M5TAB5_HOSTED_BOOT_DELAY_MS)); |
| 59 | + |
| 60 | + esp_err_t err = esp_hosted_init(); |
| 61 | + if (err != ESP_OK) |
| 62 | + { |
| 63 | + ESP_LOGW(TAG, "esp_hosted_init failed (%s)", esp_err_to_name(err)); |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + ESP_LOGI(TAG, "ESP-Hosted initialized."); |
| 68 | + s_hosted_ready = true; |
| 69 | + |
| 70 | + wifi_init_config_t wifi_cfg = WIFI_INIT_CONFIG_DEFAULT(); |
| 71 | + err = esp_wifi_remote_init(&wifi_cfg); |
| 72 | + if (err != ESP_OK) |
| 73 | + { |
| 74 | + ESP_LOGW(TAG, |
| 75 | + "esp_wifi_remote_init failed (%s), continuing without Wi-Fi.", |
| 76 | + esp_err_to_name(err)); |
| 77 | + hosted_deinit_safe(); |
| 78 | + s_hosted_ready = false; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (s_hosted_ready) |
| 83 | + { |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + ESP_LOGW(TAG, "ESP-Hosted init failed; %d retry(ies) left.", retries); |
| 88 | + vTaskDelay(pdMS_TO_TICKS(250)); |
| 89 | + } |
| 90 | + |
| 91 | + ESP_LOGW(TAG, "ESP-Hosted not available; booting without Wi-Fi."); |
| 92 | + return false; |
| 93 | +#endif |
| 94 | +} |
| 95 | + |
| 96 | +void hosted_deinit_safe(void) |
| 97 | +{ |
| 98 | +#if CONFIG_M5TAB5_USE_ESP_HOSTED |
| 99 | + if (!s_hosted_ready) |
| 100 | + { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + esp_wifi_remote_deinit(); |
| 105 | + esp_hosted_deinit(); |
| 106 | + s_hosted_ready = false; |
| 107 | +#endif |
| 108 | +} |
0 commit comments