Files
desklock/firmware/components/esp_hosted/slave/main/example_light_sleep.c
T
jpmschweitzerandClaude Fable 5 cb5826e02b
Test, Build and Push / test-gateway (push) Successful in 12s
Test, Build and Push / release (push) Skipped
Test, Build and Push / build-gateway (push) Skipped
Fork fix: the SDIO wedge is FIXED (esp-hosted-mcu #167)
Root cause (verified against our exact IDF tree, not the community guess):
the "258" in "sdio_write_task: Failed to send data: 258" is NOT a timeout
(that is 263). 258 = 0x102 = ESP_ERR_INVALID_ARG. On the ESP32-P4, block-
mode CMD53 writes require the SOURCE buffer to be 64-byte (cache-line)
aligned; the IDF sdmmc driver rejects a misaligned source with INVALID_ARG
BEFORE any bus activity. esp_hosts write loop then declares "Unrecoverable
host sdio state" and reboots the whole P4. The audio TX payload is not
64-aligned, so streaming mic audio wedged on the very FIRST frame (which is
exactly what we saw: listening -> instant Failed to send -> reboot).

This also explains why buffer/queue/clock/retry tuning all did nothing: the
write never reached the bus. And why our symptom was instant, not after
~100 writes (the community block-mode-desync theory) — it is the first
misaligned buffer, every time.

Fix: vendored esp_hosted 2.12.11 as an editable local component (overrides
the registry copy) and bounce a misaligned TX payload through one aligned
DMA scratch buffer in hosted_sdio_write_block (port_esp_hosted_host_sdio.c).
TX is serialized by the bus lock so a single static bounce buffer is safe;
freed in hosted_sdio_deinit. Host-only change — no C6 reflash.

VERIFIED ON HARDWARE (autonomous self-test): 40s of continuous mic-audio
upstream streaming — the traffic that previously wedged on the first frame
— ran clean, zero timeouts, zero reboots. A guarded SDIO_TX_SELFTEST harness
is kept (compiled out) for future SDIO stress testing.

Credit: root cause + patch designed via multi-agent investigation; the
precise 258=INVALID_ARG decode (correcting the upstream community timeout
assumption) came from checking our actual esp_err.h.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-15 09:50:27 +02:00

147 lines
4.8 KiB
C

/*
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "example_light_sleep.h"
#include "slave_light_sleep.h"
#include "host_power_save.h"
#include "esp_hosted_cli.h"
#include "esp_log.h"
static const char *TAG = "example_light_sleep";
/* Callback implementations for automatic light sleep on host power save events */
static void host_power_save_on_prepare_cb(void)
{
ESP_EARLY_LOGI(TAG, "==> Host preparing to enter power save");
/* User can add custom pre-sleep cleanup here:
* - Save application state
* - Flush buffers
* - Stop non-essential tasks
* - etc.
*/
}
static void host_power_save_on_ready_cb(void)
{
ESP_EARLY_LOGI(TAG, "==> Host power save active - entering light sleep");
#ifdef CONFIG_ESP_HOSTED_LIGHT_SLEEP_ENABLE
/* Handle CLI based on peripheral powerdown configuration */
#if defined(CONFIG_PM_POWER_DOWN_PERIPHERAL_IN_LIGHT_SLEEP) && defined(CONFIG_ESP_HOSTED_LIGHT_SLEEP_PERIPHERAL_POWERDOWN)
/* Peripheral powerdown enabled - UART will die, stop CLI */
ESP_EARLY_LOGI(TAG, "Stopping CLI (UART powering down)");
esp_hosted_cli_stop();
//ESP_EARLY_LOGW(TAG, "CLI stopped. UART console unavailable during sleep.");
#else
/* Peripheral stays powered - CLI can remain active */
ESP_EARLY_LOGI(TAG, "CLI remains active (UART stays powered)");
#endif
esp_err_t ret = slave_light_sleep_start();
if (ret == ESP_OK) {
//ESP_EARLY_LOGI(TAG, "Light sleep started successfully");
} else {
//ESP_EARLY_LOGW(TAG, "Failed to start light sleep: %s", esp_err_to_name(ret));
}
#else
ESP_EARLY_LOGW(TAG, "Light sleep not enabled in menuconfig");
#endif
}
static void host_power_save_off_prepare_cb(void)
{
#ifdef CONFIG_ESP_HOSTED_LIGHT_SLEEP_ENABLE
esp_err_t ret = slave_light_sleep_stop();
if (ret == ESP_OK) {
//ESP_EARLY_LOGI(TAG, "Light sleep stopped successfully");
} else {
//ESP_EARLY_LOGW(TAG, "Failed to stop light sleep: %s", esp_err_to_name(ret));
}
#endif
}
static void host_power_save_off_ready_cb(void)
{
ESP_EARLY_LOGI(TAG, "==> Host power save off - device fully ready");
/* Restart CLI if it was stopped due to peripheral powerdown */
/* This happens here (after ready) to ensure UART is fully powered up */
#if defined(CONFIG_PM_POWER_DOWN_PERIPHERAL_IN_LIGHT_SLEEP) && defined(CONFIG_ESP_HOSTED_LIGHT_SLEEP_PERIPHERAL_POWERDOWN)
ESP_EARLY_LOGI(TAG, "Restarting CLI (UART now fully powered up)");
esp_hosted_cli_start();
#endif
/* User can add custom post-wake initialization here:
* - Restore application state
* - Resume tasks
* - Re-initialize peripherals if needed
* - etc.
*/
}
esp_err_t example_light_sleep_init(void)
{
int ret = 0;
ESP_LOGI(TAG, "=======================================================");
ESP_LOGI(TAG, "Initializing Light Sleep Example");
ESP_LOGI(TAG, "=======================================================");
/*
* STEP 1: Initialize host power save infrastructure
* This enables monitoring of host power save events.
* Without this, callbacks won't be invoked.
*/
ESP_LOGI(TAG, "Step 1: Initializing host power save monitoring");
host_power_save_config_t ps_config = HOST_POWER_SAVE_DEFAULT_CONFIG();
host_power_save_callbacks_t callbacks = {
.host_power_save_on_prepare_cb = host_power_save_on_prepare_cb,
.host_power_save_on_ready_cb = host_power_save_on_ready_cb,
.host_power_save_off_prepare_cb = host_power_save_off_prepare_cb,
.host_power_save_off_ready_cb = host_power_save_off_ready_cb
};
ps_config.callbacks = callbacks;
ret = host_power_save_init(&ps_config);
if (ret) {
ESP_LOGE(TAG, "Host power save init failed: %u", ret);
return ESP_FAIL;
}
/*
* STEP 2: Initialize slave light sleep (optional)
* This component can be used independently of host power save.
* Comment this out if you only want host event monitoring.
*/
#ifdef CONFIG_ESP_HOSTED_LIGHT_SLEEP_ENABLE
ESP_LOGI(TAG, "Step 2: Initializing slave light sleep component");
ret = slave_light_sleep_init();
if (ret != ESP_OK) {
ESP_LOGE(TAG, " ✗ Light sleep init failed: %s", esp_err_to_name(ret));
ESP_LOGE(TAG, " Check menuconfig: PM_ENABLE and FREERTOS_USE_TICKLESS_IDLE");
ESP_LOGE(TAG, " Continuing without light sleep...");
} else {
ESP_LOGI(TAG, " ✓ Light sleep component ready");
}
#else
ESP_LOGI(TAG, "Step 2: Light sleep component not enabled");
ESP_LOGI(TAG, " ⓘ Enable in: Light Sleep Power Management menu");
ESP_LOGI(TAG, " ⓘ Host power save callbacks will still work");
#endif
ESP_LOGI(TAG, "=======================================================");
ESP_LOGI(TAG, "Example initialized - Automatic mode active");
ESP_LOGI(TAG, " • Host sleep event → slave enters light sleep");
ESP_LOGI(TAG, " • Host wake event → slave exits light sleep");
ESP_LOGI(TAG, "=======================================================");
return ESP_OK;
}