RFM9X.IoTCore on Github

After a month of posts the source code of V0.9 of my RFM9X/SX127X library is on GitHub. I included all of the source for my test harness and proof of concept(PoC) applications so other people can follow along with “my learning experience”.

I started wanting a library to for a LoRa telemetry field gateway and ended up writing one (which is usually not a good idea). My use case was a device that was configured, then run for long periods of time, was not battery powered, and if settings were changed could be restarted. I need to trial with some more hardware, frequency bands, variety of clients, initialisation configurations and backport the last round of fixes to my .NetMF library.

I am also looking at writing an RFM69 library using a pair of shields (434MHz & 915MHz)  from seegel-systeme.

The simplest possible application using the new library (a fair bit of the code is to support the different supported shields)

//---------------------------------------------------------------------------------
// Copyright (c) August 2018, devMobile Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//---------------------------------------------------------------------------------
namespace devMobile.IoT.Rfm9x.LoRaDeviceClient
{
	using System;
	using System.Diagnostics;
	using System.Text;
	using System.Threading.Tasks;

	using devMobile.IoT.Rfm9x;
	using Windows.ApplicationModel.Background;

	public sealed class StartupTask : IBackgroundTask
    {
		private byte NessageCount = Byte.MaxValue;
#if DRAGINO
		private const byte ChipSelectLine = 25;
		private const byte ResetLine = 17;
		private const byte InterruptLine = 4;
		private Rfm9XDevice rfm9XDevice = new Rfm9XDevice(ChipSelectPin.CS0, ChipSelectLine, ResetLine, InterruptLine);
#endif
#if M2M
		private const byte ChipSelectLine = 25;
		private const byte ResetLine = 17;
		private const byte InterruptLine = 4;
		private Rfm9XDevice rfm9XDevice = new Rfm9XDevice(ChipSelectPin.CS0, ChipSelectLine, ResetLine, InterruptLine);
#endif
#if ELECROW
		private const byte ResetLine = 22;
		private const byte InterruptLine = 25;
		private Rfm9XDevice rfm9XDevice = new Rfm9XDevice(ChipSelectPin.CS1, ResetLine, InterruptLine);
#endif
#if ELECTRONIC_TRICKS
		private const byte ResetLine = 22;
		private const byte InterruptLine = 25;
		private Rfm9XDevice rfm9XDevice = new Rfm9XDevice(ChipSelectPin.CS0, 22, 25);
#endif

		public void Run(IBackgroundTaskInstance taskInstance)
		{
			rfm9XDevice.Initialise(Rfm9XDevice.RegOpModeMode.ReceiveContinuous, 915000000.0, paBoost: true);

#if DEBUG
			rfm9XDevice.RegisterDump();
#endif
			rfm9XDevice.OnReceive += Rfm9XDevice_OnReceive;
			rfm9XDevice.OnTransmit += Rfm9XDevice_OnTransmit;

			Task.Delay(10000).Wait();

			while (true)
			{
				string messageText = string.Format("Hello W10 IoT Core LoRa! {0}", NessageCount);
				NessageCount -= 1;

				byte[] messageBytes = UTF8Encoding.UTF8.GetBytes(messageText);
				Debug.WriteLine("{0:HH:mm:ss}-TX {1} byte message {2}", DateTime.Now, messageBytes.Length, messageText);
				this.rfm9XDevice.Send(messageBytes);

				Task.Delay(10000).Wait();
			}
		}

		private void Rfm9XDevice_OnReceive(object sender, Rfm9XDevice.OnDataReceivedEventArgs e)
		{
			try
			{
				string messageText = UTF8Encoding.UTF8.GetString(e.Data);

				Debug.WriteLine("{0:HH:mm:ss}-RX {1} byte message {2}", DateTime.Now, e.Data.Length, messageText);
			}
			catch (Exception ex)
			{
				Debug.WriteLine(ex.Message);
			}
		}

		private void Rfm9XDevice_OnTransmit(object sender, Rfm9XDevice.OnDataTransmitedEventArgs e)
		{
			Debug.WriteLine("{0:HH:mm:ss}-TX Done", DateTime.Now);
		}
	}
}

I have a shield from uputronics on order which should arrive from the UK in roughly a week. This shield has two RFM9X devices onboard (In my case 434MHz & 915MHz) so it will be interesting to see how my library copes with two instances of the stack running together.

