Skip to content

A .Net Core library to interact with MCP2221 Usb devices

License

Notifications You must be signed in to change notification settings

DerekGn/MCP2221IO

Repository files navigation

Build Status

NuGet Badge

MCP2221IO

A .Net Core library to interact with Usb MCP2221/MCP2221A devices. For example the ADM00559 device.

Installing MCP2221IO

Install the MCP2221IO package via nuget package manager console:

Install-Package MCP2221IO

Or from the dot net command line console

dotnet add package MCP2221IO

Supported Functions

The following is the list of functions supported by the API.

  • Status/Set Parameters
    • Cancel I2C Transfer
    • Set I2C Speed
  • Read Flash Data
    • Read Chip Settings
    • Read GP Settings
    • Read USB Manufacturer Descriptor String
    • Read USB Product Descriptor String
    • Read USB Serial Number Descriptor String
    • Read Chip Factory Serial Number
  • Write Flash Data
    • Write Chip Settings
    • Write GP Settings
    • Write USB Manufacturer Descriptor String
    • Write USB Product Descriptor String
    • Write USB Serial Number Descriptor String
    • Write Chip Factory Serial Number
  • Send Access Flash Password
  • I2C Operations (7 Bit And 10 Bit address)
    • Scan Bus
    • Write Data
    • Write Data Repeated-Start
    • Write Data No Stop
    • Read Data
    • Read Data Repeated-Start
  • Set GPIO (Output And Direction)
    • GPO, GP1, GP2, GP3
  • Get GPIO (Output And Direction)
    • GPO, GP1, GP2, GP3
  • Set SRAM Settings
    • Clock Output Divider Value
    • DAC Voltage Reference
    • Set DAC Output Value
    • ADC Voltage Reference
    • Set Up the Interrupt Detection Mechanism and Clear the Detection Flag
    • GP0, GP1, GP2, GP3 Settings (Output And Designation)
  • Get SRAM Settings
    • CDC Serial Number Enumeration Enable
    • Chip Configuration Security Option
    • Clock Output Divider Value
    • DAC Reference Voltage Option
    • DAC Reference Option
    • Power-up DAC Value
    • Interrupt Detection – Negative Edge
    • Interrupt Detection – Positive Edge
    • ADC Reference Voltage
    • ADC Reference Option
    • USB VID
    • USB PID
    • USB Power Attributes
    • Usb Request Number Of mA
    • Password
  • Reset
  • SmBus
    • SmBusBlockRead
    • SmBusBlockWrite
    • SmBusQuickCommand
    • SmBusReadByte
    • SmBusReadByteCommand
    • SmBusReadIntCommand
    • SmBusReadLongCommand
    • SmBusReadShortCommand
    • SmBusWriteByte
    • SmBusWriteByteCommand
    • SmBusWriteIntCommand
    • SmBusWriteLongCommand
    • SmBusWriteShortCommand

Example code

The MCP2221A Api contains a mix of Read/Write properties and Read Before Write properties. The intent is to allow the code consuming the MCP 2221A Api to control the read write IO to the USB device.

Resolve HID Device Instance

var hidDevice = DeviceList.Local.GetHidDeviceOrNull(Vid, Pid, null, Serial);

if (hidDevice != null)
{
    using HidSharpHidDevice hidSharpHidDevice = new HidSharpHidDevice((ILogger<IHidDevice>)_serviceProvider.GetService(typeof(ILogger<IHidDevice>)), hidDevice);
    using MCP2221IO.Device device = new MCP2221IO.Device((ILogger<IDevice>)_serviceProvider.GetService(typeof(ILogger<IDevice>)), hidSharpHidDevice);

    device.Open();

    result = action(device);
}
else
{
    Console.Error.WriteLine($"Unable to find HID device VID: [0x{Vid:X}] PID: [0x{Vid:X}] SerialNumber: [{Serial}]");
}

Read Write Properties

Read write properties can be simply used as any standard property.

Console.WriteLine($"Usb Manufacture Descriptor: [{device.UsbManufacturerDescriptor}]");

device.UsbManufacturerDescriptor = "Updated";

Read Before Write Methods

To reduce the IO overhead to and from the USB device multiple settings can be set and then applied in a single operation.

// Read the chip settings
device.ReadChipSettings();

// Update  multiple settings
device.ChipSettings.AdcRefOption = AdcRef.Value;

Password password = MCP2221IO.Settings.Password.DefaultPassword;

// Write the CHIP Settings
device.WriteChipSettings(password);

Console.WriteLine("CHIP settings updated");

Synchronous Methods

There a number of synchronous methods that are used to change state of the device or read write data for example I2C and SmBus operations.

// Cancel the I2C bus transfer
device.CancelI2cBusTransfer();

// Set the I2C bus speed
device.SetI2cBusSpeed(int speed);

MCP2221 Console Application

A console application is available from nuget that allows command line access to the functions of a connected MCP2221 HID usb device. Type MCP2221Console -?|-h|--help to see the list of commands for the device.

Installing MCP2221IO.Console

Install the MCP2221IO.Console package via nuget package manager console:

Install-Package MCP2221IO.Console

Or from the dot net command line console

dotnet add package MCP2221IO.Console

Example Console Application

The source code contains an example application for the PModAqs sensor which is based on the CCS811 see here for further information.

The ADM00559 should be configured for 3.3v operation via the jumper selectable power supply. The ADM00559 should be connected to the PMosAqs device in the following way:

ADM00559 J3 PMosAqs
Pin 2 (Vdd) Pin 6 (Vcc)
Pin 3 (Gnd) Pin 5 (Gnd)
Pin 4 (Sda) Pin 4 (Sda)
Pin 5 (Scl) Pin 3 (Scl)