Skip to content

Commit

Permalink
Merge pull request #409 from WildernessLabs/i2cbus_cleanup
Browse files Browse the repository at this point in the history
I2cBus Cleanup
  • Loading branch information
ctacke authored Dec 4, 2023
2 parents 0b3f81f + dfdad30 commit e0315fd
Showing 1 changed file with 11 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ namespace Meadow.Hardware
/// </summary>
public class I2cBus : II2cBus
{
private readonly bool _showI2cDebug = false;
private readonly SemaphoreSlim _busSemaphore = new SemaphoreSlim(1, 1);
private readonly SemaphoreSlim _busSemaphore = new(1, 1);

private IMeadowIOController IOController { get; }
internal int BusNumber { get; set; } = 1;
Expand All @@ -25,25 +24,14 @@ public class I2cBus : II2cBus
public I2cBusSpeed BusSpeed { get; set; }

/// <summary>
/// Default constructor for the I2cBus class. This is private to prevent the
/// developer from calling it.
/// Default constructor for the I2cBus class. This is private to prevent the developer from calling it.
/// </summary>
private I2cBus(
IMeadowIOController ioController,
IPin clock,
II2cChannelInfo clockChannel,
IPin data,
II2cChannelInfo dataChannel,
I2cBusSpeed busSpeed,
ushort transactionTimeout = 100)
I2cBusSpeed busSpeed)
{
IOController = ioController;
BusSpeed = busSpeed;

#if !DEBUG
// ensure this is off in release (in case a dev sets it to true and forgets during check-in
_showI2cDebug = false;
#endif
}

private void Disable()
Expand Down Expand Up @@ -83,7 +71,7 @@ public static I2cBus From(IMeadowIOController ioController, IPin clock, IPin dat
success &= ioController.DeviceChannelManager.ReservePin(clock, ChannelConfigurationType.I2C).Item1;
success &= ioController.DeviceChannelManager.ReservePin(data, ChannelConfigurationType.I2C).Item1;

return new I2cBus(ioController, clock, clockChannel, data, dataChannel, busSpeed, transactionTimeout);
return new I2cBus(ioController, busSpeed);
}

/// <summary>
Expand Down Expand Up @@ -208,19 +196,14 @@ public unsafe void Exchange(byte peripheralAddress, Span<byte> writeBuffer, Span

private void DecipherI2cError(Nuttx.ErrorCode ec)
{
switch (ec)
throw ec switch
{
case (Nuttx.ErrorCode)125:
throw new NativeException("Communication error. Verify address and that SCL and SDA are not reversed.");
case (Nuttx.ErrorCode)116:
throw new NativeException("Communication error. Verify device is powered and that SCL is Connected.");
case (Nuttx.ErrorCode)112:
throw new NativeException("Communication error. No device found at requested address.");
case Nuttx.ErrorCode.TryAgain:
throw new NativeException("Communication error. Verify SDA Is Connected.");
default:
throw new NativeException($"Communication error. Error code {(int)ec}");
}
(Nuttx.ErrorCode)125 => new NativeException("Communication error. Verify address and that SCL and SDA are not reversed."),
(Nuttx.ErrorCode)116 => new NativeException("Communication error. Verify device is powered and that SCL is Connected."),
(Nuttx.ErrorCode)112 => new NativeException("Communication error. No device found at requested address."),
Nuttx.ErrorCode.TryAgain => new NativeException("Communication error. Verify SDA Is Connected."),
_ => new NativeException($"Communication error. Error code {(int)ec}"),
};
}

/// <inheritdoc/>
Expand Down

0 comments on commit e0315fd

Please sign in to comment.