# IEEE 802.15.4 Previous: [Channelizer](channelizer.md) · Up: [Modulation and coding](index.md) · Next: [GFSK and Bluetooth LE](gfsk.md) A software 2.4 GHz O-QPSK radio for the ESP32-S3, which has no 802.15.4 hardware: the Wi-Fi receiver's I/Q capture is the input, the Wi-Fi DAC is the output. It receives from the ESP32-S3 ring of [Gapless receive](../samples/rings.md) and sends through the DAC stream of [Transmit](../samples/transmit.md). Status in one line: it runs on hardware against the native radio of an ESP32-C6, both ways, and acknowledges frames to its address 192 us after their end ([Sending ACKs](#sending-acks)). The numbers are under [Measurements](#measurements). This page follows [Channelizer](channelizer.md) in [Modulation and coding](index.md). Its receiver, a phase-difference discriminator, is the idea that [GFSK and Bluetooth LE](gfsk.md) builds on next, and [Burst detection](bursts.md) reuses this PHY. ## How the PHY works ### O-QPSK as MSK The receiver treats half-sine O-QPSK as MSK: each chip period the carrier phase turns by plus or minus 90 degrees. A one-chip phase-difference detector therefore recovers the chips without carrier recovery, and a carrier offset only biases the phase step slightly. ### Receive chain Per sample at 4 MS/s (2 samples per chip): 1. **DC removal.** A running mean (time constant 256 samples) is subtracted. It updates only while both trackers are hunting, because MSK has energy at DC. 2. **Discriminator.** `d = Im(x[n] * conj(x[n-2]))`, two multiplies and a subtract. Its sign is one hard decision per chip. 3. **Two phase trackers.** Samples alternate between tracker 0 and tracker 1. One sees chip-aligned phase steps; the other sees half-aligned steps and mostly fails. Whichever decodes a valid frame first wins and resets the other. 4. **Preamble hunt.** A 32-bit shift register is compared to the symbol-0 reference by XOR and popcount. Four hits exactly 32 chips apart (at most 6 chip errors each) give symbol alignment. 5. **SFD, PHR, PSDU.** Further symbols are decided by minimum distance over all 16 references (at most 10 errors). The state machine expects more zeros, then symbols 7 and 10 (SFD 0xA7), the length byte, then the PSDU, low nibble first. 6. **Frame check.** CRC-16 (reflected polynomial 0x8408, initial value 0). On success the callback gets the PSDU and an LQI from the mean chip-error count. ### Differential references and chip tables - **Why the references are differential.** The discriminator measures the change between adjacent chips, not the chips themselves. The reference for each symbol is therefore `c ^ (c << 1) ^ MSK_MASK`: the XOR of neighbouring chips plus a fixed parity pattern. - **Masked bit.** The last bit of each symbol depends on the next symbol's first chip, so it is masked out of every comparison (`DMASK = 0xFFFFFFFE`). - **Chip tables.** Only symbol 0 is stored (`0xD9C3522E`, chip c0 in the MSB). Symbols 1 to 7 are rotations by 4 chips each; symbols 8 to 15 are symbols 0 to 7 with the odd chips inverted. This matches the 802.15.4 2450 MHz table and avoids a hand-typed table. ### Transmit modulator - `ieee154_phy_modulate()` turns each chip into a 4-sample half-sine pulse (0, 0.707, 1, 0.707). - Even chips go on I and odd chips on Q; because chip k starts at sample 2k, Q is automatically one chip (2 samples) behind I. - The output is a full frame buffer, at most 17,028 complex samples for a 127-byte PSDU. - The driver does not use it on the air ([Transmit](#transmit)); it is the reference for the host tests and for calibrating `MSK_MASK`. ### Sign convention The noiseless calibration test (`test_calibration_noiseless`) decides `MSK_MASK`: of the four candidates only `0xAAAAAAAA` decodes (PER 0%), the other three lose every frame. It is the default in the source and is confirmed on the air against the C6. ## Implementation ### Code map | File | Role | | --- | --- | | `lib/ieee802154/ieee154_phy.c`, `include/esp_sdr/ieee154_phy.h` | Portable PHY: RX discriminator, despreader, frame state machine; reference TX modulator; CRC-16. No Zephyr dependencies. | | `lib/ieee802154/ieee154_dsp.h` | The driver's signal processing as portable inline C, shared with the unit test: DAC words from slot templates, the receive mix and decimation, and the measurement helpers. | | `lib/ieee802154/ieee154_esp_sdr.c`, `include/esp_sdr/ieee154_esp_sdr.h` | The `ieee802154_radio_api` driver (device `esp_sdr_154`, `CONFIG_ESP_SDR_IEEE802154`) and its diagnostics. | | `apps/wpan/` | The `wpan` shell: test frames and counters on the software radio (S3) or a native radio (C6). | | `tests/unit/ieee154_phy/`, `tests/unit/ieee154_dsp/` | Unit tests (twister, native_sim) of the PHY and of the driver's chain. With `IEEE154_TABLES` set they also print the PHY's error rate table and the driver chain's decimator table (`scripts/unit-tables.sh`). | [wpan](../../apps/wpan/README.rst) runs the same `wpan` shell on the S3 software radio and on the C6 native radio. `apps/wpan/tools/interop.py` runs frames both ways between them; the regression test is in [Testing](../dev/testing.md#ieee802154_link). ### PHY API All sample buffers are interleaved int16 I/Q at 4 MS/s. | Function | Purpose | | --- | --- | | `ieee154_phy_init()` | Builds the 16 chip sequences and their discriminator-domain references. Call once. | | `ieee154_phy_set_rx_cb(cb, user)` | Registers the callback that receives each CRC-valid PSDU (including FCS), its length and an LQI. | | `ieee154_phy_rx_block(iq, n)` | Feeds the next n complex samples. Blocks must be consecutive; state carries across calls. | | `ieee154_phy_rx_reset()` | Drops a frame in progress, before a block that does not follow the last one. | | `ieee154_phy_set_hdr_cb(cb, user)` | Registers a callback for frames coming in: the length and the predicted end at the PHR, then each further byte while it returns true (the driver's ACK decision). | | `ieee154_phy_rx_position()` | Samples fed so far; in a callback, up to the sample being decided. | | `ieee154_phy_modulate(psdu, len, amp, out, max)` | Writes SHR + PHR + PSDU as I/Q. Needs (6 + len) x 128 + 4 samples; returns 0 if `max` is too small. | | `ieee154_phy_append_fcs(buf, n)` | Appends the little-endian FCS; returns the new length. | | `ieee154_phy_chips(sym)` | The 32 chips of a symbol, chip c0 in the MSB. | ### The driver #### Tuning The LO sits 4 MHz below the channel; why is in [Tuning](../radio/tuning.md#lo-placement). #### Gapless receive - **Needs** `CONFIG_ESP_SDR_RING` and SMP. - **Ring.** The 16 MS/s ring on CPU 1 takes every fourth pair at the phase where the mix by -fs/4 is 1, into a sample ring in internal RAM, and the PHY thread drains it on CPU 0. - **Why not the boxcar.** - Summing four, the boxcar, reaches about 8 dB further on the air. - The ring keeps up with its sums only in units of 2048 pairs. - In those units the PHY decides a frame too late for its ACK, so the driver subsamples. - The ring's side of the trade-off is in [Decimation by four](../samples/rings.md#decimation-by-four), this side's numbers in [Driver chain and on the air](#driver-chain-and-on-the-air). - **Kconfig.** `CONFIG_ESP_SDR_IEEE802154_RX_SUMS` sums in builds without ACKs; `CONFIG_ESP_SDR_RING_BOX4`, selected by the driver, builds both. - **PHY cost.** About 40 cycles per sample (53 before its hunting loop of its own, which takes 99.8 % of the samples), against a budget of 60, some 70 % of CPU 0. - **IRAM.** In the S3 wpan image the PHY's per-sample code runs from IRAM (`apps/wpan/place.h`, which the build without the Wi-Fi driver had room for). - **Units.** The ring runs in units of 1024 pairs (64 us): the PHY decides a frame about 120 us after its end. With ACKs on it rotates through two banks and keeps the third for the ACK waveform. #### Transmit - `esp_sdr_tx_dac_play_gen()`, a generator mode of the DAC stream, plays the frame gapless at 40 MS/s from 18 half-sine slot templates, one chip period each, with the +4 MHz offset included. - A transmission stops the ring and restarts it afterwards. - A block that is filled late ends the session with silence and the call returns `-EIO`. #### Receiving ACKs - A frame with the AR bit makes `tx()` wait for its ACK (`IEEE802154_HW_TX_RX_ACK`). - The generator session ends at the frame's last sample, the front end is back in RX 50 us later, and one 1 ms bank capture then holds an ACK sent after the 192 us turnaround. - No ACK returns `-ENOMSG`. - ACKs are consumed by the driver; the L2 never sees them. #### Without the L2 - Without `CONFIG_NET_L2_IEEE802154` the driver is a plain device with a frame callback (`ieee154_esp_sdr_set_rx_cb()`): raw 802.15.4 mode would compile out the interface layer the Wi-Fi driver needs. - The callback runs on the PHY thread with the PHY lock held and must not call back into the driver. #### Diagnostics - `ieee154_esp_sdr_set_invert()` conjugates RX (after the mix) or TX, to check a peer's I/Q convention. - `ieee154_esp_sdr_measure()` and `_dump()` capture 1 ms at the current channel: carrier offset, power, chip phase step histogram, or the samples themselves for the host. - A breadcrumb trail and the PCs of a stalled CPU 0 are kept in RTC memory across resets (`wpan crash`). #### Memory The driver keeps its stacks and tables in `ESP_SDR_HIGH_RAM` ([Code placement](../dev/code-placement.md)). ## Sending ACKs ### Why ACKs matter Built on 2026-10-12: the S3 acknowledges frames to its address from inside the ring run, 192 us after their end in more than 99 % of the cases. - Zigbee and Thread need an ACK for every unicast frame to the node that has the AR bit set. Without it the sender retries three times and reports the frame as lost. - So a node that cannot send one in time cannot even join a network as an end device: the association response from the parent asks for an ACK. - Before this, the S3 software radio received 802.15.4 frames and the ACKs to its own frames, but its ACK left milliseconds late. The ring handed samples over in units of 768 us, the PHY lagged most of a unit behind, and stopping the ring and starting a DAC session took a millisecond each. ### The deadline - **Turnaround.** An Imm-Ack starts aTurnaroundTime after the last symbol of the frame it acknowledges: 12 symbols, 192 us. - **Sender's wait.** The sender waits macAckWaitDuration = 54 symbols (864 us) from the end of its frame for the whole ACK: 20 (backoff period) + 12 (turnaround) + 10 (SHR) + 12 (PHR and the 5-byte frame). - **Latest start.** The ACK lasts 22 symbols (352 us), so a sender that follows the formula accepts an ACK that starts up to 32 symbols, 512 us, after its frame. - **The peer's limit binds.** Neither the Espressif HAL nor Zephyr's ESP32 driver sets the ESP32-C6's ACK timeout (`IEEE802154_ACK_TIMEOUT_REG`, 16 bits in units of 16 us), so the C6 runs on the hardware default; `esp_ieee802154_get_ack_timeout()` reads it. - **Measured on the C6:** the ACK timeout is 1728 us (`wpan status` on the C6: 108 units of 16 us, the hardware default). Far more lenient than the 864 us of the standard; other peers will not be. - **Target.** Start 192 us after the frame. Earlier is not better (the sender may still be turning its receiver on); later is accepted up to the peer's limit and counted; an ACK that would start beyond the limit is skipped. ### Measurements before the build Measured on 2026-10-12 on the XIAO S3 against the C6, coax with 30 dB between them, channel 15 unless noted. The tools for them stay in the tree: the ring's unit length as a run parameter (`esp_sdr_ring_cfg::unit_pairs`), `esp_sdr_ring_now()`, `ieee154_phy_rx_position()`, the driver's verdict counters, `wpan set unit` and `wpan bench ack` (see [wpan](../../apps/wpan/README.rst)). #### Verdict latency From the frame's last sample to the PHY's frame callback, read from the ring's live pair index, 200 frames of 16 bytes: | Unit (pairs) | min | avg | max (us) | | --- | --- | --- | --- | | 12288 | 751 | 917 | 1067 | | 4096 | 272 | 522 | 865 | | 2048 | 146 | 273 | 829 | | 1024 | 84 | 104 | 177 | With 100-byte frames on channels 11 to 25 at 1024: average about 118 us, maxima 135 to 414 us (the tail is CPU 0 contention, see [Open](#open)). #### Unit size The ring keeps up at 1024-pair units after a faster subsampling path ([Unit size and cost](../samples/rings.md#unit-size-and-cost)). The driver now runs 1024-pair units always: with the faster path, 12288-pair units arrive as one burst larger than the PHY's "fell behind" margin, and 73 of 200 frames were lost that way. #### ACK waveform Scalar slot copies into the capture bank: - chips: 3.2 to 3.8k cycles (16 us); - all 14100 words: 60 to 63k cycles (**251 us**); - from slot 416 on, the part that depends on the sequence number, frame pending bit and FCS: 25k cycles (105 us); - about 85 cycles per 20-word slot. #### Front end The switch between RX and TX and its cost per ROM and PHY call are in [Front-end switch cost](../samples/transmit.md#front-end-switch-cost). The ACK's switch to TX leaves out the 50 us settle wait. ### How it is built With `CONFIG_ESP_SDR_IEEE802154_AUTO_ACK` (default y) the driver reports `IEEE802154_HW_RX_TX_ACK` and `IEEE802154_HW_FILTER`. #### Ring The ring (`esp_sdr_ring_cfg::spare_bank`, `esp_sdr_ring_burst_*()`) keeps bank 2 for the ACK waveform, plays it once at a unit boundary after the frame's end and restarts the capture. It is described in [Spare bank and bursts from a run](../samples/rings.md#spare-bank-and-bursts-from-a-run). #### PHY `ieee154_phy_set_hdr_cb()` reports the length and the predicted end at the PHR and each further byte ([PHY API](#phy-api)). #### Driver - **Decision.** It decides from the frame control, PAN ID and destination address (`filter()`, not in promiscuous mode) whether an ACK is due. - **Arming.** It arms with the sequence number, fires `ack_delay` (192 us) after the end when the FCS passes, and cancels when the PHY is past the end without a verdict. - **Precomputed waveform.** The first 416 slots of every ACK (preamble, SFD, PHR, frame control low nibble) go to bank 2 before each run. - **Priority.** While armed the PHY thread does not sleep and runs at `CONFIG_ESP_SDR_IEEE802154_ACK_PRIORITY`. - **Not sent:** Enh-Ack (frame version 2), ACKs to frames without a destination address as PAN coordinator, the frame pending bit (always 0, so no table for polling end devices). #### wpan The ACK commands (`wpan set ack`, `wpan bench acktx`) and the counters in `wpan status` are in [wpan](../../apps/wpan/README.rst). ### Results Measured with the C6 sending to the S3 (`wpan tx 200 10 0x0001 1`; the C6 reports ok when its hardware got the ACK), coax with 30 dB: | Case | Result | | --- | --- | | Channel 15, payloads 8, 16, 50, 100 bytes | 200 of 200 ACKs each | | Channels 11, 20, 25, payloads 8 and 100 | 197 to 200 of 200; each miss a frame the S3 did not receive (the PHY's sample skips, see [Open](#open)) | | Promiscuous, broadcast, another address | no ACK sent | | Start after the frame's end | 192 us for 397 of 400; the rest up to about 350 us | | Verdict (FCS checked) after the end | average 115 to 125 us, at most 150 to 226 us | | Front end in TX after the end | 86 us typically, at most 208 us (waveform still being built after the stop for short frames) | | Capture running again after the end | about 670 us (the ACK ends at 544 us) | Steps that got there: - The front end's 50 us settle no longer delays an ACK: late ACKs 35 of 400 went to 0. - The PHY thread's raised priority while armed: late ACKs 34 to 17 of 400 before that. - The interop test (frames both ways, the S3's own ACK requests) passes as before on the two-bank ring. ## Measurements ### Simulated error rate The PHY decodes every frame from 6 dB SNR up, with carrier offsets up to 100 kHz (`scripts/unit-tables.sh phy`, 300 frames per point, DC offset 100 LSB; how to regenerate it is in [Testing](../dev/testing.md#sweep-tables)): | SNR dB | PER at 0 Hz | PER at 50 kHz | PER at 100 kHz | | --- | --- | --- | --- | | 0 | 0.993 | 0.990 | 0.993 | | 2 | 0.337 | 0.390 | 0.550 | | 4 | 0.007 | 0.003 | 0.010 | | 6 to 16 | 0.000 | 0.000 | 0.000 | - No frame was delivered twice or with wrong content. - SNR is measured over the full 4 MHz sample bandwidth, so the in-band SNR is roughly 3 dB higher. ### Driver chain and on the air This is the 802.15.4 side of the decimation trade-off in [Decimation by four](../samples/rings.md#decimation-by-four). #### Simulated driver chain `scripts/unit-tables.sh dsp` runs the driver's chain: DAC words, resampling to 16 MS/s, delay, carrier offset, DC, noise, 10-bit receive words, mix and decimation, PHY. All frames decode down to: | Decimation | Lowest SNR over 16 MHz | | --- | --- | | burst FIR | -2 dB | | boxcar (sum of four) | 0 dB (2 dB more) | | every fourth pair (the driver's) | 6 dB | Summing the four keeps the noise of the 12 MHz outside the channel from folding into it: about 6 dB for white noise. #### On the air, 2026-11-13 On the air it was more. The XIAO C6 sent 200 test frames a step on channel 20 to the XIAO S3; half of them came through at: | Decimation | 50 % point (C6 transmit power) | Frames received | | --- | --- | --- | | every fourth pair | about -5 dBm | three rounds: 75, 66 and 28 at -6 dBm, none at -12 | | sums, 2048-pair units | about -13.5 dBm | 152 at -9 dBm, 159 at -12, 32 at -15 | In 1024-pair units the ring abandons every unit with the sums. #### Losses above the 50 % point, 2026-11-15 - Above the 50 % point the losses do not fall to nothing. At the C6's default power the S3 lost 2 to 67 % of the frames depending on the hour: on channel 20 5 %, ten minutes later 33 to 67 % with the same PHY code, a quarter of an hour after that 8 %. - This held with the receive gain fixed anywhere from 30 to 70 as with the AGC, and it was not more after a boot. - `wpan status` counts where the phase trackers' frames went (`phy trackers`). The lost frames are found (four preamble symbols in a row) and then break off before the SFD or in the frame, on a wrong symbol or one with more than 10 chip errors. - Going on with the best match through the PSDU only turned the drops there into bad FCSs: 1119, 1116 and 1121 of 1200 frames delivered with limits of 10, 16 and 31 errors. So the frames are corrupted on the air. - This desk's S3 hears the C6 only by leakage (its antenna port goes to a terminated switch port), and every fourth pair also brings in whatever else the 16 MHz capture holds. ### Interop with the ESP32-C6 - **Channels 11 to 25:** 200 of 200 test frames with 100-byte payloads in each direction on channels 11, 15 and 20, over a coax cable, and 50 of 50 ACKs received by the S3 for frames that asked for one (before the ring ran 1024-pair units; since then see [ACKs, receiving and sending](#acks-receiving-and-sending)). - `apps/wpan` selects the C6's U.FL port (`CONFIG_XIAO_ESP32C6_EXT_ANTENNA=y`). - **ACKs sent by the S3:** see [Results](#results). - **Channel 26** from the C6 does not decode: see [Channel 26 from the C6](#channel-26-from-the-c6). ## Limits and open issues ### Not done - CCA and energy detection (CCA always reports clear). - Dropping frames for other addresses: every received frame goes up to L2, which filters it. With `CONFIG_ESP_SDR_IEEE802154_AUTO_ACK` the driver reports `IEEE802154_HW_FILTER`, but the addresses `filter()` sets decide only which frames get an ACK ([How it is built](#how-it-is-built)). - RSSI. - A calibrated TX power: dBm is mapped onto the vendor ladder with +20 dBm assumed at the top. - The ESP32-C6 as a software radio. Its CPU-paced transmit stops at 250 kS/s, far below the 2 Mchip/s ([Paced streams](../samples/paced.md)), and its gapless 80 MS/s receive runs without the Wi-Fi driver and feeds no PHY ([Gapless receive](../samples/rings.md#esp32-c6-gapless-80-mss)). ### Channel 26 from the C6 Parked. S3 to C6 works on channel 26; C6 to S3 does not decode. - The C6's channel 26 waveform carries the right chips at the right timing and power, but its one-chip phase steps are scrambled: 25 to 30 % of the hard chip decisions are wrong. - A carrier line sits 6 to 8 dB below the modulation, and the spectrum is wider. - No linear, I/Q imbalance, modulation index, limiter or time-varying model explains it. - Soft differential despreading decodes the captures, with the margin of a weak signal. - Next steps: a soft despreader in the PHY, a capture with the S3's LO moved to rule out the receiver, and a look at what the C6 changes at 2480 MHz (its RF test library's `phy_2480m_opt` is an empty stub). - Captures and analysis scripts are kept outside the repository. The measurement a reference radio would add is in [ZigBit reference](../dev/zigbit.md#what-the-rf233-answers). ### Open A reference radio is planned in [ZigBit reference](../dev/zigbit.md). #### ACKs, receiving and sending - **Receiving.** The S3 receives 49 of 50 ACKs to its own frames per channel since the ring runs 1024-pair units (50 of 50 before). With 1024 units, interop is 197 to 200 of 200 per direction and 48 to 49 of 50 ACKs received (measured before the ACK build, 2026-10-12). - **Skips.** A sample the PHY skips at a frame's end loses the ACK it would send: the PHY skips a burst at each ring restart. - **CPU 0 share.** At some 70 % of CPU 0 the PHY runs above the shell and the log thread, and skips no sample in 30 s idle (0.13 % at 87 % and the lowest priority, where the console's output held it off for a ring's length; 0.03 % now while every frame is dumped in hex, which a network thread does above it). - **CPU 0 share, ACK measurements of 2026-10-12.** The PHY was once 87 % of CPU 0 at the lowest priority, then skipping about 0.2 % of the samples while idle; now some 70 % above the shell, no skips idle. - **Not sent:** see the driver's list in [How it is built](#how-it-is-built). - **Forced TX gain.** The forced TX gain call (38 us) runs on every ACK; it could be skipped when unchanged if the gain survives the RX phase. - **Receiving again.** After its ACK the S3 receives again about 670 us after the frame's end ([Results](#results)), which a sender's next frame without backoff may beat. - **Timing check.** The ACK's start against the frame's end is measured in samples, not on the air; the ZigBit's timer could check it on one clock. #### Ring lateness - At 64 us units little room is left for the forced gain refresh, interrupt windows and slices. - A flash write on CPU 0 parks CPU 1 and ends the run. - The analog I2C lock is shared with the Wi-Fi PHY on CPU 0, so a long hold there delays the front end switch. #### RAM and CPU - Without the Wi-Fi driver (since 2026-11-15) the S3 wpan image has some 23 KB of internal RAM left below the dump banks, the PHY's per-sample code in IRAM included. - With the driver it was nearly used up: the ISR stacks went from 1280 to 1024 bytes for the ACK path. - The PHY's share of CPU 0 plus the ring's CPU 1 leave little for a Zigbee or Thread stack on the S3. #### Wireshark sniffer - Not a `wpan` mode: [pcap](../../apps/pcap/README.rst) turns the WHY2025 badge into the sniffer. - Its channel mode streams one 802.15.4 channel and writes each frame as pcapng with `LINKTYPE_IEEE802_15_4_TAP` (283), with channel, frequency and LQI as TAP fields. - Zigbee decoding is then a Wireshark setting (network key). - How it finds and decodes the frames is in [Burst detection](bursts.md). #### Zephyr L2 and OpenThread The L2 build compiles (CI builds it: `regression.esp_sdr.ieee802154_link.s3_l2`); untested on the air. #### Sampling clock drift Only simulated: 40 ppm over 4.3 ms is about 0.3 samples. Previous: [Channelizer](channelizer.md) · Up: [Modulation and coding](index.md) · Next: [GFSK and Bluetooth LE](gfsk.md)