esp-idf icon indicating copy to clipboard operation
esp-idf copied to clipboard

ESP32C6 - WiFi 6 - Targeted Wait Time (TWT) - Require Functions: Wakeup Event / Auto Light Sleep Duration (IDFGH-12265)

Open djangoa opened this issue 1 year ago • 5 comments

Is your feature request related to a problem?

After many days of trying to get TWT functional, one of the main selling points of ESP32-C6, I can't get the current implementation (ESP-IDF 5.1.3/5.2) to be usable and/or save enough power to warrant using it over deep sleep in any circumstance. While the example itwt works to reduce current consumption (I've measured an average of ~110ua) during automated light sleep, it's impossible to integrate it with your own application code for non-trivial wakeup times (i.e. >10s vs the 500ms as shown in the example code), as nothing in ESP-IDF exposes when WiFi will be awake and able to send data. This has been highlighted indirectly previously several months ago:

"Until now, we don't have a way to get the light sleep duration and the next wake up trigger. And we will enable a twt wakeup event, which is currently in progress." Originally posted by @xuxiao111 in https://github.com/espressif/esp-idf/issues/12441#issuecomment-1775029218

Describe the solution you'd like.

New functionality:

  1. Providing a WIFI_EVENT (e.g. "WIFI_EVENT_ITWT_AWAKE") enabling an application to register a callback function via esp_event_handler_instance_register() when WiFi is awake after a TWT has expired.

  2. Providing a function to ascertain the remaining automatic light sleep duration until next targeted wakeup.

Describe alternatives you've considered.

I've tried working round this issue using functions in esp_wifi/include/esp_wifi_he.h such as:

esp_err_t esp_wifi_sta_itwt_teardown(int flow_id); esp_err_t esp_wifi_sta_itwt_setup(wifi_twt_setup_config_t *setup_config);

and

esp_err_t esp_wifi_sta_itwt_suspend(int flow_id, int suspend_time_ms);

to temporarily disable TWT but neither work. Invoking the first 2 functions to teardown and then setup again TWT more than once causes the Wifi station to disconnect (Issue TBC and submitted). Using suspend disables TWT but never re-enables when using either the suspend_time_ms or calling the function again with suspend_time_ms=0 as per the function brief (See https://github.com/espressif/esp-idf/issues/13312#issue-2168108301).

Additional context.

Is there any indication when this functionality will be complete?

djangoa avatar Mar 04 '24 19:03 djangoa

In reference to https://github.com/espressif/esp-idf/issues/13312#issuecomment-1980948036, I've managed to create a workaround to this issue using the actual_suspend_time_ms when invoking esp_wifi_sta_itwt_suspend(int flow_id, int suspend_time_ms); to ascertain the remaining sleep time until the net targeted wake.

It's not ideal as I have to calculate the next remaining sleep time by adding the previous actual_suspend_time_ms to the TWT wake interval and subtracting the max execution time of my application and the call to esp_wifi_sta_itwt_suspend(). Additionally to calculate the time I have no choice but to suspend.

I've also managed to get the esp_wifi_sta_itwt_teardown() and esp_wifi_sta_itwt_setup() to function with out causing superfluous disconnects. I'm also not sure how either as previously wifi would reliably disconnect and reconnect when tearing down, invoking a TCP connection and setting up a TWT agreement after a single iteration. Maybe it's a timing issue when tearing down too close to a TWT interval expiring?

djangoa avatar Mar 06 '24 14:03 djangoa

Hi @djangoa, we will provide a WIFI_EVENT (WIFI_EVENT_TWT_AWAKE) when WiFi is awake. And now it‘s in the process of code review. we will merge it ASAP.

xuxiao111 avatar Mar 07 '24 08:03 xuxiao111

@xuxiao111 Thanks for the update, looking forward to the result.

djangoa avatar Mar 07 '24 15:03 djangoa

Any progress in this issue? I would appreciate WIFI_EVENT_TWT_AWAKE as well.

tom-van avatar Mar 30 '25 20:03 tom-van

Hi @tom-van, the TWT wakeup event functionality has been supported in commit 27f61966cdd65da99138eaa61b4c02ed5b75fcb0. You can call “esp_err_t esp_wifi_sta_twt_config(wifi_twt_config_t *config);” to enable this feature.

xuxiao111 avatar Mar 31 '25 07:03 xuxiao111

Dear All, how are you?

I am currently using ESP-IDF 5.4.1 at my ESP32C6 but I cant make the WIFI_EVENT_TWT_AWAKE work.

I am using the itwt example and did the following changes:

Added in wifi_itwt(void) function with other handlers:
      ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
                      WIFI_EVENT_TWT_WAKEUP,
                      &itwt_wkp_handler,
                      NULL,
                      NULL));

And added the handler function:

static void itwt_wkp_handler(void *arg, esp_event_base_t event_base,
                               int32_t event_id, void *event_data)
{
    wifi_event_sta_twt_wakeup_t *wake = (wifi_event_sta_twt_wakeup_t *) event_data;
    ESP_LOGI(TAG, "<WIFI_EVENT_ITWT_WAKEUP>flow_id:%d, twt_type:%d", wake->flow_id, wake->twt_type);
}

The problem is that it nevers enters the handler function. Am I doing something wrong?

gbarranovac avatar May 22 '25 21:05 gbarranovac

Hi @gbarranovac, the current design logic defaults to not posting TWT event notifications. To enable them, you need to call "esp_wifi_sta_twt_config". You can refer to the following configuration as an example.

Image

You can have a try. If you have any questions, please let me know.

xuxiao111 avatar May 23 '25 02:05 xuxiao111