Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions examples/lora/lorawan/common/stm32wlx.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ var (
loraRadio *sx126x.Device
)

var spi = machine.SPI3
var (
spi = machine.SPI3
rstPin = machine.NoPin
)

func newRadioControl() sx126x.RadioController {
return sx126x.NewRadioControl()
}

// do sx126x setup here
func SetupLora() (lora.Radio, error) {
loraRadio = sx126x.New(spi)
loraRadio = sx126x.New(spi, rstPin)
loraRadio.SetDeviceType(sx126x.DEVICE_TYPE_SX1262)

// Create radio controller for target
Expand Down
4 changes: 3 additions & 1 deletion examples/sx126x/lora_continuous/lora_continuous.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ const FREQ = 868100000

var (
loraRadio *sx126x.Device
rstPin = machine.GP10
)

func main() {
println("\n# TinyGo Lora continuous Wave/Preamble test")
println("# -----------------------------------------")

machine.LED.Configure(machine.PinConfig{Mode: machine.PinOutput})
rstPin.Configure(machine.PinConfig{Mode: machine.PinOutput})

// Create the driver
loraRadio = sx126x.New(spi)
loraRadio = sx126x.New(spi, rstPin)
loraRadio.SetDeviceType(sx126x.DEVICE_TYPE_SX1262)

// Create radio controller for target
Expand Down
1 change: 1 addition & 0 deletions examples/sx126x/lora_continuous/radio.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var (
spi = machine.SPI0
nssPin, busyPin, dio1Pin = machine.GP17, machine.GP10, machine.GP11
rxPin, txLowPin, txHighPin = machine.GP13, machine.GP12, machine.GP12
rstPin = machine.GP10
)

func newRadioControl() sx126x.RadioController {
Expand Down
5 changes: 4 additions & 1 deletion examples/sx126x/lora_continuous/radio_stm32wl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (
"tinygo.org/x/drivers/sx126x"
)

var spi = machine.SPI3
var (
spi = machine.SPI3
rstPin = machine.NoPin
)

func newRadioControl() sx126x.RadioController {
return sx126x.NewRadioControl()
Expand Down
3 changes: 2 additions & 1 deletion examples/sx126x/lora_rxtx/lora_rxtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ func main() {
println("\n# TinyGo Lora RX/TX test")
println("# ----------------------")
machine.LED.Configure(machine.PinConfig{Mode: machine.PinOutput})
rstPin.Configure(machine.PinConfig{Mode: machine.PinOutput})

// Create the driver
loraRadio = sx126x.New(spi)
loraRadio = sx126x.New(spi, rstPin)
loraRadio.SetDeviceType(sx126x.DEVICE_TYPE_SX1262)

// Create radio controller for target
Expand Down
1 change: 1 addition & 0 deletions examples/sx126x/lora_rxtx/radio.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var (
spi = machine.SPI1
nssPin, busyPin, dio1Pin = machine.GP13, machine.GP6, machine.GP7
rxPin, txLowPin, txHighPin = machine.GP9, machine.GP8, machine.GP8
rstPin = machine.GP10
)

func newRadioControl() sx126x.RadioController {
Expand Down
5 changes: 4 additions & 1 deletion examples/sx126x/lora_rxtx/radio_stm32wl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (
"tinygo.org/x/drivers/sx126x"
)

var spi = machine.SPI3
var (
spi = machine.SPI3
rstPin = machine.NoPin
)

func newRadioControl() sx126x.RadioController {
return sx126x.NewRadioControl()
Expand Down
28 changes: 28 additions & 0 deletions sx126x/radiocontrol_wio_sx1262.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//go:build wio_sx1262

package sx126x

import (
"machine"
)

// NewRadioControlWio creates RadioControl configured for wio1262 module.
// The board has RF_SW pin which controls RX/TX, but providing it is not required
// if SetDio2AsRfSwitchCtrl is set to true (use machine.NoPin instead).
// Additionally, the board requires following settings on initalization:
/*
radio.SetDio3AsTcxoCtrl(sx126x.SX126X_DIO3_OUTPUT_1_8, 5*time.Microsecond)
radio.SetRegulatorMode(sx126x.SX126X_REGULATOR_DC_DC)
radio.SetDeviceType(sx126x.DEVICE_TYPE_SX1262)
radio.Calibrate(sx126x.SX126X_CALIBRATE_ALL)
*/
func NewRadioControlWio(nssPin, busyPin, dio1Pin, rfSw machine.Pin) *RadioControl {
return &RadioControl{
nssPin: nssPin,
busyPin: busyPin,
dio1Pin: dio1Pin,
rxPin: rfSw,
txLowPin: machine.NoPin,
txHighPin: machine.NoPin,
}
}
24 changes: 14 additions & 10 deletions sx126x/registers.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,6 @@ const (
SX126X_DIO2_AS_IRQ = 0x00 // 7 0 DIO2 configuration: IRQ
SX126X_DIO2_AS_RF_SWITCH = 0x01 // 7 0 RF switch control

//SX126X_CMD_SET_DIO3_AS_TCXO_CTRL
SX126X_DIO3_OUTPUT_1_6 = 0x00 // 7 0 DIO3 voltage output for TCXO: 1.6 V
SX126X_DIO3_OUTPUT_1_7 = 0x01 // 7 0 1.7 V
SX126X_DIO3_OUTPUT_1_8 = 0x02 // 7 0 1.8 V
SX126X_DIO3_OUTPUT_2_2 = 0x03 // 7 0 2.2 V
SX126X_DIO3_OUTPUT_2_4 = 0x04 // 7 0 2.4 V
SX126X_DIO3_OUTPUT_2_7 = 0x05 // 7 0 2.7 V
SX126X_DIO3_OUTPUT_3_0 = 0x06 // 7 0 3.0 V
SX126X_DIO3_OUTPUT_3_3 = 0x07 // 7 0 3.3 V

//SX126X_CMD_SET_PACKET_TYPE
SX126X_PACKET_TYPE_GFSK = 0x00 // 7 0 packet type: GFSK
SX126X_PACKET_TYPE_LORA = 0x01 // 7 0 LoRa
Expand Down Expand Up @@ -312,3 +302,17 @@ const (
SX126X_LORA_MAC_PUBLIC_SYNCWORD = 0x3444
SX126X_LORA_MAC_PRIVATE_SYNCWORD = 0x1424
)

type Dio3OutputVoltage uint8

const (
//SX126X_CMD_SET_DIO3_AS_TCXO_CTRL
SX126X_DIO3_OUTPUT_1_6 Dio3OutputVoltage = 0x00 // 7 0 DIO3 voltage output for TCXO: 1.6 V
SX126X_DIO3_OUTPUT_1_7 Dio3OutputVoltage = 0x01 // 7 0 1.7 V
SX126X_DIO3_OUTPUT_1_8 Dio3OutputVoltage = 0x02 // 7 0 1.8 V
SX126X_DIO3_OUTPUT_2_2 Dio3OutputVoltage = 0x03 // 7 0 2.2 V
SX126X_DIO3_OUTPUT_2_4 Dio3OutputVoltage = 0x04 // 7 0 2.4 V
SX126X_DIO3_OUTPUT_2_7 Dio3OutputVoltage = 0x05 // 7 0 2.7 V
SX126X_DIO3_OUTPUT_3_0 Dio3OutputVoltage = 0x06 // 7 0 3.0 V
SX126X_DIO3_OUTPUT_3_3 Dio3OutputVoltage = 0x07 // 7 0 3.3 V
)
30 changes: 29 additions & 1 deletion sx126x/sx126x.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ type Device struct {
}

// New creates a new SX126x connection.
func New(spi drivers.SPI) *Device {
func New(spi drivers.SPI, rstPin machine.Pin) *Device {
return &Device{
spi: spi,
rstPin: rstPin,
radioEventChan: make(chan lora.RadioEvent, RADIOEVENTCHAN_SIZE),
spiTxBuf: make([]byte, SPI_BUFFER_SIZE),
spiRxBuf: make([]byte, SPI_BUFFER_SIZE),
Expand Down Expand Up @@ -309,6 +310,33 @@ func (d *Device) SetDioIrqParams(irqMask, dio1Mask, dio2Mask, dio3Mask uint16) {
d.ExecSetCommand(SX126X_CMD_SET_DIO_IRQ_PARAMS, p[:])
}

// SetDio2AsRfSwitchCtrl configures if DIO2 is used to control an external RF switch.
// When controlling the external RX switch, the pin DIO2 will toggle according to
// the internal state machine (DIO2 = 0 in SLEEP, STDBY_RX, STDBY_XOSC, FS and RX modes,
// DIO2 = 1 in TX mode). Otherwise DIO2 is free to be used as an IRQ.
func (d *Device) SetDio2AsRfSwitchCtrl(enable bool) {
p := [1]uint8{SX126X_DIO2_AS_IRQ}
if enable {
p[0] = SX126X_DIO2_AS_RF_SWITCH
}
d.ExecSetCommand(SX126X_CMD_SET_DIO2_AS_RF_SWITCH_CTRL, p[:])
}

// SetDio3AsTcxoCtrl configures the DIO3 as an external TCXO voltage reference.
// After TCXO control is set, it is recommended to perform full calibration (CALIBRATE_ALL command).
// voltage: output voltage on DIO3 pin
// delay: time for the TCXO to stabilize
func (d *Device) SetDio3AsTcxoCtrl(voltage Dio3OutputVoltage, delay time.Duration) {
timeout := delay / (15625 * time.Nanosecond)
var p [5]uint8
p[0] = uint8(voltage)
p[1] = uint8((timeout >> 24) & 0xFF)
p[2] = uint8((timeout >> 16) & 0xFF)
p[3] = uint8((timeout >> 8) & 0xFF)
p[4] = uint8((timeout >> 0) & 0xFF)
d.ExecSetCommand(SX126X_CMD_SET_DIO3_AS_TCXO_CTRL, p[:])
}

// GetIrqStatus returns IRQ status
func (d *Device) GetIrqStatus() (irqStatus uint16) {
r := d.ExecGetCommand(SX126X_CMD_GET_IRQ_STATUS, 2)
Expand Down