{"id":659,"date":"2016-12-02T19:05:43","date_gmt":"2016-12-02T19:05:43","guid":{"rendered":"https:\/\/ibex.tech\/windows-iot\/?p=219"},"modified":"2016-12-02T19:05:43","modified_gmt":"2016-12-02T19:05:43","slug":"rpi-using-the-spi-interface","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/uwp-programming-in-c\/spi\/rpi-using-the-spi-interface","title":{"rendered":"RPi Using The SPI Interface"},"content":{"rendered":"\n<p> See the\u00a0<a href=\"https:\/\/ibex.tech\/windows-iot\/windows-iot\/programming-in-c\/io-pins\/rpi-io-pins\">IO Pins page here<\/a>\u00a0for any extensions needed by Visual Studio and to enable the IO pins. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Threading.Tasks;\nusing Windows.Devices.Enumeration;\nusing Windows.Devices.Spi;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\tprivate const string SPI_PORT_NAME = \"SPI0\";\n\tprivate SpiDevice OurSpiPort;\n\tpublic bool InitialiseComplete = false;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\t\/\/********************************\n\t\/\/********************************\n\t\/\/********** INITIALISE **********\n\t\/\/********************************\n\t\/\/********************************\n\t\/\/ChipSelectId: 0 or 1\n\t\/\/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)\n\tpublic async void Initialise(int ChipSelectId)\n\t{\n\n\t\ttry\n\t\t{\n\t\t\t\/\/ Create SPI initialization settings\n\t\t\tvar settings = new SpiConnectionSettings(ChipSelectId);\t\t\t\/\/CS Select line\n\t\t\tsettings.ClockFrequency = 10000;                    \/\/&lt;&lt;&lt;&lt;&lt;SET BUS SPEED (10000000 = 10MHz - 100nS per bit).  10000 = Is slowest possible ClockFrequency\n\t\t\tsettings.Mode = SpiMode.Mode3;\n\n\t\t\tstring spiAqs = SpiDevice.GetDeviceSelector(SPI_PORT_NAME);\n\t\t\tvar devicesInfo = await DeviceInformation.FindAllAsync(spiAqs);\n\t\t\tOurSpiPort = await SpiDevice.FromIdAsync(devicesInfo[0].Id, settings);\n\t\t}\n\t\tcatch (Exception e)\n\t\t{\n\t\t\tSystem.Diagnostics.Debug.WriteLine(\"SPI Initialisation Error\", e.Message);\n\t\t}\n\t\tfinally\n\t\t{\n\t\t\tInitialiseComplete = true;\n\t\t}\n\n\t\treturn;\n\t}\n\t\t\n\t\/\/**************************************\n\t\/\/**************************************\n\t\/\/********** READ\/WRITE BYTES **********\n\t\/\/**************************************\n\t\/\/**************************************\n\tpublic void ReadWriteBytes(byte[] TxData, out byte[] RxData)\n\t{\n\t\tRxData = new byte[(TxData.Length)];\n\t\tint Count;\n\t\tfor (Count = 0; Count &lt; RxData.Length; Count++)\n\t\t\tRxData[Count] = 0;\n\n\t\ttry\n\t\t{\n\t\t\tif (!InitialiseComplete)\n\t\t\t\treturn;\n\n\t\t\tRxData = new byte[TxData.Length];\n\t\t\tOurSpiPort.TransferFullDuplex(TxData, RxData);\n\t\t}\n\t\tcatch (Exception)\n\t\t{\n\t\t\t\/\/System.Diagnostics.Debug.WriteLine(\"Exception: {0}\", e.Message);\n\t\t\treturn;\n\t\t}\n\n\t\treturn;\n\t}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>See the\u00a0IO Pins page here\u00a0for any extensions needed by Visual Studio and to enable the IO pins.<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[],"class_list":["post-659","post","type-post","status-publish","format-standard","hentry","category-spi"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/659","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=659"}],"version-history":[{"count":0,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/659\/revisions"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=659"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=659"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=659"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}