LoRa

SMotlaqMIT活跃维护

面向 STM32 的 Ra-02/SX1278 LoRa 模块驱动库,基于 HAL 库封装,帮嵌入式开发者快速在 ARM 单片机上实现 LoRa 无线通信。

Ra-02 LoRa module (SX1278) library for STM32 (ARM processors) using HAL drivers ⚡

v2.0发布于 2026-10-23 09:44 UTC331star62fork同步于 2026-11-10 18:02 UTC

怎么上手

由 README 提炼,执行前请确认
把 LoRa.h 和 LoRa.c 复制进工程并加入 include 路径,然后 #include "LoRa.h";旧 API 代码可额外 #include "LoRa_compat.h"
硬件要求按 README 说明

需要带 SPI 的 STM32 微控制器和 Ra-02(SX1278)或兼容 SX127x 模块,配 433 MHz 或对应频段天线;模块必须用 3.3 V 供电,切勿接 5 V,发射前先接天线。README 列出已在 STM32F103C8、F103RE、F030C8、F411VE、F429ZG、L476RG 上测试通过。

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

基于 HAL 的 STM32 LoRa 驱动库,让 Ra-02/SX127x 模块快速收发数据

面向 STM32 的 Ra-02/SX1278 LoRa 模块驱动库,基于 HAL 库封装,帮嵌入式开发者快速在 ARM 单片机上实现 LoRa 无线通信。

核心能力

  • 用 HAL 驱动 SPI 控制 SX1276/77/78/79 系列模块
  • 可配置频率、扩频因子、带宽、编码率与发射功率
  • 支持阻塞发送带超时和中断式接收 LoRa_receive_IT
  • 读取 RSSI 与 SNR 并自动启用低速率优化
  • 提供睡眠/待机/发送/接收模式与硬件复位

容易踩的坑

模块只能接 3.3 V,接 5 V 会损坏,且发射前必须接好天线;调用 LoRa_transmit 后 FIFO 被覆盖,紧接着接收需加延时或显式切回接收模式;v2.0 改了结构体成员名(如 spredingFactor 改为 spreadingFactor),旧代码需按迁移表改名

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

