From f1923b9b59776be0caa0e2780ee919b67382dad3 Mon Sep 17 00:00:00 2001 From: DanielSSilva Date: Tue, 14 Apr 2020 23:48:54 +0100 Subject: [PATCH 1/2] initial commit for possible changes on SPI device and data --- .../SPI/Get/GetSPIDevice.cs | 62 +++++++++++++++++++ src/Microsoft.PowerShell.IoT/SPI/SPIData.cs | 34 +++++++--- src/Microsoft.PowerShell.IoT/SPI/SPIDevice.cs | 34 ++++++++++ .../SPI/Send/SendSPIData.cs | 32 ++++++++-- 4 files changed, 149 insertions(+), 13 deletions(-) create mode 100644 src/Microsoft.PowerShell.IoT/SPI/Get/GetSPIDevice.cs create mode 100644 src/Microsoft.PowerShell.IoT/SPI/SPIDevice.cs diff --git a/src/Microsoft.PowerShell.IoT/SPI/Get/GetSPIDevice.cs b/src/Microsoft.PowerShell.IoT/SPI/Get/GetSPIDevice.cs new file mode 100644 index 0000000..b06ca51 --- /dev/null +++ b/src/Microsoft.PowerShell.IoT/SPI/Get/GetSPIDevice.cs @@ -0,0 +1,62 @@ +using System; +using System.Management.Automation; +using System.Device.Spi; +using System.Device.Gpio; + +[Cmdlet(VerbsCommon.Get, "SPIDevice")] +public class GetSPIDevice : Cmdlet +{ + + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 0)] + public int BusId { get; set; } + + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 1)] + public int ChipSelectLine { get; set; } + + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 2)] + public int Frequency { get; set; } + + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 3)] + public int DataBitLength { get; set;} + + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 4)] + public SpiMode Mode { get; set; } + + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 5)] + public DataFlow DataFlow { get; set; } + + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 6)] + public PinValue ChipSelectLineActiveState { get; set; } + + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)] + public SwitchParameter Raw { get; set; } + + public GetSPIDevice() + { + this.BusId = 0; + this.ChipSelectLine = 0; + this.Frequency = 500_000; // 500 KHz default speed + } + + protected override void ProcessRecord() + { + var settings = new SpiConnectionSettings(this.BusId, this.ChipSelectLine) + { + ClockFrequency = this.Frequency, + DataBitLength = this.DataBitLength, + Mode = this.Mode, + DataFlow = this.DataFlow, + ChipSelectLineActiveState = this.ChipSelectLineActiveState + }; + + SpiDevice spiDevice = SpiDevice.Create(settings); + //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 + // WriteObject(new SPIDevice(spiDevice, this.BusId, this.ChipSelectLine, this.Frequency, + // this.DataBitLength, this.Mode, this.DataFlow, + // this.ChipSelectLineActiveState)); + //Because I'm currently testing this like this : $device = Get-SPIDevice -Frequency 2400000 -Mode Mode0 -DataBitLength 8 -BusId 0 -ChipSelectLine 0 + // I want the returned object to be the spiDevice created. + WriteObject(spiDevice); + + } +} diff --git a/src/Microsoft.PowerShell.IoT/SPI/SPIData.cs b/src/Microsoft.PowerShell.IoT/SPI/SPIData.cs index 27528fb..1a3e8b9 100644 --- a/src/Microsoft.PowerShell.IoT/SPI/SPIData.cs +++ b/src/Microsoft.PowerShell.IoT/SPI/SPIData.cs @@ -1,16 +1,34 @@ -public class SPIData +// public class SPIData +// { +// public int BusId { get; set; } +// public int ChipSelectLine { get; set; } +// public int Frequency { get; set; } +// public byte[] Data { get; set; } +// public byte[] Response { get; set; } + +// public SPIData(int busId, int chipSelectLine, int frequency, byte[] data, byte[] response) +// { +// this.BusId = busId; +// this.ChipSelectLine = chipSelectLine; +// this.Frequency = frequency; +// this.Data = data; +// this.Response = response; +// } +// } + +using System.Device.Gpio; +using System.Device.Spi; + +public class SPIData : SPIDevice { - public int BusId { get; set; } - public int ChipSelectLine { get; set; } - public int Frequency { get; set; } public byte[] Data { get; set; } public byte[] Response { get; set; } - public SPIData(int busId, int chipSelectLine, int frequency, byte[] data, byte[] response) + public SPIData(SpiDevice device, int busId, int chipSelectLine, int frequency, + int dataBitLength, SpiMode mode, DataFlow dataFlow, + PinValue chipSelectLineActiveState, byte[] data, byte[] response + ): base(device, busId, chipSelectLine, frequency, dataBitLength, mode, dataFlow, chipSelectLineActiveState) { - this.BusId = busId; - this.ChipSelectLine = chipSelectLine; - this.Frequency = frequency; this.Data = data; this.Response = response; } diff --git a/src/Microsoft.PowerShell.IoT/SPI/SPIDevice.cs b/src/Microsoft.PowerShell.IoT/SPI/SPIDevice.cs new file mode 100644 index 0000000..84ba5aa --- /dev/null +++ b/src/Microsoft.PowerShell.IoT/SPI/SPIDevice.cs @@ -0,0 +1,34 @@ +using System.Device.Gpio; +using System.Device.Spi; + +public class SPIDevice +{ + internal SpiDevice device = null; + public int BusId { get; set; } + + public int ChipSelectLine { get; set; } + + public int Frequency { get; set; } + + public int DataBitLength { get; set;} + + public SpiMode Mode { get; set; } + + public DataFlow DataFlow { get; set; } + + public PinValue ChipSelectLineActiveState { get; set; } + + public SPIDevice(SpiDevice device, int busId, int chipSelectLine, int frequency, + int dataBitLength, SpiMode mode, DataFlow dataFlow, + PinValue chipSelectLineActiveState) + { + this.device = device; + this.BusId = busId; + this.ChipSelectLine = chipSelectLine; + this.Frequency = frequency; + this.DataBitLength = dataBitLength; + this.Mode = mode; + this.DataFlow = dataFlow; + this.ChipSelectLineActiveState = chipSelectLineActiveState; + } +} diff --git a/src/Microsoft.PowerShell.IoT/SPI/Send/SendSPIData.cs b/src/Microsoft.PowerShell.IoT/SPI/Send/SendSPIData.cs index c23d5ab..a764181 100644 --- a/src/Microsoft.PowerShell.IoT/SPI/Send/SendSPIData.cs +++ b/src/Microsoft.PowerShell.IoT/SPI/Send/SendSPIData.cs @@ -1,6 +1,7 @@ using System; using System.Management.Automation; using System.Device.Spi; +using System.Device.Gpio; [Cmdlet(VerbsCommunications.Send, "SPIData")] public class SendSPIData : Cmdlet @@ -17,6 +18,18 @@ public class SendSPIData : Cmdlet [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 3)] public int Frequency { get; set; } + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 4)] + public int DataBitLength { get; set;} + + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 5)] + public SpiMode Mode { get; set; } + + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 6)] + public DataFlow DataFlow { get; set; } + + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, Position = 7)] + public PinValue ChipSelectLineActiveState { get; set; } + [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)] public SwitchParameter Raw { get; set; } @@ -29,10 +42,16 @@ public SendSPIData() protected override void ProcessRecord() { - var settings = new SpiConnectionSettings(this.BusId, this.ChipSelectLine); - settings.ClockFrequency = this.Frequency; - - using(var spiDevice = SpiDevice.Create(settings)) + var settings = new SpiConnectionSettings(this.BusId, this.ChipSelectLine) + { + ClockFrequency = this.Frequency, + DataBitLength = this.DataBitLength, + Mode = this.Mode, + DataFlow = this.DataFlow, + ChipSelectLineActiveState = this.ChipSelectLineActiveState + }; + + using (var spiDevice = SpiDevice.Create(settings)) { var response = new byte[this.Data.Length]; @@ -44,7 +63,10 @@ protected override void ProcessRecord() } else { - SPIData spiData = new SPIData(this.BusId, this.ChipSelectLine, this.Frequency, this.Data, response); + SPIData spiData = new SPIData(spiDevice, this.BusId, this.ChipSelectLine, this.Frequency, + this.DataBitLength, this.Mode, this.DataFlow, + this.ChipSelectLineActiveState, this.Data, response); + //SPIData spiData = new SPIData(this.BusId, this.ChipSelectLine, this.Frequency, this.Data, response); WriteObject(spiData); } } From d9e6795a12c5688e6759acc35547b20b800acfb6 Mon Sep 17 00:00:00 2001 From: DanielSSilva Date: Sat, 26 Sep 2020 23:15:56 +0100 Subject: [PATCH 2/2] added default values for SPI settings --- src/Microsoft.PowerShell.IoT/SPI/SPIData.cs | 1 - src/Microsoft.PowerShell.IoT/SPI/Send/SendSPIData.cs | 7 ++++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.IoT/SPI/SPIData.cs b/src/Microsoft.PowerShell.IoT/SPI/SPIData.cs index 1a3e8b9..a34a077 100644 --- a/src/Microsoft.PowerShell.IoT/SPI/SPIData.cs +++ b/src/Microsoft.PowerShell.IoT/SPI/SPIData.cs @@ -23,7 +23,6 @@ public class SPIData : SPIDevice { public byte[] Data { get; set; } public byte[] Response { get; set; } - public SPIData(SpiDevice device, int busId, int chipSelectLine, int frequency, int dataBitLength, SpiMode mode, DataFlow dataFlow, PinValue chipSelectLineActiveState, byte[] data, byte[] response diff --git a/src/Microsoft.PowerShell.IoT/SPI/Send/SendSPIData.cs b/src/Microsoft.PowerShell.IoT/SPI/Send/SendSPIData.cs index a764181..c448582 100644 --- a/src/Microsoft.PowerShell.IoT/SPI/Send/SendSPIData.cs +++ b/src/Microsoft.PowerShell.IoT/SPI/Send/SendSPIData.cs @@ -38,6 +38,11 @@ public SendSPIData() this.BusId = 0; this.ChipSelectLine = 0; this.Frequency = 500_000; // 500 KHz default speed + //use the same defaults as .NET core team is using + this.Mode = SpiMode.Mode0; + this.DataBitLength = 8; + this.DataFlow = DataFlow.MsbFirst; + this.ChipSelectLineActiveState = PinValue.Low; } protected override void ProcessRecord() @@ -50,7 +55,7 @@ protected override void ProcessRecord() DataFlow = this.DataFlow, ChipSelectLineActiveState = this.ChipSelectLineActiveState }; - + using (var spiDevice = SpiDevice.Create(settings)) { var response = new byte[this.Data.Length];