Esempio n. 1
0
        public void Enable()
        {
            lock (IOWarrior.SyncObject)
            {
                if (IOWarrior is IOWarrior56 && ((IOWarrior as IOWarrior56).PWM as PWMInterfaceImplementation).SelectedChannels == PWMConfig.PWM_1To2)
                {
                    throw new InvalidOperationException("SPI cannot be used while PWM_2 is enabled.");
                }

                var report = IOWarrior.NewReport(Pipe.SPECIAL_MODE);

                report[0] = ReportId.SPI_SETUP;
                report[1] = 0x01; // Enable

                if (IOWarrior.Type == IOWarriorType.IOWarrior56)
                {
                    // SPI Mode 0, msb first
                    report[2] = 0x00;

                    // Clock -> 8 MHz
                    report[3] = 0x02;
                }

                if (IOWarrior.Type == IOWarriorType.IOWarrior24)
                {
                    // SPI Mode 0, msb first, 1MBit/sec
                    report[2] = 0x05;
                }

                IOWarrior.WriteReport(report, Pipe.SPECIAL_MODE);
                Enabled = true;
            }
        }
        public byte[] ReadBytes(byte address, int length)
        {
            lock (IOWarrior.SyncObject)
            {
                if (!Enabled)
                {
                    throw new InvalidOperationException("I2C is not enabled.");
                }
                if (address.GetBit(7))
                {
                    throw new ArgumentException("Illegal I2C Address: " + string.Format("0x{0:X2}", address));
                }
                if (length > I2CPacketLength || length < 0)
                {
                    throw new ArgumentException("Data length must be between 0 and " + I2CPacketLength + ".");
                }

                var report = IOWarrior.NewReport(I2CPipe);

                report[0] = ReportId.I2C_READ;

                report[1] = (byte)length; // Amount of bytes to read

                // Write address byte
                report[2] = (byte)(address << 1);
                report[2].SetBit(0, true); // true -> read

                IOWarrior.WriteReport(report, I2CPipe);

                var result = IOWarrior.ReadReport(I2CPipe);

                if (result[0] != ReportId.I2C_READ)
                {
                    if (Debugger.IsAttached)
                    {
                        Debugger.Break();
                    }

                    throw new InvalidOperationException("Recieved wrong packet!");
                }

                if (result[1].GetBit(7))
                {
                    throw new IOException("Error while reading data.");
                }

                if (IOWarrior.Type == IOWarriorType.IOWarrior56 && result[1].GetBit(6))
                {
                    throw new IOException("Error while reading data, arbitration lost.");
                }

                return(result.Skip(2).Take(length).ToArray());
            }
        }
        public void Enable()
        {
            lock (IOWarrior.SyncObject)
            {
                var report = IOWarrior.NewReport(I2CPipe);

                report[0] = ReportId.I2C_SETUP;
                report[1] = 0x01; // Enable

                IOWarrior.WriteReport(report, I2CPipe);
                Enabled = true;
            }
        }
        public void Disable()
        {
            lock (IOWarrior.SyncObject)
            {
                if (!Enabled)
                {
                    return;
                }

                var report = IOWarrior.NewReport(I2CPipe);

                report[0] = ReportId.I2C_SETUP;
                report[1] = 0x00; // Disable

                IOWarrior.WriteReport(report, I2CPipe);
                Enabled = false;
            }
        }
Esempio n. 5
0
        public void Disable()
        {
            lock (IOWarrior.SyncObject)
            {
                if (!Enabled)
                {
                    return;
                }

                var report = IOWarrior.NewReport(Pipe.SPECIAL_MODE);

                report[0] = ReportId.SPI_SETUP;
                report[1] = 0x00; // Disable

                IOWarrior.WriteReport(report, Pipe.SPECIAL_MODE);
                Enabled = false;
            }
        }
        public void WriteBytes(byte address, params byte[] data)
        {
            lock (IOWarrior.SyncObject)
            {
                if (!Enabled)
                {
                    throw new InvalidOperationException("I2C interface is not enabled.");
                }
                if (address.GetBit(7))
                {
                    throw new ArgumentException("Illegal I2C Address: " + string.Format("0x{0:X2}", address));
                }
                if (data.Length > (I2CPacketLength - 1))
                {
                    throw new ArgumentException("Data length must be between 0 and " + (I2CPacketLength - 1) + ".");
                }

                var report = IOWarrior.NewReport(I2CPipe);

                report[0] = ReportId.I2C_WRITE;

                // Write IOW I2C settings
                report[1] = (byte)(1 + data.Length);
                report[1].SetBit(7, true); // Enable start bit
                report[1].SetBit(6, true); // Enable stop bit

                // Write address byte
                report[2] = (byte)(address << 1);
                report[2].SetBit(0, false); // false -> write

                // Write data bytes
                for (int index = 0; index < data.Length; index++)
                {
                    report[3 + index] = data[index];
                }

                IOWarrior.WriteReport(report, I2CPipe);

                var result = IOWarrior.ReadReport(I2CPipe);

                if (result[0] != ReportId.I2C_WRITE)
                {
                    if (Debugger.IsAttached)
                    {
                        Debugger.Break();
                    }

                    throw new InvalidOperationException("Recieved wrong packet!");
                }

                if (result[1].GetBit(7))
                {
                    throw new IOException("Error while writing data.");
                }

                if (IOWarrior.Type == IOWarriorType.IOWarrior56 || IOWarrior.Type == IOWarriorType.IOWarrior28)
                {
                    if (result[1].GetBit(6))
                    {
                        throw new IOException("Error while writing data, arbitration lost.");
                    }
                }
            }
        }
Esempio n. 7
0
        public byte[] TransferBytes(params byte[] data)
        {
            lock (IOWarrior.SyncObject)
            {
                if (!Enabled)
                {
                    throw new InvalidOperationException("SPI interface is not enabled.");
                }
                if (data.Length < 1 || data.Length > SPIPacketLength)
                {
                    throw new ArgumentException("Data length must be between 1 and " + SPIPacketLength + ".");
                }

                var report = IOWarrior.NewReport(Pipe.SPECIAL_MODE);

                report[0] = ReportId.SPI_TRANSFER;

                if (IOWarrior.Type == IOWarriorType.IOWarrior56)
                {
                    // Count
                    report[1] = (byte)data.Length;

                    // Flags -> no DRDY, SS stays not active
                    report[2] = 0x00;

                    // Write data bytes
                    for (int index = 0; index < data.Length; index++)
                    {
                        report[3 + index] = data[index];
                    }
                }

                if (IOWarrior.Type == IOWarriorType.IOWarrior24)
                {
                    // Count & Flags -> no DRDY, SS stays not acive
                    report[1] = (byte)data.Length;

                    // Write data bytes
                    for (int index = 0; index < data.Length; index++)
                    {
                        report[2 + index] = data[index];
                    }
                }

                IOWarrior.WriteReport(report, Pipe.SPECIAL_MODE);

                var result = IOWarrior.ReadReport(Pipe.SPECIAL_MODE);

                if (result[0] != ReportId.SPI_TRANSFER)
                {
                    if (Debugger.IsAttached)
                    {
                        Debugger.Break();
                    }

                    throw new InvalidOperationException("Recieved wrong packet!");
                }

                return(result.Skip(2).Take(result[1]).ToArray());
            }
        }