I need to do more testing (especially of the initialisation options) and will add basic device addressing soon so my field gateway will only see messages which it is interested in.

Electronic Tricks Lora/LoraWan shield for Raspberry Pi Zero and PI3

For the example code so far I had been using the Dragino LoRa GPS HAT for Raspberry PI which, after looking at the schematic (to figure out how the chip select line was connected) worked pretty well.

I had also purchased a Lora/LoRaWAN shield for Raspberry PI Zero and PI3 from Tindie (plus some unpopulated printed circuit boards so I can try building a RFM69HCW based shield).

The board didn’t fit on my Raspberry PI 2 & 3 devices so I used a Dexter industries Grove PI0 Shield as a temporary spacer to lift the antenna connector above the USB sockets.

The RFM95 chip select line is connected to pin 24 (GPIO8), the reset line to pin 29(GPIO5) and the interrupt line (RFM95 DIO0) to pin 22(GPIO25).

ElectronicTricksRFM95

My board doesn’t have any Light Emitting Diodes (LEDs) so it was straight into reading register values

//---------------------------------------------------------------------------------
// Copyright (c) July 2018, devMobile Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//---------------------------------------------------------------------------------
namespace devMobile.IoT.Rfm9x.ElectronicTricksSPI
{
	using System;
	using System.Diagnostics;
	using System.Threading;
	using Windows.ApplicationModel.Background;
	using Windows.Devices.Spi;

	public sealed class StartupTask : IBackgroundTask
	{
		public void Run(IBackgroundTaskInstance taskInstance)
		{
			SpiController spiController = SpiController.GetDefaultAsync().AsTask().GetAwaiter().GetResult();
			var settings = new SpiConnectionSettings(0) // GPIO8 Electronic Tricks
			{
				ClockFrequency = 500000,
				Mode = SpiMode.Mode0,   // From SemTech docs pg 80 CPOL=0, CPHA=0
			};

			SpiDevice Device = spiController.GetDevice(settings);

			while (true)
			{
				byte[] writeBuffer = new byte[] { 0x42 }; // RegVersion
				byte[] readBuffer = new byte[1];

				Device.TransferSequential(writeBuffer, readBuffer);

				byte registerValue = readBuffer[0];
				Debug.WriteLine("Register 0x{0:x2} - Value 0X{1:x2} - Bits {2}", 0x42, registerValue, Convert.ToString(registerValue, 2).PadLeft(8, '0'));

				Thread.Sleep(10000);
			}
		}
	}
}

The debug output confirmed I was reading the right value from the RegVer register

Register 0x42 - Value 0X12 - Bits 00010010
Register 0x42 - Value 0X12 - Bits 00010010
Register 0x42 - Value 0X12 - Bits 00010010
Register 0x42 - Value 0X12 - Bits 00010010
Register 0x42 - Value 0X12 - Bits 00010010
Register 0x42 - Value 0X12 - Bits 00010010
Register 0x42 - Value 0X12 - Bits 00010010
Register 0x42 - Value 0X12 - Bits 00010010

The antenna connector not clearing the USB socket is an issue which I’ll solve with a socket like the one on the GrovePI which has longer leads and acts as a spacer.
ElectronicTricksLoraShield

RFM95/96/97/98 shield library Part2

Register Dump

Next step was to dump all registers (0x00 thru 0x40) of the SX1276/7/8/9 device

//---------------------------------------------------------------------------------
// Copyright (c) July 2018, devMobile Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//---------------------------------------------------------------------------------
namespace devMobile.IoT.Rfm9x.RegisterScan
{
   using System;
   using System.Diagnostics;
   using System.Threading.Tasks;
   using Windows.ApplicationModel.Background;
   using Windows.Devices.Gpio;
   using Windows.Devices.Spi;

   public sealed class Rfm9XDevice
   {
      private SpiDevice rfm9XLoraModem;
      private GpioPin chipSelectGpioPin;

      public Rfm9XDevice(int chipSelectPin)
      {
         SpiController spiController = SpiController.GetDefaultAsync().AsTask().GetAwaiter().GetResult();
         var settings = new SpiConnectionSettings(0)
         {
            ClockFrequency = 500000,
            Mode = SpiMode.Mode0,
         };

         GpioController gpioController = GpioController.GetDefault();
         chipSelectGpioPin = gpioController.OpenPin(chipSelectPin);
         chipSelectGpioPin.SetDriveMode(GpioPinDriveMode.Output);
         chipSelectGpioPin.Write(GpioPinValue.High);

         rfm9XLoraModem = spiController.GetDevice(settings);
      }

