diff --git a/examples/lora/lorawan/atcmd/commands.go b/examples/lora/lorawan/atcmd/commands.go index 1ebcdf9..0afede7 100644 --- a/examples/lora/lorawan/atcmd/commands.go +++ b/examples/lora/lorawan/atcmd/commands.go @@ -343,10 +343,8 @@ func lw(setting string) error { if param == "NET" { if val == "ON" { lorawan.SetPublicNetwork(true) - println("SET PUBLIC NET") } else { lorawan.SetPublicNetwork(false) - println("SET PRIVATE NET") } } } diff --git a/lora/config.go b/lora/config.go index 7dd40d1..84e77bb 100644 --- a/lora/config.go +++ b/lora/config.go @@ -82,4 +82,4 @@ const ( MHz_868_5 = 868500000 MHz_916_8 = 916800000 MHz_923_3 = 923300000 -) \ No newline at end of file +) diff --git a/sx127x/sx127x.go b/sx127x/sx127x.go index 982c124..9fe6897 100644 --- a/sx127x/sx127x.go +++ b/sx127x/sx127x.go @@ -346,14 +346,15 @@ func (d *Device) SetIqMode(val uint8) { d.WriteRegister(SX127X_REG_INVERTIQ2, 0x19) } } + // SetPublicNetwork changes Sync Word to match network type -func (d *Device) SetPublicNetwork(enabled bool) { +func (d *Device) SetPublicNetwork(enabled bool) { if enabled { d.SetSyncWord(SX127X_LORA_MAC_PUBLIC_SYNCWORD) } else { d.SetSyncWord(SX127X_LORA_MAC_PRIVATE_SYNCWORD) } - +} // Tx sends a lora packet, (with timeout) func (d *Device) Tx(pkt []uint8, timeoutMs uint32) error { @@ -374,7 +375,7 @@ func (d *Device) Tx(pkt []uint8, timeoutMs uint32) error { d.SetCodingRate(d.loraConf.Cr) d.SetCrc(d.loraConf.Crc == lora.CRCOn) d.SetTxPower(d.loraConf.LoraTxPowerDBm) - d.SetHeaderMode(d.loraConf.HeaderType) + d.SetHeaderType(d.loraConf.HeaderType) d.SetAgcAuto(SX127X_AGC_AUTO_ON) // set the IRQ mapping DIO0=TxDone DIO1=NOP DIO2=NOP @@ -430,21 +431,32 @@ func (d *Device) Rx(timeoutMs uint32) ([]uint8, error) { d.SetCodingRate(d.loraConf.Cr) d.SetCrc(d.loraConf.Crc == lora.CRCOn) d.SetTxPower(d.loraConf.LoraTxPowerDBm) - d.SetHeaderMode(d.loraConf.HeaderType) + d.SetHeaderType(d.loraConf.HeaderType) d.SetAgcAuto(SX127X_AGC_AUTO_ON) + //d.SetRxTimeout(200) + // set the IRQ mapping DIO0=RxDone DIO1=RxTimeout DIO2=NOP d.WriteRegister(SX127X_REG_DIO_MAPPING_1, SX127X_MAP_DIO0_LORA_RXDONE|SX127X_MAP_DIO1_LORA_RXTOUT|SX127X_MAP_DIO2_LORA_NOP) // Clear all radio IRQ Flags d.WriteRegister(SX127X_REG_IRQ_FLAGS, 0xFF) // Mask all but RxDone d.WriteRegister(SX127X_REG_IRQ_FLAGS_MASK, ^(SX127X_IRQ_LORA_RXDONE_MASK | SX127X_IRQ_LORA_RXTOUT_MASK)) - // Switch to RX Mode - d.SetOpMode(SX127X_OPMODE_RX_SINGLE) // - // Wait for Radio Event + // Get Radio Event Channel radioCh := d.GetRadioEventChan() + // Single RX mode don't properly handle Timeouts on sx127x, so we use Continuous RX + // Go routine is a workaround to stop the Continuous RX and fire a timeout Event + d.SetOpMode(SX127X_OPMODE_RX) + + go func() { + time.Sleep(time.Millisecond * time.Duration(timeoutMs)) + d.SetOpMode(SX127X_OPMODE_STANDBY) //Go standby + radioCh <- lora.NewRadioEvent(lora.RadioEventTimeout, 0, nil) + }() + + // Now the channel can receive either a Timeout or a RxDone: msg := <-radioCh if msg.EventType == lora.RadioEventTimeout { return nil, nil @@ -452,6 +464,7 @@ func (d *Device) Rx(timeoutMs uint32) ([]uint8, error) { return nil, errors.New("Unexpected Radio Event while RX " + string(0x30+msg.EventType)) } + // Get the received payload d.WriteRegister(SX127X_REG_FIFO_RX_BASE_ADDR, 0) d.WriteRegister(SX127X_REG_FIFO_ADDR_PTR, 0) @@ -546,4 +559,9 @@ func bandwidth(bw uint8) uint8 { } } +func syncword(sw int) uint16 { + if sw == lora.SyncPublic { + return SX127X_LORA_MAC_PUBLIC_SYNCWORD + } + return SX127X_LORA_MAC_PRIVATE_SYNCWORD }