How lora-net/sx1302_hal works
Purpose and entry points
This is Semtech’s SX1302/SX1303 LoRa gateway host software. It builds a static concentrator HAL, a production UDP packet forwarder, and hardware-facing utilities. The top-level build/install orchestration is Makefile; the runtime gateway entry point is packet_forwarder/src/lora_pkt_fwd.c. Hardware reset/power sequencing is externalized to tools/reset_lgw.sh.
Concentrator HAL boundary
libloragw is the core C library statically linked into executables. Its public API is defined in libloragw/inc/loragw_hal.h and implemented primarily by libloragw/src/loragw_hal.c. It owns concentrator configuration, startup/calibration, receive extraction, packet timestamping, and scheduled transmit, insulating callers from SX1302 register and radio details.
Hardware and radio drivers
The HAL delegates low-level concentrator work to the SX1302 driver (libloragw/src/loragw_sx1302.c, libloragw/src/loragw_sx1302_rx.c) and register access layer (libloragw/src/loragw_reg.c). Separate RF front-end drivers support SX1250, SX125x, and SX1261 radios: libloragw/src/loragw_sx1250.c, libloragw/src/loragw_sx125x.c, and libloragw/src/loragw_sx1261.c. Firmware blobs under libloragw/src/*.var supply AGC, arbitration, calibration, and SX1261 program data.
Transport abstraction and physical interfaces
A communication layer selects SPI or USB transport, giving device drivers aligned access patterns: libloragw/src/loragw_com.c, libloragw/src/loragw_spi.c, and libloragw/src/loragw_usb.c. USB gateways use an STM32 as a USB-to-SPI bridge, programmed from mcu_bin/rlz_010000_CoreCell_USB.bin; host-side MCU protocol handling is in libloragw/src/loragw_mcu.c. This is the key boundary between Linux host software and concentrator-board hardware.
Packet forwarding pipeline
packet_forwarder/src/lora_pkt_fwd.c configures the HAL from regional global_conf.json.* files, receives RF uplinks, adds gateway metadata, serializes protocol datagrams, and sends them to a network server over IP/UDP. Incoming server downlinks are parsed, validated, and submitted to the HAL for radio transmission. Wire protocol expectations are documented in packet_forwarder/PROTOCOL.md; JSON parsing comes from libtools/src/parson.c.
Downlink timing and coordination
The forwarder uses a just-in-time queue to arbitrate timestamped downlinks before invoking HAL transmit APIs: packet_forwarder/src/jitqueue.c and packet_forwarder/inc/jitqueue.h. Timing inputs originate from concentrator counters and optional GPS/PPS/NMEA support in libloragw/src/loragw_sx1302_timestamp.c and libloragw/src/loragw_gps.c. This separates unreliable network arrival timing from strict RF transmit deadlines.
Optional radio services and operational utilities
The HAL includes Listen-Before-Talk and SX1261 spectral-scanning capabilities (libloragw/src/loragw_lbt.c, util_spectral_scan/src/spectral_scan.c). Operational programs expose focused hardware workflows: gateway EUI discovery in util_chip_id/src/chip_id.c, USB MCU DFU boot mode in util_boot/src/boot.c, and a local UDP downlink simulator/logger in util_net_downlink/src/net_downlink.c. Systemd deployment assets are under tools/systemd/lora_pkt_fwd.service.
Main entry points: loragw_hal.c Concentrator HAL · lora_pkt_fwd.c UDP packet forwarder · loragw_hal.h HAL public API · Makefile Build and install
Components 21
Packet Forwarder
- UDP packet forwarder · Configures the concentrator and bridges LoRa packets to a network server.
packet_forwarder/src/lora_pkt_fwd.c - Regional configuration
- UDP wire protocol
packet_forwarder/PROTOCOL.md - JSON parser
libtools/src/parson.c - JIT downlink queue · Queues timestamped downlinks against RF transmission deadlines.
packet_forwarder/src/jitqueue.c
Concentrator HAL
- HAL public API
libloragw/inc/loragw_hal.h - Concentrator HAL · Owns configuration, calibration, RX extraction, timestamps, and scheduled TX.
libloragw/src/loragw_hal.c - SX1302 driver
libloragw/src/loragw_sx1302.c - SX1302 RX path
libloragw/src/loragw_sx1302_rx.c - Timestamp and GPS
libloragw/src/loragw_sx1302_timestamp.c - Listen-Before-Talk
libloragw/src/loragw_lbt.c
Hardware Boundary
- Register access
libloragw/src/loragw_reg.c - RF front-end drivers
libloragw/src/loragw_sx1250.c - SPI / USB transport · Selects the physical host-to-board transport.
libloragw/src/loragw_com.c - USB MCU bridge · USB gateways use an MCU as a USB-to-SPI bridge.
libloragw/src/loragw_mcu.c - Radio firmware blobs
Operations
- Board reset and power
tools/reset_lgw.sh - Spectral scan utility
util_spectral_scan/src/spectral_scan.c - Systemd service
tools/systemd/lora_pkt_fwd.service
Other components
- Build and install
Makefile - Network server
Connections 22
- Build and install → Concentrator HAL: builds
- Build and install → UDP packet forwarder: builds
- Systemd service → UDP packet forwarder: runs
- Board reset and power → Concentrator HAL: prepares hardware for
- Regional configuration → UDP packet forwarder: configures
- JSON parser → UDP packet forwarder: parses configuration and datagrams
- UDP wire protocol → UDP packet forwarder: defines wire format
- UDP packet forwarder → Network server: uplinks and downlinks over UDP
- UDP packet forwarder → JIT downlink queue: queues parsed downlinks
- JIT downlink queue → HAL public API: submits scheduled TX
- UDP packet forwarder → HAL public API: configures and receives packets
- HAL public API → Concentrator HAL: implemented by
- Concentrator HAL → SX1302 driver: controls
- SX1302 driver → SX1302 RX path: uses receive path
- Concentrator HAL → Timestamp and GPS: uses timing inputs
- Concentrator HAL → Listen-Before-Talk: supports
- Concentrator HAL → Register access: accesses hardware through
- Concentrator HAL → RF front-end drivers: configures radios
- Concentrator HAL → SPI / USB transport: uses
- SPI / USB transport → USB MCU bridge: USB transport protocol
- Radio firmware blobs → Concentrator HAL: supplies calibration programs
- Spectral scan utility → HAL public API: uses