示例#1
0
        protected Task <double> ReadTemp(byte command)
        {
            return(Task.Run(() =>
            {
                var result = Device.Write(Address, new byte[] { command }, 1, out int transferred, FtI2CTransferOptions.I2C_TRANSFER_OPTIONS_START_BIT);

                if (!Device.Ok(result))
                {
                    throw new TempSensorException("Could not setup Ta read");
                }

                var taData = new byte[3];
                result = Device.Read(Address, taData, taData.Length, out transferred, FtI2CTransferOptions.I2C_TRANSFER_OPTIONS_STOP_BIT | FtI2CTransferOptions.I2C_TRANSFER_OPTIONS_START_BIT);

                if (!Device.Ok(result))
                {
                    throw new TempSensorException("Could not setup Ta read");
                }

                var crc = PEC.Calculate(Address, command, taData[0], taData[1]);
                //if (crc != taData[2])
                //	Console.WriteLine("PEC doesn't match");

                Console.WriteLine($"{command}: {taData[0]},{taData[1]}");

                return ((taData[1] << 8) | taData[0]) * 0.02;
            }));
        }
示例#2
0
        public Task SetAddress(byte address)
        {
            // Sequence for writing a value:
            // 1. Power up device
            // 2. Write 0x0000 into the cell of interest
            // 3. Wait at least 5ms
            // 4. Write the new value
            // 5. Wait at least 5ms
            // 6. Read back and compare if the write was successful
            // 7. Power down (to make sure the changes take place at next power up

            var defaults = FtI2CTransferOptions.I2C_TRANSFER_OPTIONS_START_BIT;

            return(Task.Run(async() =>
            {
                byte eepromLocation = (byte)(0x20 | RegisterAddress.SmbusAddress);
                var pec = PEC.Calculate(0, eepromLocation, 0, 0);
                var data = new byte[] { eepromLocation, 0, 0, pec };

                var result = Device.Write(0, data, data.Length, out int transfered, defaults | FtI2CTransferOptions.I2C_TRANSFER_OPTIONS_STOP_BIT);
                if (result != FtdiMpsseI2CResult.Ok)
                {
                    throw new TempSensorException("Could not write 0 into bus address");
                }
                await Task.Delay(12);

                pec = PEC.Calculate(0, eepromLocation, address, 0);
                data = new byte[] { eepromLocation, address, 0, pec };
                result = Device.Write(0, data, data.Length, out transfered, defaults | FtI2CTransferOptions.I2C_TRANSFER_OPTIONS_STOP_BIT);
                if (!Device.Ok(result))
                {
                    throw new TempSensorException("Could not set new address");
                }
                await Task.Delay(12);

                var readData = new byte[3];
                data = new byte[] { eepromLocation };
                result = Device.Write(0, data, data.Length, out transfered, defaults);
                result = Device.Read(0, readData, 3, out transfered, defaults | FtI2CTransferOptions.I2C_TRANSFER_OPTIONS_STOP_BIT);
                if (!Device.Ok(result))
                {
                    throw new TempSensorException("Could not read I2C address");
                }

                if (readData[0] != address)
                {
                    throw new TempSensorException("Temperature sensor address was not set successfully");
                }

                Address = address;
            }));
        }