.NET library for controlling MCP2221/MCP2221A USB2.0 to I2C/UART Protocol Converter with GPIO
Smdn.Devices.MCP2221 is a .NET library for controlling MCP2221/MCP2221A USB2.0 to I2C/UART Protocol Converter with GPIO. This library enables you to control MCP2221/MCP2221A’s GPIO, I2C interface, and other functionalities via USB-HID interface.
With this library, you can control I2C devices and others devices on any PCs with USB ports. It is not needed the board like the Raspberry Pi or Arduino. Also it is not required to install native device drivers on your system.
See Smdn.Devices.MCP2221 examples.
Smdn.Devices.MCP2221.GpioAdapter provides the MCP2221/MCP2221A adapter interface for System.Device.Gpio. This library enables you to use the many device bindings provided by Iot.Device.Bindings.
See Smdn.Devices.MCP2221.GpioAdapter examples.
01234567
(issue #8))Haven’t tested with the actual MCP2221, but it is expected that works as same as MCP2221A.
Smdn.Devices.MCP2221
Read
/Write
and other command methods
Span<byte>
/Memory<byte>
async
, CancellationToken
ILogger
, Microsoft.Extensions.Logging (example)Predicate<IUsbHidDevice>
Smdn.Devices.MCP2221.GpioAdapter
Firstly, add package Smdn.Devices.MCP2221 to your project.
dotnet add package Smdn.Devices.MCP2221
Nextly, write your codes. The simplest code, blinking the LEDs connected to the GP pins is like below.
using System;
using System.Threading;
using Smdn.Devices.MCP2221;
using var device = MCP2221.Open();
// configure GP0-GP3 as GPIO output
device.GP0.ConfigureAsGPIO(PinMode.Output);
device.GP1.ConfigureAsGPIO(PinMode.Output);
device.GP2.ConfigureAsGPIO(PinMode.Output);
device.GP3.ConfigureAsGPIO(PinMode.Output);
// blink GP0-GP3
foreach (var gp in device.GPs) {
Console.WriteLine($"blink {gp.PinDesignation}");
for (var n = 0; n < 10; n++) {
gp.SetValue(false); Thread.Sleep(100);
gp.SetValue(true); Thread.Sleep(100);
}
}
For detailed instructions, including wiring of the devices and parts, see blink example page.
More examples can be found in following examples directory.
LibUsbDotNet do DllImport-ing a shared library with the filename libusb-1.0.so.0
.
If the libusb’s .so filename installed on your system is different from that, use the NativeLibrary.SetDllImportResolver() to load installed .so file like below.
$ find /lib/ -name "libusb-*.so*"
/lib/x86_64-linux-gnu/libusb-1.0.so.x.y.z
/lib/i386-linux-gnu/libusb-1.0.so.x.y.z
using System.Runtime.InteropServices;
static void Main() {
// libusb.so filename which is installed on your system
const string fileNameLibUsb = "libusb-1.0.so.x.y.z";
NativeLibrary.SetDllImportResolver(
typeof(global::LibUsbDotNet.LibUsb.UsbDevice).Assembly,
(libraryName, assembly, searchPath) => {
if (string.Equals(libraryName, "libusb-1.0.so.0", StringComparison.OrdinalIgnoreCase)) {
if (NativeLibrary.TryLoad(fileNameLibUsb, out var handleOfLibUsb))
return handleOfLibUsb;
}
return IntPtr.Zero;
}
);
// your codes here
︙
︙
}
When using LibUsbDotNet, you need to unbind the devices that are bound to the usbhid driver.
If you got the exception like below, configure the MCP2221/MCP2221A to unbind, and reconnect it again. See udev rule file for detail.
Unhandled exception. Smdn.Devices.MCP2221.DeviceUnavailableException: MCP2221/MCP2221A is not available, not privileged or disconnected.
---> LibUsbDotNet.LibUsb.UsbException: Resource busy
at LibUsbDotNet.LibUsb.ErrorExtensions.ThrowOnError(Error error)
at LibUsbDotNet.LibUsb.UsbDevice.ClaimInterface(Int32 interfaceID)
If you got the exception like below, you need to run as the root user, the command like sudo dotnet run
.
LibUsbDotNet:
Unhandled exception. Smdn.Devices.MCP2221.DeviceUnavailableException: MCP2221/MCP2221A is not available, not privileged or disconnected.
---> LibUsbDotNet.LibUsb.UsbException: Access denied (insufficient permissions)
at LibUsbDotNet.LibUsb.ErrorExtensions.ThrowOnError(Error error)
at LibUsbDotNet.LibUsb.UsbDevice.Open()
HIDSharp:
Unhandled exception. Smdn.Devices.MCP2221.DeviceUnavailableException: MCP2221/MCP2221A is not available, not privileged or disconnected.
---> HidSharp.Exceptions.DeviceIOException: Unable to open HID class device (OK).
at HidSharp.Platform.Linux.LinuxHidStream.DeviceHandleFromPath(String path, HidDevice hidDevice, oflag oflag)
at HidSharp.Platform.Linux.LinuxHidDevice.TryParseReportDescriptor(ReportDescriptor& parser, Byte[]& reportDescriptor)
at HidSharp.Platform.Linux.LinuxHidDevice.RequiresGetInfo()
at HidSharp.Platform.Linux.LinuxHidDevice.OpenDeviceDirectly(OpenConfiguration openConfig)
at HidSharp.Device.OpenDeviceAndRestrictAccess(OpenConfiguration openConfig)
at HidSharp.Device.Open(OpenConfiguration openConfig)
at HidSharp.Device.Open()
at HidSharp.HidDevice.Open()
If you want to give access privileges to a non-root user instead, you can use udev rule file. See 90-MCP2221-HIDSharp.rules or 90-MCP2221-LibUsbDotNet.rules