diff --git a/examples/lora/atcmd/commands.go b/examples/lora/atcmd/commands.go index 20f6a0b..f06bda6 100644 --- a/examples/lora/atcmd/commands.go +++ b/examples/lora/atcmd/commands.go @@ -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. diff --git a/examples/lora/atcmd/radio.go b/examples/lora/atcmd/radio.go index 6cf1248..15cc4db 100644 --- a/examples/lora/atcmd/radio.go +++ b/examples/lora/atcmd/radio.go @@ -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) diff --git a/examples/lora/atcmd/sim.go b/examples/lora/atcmd/sim.go index 1af9173..57f8b9a 100644 --- a/examples/lora/atcmd/sim.go +++ b/examples/lora/atcmd/sim.go @@ -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() +} diff --git a/examples/lora/atcmd/sx126x.go b/examples/lora/atcmd/sx126x.go index fa2df2c..41add5c 100644 --- a/examples/lora/atcmd/sx126x.go +++ b/examples/lora/atcmd/sx126x.go @@ -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" } diff --git a/examples/lora/atcmd/sx127x.go b/examples/lora/atcmd/sx127x.go index e620763..986119c 100644 --- a/examples/lora/atcmd/sx127x.go +++ b/examples/lora/atcmd/sx127x.go @@ -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)) }