Windows 10: How to use IoT extension for Raspberry Pi 2 (part 1)

If you have already installed Windows 10 on Raspberry it’s time to discuss how to use pins and I2C there. Of course, if you are going to deploy an application which doesn’t work with GPIO, you don’t have to do anything special – you need to create the same Universal application like for desktop or phone using C# or other languages and deploy it using ARM platform selection directly from Visual Studio. But GPIO is device-specific feature for which it makes no sense to include it to the common library. So, if you are going to use some IoT specific features you need to add IoT Extension to you project.

 

This extension declares just two contracts: Windows.Devices.DevicesLowLevelContract and Windows.System.SystemManagementContract. It’s easy to find this declarations if you go to the following folder C:\Program Files (x86)\Windows Kits\10\Extension SDKs\WindowsIoT\10.0.10069.0 and check SDKManifest.xaml. SystemManagement doesn’t contain anything special – just classes which help shutdown the device and change time zone. But DevicesLowLevelContract contains many important classes. Let’s looks at the Raspberry and see that we can achieve thanks to these classes.

 

In case of GPIO you can use the yellow pins to send signals to sensors or receive data from them. On Raspberry, all pins generate 3.3 volts of voltage. Pink pins supports SPI protocol and you can connect up to two devices/sensors there (probably three but I never made this experiment). Finally, blue pins allow to use I2C hub which supports more than hundred devices/sensors. Of course, GND pins are for ground and there is four power pins (two 5V pins and two 3.3V pins).

Let’s start with GPIO. You can find necessary classes (and enumeration types) in Windows.Devices.Gpio namespace and they allow developers to control GPIO pins on the board. The first class there is GpioController
which allows to get access to controller and get reference to needed pins. If you are going to use GPIO you need to start with calling of GetDefault method which return reference to current GPIO controller (if exist):

 var controller=GpioController.GetDefault();

If you are going to write really universal code you need to check if controller is null. If it is, your device doesn’t have GPIO (desktop, phone etc.) It’s very useful because starting with Windows 10 we have Universal platform and you can run the same binary anywhere.

Once you have access to controller you can try to get reference to pins. OpenPin method allows to get reference to pin lock it exclusively or leave it in shared mode. Of course, this method doesn’t help you to setup the pin and just return reference to GpioPin object. There is the same thing like for GpioController – you need to check if returned reference is not null, because other processes could lock the pin for own purposes.

 var pin = controller.OpenPin(0);
 
if (pin == null)
{
 return;
}

OpenPin method should get number of the pin and you should use the same numbers like on the image above. Finally, if you got access to the pin you can try to work with it. There are three important methods like SetDriveMode,
Read and Write. With the first one you can setup the pin like input or output pin. Read and Write methods allow to send or receive signal there.

So, if you already worked with some micro boards, you will not find anything special but if you are new in IoT you can start with creation of a simple Blinky project, which Microsoft published here.

Of course, more interesting classes are located in Windows.Devices.I2C and Windows.Devices.SPI namespaces. Finally I got my MPU6050 sensor, so next time we will get data from there using these classes.