예제 #1
0
        void InterruptPin_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            RegIrqFlags IrqFlags = (RegIrqFlags)this.Rfm9XLoraModem.ReadByte((byte)Registers.RegIrqFlags);

            //Debug.Print("RegIrqFlags " + ByteToHexString((byte)IrqFlags));

            if ((IrqFlags & RegIrqFlags.RxDone) == RegIrqFlags.RxDone) // RxDone
            {
                byte currentFifoAddress = this.Rfm9XLoraModem.ReadByte((byte)Registers.RegFifoRxCurrent);
                this.Rfm9XLoraModem.WriteByte((byte)Registers.RegFifoAddrPtr, currentFifoAddress); // RegFifoAddrPtr
                byte numberOfBytes = this.Rfm9XLoraModem.ReadByte((byte)Registers.RegRxNbBytes);   // RegRxNbBytes

                // Allocate buffer for message
                byte[] messageBytes = new byte[numberOfBytes];

                for (int i = 0; i < numberOfBytes; i++)
                {
                    messageBytes[i] = this.Rfm9XLoraModem.ReadByte(0x00); // RegFifo
                }

                OnDataReceived(messageBytes);
            }

            if ((IrqFlags & RegIrqFlags.TxDone) == RegIrqFlags.TxDone)
            {
                this.SetMode(RegOpModeAfterInitialise);
                OnTransmit();
            }

            this.Rfm9XLoraModem.WriteByte((byte)Registers.RegDioMapping1, 0x0);
            this.Rfm9XLoraModem.WriteByte((byte)Registers.RegIrqFlags, (byte)RegIrqFlags.ClearAll);
        }
        private void ProcessTxDone(RegIrqFlags IrqFlags)
        {
            Debug.Assert(IrqFlags != 0);
            this.SetMode(RegOpModeModeDefault);

            OnTransmit.Invoke();
        }
        void InterruptPin_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            RegIrqFlags irqFlags = (RegIrqFlags)this.Rfm9XLoraModem.ReadByte((byte)Registers.RegIrqFlags);

            if ((irqFlags & RegIrqFlags.RxDone) == RegIrqFlags.RxDone)
            {
                ProcessRxDone(irqFlags);
            }

            if ((irqFlags & RegIrqFlags.TxDone) == RegIrqFlags.TxDone)
            {
                ProcessTxDone(irqFlags);
            }

            this.Rfm9XLoraModem.WriteByte((byte)Registers.RegDioMapping1, (byte)RegDioMapping1.Dio0RxDone);
            this.Rfm9XLoraModem.WriteByte((byte)Registers.RegIrqFlags, (byte)RegIrqFlags.ClearAll);
        }
        private void ProcessRxDone(RegIrqFlags IrqFlags)
        {
            byte[] payloadBytes;
            Debug.Assert(IrqFlags != 0);

            // Check to see if payload has CRC
            if (RxDoneIgnoreIfCrcMissing)
            {
                byte regHopChannel = this.Rfm9XLoraModem.ReadByte((byte)Registers.RegHopChannel);
                if ((regHopChannel & (byte)RegHopChannelFlags.CrcOnPayload) != (byte)RegHopChannelFlags.CrcOnPayload)
                {
                    return;
                }
            }

            // Check to see if payload CRC is valid
            if (RxDoneIgnoreIfCrcInvalid)
            {
                if (((byte)IrqFlags & (byte)RegIrqFlagsMask.PayLoadCrcErrorMask) == (byte)RegIrqFlagsMask.PayLoadCrcErrorMask)
                {
                    return;
                }
            }

            // Extract the message from the RFM9X fifo, try and keep lock in place for the minimum possible time
            lock (Rfm9XRegFifoLock)
            {
                byte currentFifoAddress = this.Rfm9XLoraModem.ReadByte((byte)Registers.RegFifoRxCurrent);

                this.Rfm9XLoraModem.WriteByte((byte)Registers.RegFifoAddrPtr, currentFifoAddress);

                byte numberOfBytes = this.Rfm9XLoraModem.ReadByte((byte)Registers.RegRxNbBytes);

                // Allocate buffer for message
                payloadBytes = new byte[numberOfBytes];

                for (int i = 0; i < numberOfBytes; i++)
                {
                    payloadBytes[i] = this.Rfm9XLoraModem.ReadByte((byte)Registers.RegFifo);
                }
            }

#if ADDRESSED_MESSAGES_PAYLOAD
            //check that message is long enough to contain header
            if (payloadBytes.Length < (AddressHeaderLength + AddressLengthMinimum + AddressLengthMinimum))
            {
                return;
            }

            int toAddressLength   = (payloadBytes[0] >> 4) & 0xf;
            int fromAddressLength = payloadBytes[0] & 0xf;
            int messageLength     = payloadBytes.Length - (AddressHeaderLength + toAddressLength + fromAddressLength);

            // Make sure the to and local addresses are the same length
            if (toAddressLength != DeviceAddress.Length)
            {
                return;
            }

            // Compare the bytes of the addresses
            for (int index = 0; index < DeviceAddress.Length; index++)
            {
                if (DeviceAddress[index] != payloadBytes[index + AddressHeaderLength])
                {
                    return;
                }
            }
#endif

            // Get the RSSI HF vs. LF port adjustment section 5.5.5 RSSI and SNR in LoRa Mode
            float packetSnr = this.Rfm9XLoraModem.ReadByte((byte)Registers.RegPktSnrValue) * 0.25f;

            int rssi = this.Rfm9XLoraModem.ReadByte((byte)Registers.RegRssiValue);
            if (Frequency > RFMidBandThreshold)
            {
                rssi = RssiAdjustmentHF + rssi;
            }
            else
            {
                rssi = RssiAdjustmentLF + rssi;
            }

            int packetRssi = this.Rfm9XLoraModem.ReadByte((byte)Registers.RegPktRssiValue);
            if (Frequency > RFMidBandThreshold)
            {
                packetRssi = RssiAdjustmentHF + packetRssi;
            }
            else
            {
                packetRssi = RssiAdjustmentLF + packetRssi;
            }

#if ADDRESSED_MESSAGES_PAYLOAD
            byte[] address = new byte[fromAddressLength];
            Array.Copy(payloadBytes, AddressHeaderLength + toAddressLength, address, 0, fromAddressLength);
            byte[] messageBytes = new byte[messageLength];
            Array.Copy(payloadBytes, AddressHeaderLength + toAddressLength + fromAddressLength, messageBytes, 0, messageLength);
#endif

#if ADDRESSED_MESSAGES_PAYLOAD
            OnDataReceived.Invoke(address, packetSnr, packetRssi, rssi, messageBytes);
#else
            OnDataReceived.Invoke(packetSnr, packetRssi, rssi, payloadBytes);
#endif
        }