Example #1
0
        public void SetMode(RegOpModeMode mode)
        {
            byte regOpModeValue;

            regOpModeValue  = RegOpModeLongRangeModeLoRa;
            regOpModeValue |= RegOpModeAcessSharedRegLoRa;
            regOpModeValue |= RegOpModeLowFrequencyModeOnHighFrequency;
            regOpModeValue |= (byte)mode;
            Rfm9XLoraModem.WriteByte((byte)Registers.RegOpMode, regOpModeValue);
        }
        public void Receive(byte[] address)
        {
            Debug.Assert(address != null);
            Debug.Assert(address.Length >= AddressLengthMinimum);
            Debug.Assert(address.Length <= AddressLengthMaximum);
            DeviceAddress = address;

            RegOpModeModeDefault = RegOpModeMode.ReceiveContinuous;
            SetMode(RegOpModeMode.ReceiveContinuous);
        }
        public void SetMode(RegOpModeMode mode)
        {
            byte regOpModeValue;

            regOpModeValue  = RegOpModeLongRangeModeLoRa;
            regOpModeValue |= RegOpModeAcessSharedRegLoRa;
            if (Frequency > RFMidBandThreshold)
            {
                regOpModeValue |= RegOpModeLowFrequencyModeOnHighFrequency;
            }
            else
            {
                regOpModeValue |= RegOpModeLowFrequencyModeOnLowFrequency;
            }
            regOpModeValue |= (byte)mode;
            Rfm9XLoraModem.WriteByte((byte)Registers.RegOpMode, regOpModeValue);
        }
