A highly optimized STM32 HAL-based driver for the LoRa1262 (SX1262) module, enabling LoRa communication using SPI. This project is built for STM32CubeIDE and is designed for low-power, long-range wireless communication.
✅ Full-duplex SPI communication with SX1262
✅ LoRa packet transmission and reception
✅ Interrupt-based packet handling
✅ Standby, Receive, and Transmit modes
✅ Adjustable frequency, bandwidth, spreading factor, and coding rate
✅ Low-power mode for energy efficiency
✅ Error handling & status monitoring
- LoRa1262 Module (SX1262)
- STM32 Microcontroller (Tested on STM32G0, STM32F4, etc.)
- SPI-compatible connections
- 3.3V Power Supply
- ST-Link Debugger (Optional)
| STM32 Pin | SX1262 Pin | Description |
|---|---|---|
MOSI (PA7) |
MOSI |
SPI Data Input |
MISO (PA6) |
MISO |
SPI Data Output |
SCK (PA5) |
SCK |
SPI Clock |
NSS (PA4) |
NSS |
Chip Select |
RESET (PB0) |
RESET |
Module Reset |
BUSY (PB1) |
BUSY |
Module Busy Pin |
DIO1 (PB2) |
DIO1 |
Interrupt Pin |
GND |
GND |
Ground |
3.3V |
VCC |
Power Supply |
├── Core/
│ ├── Src/ # Source files
│ ├── Inc/ # Header files
│ ├── main.c # Main application
│ ├── spi.c # SPI communication
│ ├── gpio.c # GPIO control
│ ├── uart.c # Debugging via UART
│ └── stm32_hal/ # HAL Libraries
├── Drivers/ # STM32 HAL Drivers
├── Libs
| ├── Src/ # Source files
| ├──SX1262.c
| ├── SX1262.h
| ├── SX1262_Definitions.h
├── README.md # Documentation
├── .gitignore # Git ignore file
└── LORA_1262f30_Receive.ioc # STM32CubeMX project file
git clone https://github.com/Saba-abiri/LORA1262.git
cd LORA1262- Import the
.iocfile in STM32CubeMX - Configure SPI and GPIO settings
- Generate project files
- SPI Mode: SPI Mode 0
- Baud Rate: ≤10MHz
- Data Size: 8-bit
- Compile the project
- Upload to STM32 using ST-Link
configuration for the SX1262 LoRa transceiver
SX1262_Get_st()->Busy_Pin = BUSY_LORA_Pin;
SX1262_Get_st()->Busy_Port= BUSY_LORA_GPIO_Port;
SX1262_Get_st()->NSS_Pin = NSS_LORA_Pin;
SX1262_Get_st()->NSS_Port = NSS_LORA_GPIO_Port;
SX1262_Get_st()->Reset_Pin = NRESET_LORA_Pin;
SX1262_Get_st()->Reset_Port = NRESET_LORA_GPIO_Port;
SX1262_Get_st()->SPI = hspi1;Call the initialization function in main.c:
SX1262_Init();uint8_t data[] = "Hello, LoRa!";
SX1262_Transmit(data, sizeof(data));set to recive mode
SX1262_setModeReceive();wait till DI01 intterupt appears!
uint8_t rxBuffer[255];
uint8_t packetLength;
SX1262_HandleCallback(rxBuffer, &packetLength);SX1262_SetFrequency(433000000); // Set to 433 MHzSX1262_setModeReceive(); // Receive Mode
SX1262_setModeStandby(); // Standby Mode| Function | Description |
|---|---|
SX1262_Init() |
Initializes the LoRa1262 module |
SX1262_Transmit(uint8_t*, uint8_t) |
Sends a LoRa packet |
SX1262_HandleCallback(uint8_t*, uint8_t*) |
Handles LoRa packet reception |
SX1262_SetFrequency(uint32_t freq) |
Sets the LoRa frequency |
SX1262_setModeReceive() |
Switches to Receive Mode |
SX1262_setModeStandby() |
Switches to Standby Mode |
SX1262_getstatus() |
Gets module status |
SX1262_IsBusy() |
Checks if the module is busy |
SX1262_BusyWait() |
Waits until the module is free |
| Issue | Possible Cause | Solution |
|---|---|---|
| ❌ No response from SX1262 | Incorrect SPI wiring | Check connections |
| ❌ Transmission failure | Wrong frequency or power settings | Verify settings in SX1262_SetFrequency() |
| ❌ Packet loss | Low signal strength or interference | Increase power, adjust SF, BW |
- Implement CRC checks for packet integrity
- Add FSK modulation support
- Improve low-power consumption
This project is open-source under the MIT License. Feel free to use, modify, and contribute!
🔗 References:
💡 Developed by Saba Abiri 🚀
