cleanup sx1262 example

This commit is contained in:
Joel Wetzell
2026-04-20 12:43:53 -05:00
parent 49a4731365
commit 81c26430c7
+14 -20
View File
@@ -1,6 +1,7 @@
package main
import (
"errors"
"machine"
"sync/atomic"
"time"
@@ -57,7 +58,7 @@ func main() {
var timeout atomic.Bool
timeout.Store(true)
dio1.Configure(machine.PinConfig{Mode: machine.PinInputPulldown})
dio1.Configure(machine.PinConfig{Mode: machine.PinInput})
dio1.SetInterrupt(machine.PinRising, func(machine.Pin) {
irqStatus := radio.GetIrqStatus()
println("irq status:", irqStatus)
@@ -108,7 +109,7 @@ func main() {
// println("waiting 1s before transmitting...")
// time.Sleep(1000 * time.Millisecond)
// 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 {
// println("failed to enter transmit:", err)
// }
@@ -118,30 +119,29 @@ func main() {
if rxDone.Load() {
rxDone.Store(false)
if !timeout.Load() {
println("receive completed without timeout, reading buffer...")
rxLength, rxPointer := radio.GetRxBufferStatus()
println("rx length:", rxLength, "rx pointer:", rxPointer)
rxData := radio.ReadBuffer(rxPointer, rxLength)
println("rx data:", string(rxData))
println("received data:", string(rxData))
led.Set(false)
} else {
println("receive timed out")
println("starting Rx again...")
}
Rx(radio, loraConfig)
println("receive started")
}
}
}
func SetupLora(radio *sx126x.Device, config lora.Config) {
// Switch to standby prior to configuration changes
chipMode, _ := radio.GetStatus()
if chipMode != sx126x.CHIP_MODE_STBY_RC {
time.Sleep(1 * time.Second)
radio.SetStandby(sx126x.STANDBY_RC)
}
deviceError := radio.GetDeviceErrors()
if deviceError&uint16(32) > 0 {
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.SetRfFrequency(config.Freq)
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
syncWord[0] = uint8(config.SyncWord >> 8)
syncWord[1] = uint8(config.SyncWord & 0x00FF)
radio.WriteRegister(sx126x.REG_LORA_SYNC_WORD_MSB, syncWord[:])
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 {
@@ -162,7 +159,7 @@ func Tx(radio *sx126x.Device, loraConfig lora.Config, data []byte) error {
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)
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.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) {
radio.SetStandby(sx126x.STANDBY_RC)
radio.ClearDeviceErrors()
radio.ClearIrqStatus(sx126x.IRQ_ALL_MASK)
radio.SetBufferBaseAddress(0, 0)
radio.SetRfFrequency(loraConfig.Freq)
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.SetStandby(sx126x.STANDBY_XOSC)
radio.SetRx(1000)
}
@@ -245,8 +244,3 @@ func bandwidth(bw uint8) uint8 {
return 0
}
}
func timeoutMsToRtcSteps(timeoutMs uint32) uint32 {
r := uint32(timeoutMs * (64000 / 1000))
return r
}