Communicate with SPI Display using Lightning providers

This sample demonstrates how to communicate with SPI bus controllers using the Micorosot.IoT.Providers.Lightning.SpiProvider

It’s based on the SPI Display sample shared on SPI Display sample. And shares the same setup steps and most of the code. The only difference is setting the Lightning provider as the default controllers provider.

Using Lightning Provider

Using the default GPIO and SPI controllers, the original code for initializing SPI and GPIO controllers used the following:

// Initializing the controllers and Spi device in the original Weather Station sample code

private async void InitAll()
{
    try
    {
        InitGpio();             /* Initialize the GPIO controller and GPIO pins */
        await InitSpi();        /* Initialize the SPI controller                */
        await InitDisplay();    /* Initialize the display                       */
    }

    // ...
}

private void InitGpio()
{
    try
    {
        IoController = GpioController.GetDefault(); /* Get the default GPIO controller on the system */
        // ...
    }
    // ...
}

private async Task InitSpi()
{
    try
    {
        var settings = new SpiConnectionSettings(SPI_CHIP_SELECT_LINE); /* Create SPI initialization settings                               */
        settings.ClockFrequency = 10000000;                             /* Datasheet specifies maximum SPI clock frequency of 10MHz         */
        settings.Mode = SpiMode.Mode3;                                  /* The display expects an idle-high clock polarity, we use Mode3
                                                                         * to set the clock polarity and phase to: CPOL = 1, CPHA = 1
                                                                         */

        string spiAqs = SpiDevice.GetDeviceSelector(SPI_CONTROLLER_NAME);       /* Find the selector string for the SPI bus controller          */
        var devicesInfo = await DeviceInformation.FindAllAsync(spiAqs);         /* Find the SPI bus controller device with our selector string  */
        SpiDisplay = await SpiDevice.FromIdAsync(devicesInfo[0].Id, settings);  /* Create an SpiDevice with our bus controller and SPI settings */

    }
    // ...
}

Now, to enable the use of the Lightning provider, the GPIO and SPI controllers initialization code needs to be modified to set the Lightning provider as the default.

In the initialization code below, if the Lightning driver is enabled on the target device, the Lightning provider will be used. Otherwise, the app falls back to using the default provider.

using Microsoft.IoT.Lightning.Providers;

private async void InitAll()
{
    try
    {
        InitLightningProvider();
        await InitGpio();       /* Initialize the GPIO controller and GPIO pins */
        await InitSpi();        /* Initialize the SPI controller                */
        await InitDisplay();    /* Initialize the display                       */
    }
    // ...
}

// Set Lightning provider as the default

private void InitLightningProvider()
{
    // Set the Lightning Provider as the default if Lightning driver is enabled on the target device
    if (LightningProvider.IsLightningEnabled)
    {
        LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();
    }
}

/* Initialize the SPI bus */
private async Task InitSpi()
{
    // The SPI initialization code works the same regardless of the current default provider
    try
    {
        var settings = new SpiConnectionSettings(SPI_CHIP_SELECT_LINE); /* Create SPI initialization settings                               */
        settings.ClockFrequency = 10000000;                             /* Datasheet specifies maximum SPI clock frequency of 10MHz         */
        settings.Mode = SpiMode.Mode3;                                  /* The display expects an idle-high clock polarity, we use Mode3    
                                                                            * to set the clock polarity and phase to: CPOL = 1, CPHA = 1         
                                                                            */

        SpiController controller = await SpiController.GetDefaultAsync();     /* Get the default SPI controller */

        SpiDisplay = controller.GetDevice(settings);             /* Get the Spi Display device using its settings */

    }
    // ...
}


/* Initialize the GPIO */
private async Task InitGpio()
{
    // The GPIO controller initialization code works the same regardless of the current default provider
    try
    {
        IoController = GpioController.GetDefault(); /* Get the default GPIO controller on the system */
        // ...
    }
    // ...
}

Building and deploying the sample

  1. Enable Lightning on your Raspberry Pi2 or MBM device using the the steps outlined in the Lightning Setup Guide.

  2. Ensure the correct version of the Windows SDK is installed on your development machine. The required Windows SDK can be installed from here.

  3. Clone the source for the SPI Display sample for Lightning from Github to a local folder.

  4. To setup the connections between the device and the SPI display, refer to the original SPI Display sample page.

  5. Load the solution in Visual Studio.

  6. Build the app.

  7. Deploy and run the app on your device.
    You can use the Deploying an App with Visual Studio for guidance.

  8. Same as the original SPI Display sample, you can now send text to the SPI display when you enter it using a keyboard attached to the device running the app.

SPI running

Sample code

Language: C#/C++

Tags: c#, c++, intermediate, lightning, spi

Verified to work with:
Windows 10 IoT Core: Version 10.0.15063.0
Visual Studio 2017
Windows SDK: Version 15063
(Included with Visual Studio)


Edit this page on GitHub


Was this page helpful?
Additional feedback?
Thank you! We appreciate your feedback.
Follow us