Example #4
0
        public void Initialise(RegOpModeMode regOpModeAfterInitialise,                                                                                                                                                                                                               // RegOpMode
                               double frequency                  = FrequencyDefault,                                                                                                                                                                                                 // RegFrMsb, RegFrMid, RegFrLsb
                               bool paBoost                      = false, byte maxPower = RegPAConfigMaxPowerDefault, byte outputPower = RegPAConfigOutputPowerDefault,                                                                                                              // RegPaConfig
                               bool ocpOn                        = true, byte ocpTrim   = RegOcpOcpTrimDefault,                                                                                                                                                                      // RegOcp
                               RegLnaLnaGain lnaGain             = LnaGainDefault, bool lnaBoostLF = false, bool lnaBoostHf = false,                                                                                                                                                 // RegLna
                               RegModemConfigBandwidth bandwidth = RegModemConfigBandwidthDefault, RegModemConfigCodingRate codingRate       = RegModemConfigCodingRateDefault, RegModemConfigImplicitHeaderModeOn implicitHeaderModeOn = RegModemConfigImplicitHeaderModeOnDefault, //RegModemConfig1
                               RegModemConfig2SpreadingFactor spreadingFactor = RegModemConfig2SpreadingFactorDefault, bool txContinuousMode = false, bool rxPayloadCrcOn = false,
                               ushort symbolTimeout     = SymbolTimeoutDefault,
                               ushort preambleLength    = PreambleLengthDefault,
                               byte payloadLength       = PayloadLengthDefault,
                               byte payloadMaxLength    = PayloadMaxLengthDefault,
                               byte freqHoppingPeriod   = FreqHoppingPeriodDefault,
                               bool lowDataRateOptimize = false, bool agcAutoOn = false,
                               byte ppmCorrection       = ppmCorrectionDefault,
                               RegDetectOptimizeDectionOptimize detectionOptimize = RegDetectOptimizeDectionOptimizeDefault,
                               bool invertIQ = false,
                               RegisterDetectionThreshold detectionThreshold = RegisterDetectionThresholdDefault,
                               byte syncWord = RegSyncWordDefault)
        {
            Frequency = frequency; // Store this away for RSSI adjustments
            RegOpModeAfterInitialise = regOpModeAfterInitialise;

            byte regVersionValue = Rfm9XLoraModem.ReadByte((byte)Registers.RegVersion);

            if (regVersionValue != RegVersionValueExpected)
            {
                throw new ApplicationException("Semtech SX127X not found");
            }

            ResetGpioPin.Write(false);
            Thread.Sleep(10);
            ResetGpioPin.Write(true);
            Thread.Sleep(10);

            // Put the device into sleep mode so registers can be changed
            SetMode(RegOpModeMode.Sleep);

            // Configure RF Carrier frequency
            if (frequency != FrequencyDefault)
            {
                byte[] bytes = BitConverter.GetBytes((long)(frequency / RH_RF95_FSTEP));
                Rfm9XLoraModem.WriteByte((byte)Registers.RegFrMsb, bytes[2]);
                Rfm9XLoraModem.WriteByte((byte)Registers.RegFrMid, bytes[1]);
                Rfm9XLoraModem.WriteByte((byte)Registers.RegFrLsb, bytes[0]);
            }

            // Set RegPaConfig if any of the associated settings are nto the default values
            if ((paBoost != false) || (maxPower != RegPAConfigMaxPowerDefault) || (outputPower != RegPAConfigOutputPowerDefault))
            {
                byte regPaConfigValue = maxPower;
                if (paBoost)
                {
                    regPaConfigValue |= RegPAConfigPASelectPABoost;
                }
                regPaConfigValue |= outputPower;
                Rfm9XLoraModem.WriteByte((byte)Registers.RegPAConfig, regPaConfigValue);
            }

            // Set RegOcp if any of the settings not defaults
            if ((ocpOn != true) || (ocpTrim != RegOcpOcpTrimDefault))
            {
                byte regOcpValue = ocpTrim;
                if (ocpOn)
                {
                    regOcpValue |= RegOcpOn;
                }
                Rfm9XLoraModem.WriteByte((byte)Registers.RegOcp, regOcpValue);
            }

            // Set RegLna if any of the settings not defaults
            if ((lnaGain != LnaGainDefault) || (lnaBoostLF != false) || (lnaBoostHf != false))
            {
                byte regLnaValue = (byte)lnaGain;
                if (lnaBoostLF)
                {
                    regLnaValue |= RegLnaLnaBoostLfOn;
                }
                if (lnaBoostHf)
                {
                    regLnaValue |= RegLnaLnaBoostHfOn;
                }
                Rfm9XLoraModem.WriteByte((byte)Registers.RegLna, regLnaValue);
            }

            // Set regModemConfig1 if any of the settings not defaults
            if ((bandwidth != RegModemConfigBandwidthDefault) || (codingRate != RegModemConfigCodingRateDefault) || (implicitHeaderModeOn != RegModemConfigImplicitHeaderModeOnDefault))
            {
                byte regModemConfig1Value = (byte)bandwidth;
                regModemConfig1Value |= (byte)codingRate;
                regModemConfig1Value |= (byte)implicitHeaderModeOn;
                Rfm9XLoraModem.WriteByte((byte)Registers.RegModemConfig1, regModemConfig1Value);
            }

            // Set regModemConfig2 if any of the settings not defaults
            if ((spreadingFactor != RegModemConfig2SpreadingFactorDefault) || (txContinuousMode != false) | (rxPayloadCrcOn != false) || (symbolTimeout != SymbolTimeoutDefault))
            {
                byte RegModemConfig2Value = (byte)spreadingFactor;
                if (txContinuousMode)
                {
                    RegModemConfig2Value |= RegModemConfig2TxContinuousModeOn;
                }
                if (rxPayloadCrcOn)
                {
                    RegModemConfig2Value |= RegModemConfig2RxPayloadCrcOn;
                }
                // Get the MSB of SymbolTimeout
                byte[] symbolTimeoutBytes = BitConverter.GetBytes(symbolTimeout);
                RegModemConfig2Value |= symbolTimeoutBytes[1];
                Rfm9XLoraModem.WriteByte((byte)Registers.RegModemConfig2, RegModemConfig2Value);
            }

            // RegModemConfig2.SymbTimout + RegSymbTimeoutLsb
            if (symbolTimeout != SymbolTimeoutDefault)
            {
                // Get the LSB of SymbolTimeout
                byte[] symbolTimeoutBytes = BitConverter.GetBytes(symbolTimeout);
                Rfm9XLoraModem.WriteByte((byte)Registers.RegSymbTimeout, symbolTimeoutBytes[0]);
            }

            // RegPreambleMsb + RegPreambleLsb
            if (preambleLength != PreambleLengthDefault)
            {
                byte[] premableBytes = BitConverter.GetBytes(preambleLength);
                Rfm9XLoraModem.WriteByte((byte)Registers.RegPreambleMsb, premableBytes[1]);
                Rfm9XLoraModem.WriteByte((byte)Registers.RegPreambleLsb, premableBytes[0]);
            }

            // RegPayloadLength
            if (payloadLength != PayloadLengthDefault)
            {
                Rfm9XLoraModem.WriteByte((byte)Registers.RegPayloadLength, payloadLength);
            }

            // RegMaxPayloadLength
            if (payloadMaxLength != PayloadMaxLengthDefault)
            {
                Rfm9XLoraModem.WriteByte((byte)Registers.RegMaxPayloadLength, payloadMaxLength);
            }

            // RegHopPeriod
            if (freqHoppingPeriod != FreqHoppingPeriodDefault)
            {
                Rfm9XLoraModem.WriteByte((byte)Registers.RegHopPeriod, freqHoppingPeriod);
            }

            // RegModemConfig3
            if ((lowDataRateOptimize != false) || (agcAutoOn != false))
            {
                byte regModemConfig3Value = 0;
                if (lowDataRateOptimize)
                {
                    regModemConfig3Value |= RegModemConfig3LowDataRateOptimizeOn;
                }
                if (agcAutoOn)
                {
                    regModemConfig3Value |= RegModemConfig3AgcAutoOn;
                }
                Rfm9XLoraModem.WriteByte((byte)Registers.RegModemConfig3, regModemConfig3Value);
            }

            // RegPpmCorrection
            if (ppmCorrection != ppmCorrectionDefault)
            {
                Rfm9XLoraModem.WriteByte((byte)Registers.RegPpmCorrection, ppmCorrection);
            }

            // RegDetectOptimize
            if (detectionOptimize != RegDetectOptimizeDectionOptimizeDefault)
            {
                Rfm9XLoraModem.WriteByte((byte)Registers.RegDetectOptimize, (byte)detectionOptimize);
            }

            // RegInvertIQ
            if (invertIQ != false)
            {
                Rfm9XLoraModem.WriteByte((byte)Registers.RegInvertIQ, (byte)InvertIqOn);
            }

            // RegDetectionThreshold
            if (detectionThreshold != RegisterDetectionThresholdDefault)
            {
                Rfm9XLoraModem.WriteByte((byte)Registers.RegDetectionThreshold, (byte)detectionThreshold);
            }

            // RegSyncWordDefault
            if (syncWord != RegSyncWordDefault)
            {
                Rfm9XLoraModem.WriteByte((byte)Registers.RegSyncWord, syncWord);
            }

            // TODO revist this split & move to onReceive function
            Rfm9XLoraModem.WriteByte((byte)Registers.RegDioMapping1, 0x00); // RegDioMapping1 0b00000000 DI0 RxReady & TxReady

            // Configure RegOpMode before returning
            SetMode(regOpModeAfterInitialise);
        }
 public void Receive()
 {
     RegOpModeModeDefault = RegOpModeMode.ReceiveContinuous;
     SetMode(RegOpModeMode.ReceiveContinuous);
 }