Compare commits

...

7 Commits

Author SHA1 Message Date
deadprogram 38ae263d5a sx127x: use select for timeout
Signed-off-by: deadprogram <ron@hybridgroup.com>
2023-01-31 00:38:32 +01:00
Olivier Fauchon dc18724d17 remove debugs, go fmt 2023-01-30 23:49:20 +01:00
Olivier Fauchon e74b01f973 sx127x: fix RX timeout 2023-01-30 23:32:05 +01:00
Olivier Fauchon c78aa53820 go fmt 2023-01-30 22:39:45 +01:00
Olivier Fauchon 060a2dc99a sx127x: fix typos 2023-01-30 22:12:48 +01:00
Olivier Fauchon 72c48d2f59 fix bandwidth,tx power in lora/sx126x/lora_continuous example 2023-01-30 22:03:54 +01:00
Olivier Fauchon 818764fe6a lorawan cleanup and optimizations:
- Remove all default loraconf default initialisation in lorawan examples (Region Settings takes care of this now)
- Remove retries while receiving JoinAccept and increase LoraRX to 10s (no need to wait more than 10s)
- Add constants for RegionSetting default frequencies
- Standardize all lora default configurations in lora examples
- Add new AT+LW=NET,(ON|OFF) command in atcmd example
2023-01-30 21:50:21 +01:00
17 changed files with 107 additions and 109 deletions
+11
View File
@@ -39,3 +39,14 @@ Builds/flashes atcmd console application on Lora-E5 using onboard SX126x.
tinygo flash -target lorae5 ./examples/lora/lorawan/atcmd/
```
## Joining a Public Lorawan Network
```
AT+ID=DevEui,0101010101010101
AT+ID=AppEui,0123012301230213
AT+KEY=APPKEY,AEAEAEAEAEAEAEAAEAEAEAEAEAEAAEAE
AT+LW=NET,ON
AT+JOIN
```
AT+LW=NET,(ON|OFF) command changes Lora Sync Word to connect on public network(ON) or private networks(OFF)
+11
View File
@@ -337,6 +337,17 @@ func delay(setting string) error {
func lw(setting string) error {
cmd := "LW"
param, val, hasComma := strings.Cut(setting, ",")
if hasComma {
if param == "NET" {
if val == "ON" {
lorawan.SetPublicNetwork(true)
} else {
lorawan.SetPublicNetwork(false)
}
}
}
writeCommandOutput(cmd, setting)
return nil
@@ -2,10 +2,16 @@
package main
import (
"tinygo.org/x/drivers/lora/lorawan"
)
// These are sample keys, so the example builds
// Either change here, or create a new go file and use customkeys build tag
func setLorawanKeys() {
otaa.SetAppEUI([]uint8{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
otaa.SetDevEUI([]uint8{0xB3, 0xD5, 0x41, 0x00, 0x0A, 0xF1, 0xA4, 0x45})
otaa.SetAppKey([]uint8{0x12, 0x22, 0xA3, 0xFF, 0x0C, 0x7B, 0x76, 0x7B, 0x8F, 0xD3, 0x12, 0x4F, 0xCE, 0x7A, 0x32, 0x16})
lorawan.SetPublicNetwork(true)
}
+1 -17
View File
@@ -65,28 +65,12 @@ func main() {
session = &lorawan.Session{}
otaa = &lorawan.Otaa{}
// Initial Lora modulation configuration
loraConf := lora.Config{
Freq: 868100000,
Bw: lora.Bandwidth_125_0,
Sf: lora.SpreadingFactor9,
Cr: lora.CodingRate4_7,
HeaderType: lora.HeaderExplicit,
Preamble: 12,
Ldr: lora.LowDataRateOptimizeOff,
Iq: lora.IQStandard,
Crc: lora.CRCOn,
SyncWord: lora.SyncPublic,
LoraTxPowerDBm: 20,
}
radio.LoraConfig(loraConf)
// Connect the lorawan with the Lora Radio device.
lorawan.UseRadio(radio)
lorawan.UseRegionSettings(region.EU868())
// Configure AppEUI, DevEUI, APPKey
// Configure AppEUI, DevEUI, APPKey, and public/private Lorawan Network
setLorawanKeys()
if debug != "" {
-16
View File
@@ -37,22 +37,6 @@ func SetupLora() (lora.Radio, error) {
return nil, errRadioNotFound
}
loraConf := lora.Config{
Freq: FREQ,
Bw: lora.Bandwidth_500_0,
Sf: lora.SpreadingFactor9,
Cr: lora.CodingRate4_7,
HeaderType: lora.HeaderExplicit,
Preamble: 12,
Ldr: lora.LowDataRateOptimizeOff,
Iq: lora.IQStandard,
Crc: lora.CRCOn,
SyncWord: lora.SyncPrivate,
LoraTxPowerDBm: 20,
}
loraRadio.LoraConfig(loraConf)
return loraRadio, nil
}
-16
View File
@@ -41,22 +41,6 @@ func SetupLora() (lora.Radio, error) {
return nil, errRadioNotFound
}
loraConf := lora.Config{
Freq: FREQ,
Bw: lora.Bandwidth_500_0,
Sf: lora.SpreadingFactor9,
Cr: lora.CodingRate4_7,
HeaderType: lora.HeaderExplicit,
Preamble: 12,
Ldr: lora.LowDataRateOptimizeOff,
Iq: lora.IQStandard,
Crc: lora.CRCOn,
SyncWord: lora.SyncPrivate,
LoraTxPowerDBm: 20,
}
loraRadio.LoraConfig(loraConf)
return loraRadio, nil
}
-16
View File
@@ -34,22 +34,6 @@ func SetupLora() (lora.Radio, error) {
return nil, errRadioNotFound
}
// Prepare for Lora Operation
loraConf := lora.Config{
Freq: FREQ,
Bw: lora.Bandwidth_125_0,
Sf: lora.SpreadingFactor9,
Cr: lora.CodingRate4_7,
HeaderType: lora.HeaderExplicit,
Preamble: 12,
Iq: lora.IQStandard,
Crc: lora.CRCOn,
SyncWord: lora.SyncPublic,
LoraTxPowerDBm: 20,
}
loraRadio.LoraConfig(loraConf)
return loraRadio, nil
}
@@ -35,8 +35,8 @@ func main() {
// Prepare for Lora operation
loraConf := lora.Config{
Freq: FREQ,
Bw: lora.Bandwidth_500_0,
Freq: lora.MHz_868_1,
Bw: lora.Bandwidth_125_0,
Sf: lora.SpreadingFactor9,
Cr: lora.CodingRate4_7,
HeaderType: lora.HeaderExplicit,
@@ -45,7 +45,7 @@ func main() {
Iq: lora.IQStandard,
Crc: lora.CRCOn,
SyncWord: lora.SyncPrivate,
LoraTxPowerDBm: 14,
LoraTxPowerDBm: 20,
}
loraRadio.LoraConfig(loraConf)
+2 -4
View File
@@ -11,8 +11,6 @@ import (
"tinygo.org/x/drivers/sx126x"
)
const FREQ = 868100000
const (
LORA_DEFAULT_RXTIMEOUT_MS = 1000
LORA_DEFAULT_TXTIMEOUT_MS = 5000
@@ -44,8 +42,8 @@ func main() {
}
loraConf := lora.Config{
Freq: FREQ,
Bw: lora.Bandwidth_500_0,
Freq: lora.MHz_868_1,
Bw: lora.Bandwidth_125_0,
Sf: lora.SpreadingFactor9,
Cr: lora.CodingRate4_7,
HeaderType: lora.HeaderExplicit,
+2 -4
View File
@@ -11,8 +11,6 @@ import (
"tinygo.org/x/drivers/sx127x"
)
const FREQ = 868100000
const (
LORA_DEFAULT_RXTIMEOUT_MS = 1000
LORA_DEFAULT_TXTIMEOUT_MS = 5000
@@ -57,8 +55,8 @@ func main() {
// Prepare for Lora Operation
loraConf := lora.Config{
Freq: FREQ,
Bw: lora.Bandwidth_500_0,
Freq: lora.MHz_868_1,
Bw: lora.Bandwidth_125_0,
Sf: lora.SpreadingFactor9,
Cr: lora.CodingRate4_7,
HeaderType: lora.HeaderExplicit,
+7
View File
@@ -76,3 +76,10 @@ const (
SyncPublic = iota
SyncPrivate
)
const (
MHz_868_1 = 868100000
MHz_868_5 = 868500000
MHz_916_8 = 916800000
MHz_923_3 = 923300000
)
+22 -16
View File
@@ -24,7 +24,7 @@ var (
const (
LORA_TX_TIMEOUT = 2000
LORA_RX_TIMEOUT = 5000
LORA_RX_TIMEOUT = 10000
)
var (
@@ -33,10 +33,12 @@ var (
regionSettings region.RegionSettings
)
// UseRegionSettings sets current Lorawan Regional parameters
func UseRegionSettings(rs region.RegionSettings) {
regionSettings = rs
}
// UseRadio attaches Lora radio driver to Lorawan
func UseRadio(r lora.Radio) {
if ActiveRadio != nil {
panic("lorawan.ActiveRadio is already set")
@@ -44,15 +46,25 @@ func UseRadio(r lora.Radio) {
ActiveRadio = r
}
func ApplyChannelConfig(ch *region.Channel) {
// SetPublicNetwork defines Lora Sync Word according to network type (public/private)
func SetPublicNetwork(enabled bool) {
ActiveRadio.SetPublicNetwork(enabled)
}
// ApplyChannelConfig sets current Lora modulation according to current regional settings
func applyChannelConfig(ch *region.Channel) {
ActiveRadio.SetFrequency(ch.Frequency)
ActiveRadio.SetBandwidth(ch.Bandwidth)
ActiveRadio.SetCodingRate(ch.CodingRate)
ActiveRadio.SetSpreadingFactor(ch.SpreadingFactor)
ActiveRadio.SetPreambleLength(ch.PreambleLength)
ActiveRadio.SetTxPower(ch.TxPowerDBm)
// Lorawan defaults to explicit headers
ActiveRadio.SetHeaderType(lora.HeaderExplicit)
ActiveRadio.SetCrc(true)
}
// Join tries to connect Lorawan Gateway
func Join(otaa *Otaa, session *Session) error {
var resp []uint8
@@ -73,8 +85,7 @@ func Join(otaa *Otaa, session *Session) error {
}
// Prepare radio for Join Tx
ApplyChannelConfig(regionSettings.JoinRequestChannel())
ActiveRadio.SetCrc(true)
applyChannelConfig(regionSettings.JoinRequestChannel())
ActiveRadio.SetIqMode(lora.IQStandard)
ActiveRadio.Tx(payload, LORA_TX_TIMEOUT)
if err != nil {
@@ -82,18 +93,13 @@ func Join(otaa *Otaa, session *Session) error {
}
// Wait for JoinAccept
ApplyChannelConfig(regionSettings.JoinAcceptChannel())
ActiveRadio.SetCrc(true)
applyChannelConfig(regionSettings.JoinAcceptChannel())
ActiveRadio.SetIqMode(lora.IQInverted)
for i := 0; i < Retries; i++ {
resp, err = ActiveRadio.Rx(LORA_RX_TIMEOUT)
if err != nil {
return err
}
if resp != nil {
break
}
resp, err = ActiveRadio.Rx(LORA_RX_TIMEOUT)
if err != nil {
return err
}
if resp == nil {
return ErrNoJoinAcceptReceived
}
@@ -106,6 +112,7 @@ func Join(otaa *Otaa, session *Session) error {
return nil
}
// SendUplink sends Lorawan Uplink message
func SendUplink(data []uint8, session *Session) error {
if regionSettings == nil {
@@ -117,8 +124,7 @@ func SendUplink(data []uint8, session *Session) error {
return err
}
ApplyChannelConfig(regionSettings.UplinkChannel())
ActiveRadio.SetCrc(true)
applyChannelConfig(regionSettings.UplinkChannel())
ActiveRadio.SetIqMode(lora.IQStandard)
ActiveRadio.Tx(payload, LORA_TX_TIMEOUT)
if err != nil {
+3 -3
View File
@@ -15,19 +15,19 @@ type RegionSettingsAU915 struct {
func AU915() *RegionSettingsAU915 {
return &RegionSettingsAU915{
joinRequestChannel: &Channel{916800000,
joinRequestChannel: &Channel{lora.MHz_916_8,
lora.Bandwidth_125_0,
lora.SpreadingFactor9,
lora.CodingRate4_7,
AU915_DEFAULT_PREAMBLE_LEN,
AU915_DEFAULT_TX_POWER_DBM},
joinAcceptChannel: &Channel{923300000,
joinAcceptChannel: &Channel{lora.MHz_923_3,
lora.Bandwidth_500_0,
lora.SpreadingFactor9,
lora.CodingRate4_7,
AU915_DEFAULT_PREAMBLE_LEN,
AU915_DEFAULT_TX_POWER_DBM},
uplinkChannel: &Channel{868500000,
uplinkChannel: &Channel{lora.MHz_868_5,
lora.Bandwidth_125_0,
lora.SpreadingFactor9,
lora.CodingRate4_7,
+3 -3
View File
@@ -15,19 +15,19 @@ type RegionSettingsEU868 struct {
func EU868() *RegionSettingsEU868 {
return &RegionSettingsEU868{
joinRequestChannel: &Channel{868100000,
joinRequestChannel: &Channel{lora.MHz_868_1,
lora.Bandwidth_125_0,
lora.SpreadingFactor9,
lora.CodingRate4_7,
EU868_DEFAULT_PREAMBLE_LEN,
EU868_DEFAULT_TX_POWER_DBM},
joinAcceptChannel: &Channel{868100000,
joinAcceptChannel: &Channel{lora.MHz_868_1,
lora.Bandwidth_125_0,
lora.SpreadingFactor9,
lora.CodingRate4_7,
EU868_DEFAULT_PREAMBLE_LEN,
EU868_DEFAULT_TX_POWER_DBM},
uplinkChannel: &Channel{868100000,
uplinkChannel: &Channel{lora.MHz_868_1,
lora.Bandwidth_125_0,
lora.SpreadingFactor9,
lora.CodingRate4_7,
+3
View File
@@ -12,5 +12,8 @@ type Radio interface {
SetSpreadingFactor(sf uint8)
SetPreambleLength(plen uint16)
SetTxPower(txpow int8)
SetSyncWord(syncWord uint16)
SetPublicNetwork(enable bool)
SetHeaderType(headerType uint8)
LoraConfig(cnf Config)
}
+6
View File
@@ -576,6 +576,12 @@ func (d *Device) SetTxPower(txpow int8) {
d.loraConf.LoraTxPowerDBm = txpow
}
// SetHeaderType sets implicit or explicit header mode
// NB: Change will be applied at next RX / TX
func (d *Device) SetHeaderType(headerType uint8) {
d.loraConf.HeaderType = headerType
}
//
// Lora functions
//
+27 -11
View File
@@ -276,8 +276,8 @@ func (d *Device) SetCodingRate(cr uint8) {
d.WriteRegister(SX127X_REG_MODEM_CONFIG_1, (d.ReadRegister(SX127X_REG_MODEM_CONFIG_1)&0xf1)|(cr<<1))
}
// SetImplicitHeaderModeOn Enables implicit header mode ***
func (d *Device) SetHeaderMode(headerType uint8) {
// SetHeaderType set implicit or explicit mode
func (d *Device) SetHeaderType(headerType uint8) {
d.loraConf.HeaderType = headerType
if headerType == lora.HeaderImplicit {
d.WriteRegister(SX127X_REG_MODEM_CONFIG_1, d.ReadRegister(SX127X_REG_MODEM_CONFIG_1)|0x01)
@@ -347,6 +347,15 @@ func (d *Device) SetIqMode(val uint8) {
}
}
// SetPublicNetwork changes Sync Word to match network type
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 {
d.SetOpModeLora()
@@ -366,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
@@ -422,7 +431,7 @@ 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)
// set the IRQ mapping DIO0=RxDone DIO1=RxTimeout DIO2=NOP
@@ -431,19 +440,26 @@ func (d *Device) Rx(timeoutMs uint32) ([]uint8, error) {
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()
msg := <-radioCh
if msg.EventType == lora.RadioEventTimeout {
// 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)
var msg lora.RadioEvent
select {
case msg = <-radioCh:
if msg.EventType != lora.RadioEventRxDone {
return nil, errors.New("Unexpected Radio Event while RX " + string(0x30+msg.EventType))
}
case <-time.After(time.Millisecond * time.Duration(timeoutMs)):
d.SetOpMode(SX127X_OPMODE_STANDBY)
return nil, nil
} else if msg.EventType != lora.RadioEventRxDone {
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)