# lora-net/sx1302_hal architecture

> Architecture diagram of the GitHub repository lora-net/sx1302_hal, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lora-net/sx1302_hal
- Repository: https://github.com/lora-net/sx1302_hal

## Overview

#### 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: [`libloragw/src/loragw_hal.c`](https://github.com/lora-net/sx1302_hal/blob/master/libloragw/src/loragw_hal.c) (Concentrator HAL), [`packet_forwarder/src/lora_pkt_fwd.c`](https://github.com/lora-net/sx1302_hal/blob/master/packet_forwarder/src/lora_pkt_fwd.c) (UDP packet forwarder), [`libloragw/inc/loragw_hal.h`](https://github.com/lora-net/sx1302_hal/blob/master/libloragw/inc/loragw_hal.h) (HAL public API), [`Makefile`](https://github.com/lora-net/sx1302_hal/blob/master/Makefile) (Build and install)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_forwarder["Packet Forwarder"]
  node_forwarder_main["UDP packet forwarder<br/>gateway runtime<br/>[lora_pkt_fwd.c]"]
  node_regional_config["Regional configuration<br/>JSON config"]
  node_udp_protocol["UDP wire protocol<br/>protocol contract<br/>[PROTOCOL.md]"]
  node_json_parser["JSON parser<br/>parser library<br/>[parson.c]"]
  node_jit_queue["JIT downlink queue<br/>timing arbiter<br/>[jitqueue.c]"]
end

subgraph group_hal["Concentrator HAL"]
  node_hal_api["HAL public API<br/>[loragw_hal.h]"]
  node_hal_core["Concentrator HAL<br/>static library core<br/>[loragw_hal.c]"]
  node_sx1302_driver["SX1302 driver<br/>concentrator driver<br/>[loragw_sx1302.c]"]
  node_sx1302_rx["SX1302 RX path<br/>receive driver<br/>[loragw_sx1302_rx.c]"]
  node_timing_gps["Timestamp and GPS<br/>timing services"]
  node_lbt["Listen-Before-Talk<br/>radio service<br/>[loragw_lbt.c]"]
end

subgraph group_hardware["Hardware Boundary"]
  node_register_access["Register access<br/>register driver<br/>[loragw_reg.c]"]
  node_rf_frontends["RF front-end drivers<br/>SX1250/SX125x/SX1261<br/>[loragw_sx1250.c]"]
  node_transport["SPI / USB transport<br/>communication layer<br/>[loragw_com.c]"]
  node_mcu_bridge["USB MCU bridge<br/>STM32 protocol<br/>[loragw_mcu.c]"]
  node_firmware["Radio firmware blobs<br/>firmware data"]
end

subgraph group_operations["Operations"]
  node_reset_script["Board reset and power<br/>hardware setup<br/>[reset_lgw.sh]"]
  node_spectral_scan["Spectral scan utility<br/>radio diagnostic<br/>[spectral_scan.c]"]
  node_systemd_service["Systemd service<br/>deployment unit"]
end

node_build["Build and install<br/>Make orchestration"]
node_network_server(("Network server<br/>external UDP peer"))

node_build -->|"builds"| node_hal_core
node_build -->|"builds"| node_forwarder_main
node_systemd_service -->|"runs"| node_forwarder_main
node_reset_script -.->|"prepares hardware for"| node_hal_core
node_regional_config -->|"configures"| node_forwarder_main
node_json_parser -->|"parses configuration and datagrams"| node_forwarder_main
node_udp_protocol -.->|"defines wire format"| node_forwarder_main
node_forwarder_main -->|"uplinks and downlinks over UDP"| node_network_server
node_forwarder_main -->|"queues parsed downlinks"| node_jit_queue
node_jit_queue -->|"submits scheduled TX"| node_hal_api
node_forwarder_main -->|"configures and receives packets"| node_hal_api
node_hal_api -->|"implemented by"| node_hal_core
node_hal_core -->|"controls"| node_sx1302_driver
node_sx1302_driver -->|"uses receive path"| node_sx1302_rx
node_hal_core -->|"uses timing inputs"| node_timing_gps
node_hal_core -.->|"supports"| node_lbt
node_hal_core -->|"accesses hardware through"| node_register_access
node_hal_core -->|"configures radios"| node_rf_frontends
node_hal_core -->|"uses"| node_transport
node_transport -->|"USB transport protocol"| node_mcu_bridge
node_firmware -->|"supplies calibration programs"| node_hal_core
node_spectral_scan -->|"uses"| node_hal_api

