C# SPI programming - Sample code for SPI programming


1. Download and open C# project file
  1. Download source code.

    SPI.Sample1.zip



2. C# source code for SPI.Sample1
  1. Program.cs

      
    
    using System;
    
    using Microsoft.SPOT;
    using Microsoft.SPOT.Input;
    using Microsoft.SPOT.Presentation;
    using Microsoft.SPOT.Presentation.Controls;
    using Microsoft.SPOT.Hardware;
    
    using SPL.Script.Engine;
    
    namespace SPLMFConsoleWindow1
    {
        public class Program : Microsoft.SPOT.Application
        {
            public static void Main()
            {
                Program myApplication = new Program();
    
                Window mainWindow = myApplication.CreateWindow();
    
                GPIOButtonInputProvider inputProvider = new GPIOButtonInputProvider(null);
    
                myApplication.Run(mainWindow);
            }
    
            private Window mainWindow;
    
            //##########################  Sample Code  #################################
            private ConsoleWindow _consoleWindow;
    
            private SPI _device = null;
            //##########################################################################
    
            public Window CreateWindow()
            {
                mainWindow = new Window();
                mainWindow.Height = SystemMetrics.ScreenHeight;
                mainWindow.Width = SystemMetrics.ScreenWidth;
    
                //##########################  Sample Code  #################################
                Font font = Resources.GetFont(Resources.FontResources.small);
                _consoleWindow = new ConsoleWindow(font);
    
                mainWindow.Child = _consoleWindow;
                //##########################################################################
    
                mainWindow.AddHandler(Buttons.ButtonUpEvent, new ButtonEventHandler(OnButtonUp), false);
    
                mainWindow.Visibility = Visibility.Visible;
    
                Buttons.Focus(mainWindow);
    
                //##########################  Sample Code  #################################
                _consoleWindow.WriteLine("Please push any button!.");
                _consoleWindow.WriteLine("Select button will clear screen.");
                //##########################################################################
    
    
    
                //##########################  Sample Code  #################################
                SPI.Configuration config = new SPI.Configuration(
                            Cpu.Pin.GPIO_Pin11,
                            false,
                            1,
                            1,
                            true,
                            false,
                            15000,
                            SPI.SPI_module.SPI1
                            );
    
                _device = new SPI(config);
                //##########################################################################
    
    
                return mainWindow;
            }
    
            private void OnButtonUp(object sender, ButtonEventArgs e)
            {
                Debug.Print(e.Button.ToString());
    
                //##########################  Sample Code  #################################
                if (e.Button == Microsoft.SPOT.Hardware.Button.VK_SELECT)
                {
                    _consoleWindow.ClearConsole();
                }
                else if (e.Button == Microsoft.SPOT.Hardware.Button.VK_UP)
                {
                    ushort[] wBuff = new ushort[1] { 0xA0A1 };
                    ushort[] rBuff = new ushort[1];
    
                    _device.WriteRead(wBuff, rBuff);
    
                    _consoleWindow.WriteLine("Received data is " + Util.UShortsToHex(rBuff));
                }
                else if (e.Button == Microsoft.SPOT.Hardware.Button.VK_DOWN)
                {
                    ushort[] wBuff = new ushort[1] { 0xB0B1 };
    
                    _device.Write(wBuff);
    
                    _consoleWindow.WriteLine("SPI device write");
                }
                //##########################################################################
            }
        }
    }
    
    
      



3. Execute sample code
  1. Open "Properties" page of C# project.

  2. Choose ".NET Micro Framework" tab.

  3. Select "SPLMFConsoleEmulator" from the emulator list.



  4. Press "F5" key to run sample code.

  5. Click Down-Button to send data through SPI bus.




4. Read data from SPI bus
  1. Before read data from SPI bus, you need to prepare emulator script to react for the read request.

  2. Select "Emulator" tab on the emulator.

  3. Add below script on the editor.

      
    BindSPIComponent  
    	/Id:spi1
    	/Procedure_OnWrite:proc1
    
    procedure proc1
    
    	print "OnReadRequested: " + value.Length + " bytes"
    	
    	data = Util.ToUShort(0xA1A2)
    	
    	spi1.SetReadBuffer(data)
    end
    
      




  4. Press "F5" key or click "Execute Script on the Emulator".

  5. Now, click Up-Button on the emulator, then you can see the result of read via SPI bus.