      public Byte RegisterReadByte(byte registerAddress)
      {
         byte[] writeBuffer = new byte[] { registerAddress };
         byte[] readBuffer = new byte[1];
         Debug.Assert(rfm9XLoraModem != null);

         chipSelectGpioPin.Write(GpioPinValue.Low);
         rfm9XLoraModem.Write(writeBuffer);
         rfm9XLoraModem.Read(readBuffer);
         chipSelectGpioPin.Write(GpioPinValue.High);

         return readBuffer[0];
      }
   }

   public sealed class StartupTask : IBackgroundTask
   {
      private const int ChipSelectLine = 25;
      private Rfm9XDevice rfm9XDevice = new Rfm9XDevice(ChipSelectLine);

      public void Run(IBackgroundTaskInstance taskInstance)
      {
         while (true)
         {
            for (byte registerIndex = 0; registerIndex <= 0x42; registerIndex++)
            {
               byte registerValue = rfm9XDevice.RegisterReadByte(registerIndex);

               Debug.WriteLine("Register 0x{0:x2} - Value 0X{1:x2} - Bits {2}", registerIndex, registerValue, Convert.ToString(registerValue, 2).PadLeft(8, '0'));
            }

            Task.Delay(10000).Wait();
         }
      }
   }
}

The output of the application looked like this

