/// <summary>
        /// Connects to an SPI device if it exists.
        /// </summary>
        /// <param name="controllerIndex">Controller index.</param>
        /// <param name="chipSelectLine">Slave Chip Select Line.</param>
        /// <param name="bits">Data length in bits.</param>
        /// <param name="frequency">Frequency in Hz.</param>
        /// <param name="mode">Communication mode, i.e. clock polarity.</param>
        /// <param name="sharingMode">Sharing mode.</param>
        /// <returns>Device when controller and device exist, otherwise null.</returns>
        public static SpiDevice ConnectSpi(int controllerIndex, int chipSelectLine, int frequency, int bits,
                                           SpiMode mode, SpiSharingMode sharingMode = SpiSharingMode.Exclusive)
        {
            // Validate
            if (controllerIndex < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(controllerIndex));
            }

            // Initialize
            Initialize();

            // Get controller (return null when doesn't exist)
            if (Spi.Count < controllerIndex + 1)
            {
                return(null);
            }
            var controller = Spi[controllerIndex];

            // Connect to device and return (if exists)
            var settings = new SpiConnectionSettings(chipSelectLine)
            {
                ClockFrequency = frequency,
                DataBitLength  = bits,
                Mode           = mode,
                SharingMode    = sharingMode
            };

            return(controller.GetDevice(settings));
        }
示例#2
0
 /// <summary>
 /// Construct a copy of an SpiConnectionSettings object.
 /// </summary>
 /// <param name="source">Source object to copy from.</param>
 internal SpiConnectionSettings(SpiConnectionSettings source)
 {
     m_chipSelectionLine = source.m_chipSelectionLine;
     m_dataBitLength     = source.m_dataBitLength;
     m_clockFrequency    = source.m_clockFrequency;
     m_mode        = source.m_mode;
     m_sharingMode = source.m_sharingMode;
 }
 /// <summary>
 /// Construct a copy of an SpiConnectionSettings object.
 /// </summary>
 /// <param name="source">Source object to copy from.</param>
 internal SpiConnectionSettings(SpiConnectionSettings source)
 {
     m_chipSelectionLine = source.m_chipSelectionLine;
     m_dataBitLength = source.m_dataBitLength;
     m_clockFrequency = source.m_clockFrequency;
     m_mode = source.m_mode;
     m_sharingMode = source.m_sharingMode;
 }
示例#4
0
 /// <summary>
 /// Initializes a copy of a <see cref="SpiConnectionSettings"/> object.
 /// </summary>
 /// <param name="value">Object to copy from.</param>
 internal SpiConnectionSettings(SpiConnectionSettings value)
 {
     _csLine         = value._csLine;
     _clockFrequency = value._clockFrequency;
     _databitLength  = value._databitLength;
     _spiMode        = value._spiMode;
     _spiSharingMode = value._spiSharingMode;
     _bitOrder       = value._bitOrder;
 }
示例#5
0
        private async void Initialize(Int32 chipSelect, Int32 speed, SpiMode mode, SpiSharingMode sharingMode = SpiSharingMode.Shared)
        {
            DeviceInformation devInfo = null;

            try
            {
                var settings = new SpiConnectionSettings(chipSelect);
                settings.ClockFrequency = speed;
                settings.Mode           = mode;
                settings.SharingMode    = sharingMode;

                try
                {
                    string aqs = SpiDevice.GetDeviceSelector();                     // Get a selector string that will return all SPI controllers on the system
                    var    dis = await DeviceInformation.FindAllAsync(aqs);         // Find the SPI bus controller devices with our selector string

                    spiDevice = await SpiDevice.FromIdAsync(dis[0].Id, settings);   // Create an SpiDevice with our bus controller and SPI settings

                    devInfo = dis[0];
                }
                catch (Exception e)
                {
                    throw new Exception("SPI Initialization failed. Exception: " + e.Message + "\n\r");
                }

                if (spiDevice == null)
                {
                    throw new Exception("SPI Controller is currently in use by " +
                                        "another application. Please ensure that no other applications are using SPI.");
                }
            }
            catch (Exception ex)
            {
                throw new Exception("SPI Initialization failed. Exception: " + ex.Message + "\n\r");
            }
        }
        /// <summary>
        /// Connects to an SPI device if it exists.
        /// </summary>
        /// <param name="controllerIndex">Controller index.</param>
        /// <param name="chipSelectLine">Slave Chip Select Line.</param>
        /// <param name="bits">Data length in bits.</param>
        /// <param name="frequency">Frequency in Hz.</param>
        /// <param name="mode">Communication mode, i.e. clock polarity.</param>
        /// <param name="sharingMode">Sharing mode.</param>
        /// <returns>Device when controller and device exist, otherwise null.</returns>
        public static SpiDevice ConnectSpi(int controllerIndex, int chipSelectLine, int frequency, int bits, 
            SpiMode mode, SpiSharingMode sharingMode = SpiSharingMode.Exclusive)
        {
            // Validate
            if (controllerIndex < 0) throw new ArgumentOutOfRangeException(nameof(controllerIndex));

            // Initialize
            Initialize();

            // Get controller (return null when doesn't exist)
            if (Spi.Count < controllerIndex + 1)
                return null;
            var controller = Spi[controllerIndex];

            // Connect to device and return (if exists)
            var settings = new SpiConnectionSettings(chipSelectLine)
            {
                ClockFrequency = frequency,
                DataBitLength = bits,
                Mode = mode,
                SharingMode = sharingMode
            };
            return controller.GetDevice(settings);
        }
示例#7
0
        /// <summary>
        /// Creates an instance using the specified I2C device.
        /// </summary>
        /// <param name="busNumber">Bus controller number, zero based.</param>
        /// <param name="chipSelectLine">Slave Chip Select Line.</param>
        /// <param name="mode">Communication mode, i.e. clock polarity.</param>
        /// <param name="dataBitLength">Data length in bits.</param>
        /// <param name="clockFrequency">Frequency in Hz.</param>
        /// <param name="sharingMode">Sharing mode.</param>
        public Px4ioDevice(int busNumber, int chipSelectLine, SpiMode mode, int dataBitLength, int clockFrequency, SpiSharingMode sharingMode)
        {
            // Connect to hardware
            _hardware = SpiExtensions.Connect(busNumber, chipSelectLine, mode, dataBitLength, clockFrequency, sharingMode);
            if (_hardware == null)
            {
                // Initialization error
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                                                                  Resources.Strings.SpiErrorDeviceNotFound, chipSelectLine, busNumber));
            }

            // Initialize configuration
            Read();
        }
示例#8
0
 public void SetSharingMode(SpiSharingMode sharingMode) => spiDevice.ConnectionSettings.SharingMode = sharingMode;
示例#9
0
 public SPI(Int32 chipSelect = 0, Int32 speed = 10000000, SpiMode mode = SpiMode.Mode3, SpiSharingMode sharingMode = SpiSharingMode.Shared)
 {
     Initialize(chipSelect, speed, mode, sharingMode);
 }