xref: /openwifi/doc/app_notes/iq.md (revision 261bb9eef73c3da1bda8d992c145e07cdacfaaac)
1<!--
2Author: Xianjun jiao
3SPDX-FileCopyrightText: 2019 UGent
4SPDX-License-Identifier: AGPL-3.0-or-later
5-->
6
7
8We implement the **IQ sample capture** with interesting extensions: many **trigger conditions**; **RSSI**, RF chip **AGC** **status (lock/unlock)** and **gain**.
9
10## Quick start
11- Power on the SDR board.
12- Connect a computer to the SDR board via Ethernet cable. The computer should have static IP 192.168.10.1. Open a terminal on the computer, and then in the terminal:
13  ```
14  ssh [email protected]
15  (password: openwifi)
16  cd openwifi
17  ./wgd.sh
18  (Wait for the script completed)
19  ./monitor_ch.sh sdr0 11
20  (Monitor on channel 11. You can change 11 to other channel that is busy)
21  insmod side_ch.ko iq_len_init=8187
22  (for smaller FPGA (7Z020), iq_len_init should be <4096, like 4095, instead of 8187)
23
24  ./side_ch_ctl wh11d4094
25  (Above command is needed only when you run with zed, adrv9364z7020, zc702 board)
26
27  ./side_ch_ctl g
28  ```
29  You should see on board outputs like:
30  ```
31  loop 64 side info count 61
32  loop 128 side info count 99
33  ...
34  ```
35  If the second number (side info count 61, 99, ...) keeps increasing, that means the trigger condition is met from time to time and the IQ sample is going to the computer smoothly.
36
37- Open another terminal on the computer, and run:
38  ```
39  cd openwifi/user_space/side_ch_ctl_src
40  python3 iq_capture.py
41  (for zed, adrv9364z7020, zc702 board, add argument that euqals to iq_len_init, like 4095)
42  ```
43  The python script needs "matplotlib.pyplot" and "numpy" packages installed. Now you should see 3 figures showing run-time **IQ sample**, **AGC gain and lock status** and **RSSI (uncalibrated)**. Meanwhile the python script prints the **timestamp**.
44  ![](./iq-screen-shot.jpg)
45
46  While running, all information is also stored into a file **iq.txt**. A matlab script **test_iq_file_display.m** is offered to help you do analysis on the IQ Information offline. For zed, adrv9364z7020, zc702 board, do not forget to change the **iq_len** in the matlab script to 4095.
47
48## Understand the IQ capture feature
49  The IQ information is extracted via the openwifi **side channel** infrastructure. This figure explains the related modules (also related source code file name) and how the information goes from the SDR board to the computer.
50  ![](./iq-architecture.jpg)
51
52  The IQ information format is shown in this figure.
53  ![](./iq-information-format.jpg)
54
55  For each element, the actual size is 64bit.
56  - timestamp: 64bit TSF timer value when the capture is triggered.
57  - IQ
58    - The first two 16bit are used for I/Q sample from the antenna currently used
59    - The 3rd 16bit is AD9361 AGC gain (bit7 -- lock/unlock; bit6~0 -- gain value)
60    - The 4th 16bit is RSSI (half dB, uncalibrated). Please check xpu.v and sdr.c to understand how the raw RSSI value is finally calibrated and reported to Linux mac80211.
61
62  The python and Matlab scripts are recommended for you to understand the IQ packet format precisely.
63
64## Config the IQ capture and interval
65  The quick start guide captures a period of history IQ when the packet FCS checksum is checked by Wifi receiver (no matter pass or fail). To initiate the capture with different trigger conditions and length, configuration commands should be issued before executing "**side_ch_ctl g**". The configuration command is realized by feeding a different parameter to "**side_ch_ctl**". The main parameters that are configurable are explained in this figure.
66  ![](./iq-capture-parameter.jpg)
67
68  **iq_len** is the number of IQ samples captured per trigger condition met. The capture is started from the time **pre_trigger_len** IQ samples before the trigger moment. **iq_len** is set only one time when you insert the side_ch.ko. Please check the next section for **iq_len** configuration. This section introduces the setting of pre_trigger_len and trigger condition.
69  - pre_trigger_len
70  ```
71  ./side_ch_ctl wh11dY
72  ```
73  The parameter **Y** specifies the pre_trigger_len. Valid range 0 ~ 8190. It is limited by the FPGA fifo size. For **small FPGA** (zed_fmcs2, adrv9364z7020, zc702), the valid range is 0 ~ **4094**.
74  - trigger condition
75  ```
76  ./side_ch_ctl wh8dY
77  ```
78  The parameter **Y** specifies the trigger condition. Valid range 0 ~ 31, which is explained in this table.
79
80  value|meaning
81  -----|-------
82  0 |receiver gives FCS checksum result. no matter pass/fail
83  1 |receiver gives FCS checksum result. pass
84  2 |receiver gives FCS checksum result. fail
85  3 |receiver gives SIGNAL field checksum result. no matter pass/fail
86  4 |receiver gives SIGNAL field checksum result. pass
87  5 |receiver gives SIGNAL field checksum result. fail
88  6 |receiver gives SIGNAL field checksum result. no matter pass/fail. HT packet
89  7 |receiver gives SIGNAL field checksum result. no matter pass/fail. non-HT packet
90  8 |receiver gives  long preamble detected
91  9 |receiver gives short preamble detected
92  10|RSSI (half dB uncalibrated) goes above the threshold
93  11|RSSI (half dB uncalibrated) goes below the threshold
94  12|AD9361 AGC from lock to unlock
95  13|AD9361 AGC from unlock to lock
96  14|AD9361 AGC gain goes above the threshold
97  15|AD9361 AGC gain goes below the threshold
98  16|phy_tx_started signal from openofdm tx core
99  17|phy_tx_done signal from openofdm tx core
100  18|positive edge of tx_bb_is_ongoing from xpu core
101  19|negative edge of tx_bb_is_ongoing from xpu core
102  20|positive edge of tx_rf_is_ongoing from xpu core
103  21|negative edge of tx_rf_is_ongoing from xpu core
104  22|phy_tx_started and this tx packet needs ACK
105  23|phy_tx_done and this tx packet needs ACK
106  24|positive edge of tx_bb_is_ongoing and this tx packet needs ACK
107  25|negative edge of tx_bb_is_ongoing and this tx packet needs ACK
108  26|positive edge of tx_rf_is_ongoing and this tx packet needs ACK
109  27|negative edge of tx_rf_is_ongoing and this tx packet needs ACK
110  28|tx_bb_is_ongoing and I/Q amplitude from the other antenna is above rssi_or_iq_th
111  29|tx_rf_is_ongoing and I/Q amplitude from the other antenna is above rssi_or_iq_th
112  30|start tx, meanwhile I/Q amplitude from the other antenna is above rssi_or_iq_th
113  31|start tx and need for ACK, meanwhile I/Q amplitude from the other antenna is above rssi_or_iq_th
114
115  If free running is wanted (alway trigger), please use the following two commands together.
116  ```
117  ./side_ch_ctl wh8d0
118  ./side_ch_ctl wh5d1
119  ```
120
121  To set the RSSI threshold
122  ```
123  ./side_ch_ctl wh9dY
124  ```
125  The parameter **Y** specifies the RSSI threshold. Valid range 0 ~ 2047.
126
127  To set the AGC gain threshold
128  ```
129  ./side_ch_ctl wh10dY
130  ```
131  The parameter **Y** specifies the AGC gain threshold. Valid range 0 ~ 127.
132
133  The command "**side_ch_ctl g**" will perform IQ capture every 100ms until you press ctrl+C. To use a different capture interval:
134  ```
135  side_ch_ctl gN
136  ```
137  The interval will become N*1ms
138
139## Config the iq_len
140  The **iq_len** (number of IQ sample per capture) is configurable in case you want less IQ samples per capture so that it can be triggered more times during a specific analysis period. The valid value is 1~**8187**. For **small FPGA** (zed_fmcs2, adrv9364z7020, zc702), the valid range is 0 ~ **4095**. It is independent from pre_trigger_len, and it can be less than pre_trigger_len if you want. You should align the **iq_len** value at the side_ch.ko, iq_capture.py and test_iq_file_display.m.
141  - When insert the kernel module, use:
142  ```
143  insmod side_ch.ko iq_len_init=3000
144  ```
145  Here 3000 is an example. **ATTENTION:** You need to specify **iq_len_init** explicitly to turn on IQ capture, which will turn off the default CSI mode. Insert the side_ch.ko without any parameter will run the default CSI mode.
146  - When launch the python script, use:
147  ```
148  python3 iq_capture.py 3000
149  ```
150  - When use the matlab script, please change the **iq_len** variable in the script to 3000.
151
152## Compile the side channel driver and user space program
153  - side_ch.ko
154  ```
155  $OPENWIFI_DIR/driver/side_ch/make_driver.sh $OPENWIFI_DIR $XILINX_DIR ARCH_BIT
156(For Zynq 7000, ARCH_BIT should be 32, for Zynq MPSoC, ARCH_BIT should be 64)
157  ```
158  - side_ch_ctl (take user_space/side_ch_ctl_src/side_ch_ctl.c and compile it on board!)
159  ```
160  gcc -o side_ch_ctl side_ch_ctl.c
161  ```
162
163## Run the IQ capture together with modes other than monitor
164  The openwifi IQ capture feature could run with not only monitor mode but also other modes, such as AP-Client or ad-hoc mode. After the communication functionality is fully up in those modes, you can start IQ capture from "**insmod side_ch.ko**" and "**./side_ch_ctl g**" on board as described in the previous sections to extract IQ information to your computer.
165
166## Map the IQ information to the WiFi packet
167  If you want to relate the IQ information to the WiFi packet, you need to capture WiFi packets (tcpdump/wireshark/etc) while capturing IQ. Then you can relate the timestamp between WiFi packet and IQ information. Please be noticed that the timestamp in the IQ information is the moment when capture is triggered, which could be different from the timestamp reported in the packet capture program. But since they share the same time base (TSF timer), you can relate them easily by analyzing the WiFi packet and IQ sample sequence.
168
169  Please learn the python and Matlab script to extract IQ information per capture according to your requirement.
170