tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/sx127x/lora_rxtx/lora_rxtx.go (about) 1 package main 2 3 // This example code demonstrates Lora RX/TX With SX127x driver 4 // You need to connect SPI, RST, CS, DIO0 (aka IRQ) and DIO1 to use. 5 6 import ( 7 "machine" 8 "time" 9 10 "tinygo.org/x/drivers/lora" 11 "tinygo.org/x/drivers/sx127x" 12 ) 13 14 const ( 15 LORA_DEFAULT_RXTIMEOUT_MS = 1000 16 LORA_DEFAULT_TXTIMEOUT_MS = 5000 17 ) 18 19 var ( 20 loraRadio *sx127x.Device 21 txmsg = []byte("Hello TinyGO") 22 23 // We assume LoRa Featherwing module is connected to PyBadge: 24 SX127X_PIN_RST = machine.D11 25 SX127X_PIN_CS = machine.D10 26 SX127X_PIN_DIO0 = machine.D6 27 SX127X_PIN_DIO1 = machine.D9 28 SX127X_SPI = machine.SPI0 29 ) 30 31 func dioIrqHandler(machine.Pin) { 32 loraRadio.HandleInterrupt() 33 } 34 35 func main() { 36 time.Sleep(5 * time.Second) 37 println("\n# TinyGo Lora RX/TX test") 38 println("# ----------------------") 39 machine.LED.Configure(machine.PinConfig{Mode: machine.PinOutput}) 40 SX127X_PIN_RST.Configure(machine.PinConfig{Mode: machine.PinOutput}) 41 42 SX127X_SPI.Configure(machine.SPIConfig{Frequency: 500000, Mode: 0}) 43 44 println("main: create and start SX127x driver") 45 loraRadio = sx127x.New(SX127X_SPI, SX127X_PIN_RST) 46 loraRadio.SetRadioController(sx127x.NewRadioControl(SX127X_PIN_CS, SX127X_PIN_DIO0, SX127X_PIN_DIO1)) 47 48 loraRadio.Reset() 49 state := loraRadio.DetectDevice() 50 if !state { 51 panic("main: sx127x NOT FOUND !!!") 52 } else { 53 println("main: sx127x found") 54 } 55 56 // Prepare for Lora Operation 57 loraConf := lora.Config{ 58 Freq: lora.MHz_868_1, 59 Bw: lora.Bandwidth_125_0, 60 Sf: lora.SpreadingFactor9, 61 Cr: lora.CodingRate4_7, 62 HeaderType: lora.HeaderExplicit, 63 Preamble: 12, 64 Iq: lora.IQStandard, 65 Crc: lora.CRCOn, 66 SyncWord: lora.SyncPrivate, 67 LoraTxPowerDBm: 20, 68 } 69 70 loraRadio.LoraConfig(loraConf) 71 72 var count uint 73 for { 74 tStart := time.Now() 75 76 println("main: Receiving Lora for 10 seconds") 77 for time.Since(tStart) < 10*time.Second { 78 buf, err := loraRadio.Rx(LORA_DEFAULT_RXTIMEOUT_MS) 79 if err != nil { 80 println("RX Error: ", err) 81 } else if buf != nil { 82 println("Packet Received: len=", len(buf), string(buf)) 83 } 84 } 85 println("main: End Lora RX") 86 println("LORA TX size=", len(txmsg), " -> ", string(txmsg)) 87 err := loraRadio.Tx(txmsg, LORA_DEFAULT_TXTIMEOUT_MS) 88 if err != nil { 89 println("TX Error:", err) 90 } 91 count++ 92 } 93 94 }