Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lora/lorawan: Further refactoring #611

Merged
merged 6 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/lora/lorawan/atcmd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ Builds/flashes atcmd console application with simulator instead of actual LoRa r
tinygo flash -target pico ./examples/lora/lorawan/atcmd/
```

## PyBadge with LoRa Featherwing
## PyBadge with LoRa Featherwing for EU868 region

Builds/flashes atcmd console application on PyBadge using LoRa Featherwing (RFM95/SX1276).

```
tinygo flash -target pybadge -tags featherwing ./examples/lora/lorawan/atcmd/
tinygo flash -target pybadge -tags featherwing -ldflags="-X main.reg=EU868" ./examples/lora/lorawan/atcmd/
```

## LoRa-E5
## LoRa-E5 for US915 region

Builds/flashes atcmd console application on Lora-E5 using onboard SX126x.

```
tinygo flash -target lorae5 ./examples/lora/lorawan/atcmd/
tinygo flash -target lorae5 -ldflags="-X main.reg=US915" ./examples/lora/lorawan/atcmd/
```

## Joining a Public Lorawan Network
Expand Down
13 changes: 12 additions & 1 deletion examples/lora/lorawan/atcmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ var (
defaultTimeout uint32 = 1000
)

var reg string

func main() {
uart.Configure(machine.UARTConfig{TX: tx, RX: rx})

Expand All @@ -45,7 +47,16 @@ func main() {
otaa = &lorawan.Otaa{}
lorawan.UseRadio(radio)

lorawan.UseRegionSettings(region.EU868())
switch reg {
case "AU915":
lorawan.UseRegionSettings(region.AU915())
case "EU868":
lorawan.UseRegionSettings(region.EU868())
case "US915":
lorawan.UseRegionSettings(region.US915())
default:
lorawan.UseRegionSettings(region.EU868())
}

for {
if uart.Buffered() > 0 {
Expand Down
8 changes: 4 additions & 4 deletions examples/lora/lorawan/basic-demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ loraConnect: Connected !
tinygo flash -target pico ./examples/lora/lorawan/basic-demo
```

## PyBadge with LoRa Featherwing
## PyBadge with LoRa Featherwing for EU868 region

```
tinygo flash -target pybadge -tags featherwing ./examples/lora/lorawan/basic-demo
tinygo flash -target pybadge -tags featherwing -ldflags="-X main.reg=EU868" ./examples/lora/lorawan/basic-demo
```

## LoRa-E5
## LoRa-E5 for US915 region

```
tinygo flash -target lorae5 ./examples/lora/lorawan/basic-demo
tinygo flash -target lorae5 -ldflags="-X main.reg=US915" ./examples/lora/lorawan/basic-demo
```


Expand Down
17 changes: 14 additions & 3 deletions examples/lora/lorawan/basic-demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import (
"tinygo.org/x/drivers/lora/lorawan/region"
)

var debug string
var (
reg string
debug string
)

