Giter VIP home page Giter VIP logo

mcp2221io's Introduction

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)

mcp2221io's People

Contributors

derekgn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mcp2221io's Issues

Smbus read-command

Hi,
This is perhaps more a question than an issue, as I might have misunderstood the usage of the library.

I'm trying to use the smbus commands (read-byte-command and read-int-command) towards a PMBUS-device on address 0x53.
I try to read the 'CAPABILITY' register (0x20) but I always get "Unexpected response code".

Am I doing this wrong?

accordion@A000619:~/net6.0 $ sudo dotnet MCP2221IOConsole.dll smbus read-int-command -ia 0x53 -c 32 [05:20:29.8907447 DBG] Executed [Device].[Open] in [208.514] ms Reading an int from the SmBus device address [Value: 0x0053 Size: SevenBit] [05:20:29.9715280 DBG] Output HID Packet: [00,0x90,0x01,0x00,0xA6,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00] [05:20:29.9873360 DBG] Input HID Packet: [00,0x90,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x1B,0x00,0xEF,0x00,0x10,0x28,0x40,0x60,0x01,0x01,0x00,0x00,0xF1,0x79,0xF0,0x00,0x00,0x00,0x30,0x30,0x0B,0x30,0x2C,0x23,0x1F,0x36,0x04,0x00,0x00,0x26,0x90,0x14,0x41,0x36,0x31,0x32,0xFF,0x03,0xFF,0x03,0xFE,0x03,0x02,0x03,0x92,0x01,0x00,0x00,0x00,0x00] [05:20:29.9903788 ERR] An exception occurred executing [Device].[I2cWriteData] Reason: [Unexpected response code Expected: [0x94] Actual [0x90]] [05:20:29.9914045 ERR] An exception occurred executing [Device].[SmBusReadCommand] Reason: [Unexpected response code Expected: [0x94] Actual [0x90]] [05:20:29.9930222 DBG] Disposing Device An unhandled exception occurred: MCP2221IO.Exceptions.InvalidResponseTypeException: Unexpected response code Expected: [0x94] Actual [0x90] at MCP2221IO.Responses.BaseResponse.Deserialize(Stream stream) in C:\dev\source\MCP2221IO\MCP2221IO\Responses\BaseResponse.cs:line 65 at MCP2221IO.Device.ExecuteCommand[T](ICommand command, Boolean checkResult) in C:\dev\source\MCP2221IO\MCP2221IO\Device.cs:line 778 at MCP2221IO.Device.<>c__DisplayClass92_01.b__0() in C:\dev\source\MCP2221IO\MCP2221IO\Device.cs:line 731
at MCP2221IO.Device.HandleOperationExecution(String className, Action operation, String memberName) in C:\dev\source\MCP2221IO\MCP2221IO\Device.cs:line 796
at MCP2221IO.Device.I2cWriteData[T](CommandCodes commandCode, I2cAddress address, IList1 data) in C:\dev\source\MCP2221IO\MCP2221IO\Device.cs:line 721 at MCP2221IO.Device.<>c__DisplayClass86_0.<SmBusReadCommand>b__0() in C:\dev\source\MCP2221IO\MCP2221IO\Device.cs:line 597 at MCP2221IO.Device.HandleOperationExecution[T](String className, Func1 operation, String memberName) in C:\dev\source\MCP2221IO\MCP2221IO\Device.cs:line 818
at MCP2221IO.Device.SmBusReadCommand(I2cAddress address, Byte command, UInt16 length, Boolean pec) in C:\dev\source\MCP2221IO\MCP2221IO\Device.cs:line 591
at MCP2221IO.Device.SmBusReadIntCommand(I2cAddress address, Byte command, Boolean pec) in C:\dev\source\MCP2221IO\MCP2221IO\Device.cs:line 486
at MCP2221IOConsole.Commands.SmBus.SmBusReadIntCommand.<>c__DisplayClass1_0.b__0(IDevice device) in C:\dev\source\MCP2221IO\MCP2221IOConsole\Commands\SmBus\SmBusReadIntCommand.cs:line 46
at MCP2221IOConsole.Commands.BaseCommand.ExecuteCommand(Func2 action) in C:\dev\source\MCP2221IO\MCP2221IOConsole\Commands\BaseCommand.cs:line 67 accordion@A000619:~/net6.0 $

Any help is appreciated.
Best regards
Daniel

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.