Feedback
Feedback

If you are having issues with the exercises, please create a ticket on DevZone: devzone.nordicsemi.com
Click or drag files to this area to upload. You can upload up to 2 files.

Exercise 2

(Optional) Enabling TWT with notification

In this exercise, we will use Target Wake Time (TWT) to reduce power consumption further. Additionally, we will use the TWT sleep status notification to be notified of when the device goes to sleep and when it wakes up, and use this to send and check for received packets during the awake time. To show that the device can send and receive packets, the device connects to a simple echo UDP server that we will run on a PC.

When the device starts up, it will be configured to use legacy power saving mode. Pressing button 1 on the board will enable TWT, or if TWT is already enabled it will instead be disabled. Pressing button 2 will enable sending packets. When this is enabled, the device will send a packet every TWT period when it wakes up.

Note

To test TWT, you need a Wi-Fi 6 router that supports TWT.

Exercise steps

In the GitHub repository for this course, go to the base code for this exercise, found in lesson6/wififund_less6_exer2.

1. Define macros and global variables

1.1 Define the port and IPv4 address for the server.

#define SERVER_PORT 7777
#define SERVER_IPV4_ADDR "192.168.32.119"

The IPv4 address is the address of the PC running the UDP server, so this address must be changed to reflect the IPv4 address of your PC.

1.2 Define macros for wake up time and interval for TWT.

#define TWT_WAKE_INTERVAL_MS 65
#define TWT_INTERVAL_MS      7000

These variables will be used when configuring TWT later. The TWT interval is the total interval between TWT wake periods: TWT interval = wake up time + sleeping time.

1.3 Create variables to keep track of TWT status, flow ID, and if sending packets is enabled.

bool nrf_wifi_twt_enabled = 0;
static uint32_t twt_flow_id = 1;
bool sending_packets_enabled = 0;

2. Configure the TWT request.

2.1 Define the TWT parameters struct wifi_twt_params and fill the parameters that are common for both TWT setup and TWT teardown.

struct wifi_twt_params params = { 0 };

params.negotiation_type = WIFI_TWT_INDIVIDUAL;
params.flow_id = twt_flow_id;
params.dialog_token = 1;

2.2 Fill in the rest of the wifi_twt_params parameters.

When TWT is not enabled we want to send a TWT setup request, and if it is enabled we want to send a TWT teardown request.

2.2.1 Fill in the TWT setup specific parameters of the wifi_twt_params struct.

params.operation = WIFI_TWT_SETUP;
params.setup_cmd = WIFI_TWT_SETUP_CMD_REQUEST;
params.setup.responder = 0;
params.setup.trigger = 0;
params.setup.implicit = 1;
params.setup.announce = 0;
params.setup.twt_wake_interval = TWT_WAKE_INTERVAL_MS * USEC_PER_MSEC;
params.setup.twt_interval = TWT_INTERVAL_MS * USEC_PER_MSEC;

We are using the TWT setup operation to enable TWT. Since our device is the one requesting to setup TWT we send a Request TWT command to the AP with the responder parameter indicating that we are a requester.

We want to reduce the power consumption as much as possible, which we do by reducing the amount of messages sent. Therefore, we disable trigger frames, and configure TWT to be implicit and unannounced. With this configuration, the device does not have to send a trigger frame to the AP to inform that it is awake and ready to receive packets.

2.2.2 Fill in the TWT teardown specific parameters of the wifi_twt_params struct.

params.operation = WIFI_TWT_TEARDOWN;
params.setup_cmd = WIFI_TWT_TEARDOWN;
twt_flow_id = twt_flow_id<WIFI_MAX_TWT_FLOWS ? twt_flow_id+1 : 1;
nrf_wifi_twt_enabled = 0;

We are using the TWT teardown operation to disable TWT by sending a TWT teardown command to the AP.

2.3 Send the TWT request with net_mgmt.

if (net_mgmt(NET_REQUEST_WIFI_TWT, iface, &params, sizeof(params))) {
	LOG_ERR("%s with %s failed, reason : %s",
		wifi_twt_operation_txt(params.operation),
		wifi_twt_negotiation_type_txt(params.negotiation_type),
		wifi_twt_get_err_code_str(params.fail_reason));
	return -1;
}

3. Add the TWT events.

3.1 Define a mask for the TWT events we are interested in.

3.2 Handle the two events in the event handler.

3.2.1 Upon a TWT event, call handle_wifi_twt_event() to handle the response.

In twt_mgmt_event_handler(), add the following code to handle the TWT event

case NET_EVENT_WIFI_TWT:
	handle_wifi_twt_event(cb);
	break;

3.2.2 Upon TWT sleep state event, inform the user of the current sleep state. When the device is in the awake state, send a packet to the server and check for any received packets if sending packets is enabled.

case NET_EVENT_WIFI_TWT_SLEEP_STATE:
	int *twt_state;
	twt_state = (int *)(cb->info);
	LOG_INF("TWT sleep state: %s", *twt_state ? "awake" : "sleeping" );
	if ((*twt_state == WIFI_TWT_STATE_AWAKE) & sending_packets_enabled) {
		send_packet();
		receive_packet();
	}
	break;

3.3 Initialize and add the TWT event handler

4. Implement the TWT event callback function function.

4.1 Create a wifi_twt_params struct for the received TWT event and fill it with the event information.

const struct wifi_twt_params *resp = (const struct wifi_twt_params *)cb->info;

4.2 If the event was a TWT teardown initiated by the AP, set change the value of nrf_wifi_twt_enabled and exit the function.

