Wi-Fi 6 Target Wake Time (TWT) on ESP32-C5: a hands-on test
See you in eight seconds
In last week’s post, the ESP32-C5 was the only one of the thirteen chips with 5 GHz Wi-Fi. It also speaks Wi-Fi 6, with a feature made for battery-powered devices: Target Wake Time (TWT).
The idea fits in one line: the device and the router agree on when to talk, and the device sleeps the rest of the time. On paper. So I asked my own router what it would accept.
The test bench
What was used for this post:
- an ESP32-C5 development board (ESP32-C5HR8, revision v1.0)
- ESP-IDF 6.1.1, on macOS
- a dual-band Wi-Fi 6 router, on 20 MHz channels, with WPA2
An appointment with the router
A Wi-Fi device that wants to save power cannot turn its radio off at will: the router may have data for it. The classic answer is to doze and wake up for some of the router’s beacons, sent about ten times per second by mine, to check whether something is waiting.
Target Wake Time, brought to Wi-Fi 6 by 802.11ax, replaces that with an appointment. The device and the router agree on a wake interval and on a minimum wake duration. Between two of those service periods, the device can doze while it stays associated.
The price is latency. Data sent to the device may wait in the router until the next one.
The flavour tested here is individual TWT (iTWT), a one-to-one agreement. The device asks with one of three words: request (the router picks the values), suggest (here are mine, but I am open) or demand (these or nothing). The router accepts, proposes something else or rejects.
Build the example
ESP-IDF ships an itwt example. With the framework installed in ~/esp/esp-idf, getting it ready takes three commands:
. ~/esp/esp-idf/export.sh
cp -R ~/esp/esp-idf/examples/wifi/itwt ./itwt && cd itwt
idf.py set-target esp32c5Before building, a few lines of the generated sdkconfig file need a change. Three of them got in the way, and are worth knowing before you start:
CONFIG_EXAMPLE_WIFI_SSID="your-network"
CONFIG_EXAMPLE_WIFI_PASSWORD="your-password"
# the example expects a static address: use DHCP
# CONFIG_EXAMPLE_ENABLE_STATIC_IP is not set
# console on the board's USB port, not on the UART pins
CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG=y
# no light sleep
# CONFIG_PM_ENABLE is not set
# CONFIG_FREERTOS_USE_TICKLESS_IDLE is not setThe last one deserves a word. Light sleep stops the processor between two tasks. On my board the USB port went with it, and the log ended right after the connection.
Without it, the Wi-Fi driver keeps its own power saving for the radio. And TWT only schedules when the board and the router expect to talk.
idf.py build
idf.py -p /dev/cu.usbmodem1101 flash
idf.py -p /dev/cu.usbmodem1101 monitorA first agreement
At association, the driver logs what the router announces:
wifi:(assoc)RESP, Extended Capabilities, MBSSID:0, TWT Responder:1, ...
wifi:(opr)len:7, TWT Required:0, ...
wifi:connected with <ssid>, channel 8, BW20(NONE, 00)
wifi:AP's beacon interval = 102400 us, DTIM period = 3TWT Responder:1 means the router says it can answer TWT requests. It is a promise, not a proof: only a real request tells. The example sends one as soon as it gets an IP address:
itwt: <WIFI_EVENT_ITWT_SETUP>twt_id:0, flow_id:1, trigger-enabled, announced, wake_dura:255, wake_dura_unit:0, wake_invl_e:10, wake_invl_m:512Accepted. The values need some decoding. The wake interval is a mantissa times a power of two, in microseconds: 512 × 2^10, so 524,288 µs, about half a second. The minimum wake duration counts in units of 256 µs: 255 of them make 65 ms.
A bench firmware
One request at boot does not say much, so my copy of the example became a small test bench. The automatic request is gone and the console drives everything, with three commands of my own:
band 2orband 5restricts the station to one band, then reconnectsstatusprints the channel, the signal and the active TWT flowswakesprints statistics on the driver’s wake-up events
That last one relies on an option of the TWT configuration. With post_wakeup_event, the driver posts a WIFI_EVENT_TWT_WAKEUP at each service period, and a handler only has to note the time:
static void twt_wakeup_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
wifi_event_sta_twt_wakeup_t *w = (wifi_event_sta_twt_wakeup_t *) event_data;
if (w->twt_type == TWT_TYPE_INDIVIDUAL && wake_count < WAKE_LOG_MAX) {
wake_log[wake_count++] = esp_timer_get_time();
}
}Two details of the example needed a fix on the way. Its itwt command declares the -u flag twice, for the duration unit and for the timeout. And its log computes the wake interval in a 32-bit integer. Beyond 35 minutes, the number overflows and the value displayed is wrong.
Both bands
The C5 has two bands, so does TWT work on both? The router uses one name for both bands and the board picks one or the other from boot to boot.
Twenty requests, five per block, alternating 2.4 and 5 GHz: nineteen were accepted, ten out of ten on 5 GHz. The remaining one failed on the board, which had just lost its association.
So yes, this router takes agreements on both bands. The rest of the tests run on 5 GHz.
How long an interval?
Half a second is a demo value. A sensor on a battery wants seconds or minutes. The itwt command of the example takes the request type, then the mantissa and the exponent:
itwt> itwt --setup 1 -m 15625 -e 9 -w 202
twt: (itwt)setup, trigger:1, announced, flow_id:0, err:0x0
itwt: <WIFI_EVENT_ITWT_SETUP>twt_id:202, flow_id:1, trigger-enabled, announced, wake_dura:255, wake_dura_unit:0, wake_invl_e:9, wake_invl_m:15625
itwt: <WIFI_EVENT_ITWT_SETUP>setup_cmd:4, target wake time:489890816000, wake duration:65280 us, wake interval:8000000 us--setup 1 is a suggest and -w a local name for the request. 15625 × 2^9 is exactly 8 seconds. err:0x0 only means the request was sent: the answer is setup_cmd:4, an accept. Five intervals went through that way, each with a request, a suggest and a demand:
| Interval, rounded | Mantissa | Exponent | Answer |
|---|---|---|---|
| 524 ms | 512 | 10 | accepted |
| 8 s | 15625 | 9 | accepted |
| 60 s | 58594 | 10 | accepted |
| 300 s | 36621 | 13 | accepted |
| 1 h | 54932 | 16 | accepted |
Fifteen negotiations, fifteen accepts, with the values returned unchanged. Even a plain request, where the router is free to choose, came back with mine. Agreements without trigger frames, unannounced ones and a 4 ms wake duration were accepted as well.
That says the router does not argue. It does not say that an agreement survives an hour, or that the chip sleeps through it.
How regular are the wake-ups?
An accepted agreement is a line in a log. The wake-up events show whether something follows. With the half-second agreement, left alone for 30 seconds on 5 GHz:
itwt> wakes
wakes:57, delta us min:524113, median:524239, max:524487, span:2935997457 events, and between two of them 524.2 ms, give or take 0.3 ms. With the 8 second agreement, ten events in 82 seconds, between 7.99986 and 8.00022 seconds apart.
With no agreement, or after a teardown: none.
Those are timestamps taken in the application when the driver reports a service period. They show that the driver follows the negotiated schedule, not when the radio is on.
Ten minutes later
A schedule is only useful if the connection survives it. So each agreement was left alone for ten minutes, with no application traffic, after a fresh association:
| Agreement | After 10 minutes |
|---|---|
| none | still associated |
| 524 ms | still associated |
| 2 s | still associated |
| 8 s | association lost |
The 8 second agreement, the interesting one for a sensor, got five runs. Four ended badly:
| Run | Traffic | Outcome |
|---|---|---|
| 1 | pings | association lost after 186 s |
| 2 | none | still associated after 7 minutes, stopped by hand |
| 3 | none | association lost after 424 s |
| 4 | none, keep-alive on | association lost after 592 s |
| 5 | pings | association lost after 54 s |
Until the disconnection, the wake-up events kept their 8 second pace. In the last three runs, the driver gave the same reason code: 7, a class 3 frame received from a nonassociated station.
At some point, the router and the board stopped agreeing on whether they were connected. The code does not say which side lost track first, or why.
Run 4 used ESP-IDF’s keep-alive option, made to show that the station is still there. It was disconnected too, and one run proves little.
What a ping sees
The price of TWT is latency. To see it, my laptop sent up to 20 pings to the board for each agreement, one at a time, 3 to 6 seconds apart, each with 20 seconds to come back:
| Agreement | Answered | Median | Slowest |
|---|---|---|---|
| none | 20 of 20 | 237 ms | 891 ms |
| 524 ms | 20 of 20 | 397 ms | 2.6 s |
| 2 s | 20 of 20 | 1,027 ms | 5.8 s |
| 8 s | 0 of 2 |
The baseline is no speed reference: even without an agreement, the example uses the driver’s power saving. The median grows with the interval, but the interval is no upper bound. Both TWT series had pings slower than their wake interval, and they all cross two Wi-Fi links.
The last line is run 5 of the previous table: two pings lost, then the connection dropped.
With light sleep
Does the chip sleep more with an agreement? A second firmware turns light sleep back on. With no console, it runs a sequence alone, then sends its results over UDP.
Four windows of five minutes without application traffic, with and without a 2 second agreement. One association for the whole sequence, and no disconnection:
| Window | Time in the sleep call | Exit callbacks | TWT wake-ups |
|---|---|---|---|
| no agreement | 79.1% | 2,115 | 0 |
| 2 s agreement | 96.5% | 467 | 149 |
| no agreement | 78.8% | 2,111 | 0 |
| 2 s agreement | 96.5% | 447 | 150 |
ESP-IDF can call a function each time it leaves its light sleep routine, with the time spent in it. The second column sums those times, the third counts the calls.
With the agreement, the callback ran about 450 times per window instead of 2,100, and more of the window went by in the sleep call.
Those are counters kept by the firmware, with the board on a USB port. How that changes the current draw remains to be measured.
So on this router, with this board, the 524 ms and 2 second agreements each survived ten idle minutes. Four of the five 8 second runs were disconnected before that. That is far from the minutes a battery sensor would like, and it is one router, one afternoon.
My router said yes to one hour, and could not keep eight seconds. Yours may do better: ask it.
← → jump to the previous / next post