Example #1
0
        /// <summary>
        /// Opens a SPI slave device.
        /// </summary>
        /// <param name="bus">The SPI bus number.</param>
        /// <param name="chip">The SPI chip select number.</param>
        public SpiDevice(int bus, int chip)
        {
            var ret = NativeSpi.Open(bus, chip, out _handle);

            if (ret != Internals.Errors.ErrorCode.None)
            {
                throw ExceptionFactory.CreateException(ret);
            }
        }
Example #2
0
        /// <summary>
        /// Writes the bytes data to the SPI slave device.
        /// </summary>
        /// <param name="data">The data buffer to write.</param>
        public void Write(byte[] data)
        {
            var length = Convert.ToUInt32(data.Length);
            var ret    = NativeSpi.Write(_handle, data, length);

            if (ret != Internals.Errors.ErrorCode.None)
            {
                throw ExceptionFactory.CreateException(ret);
            }
        }
Example #3
0
        /// <summary>
        /// Reads the bytes data from the SPI slave device.
        /// </summary>
        /// <param name="buffer">The Data buffer.</param>
        public void Read(byte[] buffer)
        {
            var length = Convert.ToUInt32(buffer.Length);
            var ret    = NativeSpi.Read(_handle, buffer, length);

            if (ret != Internals.Errors.ErrorCode.None)
            {
                throw ExceptionFactory.CreateException(ret);
            }
        }
Example #4
0
        /// <summary>
        /// Disposes the Spi.
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            NativeSpi.Close(_handle);
            _disposed = true;
        }
Example #5
0
        /// <summary>
        /// Exchanges the bytes data to the SPI slave device.
        /// writeBuffer.Length and readBuffer.Length must be equal.
        /// </summary>
        /// <param name="writeBuffer">Array containing data to write to the device.</param>
        /// <param name="readBuffer">Array containing data read from the dievice.</param>
        public void TransferSequential(byte[] writeBuffer, byte[] readBuffer)
        {
            if (writeBuffer.Length != readBuffer.Length)
            {
                throw new Exception("writeBuffer.Length is not equal to readBuffer.Length");
            }

            var buffersLength = Convert.ToUInt32(writeBuffer.Length);

            var ret = NativeSpi.Transfer(_handle, writeBuffer, readBuffer, buffersLength);

            if (ret != Internals.Errors.ErrorCode.None)
            {
                throw ExceptionFactory.CreateException(ret);
            }
        }