mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-07 08:23:42 +00:00
lora: move shared config for sx126x/sx127x to single package
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
@@ -1,127 +0,0 @@
|
||||
// Receives data with LoRa.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"machine"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"tinygo.org/x/drivers/lora/sx127x"
|
||||
)
|
||||
|
||||
var loraConfig = sx127x.Config{
|
||||
Frequency: 868100000,
|
||||
SpreadingFactor: 12,
|
||||
Bandwidth: 125000,
|
||||
CodingRate: 6,
|
||||
TxPower: 17,
|
||||
}
|
||||
|
||||
const (
|
||||
|
||||
// Serial console
|
||||
UART_TX_PIN = machine.PA9
|
||||
UART_RX_PIN = machine.PA10
|
||||
|
||||
// RFM95 SPI Connection to Bluepill
|
||||
SPI_SCK_PIN = machine.PA5
|
||||
SPI_SDO_PIN = machine.PA7
|
||||
SPI_SDI_PIN = machine.PA6
|
||||
SPI_CS_PIN = machine.PB8
|
||||
SPI_RST_PIN = machine.PB9
|
||||
|
||||
// DIO RFM95 Pin connection to BluePill
|
||||
DIO0_PIN = machine.PA2
|
||||
DIO0_PIN_MODE = machine.PinInputPulldown
|
||||
DIO0_PIN_CHANGE = machine.PinRising
|
||||
|
||||
pollDelayMs int = 1000
|
||||
)
|
||||
|
||||
// configureDioInt sets up the DIO0 interrupt handler
|
||||
func configureDioInt(radio *sx127x.Device) {
|
||||
// Set an interrupt on this pin.
|
||||
err := DIO0_PIN.SetInterrupt(DIO0_PIN_CHANGE, func(machine.Pin) {
|
||||
if DIO0_PIN.Get() {
|
||||
radio.CheckIrq()
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
println("could not configure pin interrupt:", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Configure serial console
|
||||
machine.Serial.Configure(machine.UARTConfig{TX: UART_TX_PIN, RX: UART_RX_PIN, BaudRate: 115200})
|
||||
|
||||
println("LoRa PingPong Example")
|
||||
|
||||
// Prepare GPIOS (SPI + DIO)
|
||||
machine.LED.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
SPI_CS_PIN.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
SPI_RST_PIN.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
DIO0_PIN.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
|
||||
machine.LED.Set(true)
|
||||
|
||||
// Enable SPI
|
||||
machine.SPI0.Configure(machine.SPIConfig{
|
||||
SCK: SPI_SCK_PIN,
|
||||
SDO: SPI_SDO_PIN,
|
||||
SDI: SPI_SDI_PIN,
|
||||
Frequency: 500000,
|
||||
Mode: 0})
|
||||
|
||||
// Create and Reset Lora driver
|
||||
loraRadio := sx127x.New(machine.SPI0, SPI_CS_PIN, SPI_RST_PIN)
|
||||
loraRadio.Reset()
|
||||
|
||||
// Setup DIO0 interrupt Handling
|
||||
configureDioInt(&loraRadio)
|
||||
|
||||
// Configure Lora settings (modulation, SF... etc )
|
||||
var err = loraRadio.SetupLora(loraConfig)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
// ping counter is incremented after every retransmission
|
||||
pingCount := 0
|
||||
|
||||
// Send first packet without using interrupts
|
||||
println("*** Send a Lora PING ")
|
||||
machine.LED.Set(false)
|
||||
loraRadio.TxLora([]byte("PING " + strconv.Itoa(pingCount)))
|
||||
machine.LED.Set(true)
|
||||
|
||||
println("*** Switch to RX Mode ")
|
||||
for {
|
||||
loraRadio.RxLora()
|
||||
// Wait for Radio event
|
||||
in := <-loraRadio.GetRadioEventChan()
|
||||
|
||||
// We have a packet
|
||||
if in.EventType == sx127x.EventRxDone {
|
||||
data := string(in.EventData)
|
||||
println("RX: ", data)
|
||||
|
||||
machine.LED.Set(false)
|
||||
// If it's ping, reply pong
|
||||
if strings.Contains(data, "PING") {
|
||||
println("Received PING, Sending PONG")
|
||||
loraRadio.TxLora([]byte("PONG #" + strconv.Itoa(pingCount)))
|
||||
} else if strings.Contains(data, "PONG") {
|
||||
println("Received PONG, Sending PING")
|
||||
loraRadio.TxLora([]byte("PING #" + strconv.Itoa(pingCount)))
|
||||
}
|
||||
machine.LED.Set(true)
|
||||
|
||||
pingCount++
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
// Receives data with LoRa.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"machine"
|
||||
|
||||
"tinygo.org/x/drivers/lora/sx127x"
|
||||
)
|
||||
|
||||
var loraConfig = sx127x.Config{
|
||||
Frequency: 433998500,
|
||||
SpreadingFactor: 7,
|
||||
Bandwidth: 125000,
|
||||
CodingRate: 6,
|
||||
TxPower: 17,
|
||||
}
|
||||
|
||||
var packet [255]byte
|
||||
|
||||
func main() {
|
||||
println("LoRa Receiver Example")
|
||||
|
||||
// SPI settings for Feather M0 LoRa board
|
||||
csPin := machine.PB1
|
||||
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
rstPin := machine.PB2
|
||||
rstPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
dio0Pin := machine.PB3
|
||||
dio0Pin.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
|
||||
machine.SPI0.Configure(machine.SPIConfig{})
|
||||
loraRadio := sx127x.New(machine.SPI0, csPin, rstPin)
|
||||
var err = loraRadio.SetupLora(loraConfig)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
println("Receiving LoRa packets...")
|
||||
|
||||
for {
|
||||
packetSize := loraRadio.ParsePacket(0)
|
||||
if packetSize > 0 {
|
||||
println("Got packet, RSSI=", loraRadio.LastPacketRSSI())
|
||||
size := loraRadio.ReadPacket(packet[:])
|
||||
println(string(packet[:size]))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
// Sends data with LoRa.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"machine"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers/lora/sx127x"
|
||||
)
|
||||
|
||||
var loraConfig = sx127x.Config{
|
||||
Frequency: 868100000,
|
||||
SpreadingFactor: 12,
|
||||
Bandwidth: 125000,
|
||||
CodingRate: 6,
|
||||
TxPower: 17,
|
||||
}
|
||||
|
||||
const (
|
||||
SPI_SCK_PIN = machine.PA5
|
||||
SPI_SDO_PIN = machine.PA7
|
||||
SPI_SDI_PIN = machine.PA6
|
||||
|
||||
SPI_CS_PIN = machine.PB8
|
||||
SPI_RST_PIN = machine.PB9
|
||||
DIO0_PIN = machine.PA2
|
||||
|
||||
UART_TX_PIN = machine.PA9
|
||||
UART_RX_PIN = machine.PA10
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
// Configure serial console
|
||||
machine.Serial.Configure(machine.UARTConfig{TX: UART_TX_PIN, RX: UART_RX_PIN, BaudRate: 115200})
|
||||
|
||||
println("LoRa Sender Example")
|
||||
|
||||
// Prepare GPIOS (SPI + DIO)
|
||||
machine.LED.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
SPI_CS_PIN.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
SPI_RST_PIN.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
DIO0_PIN.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
|
||||
// Enable SPI
|
||||
machine.SPI0.Configure(machine.SPIConfig{
|
||||
SCK: SPI_SCK_PIN,
|
||||
SDO: SPI_SDO_PIN,
|
||||
SDI: SPI_SDI_PIN,
|
||||
Frequency: 500000,
|
||||
Mode: 0})
|
||||
|
||||
// Create and Reset Lora driver
|
||||
loraRadio := sx127x.New(machine.SPI0, SPI_CS_PIN, SPI_RST_PIN)
|
||||
loraRadio.Reset()
|
||||
|
||||
// Check module identification
|
||||
if loraRadio.GetVersion() != 0x12 {
|
||||
println("SX1276 module not found")
|
||||
for {
|
||||
}
|
||||
} else {
|
||||
println("Module version 0x12")
|
||||
}
|
||||
|
||||
// Configure Lora settings (modulation, SF... etc )
|
||||
var err = loraRadio.SetupLora(loraConfig)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
// Send a Lora packet every 5 sec
|
||||
println("Sending LoRa packets every 5 seconds...")
|
||||
for i := 0; ; i++ {
|
||||
var packet = "TinyGo LoRa Sender: " + strconv.Itoa(i)
|
||||
println("Sending:", packet)
|
||||
|
||||
machine.LED.Low()
|
||||
time.Sleep(time.Millisecond * 150)
|
||||
machine.LED.High()
|
||||
|
||||
loraRadio.TxLora([]byte(packet))
|
||||
time.Sleep(5000 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
rfswitch "tinygo.org/x/drivers/examples/sx126x/rfswitch"
|
||||
|
||||
"tinygo.org/x/drivers/lora"
|
||||
"tinygo.org/x/drivers/sx126x"
|
||||
)
|
||||
|
||||
@@ -36,17 +37,17 @@ func main() {
|
||||
}
|
||||
|
||||
// Prepare for Lora operation
|
||||
loraConf := sx126x.LoraConfig{
|
||||
loraConf := lora.Config{
|
||||
Freq: FREQ,
|
||||
Bw: sx126x.SX126X_LORA_BW_500_0,
|
||||
Sf: sx126x.SX126X_LORA_SF9,
|
||||
Cr: sx126x.SX126X_LORA_CR_4_7,
|
||||
HeaderType: sx126x.SX126X_LORA_HEADER_EXPLICIT,
|
||||
Bw: lora.Bandwidth_500_0,
|
||||
Sf: lora.SpreadingFactor9,
|
||||
Cr: lora.CodingRate4_7,
|
||||
HeaderType: lora.HeaderExplicit,
|
||||
Preamble: 12,
|
||||
Ldr: sx126x.SX126X_LORA_LOW_DATA_RATE_OPTIMIZE_OFF,
|
||||
Iq: sx126x.SX126X_LORA_IQ_STANDARD,
|
||||
Crc: sx126x.SX126X_LORA_CRC_ON,
|
||||
SyncWord: sx126x.SX126X_LORA_MAC_PRIVATE_SYNCWORD,
|
||||
Ldr: lora.LowDataRateOptimizeOff,
|
||||
Iq: lora.IQStandard,
|
||||
Crc: lora.CRCOn,
|
||||
SyncWord: lora.SyncPrivate,
|
||||
LoraTxPowerDBm: 14,
|
||||
}
|
||||
loraRadio.LoraConfig(loraConf)
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
rfswitch "tinygo.org/x/drivers/examples/sx126x/rfswitch"
|
||||
|
||||
"tinygo.org/x/drivers/lora"
|
||||
"tinygo.org/x/drivers/sx126x"
|
||||
)
|
||||
|
||||
@@ -54,17 +55,17 @@ func main() {
|
||||
intr := interrupt.New(stm32.IRQ_Radio_IRQ_Busy, radioIntHandler)
|
||||
intr.Enable()
|
||||
|
||||
loraConf := sx126x.LoraConfig{
|
||||
loraConf := lora.Config{
|
||||
Freq: FREQ,
|
||||
Bw: sx126x.SX126X_LORA_BW_500_0,
|
||||
Sf: sx126x.SX126X_LORA_SF9,
|
||||
Cr: sx126x.SX126X_LORA_CR_4_7,
|
||||
HeaderType: sx126x.SX126X_LORA_HEADER_EXPLICIT,
|
||||
Bw: lora.Bandwidth_500_0,
|
||||
Sf: lora.SpreadingFactor9,
|
||||
Cr: lora.CodingRate4_7,
|
||||
HeaderType: lora.HeaderExplicit,
|
||||
Preamble: 12,
|
||||
Ldr: sx126x.SX126X_LORA_LOW_DATA_RATE_OPTIMIZE_OFF,
|
||||
Iq: sx126x.SX126X_LORA_IQ_STANDARD,
|
||||
Crc: sx126x.SX126X_LORA_CRC_ON,
|
||||
SyncWord: sx126x.SX126X_LORA_MAC_PRIVATE_SYNCWORD,
|
||||
Ldr: lora.LowDataRateOptimizeOff,
|
||||
Iq: lora.IQStandard,
|
||||
Crc: lora.CRCOn,
|
||||
SyncWord: lora.SyncPrivate,
|
||||
LoraTxPowerDBm: 20,
|
||||
}
|
||||
|
||||
@@ -74,20 +75,17 @@ func main() {
|
||||
for {
|
||||
tStart := time.Now()
|
||||
|
||||
// Blocking RX for LORA_DEFAULT_RXTIMEOUT_MS
|
||||
println("Start Lora RX for 10 sec")
|
||||
println("main: Receiving Lora for 10 seconds")
|
||||
for int(time.Now().Sub(tStart).Seconds()) < 10 {
|
||||
buf, err := loraRadio.LoraRx(LORA_DEFAULT_RXTIMEOUT_MS)
|
||||
|
||||
if err != nil {
|
||||
println("RX Error: ", err)
|
||||
} else if buf != nil {
|
||||
println("Packet Received: len=", len(buf), string(buf))
|
||||
}
|
||||
}
|
||||
println("END Lora RX")
|
||||
|
||||
println("LORA TX size=", len(txmsg))
|
||||
println("main: End Lora RX")
|
||||
println("LORA TX size=", len(txmsg), " -> ", string(txmsg))
|
||||
err := loraRadio.LoraTx(txmsg, LORA_DEFAULT_TXTIMEOUT_MS)
|
||||
if err != nil {
|
||||
println("TX Error:", err)
|
||||
|
||||
@@ -7,18 +7,20 @@ import (
|
||||
"machine"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers/lora"
|
||||
"tinygo.org/x/drivers/sx127x"
|
||||
)
|
||||
|
||||
const FREQ = 868100000
|
||||
|
||||
const (
|
||||
LORA_DEFAULT_RXTIMEOUT_MS = 1000
|
||||
LORA_DEFAULT_TXTIMEOUT_MS = 5000
|
||||
|
||||
DIO_PIN_CHANGE = machine.PinRising
|
||||
)
|
||||
|
||||
var (
|
||||
loraRadio *sx127x.Device
|
||||
txmsg = []byte("Hello TinyGO")
|
||||
|
||||
// We assume the module is connected this way:
|
||||
SX127X_PIN_RST = machine.PB9
|
||||
@@ -26,29 +28,15 @@ var (
|
||||
SX127X_PIN_DIO0 = machine.PA0
|
||||
SX127X_PIN_DIO1 = machine.PA1
|
||||
SX127X_SPI = machine.SPI0
|
||||
|
||||
txmsg = []byte("Hello TinyGO")
|
||||
|
||||
// Prepare for Lora Operation
|
||||
loraConf = sx127x.LoraConfig{
|
||||
Freq: 868100000,
|
||||
Bw: sx127x.SX127X_LORA_BW_125_0,
|
||||
Sf: sx127x.SX127X_LORA_SF9,
|
||||
Cr: sx127x.SX127X_LORA_CR_4_7,
|
||||
HeaderType: sx127x.SX127X_LORA_HEADER_EXPLICIT,
|
||||
Preamble: 12,
|
||||
Iq: sx127x.SX127X_LORA_IQ_STANDARD,
|
||||
Crc: sx127x.SX127X_LORA_CRC_ON,
|
||||
SyncWord: sx127x.SX127X_LORA_MAC_PUBLIC_SYNCWORD,
|
||||
LoraTxPowerDBm: 20,
|
||||
}
|
||||
)
|
||||
|
||||
func main() {
|
||||
println("\n# TinyGo Driver SX127X RX/TX example")
|
||||
println("# ------------------------------------")
|
||||
func dioIrqHandler(machine.Pin) {
|
||||
loraRadio.HandleInterrupt()
|
||||
}
|
||||
|
||||
println("main: configuring LED/SPI/DIO/IRQ")
|
||||
func main() {
|
||||
println("\n# TinyGo Lora RX/TX test")
|
||||
println("# ----------------------")
|
||||
machine.LED.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
SX127X_PIN_RST.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
SX127X_PIN_CS.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
@@ -67,36 +55,34 @@ func main() {
|
||||
}
|
||||
|
||||
// Setup DIO0 interrupt Handling
|
||||
err := SX127X_PIN_DIO0.SetInterrupt(DIO_PIN_CHANGE, func(machine.Pin) {
|
||||
if SX127X_PIN_DIO0.Get() {
|
||||
loraRadio.HandleInterrupt()
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
if err := SX127X_PIN_DIO0.SetInterrupt(machine.PinRising, dioIrqHandler); err != nil {
|
||||
println("could not configure DIO0 pin interrupt:", err.Error())
|
||||
}
|
||||
|
||||
// Setup DIO1 interrupt Handling
|
||||
err = SX127X_PIN_DIO1.SetInterrupt(DIO_PIN_CHANGE, func(machine.Pin) {
|
||||
if SX127X_PIN_DIO1.Get() {
|
||||
loraRadio.HandleInterrupt()
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
if err := SX127X_PIN_DIO1.SetInterrupt(machine.PinRising, dioIrqHandler); err != nil {
|
||||
println("could not configure DIO1 pin interrupt:", err.Error())
|
||||
}
|
||||
|
||||
println("main: Configure lora modulation")
|
||||
loraRadio.LoraConfig(loraConf)
|
||||
// Prepare for Lora Operation
|
||||
loraConf := lora.Config{
|
||||
Freq: FREQ,
|
||||
Bw: lora.Bandwidth_500_0,
|
||||
Sf: lora.SpreadingFactor9,
|
||||
Cr: lora.CodingRate4_7,
|
||||
HeaderType: lora.HeaderExplicit,
|
||||
Preamble: 12,
|
||||
Iq: lora.IQStandard,
|
||||
Crc: lora.CRCOn,
|
||||
SyncWord: lora.SyncPrivate,
|
||||
LoraTxPowerDBm: 20,
|
||||
}
|
||||
|
||||
// Get uint32 from RSSI
|
||||
rand32 := loraRadio.RandomU32()
|
||||
println("main: Get random 32bit from RSSI:", rand32)
|
||||
loraRadio.LoraConfig(loraConf)
|
||||
|
||||
var count uint
|
||||
for {
|
||||
tStart := time.Now()
|
||||
machine.LED.Set(!machine.LED.Get())
|
||||
|
||||
println("main: Receiving Lora for 10 seconds")
|
||||
for int(time.Now().Sub(tStart).Seconds()) < 10 {
|
||||
|
||||
Reference in New Issue
Block a user