if (resp->operation == WIFI_TWT_TEARDOWN) {
	LOG_INF("TWT teardown received for flow ID %d\n",
	      resp->flow_id);
	nrf_wifi_twt_enabled = 0;
	return;
}

4.3 Update twt_flow_id to reflect the flow ID received in the TWT response.

twt_flow_id = resp->flow_id;

4.4 Check if a TWT response was received. If not, the TWT request timed out.

if (resp->resp_status == WIFI_TWT_RESP_RECEIVED) {
	LOG_INF("TWT response: %s",
	      wifi_twt_setup_cmd_txt(resp->setup_cmd));		
} 
else {
	LOG_INF("TWT response timed out\n");
	return;
}

4.5 If the TWT setup was accepted, change the value of nrf_wifi_twt_enabled and print the negotiated parameters.

if (resp->setup_cmd == WIFI_TWT_SETUP_CMD_ACCEPT) {
	nrf_wifi_twt_enabled = 1;

	LOG_INF("== TWT negotiated parameters ==");
	LOG_INF("TWT Dialog token: %d",
	      resp->dialog_token);
	LOG_INF("TWT flow ID: %d",
	      resp->flow_id);
	LOG_INF("TWT negotiation type: %s",
	      wifi_twt_negotiation_type_txt(resp->negotiation_type));
	LOG_INF("TWT responder: %s",
	       resp->setup.responder ? "true" : "false");
	LOG_INF("TWT implicit: %s",
	      resp->setup.implicit ? "true" : "false");
	LOG_INF("TWT announce: %s",
	      resp->setup.announce ? "true" : "false");
	LOG_INF("TWT trigger: %s",
	      resp->setup.trigger ? "true" : "false");
	LOG_INF("TWT wake interval: %d ms (%d us)",
	      resp->setup.twt_wake_interval/USEC_PER_MSEC,
		  resp->setup.twt_wake_interval);
	LOG_INF("TWT interval: %lld s (%lld us)",
		  resp->setup.twt_interval/USEC_PER_SEC,
	      resp->setup.twt_interval);
	LOG_INF("===============================");
}

5. Modify the button handler.

5.1 Call wifi_set_twt() to enable or disable TWT when button 1 is pressed.

if (button & DK_BTN1_MSK) {
	wifi_set_twt();
}

5.2 Enable or disable sending packets during TWT awake when button 2 is pressed.

if (button & DK_BTN2_MSK) {
	sending_packets_enabled = !sending_packets_enabled;
	LOG_INF("Sending packets %s", sending_packets_enabled ? "enabled" : "disabled" );
}

6. Testing

6.1 Start the UDP server.

The UDP server is a Python script located in wififund_less6_exer2/scripts. Open the scripts directory in a terminal or command-line windows and enter the following command to start the server

You should see Starting UDP server in the window running the server.

6.2 Build and flash the application to your device.

This exercise uses the PSA backend for storing the Wi-Fi credentials. Therefore, you must build with TF-M.

BoardBuild with TF-M
nRF7002 DKnrf7002dk_nrf5340_cpuapp_ns
nRF5340 DK + nRF7002 EKnrf5340dk_nrf5340_cpuapp_ns

If necessary, input the commands to connect to Wi-Fi, as we have done in previous exercises.

You should see the following log when the device has started

6.3 Press button one on the board to enable TWT.

You should see a log that TWT setup has been requested. If the router accepts the TWT setup request the device will print the negotiated TWT parameters.

Once TWT is enabled, the sleep state will be printed to the log.

6.4 Press button two to enable sending packets during TWT awake periods.

The device will start sending and receiving UDP packets, which will be printed to logs.

The UDP server will print the packets it receives from the device.

6.5 Press button one on the board once more to disable TWT.

7. Testing with power measurement

7.1 Connect the PPK2 to the device as shown below

PPK2DK
VOUTP23 VBAT
GNDP21

Note

The current is measured over only the nRF7002 IC in this exercise, not including the nRF5340 SoC. Guides for how to measure the total current or measuring the nRF7002 IC and nRF5340 SoC separately can be found at nRF7002 DK – Measuring current.

7.2 Open the Power Profiler app and select the PPK2 in the drop-down menu.

7.3 Select source meter mode, set the supply voltage to 3.6 volts, and enable power output.

7.4 Build and program the example to the board.

7.5 Press ‘Start’ in the Power Profiler app to start the measurement.

7.6 Test the example as in step 6 and look at the power measurement.

When enabling TWT we see that the time between the wake up spikes increases compared to with legacy power saving. One of the big advantages of TWT is that it enables the device to be asleep for longer periods of time, which decreases the average power consumption.

Legacy power saving compared to TWT.

8. Play around with different TWT periods.

Modify TWT_INTERVAL_MS to see how the TWT interval affects the average power consumption.

The default TWT interval in the exercise is 7 seconds. Below we show the power consumption when the interval is 7 seconds and 20 seconds. The first image shows the 7 second interval where the device is only listening for packets during wake up, and the seconds shows the spikes when it is also sending a packet each time it wakes up. The second image shows when the TWT interval is 20 seconds.

TWT with 7 second TWT interval.
TWT with 7 second TWT interval and sending packets enabled.
TWT with 20 second TWT interval.

Increasing the TWT interval will increase the time the device is asleep, thus decreasing the average power consumption.

This is the complete log

Register an account
Already have an account? Log in
(All fields are required unless specified optional)

  • 8 or more characters
  • Upper and lower case letters
  • At least one number or special character

Forgot your password?
Enter the email associated with your account, and we will send you a link to reset your password.