diff --git a/.vs/ProjectSettings.json b/.vs/ProjectSettings.json new file mode 100644 index 0000000..f8b4888 --- /dev/null +++ b/.vs/ProjectSettings.json @@ -0,0 +1,3 @@ +{ + "CurrentProjectSetting": null +} \ No newline at end of file diff --git a/.vs/VSWorkspaceState.json b/.vs/VSWorkspaceState.json new file mode 100644 index 0000000..510fc61 --- /dev/null +++ b/.vs/VSWorkspaceState.json @@ -0,0 +1,8 @@ +{ + "ExpandedNodes": [ + "", + "\\src" + ], + "SelectedNode": "\\src\\Kingsland.PiFaceSharp.sln", + "PreviewInSolutionExplorer": false +} \ No newline at end of file diff --git a/.vs/slnx.sqlite b/.vs/slnx.sqlite new file mode 100644 index 0000000..84ff2fd Binary files /dev/null and b/.vs/slnx.sqlite differ diff --git a/src/.vs/Kingsland.PiFaceSharp/v16/Server/sqlite3/db.lock b/src/.vs/Kingsland.PiFaceSharp/v16/Server/sqlite3/db.lock new file mode 100644 index 0000000..e69de29 diff --git a/src/.vs/Kingsland.PiFaceSharp/v16/Server/sqlite3/storage.ide b/src/.vs/Kingsland.PiFaceSharp/v16/Server/sqlite3/storage.ide new file mode 100644 index 0000000..b40c116 Binary files /dev/null and b/src/.vs/Kingsland.PiFaceSharp/v16/Server/sqlite3/storage.ide differ diff --git a/src/.vs/Kingsland.PiFaceSharp/v16/Server/sqlite3/storage.ide-shm b/src/.vs/Kingsland.PiFaceSharp/v16/Server/sqlite3/storage.ide-shm new file mode 100644 index 0000000..c3d0df5 Binary files /dev/null and b/src/.vs/Kingsland.PiFaceSharp/v16/Server/sqlite3/storage.ide-shm differ diff --git a/src/.vs/Kingsland.PiFaceSharp/v16/Server/sqlite3/storage.ide-wal b/src/.vs/Kingsland.PiFaceSharp/v16/Server/sqlite3/storage.ide-wal new file mode 100644 index 0000000..4afd4c0 Binary files /dev/null and b/src/.vs/Kingsland.PiFaceSharp/v16/Server/sqlite3/storage.ide-wal differ diff --git a/src/Kingsland.PiFaceSharp/Spi/HardwareSpiDevice.cs b/src/Kingsland.PiFaceSharp/Spi/HardwareSpiDevice.cs index 1330006..2cd85fd 100644 --- a/src/Kingsland.PiFaceSharp/Spi/HardwareSpiDevice.cs +++ b/src/Kingsland.PiFaceSharp/Spi/HardwareSpiDevice.cs @@ -14,17 +14,36 @@ public sealed class HardwareSpiDevice : ISpiDevice, IDisposable private const int TxRxBufferLength = 3; private IntPtr _txBufferPtr = IntPtr.Zero; private IntPtr _rxBufferPtr = IntPtr.Zero; + private byte SPI_SLAVE_ID = 0x40; + private const byte SPI_SLAVE_ADDR = 0; + private const byte SPI_SLAVE_MSG_END = 0x0E; + private const byte SPI_SLAVE_READ = 1; + private const byte SPI_SLAVE_WRITE = 0; #endregion #region Constructors - - public HardwareSpiDevice(uint bus, uint chipSelect, string deviceName) + /// + /// + /// + /// + /// + /// + /// + /// PiFace addresses: + /// 1st => 0x40 + /// 2nd => 0x42 + /// 3rd => 0x44 + /// 4th => 0x46 + /// NOTE: Address must be "selected" on PiFace with the address jumpers + /// + public HardwareSpiDevice(uint bus, uint chipSelect, string deviceName, byte slaveId = 0x40) { this.Bus = bus; this.ChipSelect = chipSelect; this.SpiDelay = 0; this.DeviceName = deviceName; + this.SPI_SLAVE_ID = slaveId; } ~HardwareSpiDevice() @@ -86,7 +105,6 @@ private ushort SpiDelay #endregion #region Methods - private void InitTxRxBuffers() { // create unmanaged transmit and receive buffers @@ -111,9 +129,6 @@ private void FreeTxRxBuffers() #region ISpiDevice Interface - private const byte CMD_WRITE = 0x40; - private const byte CMD_READ = 0x41; - public void Open(int flags) { if (this.DeviceHandle != 0) @@ -151,8 +166,10 @@ public void Close() /// public byte ReadByte(byte address) { + byte buffer = SPI_SLAVE_ID; + buffer |= (SPI_SLAVE_ADDR << 1 & SPI_SLAVE_MSG_END) | SPI_SLAVE_READ; //Console.WriteLine(" spiBufTx = {0} {1} {2}", CMD_READ, address, 0); - Marshal.Copy(new byte[] { CMD_READ, address, 0 }, 0, this._txBufferPtr, HardwareSpiDevice.TxRxBufferLength); + Marshal.Copy(new byte[] { buffer, address, 0 }, 0, this._txBufferPtr, HardwareSpiDevice.TxRxBufferLength); Marshal.Copy(new byte[] { 0, 0, 0 }, 0, this._rxBufferPtr, HardwareSpiDevice.TxRxBufferLength); // build the command var cmd = SpiDev.SPI_IOC_MESSAGE(1); @@ -183,12 +200,14 @@ public byte ReadByte(byte address) /// /// Write a value to the SPI bus. - /// + /// < /// /// public void WriteByte(byte address, byte value) { - Marshal.Copy(new byte[] { CMD_WRITE, address, value }, 0, this._txBufferPtr, HardwareSpiDevice.TxRxBufferLength); + byte buffer = SPI_SLAVE_ID; + buffer |= (SPI_SLAVE_ADDR << 1 & SPI_SLAVE_MSG_END) | SPI_SLAVE_WRITE; + Marshal.Copy(new byte[] { buffer, address, value }, 0, this._txBufferPtr, HardwareSpiDevice.TxRxBufferLength); Marshal.Copy(new byte[] { 0, 0, 0 }, 0, this._rxBufferPtr, HardwareSpiDevice.TxRxBufferLength); // build the command var cmd = SpiDev.SPI_IOC_MESSAGE(1);