click node_build "https://github.com/lora-net/sx1302_hal/blob/master/Makefile"
click node_forwarder_main "https://github.com/lora-net/sx1302_hal/blob/master/packet_forwarder/src/lora_pkt_fwd.c"
click node_udp_protocol "https://github.com/lora-net/sx1302_hal/blob/master/packet_forwarder/PROTOCOL.md"
click node_json_parser "https://github.com/lora-net/sx1302_hal/blob/master/libtools/src/parson.c"
click node_jit_queue "https://github.com/lora-net/sx1302_hal/blob/master/packet_forwarder/src/jitqueue.c"
click node_hal_api "https://github.com/lora-net/sx1302_hal/blob/master/libloragw/inc/loragw_hal.h"
click node_hal_core "https://github.com/lora-net/sx1302_hal/blob/master/libloragw/src/loragw_hal.c"
click node_sx1302_driver "https://github.com/lora-net/sx1302_hal/blob/master/libloragw/src/loragw_sx1302.c"
click node_sx1302_rx "https://github.com/lora-net/sx1302_hal/blob/master/libloragw/src/loragw_sx1302_rx.c"
click node_timing_gps "https://github.com/lora-net/sx1302_hal/blob/master/libloragw/src/loragw_sx1302_timestamp.c"
click node_lbt "https://github.com/lora-net/sx1302_hal/blob/master/libloragw/src/loragw_lbt.c"
click node_register_access "https://github.com/lora-net/sx1302_hal/blob/master/libloragw/src/loragw_reg.c"
click node_rf_frontends "https://github.com/lora-net/sx1302_hal/blob/master/libloragw/src/loragw_sx1250.c"
click node_transport "https://github.com/lora-net/sx1302_hal/blob/master/libloragw/src/loragw_com.c"
click node_mcu_bridge "https://github.com/lora-net/sx1302_hal/blob/master/libloragw/src/loragw_mcu.c"
click node_reset_script "https://github.com/lora-net/sx1302_hal/blob/master/tools/reset_lgw.sh"
click node_spectral_scan "https://github.com/lora-net/sx1302_hal/blob/master/util_spectral_scan/src/spectral_scan.c"
click node_systemd_service "https://github.com/lora-net/sx1302_hal/blob/master/tools/systemd/lora_pkt_fwd.service"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_forwarder_main,node_regional_config,node_udp_protocol,node_json_parser,node_jit_queue toneBlue
class node_hal_api,node_hal_core,node_sx1302_driver,node_sx1302_rx,node_timing_gps,node_lbt toneAmber
class node_register_access,node_rf_frontends,node_transport,node_mcu_bridge,node_firmware toneMint
class node_reset_script,node_spectral_scan,node_systemd_service toneRose
class node_build,node_network_server toneNeutral
```

## Components

### Packet Forwarder

- [UDP packet forwarder](https://github.com/lora-net/sx1302_hal/blob/master/packet_forwarder/src/lora_pkt_fwd.c): gateway runtime · Configures the concentrator and bridges LoRa packets to a network server. · `packet_forwarder/src/lora_pkt_fwd.c`
- **Regional configuration**: JSON config
- [UDP wire protocol](https://github.com/lora-net/sx1302_hal/blob/master/packet_forwarder/PROTOCOL.md): protocol contract · `packet_forwarder/PROTOCOL.md`
- [JSON parser](https://github.com/lora-net/sx1302_hal/blob/master/libtools/src/parson.c): parser library · `libtools/src/parson.c`
- [JIT downlink queue](https://github.com/lora-net/sx1302_hal/blob/master/packet_forwarder/src/jitqueue.c): timing arbiter · Queues timestamped downlinks against RF transmission deadlines. · `packet_forwarder/src/jitqueue.c`

### Concentrator HAL

- [HAL public API](https://github.com/lora-net/sx1302_hal/blob/master/libloragw/inc/loragw_hal.h): C API · `libloragw/inc/loragw_hal.h`
- [Concentrator HAL](https://github.com/lora-net/sx1302_hal/blob/master/libloragw/src/loragw_hal.c): static library core · Owns configuration, calibration, RX extraction, timestamps, and scheduled TX. · `libloragw/src/loragw_hal.c`
- [SX1302 driver](https://github.com/lora-net/sx1302_hal/blob/master/libloragw/src/loragw_sx1302.c): concentrator driver · `libloragw/src/loragw_sx1302.c`
- [SX1302 RX path](https://github.com/lora-net/sx1302_hal/blob/master/libloragw/src/loragw_sx1302_rx.c): receive driver · `libloragw/src/loragw_sx1302_rx.c`
- [Timestamp and GPS](https://github.com/lora-net/sx1302_hal/blob/master/libloragw/src/loragw_sx1302_timestamp.c): timing services · `libloragw/src/loragw_sx1302_timestamp.c`
- [Listen-Before-Talk](https://github.com/lora-net/sx1302_hal/blob/master/libloragw/src/loragw_lbt.c): radio service · `libloragw/src/loragw_lbt.c`

### Hardware Boundary

- [Register access](https://github.com/lora-net/sx1302_hal/blob/master/libloragw/src/loragw_reg.c): register driver · `libloragw/src/loragw_reg.c`
- [RF front-end drivers](https://github.com/lora-net/sx1302_hal/blob/master/libloragw/src/loragw_sx1250.c): SX1250/SX125x/SX1261 · `libloragw/src/loragw_sx1250.c`
- [SPI / USB transport](https://github.com/lora-net/sx1302_hal/blob/master/libloragw/src/loragw_com.c): communication layer · Selects the physical host-to-board transport. · `libloragw/src/loragw_com.c`
- [USB MCU bridge](https://github.com/lora-net/sx1302_hal/blob/master/libloragw/src/loragw_mcu.c): STM32 protocol · USB gateways use an MCU as a USB-to-SPI bridge. · `libloragw/src/loragw_mcu.c`
- **Radio firmware blobs**: firmware data

### Operations

- [Board reset and power](https://github.com/lora-net/sx1302_hal/blob/master/tools/reset_lgw.sh): hardware setup · `tools/reset_lgw.sh`
- [Spectral scan utility](https://github.com/lora-net/sx1302_hal/blob/master/util_spectral_scan/src/spectral_scan.c): radio diagnostic · `util_spectral_scan/src/spectral_scan.c`
- [Systemd service](https://github.com/lora-net/sx1302_hal/blob/master/tools/systemd/lora_pkt_fwd.service): deployment unit · `tools/systemd/lora_pkt_fwd.service`

### Other components

- [Build and install](https://github.com/lora-net/sx1302_hal/blob/master/Makefile): Make orchestration · `Makefile`
- **Network server**: external UDP peer

## Connections

- 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
