/// <summary> /// Initializes a new instance of the <see cref="Windows10SpiDevice"/> class that will use the specified settings to communicate with the SPI device. /// </summary> /// <param name="settings"> /// The connection settings of a device on a SPI bus. /// </param> public Windows10SpiDevice(SpiConnectionSettings settings) { if (settings.DataFlow != DataFlow.MsbFirst || settings.ChipSelectLineActiveState != PinValue.Low) { throw new PlatformNotSupportedException($"Changing {nameof(settings.DataFlow)} or {nameof(settings.ChipSelectLineActiveState)} options is not supported on the current platform."); } _settings = settings; var winSettings = new WinSpi.SpiConnectionSettings(_settings.ChipSelectLine) { Mode = ToWinMode(settings.Mode), DataBitLength = settings.DataBitLength, ClockFrequency = settings.ClockFrequency, }; string busFriendlyName = $"SPI{settings.BusId}"; string deviceSelector = WinSpi.SpiDevice.GetDeviceSelector(busFriendlyName); DeviceInformationCollection deviceInformationCollection = DeviceInformation.FindAllAsync(deviceSelector).WaitForCompletion(); if (deviceInformationCollection.Count == 0) { throw new ArgumentException($"No SPI device exists for bus ID {settings.BusId}.", $"{nameof(settings)}.{nameof(settings.BusId)}"); } _winDevice = WinSpi.SpiDevice.FromIdAsync(deviceInformationCollection[0].Id, winSettings).WaitForCompletion(); }
private static SpiDevice CreateWindows10SpiDevice(SpiConnectionSettings settings) { // If we land in this method it means the console application is running on Windows and targetting net5.0 (without specifying Windows platform) // In order to call WinRT code in net5.0 it is required for the application to target the specific platform // so we throw the bellow exception with a detailed message in order to instruct the consumer on how to move forward. throw new PlatformNotSupportedException(CommonHelpers.GetFormattedWindowsPlatformTargetingErrorMessage(nameof(SpiDevice))); }
private static SpiDevice CreateWindows10SpiDevice(SpiConnectionSettings settings) { // This wrapper is needed to prevent Mono from loading Windows10SpiDevice // which causes all fields to be loaded - one of such fields is WinRT type which does not // exist on Linux which causes TypeLoadException. // Using NoInlining and no explicit type prevents this from happening. return(new Windows10SpiDevice(settings)); }
internal SpiConnectionSettings(SpiConnectionSettings other) { BusId = other.BusId; ChipSelectLine = other.ChipSelectLine; Mode = other.Mode; DataBitLength = other.DataBitLength; ClockFrequency = other.ClockFrequency; DataFlow = other.DataFlow; ChipSelectLineActiveState = other.ChipSelectLineActiveState; }
/// <summary> /// Creates a communications channel to a device on a SPI bus running on the current hardware /// </summary> /// <param name="settings">The connection settings of a device on a SPI bus.</param> /// <returns>A communications channel to a device on a SPI bus running on Windows 10 IoT.</returns> public static SpiDevice Create(SpiConnectionSettings settings) { if (Environment.OSVersion.Platform == PlatformID.Win32NT) { return(CreateWindows10SpiDevice(settings)); } else { return(new UnixSpiDevice(settings)); } }
/// <summary> /// Initializes a new instance of the <see cref="Windows10SpiDevice"/> class that will use the specified settings to communicate with the SPI device. /// </summary> /// <param name="settings"> /// The connection settings of a device on a SPI bus. /// </param> public Windows10SpiDevice(SpiConnectionSettings settings) { if (settings.ChipSelectLineActiveState != PinValue.Low) { throw new PlatformNotSupportedException($"Changing{nameof(settings.ChipSelectLineActiveState)} options is not supported on the current platform."); } if (settings.DataFlow == DataFlow.LsbFirst) { _isInverted = true; } _settings = settings; // -1 means ignore Chip Select Line int chipSelectLine = _settings.ChipSelectLine == -1 ? 0 : _settings.ChipSelectLine; var winSettings = new WinSpi.SpiConnectionSettings(chipSelectLine) { Mode = ToWinMode(settings.Mode), DataBitLength = settings.DataBitLength, ClockFrequency = settings.ClockFrequency, }; string busFriendlyName = $"SPI{settings.BusId}"; string deviceSelector = WinSpi.SpiDevice.GetDeviceSelector(busFriendlyName); DeviceInformationCollection?deviceInformationCollection = DeviceInformation.FindAllAsync(deviceSelector).WaitForCompletion(); if (deviceInformationCollection is null || deviceInformationCollection.Count == 0) { throw new ArgumentException($"No SPI device exists for bus ID {settings.BusId}.", $"{nameof(settings)}.{nameof(settings.BusId)}"); } WinSpi.SpiDevice?winDevice = WinSpi.SpiDevice.FromIdAsync(deviceInformationCollection[0].Id, winSettings).WaitForCompletion(); if (winDevice is null) { throw new Exception("A SPI device could not be found."); } _winDevice = winDevice; }
/// <summary> /// Initializes a new instance of the <see cref="UnixSpiDevice"/> class that will use the specified settings to communicate with the SPI device. /// </summary> /// <param name="settings"> /// The connection settings of a device on a SPI bus. /// </param> public UnixSpiDevice(SpiConnectionSettings settings) { _settings = settings; DevicePath = DefaultDevicePath; }