Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multiple PiFace #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vs/ProjectSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"CurrentProjectSetting": null
}
8 changes: 8 additions & 0 deletions .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"ExpandedNodes": [
"",
"\\src"
],
"SelectedNode": "\\src\\Kingsland.PiFaceSharp.sln",
"PreviewInSolutionExplorer": false
}
Binary file added .vs/slnx.sqlite
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
37 changes: 28 additions & 9 deletions src/Kingsland.PiFaceSharp/Spi/HardwareSpiDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
/// <summary>
///
/// </summary>
/// <param name="bus"></param>
/// <param name="chipSelect"></param>
/// <param name="deviceName"></param>
/// <param name="slaveId">
/// PiFace addresses:
/// <para>1st => 0x40</para>
/// <para>2nd => 0x42</para>
/// <para>3rd => 0x44</para>
/// <para>4th => 0x46</para>
/// <para>NOTE: Address must be "selected" on PiFace with the address jumpers</para>
///</param>
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()
Expand Down Expand Up @@ -86,7 +105,6 @@ private ushort SpiDelay
#endregion

#region Methods

private void InitTxRxBuffers()
{
// create unmanaged transmit and receive buffers
Expand All @@ -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)
Expand Down Expand Up @@ -151,8 +166,10 @@ public void Close()
/// <returns></returns>
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);
Expand Down Expand Up @@ -183,12 +200,14 @@ public byte ReadByte(byte address)

/// <summary>
/// Write a value to the SPI bus.
/// </summary>
/// </summary><
/// <param name="address"></param>
/// <param name="value"></param>
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);
Expand Down