From dff49e1f7c0f414e1fb66fa4373f5c3c2ca9fa67 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 2 Jun 2026 15:54:53 +0000 Subject: [PATCH 1/5] Initial plan From 17ad125fab3cae09e330bdff7497bcce35fc1a76 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 2 Jun 2026 15:57:54 +0000 Subject: [PATCH 2/5] Allow configuring FT4222 I2C master clock frequency --- src/devices/Ft4222/Ft4222Device.cs | 8 +++++++- src/devices/Ft4222/Ft4222I2cBus.cs | 31 ++++++++++++++++++++++++++++-- src/devices/Ft4222/README.md | 8 ++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) 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..eb9504bce4 100644 --- a/src/devices/Ft4222/Ft4222I2cBus.cs +++ b/src/devices/Ft4222/Ft4222I2cBus.cs @@ -18,11 +18,29 @@ namespace Iot.Device.Ft4222 /// internal 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 /// @@ -32,8 +50,16 @@ internal class Ft4222I2cBus : I2cBus /// Create a FT4222 I2C Device /// /// Device information. Use FtCommon.GetDevices to get it. - public Ft4222I2cBus(Ft4222Device deviceInformation) + /// The I2C master clock frequency in kbps. Supported range is 60 to 3400 kbps. + public Ft4222I2cBus(Ft4222Device deviceInformation, uint i2cMasterFrequencyKbps = DefaultI2cMasterFrequencyKbps) { + 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: @@ -68,6 +94,7 @@ public Ft4222I2cBus(Ft4222Device deviceInformation) throw new IOException($"Failed to initialize I2C Master mode on device: {DeviceInformation.Description}, status: {ftStatus}"); } } + } internal void Read(int deviceAddress, Span buffer) { 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```. From 68b667661aa855702c36d197b7d8a09bb8305fbc Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 2 Jun 2026 16:38:51 +0000 Subject: [PATCH 3/5] Fix stray closing brace breaking compilation in Ft4222I2cBus --- src/devices/Ft4222/Ft4222I2cBus.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/devices/Ft4222/Ft4222I2cBus.cs b/src/devices/Ft4222/Ft4222I2cBus.cs index eb9504bce4..39210312a0 100644 --- a/src/devices/Ft4222/Ft4222I2cBus.cs +++ b/src/devices/Ft4222/Ft4222I2cBus.cs @@ -94,7 +94,6 @@ public Ft4222I2cBus(Ft4222Device deviceInformation, uint i2cMasterFrequencyKbps throw new IOException($"Failed to initialize I2C Master mode on device: {DeviceInformation.Description}, status: {ftStatus}"); } } - } internal void Read(int deviceAddress, Span buffer) { From e93fbe7e54096c52c6ee5e2e38208879f1c1e477 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 6 Jun 2026 15:34:33 +0000 Subject: [PATCH 4/5] Adjust Ft4222I2cBus API for review feedback --- src/devices/Ft4222/Ft4222I2cBus.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/devices/Ft4222/Ft4222I2cBus.cs b/src/devices/Ft4222/Ft4222I2cBus.cs index 39210312a0..c56ddcd4bd 100644 --- a/src/devices/Ft4222/Ft4222I2cBus.cs +++ b/src/devices/Ft4222/Ft4222I2cBus.cs @@ -16,7 +16,7 @@ namespace Iot.Device.Ft4222 /// /// FT4222 I2C Device /// - internal class Ft4222I2cBus : I2cBus + public class Ft4222I2cBus : I2cBus { /// /// The default I2C master clock frequency in kbps used when none is specified. @@ -46,12 +46,21 @@ internal class Ft4222I2cBus : I2cBus /// public Ft4222Device DeviceInformation { get; private set; } + /// + /// Create a FT4222 I2C Device + /// + /// Device information. Use FtCommon.GetDevices to get it. + public Ft4222I2cBus(Ft4222Device deviceInformation) + : this(deviceInformation, DefaultI2cMasterFrequencyKbps) + { + } + /// /// Create a FT4222 I2C Device /// /// 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 = DefaultI2cMasterFrequencyKbps) + public Ft4222I2cBus(Ft4222Device deviceInformation, uint i2cMasterFrequencyKbps) { if (i2cMasterFrequencyKbps < MinimumI2cMasterFrequencyKbps || i2cMasterFrequencyKbps > MaximumI2cMasterFrequencyKbps) { From 84a18065cf3649b404cf290134e905136b0e51d8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 6 Jun 2026 15:37:59 +0000 Subject: [PATCH 5/5] Clarify Ft4222I2cBus constructor docs --- src/devices/Ft4222/Ft4222I2cBus.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/devices/Ft4222/Ft4222I2cBus.cs b/src/devices/Ft4222/Ft4222I2cBus.cs index c56ddcd4bd..ebd3063a62 100644 --- a/src/devices/Ft4222/Ft4222I2cBus.cs +++ b/src/devices/Ft4222/Ft4222I2cBus.cs @@ -47,7 +47,7 @@ public class Ft4222I2cBus : I2cBus 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) @@ -56,7 +56,7 @@ public Ft4222I2cBus(Ft4222Device deviceInformation) } /// - /// Create a FT4222 I2C Device + /// 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.