mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-10 09:53:42 +00:00
lorawan: Basic implementation of Lorawan Regional Settings and EU868/AU915 regions
This commit is contained in:
committed by
Ron Evans
parent
e6cde8f7ae
commit
78cc1afe46
+46
-13
@@ -4,20 +4,22 @@ import (
|
||||
"errors"
|
||||
|
||||
"tinygo.org/x/drivers/lora"
|
||||
"tinygo.org/x/drivers/lora/lorawan/region"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNoJoinAcceptReceived = errors.New("no JoinAccept packet received")
|
||||
ErrNoRadioAttached = errors.New("no LoRa radio attached")
|
||||
ErrInvalidEuiLength = errors.New("invalid EUI length")
|
||||
ErrInvalidAppKeyLength = errors.New("invalid AppKey length")
|
||||
ErrInvalidPacketLength = errors.New("invalid packet length")
|
||||
ErrInvalidDevAddrLength = errors.New("invalid DevAddr length")
|
||||
ErrInvalidMic = errors.New("invalid Mic")
|
||||
ErrFrmPayloadTooLarge = errors.New("FRM payload too large")
|
||||
ErrInvalidNetIDLength = errors.New("invalid NetID length")
|
||||
ErrInvalidNwkSKeyLength = errors.New("invalid NwkSKey length")
|
||||
ErrInvalidAppSKeyLength = errors.New("invalid AppSKey length")
|
||||
ErrNoJoinAcceptReceived = errors.New("no JoinAccept packet received")
|
||||
ErrNoRadioAttached = errors.New("no LoRa radio attached")
|
||||
ErrInvalidEuiLength = errors.New("invalid EUI length")
|
||||
ErrInvalidAppKeyLength = errors.New("invalid AppKey length")
|
||||
ErrInvalidPacketLength = errors.New("invalid packet length")
|
||||
ErrInvalidDevAddrLength = errors.New("invalid DevAddr length")
|
||||
ErrInvalidMic = errors.New("invalid Mic")
|
||||
ErrFrmPayloadTooLarge = errors.New("FRM payload too large")
|
||||
ErrInvalidNetIDLength = errors.New("invalid NetID length")
|
||||
ErrInvalidNwkSKeyLength = errors.New("invalid NwkSKey length")
|
||||
ErrInvalidAppSKeyLength = errors.New("invalid AppSKey length")
|
||||
ErrUndefinedRegionSettings = errors.New("undefined Regionnal Settings ")
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -26,10 +28,15 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
ActiveRadio lora.Radio
|
||||
Retries = 15
|
||||
ActiveRadio lora.Radio
|
||||
Retries = 15
|
||||
regionSettings region.RegionSettings
|
||||
)
|
||||
|
||||
func UseRegionSettings(rs region.RegionSettings) {
|
||||
regionSettings = rs
|
||||
}
|
||||
|
||||
func UseRadio(r lora.Radio) {
|
||||
if ActiveRadio != nil {
|
||||
panic("lorawan.ActiveRadio is already set")
|
||||
@@ -44,6 +51,10 @@ func Join(otaa *Otaa, session *Session) error {
|
||||
return ErrNoRadioAttached
|
||||
}
|
||||
|
||||
if regionSettings == nil {
|
||||
return ErrUndefinedRegionSettings
|
||||
}
|
||||
|
||||
otaa.Init()
|
||||
|
||||
// Send join packet
|
||||
@@ -52,6 +63,12 @@ func Join(otaa *Otaa, session *Session) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// Prepare radio for Join Tx
|
||||
joinChannel := regionSettings.GetJoinRequestChannel()
|
||||
ActiveRadio.SetFrequency(joinChannel.Frequency)
|
||||
ActiveRadio.SetBandwidth(joinChannel.Bandwidth)
|
||||
ActiveRadio.SetCodingRate(joinChannel.CodingRate)
|
||||
ActiveRadio.SetSpreadingFactor(joinChannel.SpreadingFactor)
|
||||
ActiveRadio.SetCrc(true)
|
||||
ActiveRadio.SetIqMode(0) // IQ Standard
|
||||
ActiveRadio.Tx(payload, LORA_TX_TIMEOUT)
|
||||
@@ -60,6 +77,11 @@ func Join(otaa *Otaa, session *Session) error {
|
||||
}
|
||||
|
||||
// Wait for JoinAccept
|
||||
joinChannel = regionSettings.GetJoinAcceptChannel()
|
||||
ActiveRadio.SetFrequency(joinChannel.Frequency)
|
||||
ActiveRadio.SetBandwidth(joinChannel.Bandwidth)
|
||||
ActiveRadio.SetCodingRate(joinChannel.CodingRate)
|
||||
ActiveRadio.SetSpreadingFactor(joinChannel.SpreadingFactor)
|
||||
ActiveRadio.SetIqMode(1) // IQ Inverted
|
||||
for i := 0; i < Retries; i++ {
|
||||
resp, err = ActiveRadio.Rx(LORA_RX_TIMEOUT)
|
||||
@@ -83,10 +105,21 @@ func Join(otaa *Otaa, session *Session) error {
|
||||
}
|
||||
|
||||
func SendUplink(data []uint8, session *Session) error {
|
||||
|
||||
if regionSettings == nil {
|
||||
return ErrUndefinedRegionSettings
|
||||
}
|
||||
|
||||
payload, err := session.GenMessage(0, []byte(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
joinChannel := regionSettings.GetUplinkChannel()
|
||||
ActiveRadio.SetFrequency(joinChannel.Frequency)
|
||||
ActiveRadio.SetBandwidth(joinChannel.Bandwidth)
|
||||
ActiveRadio.SetCodingRate(joinChannel.CodingRate)
|
||||
ActiveRadio.SetSpreadingFactor(joinChannel.SpreadingFactor)
|
||||
ActiveRadio.SetCrc(true)
|
||||
ActiveRadio.SetIqMode(0) // IQ Standard
|
||||
ActiveRadio.Tx(payload, LORA_TX_TIMEOUT)
|
||||
|
||||
@@ -20,7 +20,6 @@ type Otaa struct {
|
||||
// Initialize DevNonce
|
||||
func (o *Otaa) Init() {
|
||||
o.buf = make([]uint8, 0)
|
||||
|
||||
o.generateDevNonce()
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package region
|
||||
|
||||
import "tinygo.org/x/drivers/lora"
|
||||
|
||||
type RegionSettingsAU915 struct {
|
||||
}
|
||||
|
||||
func AU915() *RegionSettingsAU915 {
|
||||
return &RegionSettingsAU915{}
|
||||
}
|
||||
|
||||
func (r *RegionSettingsAU915) GetJoinRequestChannel() Channel {
|
||||
return Channel{916800000, lora.Bandwidth_125_0, lora.SpreadingFactor9, lora.CodingRate4_5}
|
||||
}
|
||||
|
||||
func (r *RegionSettingsAU915) GetJoinAcceptChannel() Channel {
|
||||
return Channel{923300000, lora.Bandwidth_500_0, lora.SpreadingFactor9, lora.CodingRate4_5}
|
||||
}
|
||||
|
||||
func (r *RegionSettingsAU915) GetUplinkChannel() Channel {
|
||||
return Channel{868500000, lora.Bandwidth_125_0, lora.SpreadingFactor9, lora.CodingRate4_5}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package region
|
||||
|
||||
import "tinygo.org/x/drivers/lora"
|
||||
|
||||
type RegionSettingsEU868 struct {
|
||||
}
|
||||
|
||||
func EU868() *RegionSettingsEU868 {
|
||||
return &RegionSettingsEU868{}
|
||||
}
|
||||
|
||||
func (r *RegionSettingsEU868) GetJoinRequestChannel() Channel {
|
||||
return Channel{868100000, lora.Bandwidth_125_0, lora.SpreadingFactor9, lora.CodingRate4_7}
|
||||
}
|
||||
|
||||
func (r *RegionSettingsEU868) GetJoinAcceptChannel() Channel {
|
||||
return Channel{868100000, lora.Bandwidth_125_0, lora.SpreadingFactor9, lora.CodingRate4_7}
|
||||
}
|
||||
|
||||
func (r *RegionSettingsEU868) GetUplinkChannel() Channel {
|
||||
return Channel{868100000, lora.Bandwidth_125_0, lora.SpreadingFactor9, lora.CodingRate4_7}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package region
|
||||
|
||||
type Channel struct {
|
||||
Frequency uint32
|
||||
Bandwidth uint8
|
||||
SpreadingFactor uint8
|
||||
CodingRate uint8
|
||||
}
|
||||
|
||||
type RegionSettings interface {
|
||||
GetJoinRequestChannel() Channel
|
||||
GetJoinAcceptChannel() Channel
|
||||
GetUplinkChannel() Channel
|
||||
}
|
||||
Reference in New Issue
Block a user