cleanup sx1262 example

This commit is contained in:
Joel Wetzell
2026-04-20 12:43:53 -05:00
parent 49a4731365
commit 81c26430c7
+15 -21
View File
@@ -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() radio.SetStandby(sx126x.STANDBY_RC)
if chipMode != sx126x.CHIP_MODE_STBY_RC { deviceError := radio.GetDeviceErrors()
radio.SetStandby(sx126x.STANDBY_RC) if deviceError&uint16(32) > 0 {
radio.ClearDeviceErrors()
} }
radio.ClearDeviceErrors() radio.SetDIO3AsTCXOCtrl(sx126x.TCXO_VOLTAGE_1_6V, 5000)
// Clear errors, disable radio interrupts for the moment 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
}