Giter VIP home page Giter VIP logo

mcp2221a's Introduction

GoDoc

mcp2221a

Go module for the MCP2221A USB to I²C/UART Protocol Converter with GPIO (datasheet)

Features

  • Fully-documented API
  • Compliant Go module (see below: Installation)
  • Supports multiple MCP2221A devices simultaneously
    • Define custom USB device VID/PID
  • GPIO input/output
  • ADC read (10-bit, 3 channels/pins), configurable reference voltage
  • DAC write (5-bit, 2 pins, shared output), configurable reference voltage and default output
  • Dedicated UART/I²C activity LED modes with configurable polarity
  • Rising/falling/both edge interrupt detection
  • Clock output on 1 pin (up to 12 MHz)
  • Save default/power-on configuration to flash memory (GPIO mode/value, DAC output, etc.)
    • Password-protected flash support
    • Configure minimum required operating current (510 mA max)
  • I²C read/write (configurable bit rate, up to 400 kHz)
    • I²C address scanner discovers all slaves on the bus
    • Convenience routines for reading registers from devices with 8-bit and 16-bit subaddressing

Note that UART support is provided natively through the USB interface as a CDC device and is not handled by this module. It should show up in your OS as a regular TTY serial interface (/dev/tty* on Linux/macOS, COM on Windows).

Dependencies

Installation

If you are not using Go modules (or are unsure), just use the go built-in package manager:

go get -u -v github.com/ardnew/mcp2221a

Installation using modules

Either use the built-in package manager as above (and drop the -u):

go get -v github.com/ardnew/mcp2221a

Or simply add the import statement to your Go package's source code:

import (
  // ... other imports ...
  mcp "github.com/ardnew/mcp2221a"
)

The next time you run go build, the appropriate package will be downloaded automatically! Gee whiz!

Examples

See examples for some demo applications:

  • GPIO - classic "Blink" demo, toggles an LED connected to GPIO pin GP0
  • I²C - I²C address scanner, and also reads and prints the "Device ID" register contents from an INA260 power sensor
  • ADC - continuously reads and prints an analog value on GPIO pin GP1
  • DAC - continuously writes and prints an always-incrementing 5-bit value on GPIO pin GP2
  • Flash - prints the USB product descriptors from flash memory, and toggles power-up GPIO output value on pin GP0

Notes

GP operating modes

All of the available operating modes for the general-purpose (GP) pins:

  • GPIO: Operate as a digital input or a digital output pin.
  • SSPND: Reflects the USB state (Suspend/Resume); active-low when Suspend has been issued by the USB host, and driven high on Resume.
    • This lets the application react (e.g enter a low-power mode) when USB communication has been suspended or resumed.
    • The pin value can be inverted (high on Suspend, low on Resume) using the Flash module.
  • USBCFG: Starts out low during power-up/reset and goes high after successfully enumerating on the USB host.
    • The pin will also go low when in Suspend mode and high on Resume.
    • The pin value can be inverted (start high, low after enumeration) using the Flash module.
  • LED_URX,LED_UTX: Indicates UART (Rx/Tx) data being received/transmitted by pulsing the pin high for a few milliseconds.
    • The pin value can be inverted (pulse low on receive/transmit) using the Flash module.
  • LED_I2C: Indicates I²C (Rx and Tx) data being received and transmitted by pulsing the pin high for a few milliseconds.
    • The pin value can be inverted (pulse low on receive and transmit) using the Flash module.
  • CLKR: Digital output, providing a clock signal derived from the device’s internal clock.
    • The clock’s nominal frequency is 12 MHz ± 0.25%.
    • Other clock values and duty cycles can be configured using the Flash module.
  • IOC: Digital input ("Interrupt-on-Change") that is sensitive to rising, falling, or both edges.
    • The desired edge can be configured using the Flash and SRAM modules.
Supported modes

However, only certain pins support each of the operating modes listed above — per the following matrix yanked from the datasheet:

GP0 GP1 GP2 GP3
Default GPIO GPIO GPIO GPIO
Dedicated SSPND CLKR USBCFG LED_I2C
Alt 1 LED_URX ADC1 ADC2 ADC3
Alt 2 -- LED_UTX DAC1 DAC2
Alt 3 -- IOC -- --

No idea why the first row of alternate functions is named Dedicated, that's how they are identified in the datasheet...

Datasheet

Please refer to this before sending me a confusing question:

To get started reading this thing, and easier grokking, note that:

  • The first half of the datasheet (.pdf) defines internal registers and inter-component behaviors which are all but completely transparent to us. This info can be ignored, but be sure to read the front-matter and any component descriptions.
  • The second half of the datasheet (.pdf) defines the USB HID command and response formats. This is the good stuff that pertains to developers of or using this Go module.

Where to get one