2 个版本v2.0
  1. v2.0V2.0最新正式版2026-10-23 · 2026-10-23 09:44 UTC

    Release Date: October 2026

    Major refactoring and quality improvement release. This version focuses on code cleanliness, correctness, documentation and backward compatibility.

    What's New

    • Complete code cleanup

      • Fixed all typos and inconsistent naming
      • Modern and consistent API naming with LORA_ prefix
      • Better structure and readability
    • New Features

      • SNR measurement (LoRa_getSNR())
      • Improved RSSI calculation (uses SNR when negative)
      • Fully implemented LoRa_receive_IT() for interrupt-driven reception
      • SF6 support
      • Proper null-pointer and parameter checks
      • Automatic high-power mode (RegPaDac) when using +20 dBm
      • Sync Word is now set during initialization
    • Documentation

      • Full Doxygen comments for all public functions and the main structure
      • Professional README with clear examples
      • Dedicated Migration Guide
    • Compatibility

      • New LoRa_compat.h layer so older projects (v1.x) can still compile with minimal changes
      • Clear migration instructions for the three renamed structure members
    • Examples

      • Five ready-to-use examples covering:
        • Basic TX
        • Polling RX
        • Interrupt RX
        • Ping-Pong
        • Long-range configuration

    Breaking Changes

    Old NameNew Name
    spredingFactorspreadingFactor
    bandWidthbandwidth
    crcRatecodingRate
    STNBY_MODELORA_MODE_STANDBY
    POWER_20db etc.LORA_POWER_20DBM etc.

    Most old #defines still work if you include LoRa_compat.h.

    Recommended Action for Existing Users

    1. Replace LoRa.h and LoRa.c with the new files.
    2. Add #include "LoRa_compat.h".
    3. Rename the first three structure fields mentioned above.
    4. Test your application.

    Full Changelog: See commit history and MIGRATION.md

  2. v1.0V1.02026-10-23 · 2026-10-23 09:43 UTC
    查看更新说明

    Release Date: 2020

    First public release of the Ra-02 / SX1278 LoRa library for STM32 (HAL).

    Features

    • Basic initialization and configuration of SX1278-based modules (Ra-02)
    • Support for Frequency, Spreading Factor (SF7–SF12), Bandwidth, Coding Rate, Output Power and Preamble
    • Over Current Protection (OCP) setting
    • Blocking transmit with timeout
    • Continuous receive mode (polling)
    • RSSI reading
    • Sleep / Standby / TX / RX Continuous modes
    • Simple and easy-to-use API

    Tested MCUs

    • STM32F103C8 / F103RE
    • STM32F030C8
    • STM32F411VE (Discovery)
    • STM32F429ZG
    • STM32L476RG (Nucleo)

    Known Limitations

    • Typographical errors in several names (spredingFactor, bandWidth, crcRate, STNBY_MODE, etc.)
    • LoRa_isvalid() always returns success
    • LoRa_receive_IT() declared but not implemented
    • No SNR support
    • RSSI calculation does not take SNR into account
    • No SF6 support
    • Limited error checking
    • Documentation is basic

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    Contributors Forks Stargazers Issues MIT License

    LoRa Library for STM32 (SX1278 / Ra-02)

    A clean, well-documented, and reliable HAL-based library for the Ra-02 (and other SX1276/77/78/79 based) LoRa modules on STM32 microcontrollers.

    Version 2.0 - Oct 2026 – Major cleanup, bug fixes, SNR support, improved RSSI, Doxygen documentation, and compatibility layer for older code.


    Donate

    Is it helpful?


    Tutorial Video


    Features

    • Full control over LoRa parameters (Frequency, Spreading Factor, Bandwidth, Coding Rate, Power, Preamble, OCP)
    • Blocking transmit with timeout
    • Continuous receive mode + interrupt-friendly receive (LoRa_receive_IT)
    • RSSI and SNR measurement
    • Automatic Low Data Rate Optimization
    • Sleep / Standby / TX / RX modes
    • Hardware reset support
    • Clean Doxygen documentation
    • Compatibility layer for code written with the old API (before October 2026)

    Successfully Tested On

    MCUBoardStatus
    STM32F103C8Blue Pill✅
    STM32F103RE✅
    STM32F030C8✅
    STM32F411VEDiscovery✅
    STM32F429ZG✅
    STM32L476RGNucleo-L476RG✅

    Hardware Requirements

    • Ra-02 LoRa module (SX1278) or compatible SX127x module
    • 433 MHz antenna (or matching antenna for your frequency)
    • STM32 microcontroller with SPI

    Wiring

    STM32 PinRa-02 Pin
    SPI MISOMISO
    SPI MOSIMOSI
    SPI SCKSCK
    Any GPIO (output, default HIGH)NSS
    Any GPIO (output, default HIGH)RST
    Any GPIO (input + EXTI rising)DIO0
    3.3 V3.3 V
    GNDGND

    Important: Never power the module with 5 V. Always use 3.3 V and connect an antenna before transmitting.


    Installation

    1. Copy LoRa.h and LoRa.c into your project.
    2. Add the include path if necessary.
    3. Include the header in your source files:
    #include "LoRa.h"
    

    Using the Compatibility Layer (for old code)

    If you have existing projects written with the old API (before October 2026 / version < 2.0), also include the compatibility header:

    #include "LoRa.h"
    #include "LoRa_compat.h"   // ← enables old names
    

    This restores most of the old #defines (SF_7, BW_125KHz, STNBY_MODE, POWER_20db, etc.).

    Note: You still need to rename the structure members (see Migration Guide).


    Quick Start

    #include "LoRa.h"
    
    LoRa myLoRa;
    
    int main(void)
    {
        // ... HAL init, SPI init, GPIO init ...
    
        myLoRa = newLoRa();
    
        // Hardware pins
        myLoRa.CS_port     = NSS_GPIO_Port;
        myLoRa.CS_pin      = NSS_Pin;
        myLoRa.reset_port  = RESET_GPIO_Port;
        myLoRa.reset_pin   = RESET_Pin;
        myLoRa.DIO0_port   = DIO0_GPIO_Port;
        myLoRa.DIO0_pin    = DIO0_Pin;
        myLoRa.hSPIx       = &hspi1;
    
        // Optional radio settings (defaults are fine for many cases)
        myLoRa.frequency        = 433;                 // MHz
        myLoRa.spreadingFactor  = LORA_SF_7;
        myLoRa.bandwidth        = LORA_BW_125_KHZ;
        myLoRa.codingRate       = LORA_CR_4_5;
        myLoRa.power            = LORA_POWER_17DBM;
        myLoRa.overCurrentProtection = 100;            // mA
        myLoRa.preamble         = 8;
    
        uint16_t status = LoRa_init(&myLoRa);
    
        if (status == LORA_OK) {
            // Module is ready
        } else if (status == LORA_NOT_FOUND) {
            // Cannot communicate with the module (check wiring / SPI)
        } else {
            // Invalid configuration (NULL pointers)
        }
    
        while (1) {
            // Your application
        }
    }
    

    Configuration Options

    Spreading Factor

    LORA_SF_6 ... LORA_SF_12
    

    Bandwidth

    LORA_BW_7_8_KHZ, LORA_BW_10_4_KHZ, LORA_BW_15_6_KHZ,
    LORA_BW_20_8_KHZ, LORA_BW_31_25_KHZ, LORA_BW_41_7_KHZ,
    LORA_BW_62_5_KHZ, LORA_BW_125_KHZ, LORA_BW_250_KHZ, LORA_BW_500_KHZ
    

    Coding Rate

    LORA_CR_4_5, LORA_CR_4_6, LORA_CR_4_7, LORA_CR_4_8
    

    Output Power

    LORA_POWER_11DBM, LORA_POWER_14DBM, LORA_POWER_17DBM, LORA_POWER_20DBM
    

    When using LORA_POWER_20DBM, the library automatically enables high-power mode via RegPaDac.
    Make sure your OCP setting is high enough (≥ 140 mA recommended for +20 dBm).

    Over Current Protection

    Valid range: 45 mA – 240 mA
    (Values are rounded according to the datasheet formula.)


    Transmitting Data

    uint8_t data[] = "Hello LoRa!";
    uint8_t success = LoRa_transmit(&myLoRa, data, sizeof(data) - 1, 1000); // 1 second timeout
    
    if (success) {
        // Transmission completed (TxDone)
    } else {
        // Timeout
    }
    

    Note: After calling LoRa_transmit(), the FIFO is overwritten. If you need to receive right after transmitting, add a short delay or switch back to receive mode explicitly.


    Receiving Data

    Polling mode

    LoRa_startReceiving(&myLoRa);
    
    uint8_t buffer[64];
    uint8_t len = LoRa_receive(&myLoRa, buffer, sizeof(buffer));
    
    if (len > 0) {
        // New packet received
        int rssi = LoRa_getRSSI(&myLoRa);
        float snr = LoRa_getSNR(&myLoRa);
    }
    

    Interrupt mode (recommended)

    1. Configure DIO0 as external interrupt (rising edge).
    2. In the EXTI callback:
    void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
    {
        if (GPIO_Pin == DIO0_Pin) {
            uint8_t buffer[64];
            uint8_t len = LoRa_receive_IT(&myLoRa, buffer, sizeof(buffer));
    
            if (len > 0) {
                // Process packet
                int rssi = LoRa_getRSSI(&myLoRa);
                float snr = LoRa_getSNR(&myLoRa);
            }
        }
    }
    

    Signal Quality

    int   rssi = LoRa_getRSSI(&myLoRa);   // dBm (improved, uses SNR when negative)
    float snr  = LoRa_getSNR(&myLoRa);    // dB  (resolution 0.25 dB)
    

    Migration from Old Versions (before October 2026)

    The October 2026 release (v2.0) introduced several breaking changes for better code quality:

    Old nameNew name
    spredingFactorspreadingFactor
    bandWidthbandwidth
    crcRatecodingRate
    STNBY_MODELORA_MODE_STANDBY
    POWER_20dbLORA_POWER_20DBM
    SF_7, BW_125KHz…LORA_SF_7, LORA_BW_125_KHZ…

    Recommended migration steps

    1. Replace LoRa.h / LoRa.c with the new files.
    2. Add #include "LoRa_compat.h" temporarily.
    3. Rename the first three structure members listed above.
    4. (Optional) Gradually switch to the new LORA_ prefixed defines.
    5. Remove LoRa_compat.h when your code is fully updated.

    A detailed migration guide is available in MIGRATION.md.


    Status Codes

    CodeMacroMeaning
    200LORA_OKInitialization successful
    404LORA_NOT_FOUNDModule not responding (SPI / wiring issue)
    413LORA_LARGE_PAYLOADPayload larger than 255 bytes
    503LORA_UNAVAILABLEInvalid configuration (NULL pointers etc.)

    Project Structure (Recommended)

    YourProject/
    ├── Core/
    │   └── ...
    ├── Drivers/
    │   └── ...
    ├── LoRa/
    │   ├── LoRa.h
    │   ├── LoRa.c
    │   └── LoRa_compat.h      ← only if you need old API
    └── ...
    

    Documentation

    All public functions are documented with Doxygen comments.
    You can generate HTML documentation with:

    doxygen Doxyfile
    

    Contributing

    Contributions are welcome! Please:

    1. Open an issue first for major changes.
    2. Follow the existing code style.
    3. Keep the compatibility layer working when possible.
    4. Update documentation if needed.

    Author & Contact

    Original author: Salman Ami Motlaq (@S_Motlaq)
    Email: [email protected]

    If this library saved you time, consider giving it a ⭐ on GitHub!