See the IO Pins page here for any extensions needed by Visual Studio and to enable the IO pins.

using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Devices.Spi;
	private const string SPI_PORT_NAME = "SPI0";
	private SpiDevice OurSpiPort;
	public bool InitialiseComplete = false;

	//********************************
	//********************************
	//********** INITIALISE **********
	//********************************
	//********************************
	//ChipSelectId: 0 or 1
	//NOTE - THIS IS AN ASYNC METHOD, IF YOU DON'T AWAIT IT WHEN CALLING THEN IT MAY NOT BE COMPELTE WHEN YOU FIRST TRY TO USE THE OTHER IO METHODS)
	public async void Initialise(int ChipSelectId)
	{

		try
		{
			// Create SPI initialization settings
			var settings = new SpiConnectionSettings(ChipSelectId);			//CS Select line
			settings.ClockFrequency = 10000;                    //<<<<<SET BUS SPEED (10000000 = 10MHz - 100nS per bit).  10000 = Is slowest possible ClockFrequency
			settings.Mode = SpiMode.Mode3;

			string spiAqs = SpiDevice.GetDeviceSelector(SPI_PORT_NAME);
			var devicesInfo = await DeviceInformation.FindAllAsync(spiAqs);
			OurSpiPort = await SpiDevice.FromIdAsync(devicesInfo[0].Id, settings);
		}
		catch (Exception e)
		{
			System.Diagnostics.Debug.WriteLine("SPI Initialisation Error", e.Message);
		}
		finally
		{
			InitialiseComplete = true;
		}

		return;
	}
		
	//**************************************
	//**************************************
	//********** READ/WRITE BYTES **********
	//**************************************
	//**************************************
	public void ReadWriteBytes(byte[] TxData, out byte[] RxData)
	{
		RxData = new byte[(TxData.Length)];
		int Count;
		for (Count = 0; Count < RxData.Length; Count++)
			RxData[Count] = 0;

		try
		{
			if (!InitialiseComplete)
				return;

			RxData = new byte[TxData.Length];
			OurSpiPort.TransferFullDuplex(TxData, RxData);
		}
		catch (Exception)
		{
			//System.Diagnostics.Debug.WriteLine("Exception: {0}", e.Message);
			return;
		}

		return;
	}
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *