LoRa WAN US915 Support

Adds a driver for us 915 protocol to the lora WAN drivers.
This commit is contained in:
Mirac Kara
2023-09-26 17:02:18 -07:00
committed by Ron Evans
parent 4edb58c0c5
commit e95cfe66c1
6 changed files with 218 additions and 52 deletions
+3
View File
@@ -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
)
+29 -23
View File
@@ -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) {
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)
@@ -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
}
for {
joinRequestChannel := regionSettings.JoinRequestChannel()
joinAcceptChannel := regionSettings.JoinAcceptChannel()
// Wait for JoinAccept
applyChannelConfig(regionSettings.JoinAcceptChannel())
ActiveRadio.SetIqMode(lora.IQInverted)
resp, err = ActiveRadio.Rx(LORA_RX_TIMEOUT)
if err != nil {
return err
}
// Prepare radio for Join Tx
applyChannelConfig(joinRequestChannel)
ActiveRadio.SetIqMode(lora.IQStandard)
ActiveRadio.Tx(payload, LORA_TX_TIMEOUT)
if err != nil {
return err
}
if resp == nil {
return ErrNoJoinAcceptReceived
// 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)
+34 -9
View File
@@ -7,27 +7,48 @@ const (
AU915_DEFAULT_TX_POWER_DBM = 20
)
type ChannelAU struct {
frequency uint32
bandwidth uint8
spreadingFactor uint8
codingRate uint8
preambleLength uint16
txPowerDBm int8
}
// Getter functions
func (c *ChannelAU) Frequency() uint32 { return c.frequency }
func (c *ChannelAU) Bandwidth() uint8 { return c.bandwidth }
func (c *ChannelAU) SpreadingFactor() uint8 { return c.spreadingFactor }
func (c *ChannelAU) CodingRate() uint8 { return c.codingRate }
func (c *ChannelAU) PreambleLength() uint16 { return c.preambleLength }
func (c *ChannelAU) TxPowerDBm() int8 { return c.txPowerDBm }
func (c *ChannelAU) Next() bool {
return false
}
type RegionSettingsAU915 struct {
joinRequestChannel *Channel
joinAcceptChannel *Channel
uplinkChannel *Channel
joinRequestChannel *ChannelAU
joinAcceptChannel *ChannelAU
uplinkChannel *ChannelAU
}
func AU915() *RegionSettingsAU915 {
return &RegionSettingsAU915{
joinRequestChannel: &Channel{lora.MHz_916_8,
joinRequestChannel: &ChannelAU{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,
joinAcceptChannel: &ChannelAU{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,
uplinkChannel: &ChannelAU{lora.MHz_916_8,
lora.Bandwidth_125_0,
lora.SpreadingFactor9,
lora.CodingRate4_5,
@@ -36,14 +57,18 @@ func AU915() *RegionSettingsAU915 {
}
}
func (r *RegionSettingsAU915) JoinRequestChannel() *Channel {
func Next(c *ChannelAU) bool {
return false
}
func (r *RegionSettingsAU915) JoinRequestChannel() Channel {
return r.joinRequestChannel
}
func (r *RegionSettingsAU915) JoinAcceptChannel() *Channel {
func (r *RegionSettingsAU915) JoinAcceptChannel() Channel {
return r.joinAcceptChannel
}
func (r *RegionSettingsAU915) UplinkChannel() *Channel {
func (r *RegionSettingsAU915) UplinkChannel() Channel {
return r.uplinkChannel
}
+30 -9
View File
@@ -7,27 +7,48 @@ const (
EU868_DEFAULT_TX_POWER_DBM = 20
)
type ChannelEU struct {
frequency uint32
bandwidth uint8
spreadingFactor uint8
codingRate uint8
preambleLength uint16
txPowerDBm int8
}
// Getter functions
func (c *ChannelEU) Frequency() uint32 { return c.frequency }
func (c *ChannelEU) Bandwidth() uint8 { return c.bandwidth }
func (c *ChannelEU) SpreadingFactor() uint8 { return c.spreadingFactor }
func (c *ChannelEU) CodingRate() uint8 { return c.codingRate }
func (c *ChannelEU) PreambleLength() uint16 { return c.preambleLength }
func (c *ChannelEU) TxPowerDBm() int8 { return c.txPowerDBm }
func (c *ChannelEU) Next() bool {
return false
}
type RegionSettingsEU868 struct {
joinRequestChannel *Channel
joinAcceptChannel *Channel
uplinkChannel *Channel
joinRequestChannel *ChannelEU
joinAcceptChannel *ChannelEU
uplinkChannel *ChannelEU
}
func EU868() *RegionSettingsEU868 {
return &RegionSettingsEU868{
joinRequestChannel: &Channel{lora.MHz_868_1,
joinRequestChannel: &ChannelEU{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,
joinAcceptChannel: &ChannelEU{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,
uplinkChannel: &ChannelEU{lora.MHz_868_1,
lora.Bandwidth_125_0,
lora.SpreadingFactor9,
lora.CodingRate4_7,
@@ -36,14 +57,14 @@ func EU868() *RegionSettingsEU868 {
}
}
func (r *RegionSettingsEU868) JoinRequestChannel() *Channel {
func (r *RegionSettingsEU868) JoinRequestChannel() Channel {
return r.joinRequestChannel
}
func (r *RegionSettingsEU868) JoinAcceptChannel() *Channel {
func (r *RegionSettingsEU868) JoinAcceptChannel() Channel {
return r.joinAcceptChannel
}
func (r *RegionSettingsEU868) UplinkChannel() *Channel {
func (r *RegionSettingsEU868) UplinkChannel() Channel {
return r.uplinkChannel
}
+12 -11
View File
@@ -1,16 +1,17 @@
package region
type Channel struct {
Frequency uint32
Bandwidth uint8
SpreadingFactor uint8
CodingRate uint8
PreambleLength uint16
TxPowerDBm int8
type RegionSettings interface {
JoinRequestChannel() Channel
JoinAcceptChannel() Channel
UplinkChannel() Channel
}
type RegionSettings interface {
JoinRequestChannel() *Channel
JoinAcceptChannel() *Channel
UplinkChannel() *Channel
type Channel interface {
Next() bool
Frequency() uint32
Bandwidth() uint8
SpreadingFactor() uint8
CodingRate() uint8
PreambleLength() uint16
TxPowerDBm() int8
}
+110
View File
@@ -0,0 +1,110 @@
package region
import "tinygo.org/x/drivers/lora"
const (
US915_DEFAULT_PREAMBLE_LEN = 8
US915_DEFAULT_TX_POWER_DBM = 20
US915_FREQUENCY_INCREMENT_DR_0 = 200000 // only for 125 kHz Bandwidth
US915_FREQUENCY_INCREMENT_DR_4 = 1600000 // only for 500 kHz Bandwidth
)
type ChannelUS struct {
frequency uint32
bandwidth uint8
spreadingFactor uint8
codingRate uint8
preambleLength uint16
txPowerDBm int8
}
// Getter functions
func (c *ChannelUS) Frequency() uint32 { return c.frequency }
func (c *ChannelUS) Bandwidth() uint8 { return c.bandwidth }
func (c *ChannelUS) SpreadingFactor() uint8 { return c.spreadingFactor }
func (c *ChannelUS) CodingRate() uint8 { return c.codingRate }
func (c *ChannelUS) PreambleLength() uint16 { return c.preambleLength }
func (c *ChannelUS) TxPowerDBm() int8 { return c.txPowerDBm }
func (c *ChannelUS) Next() bool {
switch c.Bandwidth() {
case lora.Bandwidth_125_0:
freq, ok := stepFrequency125(c.frequency)
if ok {
c.frequency = freq
return true
} else {
c.frequency = lora.Mhz_903_0
return true
}
case lora.Bandwidth_500_0:
freq, ok := stepFrequency500(c.frequency)
if ok {
c.frequency = freq
return true
} else {
return false
}
}
return false
}
func stepFrequency125(freq uint32) (uint32, bool) {
f := freq + US915_FREQUENCY_INCREMENT_DR_0
if f >= lora.MHZ_915_0 {
return 0, false
}
return f, true
}
func stepFrequency500(freq uint32) (uint32, bool) {
f := freq + US915_FREQUENCY_INCREMENT_DR_4
if f >= lora.MHZ_915_0 {
return 0, false
}
return f, true
}
type RegionSettingsUS915 struct {
joinRequestChannel *ChannelUS
joinAcceptChannel *ChannelUS
uplinkChannel *ChannelUS
}
func US915() *RegionSettingsUS915 {
return &RegionSettingsUS915{
joinRequestChannel: &ChannelUS{lora.MHz_902_3,
lora.Bandwidth_125_0,
lora.SpreadingFactor10,
lora.CodingRate4_5,
US915_DEFAULT_PREAMBLE_LEN,
US915_DEFAULT_TX_POWER_DBM},
joinAcceptChannel: &ChannelUS{0,
lora.Bandwidth_500_0,
lora.SpreadingFactor9,
lora.CodingRate4_5,
US915_DEFAULT_PREAMBLE_LEN,
US915_DEFAULT_TX_POWER_DBM},
uplinkChannel: &ChannelUS{lora.Mhz_903_0,
lora.Bandwidth_500_0,
lora.SpreadingFactor9,
lora.CodingRate4_5,
US915_DEFAULT_PREAMBLE_LEN,
US915_DEFAULT_TX_POWER_DBM},
}
}
func (r *RegionSettingsUS915) JoinRequestChannel() Channel {
return r.joinRequestChannel
}
func (r *RegionSettingsUS915) JoinAcceptChannel() Channel {
return r.joinAcceptChannel
}
func (r *RegionSettingsUS915) UplinkChannel() Channel {
return r.uplinkChannel
}