Skip to content

Commit f1923b9

Browse files
committed
initial commit for possible changes on SPI device and data
1 parent e90823a commit f1923b9

File tree

4 files changed

+149
-13
lines changed

4 files changed

+149
-13
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Management.Automation;
3+
using System.Device.Spi;
4+
using System.Device.Gpio;
5+
6+
[Cmdlet(VerbsCommon.Get, "SPIDevice")]
7+
public class GetSPIDevice : Cmdlet
8+
{
9+
10+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 0)]
11+
public int BusId { get; set; }
12+
13+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 1)]
14+
public int ChipSelectLine { get; set; }
15+
16+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 2)]
17+
public int Frequency { get; set; }
18+
19+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 3)]
20+
public int DataBitLength { get; set;}
21+
22+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 4)]
23+
public SpiMode Mode { get; set; }
24+
25+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 5)]
26+
public DataFlow DataFlow { get; set; }
27+
28+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 6)]
29+
public PinValue ChipSelectLineActiveState { get; set; }
30+
31+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
32+
public SwitchParameter Raw { get; set; }
33+
34+
public GetSPIDevice()
35+
{
36+
this.BusId = 0;
37+
this.ChipSelectLine = 0;
38+
this.Frequency = 500_000; // 500 KHz default speed
39+
}
40+
41+
protected override void ProcessRecord()
42+
{
43+
var settings = new SpiConnectionSettings(this.BusId, this.ChipSelectLine)
44+
{
45+
ClockFrequency = this.Frequency,
46+
DataBitLength = this.DataBitLength,
47+
Mode = this.Mode,
48+
DataFlow = this.DataFlow,
49+
ChipSelectLineActiveState = this.ChipSelectLineActiveState
50+
};
51+
52+
SpiDevice spiDevice = SpiDevice.Create(settings);
53+
//TODO: This can be done this way if we follow the same logic as in I2C where we access the device by doing device.Device
54+
// WriteObject(new SPIDevice(spiDevice, this.BusId, this.ChipSelectLine, this.Frequency,
55+
// this.DataBitLength, this.Mode, this.DataFlow,
56+
// this.ChipSelectLineActiveState));
57+
//Because I'm currently testing this like this : $device = Get-SPIDevice -Frequency 2400000 -Mode Mode0 -DataBitLength 8 -BusId 0 -ChipSelectLine 0
58+
// I want the returned object to be the spiDevice created.
59+
WriteObject(spiDevice);
60+
61+
}
62+
}

