examples: lora atcmd compiled for simulator, sx126x, and sx127x

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram
2023-01-06 17:04:21 +01:00
parent 59ff8a4585
commit dc953b09b6
5 changed files with 144 additions and 6 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ func quicktest() {
// Check firmware version.
func version() {
writeCommandOutput("VER", currentVersion())
writeCommandOutput("VER", currentVersion()+" "+firmwareVersion())
}
// Use to check the ID of the LoRaWAN module, or change the ID.
+9 -1
View File
@@ -1,7 +1,15 @@
package main
import (
"errors"
)
var (
errRadioNotFound = errors.New("radio not found")
)
type LoraRadio interface {
Reset() error
Reset()
LoraTx(pkt []uint8, timeoutMs uint32) error
LoraRx(timeoutMs uint32) ([]uint8, error)
SetLoraFrequency(freq uint32)
+5 -2
View File
@@ -10,8 +10,7 @@ func setupLora() (LoraRadio, error) {
type SimLoraRadio struct {
}
func (sr *SimLoraRadio) Reset() error {
return nil
func (sr *SimLoraRadio) Reset() {
}
func (sr *SimLoraRadio) LoraTx(pkt []uint8, timeoutMs uint32) error {
@@ -28,3 +27,7 @@ func (sr *SimLoraRadio) SetLoraCodingRate(cr uint8) {}
func (sr *SimLoraRadio) SetLoraBandwidth(bw uint8) {}
func (sr *SimLoraRadio) SetLoraCrc(enable bool) {}
func (sr *SimLoraRadio) SetLoraSpreadingFactor(sf uint8) {}
func firmwareVersion() string {
return "Simulator "+currentVersion()
}
+58 -1
View File
@@ -2,7 +2,64 @@
package main
import (
"device/stm32"
"machine"
"runtime/interrupt"
rfswitch "tinygo.org/x/drivers/examples/sx126x/rfswitch"
"tinygo.org/x/drivers/lora"
"tinygo.org/x/drivers/sx126x"
)
const FREQ = 868100000
var (
loraRadio *sx126x.Device
)
// do sx126x setup here
func setupLora() (LoraRadio, error) {
return nil, nil
loraRadio = sx126x.New(machine.SPI3)
loraRadio.SetDeviceType(sx126x.DEVICE_TYPE_SX1262)
// Create RF Switch
var radioSwitch rfswitch.CustomSwitch
loraRadio.SetRfSwitch(radioSwitch)
if state := loraRadio.DetectDevice(); !state {
return nil, errRadioNotFound
}
// Add interrupt handler for Radio IRQs
intr := interrupt.New(stm32.IRQ_Radio_IRQ_Busy, radioIntHandler)
intr.Enable()
loraConf := lora.Config{
Freq: FREQ,
Bw: lora.Bandwidth_500_0,
Sf: lora.SpreadingFactor9,
Cr: lora.CodingRate4_7,
HeaderType: lora.HeaderExplicit,
Preamble: 12,
Ldr: lora.LowDataRateOptimizeOff,
Iq: lora.IQStandard,
Crc: lora.CRCOn,
SyncWord: lora.SyncPrivate,
LoraTxPowerDBm: 20,
}
loraRadio.LoraConfig(loraConf)
return loraRadio, nil
}
// radioIntHandler will take care of radio interrupts
func radioIntHandler(intr interrupt.Interrupt) {
loraRadio.HandleInterrupt()
}
func firmwareVersion() string {
return "sx126x"
}
+71 -1
View File
@@ -2,7 +2,77 @@
package main
import (
"strconv"
"machine"
"tinygo.org/x/drivers/lora"
"tinygo.org/x/drivers/sx127x"
)
const FREQ = 868100000
var (
// We assume LoRa Featherwing module is connected to PyBadge:
rstPin = machine.D11
csPin = machine.D10
dio0Pin = machine.D6
dio1Pin = machine.D9
spi = machine.SPI0
loraRadio *sx127x.Device
)
// do sx127x setup here
func setupLora() (LoraRadio, error) {
return nil, nil
rstPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
dio0Pin.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
dio1Pin.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
spi.Configure(machine.SPIConfig{Frequency: 500000, Mode: 0})
loraRadio = sx127x.New(spi, csPin, rstPin)
loraRadio.Reset()
if state := loraRadio.DetectDevice(); !state {
return nil, errRadioNotFound
}
// Setup DIO0 interrupt Handling
if err := dio0Pin.SetInterrupt(machine.PinRising, dioIrqHandler); err != nil {
println("could not configure DIO0 pin interrupt:", err.Error())
}
// Setup DIO1 interrupt Handling
if err := dio1Pin.SetInterrupt(machine.PinRising, dioIrqHandler); err != nil {
println("could not configure DIO1 pin interrupt:", err.Error())
}
// 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,
}
loraRadio.LoraConfig(loraConf)
return loraRadio, nil
}
func dioIrqHandler(machine.Pin) {
loraRadio.HandleInterrupt()
}
func firmwareVersion() string {
v := loraRadio.GetVersion()
return "sx127x v"+strconv.Itoa(int(v))
}