const (
LORAWAN_JOIN_TIMEOUT_SEC = 180
Expand Down Expand Up @@ -67,8 +70,16 @@ func main() {

// Connect the lorawan with the Lora Radio device.
lorawan.UseRadio(radio)

lorawan.UseRegionSettings(region.EU868())
switch reg {
case "AU915":
lorawan.UseRegionSettings(region.AU915())
case "EU868":
lorawan.UseRegionSettings(region.EU868())
case "US915":
lorawan.UseRegionSettings(region.US915())
default:
lorawan.UseRegionSettings(region.EU868())
}

// Configure AppEUI, DevEUI, APPKey, and public/private Lorawan Network
setLorawanKeys()
Expand Down
19 changes: 12 additions & 7 deletions examples/lora/lorawan/common/sim.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ func (sr *SimLoraRadio) Rx(timeoutMs uint32) ([]uint8, error) {
return nil, nil
}

func (sr *SimLoraRadio) SetFrequency(freq uint32) {}
func (sr *SimLoraRadio) SetIqMode(mode uint8) {}
func (sr *SimLoraRadio) SetCodingRate(cr uint8) {}
func (sr *SimLoraRadio) SetBandwidth(bw uint8) {}
func (sr *SimLoraRadio) SetCrc(enable bool) {}
func (sr *SimLoraRadio) SetSpreadingFactor(sf uint8) {}
func (sr *SimLoraRadio) LoraConfig(cnf lora.Config) {}
func (sr *SimLoraRadio) SetFrequency(freq uint32) {}
func (sr *SimLoraRadio) SetIqMode(mode uint8) {}
func (sr *SimLoraRadio) SetCodingRate(cr uint8) {}
func (sr *SimLoraRadio) SetBandwidth(bw uint8) {}
func (sr *SimLoraRadio) SetCrc(enable bool) {}
func (sr *SimLoraRadio) SetSpreadingFactor(sf uint8) {}
func (sr *SimLoraRadio) SetHeaderType(headerType uint8) {}
func (sr *SimLoraRadio) SetPreambleLength(pLen uint16) {}
func (sr *SimLoraRadio) SetPublicNetwork(enabled bool) {}
func (sr *SimLoraRadio) SetSyncWord(syncWord uint16) {}
func (sr *SimLoraRadio) SetTxPower(txPower int8) {}
func (sr *SimLoraRadio) LoraConfig(cnf lora.Config) {}

func FirmwareVersion() string {
return "simulator " + CurrentVersion()
Expand Down
3 changes: 3 additions & 0 deletions lora/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ const (
const (
MHz_868_1 = 868100000
MHz_868_5 = 868500000
MHz_902_3 = 902300000
Mhz_903_0 = 903000000
MHZ_915_0 = 915000000
MHz_916_8 = 916800000
MHz_923_3 = 923300000
)
60 changes: 33 additions & 27 deletions lora/lorawan/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ const (
var (
ActiveRadio lora.Radio
Retries = 15
regionSettings region.RegionSettings
regionSettings region.Settings
)

// UseRegionSettings sets current Lorawan Regional parameters
func UseRegionSettings(rs region.RegionSettings) {
func UseRegionSettings(rs region.Settings) {
regionSettings = rs
}

Expand All @@ -52,13 +52,13 @@ func SetPublicNetwork(enabled bool) {
}

// 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)
func applyChannelConfig(ch region.Channel) {
deadprogram marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand All @@ -84,24 +84,30 @@ func Join(otaa *Otaa, session *Session) error {
return err
}

// Prepare radio for Join Tx
applyChannelConfig(regionSettings.JoinRequestChannel())
ActiveRadio.SetIqMode(lora.IQStandard)
ActiveRadio.Tx(payload, LORA_TX_TIMEOUT)
if err != nil {
return err
}

// Wait for JoinAccept
applyChannelConfig(regionSettings.JoinAcceptChannel())
ActiveRadio.SetIqMode(lora.IQInverted)
resp, err = ActiveRadio.Rx(LORA_RX_TIMEOUT)
if err != nil {
return err
}

if resp == nil {
return ErrNoJoinAcceptReceived
for {
joinRequestChannel := regionSettings.JoinRequestChannel()
joinAcceptChannel := regionSettings.JoinAcceptChannel()

// Prepare radio for Join Tx
applyChannelConfig(joinRequestChannel)
ActiveRadio.SetIqMode(lora.IQStandard)
ActiveRadio.Tx(payload, LORA_TX_TIMEOUT)
if err != nil {
return err
}

// Wait for JoinAccept
if joinAcceptChannel.Frequency() != 0 {
applyChannelConfig(joinAcceptChannel)
}
ActiveRadio.SetIqMode(lora.IQInverted)
resp, err = ActiveRadio.Rx(LORA_RX_TIMEOUT)
if err == nil && resp != nil {
break
}
if !joinAcceptChannel.Next() {
return ErrNoJoinAcceptReceived
}
}

err = otaa.DecodeJoinAccept(resp, session)
Expand Down
44 changes: 21 additions & 23 deletions lora/lorawan/region/au915.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,41 @@ const (
AU915_DEFAULT_TX_POWER_DBM = 20
)

type RegionSettingsAU915 struct {
joinRequestChannel *Channel
joinAcceptChannel *Channel
uplinkChannel *Channel
type ChannelAU struct {
channel
}

func AU915() *RegionSettingsAU915 {
return &RegionSettingsAU915{
joinRequestChannel: &Channel{lora.MHz_916_8,
func (c *ChannelAU) Next() bool {
return false
}

type SettingsAU915 struct {
settings
}

func AU915() *SettingsAU915 {
return &SettingsAU915{settings: settings{
joinRequestChannel: &ChannelAU{channel: channel{lora.MHz_916_8,
lora.Bandwidth_125_0,
lora.SpreadingFactor9,
lora.CodingRate4_5,
AU915_DEFAULT_PREAMBLE_LEN,
AU915_DEFAULT_TX_POWER_DBM},
joinAcceptChannel: &Channel{lora.MHz_923_3,
AU915_DEFAULT_TX_POWER_DBM}},
joinAcceptChannel: &ChannelAU{channel: channel{lora.MHz_923_3,
lora.Bandwidth_500_0,
lora.SpreadingFactor9,
lora.CodingRate4_5,
AU915_DEFAULT_PREAMBLE_LEN,
AU915_DEFAULT_TX_POWER_DBM},
uplinkChannel: &Channel{lora.MHz_916_8,
AU915_DEFAULT_TX_POWER_DBM}},
uplinkChannel: &ChannelAU{channel: channel{lora.MHz_916_8,
lora.Bandwidth_125_0,
lora.SpreadingFactor9,
lora.CodingRate4_5,
AU915_DEFAULT_PREAMBLE_LEN,
AU915_DEFAULT_TX_POWER_DBM},
}
}

func (r *RegionSettingsAU915) JoinRequestChannel() *Channel {
return r.joinRequestChannel
}

func (r *RegionSettingsAU915) JoinAcceptChannel() *Channel {
return r.joinAcceptChannel
AU915_DEFAULT_TX_POWER_DBM}},
}}
}

func (r *RegionSettingsAU915) UplinkChannel() *Channel {
return r.uplinkChannel
func Next(c *ChannelAU) bool {
return false
}
42 changes: 42 additions & 0 deletions lora/lorawan/region/channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package region

type Channel interface {
Next() bool
Frequency() uint32
Bandwidth() uint8
SpreadingFactor() uint8
CodingRate() uint8
PreambleLength() uint16
TxPowerDBm() int8
SetFrequency(v uint32)
SetBandwidth(v uint8)
SetSpreadingFactor(v uint8)
SetCodingRate(v uint8)
SetPreambleLength(v uint16)
SetTxPowerDBm(v int8)
}

type channel struct {
frequency uint32
bandwidth uint8
spreadingFactor uint8
codingRate uint8
preambleLength uint16
txPowerDBm int8
}

// Getter functions
func (c *channel) Frequency() uint32 { return c.frequency }
func (c *channel) Bandwidth() uint8 { return c.bandwidth }
func (c *channel) SpreadingFactor() uint8 { return c.spreadingFactor }
func (c *channel) CodingRate() uint8 { return c.codingRate }
func (c *channel) PreambleLength() uint16 { return c.preambleLength }
func (c *channel) TxPowerDBm() int8 { return c.txPowerDBm }

// Set functions
func (c *channel) SetFrequency(v uint32) { c.frequency = v }
func (c *channel) SetBandwidth(v uint8) { c.bandwidth = v }
func (c *channel) SetSpreadingFactor(v uint8) { c.spreadingFactor = v }
func (c *channel) SetCodingRate(v uint8) { c.codingRate = v }
func (c *channel) SetPreambleLength(v uint16) { c.preambleLength = v }
func (c *channel) SetTxPowerDBm(v int8) { c.txPowerDBm = v }
44 changes: 19 additions & 25 deletions lora/lorawan/region/eu868.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,37 @@ const (
EU868_DEFAULT_TX_POWER_DBM = 20
)

type RegionSettingsEU868 struct {
joinRequestChannel *Channel
joinAcceptChannel *Channel
uplinkChannel *Channel
type ChannelEU struct {
channel
}

func EU868() *RegionSettingsEU868 {
return &RegionSettingsEU868{
joinRequestChannel: &Channel{lora.MHz_868_1,
func (c *ChannelEU) Next() bool {
return false
}

type SettingsEU868 struct {
settings
}

func EU868() *SettingsEU868 {
return &SettingsEU868{settings: settings{
joinRequestChannel: &ChannelEU{channel: 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{lora.MHz_868_1,
EU868_DEFAULT_TX_POWER_DBM}},
joinAcceptChannel: &ChannelEU{channel: 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{lora.MHz_868_1,
EU868_DEFAULT_TX_POWER_DBM}},
uplinkChannel: &ChannelEU{channel: channel{lora.MHz_868_1,
lora.Bandwidth_125_0,
lora.SpreadingFactor9,
lora.CodingRate4_7,
EU868_DEFAULT_PREAMBLE_LEN,
EU868_DEFAULT_TX_POWER_DBM},
}
}

func (r *RegionSettingsEU868) JoinRequestChannel() *Channel {
return r.joinRequestChannel
}

func (r *RegionSettingsEU868) JoinAcceptChannel() *Channel {
return r.joinAcceptChannel
}

func (r *RegionSettingsEU868) UplinkChannel() *Channel {
return r.uplinkChannel
EU868_DEFAULT_TX_POWER_DBM}},
}}
}
Loading