mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-16 12:53:23 +00:00
LoRa WAN US915 Support
Adds a driver for us 915 protocol to the lora WAN drivers.
This commit is contained in:
@@ -80,6 +80,9 @@ const (
|
|||||||
const (
|
const (
|
||||||
MHz_868_1 = 868100000
|
MHz_868_1 = 868100000
|
||||||
MHz_868_5 = 868500000
|
MHz_868_5 = 868500000
|
||||||
|
MHz_902_3 = 902300000
|
||||||
|
Mhz_903_0 = 903000000
|
||||||
|
MHZ_915_0 = 915000000
|
||||||
MHz_916_8 = 916800000
|
MHz_916_8 = 916800000
|
||||||
MHz_923_3 = 923300000
|
MHz_923_3 = 923300000
|
||||||
)
|
)
|
||||||
|
|||||||
+29
-23
@@ -52,13 +52,13 @@ func SetPublicNetwork(enabled bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ApplyChannelConfig sets current Lora modulation according to current regional settings
|
// ApplyChannelConfig sets current Lora modulation according to current regional settings
|
||||||
func applyChannelConfig(ch *region.Channel) {
|
func applyChannelConfig(ch region.Channel) {
|
||||||
ActiveRadio.SetFrequency(ch.Frequency)
|
ActiveRadio.SetFrequency(ch.Frequency())
|
||||||
ActiveRadio.SetBandwidth(ch.Bandwidth)
|
ActiveRadio.SetBandwidth(ch.Bandwidth())
|
||||||
ActiveRadio.SetCodingRate(ch.CodingRate)
|
ActiveRadio.SetCodingRate(ch.CodingRate())
|
||||||
ActiveRadio.SetSpreadingFactor(ch.SpreadingFactor)
|
ActiveRadio.SetSpreadingFactor(ch.SpreadingFactor())
|
||||||
ActiveRadio.SetPreambleLength(ch.PreambleLength)
|
ActiveRadio.SetPreambleLength(ch.PreambleLength())
|
||||||
ActiveRadio.SetTxPower(ch.TxPowerDBm)
|
ActiveRadio.SetTxPower(ch.TxPowerDBm())
|
||||||
// Lorawan defaults to explicit headers
|
// Lorawan defaults to explicit headers
|
||||||
ActiveRadio.SetHeaderType(lora.HeaderExplicit)
|
ActiveRadio.SetHeaderType(lora.HeaderExplicit)
|
||||||
ActiveRadio.SetCrc(true)
|
ActiveRadio.SetCrc(true)
|
||||||
@@ -84,24 +84,30 @@ func Join(otaa *Otaa, session *Session) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare radio for Join Tx
|
for {
|
||||||
applyChannelConfig(regionSettings.JoinRequestChannel())
|
joinRequestChannel := regionSettings.JoinRequestChannel()
|
||||||
ActiveRadio.SetIqMode(lora.IQStandard)
|
joinAcceptChannel := regionSettings.JoinAcceptChannel()
|
||||||
ActiveRadio.Tx(payload, LORA_TX_TIMEOUT)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait for JoinAccept
|
// Prepare radio for Join Tx
|
||||||
applyChannelConfig(regionSettings.JoinAcceptChannel())
|
applyChannelConfig(joinRequestChannel)
|
||||||
ActiveRadio.SetIqMode(lora.IQInverted)
|
ActiveRadio.SetIqMode(lora.IQStandard)
|
||||||
resp, err = ActiveRadio.Rx(LORA_RX_TIMEOUT)
|
ActiveRadio.Tx(payload, LORA_TX_TIMEOUT)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp == nil {
|
// Wait for JoinAccept
|
||||||
return ErrNoJoinAcceptReceived
|
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)
|
err = otaa.DecodeJoinAccept(resp, session)
|
||||||
|
|||||||
@@ -7,27 +7,48 @@ const (
|
|||||||
AU915_DEFAULT_TX_POWER_DBM = 20
|
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 {
|
type RegionSettingsAU915 struct {
|
||||||
joinRequestChannel *Channel
|
joinRequestChannel *ChannelAU
|
||||||
joinAcceptChannel *Channel
|
joinAcceptChannel *ChannelAU
|
||||||
uplinkChannel *Channel
|
uplinkChannel *ChannelAU
|
||||||
}
|
}
|
||||||
|
|
||||||
func AU915() *RegionSettingsAU915 {
|
func AU915() *RegionSettingsAU915 {
|
||||||
return &RegionSettingsAU915{
|
return &RegionSettingsAU915{
|
||||||
joinRequestChannel: &Channel{lora.MHz_916_8,
|
joinRequestChannel: &ChannelAU{lora.MHz_916_8,
|
||||||
lora.Bandwidth_125_0,
|
lora.Bandwidth_125_0,
|
||||||
lora.SpreadingFactor9,
|
lora.SpreadingFactor9,
|
||||||
lora.CodingRate4_5,
|
lora.CodingRate4_5,
|
||||||
AU915_DEFAULT_PREAMBLE_LEN,
|
AU915_DEFAULT_PREAMBLE_LEN,
|
||||||
AU915_DEFAULT_TX_POWER_DBM},
|
AU915_DEFAULT_TX_POWER_DBM},
|
||||||
joinAcceptChannel: &Channel{lora.MHz_923_3,
|
joinAcceptChannel: &ChannelAU{lora.MHz_923_3,
|
||||||
lora.Bandwidth_500_0,
|
lora.Bandwidth_500_0,
|
||||||
lora.SpreadingFactor9,
|
lora.SpreadingFactor9,
|
||||||
lora.CodingRate4_5,
|
lora.CodingRate4_5,
|
||||||
AU915_DEFAULT_PREAMBLE_LEN,
|
AU915_DEFAULT_PREAMBLE_LEN,
|
||||||
AU915_DEFAULT_TX_POWER_DBM},
|
AU915_DEFAULT_TX_POWER_DBM},
|
||||||
uplinkChannel: &Channel{lora.MHz_916_8,
|
uplinkChannel: &ChannelAU{lora.MHz_916_8,
|
||||||
lora.Bandwidth_125_0,
|
lora.Bandwidth_125_0,
|
||||||
lora.SpreadingFactor9,
|
lora.SpreadingFactor9,
|
||||||
lora.CodingRate4_5,
|
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
|
return r.joinRequestChannel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RegionSettingsAU915) JoinAcceptChannel() *Channel {
|
func (r *RegionSettingsAU915) JoinAcceptChannel() Channel {
|
||||||
return r.joinAcceptChannel
|
return r.joinAcceptChannel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RegionSettingsAU915) UplinkChannel() *Channel {
|
func (r *RegionSettingsAU915) UplinkChannel() Channel {
|
||||||
return r.uplinkChannel
|
return r.uplinkChannel
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,27 +7,48 @@ const (
|
|||||||
EU868_DEFAULT_TX_POWER_DBM = 20
|
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 {
|
type RegionSettingsEU868 struct {
|
||||||
joinRequestChannel *Channel
|
joinRequestChannel *ChannelEU
|
||||||
joinAcceptChannel *Channel
|
joinAcceptChannel *ChannelEU
|
||||||
uplinkChannel *Channel
|
uplinkChannel *ChannelEU
|
||||||
}
|
}
|
||||||
|
|
||||||
func EU868() *RegionSettingsEU868 {
|
func EU868() *RegionSettingsEU868 {
|
||||||
return &RegionSettingsEU868{
|
return &RegionSettingsEU868{
|
||||||
joinRequestChannel: &Channel{lora.MHz_868_1,
|
joinRequestChannel: &ChannelEU{lora.MHz_868_1,
|
||||||
lora.Bandwidth_125_0,
|
lora.Bandwidth_125_0,
|
||||||
lora.SpreadingFactor9,
|
lora.SpreadingFactor9,
|
||||||
lora.CodingRate4_7,
|
lora.CodingRate4_7,
|
||||||
EU868_DEFAULT_PREAMBLE_LEN,
|
EU868_DEFAULT_PREAMBLE_LEN,
|
||||||
EU868_DEFAULT_TX_POWER_DBM},
|
EU868_DEFAULT_TX_POWER_DBM},
|
||||||
joinAcceptChannel: &Channel{lora.MHz_868_1,
|
joinAcceptChannel: &ChannelEU{lora.MHz_868_1,
|
||||||
lora.Bandwidth_125_0,
|
lora.Bandwidth_125_0,
|
||||||
lora.SpreadingFactor9,
|
lora.SpreadingFactor9,
|
||||||
lora.CodingRate4_7,
|
lora.CodingRate4_7,
|
||||||
EU868_DEFAULT_PREAMBLE_LEN,
|
EU868_DEFAULT_PREAMBLE_LEN,
|
||||||
EU868_DEFAULT_TX_POWER_DBM},
|
EU868_DEFAULT_TX_POWER_DBM},
|
||||||
uplinkChannel: &Channel{lora.MHz_868_1,
|
uplinkChannel: &ChannelEU{lora.MHz_868_1,
|
||||||
lora.Bandwidth_125_0,
|
lora.Bandwidth_125_0,
|
||||||
lora.SpreadingFactor9,
|
lora.SpreadingFactor9,
|
||||||
lora.CodingRate4_7,
|
lora.CodingRate4_7,
|
||||||
@@ -36,14 +57,14 @@ func EU868() *RegionSettingsEU868 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RegionSettingsEU868) JoinRequestChannel() *Channel {
|
func (r *RegionSettingsEU868) JoinRequestChannel() Channel {
|
||||||
return r.joinRequestChannel
|
return r.joinRequestChannel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RegionSettingsEU868) JoinAcceptChannel() *Channel {
|
func (r *RegionSettingsEU868) JoinAcceptChannel() Channel {
|
||||||
return r.joinAcceptChannel
|
return r.joinAcceptChannel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RegionSettingsEU868) UplinkChannel() *Channel {
|
func (r *RegionSettingsEU868) UplinkChannel() Channel {
|
||||||
return r.uplinkChannel
|
return r.uplinkChannel
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,17 @@
|
|||||||
package region
|
package region
|
||||||
|
|
||||||
type Channel struct {
|
type RegionSettings interface {
|
||||||
Frequency uint32
|
JoinRequestChannel() Channel
|
||||||
Bandwidth uint8
|
JoinAcceptChannel() Channel
|
||||||
SpreadingFactor uint8
|
UplinkChannel() Channel
|
||||||
CodingRate uint8
|
|
||||||
PreambleLength uint16
|
|
||||||
TxPowerDBm int8
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type RegionSettings interface {
|
type Channel interface {
|
||||||
JoinRequestChannel() *Channel
|
Next() bool
|
||||||
JoinAcceptChannel() *Channel
|
Frequency() uint32
|
||||||
UplinkChannel() *Channel
|
Bandwidth() uint8
|
||||||
|
SpreadingFactor() uint8
|
||||||
|
CodingRate() uint8
|
||||||
|
PreambleLength() uint16
|
||||||
|
TxPowerDBm() int8
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user