Permalink
Browse files

Initial commit

  • Loading branch information...
zkemble committed Aug 13, 2015
1 parent a9790a6 commit 58241a5085a835d1926bb4d7463dff2e04eb2c12
Showing with 8,538 additions and 0 deletions.
  1. +2 −0 Changelog.txt
  2. +674 −0 License.txt
  3. BIN MCP2221.pdf
  4. +64 −0 README.md
  5. 0 TODO.txt
  6. +400 −0 eagle/mcp2221.lbr
  7. +954 −0 eagle/mcp2221_tssop.brd
  8. +2,000 −0 eagle/mcp2221_tssop.sch
  9. +40 −0 examples/adc/Makefile
  10. BIN examples/adc/adc
  11. BIN examples/adc/adc.exe
  12. BIN examples/adc/libmcp2221.dll
  13. +131 −0 examples/adc/main.c
  14. +40 −0 examples/bunch_of_stuff/Makefile
  15. BIN examples/bunch_of_stuff/bunch_of_stuff
  16. BIN examples/bunch_of_stuff/bunch_of_stuff.exe
  17. BIN examples/bunch_of_stuff/libmcp2221.dll
  18. +269 −0 examples/bunch_of_stuff/main.c
  19. +39 −0 examples/dac/Makefile
  20. BIN examples/dac/dac
  21. BIN examples/dac/dac.exe
  22. BIN examples/dac/libmcp2221.dll
  23. +106 −0 examples/dac/main.c
  24. BIN examples/dac/main.o
  25. +40 −0 examples/gpio/Makefile
  26. BIN examples/gpio/gpio
  27. BIN examples/gpio/gpio.exe
  28. BIN examples/gpio/libmcp2221.dll
  29. +144 −0 examples/gpio/main.c
  30. +40 −0 examples/i2c/Makefile
  31. BIN examples/i2c/i2c
  32. BIN examples/i2c/i2c.exe
  33. BIN examples/i2c/libmcp2221.dll
  34. +128 −0 examples/i2c/main.c
  35. +40 −0 examples/interrupt/Makefile
  36. BIN examples/interrupt/interrupt
  37. BIN examples/interrupt/interrupt.exe
  38. BIN examples/interrupt/libmcp2221.dll
  39. +103 −0 examples/interrupt/main.c
  40. +849 −0 lib/libmcp2221.h
  41. BIN lib/unix/libmcp2221.a
  42. BIN lib/unix/libmcp2221.so
  43. BIN lib/win/libmcp2221.a
  44. BIN lib/win/libmcp2221.dll
  45. +83 −0 libmcp2221/Makefile
  46. +1,452 −0 libmcp2221/libmcp2221.c
  47. +849 −0 libmcp2221/libmcp2221.h
  48. +10 −0 libmcp2221/win/libmcp2221.dll.manifest
  49. +59 −0 libmcp2221/win/resource.rc
  50. +22 −0 libmcp2221/win/win.h