src/Microsoft.PowerShell.IoT/SPI/SPIData.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
1-
public class SPIData
1+
// public class SPIData
2+
// {
3+
// public int BusId { get; set; }
4+
// public int ChipSelectLine { get; set; }
5+
// public int Frequency { get; set; }
6+
// public byte[] Data { get; set; }
7+
// public byte[] Response { get; set; }
8+
9+
// public SPIData(int busId, int chipSelectLine, int frequency, byte[] data, byte[] response)
10+
// {
11+
// this.BusId = busId;
12+
// this.ChipSelectLine = chipSelectLine;
13+
// this.Frequency = frequency;
14+
// this.Data = data;
15+
// this.Response = response;
16+
// }
17+
// }
18+
19+
using System.Device.Gpio;
20+
using System.Device.Spi;
21+
22+
public class SPIData : SPIDevice
223
{
3-
public int BusId { get; set; }
4-
public int ChipSelectLine { get; set; }
5-
public int Frequency { get; set; }
624
public byte[] Data { get; set; }
725
public byte[] Response { get; set; }
826

9-
public SPIData(int busId, int chipSelectLine, int frequency, byte[] data, byte[] response)
27+
public SPIData(SpiDevice device, int busId, int chipSelectLine, int frequency,
28+
int dataBitLength, SpiMode mode, DataFlow dataFlow,
29+
PinValue chipSelectLineActiveState, byte[] data, byte[] response
30+
): base(device, busId, chipSelectLine, frequency, dataBitLength, mode, dataFlow, chipSelectLineActiveState)
1031
{
11-
this.BusId = busId;
12-
this.ChipSelectLine = chipSelectLine;
13-
this.Frequency = frequency;
1432
this.Data = data;
1533
this.Response = response;
1634
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Device.Gpio;
2+
using System.Device.Spi;
3+
4+
public class SPIDevice
5+
{
6+
internal SpiDevice device = null;
7+
public int BusId { get; set; }
8+
9+
public int ChipSelectLine { get; set; }
10+
11+
public int Frequency { get; set; }
12+
13+
public int DataBitLength { get; set;}
14+
15+
public SpiMode Mode { get; set; }
16+
17+
public DataFlow DataFlow { get; set; }
18+
19+
public PinValue ChipSelectLineActiveState { get; set; }
20+
21+
public SPIDevice(SpiDevice device, int busId, int chipSelectLine, int frequency,
22+
int dataBitLength, SpiMode mode, DataFlow dataFlow,
23+
PinValue chipSelectLineActiveState)
24+
{
25+
this.device = device;
26+
this.BusId = busId;
27+
this.ChipSelectLine = chipSelectLine;
28+
this.Frequency = frequency;
29+
this.DataBitLength = dataBitLength;
30+
this.Mode = mode;
31+
this.DataFlow = dataFlow;
32+
this.ChipSelectLineActiveState = chipSelectLineActiveState;
33+
}
34+
}

src/Microsoft.PowerShell.IoT/SPI/Send/SendSPIData.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Management.Automation;
33
using System.Device.Spi;
4+
using System.Device.Gpio;
45

56
[Cmdlet(VerbsCommunications.Send, "SPIData")]
67
public class SendSPIData : Cmdlet
@@ -17,6 +18,18 @@ public class SendSPIData : Cmdlet
1718
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 3)]
1819
public int Frequency { get; set; }
1920

21+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 4)]
22+
public int DataBitLength { get; set;}
23+
24+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 5)]
25+
public SpiMode Mode { get; set; }
26+
27+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 6)]
28+
public DataFlow DataFlow { get; set; }
29+
30+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 7)]
31+
public PinValue ChipSelectLineActiveState { get; set; }
32+
2033
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)]
2134
public SwitchParameter Raw { get; set; }
2235

@@ -29,10 +42,16 @@ public SendSPIData()
2942

3043
protected override void ProcessRecord()
3144
{
32-
var settings = new SpiConnectionSettings(this.BusId, this.ChipSelectLine);
33-
settings.ClockFrequency = this.Frequency;
34-
35-
using(var spiDevice = SpiDevice.Create(settings))
45+
var settings = new SpiConnectionSettings(this.BusId, this.ChipSelectLine)
46+
{
47+
ClockFrequency = this.Frequency,
48+
DataBitLength = this.DataBitLength,
49+
Mode = this.Mode,
50+
DataFlow = this.DataFlow,
51+
ChipSelectLineActiveState = this.ChipSelectLineActiveState
52+
};
53+
54+
using (var spiDevice = SpiDevice.Create(settings))
3655
{
3756
var response = new byte[this.Data.Length];
3857

@@ -44,7 +63,10 @@ protected override void ProcessRecord()
4463
}
4564
else
4665
{
47-
SPIData spiData = new SPIData(this.BusId, this.ChipSelectLine, this.Frequency, this.Data, response);
66+
SPIData spiData = new SPIData(spiDevice, this.BusId, this.ChipSelectLine, this.Frequency,
67+
this.DataBitLength, this.Mode, this.DataFlow,
68+
this.ChipSelectLineActiveState, this.Data, response);
69+
//SPIData spiData = new SPIData(this.BusId, this.ChipSelectLine, this.Frequency, this.Data, response);
4870
WriteObject(spiData);
4971
}
5072
}

0 commit comments

Comments
 (0)