Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/devices/Ft4222/Ft4222Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ namespace Iot.Device.Ft4222
/// </summary>
public class Ft4222Device : FtDevice
{
/// <summary>
/// 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.
/// </summary>
Comment thread
pgrawehr marked this conversation as resolved.
public uint I2cBusFrequencyKbps { get; set; } = Ft4222I2cBus.DefaultI2cMasterFrequencyKbps;

/// <summary>
/// Gets all the FT4222 connected
/// </summary>
Expand Down Expand Up @@ -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));
}

/// <inheritdoc />
Expand Down
30 changes: 28 additions & 2 deletions src/devices/Ft4222/Ft4222I2cBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,29 @@ namespace Iot.Device.Ft4222
/// </summary>
internal class Ft4222I2cBus : I2cBus
{
private const uint I2cMasterFrequencyKbps = 400;
/// <summary>
/// The default I2C master clock frequency in kbps used when none is specified.
/// </summary>
public const uint DefaultI2cMasterFrequencyKbps = 400;

/// <summary>
/// The minimum I2C master clock frequency in kbps supported by the FT4222.
/// </summary>
public const uint MinimumI2cMasterFrequencyKbps = 60;

/// <summary>
/// The maximum I2C master clock frequency in kbps supported by the FT4222.
/// </summary>
public const uint MaximumI2cMasterFrequencyKbps = 3400;
Comment on lines +24 to +34

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Make the containing class public instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 84a1806: Ft4222I2cBus is now public, so the public frequency constants are part of the public API.


private readonly Dictionary<int, I2cDevice> _usedAddresses = new Dictionary<int, I2cDevice>();
private SafeFtHandle _ftHandle;

/// <summary>
/// Gets the I2C master clock frequency in kbps used by this bus.
/// </summary>
public uint I2cMasterFrequencyKbps { get; }

/// <summary>
/// Store the FTDI Device Information
/// </summary>
Expand All @@ -32,8 +50,16 @@ internal class Ft4222I2cBus : I2cBus
/// Create a FT4222 I2C Device
/// </summary>
/// <param name="deviceInformation">Device information. Use FtCommon.GetDevices to get it.</param>
public Ft4222I2cBus(Ft4222Device deviceInformation)
/// <param name="i2cMasterFrequencyKbps">The I2C master clock frequency in kbps. Supported range is 60 to 3400 kbps.</param>
public Ft4222I2cBus(Ft4222Device deviceInformation, uint i2cMasterFrequencyKbps = DefaultI2cMasterFrequencyKbps)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot To keep this binary compatible, create 2 constructors instead of using a default argument

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 84a1806: the optional parameter was replaced with two constructors so existing callers keep the default-frequency overload without relying on a default argument.

{
if (i2cMasterFrequencyKbps < MinimumI2cMasterFrequencyKbps || i2cMasterFrequencyKbps > MaximumI2cMasterFrequencyKbps)
{
throw new ArgumentOutOfRangeException(nameof(i2cMasterFrequencyKbps), $"I2C master clock frequency must be between {MinimumI2cMasterFrequencyKbps} and {MaximumI2cMasterFrequencyKbps} kbps.");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include this in the next change.

}

I2cMasterFrequencyKbps = i2cMasterFrequencyKbps;

switch (deviceInformation.Type)
{
case FtDeviceType.Ft4222HMode0or2With2Interfaces:
Expand Down
8 changes: 8 additions & 0 deletions src/devices/Ft4222/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, apply this.


```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```.
Expand Down
Loading