Receive and Transmit with Interrupts
After confirming my TransmitInterrupt and ReceiveInterupt test-rigs worked with an Arduino device running the SandeepMistry Arduino LoRa library LoRaSimpleNode example (LoRa.enableInvertIQ disabled) I merged them together.
For this client I’m using a Dragino Raspberry Pi HAT featuring GPS and LoRa® technology on my Raspberry PI.
The Dragino Raspberry Pi HAT featuring GPS and LoRa® technology hat has the same pin configuration as the M2M 1 Channel LoRaWAN Gateway Shield for Raspberry Pi so no code changes were required.
class Program
{
static void Main(string[] args)
{
int messageCount = 1;
// Uptronics has no reset pin uses CS0 or CS1
//SX127XDevice sX127XDevice = new SX127XDevice(25, chipSelectLine: 0);
//SX127XDevice sX127XDevice = new SX127XDevice(25, chipSelectLine: 1);
// M2M device has reset pin uses non standard chip select
//SX127XDevice sX127XDevice = new SX127XDevice(4, chipSelectLine: 0, chipSelectLogicalPinNumber: 25, resetPin: 17);
SX127XDevice sX127XDevice = new SX127XDevice(4, chipSelectLine: 1, chipSelectLogicalPinNumber: 25, resetPinNumber: 17);
// Put device into LoRa + Sleep mode
sX127XDevice.WriteByte(0x01, 0b10000000); // RegOpMode
// Set the frequency to 915MHz
byte[] frequencyBytes = { 0xE4, 0xC0, 0x00 }; // RegFrMsb, RegFrMid, RegFrLsb
sX127XDevice.WriteBytes(0x06, frequencyBytes);
sX127XDevice.WriteByte(0x0F, 0x0); // RegFifoRxBaseAddress
// More power PA Boost
sX127XDevice.WriteByte(0x09, 0b10000000); // RegPaConfig
sX127XDevice.WriteByte(0x40, 0b00000000); // RegDioMapping1 0b00000000 DI0 RxReady & TxReady
sX127XDevice.WriteByte(0x01, 0b10000101); // RegOpMode set LoRa & RxContinuous
while (true)
{
sX127XDevice.WriteByte(0x0E, 0x0); // RegFifoTxBaseAddress
// Set the Register Fifo address pointer
sX127XDevice.WriteByte(0x0D, 0x0); // RegFifoAddrPtr
string messageText = "Hello LoRa from .NET Core! " + messageCount.ToString();
messageCount += 1;
// load the message into the fifo
byte[] messageBytes = UTF8Encoding.UTF8.GetBytes(messageText);
sX127XDevice.WriteBytes(0x00, messageBytes); // RegFifoAddrPtr
// Set the length of the message in the fifo
sX127XDevice.WriteByte(0x22, (byte)messageBytes.Length); // RegPayloadLength
sX127XDevice.WriteByte(0x40, 0b01000000); // RegDioMapping1 0b00000000 DI0 RxReady & TxReady
sX127XDevice.WriteByte(0x01, 0b10000011); // RegOpMode
Debug.WriteLine($"Sending {messageBytes.Length} bytes message {messageText}");
Thread.Sleep(10000);
}
}
}
For the SX127X to transmit and receive messages the device has to be put into sleep mode (RegOpMode), the frequency set to 915MHz(RegFrMsb, RegFrMid, RegFrLsb) and the receiver enabled(RxContinuous). In addition interrupts have to be enabled(RegDioMapping1) on message received(RxReady) and message sent(TxReady).
sX127XDevice.WriteByte(0x40, 0b00000000); // RegDioMapping1 0b00000000 DI0 RxReady & TxReady
sX127XDevice.WriteByte(0x01, 0b10000101); // RegOpMode set LoRa & RxContinuous
When running the applications sleeps the SX127X module(RegOpMode), writes the message payload to the buffer(RegFifoAddrPtr,RegPayloadLength) then turns on the transmitter(RegOpMode). When has message arrived or a message has been sent the DI0 pin is strobed, the type of interrupt is determined (RegIrqFlags) and processed accordingly. Once the interrupt has been processed the interrupt flags(RegIrqFlags) are cleared, the receiver re-enabled and the interrupt mappings reset(RegDioMapping1) reset.
Loaded '/usr/lib/dotnet/shared/Microsoft.NETCore.App/5.0.4/System.Memory.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Sending 28 bytes message Hello LoRa from .NET Core! 1
RegIrqFlags 00001000
Transmit-Done
RegIrqFlags 01010000
Receive-Message
Received 32 byte message HeLoRa World! I'm a Node! 880000
Sending 28 bytes message Hello LoRa from .NET Core! 2
RegIrqFlags 00001000
Transmit-Done
RegIrqFlags 01010000
Receive-Message
Received 32 byte message HeLoRa World! I'm a Node! 890000
Sending 28 bytes message Hello LoRa from .NET Core! 3
RegIrqFlags 00001000
Transmit-Done
RegIrqFlags 01010000
Receive-Message
Received 32 byte message HeLoRa World! I'm a Node! 900000
Sending 28 bytes message Hello LoRa from .NET Core! 4
RegIrqFlags 00001000
Transmit-Done
RegIrqFlags 01010000
Receive-Message
Received 32 byte message HeLoRa World! I'm a Node! 910000
Sending 28 bytes message Hello LoRa from .NET Core! 5
RegIrqFlags 00001000
Transmit-Done
RegIrqFlags 01010000
Receive-Message
Received 32 byte message HeLoRa World! I'm a Node! 920000
Summary
In this iteration I sent messages to and from my .Net Core 5 dotnet/iot powered Raspberry PI using a Dragino LoRa Shield 915MHz and a Seeeduino V4.2 client.