diff --git a/src/devices/Ft4222/Ft4222Device.cs b/src/devices/Ft4222/Ft4222Device.cs index 8bd325f20a..f501332afd 100644 --- a/src/devices/Ft4222/Ft4222Device.cs +++ b/src/devices/Ft4222/Ft4222Device.cs @@ -16,6 +16,12 @@ namespace Iot.Device.Ft4222 /// public class Ft4222Device : FtDevice { + /// + /// Gets or sets the I2C master clock frequency in kbps used when creating an I2C bus. + /// Supported range is 60 to 3400 kbps. Defaults to 400 kbps. + /// + public uint I2cBusFrequencyKbps { get; set; } = Ft4222I2cBus.DefaultI2cMasterFrequencyKbps; + /// /// Gets all the FT4222 connected /// @@ -69,7 +75,7 @@ protected override I2cBusManager CreateI2cBusCore(int busNumber, int[]? pins) throw new ArgumentOutOfRangeException(nameof(busNumber)); } - return new I2cBusManager(this, busNumber, pins, new Ft4222I2cBus(this)); + return new I2cBusManager(this, busNumber, pins, new Ft4222I2cBus(this, I2cBusFrequencyKbps)); } /// diff --git a/src/devices/Ft4222/Ft4222I2cBus.cs b/src/devices/Ft4222/Ft4222I2cBus.cs index 31e3682fd8..ebd3063a62 100644 --- a/src/devices/Ft4222/Ft4222I2cBus.cs +++ b/src/devices/Ft4222/Ft4222I2cBus.cs @@ -16,24 +16,59 @@ namespace Iot.Device.Ft4222 /// /// FT4222 I2C Device /// - internal class Ft4222I2cBus : I2cBus + public class Ft4222I2cBus : I2cBus { - private const uint I2cMasterFrequencyKbps = 400; + /// + /// The default I2C master clock frequency in kbps used when none is specified. + /// + public const uint DefaultI2cMasterFrequencyKbps = 400; + + /// + /// The minimum I2C master clock frequency in kbps supported by the FT4222. + /// + public const uint MinimumI2cMasterFrequencyKbps = 60; + + /// + /// The maximum I2C master clock frequency in kbps supported by the FT4222. + /// + public const uint MaximumI2cMasterFrequencyKbps = 3400; private readonly Dictionary _usedAddresses = new Dictionary(); private SafeFtHandle _ftHandle; + /// + /// Gets the I2C master clock frequency in kbps used by this bus. + /// + public uint I2cMasterFrequencyKbps { get; } + /// /// Store the FTDI Device Information /// public Ft4222Device DeviceInformation { get; private set; } /// - /// Create a FT4222 I2C Device + /// Create a FT4222 I2C device using the default I2C master clock frequency. /// /// Device information. Use FtCommon.GetDevices to get it. public Ft4222I2cBus(Ft4222Device deviceInformation) + : this(deviceInformation, DefaultI2cMasterFrequencyKbps) { + } + + /// + /// Create a FT4222 I2C device using the specified I2C master clock frequency. + /// + /// Device information. Use FtCommon.GetDevices to get it. + /// The I2C master clock frequency in kbps. Supported range is 60 to 3400 kbps. + public Ft4222I2cBus(Ft4222Device deviceInformation, uint i2cMasterFrequencyKbps) + { + if (i2cMasterFrequencyKbps < MinimumI2cMasterFrequencyKbps || i2cMasterFrequencyKbps > MaximumI2cMasterFrequencyKbps) + { + throw new ArgumentOutOfRangeException(nameof(i2cMasterFrequencyKbps), $"I2C master clock frequency must be between {MinimumI2cMasterFrequencyKbps} and {MaximumI2cMasterFrequencyKbps} kbps."); + } + + I2cMasterFrequencyKbps = i2cMasterFrequencyKbps; + switch (deviceInformation.Type) { case FtDeviceType.Ft4222HMode0or2With2Interfaces: diff --git a/src/devices/Ft4222/README.md b/src/devices/Ft4222/README.md index 0f1a76ba49..c1c6e9574d 100644 --- a/src/devices/Ft4222/README.md +++ b/src/devices/Ft4222/README.md @@ -94,6 +94,14 @@ if (bme280.TryReadTemperature(out Temperature temperature)) } ``` +By default, the I2C bus is initialized at 400 kbps. You can change the I2C master clock frequency (supported range is 60 to 3400 kbps) by setting `I2cBusFrequencyKbps` on the `Ft4222Device` before creating the bus: + +```csharp +Ft4222Device device = new Ft4222Device(FtCommon.GetDevices()[0]); +device.I2cBusFrequencyKbps = 100; +using I2cBus ftI2c = device.CreateI2cBus(); +``` + ### SPI ```Ft4222Spi``` is the SPI driver which you can pass later to any device requiring SPI or directly use it to send SPI commands. The SPI implementation is fully compatible with ```System.Device.Spi.SpiDevice```.