Register 0x00 – Value 0X00 – Bits 00000000
Register 0x01 – Value 0X09 – Bits 00001001
Register 0x02 – Value 0X1a – Bits 00011010
Register 0x03 – Value 0X0b – Bits 00001011
Register 0x04 – Value 0X00 – Bits 00000000
Register 0x05 – Value 0X52 – Bits 01010010
Register 0x06 – Value 0X6c – Bits 01101100
Register 0x07 – Value 0X80 – Bits 10000000
Register 0x08 – Value 0X00 – Bits 00000000
Register 0x09 – Value 0X4f – Bits 01001111
Register 0x0a – Value 0X09 – Bits 00001001
Register 0x0b – Value 0X2b – Bits 00101011
Register 0x0c – Value 0X20 – Bits 00100000
Register 0x0d – Value 0X08 – Bits 00001000
Register 0x0e – Value 0X02 – Bits 00000010
Register 0x0f – Value 0X0a – Bits 00001010
Register 0x10 – Value 0Xff – Bits 11111111
Register 0x11 – Value 0X71 – Bits 01110001
Register 0x12 – Value 0X15 – Bits 00010101
Register 0x13 – Value 0X0b – Bits 00001011
Register 0x14 – Value 0X28 – Bits 00101000
Register 0x15 – Value 0X0c – Bits 00001100
Register 0x16 – Value 0X12 – Bits 00010010
Register 0x17 – Value 0X47 – Bits 01000111
Register 0x18 – Value 0X32 – Bits 00110010
Register 0x19 – Value 0X3e – Bits 00111110
Register 0x1a – Value 0X00 – Bits 00000000
Register 0x1b – Value 0X00 – Bits 00000000
Register 0x1c – Value 0X00 – Bits 00000000
Register 0x1d – Value 0X00 – Bits 00000000
Register 0x1e – Value 0X00 – Bits 00000000
Register 0x1f – Value 0X40 – Bits 01000000
Register 0x20 – Value 0X00 – Bits 00000000
Register 0x21 – Value 0X00 – Bits 00000000
Register 0x22 – Value 0X00 – Bits 00000000
Register 0x23 – Value 0X00 – Bits 00000000
Register 0x24 – Value 0X05 – Bits 00000101
Register 0x25 – Value 0X00 – Bits 00000000
Register 0x26 – Value 0X03 – Bits 00000011
Register 0x27 – Value 0X93 – Bits 10010011
Register 0x28 – Value 0X55 – Bits 01010101
Register 0x29 – Value 0X55 – Bits 01010101
Register 0x2a – Value 0X55 – Bits 01010101
Register 0x2b – Value 0X55 – Bits 01010101
Register 0x2c – Value 0X55 – Bits 01010101
Register 0x2d – Value 0X55 – Bits 01010101
Register 0x2e – Value 0X55 – Bits 01010101
Register 0x2f – Value 0X55 – Bits 01010101
Register 0x30 – Value 0X90 – Bits 10010000
Register 0x31 – Value 0X40 – Bits 01000000
Register 0x32 – Value 0X40 – Bits 01000000
Register 0x33 – Value 0X00 – Bits 00000000
Register 0x34 – Value 0X00 – Bits 00000000
Register 0x35 – Value 0X0f – Bits 00001111
Register 0x36 – Value 0X00 – Bits 00000000
Register 0x37 – Value 0X00 – Bits 00000000
Register 0x38 – Value 0X00 – Bits 00000000
Register 0x39 – Value 0Xf5 – Bits 11110101
Register 0x3a – Value 0X20 – Bits 00100000
Register 0x3b – Value 0X82 – Bits 10000010
Register 0x3c – Value 0Xff – Bits 11111111
Register 0x3d – Value 0X02 – Bits 00000010
Register 0x3e – Value 0X80 – Bits 10000000
Register 0x3f – Value 0X40 – Bits 01000000
Register 0x40 – Value 0X00 – Bits 00000000
Register 0x41 – Value 0X00 – Bits 00000000
Register 0x42 – Value 0X12 – Bits 00010010
Register 0x00 – Value 0X00 – Bits 00000000
Register 0x01 – Value 0X09 – Bits 00001001
Register 0x02 – Value 0X1a – Bits 00011010
Register 0x03 – Value 0X0b – Bits 00001011
Register 0x04 – Value 0X00 – Bits 00000000
Register 0x05 – Value 0X52 – Bits 01010010
Register 0x06 – Value 0X6c – Bits 01101100
Register 0x07 – Value 0X80 – Bits 10000000
Register 0x08 – Value 0X00 – Bits 00000000
Register 0x09 – Value 0X4f – Bits 01001111
Register 0x0a – Value 0X09 – Bits 00001001
Register 0x0b – Value 0X2b – Bits 00101011
Register 0x0c – Value 0X20 – Bits 00100000
Register 0x0d – Value 0X08 – Bits 00001000
Register 0x0e – Value 0X02 – Bits 00000010
Register 0x0f – Value 0X0a – Bits 00001010
Register 0x10 – Value 0Xff – Bits 11111111
Register 0x11 – Value 0X71 – Bits 01110001
Register 0x12 – Value 0X15 – Bits 00010101
Register 0x13 – Value 0X0b – Bits 00001011
Register 0x14 – Value 0X28 – Bits 00101000
Register 0x15 – Value 0X0c – Bits 00001100
Register 0x16 – Value 0X12 – Bits 00010010
Register 0x17 – Value 0X47 – Bits 01000111
Register 0x18 – Value 0X32 – Bits 00110010
Register 0x19 – Value 0X3e – Bits 00111110
Register 0x1a – Value 0X00 – Bits 00000000
Register 0x1b – Value 0X00 – Bits 00000000
Register 0x1c – Value 0X00 – Bits 00000000
Register 0x1d – Value 0X00 – Bits 00000000
Register 0x1e – Value 0X00 – Bits 00000000
Register 0x1f – Value 0X40 – Bits 01000000
Register 0x20 – Value 0X00 – Bits 00000000
Register 0x21 – Value 0X00 – Bits 00000000
Register 0x22 – Value 0X00 – Bits 00000000
Register 0x23 – Value 0X00 – Bits 00000000
Register 0x24 – Value 0X05 – Bits 00000101
Register 0x25 – Value 0X00 – Bits 00000000
Register 0x26 – Value 0X03 – Bits 00000011
Register 0x27 – Value 0X93 – Bits 10010011
Register 0x28 – Value 0X55 – Bits 01010101
Register 0x29 – Value 0X55 – Bits 01010101
Register 0x2a – Value 0X55 – Bits 01010101
Register 0x2b – Value 0X55 – Bits 01010101
Register 0x2c – Value 0X55 – Bits 01010101
Register 0x2d – Value 0X55 – Bits 01010101
Register 0x2e – Value 0X55 – Bits 01010101
Register 0x2f – Value 0X55 – Bits 01010101
Register 0x30 – Value 0X90 – Bits 10010000
Register 0x31 – Value 0X40 – Bits 01000000
Register 0x32 – Value 0X40 – Bits 01000000
Register 0x33 – Value 0X00 – Bits 00000000
Register 0x34 – Value 0X00 – Bits 00000000
Register 0x35 – Value 0X0f – Bits 00001111
Register 0x36 – Value 0X00 – Bits 00000000
Register 0x37 – Value 0X00 – Bits 00000000
Register 0x38 – Value 0X00 – Bits 00000000
Register 0x39 – Value 0Xf5 – Bits 11110101
Register 0x3a – Value 0X20 – Bits 00100000
Register 0x3b – Value 0X82 – Bits 10000010
Register 0x3c – Value 0Xff – Bits 11111111
Register 0x3d – Value 0X02 – Bits 00000010
Register 0x3e – Value 0X80 – Bits 10000000
Register 0x3f – Value 0X40 – Bits 01000000
Register 0x40 – Value 0X00 – Bits 00000000
Register 0x41 – Value 0X00 – Bits 00000000
Register 0x42 – Value 0X12 – Bits 00010010

