Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        public byte ReadByte(byte address)
        {
            //Console.WriteLine("    spiBufTx = {0} {1} {2}", CMD_READ, address, 0);
            Marshal.Copy(new byte[] { CMD_READ, address, 0 }, 0, this._txBufferPtr, HardwareSpiDevice.TxRxBufferLength);
            Marshal.Copy(new byte[] { 0, 0, 0 }, 0, this._rxBufferPtr, HardwareSpiDevice.TxRxBufferLength);
            // build the command
            var cmd = SpiDev.SPI_IOC_MESSAGE(1);
            // build the spi transfer structure
            var spi = new SpiDev.spi_ioc_transfer
            {
                tx_buf        = (UInt64)this._txBufferPtr.ToInt64(),
                rx_buf        = (UInt64)this._rxBufferPtr.ToInt64(),
                len           = HardwareSpiDevice.TxRxBufferLength,
                delay_usecs   = this.SpiDelay,
                speed_hz      = this.MaxSpeedHz,
                bits_per_word = (byte)this.BitsPerWord
            };
            // call the native method
            var result = IoCtl.ioctl(this.DeviceHandle, cmd, ref spi);

            if (result < 0)
            {
                var error = Mono.Unix.Native.Stdlib.GetLastError();
            }
            // return the result. every byte transmitted results in a
            // data or dummy byte received, so we have to skip the
            // leading dummy bytes to read out actual data bytes.
            var bufOut = new byte[HardwareSpiDevice.TxRxBufferLength];

            Marshal.Copy(this._txBufferPtr, bufOut, 0, bufOut.Length);
            Marshal.Copy(this._rxBufferPtr, bufOut, 0, bufOut.Length);
            return(bufOut[2]);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Write a value to the SPI bus.
        /// </summary><
        /// <param name="address"></param>
        /// <param name="value"></param>
        public void WriteByte(byte address, byte value)
        {
            byte buffer = SPI_SLAVE_ID;

            buffer |= (SPI_SLAVE_ADDR << 1 & SPI_SLAVE_MSG_END) | SPI_SLAVE_WRITE;
            Marshal.Copy(new byte[] { buffer, address, value }, 0, this._txBufferPtr, HardwareSpiDevice.TxRxBufferLength);
            Marshal.Copy(new byte[] { 0, 0, 0 }, 0, this._rxBufferPtr, HardwareSpiDevice.TxRxBufferLength);
            // build the command
            var cmd = SpiDev.SPI_IOC_MESSAGE(1);
            // build the spi transfer structure
            var spi = new SpiDev.spi_ioc_transfer
            {
                tx_buf        = (UInt64)this._txBufferPtr.ToInt64(),
                rx_buf        = (UInt64)this._rxBufferPtr.ToInt64(),
                len           = HardwareSpiDevice.TxRxBufferLength,
                delay_usecs   = this.SpiDelay,
                speed_hz      = this.MaxSpeedHz,
                bits_per_word = (byte)this.BitsPerWord
            };
            // call the native method
            var result = IoCtl.ioctl(this.DeviceHandle, cmd, ref spi);

            if (result < 0)
            {
                var error = Mono.Unix.Native.Stdlib.GetLastError();
            }
        }