espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.35k stars 7.21k forks source link

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

Open djangoa opened 6 months ago

djangoa commented 6 months ago

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_wifihe.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 *setupconfig);

and

_esp_err_t esp_wifi_sta_itwt_suspend(int flow_id, int suspend_timems);

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 commented 6 months ago

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_timems when invoking _esp_wifi_sta_itwt_suspend(int flow_id, int suspend_timems); 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_timems to the TWT wake interval and subtracting the max execution time of my application and the call to _esp_wifi_sta_itwtsuspend(). Additionally to calculate the time I have no choice but to suspend.

I've also managed to get the _esp_wifi_sta_itwtteardown() and _esp_wifi_sta_itwtsetup() 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?

xuxiao111 commented 6 months ago

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.

djangoa commented 6 months ago

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