Simvik

Time on air

Compute a LoRa packet's airtime from spreading factor, bandwidth and payload. A medium embedded C problem (LoRa and LoRaWAN), run and graded in the browser.

medium · LoRa and LoRaWAN · lora · rf · duty-cycle

LoRa packets are slow, and regulations limit how long a device may transmit (1 % duty cycle in much of Europe). Airtime from the Semtech formula:

  • symbol time: Tsym = 2^SF / BW
  • payload symbols: 8 + max( ceil( (8·PL − 4·SF + 28 + 16·CRC − 20·IH) / (4·(SF − 2·DE)) ) × (CR + 4), 0 )
  • total: (preamble + 4.25) × Tsym + payload symbols × Tsym

where PL is the payload in bytes, CRC = 1 if a CRC is sent, IH = 1 for implicit header (0 here), DE = 1 when low data-rate optimisation is on (required when Tsym ≥ 16 ms), CR is 1 to 4 (for coding rates 4/5 to 4/8), and the preamble is 8 symbols.

Write airtime_us(sf, bw_hz, pl, cr) with CRC on, explicit header, an 8-symbol preamble, and DE set automatically; return microseconds, rounded down.

Starter code

uint32_t airtime_us(int sf, uint32_t bw_hz, int pl, int cr) {
  double tsym = (double)(1 << sf) / bw_hz * 1e6;
  (void)cr;
  return (uint32_t)((8 + pl * 2) * tsym);
}

Example tests

SF7, 125 kHz

show(7, 125000, 10, 1);

Expected output:

SF7, 125 kHz, 10 bytes, CR 4/5: 41216 us

SF12, 125 kHz

show(12, 125000, 10, 1);

Expected output:

SF12, 125 kHz, 10 bytes, CR 4/5: 991232 us

Learn it first