/// <summary> /// Create a FT4222 I2C Device /// </summary> /// <param name="settings">I2C Connection Settings</param> /// <param name="frequencyKbps">The speed of I2C transmission.</param> public Ft4222I2cEx(I2cConnectionSettings settings, uint frequencyKbps = I2cMasterFrequencyKbps) { _settings = settings ?? throw new ArgumentNullException(nameof(settings)); FrequencyKbps = frequencyKbps; // Check device var devInfos = FtCommon.GetDevices(); if (devInfos.Count == 0) { throw new IOException("No FTDI device available"); } // Select the one from bus Id // FT4222 propose depending on the mode multiple interfaces. Only the A is available for I2C or where there is none as it's the only interface var devInfo = devInfos.Where(m => m.Description == "FT4222 A" || m.Description == "FT4222").ToArray(); if ((devInfo.Length == 0) || (devInfo.Length < _settings.BusId)) { throw new IOException($"Can't find a device to open I2C on index {_settings.BusId}"); } DeviceInformation = devInfo[_settings.BusId]; // Open device var ftStatus = FtFunction.FT_OpenEx(DeviceInformation.LocId, FtOpenType.OpenByLocation, out _ftHandle); if (ftStatus != FtStatus.Ok) { throw new IOException($"Failed to open device {DeviceInformation.Description}, status: {ftStatus}"); } // Set the clock FtClockRate ft4222Clock = FtClockRate.Clock24MHz; ftStatus = FtFunction.FT4222_SetClock(_ftHandle, ft4222Clock); if (ftStatus != FtStatus.Ok) { throw new IOException($"Failed set clock rate {ft4222Clock} on device: {DeviceInformation.Description}, status: {ftStatus}"); } // Set the device as I2C Master ftStatus = FtFunction.FT4222_I2CMaster_Init(_ftHandle, frequencyKbps); if (ftStatus != FtStatus.Ok) { throw new IOException($"Failed to initialize I2C Master mode on device: {DeviceInformation.Description}, status: {ftStatus}"); } }
/// <summary> /// Create a FT4222 I2C Device /// </summary> /// <param name="deviceInformation">Device information. Use FtCommon.GetDevices to get it.</param> public Ft4222I2cBus(FtDevice deviceInformation) { switch (deviceInformation.Type) { case FtDeviceType.Ft4222HMode0or2With2Interfaces: case FtDeviceType.Ft4222HMode1or2With4Interfaces: case FtDeviceType.Ft4222HMode3With1Interface: break; default: throw new ArgumentException($"Unknown device type: {deviceInformation.Type}"); } DeviceInformation = deviceInformation; // Open device var ftStatus = FtFunction.FT_OpenEx(DeviceInformation.LocId, FtOpenType.OpenByLocation, out _ftHandle); if (ftStatus != FtStatus.Ok) { throw new IOException($"Failed to open device {DeviceInformation.Description}, status: {ftStatus}"); } // Set the clock FtClockRate ft4222Clock = FtClockRate.Clock24MHz; ftStatus = FtFunction.FT4222_SetClock(_ftHandle, ft4222Clock); if (ftStatus != FtStatus.Ok) { throw new IOException($"Failed set clock rate {ft4222Clock} on device: {DeviceInformation.Description}, status: {ftStatus}"); } // Set the device as I2C Master ftStatus = FtFunction.FT4222_I2CMaster_Init(_ftHandle, I2cMasterFrequencyKbps); if (ftStatus != FtStatus.Ok) { throw new IOException($"Failed to initialize I2C Master mode on device: {DeviceInformation.Description}, status: {ftStatus}"); } }