View
@@ -0,0 +1,2 @@
2015-08-13 (v1.0):
- Initial release
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
Binary file not shown.
View
@@ -0,0 +1,64 @@
# MCP2221 HID Library
This is a library for interfacing with the HID features of the [MCP2221 USB to UART and I2C/SMBus serial converter from Microchip](http://www.microchip.com/wwwproducts/Devices.aspx?product=MCP2221). The converter includes 4 GPIO pins, 3x 10-bit ADCs, 1x 5-bit DAC and more.
Microchip does provide a library for interfacing with the chip, however it is supplied as proprietary DLLs. This project aims to be an open-source and multi-platform alternative.
This library also makes use of [HIDAPI](http://www.signal11.us/oss/hidapi/).
| Feature | Status
| ------- | ------
| ADC | Supported
| DAC | Supported
| GPIO | Supported
| Interrupt input | Supported
| Clock reference output | Mostly supported*
| USB Descriptors (Manufacturer, product, serial, VID, PID) | Supported
| I2C/SMB | Limited support, WIP
| Flash password protection | Not yet implemented
| C++ and C# wrappers | Not yet implemented
*Reading clock out duty cycle is not documented in the datasheet, but the Microchip library seems to support it.
## Documentation
[Doxygen pages](http://zkemble.github.io/libmcp2221/)
Hint:
| Function prefix | Description
| --------------- | ------
| mcp2221_set* | Set SRAM config, this takes immediate effect
| mcp2221_get* | Get SRAM config
| mcp2221_save* | Writes to flash, this setting is used at startup
| mcp2221_load* | Read from flash
| mcp2221_read* | Read ADC/GPIO/interrupt values
## Setting up
### Using pre-built binaries
- Copy `libmcp2221.h` to your compilers include directory (`/usr/include/` on Linux)
- Windows: Copy `./lib/win/libmcp2221.dll` to your compilers lib directory. Each program that uses libmcp2221 will need a copy of `libmcp2221.dll` in the same directory.
- Linux: Copy `./lib/unix/libmcp2221.so` and `./lib/unix/libmcp2221.a` to `/usr/lib/` (these were compiled on Debian 8.1 x64 using the HIDRAW version of HIDAPI)
### Compiling
- Download hid.c for your OS from https://github.com/signal11/hidapi ([Windows](https://raw.githubusercontent.com/signal11/hidapi/master/windows/hid.c) | [Linux (HIDRAW)](https://raw.githubusercontent.com/signal11/hidapi/master/linux/hid.c) | [Linux (LibUSB - this doesn't seem to work at the moment)](https://raw.githubusercontent.com/signal11/hidapi/master/libusb/hid.c) | [Mac](https://raw.githubusercontent.com/signal11/hidapi/master/mac/hid.c)) and [hidapi.h](https://raw.githubusercontent.com/signal11/hidapi/master/hidapi/hidapi.h)
- Place `hid.c` and `hidapi.h` files into the libmcp2221 directory
- Linux HIDAPI requires dev packages for libudev and libusb `apt-get install libudev-dev libusb-1.0-0-dev`
- Run `make`
- Copy `libmcp2221.h` to your compilers include directory (`/usr/include/` on Linux)
- Windows: Copy `libmcp2221.dll` from the bin folder to your compilers lib directory. Each program that uses libmcp2221 will need a copy of `libmcp2221.dll` in the same directory.
- Linux: Copy `libmcp2221.so` and `libmcp2221.a` from the bin folder to `/usr/lib/`
--------
Third party contents are copyrighted by their respective authors.
My productions are published under GNU GPL v3 (see License.txt).
--------
[MCP2221 HID Library](http://blog.zakkemble.co.uk/mcp2221-hid-library/)
--------
Zak Kemble
contact@zakkemble.co.uk
View
No changes.
View

Large diffs are not rendered by default.

Oops, something went wrong.
View

Large diffs are not rendered by default.

Oops, something went wrong.
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -0,0 +1,40 @@
PROJECT=adc
SOURCES= \
main.c
CFLAGS= \
-c \
-Wall \
-Wextra \
-Wstrict-prototypes \
-Wunused-result \
-O3 \
-std=c99 \
-fmessage-length=0
LDFLAGS= \
-s
LDLIBS= \
-lmcp2221
EXECUTABLE=$(PROJECT)
CC=gcc
OBJECTS=$(SOURCES:.c=.o)
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) $(LDLIBS) -o $@
.c.o:
$(CC) $(CFLAGS) $< -o $@
clean:
rm -rf *.o $(EXECUTABLE)
.PHONY: clean all
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
View
@@ -0,0 +1,131 @@
/*
* Project: MCP2221 HID Library
* Author: Zak Kemble, contact@zakkemble.co.uk
* Copyright: (C) 2015 by Zak Kemble
* License: GNU GPL v3 (see License.txt)
* Web: http://blog.zakkemble.co.uk/mcp2221-hid-library/
*/
#ifndef _WIN32
#define _BSD_SOURCE
#include <unistd.h>
#define Sleep(ms) usleep(ms * 1000)
#endif
#include <stdio.h>
#include "../../libmcp2221/win/win.h"
#include "../../libmcp2221/libmcp2221.h"
#include "../../libmcp2221/hidapi.h"
int main(void)
{
puts("Starting!");
mcp2221_init();
// Get list of MCP2221s
printf("Looking for devices... ");
int count = mcp2221_find(MCP2221_DEFAULT_VID, MCP2221_DEFAULT_PID, NULL, NULL, NULL);
printf("found %d devices\n", count);
// Open whatever device was found first
printf("Opening device... ");
mcp2221_t* myDev = mcp2221_open();
if(!myDev)
{
mcp2221_exit();
puts("No MCP2221s found");
getchar();
return 0;
}
puts("done");
// Configure GPIOs
printf("Configuring GPIOs... ");
mcp2221_gpioconfset_t gpioConf = mcp2221_GPIOConfInit();
// Check GP DESIGNATION TABLE in the datasheet for what the dedicated and alternate functions are for each GPIO pin
// Configure GPIO 0 as OUTPUT LOW
gpioConf.conf[0].gpios = MCP2221_GPIO0;
gpioConf.conf[0].mode = MCP2221_GPIO_MODE_GPIO;
gpioConf.conf[0].direction = MCP2221_GPIO_DIR_OUTPUT;
gpioConf.conf[0].value = MCP2221_GPIO_VALUE_LOW;
// Configure GPIO 1, 2 and 3 as alternate function 0, which is ADC
gpioConf.conf[1].gpios = MCP2221_GPIO1 | MCP2221_GPIO2 | MCP2221_GPIO3;
gpioConf.conf[1].mode = MCP2221_GPIO_MODE_ALT0;
// Apply config
mcp2221_setGPIOConf(myDev, &gpioConf);
puts("done");
printf("Setting ADC ref voltage to VDD... ");
mcp2221_setADC(myDev, MCP2221_ADC_REF_VDD);
puts("done");
mcp2221_error res;
while(1)
{
Sleep(200);
puts("~~~~~~~~~~~~~");
// Read ADC values
int adc[MCP2221_ADC_COUNT];
res = mcp2221_readADC(myDev, adc);
// Check for errors
if(res != MCP2221_SUCCESS)
break;
// Show and check values
int valueExceeded = 0;
for(int i=0;i<MCP2221_ADC_COUNT;i++)
{
printf("ADC %d: %d\n", i, adc[i]);
if(adc[i] > 700)
valueExceeded = 1;
}
mcp2221_gpio_value_t value;
if(valueExceeded)
{
puts("Active!");
value = MCP2221_GPIO_VALUE_HIGH;
}
else
value = MCP2221_GPIO_VALUE_LOW;
// Set GPIO0 to HIGH
res = mcp2221_setGPIO(myDev, MCP2221_GPIO0, value);
if(res != MCP2221_SUCCESS)
break;
}
switch(res)
{
case MCP2221_SUCCESS:
puts("No error");
break;
case MCP2221_ERROR:
puts("General error");
break;
case MCP2221_INVALID_ARG:
puts("Invalid argument, probably null pointer");
break;
case MCP2221_ERROR_HID:
printf("USB HID Error: %ls\n", hid_error(myDev->handle));
break;
default:
printf("Unknown error %d\n", res);
break;
}
mcp2221_exit();
return 0;
}
@@ -0,0 +1,40 @@
PROJECT=bunch_of_stuff
SOURCES= \
main.c
CFLAGS= \
-c \
-Wall \
-Wextra \
-Wstrict-prototypes \
-Wunused-result \
-O3 \
-std=c99 \
-fmessage-length=0
LDFLAGS= \
-s
LDLIBS= \
-lmcp2221
EXECUTABLE=$(PROJECT)
CC=gcc
OBJECTS=$(SOURCES:.c=.o)
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) $(LDLIBS) -o $@
.c.o:
$(CC) $(CFLAGS) $< -o $@
clean:
rm -rf *.o $(EXECUTABLE)
.PHONY: clean all
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.

0 comments on commit 58241a5

Please sign in to comment.