Adafruit makes a crazy cheap, snazzy breakout with built-in 3.3V regulator (with VBUS/5V and 3.3V output pins), an I²C Qwiic/Stemma QT connector (as well as the regular SDA/SCL pins), and best of all a USB-C connector as its programming interface:

mcp2221a's People

Contributors

ardnew avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

mcp2221a's Issues

Build Fails on windows

$ go build

github.com/karalabe/hid

In file included from ....\pkg\mod\github.com\karalabe\[email protected]\hid_enabled.go:40:
....\pkg\mod\github.com\karalabe\[email protected]/hidapi/windows/hid.c: In function 'hid_enumerate':
....\pkg\mod\github.com\karalabe\[email protected]/hidapi/windows/hid.c:431:5: warning: 'strncpy' specified bound depends on the length of the source argument [-Wstringop-overflow=]
strncpy(cur_dev->path, str, len+1);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
....\pkg\mod\github.com\karalabe\[email protected]/hidapi/windows/hid.c:429:11: note: length computed here
len = strlen(str);
^~~~~~~~~~~

This looks to be an underlying bug in hidapi.
built on go1.20.3

Mangled VID/PID and permanently locked flash

I took a brand new Adafruit MCP2221A breakout, connected it to my laptop, built and ran the example programs. This was my first successful run:

~/src/mcp2221a/examples/flash$ ./flash 
2021/09/01 17:25:23 mcp2221a v0.3.1
2021/09/01 17:25:23 USB Manufacturer  = "Microchip Technology Inc."
2021/09/01 17:25:23 USB Product       = "MCP2221 USB-I2C/UART Combo"
2021/09/01 17:25:23 USB Serial No     = "xxxxxxxxxx"
2021/09/01 17:25:23 Factory Serial No = "012345"
2021/09/01 17:25:23 setting pin 0 = 0 (Flash, non-volatile)
2021/09/01 17:25:23 Pin[0] = 0
2021/09/01 17:25:25 setting pin 0 = 1 (SRAM, volatile)
2021/09/01 17:25:25 Pin[0] = 1
2021/09/01 17:25:27 resetting device ...
2021/09/01 17:25:28 Pin[0] = 0
2021/09/01 17:25:30 setting pin 0 = 1 (Flash, non-volatile)
2021/09/01 17:25:30 Pin[0] = 1
2021/09/01 17:25:32 setting pin 0 = 0 (SRAM, volatile)
2021/09/01 17:25:32 Pin[0] = 0
2021/09/01 17:25:34 resetting device ...
2021/09/01 17:25:35 Pin[0] = 1

Then I ran a service that we built using the mcp2221a library. This should have streamed ADC values, but instead closed the device without returning any samples:

server: 2021/09/01 17:27:52 ADC.Read(): status(): send(): Write([cmd=0x10]): hid: device closed

This service does the following (error handling etc. eliminated for clarity):

m, err := mcp.New(0, mcp.PID, mcp.VID)

pins := []byte{1,2,3}
for _, pin := range pins {
  m.ADC.FlashConfig(pin, mcp.VRef1p024)
}

for _, pin := range pins {
  m.ADC.Read(pin)
}

So then I tried running the adc sample program, which recognized the device, but failed to open it:

~/src/mcp2221a/examples/adc$ ./adc 
2021/09/01 17:29:49 Attached devices: [{0001:0017:02 1240 221 256  Microchip Technology Inc. MCP2221 USB-I2C/UART Combo 0 0 2}]
2021/09/01 17:29:49 mcp2221a v0.3.1
2021/09/01 17:29:54 Reset(): New([0]): timed out opening USB HID device

At this point, the VID and PID had been altered, but I didn't know it yet:

~/src/mcp2221a/examples/adc$ ./adc 
2021/09/01 17:30:19 Attached devices: []
2021/09/01 17:30:19 Open(): openUSBDevice(): device index 0 out of range [0, -1]

Then I discovered the bad news:

~/src/mcp2221a/examples/flash$ lsusb | fgrep MCP
Bus 001 Device 027: ID 0c0a:0e7c Highpoint Technologies, Inc. MCP2221 USB-I2C/UART Combo

So I altered the PID/VID in the flash example, modified it to change the VID/PID back, and ran it again:

~/src/mcp2221a/examples/flash$ sudo ./flash
2021/09/01 17:50:31 mcp2221a v0.3.1
2021/09/01 17:50:32 USB Manufacturer  = "Microchip Technology Inc."
2021/09/01 17:50:32 USB Product       = "MCP2221 USB-I2C/UART Combo"
2021/09/01 17:50:32 USB Serial No     = "0002600059"
2021/09/01 17:50:32 Factory Serial No = "012345"
2021/09/01 17:50:32 ConfigVIDPID: write(): flash access permanently locked

I bypassed the locked struct member checks and confirmed that attempts to write to flash fail.

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.