Receive Basic
For testing this code I used the same version of the LoRaSetSyncWord example as Transmit Basic
20:57:40.239 -> LoRa init succeeded.
20:57:40.759 -> Sending HeLoRa World! 0
20:57:40.968 -> Message: ⸮LoRaIoT1Maduino2at 57.7,ah 64,wsa 1,wsg 2,wd 88.13,r 0.00,
20:57:41.036 -> RSSI: -72
20:57:41.036 -> Snr: 9.50
20:57:41.036 ->
20:57:51.766 -> Sending HeLoRa World! 2
20:58:02.532 -> Sending HeLoRa World! 4
20:58:12.845 -> Sending HeLoRa World! 6
20:58:23.434 -> Sending HeLoRa World! 8
20:58:34.190 -> Sending HeLoRa World! 10
20:58:42.005 -> Message: ⸮LoRaIoT1Maduino2at 57.6,ah 64,wsa 2,wsg 5,wd 74.25,r 0.00,
20:58:42.074 -> RSSI: -72
20:58:42.074 -> Snr: 9.75
The Meadow code builds on my Windows 10 IoT Core Receive Basic and the Meadow Transmit Basic samples.
public class MeadowApp : App<F7Micro, MeadowApp>
{
private Rfm9XDevice rfm9XDevice;
public MeadowApp()
{
ISpiBus spiBus = Device.CreateSpiBus(500);
if (spiBus == null)
{
Console.WriteLine("spiBus == null");
}
rfm9XDevice = new Rfm9XDevice(Device, spiBus, Device.Pins.D09, Device.Pins.D11);
// Put device into LoRa + Sleep mode
rfm9XDevice.RegisterWriteByte(0x01, 0b10000000); // RegOpMode
// Set the frequency to 915MHz
byte[] frequencyWriteBytes = { 0xE4, 0xC0, 0x00 }; // RegFrMsb, RegFrMid, RegFrLsb
rfm9XDevice.RegisterWrite(0x06, frequencyWriteBytes);
rfm9XDevice.RegisterWriteByte(0x0F, 0x0); // RegFifoRxBaseAddress
rfm9XDevice.RegisterWriteByte(0x01, 0b10000101); // RegOpMode set LoRa & RxContinuous
while (true)
{
// Wait until a packet is received, no timeouts in PoC
Console.WriteLine("Receive-Wait");
byte IrqFlags = rfm9XDevice.RegisterReadByte(0x12); // RegIrqFlags
while ((IrqFlags & 0b01000000) == 0) // wait until RxDone cleared
{
Task.Delay(100).Wait();
IrqFlags = rfm9XDevice.RegisterReadByte(0x12); // RegIrqFlags
Console.Write(".");
}
Console.WriteLine("");
Console.WriteLine(string.Format("RegIrqFlags {0}", Convert.ToString((byte)IrqFlags, 2).PadLeft(8, '0')));
Console.WriteLine("Receive-Message");
byte currentFifoAddress = rfm9XDevice.RegisterReadByte(0x10); // RegFifiRxCurrent
rfm9XDevice.RegisterWriteByte(0x0d, currentFifoAddress); // RegFifoAddrPtr
byte numberOfBytes = rfm9XDevice.RegisterReadByte(0x13); // RegRxNbBytes
byte[] messageBytes = rfm9XDevice.RegisterRead(0x00, numberOfBytes); // RegFifo
rfm9XDevice.RegisterWriteByte(0x0d, 0);
rfm9XDevice.RegisterWriteByte(0x12, 0b11111111); // RegIrqFlags clear all the bits
string messageText = UTF8Encoding.UTF8.GetString(messageBytes);
Console.WriteLine("Received {0} byte message {1}", messageBytes.Length, messageText);
Console.WriteLine("Receive-Done");
}
}
}
The receive code works reliably but has no error detection or correction capability, so every so often a message gets corrupted. Which is can be seen in the Debug output below.
'App.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'.
'App.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\BrynLewis\source\repos\RFM9X.Meadow\ReceiveBasic\bin\Debug\net472\App.exe'. Symbols loaded.
'App.exe' (CLR v4.0.30319: App.exe): Loaded 'C:\Users\BrynLewis\source\repos\RFM9X.Meadow\ReceiveBasic\bin\Debug\net472\Meadow.dll'.
The program '[18208] App.exe: Program Trace' has exited with code 0 (0x0).
The program '[18208] App.exe' has exited with code 0 (0x0).
.
.
DirectRegisterAccess = True
.
.
Receive-Wait
RegIrqFlags 01010000
Receive-Message
Received 61 byte message ???LoRaIoT1Maduino2at 57.7,ah 64,wsa 2,wsg 4,wd 254.25,r 0.00,
Receive-Done
Receive-Wait
RegIrqFlags 01110000
Receive-Message
Received 60 byte message ???LoReE??????!???ngvyno2at,57/7-ah 6???,w???a 2,w?????????6,7$.13,r 0.00-
Receive-Done
Receive-Wait
RegIrqFlags 01010000
Receive-Message
Received 16 byte message HeLoRa World! 0
Receive-Done
Receive-Wait
RegIrqFlags 01000000
Receive-Message
Received 60 byte message ???LoRaIoT1Maduino2at 57.7,ah 64,wsa 1,wsg 2,wd 88.13,r 0.00,
Receive-Done
Receive-Wait
RegIrqFlags 01010000
Receive-Message
Received 16 byte message HeLoRa World! 2
Receive-Done
Receive-Wait
I will look at implementing some sort of carrier-sense multiple access with collision avoidance solution to reduce the number of corrupted messages when two (or possibly more devices) transmit at the same time.
Transmitting/receiving with interrupts or design goals next.