nanoFramework LoRa library Part1

After writing my Windows 10 IoT Core RFM9X library and porting it to .NetMF, Wilderness Labs Meadow and GHI Electronics TinyCLR-OS I figured yet another platform port shouldn’t take too long.

To get started I used a Dragino LoRa shield for Arduino and jumper wires to connect it to my STM32F4 Discovery Kit running the nanoFramework.

Dragino Arduino LoRa Shield Schematic

My initial pin mapping was

  • SCK->PF7->D10
  • MISO->PF8->D12
  • MOSI->PF9->D11
  • CS->PC2->D10

The shield uses D10 for chip select, D2 for RFM9X DI0 interrupt and D9 for Reset. The shield ships with the SPI lines configured for ICSP so the three jumpers diagonally across the shield from the antenna connector need to be swapped to the side closest to the edge of the shield.

STM32F429 Discovery kit with Dragino Shield

First step was to confirm I could (using the nanoFramework GPIO and SPI Nuget packages) read a couple of the Semtech SX1276 registers.

//---------------------------------------------------------------------------------
// Copyright (c) April 2020, 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.ShieldSPI
{
   using System;
   using System.Threading;

   using Windows.Devices.Gpio;
   using Windows.Devices.Spi;

   public class Program
   {
      public static void Main()
      {
         try
         {
            GpioController gpioController = GpioController.GetDefault();

            GpioPin chipSelectGpioPin = gpioController.OpenPin(PinNumber('C', 2));
            chipSelectGpioPin.SetDriveMode(GpioPinDriveMode.Output);

            var settings = new SpiConnectionSettings(chipSelectGpioPin.PinNumber)
            {
               ClockFrequency = 500000,
               Mode = SpiMode.Mode0,// From SemTech docs pg 80 CPOL=0, CPHA=0
               //Mode = SpiMode.Mode1,
               //Mode = SpiMode.Mode2,
               //Mode = SpiMode.Mode3,
               SharingMode = SpiSharingMode.Shared,
               //SharingMode = SpiSharingMode.Exclusive,
            };
            

            SpiDevice Device = SpiDevice.FromId("SPI5", settings);

            Thread.Sleep(500);

            while (true)
            {
               byte[] writeBuffer = new byte[] { 0x42 };
               byte[] readBuffer = new byte[1];
            
               //Device.Write(writeBuffer);
               //Device.Read(readBuffer);
               Device.TransferSequential(writeBuffer, readBuffer);

               byte registerValue = readBuffer[0];
               Console.WriteLine(String.Format("Register 0x{0:x2} - Value 0X{1:x2}", 0x42, registerValue));

               Thread.Sleep(10000);
            }
         }
         catch (Exception ex)
         {
            Console.WriteLine(ex.Message);
         }
      }

      static int PinNumber(char port, byte pin)
      {
         if (port < 'A' || port > 'J')
            throw new ArgumentException();

         return ((port - 'A') * 16) + pin;
      }
   }
}

After trying many permutations of settings I could successfully read the RegVersion and default frequency values

Attaching to nanoDevice...
Waiting for nanoDevice to initialize...
Updating nanoDevice debugger engine.
The nanoDevice runtime is loading the application assemblies and starting execution.
'nanoFramework.Tools.VS2019.Extension.dll' (Managed): Loaded 'C:\Users\BrynLewis\source\repos\RFM9X.NetNF\ShieldSPI\bin\Debug\ShieldSPI.exe', Symbols loaded.
'nanoFramework.Tools.VS2019.Extension.dll' (Managed): Loaded 'C:\Users\BrynLewis\source\repos\RFM9X.NetNF\packages\nanoFramework.Windows.Devices.Spi.1.3.0-preview.12\lib\Windows.Devices.Spi.dll', Symbols loaded.
'nanoFramework.Tools.VS2019.Extension.dll' (Managed): Loaded 'C:\Users\BrynLewis\source\repos\RFM9X.NetNF\packages\nanoFramework.Runtime.Events.1.4.2-preview.1\lib\nanoFramework.Runtime.Events.dll', Symbols loaded.
'nanoFramework.Tools.VS2019.Extension.dll' (Managed): Loaded 'C:\Users\BrynLewis\source\repos\RFM9X.NetNF\packages\nanoFramework.Windows.Devices.Gpio.1.4.1-preview.13\lib\Windows.Devices.Gpio.dll', Symbols loaded.
The thread '<No Name>' (0x2) has exited with code 0 (0x0).
Register 0x42 - Value 0X12
Register 0x42 - Value 0X12

Mapping the Discovery board pins to corresponding SPI and GPIO pins was a bit painful

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.