I also started to refactor the code (extracting the Read functionality and format the output in both hexadecimal and binary (convert.ToString in base2 + padleft) to make comparison of register values with the datasheet easier.

The device was not in LoRa mode (Bit 7 of RegOpMode 0x01) so the next step was to read and write registers so I could change its configuration.

RFM95/96/97/98 shield library Part1

Register Read

Over the last couple of weeks I have been working on a Windows 10 IoT Core C# library for my Dragino LoRa GPS hat for Raspberry PI. I initially started with the Dragino.LoRa library but after some experimentation and hacking I decided to write my own library (which is usually not a good idea).

DraginoLoraGPSHat

I wanted a lightweight LoRa only library (hopefully possible to backport to .NetMF) which didn’t try to hide how the Semtech 1276 chip functioned, and in the future could be configured to work with other vendors’ shields (dragino, electronictricks, elecrow, m2m).

I’m also working on an RFM69 Raspberry PI shield based on an electronictricks PCB populated with a RFM69HCW so I figured the experience of building a library would be useful.

The first step was to build a basic universal windows platform (UWP) background task to confirm that I could reliably communicate with the shield over SPI bus by reading a single register value (RegVersion the silicon version specified in the vendor datasheet).

//---------------------------------------------------------------------------------
// Copyright (c) July 2018, devMobile Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//---------------------------------------------------------------------------------
namespace devMobile.IoT.Rfm9x.RegisterRead
{
   using System;
   using System.Diagnostics;
   using System.Threading.Tasks;
   using Windows.ApplicationModel.Background;
   using Windows.Devices.Gpio;
   using Windows.Devices.Spi;

   public sealed class StartupTask : IBackgroundTask
   {
      private const int ChipSelectLine = 25;
      private SpiDevice rfm9XLoraModem;

      public void Run(IBackgroundTaskInstance taskInstance)
      {
         // Have to setup the SPI bus with custom Chip Select line rather than std CE0/CE1
         SpiController spiController = SpiController.GetDefaultAsync().AsTask().GetAwaiter().GetResult();
         var settings = new SpiConnectionSettings(0)
         {
            ClockFrequency = 500000,
            Mode = SpiMode.Mode0,
         };

         GpioController gpioController = GpioController.GetDefault();
         GpioPin chipSelectGpioPin = gpioController.OpenPin(ChipSelectLine);
         chipSelectGpioPin.SetDriveMode(GpioPinDriveMode.Output);
         chipSelectGpioPin.Write(GpioPinValue.High);

         rfm9XLoraModem = spiController.GetDevice(settings);

         while (true)
         {
            byte[] writeBuffer = new byte[]{ 0x42 }; // RegVersion
            byte[] readBuffer = new byte[1];

            chipSelectGpioPin.Write(GpioPinValue.Low);
            rfm9XLoraModem.Write(writeBuffer);
            rfm9XLoraModem.Read(readBuffer);
            chipSelectGpioPin.Write(GpioPinValue.High);

            Debug.WriteLine("RegVersion {0:x2}", readBuffer[0]);

            Task.Delay(10000).Wait();
            }
         }
   }
}

The dragino shield has the chip select (also know as slave select) line connected to pin 25 rather than the usual CS0 (pin 24) & CS1 (pin 26) so it has to be manually strobed.

'backgroundTaskHost.exe' (CoreCLR: CoreCLR_UWP_Domain): Loaded 'C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\RegisterRead-uwpVS.Debug_ARM.Bryn.Lewis\System.Diagnostics.Debug.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
RegVersion 12
RegVersion 12

Next step was to dump all the registers of the HopeRF module.