cleanup sx1262 example
This commit is contained in:
+14
-20
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"machine"
|
"machine"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
@@ -57,7 +58,7 @@ func main() {
|
|||||||
var timeout atomic.Bool
|
var timeout atomic.Bool
|
||||||
timeout.Store(true)
|
timeout.Store(true)
|
||||||
|
|
||||||
dio1.Configure(machine.PinConfig{Mode: machine.PinInputPulldown})
|
dio1.Configure(machine.PinConfig{Mode: machine.PinInput})
|
||||||
dio1.SetInterrupt(machine.PinRising, func(machine.Pin) {
|
dio1.SetInterrupt(machine.PinRising, func(machine.Pin) {
|
||||||
irqStatus := radio.GetIrqStatus()
|
irqStatus := radio.GetIrqStatus()
|
||||||
println("irq status:", irqStatus)
|
println("irq status:", irqStatus)
|
||||||
@@ -108,7 +109,7 @@ func main() {
|
|||||||
// println("waiting 1s before transmitting...")
|
// println("waiting 1s before transmitting...")
|
||||||
// time.Sleep(1000 * time.Millisecond)
|
// time.Sleep(1000 * time.Millisecond)
|
||||||
// led.Set(true)
|
// led.Set(true)
|
||||||
// err := Tx(radio, loraConfig, []byte("hello world"))
|
// err := Tx(radio, loraConfig, []byte("hello world "+time.Now().Format("15:04:05")))
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
// println("failed to enter transmit:", err)
|
// println("failed to enter transmit:", err)
|
||||||
// }
|
// }
|
||||||
@@ -118,30 +119,29 @@ func main() {
|
|||||||
if rxDone.Load() {
|
if rxDone.Load() {
|
||||||
rxDone.Store(false)
|
rxDone.Store(false)
|
||||||
if !timeout.Load() {
|
if !timeout.Load() {
|
||||||
println("receive completed without timeout, reading buffer...")
|
|
||||||
rxLength, rxPointer := radio.GetRxBufferStatus()
|
rxLength, rxPointer := radio.GetRxBufferStatus()
|
||||||
println("rx length:", rxLength, "rx pointer:", rxPointer)
|
|
||||||
rxData := radio.ReadBuffer(rxPointer, rxLength)
|
rxData := radio.ReadBuffer(rxPointer, rxLength)
|
||||||
println("rx data:", string(rxData))
|
println("received data:", string(rxData))
|
||||||
led.Set(false)
|
led.Set(false)
|
||||||
} else {
|
} else {
|
||||||
println("receive timed out")
|
println("receive timed out")
|
||||||
|
println("starting Rx again...")
|
||||||
}
|
}
|
||||||
Rx(radio, loraConfig)
|
Rx(radio, loraConfig)
|
||||||
println("receive started")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetupLora(radio *sx126x.Device, config lora.Config) {
|
func SetupLora(radio *sx126x.Device, config lora.Config) {
|
||||||
// Switch to standby prior to configuration changes
|
// Switch to standby prior to configuration changes
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
chipMode, _ := radio.GetStatus()
|
|
||||||
if chipMode != sx126x.CHIP_MODE_STBY_RC {
|
|
||||||
radio.SetStandby(sx126x.STANDBY_RC)
|
radio.SetStandby(sx126x.STANDBY_RC)
|
||||||
}
|
deviceError := radio.GetDeviceErrors()
|
||||||
|
if deviceError&uint16(32) > 0 {
|
||||||
radio.ClearDeviceErrors()
|
radio.ClearDeviceErrors()
|
||||||
// Clear errors, disable radio interrupts for the moment
|
}
|
||||||
|
radio.SetDIO3AsTCXOCtrl(sx126x.TCXO_VOLTAGE_1_6V, 5000)
|
||||||
|
radio.SetDIO2AsRfSwitchCtrl(true)
|
||||||
radio.SetPacketType(sx126x.PACKET_TYPE_LORA)
|
radio.SetPacketType(sx126x.PACKET_TYPE_LORA)
|
||||||
radio.SetRfFrequency(config.Freq)
|
radio.SetRfFrequency(config.Freq)
|
||||||
radio.SetModulationParamsLoRa(spreadingFactor(config.Sf), bandwidth(config.Bw), codingRate(config.Cr), config.Ldr)
|
radio.SetModulationParamsLoRa(spreadingFactor(config.Sf), bandwidth(config.Bw), codingRate(config.Cr), config.Ldr)
|
||||||
@@ -150,11 +150,8 @@ func SetupLora(radio *sx126x.Device, config lora.Config) {
|
|||||||
var syncWord [2]uint8
|
var syncWord [2]uint8
|
||||||
syncWord[0] = uint8(config.SyncWord >> 8)
|
syncWord[0] = uint8(config.SyncWord >> 8)
|
||||||
syncWord[1] = uint8(config.SyncWord & 0x00FF)
|
syncWord[1] = uint8(config.SyncWord & 0x00FF)
|
||||||
|
|
||||||
radio.WriteRegister(sx126x.REG_LORA_SYNC_WORD_MSB, syncWord[:])
|
radio.WriteRegister(sx126x.REG_LORA_SYNC_WORD_MSB, syncWord[:])
|
||||||
radio.SetRxTxFallbackMode(sx126x.FALLBACK_MODE_STDBY_RC)
|
radio.SetRxTxFallbackMode(sx126x.FALLBACK_MODE_STDBY_RC)
|
||||||
radio.SetDIO2AsRfSwitchCtrl(true)
|
|
||||||
radio.SetDIO3AsTCXOCtrl(sx126x.TCXO_VOLTAGE_1_6V, 5000)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Tx(radio *sx126x.Device, loraConfig lora.Config, data []byte) error {
|
func Tx(radio *sx126x.Device, loraConfig lora.Config, data []byte) error {
|
||||||
@@ -162,7 +159,7 @@ func Tx(radio *sx126x.Device, loraConfig lora.Config, data []byte) error {
|
|||||||
radio.SetBufferBaseAddress(0, 0)
|
radio.SetBufferBaseAddress(0, 0)
|
||||||
radio.SetDioIrqParams(sx126x.IRQ_TX_DONE_MASK|sx126x.IRQ_TIMEOUT_MASK, sx126x.IRQ_TX_DONE_MASK|sx126x.IRQ_TIMEOUT_MASK, 0x00, 0x00)
|
radio.SetDioIrqParams(sx126x.IRQ_TX_DONE_MASK|sx126x.IRQ_TIMEOUT_MASK, sx126x.IRQ_TX_DONE_MASK|sx126x.IRQ_TIMEOUT_MASK, 0x00, 0x00)
|
||||||
if len(data) > 255 {
|
if len(data) > 255 {
|
||||||
return nil
|
return errors.New("data length exceeds maximum of 255 bytes")
|
||||||
}
|
}
|
||||||
radio.SetPacketParamsLoRa(loraConfig.Preamble, loraConfig.HeaderType, uint8(len(data)&0xFF), loraConfig.Crc, loraConfig.Iq)
|
radio.SetPacketParamsLoRa(loraConfig.Preamble, loraConfig.HeaderType, uint8(len(data)&0xFF), loraConfig.Crc, loraConfig.Iq)
|
||||||
radio.WriteBuffer(0, data)
|
radio.WriteBuffer(0, data)
|
||||||
@@ -173,11 +170,13 @@ func Tx(radio *sx126x.Device, loraConfig lora.Config, data []byte) error {
|
|||||||
|
|
||||||
func Rx(radio *sx126x.Device, loraConfig lora.Config) {
|
func Rx(radio *sx126x.Device, loraConfig lora.Config) {
|
||||||
radio.SetStandby(sx126x.STANDBY_RC)
|
radio.SetStandby(sx126x.STANDBY_RC)
|
||||||
|
radio.ClearDeviceErrors()
|
||||||
radio.ClearIrqStatus(sx126x.IRQ_ALL_MASK)
|
radio.ClearIrqStatus(sx126x.IRQ_ALL_MASK)
|
||||||
radio.SetBufferBaseAddress(0, 0)
|
radio.SetBufferBaseAddress(0, 0)
|
||||||
radio.SetRfFrequency(loraConfig.Freq)
|
radio.SetRfFrequency(loraConfig.Freq)
|
||||||
radio.SetPacketParamsLoRa(loraConfig.Preamble, loraConfig.HeaderType, 0xFF, loraConfig.Crc, loraConfig.Iq)
|
radio.SetPacketParamsLoRa(loraConfig.Preamble, loraConfig.HeaderType, 0xFF, loraConfig.Crc, loraConfig.Iq)
|
||||||
radio.SetDioIrqParams(sx126x.IRQ_RX_DONE_MASK|sx126x.IRQ_TIMEOUT_MASK, sx126x.IRQ_RX_DONE_MASK|sx126x.IRQ_TIMEOUT_MASK, 0x00, 0x00)
|
radio.SetDioIrqParams(sx126x.IRQ_RX_DONE_MASK|sx126x.IRQ_TIMEOUT_MASK, sx126x.IRQ_RX_DONE_MASK|sx126x.IRQ_TIMEOUT_MASK, 0x00, 0x00)
|
||||||
|
radio.SetStandby(sx126x.STANDBY_XOSC)
|
||||||
radio.SetRx(1000)
|
radio.SetRx(1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -245,8 +244,3 @@ func bandwidth(bw uint8) uint8 {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func timeoutMsToRtcSteps(timeoutMs uint32) uint32 {
|
|
||||||
r := uint32(timeoutMs * (64000 / 1000))
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user