Anatomy of GPIO on Windows IoT

Let’s explore the General Purpose Input/Output (GPIO) interface offered by the Windows 10 IoT core.

The GPIO interface is deceptively simple. It consists in a few classes and enumerations. The main classes are:

  • GpioController class: Provides access to all the pins of the GPIO(s) bus.
  • GpioPin class: Represents a single pin on the GPIO. It provides functions to read/write pin values and provides an event that can be triggered when a pin value changes.

GpioController

The GpioController class cannot be directly instantiated. Instances are accessed through one of the static functions on the class:

  • GetDefault(): Returns the default Gpio controller for the device.
  • GetDefaultAsync(): Returns the default Gpio controller for the device asynchronously.
  • GetControllersAsync() : Returns a list of all the Gpio controllers available for the device asynchronously.

The instance functions of the GpioController are used to manage pins:

  • PinCount: Returns the number of pins available for this GpioController.
  • OpenPin(): Opens a connection to the pin and returns the corresponding GpioPin. An overload of the function allows you to supply a sharing mode other than exclusive.
  • TryOpenPin(): Opens a connection to the pin and returns the corresponding GpioPin. This variant will not throw an exception on error but will return a status making it easier to handle errors.

GpioPin

The GpioPin class gives you access to read and manipulate a single pin. Like the GpioController, you cannot instantiate this class directly. You access it using the GpioController OpenPin or TryOpenPin functions.

The GpioPin class provides members query information about the pin:

  • PinNumber: Returns the pin number for this pin.
  • SharingMode: Returns the sharing mode for this pin. Modes can be Exclusive or SharedReadOnly.

The GpioPin class provides members configure the pin. Drive mode is probably the most important item to configure on a pin. It tells the pin whether it is an input or an output and specifies the way the electronic is organized. There are multiple drive mode available see GpioPinDriveMode for more information about the available drive modes. I will do a post specific to this later on.

  • IsDriveModeSupported(): Can the pin support this drive mode.
  • GetDriveMode(): Returns the currently configured drive mode for this pin.
  • SetDriveMode(): Sets the drive mode for this pin.
  • ValueChanged: Event that is triggered when a change of state is detected on the pin. This member allows us to attach an event handler to handle that event.
  • DebounceTimeout: Get/Sets the interval of time after the ValueChanged event is trigger during which the changes in the pin will not trigger another ValueChanged event.

The GpioPin class provides members to read and write the value of the pin:

  • Read(): Returns the current value of the pin High/Low. This will work on inputs or output pins.
  • Write(): Sets the value for the pin High/Low. This will only work on output pins.

Usage

There are a number of possible ways to use the GPIO. The most common scenario are:

Pin reading / writing

This process consist in setting up your pin(s), manipulating them and disposing of the pin when done.

GPIO-Manipulation-Activity

Reading and Writing GPIO Pins

 

Monitoring Events

This process consist in setting up your pin(s), registering an event handler that will be triggered when a pin change is detected and waiting for events to occur, and disposing of the pin(s) when done .

GPIO-EventBased-Activity

Reading and Writing GPIO Pins

 

Conclusion

The GPIO interface provided by the Windows 10 IoT library is simple but comprehensive. This information should allow us to come up with a plan on how to write device code.

The only touchy points I can see so far is: Figuring out how to set a valid DebounceTimeout value. We’ll be in a better position to figure out those details once we start coding against an actual device.

For more information see the MSDN documentation

https://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.gpio.gpiocontroller.aspx

, ,

  1. Leave a comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: