Raspberry.IO.InterIntegratedCircuit.I2cDriver.Read C# (CSharp) Method

Read() private method

private Read ( int deviceAddress, byte buffer ) : void
deviceAddress int
buffer byte
return void
        private void Read(int deviceAddress, byte[] buffer)
        {
            this.EnsureDeviceAddress(deviceAddress);

            var dlen = this.bscAddress + (int)Interop.BCM2835_BSC_DLEN;
            var fifo = this.bscAddress + (int)Interop.BCM2835_BSC_FIFO;
            var status = this.bscAddress + (int)Interop.BCM2835_BSC_S;
            var control = this.bscAddress + (int)Interop.BCM2835_BSC_C;

            var remaining = (uint)buffer.Length;
            uint i = 0;

            // Clear FIFO
            WriteUInt32Mask(control, Interop.BCM2835_BSC_C_CLEAR_1, Interop.BCM2835_BSC_C_CLEAR_1);

            // Clear Status
            WriteUInt32(status, Interop.BCM2835_BSC_S_CLKT | Interop.BCM2835_BSC_S_ERR | Interop.BCM2835_BSC_S_DONE);

            // Set Data Length
            WriteUInt32(dlen, (uint)buffer.Length);

            // Start read
            WriteUInt32(control, Interop.BCM2835_BSC_C_I2CEN | Interop.BCM2835_BSC_C_ST | Interop.BCM2835_BSC_C_READ);

            while ((ReadUInt32(status) & Interop.BCM2835_BSC_S_DONE) == 0)
            {
                while ((ReadUInt32(status) & Interop.BCM2835_BSC_S_RXD) != 0)
                {
                    // Read from FIFO, no barrier
                    buffer[i] = (byte)ReadUInt32(fifo);

                    i++;
                    remaining--;
                }

                this.Wait(remaining);
            }

            while (remaining != 0 && (ReadUInt32(status) & Interop.BCM2835_BSC_S_RXD) != 0)
            {
                buffer[i] = (byte)ReadUInt32(fifo);
                i++;
                remaining--;
            }

            if ((SafeReadUInt32(status) & Interop.BCM2835_BSC_S_ERR) != 0) // Received a NACK
                throw new InvalidOperationException("Read operation failed with BCM2835_I2C_REASON_ERROR_NACK status");
            if ((SafeReadUInt32(status) & Interop.BCM2835_BSC_S_CLKT) != 0) // Received Clock Stretch Timeout
                throw new InvalidOperationException("Read operation failed with BCM2835_I2C_REASON_ERROR_CLKT status");
            if (remaining != 0) // Not all data is received
                throw new InvalidOperationException(string.Format("Read operation failed with BCM2835_I2C_REASON_ERROR_DATA status, missing {0} bytes", remaining));
        }

Usage Example

コード例 #1
0
        /// <summary>
        /// Reads the specified number of bytes.
        /// </summary>
        /// <param name="byteCount">The byte count.</param>
        /// <returns>The buffer.</returns>
        public byte[] Read(int byteCount)
        {
            ////var readAction = new I2cReadAction(new byte[byteCount]);

            ////Execute(new I2cTransaction(readAction));

            ////return readAction.Buffer;
            return(driver.Read(deviceAddress, byteCount));
        }