-
Notifications
You must be signed in to change notification settings - Fork 624
Make FT4222 I2C master clock frequency configurable #2558
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
dff49e1
17ad125
68b6676
e93fbe7
84a1806
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,24 +16,59 @@ | |
| /// <summary> | ||
| /// FT4222 I2C Device | ||
| /// </summary> | ||
| internal class Ft4222I2cBus : I2cBus | ||
| public 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot Make the containing class public instead.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 84a1806: |
||
|
|
||
| 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> | ||
| public Ft4222Device DeviceInformation { get; private set; } | ||
|
|
||
| /// <summary> | ||
| /// Create a FT4222 I2C Device | ||
| /// Create a FT4222 I2C device using the default I2C master clock frequency. | ||
| /// </summary> | ||
| /// <param name="deviceInformation">Device information. Use FtCommon.GetDevices to get it.</param> | ||
| public Ft4222I2cBus(Ft4222Device deviceInformation) | ||
| : this(deviceInformation, DefaultI2cMasterFrequencyKbps) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Create a FT4222 I2C device using the specified I2C master clock frequency. | ||
| /// </summary> | ||
| /// <param name="deviceInformation">Device information. Use FtCommon.GetDevices to get it.</param> | ||
| /// <param name="i2cMasterFrequencyKbps">The I2C master clock frequency in kbps. Supported range is 60 to 3400 kbps.</param> | ||
| 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."); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
|
@@ -143,7 +178,7 @@ | |
| _ftHandle = null!; | ||
| } | ||
|
|
||
| public override ComponentInformation QueryComponentInformation() | ||
|
Check failure on line 181 in src/devices/Ft4222/Ft4222I2cBus.cs
|
||
| { | ||
| var self = new ComponentInformation(this, "FT4222 I2C Bus driver"); | ||
| self.Properties["BusNo"] = "0"; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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```. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.