tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/lora/lorawan/common/sx127x.go (about)

     1  //go:build featherwing || lgt92
     2  
     3  package common
     4  
     5  import (
     6  	"strconv"
     7  
     8  	"machine"
     9  
    10  	"tinygo.org/x/drivers/lora"
    11  	"tinygo.org/x/drivers/sx127x"
    12  )
    13  
    14  const (
    15  	FREQ                      = 868100000
    16  	LORA_DEFAULT_RXTIMEOUT_MS = 1000
    17  	LORA_DEFAULT_TXTIMEOUT_MS = 5000
    18  )
    19  
    20  var (
    21  	loraRadio *sx127x.Device
    22  )
    23  
    24  // do sx127x setup here
    25  func SetupLora() (lora.Radio, error) {
    26  	rstPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
    27  	spi.Configure(machine.SPIConfig{Frequency: 500000, Mode: 0})
    28  
    29  	loraRadio = sx127x.New(spi, rstPin)
    30  	loraRadio.SetRadioController(sx127x.NewRadioControl(csPin, dio0Pin, dio1Pin))
    31  	loraRadio.Reset()
    32  
    33  	if state := loraRadio.DetectDevice(); !state {
    34  		return nil, errRadioNotFound
    35  	}
    36  
    37  	return loraRadio, nil
    38  }
    39  
    40  func FirmwareVersion() string {
    41  	v := loraRadio.GetVersion()
    42  	return "sx127x v" + strconv.Itoa(int(v))
    43  }
    44  
    45  func Lorarx() ([]byte, error) {
    46  	return loraRadio.Rx(LORA_DEFAULT_RXTIMEOUT_MS)
    47  }