mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-09-09 00:09:02 +00:00
lorawan/sx12xx : Code cleanup
This commit is contained in:
committed by
Ron Evans
parent
d473b9e1dc
commit
02d808a6fb
+16
-18
@@ -44,6 +44,15 @@ func UseRadio(r lora.Radio) {
|
|||||||
ActiveRadio = r
|
ActiveRadio = r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 Join(otaa *Otaa, session *Session) error {
|
func Join(otaa *Otaa, session *Session) error {
|
||||||
var resp []uint8
|
var resp []uint8
|
||||||
|
|
||||||
@@ -64,25 +73,18 @@ func Join(otaa *Otaa, session *Session) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Prepare radio for Join Tx
|
// Prepare radio for Join Tx
|
||||||
joinChannel := regionSettings.GetJoinRequestChannel()
|
ApplyChannelConfig(regionSettings.JoinRequestChannel())
|
||||||
ActiveRadio.SetFrequency(joinChannel.Frequency)
|
|
||||||
ActiveRadio.SetBandwidth(joinChannel.Bandwidth)
|
|
||||||
ActiveRadio.SetCodingRate(joinChannel.CodingRate)
|
|
||||||
ActiveRadio.SetSpreadingFactor(joinChannel.SpreadingFactor)
|
|
||||||
ActiveRadio.SetCrc(true)
|
ActiveRadio.SetCrc(true)
|
||||||
ActiveRadio.SetIqMode(0) // IQ Standard
|
ActiveRadio.SetIqMode(lora.IQStandard)
|
||||||
ActiveRadio.Tx(payload, LORA_TX_TIMEOUT)
|
ActiveRadio.Tx(payload, LORA_TX_TIMEOUT)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for JoinAccept
|
// Wait for JoinAccept
|
||||||
joinChannel = regionSettings.GetJoinAcceptChannel()
|
ApplyChannelConfig(regionSettings.JoinAcceptChannel())
|
||||||
ActiveRadio.SetFrequency(joinChannel.Frequency)
|
ActiveRadio.SetCrc(true)
|
||||||
ActiveRadio.SetBandwidth(joinChannel.Bandwidth)
|
ActiveRadio.SetIqMode(lora.IQInverted)
|
||||||
ActiveRadio.SetCodingRate(joinChannel.CodingRate)
|
|
||||||
ActiveRadio.SetSpreadingFactor(joinChannel.SpreadingFactor)
|
|
||||||
ActiveRadio.SetIqMode(1) // IQ Inverted
|
|
||||||
for i := 0; i < Retries; i++ {
|
for i := 0; i < Retries; i++ {
|
||||||
resp, err = ActiveRadio.Rx(LORA_RX_TIMEOUT)
|
resp, err = ActiveRadio.Rx(LORA_RX_TIMEOUT)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -115,13 +117,9 @@ func SendUplink(data []uint8, session *Session) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
joinChannel := regionSettings.GetUplinkChannel()
|
ApplyChannelConfig(regionSettings.UplinkChannel())
|
||||||
ActiveRadio.SetFrequency(joinChannel.Frequency)
|
|
||||||
ActiveRadio.SetBandwidth(joinChannel.Bandwidth)
|
|
||||||
ActiveRadio.SetCodingRate(joinChannel.CodingRate)
|
|
||||||
ActiveRadio.SetSpreadingFactor(joinChannel.SpreadingFactor)
|
|
||||||
ActiveRadio.SetCrc(true)
|
ActiveRadio.SetCrc(true)
|
||||||
ActiveRadio.SetIqMode(0) // IQ Standard
|
ActiveRadio.SetIqMode(lora.IQInverted)
|
||||||
ActiveRadio.Tx(payload, LORA_TX_TIMEOUT)
|
ActiveRadio.Tx(payload, LORA_TX_TIMEOUT)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -2,21 +2,48 @@ package region
|
|||||||
|
|
||||||
import "tinygo.org/x/drivers/lora"
|
import "tinygo.org/x/drivers/lora"
|
||||||
|
|
||||||
|
const (
|
||||||
|
AU915_DEFAULT_PREAMBLE_LEN = 8
|
||||||
|
AU915_DEFAULT_TX_POWER_DBM = 20
|
||||||
|
)
|
||||||
|
|
||||||
type RegionSettingsAU915 struct {
|
type RegionSettingsAU915 struct {
|
||||||
|
joinRequestChannel *Channel
|
||||||
|
joinAcceptChannel *Channel
|
||||||
|
uplinkChannel *Channel
|
||||||
}
|
}
|
||||||
|
|
||||||
func AU915() *RegionSettingsAU915 {
|
func AU915() *RegionSettingsAU915 {
|
||||||
return &RegionSettingsAU915{}
|
return &RegionSettingsAU915{
|
||||||
|
joinRequestChannel: &Channel{916800000,
|
||||||
|
lora.Bandwidth_125_0,
|
||||||
|
lora.SpreadingFactor9,
|
||||||
|
lora.CodingRate4_7,
|
||||||
|
AU915_DEFAULT_PREAMBLE_LEN,
|
||||||
|
AU915_DEFAULT_TX_POWER_DBM},
|
||||||
|
joinAcceptChannel: &Channel{923300000,
|
||||||
|
lora.Bandwidth_500_0,
|
||||||
|
lora.SpreadingFactor9,
|
||||||
|
lora.CodingRate4_7,
|
||||||
|
AU915_DEFAULT_PREAMBLE_LEN,
|
||||||
|
AU915_DEFAULT_TX_POWER_DBM},
|
||||||
|
uplinkChannel: &Channel{868500000,
|
||||||
|
lora.Bandwidth_125_0,
|
||||||
|
lora.SpreadingFactor9,
|
||||||
|
lora.CodingRate4_7,
|
||||||
|
AU915_DEFAULT_PREAMBLE_LEN,
|
||||||
|
AU915_DEFAULT_TX_POWER_DBM},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RegionSettingsAU915) GetJoinRequestChannel() Channel {
|
func (r *RegionSettingsAU915) JoinRequestChannel() *Channel {
|
||||||
return Channel{916800000, lora.Bandwidth_125_0, lora.SpreadingFactor9, lora.CodingRate4_5}
|
return r.joinRequestChannel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RegionSettingsAU915) GetJoinAcceptChannel() Channel {
|
func (r *RegionSettingsAU915) JoinAcceptChannel() *Channel {
|
||||||
return Channel{923300000, lora.Bandwidth_500_0, lora.SpreadingFactor9, lora.CodingRate4_5}
|
return r.joinAcceptChannel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RegionSettingsAU915) GetUplinkChannel() Channel {
|
func (r *RegionSettingsAU915) UplinkChannel() *Channel {
|
||||||
return Channel{868500000, lora.Bandwidth_125_0, lora.SpreadingFactor9, lora.CodingRate4_5}
|
return r.uplinkChannel
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,21 +2,48 @@ package region
|
|||||||
|
|
||||||
import "tinygo.org/x/drivers/lora"
|
import "tinygo.org/x/drivers/lora"
|
||||||
|
|
||||||
|
const (
|
||||||
|
EU868_DEFAULT_PREAMBLE_LEN = 8
|
||||||
|
EU868_DEFAULT_TX_POWER_DBM = 20
|
||||||
|
)
|
||||||
|
|
||||||
type RegionSettingsEU868 struct {
|
type RegionSettingsEU868 struct {
|
||||||
|
joinRequestChannel *Channel
|
||||||
|
joinAcceptChannel *Channel
|
||||||
|
uplinkChannel *Channel
|
||||||
}
|
}
|
||||||
|
|
||||||
func EU868() *RegionSettingsEU868 {
|
func EU868() *RegionSettingsEU868 {
|
||||||
return &RegionSettingsEU868{}
|
return &RegionSettingsEU868{
|
||||||
|
joinRequestChannel: &Channel{868100000,
|
||||||
|
lora.Bandwidth_125_0,
|
||||||
|
lora.SpreadingFactor9,
|
||||||
|
lora.CodingRate4_7,
|
||||||
|
EU868_DEFAULT_PREAMBLE_LEN,
|
||||||
|
EU868_DEFAULT_TX_POWER_DBM},
|
||||||
|
joinAcceptChannel: &Channel{868100000,
|
||||||
|
lora.Bandwidth_125_0,
|
||||||
|
lora.SpreadingFactor9,
|
||||||
|
lora.CodingRate4_7,
|
||||||
|
EU868_DEFAULT_PREAMBLE_LEN,
|
||||||
|
EU868_DEFAULT_TX_POWER_DBM},
|
||||||
|
uplinkChannel: &Channel{868100000,
|
||||||
|
lora.Bandwidth_125_0,
|
||||||
|
lora.SpreadingFactor9,
|
||||||
|
lora.CodingRate4_7,
|
||||||
|
EU868_DEFAULT_PREAMBLE_LEN,
|
||||||
|
EU868_DEFAULT_TX_POWER_DBM},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RegionSettingsEU868) GetJoinRequestChannel() Channel {
|
func (r *RegionSettingsEU868) JoinRequestChannel() *Channel {
|
||||||
return Channel{868100000, lora.Bandwidth_125_0, lora.SpreadingFactor9, lora.CodingRate4_7}
|
return r.joinRequestChannel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RegionSettingsEU868) GetJoinAcceptChannel() Channel {
|
func (r *RegionSettingsEU868) JoinAcceptChannel() *Channel {
|
||||||
return Channel{868100000, lora.Bandwidth_125_0, lora.SpreadingFactor9, lora.CodingRate4_7}
|
return r.joinAcceptChannel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RegionSettingsEU868) GetUplinkChannel() Channel {
|
func (r *RegionSettingsEU868) UplinkChannel() *Channel {
|
||||||
return Channel{868100000, lora.Bandwidth_125_0, lora.SpreadingFactor9, lora.CodingRate4_7}
|
return r.uplinkChannel
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,10 +5,12 @@ type Channel struct {
|
|||||||
Bandwidth uint8
|
Bandwidth uint8
|
||||||
SpreadingFactor uint8
|
SpreadingFactor uint8
|
||||||
CodingRate uint8
|
CodingRate uint8
|
||||||
|
PreambleLength uint16
|
||||||
|
TxPowerDBm int8
|
||||||
}
|
}
|
||||||
|
|
||||||
type RegionSettings interface {
|
type RegionSettings interface {
|
||||||
GetJoinRequestChannel() Channel
|
JoinRequestChannel() *Channel
|
||||||
GetJoinAcceptChannel() Channel
|
JoinAcceptChannel() *Channel
|
||||||
GetUplinkChannel() Channel
|
UplinkChannel() *Channel
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,5 +10,7 @@ type Radio interface {
|
|||||||
SetBandwidth(bw uint8)
|
SetBandwidth(bw uint8)
|
||||||
SetCrc(enable bool)
|
SetCrc(enable bool)
|
||||||
SetSpreadingFactor(sf uint8)
|
SetSpreadingFactor(sf uint8)
|
||||||
|
SetPreambleLength(plen uint16)
|
||||||
|
SetTxPower(txpow int8)
|
||||||
LoraConfig(cnf Config)
|
LoraConfig(cnf Config)
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-1
@@ -558,12 +558,24 @@ func (d *Device) SetCrc(enable bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetSpreadingFactor setc surrent Lora Spreading Factor
|
// SetSpreadingFactor sets current Lora Spreading Factor
|
||||||
// NB: Change will be applied at next RX / TX
|
// NB: Change will be applied at next RX / TX
|
||||||
func (d *Device) SetSpreadingFactor(sf uint8) {
|
func (d *Device) SetSpreadingFactor(sf uint8) {
|
||||||
d.loraConf.Sf = sf
|
d.loraConf.Sf = sf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetPreambleLength sets current Lora Preamble Length
|
||||||
|
// NB: Change will be applied at next RX / TX
|
||||||
|
func (d *Device) SetPreambleLength(pl uint16) {
|
||||||
|
d.loraConf.Preamble = pl
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetTxPowerDbm sets current Lora TX Power in DBm
|
||||||
|
// NB: Change will be applied at next RX / TX
|
||||||
|
func (d *Device) SetTxPower(txpow int8) {
|
||||||
|
d.loraConf.LoraTxPowerDBm = txpow
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Lora functions
|
// Lora functions
|
||||||
//
|
//
|
||||||
|
|||||||
+12
-7
@@ -145,8 +145,13 @@ func (d *Device) GetBandwidth() int32 {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// SetTxPower sets the transmitter output power
|
// SetTxPower sets the transmitter output (with paBoost ON)
|
||||||
func (d *Device) SetTxPower(txPower int8, paBoost bool) {
|
func (d *Device) SetTxPower(txPower int8) {
|
||||||
|
d.SetTxPowerWithPaBoost(txPower, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetTxPowerWithPaBoost sets the transmitter output power and may activate paBoost
|
||||||
|
func (d *Device) SetTxPowerWithPaBoost(txPower int8, paBoost bool) {
|
||||||
if !paBoost {
|
if !paBoost {
|
||||||
// RFO
|
// RFO
|
||||||
if txPower < 0 {
|
if txPower < 0 {
|
||||||
@@ -315,7 +320,7 @@ func (d *Device) SetCrc(enable bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Device) SetPreamble(pLen uint16) {
|
func (d *Device) SetPreambleLength(pLen uint16) {
|
||||||
// Sets preamble length
|
// Sets preamble length
|
||||||
d.WriteRegister(SX127X_REG_PREAMBLE_MSB, uint8((pLen>>8)&0xFF))
|
d.WriteRegister(SX127X_REG_PREAMBLE_MSB, uint8((pLen>>8)&0xFF))
|
||||||
d.WriteRegister(SX127X_REG_PREAMBLE_LSB, uint8(pLen&0xFF))
|
d.WriteRegister(SX127X_REG_PREAMBLE_LSB, uint8(pLen&0xFF))
|
||||||
@@ -353,14 +358,14 @@ func (d *Device) Tx(pkt []uint8, timeoutMs uint32) error {
|
|||||||
d.WriteRegister(SX127X_REG_LNA, SX127X_LNA_MAX_GAIN) // Set Low Noise Amplifier to MAX
|
d.WriteRegister(SX127X_REG_LNA, SX127X_LNA_MAX_GAIN) // Set Low Noise Amplifier to MAX
|
||||||
|
|
||||||
d.SetFrequency(d.loraConf.Freq)
|
d.SetFrequency(d.loraConf.Freq)
|
||||||
d.SetPreamble(d.loraConf.Preamble)
|
d.SetPreambleLength(d.loraConf.Preamble)
|
||||||
d.SetSyncWord(d.loraConf.SyncWord)
|
d.SetSyncWord(d.loraConf.SyncWord)
|
||||||
d.SetBandwidth(d.loraConf.Bw)
|
d.SetBandwidth(d.loraConf.Bw)
|
||||||
d.SetSpreadingFactor(d.loraConf.Sf)
|
d.SetSpreadingFactor(d.loraConf.Sf)
|
||||||
d.SetIqMode(d.loraConf.Iq)
|
d.SetIqMode(d.loraConf.Iq)
|
||||||
d.SetCodingRate(d.loraConf.Cr)
|
d.SetCodingRate(d.loraConf.Cr)
|
||||||
d.SetCrc(d.loraConf.Crc == lora.CRCOn)
|
d.SetCrc(d.loraConf.Crc == lora.CRCOn)
|
||||||
d.SetTxPower(d.loraConf.LoraTxPowerDBm, true)
|
d.SetTxPower(d.loraConf.LoraTxPowerDBm)
|
||||||
d.SetHeaderMode(d.loraConf.HeaderType)
|
d.SetHeaderMode(d.loraConf.HeaderType)
|
||||||
d.SetAgcAuto(SX127X_AGC_AUTO_ON)
|
d.SetAgcAuto(SX127X_AGC_AUTO_ON)
|
||||||
|
|
||||||
@@ -409,14 +414,14 @@ func (d *Device) Rx(timeoutMs uint32) ([]uint8, error) {
|
|||||||
d.WriteRegister(SX127X_REG_LNA, SX127X_LNA_MAX_GAIN) // Set Low Noise Amplifier to MAX
|
d.WriteRegister(SX127X_REG_LNA, SX127X_LNA_MAX_GAIN) // Set Low Noise Amplifier to MAX
|
||||||
|
|
||||||
d.SetFrequency(d.loraConf.Freq)
|
d.SetFrequency(d.loraConf.Freq)
|
||||||
d.SetPreamble(d.loraConf.Preamble)
|
d.SetPreambleLength(d.loraConf.Preamble)
|
||||||
d.SetSyncWord(d.loraConf.SyncWord)
|
d.SetSyncWord(d.loraConf.SyncWord)
|
||||||
d.SetBandwidth(d.loraConf.Bw)
|
d.SetBandwidth(d.loraConf.Bw)
|
||||||
d.SetSpreadingFactor(d.loraConf.Sf)
|
d.SetSpreadingFactor(d.loraConf.Sf)
|
||||||
d.SetIqMode(d.loraConf.Iq)
|
d.SetIqMode(d.loraConf.Iq)
|
||||||
d.SetCodingRate(d.loraConf.Cr)
|
d.SetCodingRate(d.loraConf.Cr)
|
||||||
d.SetCrc(d.loraConf.Crc == lora.CRCOn)
|
d.SetCrc(d.loraConf.Crc == lora.CRCOn)
|
||||||
d.SetTxPower(d.loraConf.LoraTxPowerDBm, true)
|
d.SetTxPower(d.loraConf.LoraTxPowerDBm)
|
||||||
d.SetHeaderMode(d.loraConf.HeaderType)
|
d.SetHeaderMode(d.loraConf.HeaderType)
|
||||||
d.SetAgcAuto(SX127X_AGC_AUTO_ON)
|
d.SetAgcAuto(SX127X_AGC_AUTO_ON)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user