MCP2221 HID Library  1.0.4
libmcp2221.h
Go to the documentation of this file.
1 /*
2  * Project: MCP2221 HID Library
3  * Author: Zak Kemble, contact@zakkemble.co.uk
4  * Copyright: (C) 2015 by Zak Kemble
5  * License: GNU GPL v3 (see License.txt)
6  * Web: http://blog.zakkemble.co.uk/mcp2221-hid-library/
7  */
8 
9 #ifndef LIBMCP2221_H_
10 #define LIBMCP2221_H_
11 
12 #include <stdint.h>
13 #include <wchar.h>
14 
15 #define MCP2221_STR_LEN 31
16 #define MCP2221_GPIO_COUNT 4
17 #define MCP2221_DAC_MAX 31
18 #define MCP2221_ADC_COUNT 3
20 #define MCP2221_DEFAULT_VID 0x04D8
21 #define MCP2221_DEFAULT_PID 0x00DD
22 #define MCP2221_DEFAULT_MANUFACTURER L"Microchip Technology Inc."
23 #define MCP2221_DEFAULT_PRODUCT L"MCP2221 USB-I2C/UART Combo"
25 #define MCP2221_REPORT_SIZE 64
31 typedef enum
32 {
38 
43 typedef enum
44 {
46  MCP2221_I2C_ADDRNOTFOUND = 37, // WR NACK
48  MCP2221_I2C_UNKNOWN1 = 98 // stop timeout
50 
55 typedef enum
56 {
63 
68 typedef enum
69 {
76 
81 typedef enum
82 {
84  MCP2221_INT_TRIG_RISING = 0b00001000,
85  MCP2221_INT_TRIG_FALLING = 0b00000010,
86  MCP2221_INT_TRIG_BOTH = 0b00001010 // TODO maybe just OR the 2 values above?
88 
94 typedef enum
95 {
103 
104 #define MCP2221_GPIO_MODE_SSPND MCP2221_GPIO_MODE_DEDI
105 #define MCP2221_GPIO_MODE_ADC MCP2221_GPIO_MODE_ALT1
106 #define MCP2221_GPIO_MODE_DAC MCP2221_GPIO_MODE_ALT2
107 #define MCP2221_GPIO_MODE_IOC MCP2221_GPIO_MODE_ALT3
113 typedef enum
114 {
119 
124 typedef enum
125 {
130 
135 typedef enum
136 {
140 
145 typedef enum
146 {
150 
155 typedef enum
156 {
166 
171 typedef enum
172 {
178 
183 typedef enum
184 {
189 
194 typedef enum
195 {
202 
207 typedef enum
208 {
214 
215 
216 
221 typedef struct{
222  wchar_t manufacturer[MCP2221_STR_LEN];
223  wchar_t product[MCP2221_STR_LEN];
224  wchar_t serial[MCP2221_STR_LEN];
226  char factorySerial[60];
227  char firmware[2];
228  char hardware[2];
229  int vid;
230  int pid;
233  int milliamps;
235 
240 typedef struct{
241  void* handle;
242  char* path;
243  uint8_t gpioCache[MCP2221_GPIO_COUNT];
245 }mcp2221_t;
246 
251 typedef struct{
252  uint8_t SCL;
253  uint8_t SDA;
255 
260 typedef struct{
261  int gpios; // TODO: change to mcp2221_gpio_t ?
266 
267 // Can't return arrays as value from funcs, but can return structs
272 typedef struct{
275 
276 
277 
278 
279 #if defined(__cplusplus)
280 extern "C" {
281 #endif
282 
289 
295 void mcp2221_exit(void);
296 
307 int mcp2221_find(int vid, int pid, wchar_t* manufacturer, wchar_t* product, wchar_t* serial);
308 
314 int mcp2221_sameDevice(mcp2221_t* dev1, mcp2221_t* dev2);
315 
321 mcp2221_t* mcp2221_open(void);
322 
329 
337 mcp2221_t* mcp2221_open_bySerial(wchar_t* serial);
338 
344 void mcp2221_close(mcp2221_t* device);
345 
353 
361 
369 mcp2221_error mcp2221_rawReport(mcp2221_t* device, uint8_t* report);
370 
377 
387 
397 
406 
416 
425 
435 
445 
454 mcp2221_error mcp2221_getDAC(mcp2221_t* device, mcp2221_dac_ref_t* ref, int* value);
455 
464 
473 
482 
491 
499 mcp2221_error mcp2221_readInterrupt(mcp2221_t* device, int* state);
500 
508 
517 
525 mcp2221_error mcp2221_saveManufacturer(mcp2221_t* device, wchar_t* buffer);
526 
534 mcp2221_error mcp2221_saveProduct(mcp2221_t* device, wchar_t* buffer);
535 
543 mcp2221_error mcp2221_saveSerial(mcp2221_t* device, wchar_t* buffer);
544 
553 mcp2221_error mcp2221_saveVIDPID(mcp2221_t* device, int vid, int pid);
554 
562 mcp2221_error mcp2221_saveSerialEnumerate(mcp2221_t* device, int enumerate);
563 
571 mcp2221_error mcp2221_saveMilliamps(mcp2221_t* device, int milliamps);
572 
581 
590 
600 
610 
620 
629 
638 
647 
655 mcp2221_error mcp2221_loadManufacturer(mcp2221_t* device, wchar_t* buffer);
656 
664 mcp2221_error mcp2221_loadProduct(mcp2221_t* device, wchar_t* buffer);
665 
673 mcp2221_error mcp2221_loadSerial(mcp2221_t* device, wchar_t* buffer);
674 
683 mcp2221_error mcp2221_loadVIDPID(mcp2221_t* device, int* vid, int* pid);
684 
692 mcp2221_error mcp2221_loadSerialEnumerate(mcp2221_t* device, int* enumerate);
693 
701 mcp2221_error mcp2221_loadMilliamps(mcp2221_t* device, int* milliamps);
702 
711 
720 
730 
740 
749 mcp2221_error mcp2221_loadDAC(mcp2221_t* device, mcp2221_dac_ref_t* ref, int* value);
750 
759 
768 
777 
789 mcp2221_error mcp2221_i2cWrite(mcp2221_t* device, int address, void* data, int len, mcp2221_i2crw_t type);
790 
801 mcp2221_error mcp2221_i2cRead(mcp2221_t* device, int address, int len, mcp2221_i2crw_t type);
802 
812 mcp2221_error mcp2221_i2cGet(mcp2221_t* device, void* data, int len);
813 
822 
832 
841 mcp2221_error mcp2221_i2cDivider(mcp2221_t* device, int i2cdiv);
842 
851 
852 #if defined(__cplusplus)
853 }
854 #endif
855 
856 #endif /* LIBMCP2221_H_ */
mcp2221_error
Error codes.
Definition: libmcp2221.h:31
mcp2221_error mcp2221_loadGPIOConf(mcp2221_t *device, mcp2221_gpioconfset_t *confSet)
Read GPIO config from flash.
mcp2221_error mcp2221_saveProduct(mcp2221_t *device, wchar_t *buffer)
Save new product USB descriptor string to flash (max 30 characters)
Definition: libmcp2221.h:116
Definition: libmcp2221.h:175
mcp2221_error mcp2221_getClockOut(mcp2221_t *device, mcp2221_clkdiv_t *div, mcp2221_clkduty_t *duty)
Get the current clock output divider (SRAM)
Definition: libmcp2221.h:59
void * handle
Definition: libmcp2221.h:241
mcp2221_error mcp2221_saveManufacturer(mcp2221_t *device, wchar_t *buffer)
Save new manufacturer USB descriptor string to flash (max 30 characters)
Definition: libmcp2221.h:212
mcp2221_i2crw_t
TODO.
Definition: libmcp2221.h:183
mcp2221_error mcp2221_setGPIOConf(mcp2221_t *device, mcp2221_gpioconfset_t *confSet)
Apply GPIO configuration.
#define MCP2221_GPIO_COUNT
Definition: libmcp2221.h:16
mcp2221_error mcp2221_loadPowerSource(mcp2221_t *device, mcp2221_pwrsrc_t *source)
Read power source from flash.
mcp2221_error mcp2221_loadMilliamps(mcp2221_t *device, int *milliamps)
Read current limit from flash.
#define MCP2221_STR_LEN
Definition: libmcp2221.h:15
Definition: libmcp2221.h:164
Definition: libmcp2221.h:99
Definition: libmcp2221.h:70
Definition: libmcp2221.h:173
Definition: libmcp2221.h:157
mcp2221_error mcp2221_rawReport(mcp2221_t *device, uint8_t *report)
Send a custom report, the response is placed in the same buffer.
mcp2221_error mcp2221_getDAC(mcp2221_t *device, mcp2221_dac_ref_t *ref, int *value)
Get the current DAC voltage reference and output value (SRAM)
Definition: libmcp2221.h:199
mcp2221_gpio_mode_t
GPIO modes (check GP DESIGNATION TABLE in the datasheet for what the dedicated and alternative functi...
Definition: libmcp2221.h:94
mcp2221_error mcp2221_i2cRead(mcp2221_t *device, int address, int len, mcp2221_i2crw_t type)
Perform an I2C read.
int mcp2221_sameDevice(mcp2221_t *dev1, mcp2221_t *dev2)
TODO.
mcp2221_error mcp2221_saveSerial(mcp2221_t *device, wchar_t *buffer)
Save new serial USB descriptor string to flash (max 30 characters)
mcp2221_gpio_t
GPIO pins.
Definition: libmcp2221.h:207
mcp2221_gpio_mode_t mode
Definition: libmcp2221.h:262
Definition: libmcp2221.h:101
mcp2221_error mcp2221_loadDAC(mcp2221_t *device, mcp2221_dac_ref_t *ref, int *value)
Read DAC settings from flash.
mcp2221_error mcp2221_getGPIO(mcp2221_t *device, mcp2221_gpioconfset_t *confGet)
Get the current GPIO configuration (SRAM)
mcp2221_error mcp2221_i2cCancel(mcp2221_t *device)
TODO.
Definition: libmcp2221.h:196
mcp2221_pwrsrc_t
Power source.
Definition: libmcp2221.h:135
mcp2221_usbinfo_t usbInfo
Definition: libmcp2221.h:244
mcp2221_gpioconfset_t mcp2221_GPIOConfInit(void)
TODO.
mcp2221_gpio_value_t value
Definition: libmcp2221.h:264
mcp2221_error mcp2221_setClockOut(mcp2221_t *device, mcp2221_clkdiv_t div, mcp2221_clkduty_t duty)
Set the clock reference output divider and duty cycle (SRAM)
mcp2221_error mcp2221_loadADC(mcp2221_t *device, mcp2221_adc_ref_t *ref)
Read ADC settings from flash.
GPIO configuration.
Definition: libmcp2221.h:260
Definition: libmcp2221.h:35
uint8_t factorySerialLen
Definition: libmcp2221.h:225
Definition: libmcp2221.h:126
mcp2221_error mcp2221_getInterrupt(mcp2221_t *device, mcp2221_int_trig_t *trig)
Get the current interrupt trigger mode (SRAM)
mcp2221_error mcp2221_readADC(mcp2221_t *device, int values[MCP2221_ADC_COUNT])
Read ADC values.
void mcp2221_close(mcp2221_t *device)
Close device.
Definition: libmcp2221.h:186
Definition: libmcp2221.h:198
mcp2221_error mcp2221_reset(mcp2221_t *device)
Perform a reset of the device.
int milliamps
Definition: libmcp2221.h:233
mcp2221_error mcp2221_i2cDivider(mcp2221_t *device, int i2cdiv)
TODO.
Definition: libmcp2221.h:162
Definition: libmcp2221.h:148
Definition: libmcp2221.h:158
Definition: libmcp2221.h:137
mcp2221_error mcp2221_saveGPIOConf(mcp2221_t *device, mcp2221_gpioconfset_t *confSet)
Save GPIO configuration to flash.
Definition: libmcp2221.h:34
Definition: libmcp2221.h:46
mcp2221_error mcp2221_setADC(mcp2221_t *device, mcp2221_adc_ref_t ref)
Set the ADC voltage reference (SRAM)
mcp2221_error mcp2221_setDAC(mcp2221_t *device, mcp2221_dac_ref_t ref, int value)
Set the DAC voltage reference and output value (SRAM)
mcp2221_error mcp2221_saveClockOut(mcp2221_t *device, mcp2221_clkdiv_t div, mcp2221_clkduty_t duty)
Save clock reference output settings to flash.
Definition: libmcp2221.h:84
Definition: libmcp2221.h:161
Definition: libmcp2221.h:159
Contains enumerated USB info about the device.
Definition: libmcp2221.h:221
Definition: libmcp2221.h:58
Definition: libmcp2221.h:71
Definition: libmcp2221.h:163
Definition: libmcp2221.h:197
Definition: libmcp2221.h:57
Definition: libmcp2221.h:86
char * path
Definition: libmcp2221.h:242
mcp2221_dac_ref_t
Reference voltages for DAC.
Definition: libmcp2221.h:55
Definition: libmcp2221.h:85
Definition: libmcp2221.h:33
mcp2221_error mcp2221_isConnected(mcp2221_t *device)
Check to see if the device is still connected, should return MCP2221_SUCCESS.
int gpios
Definition: libmcp2221.h:261
mcp2221_clkduty_t
Clock reference output duty cycle.
Definition: libmcp2221.h:171
mcp2221_error mcp2221_i2cWrite(mcp2221_t *device, int address, void *data, int len, mcp2221_i2crw_t type)
Perform an I2C write.
mcp2221_i2c_state_t
I2C states (TODO: incomplete)
Definition: libmcp2221.h:43
mcp2221_error mcp2221_readInterrupt(mcp2221_t *device, int *state)
Read interrupt state.
uint8_t SCL
Definition: libmcp2221.h:252
mcp2221_error mcp2221_loadInterrupt(mcp2221_t *device, mcp2221_int_trig_t *trig)
Read interrupt settings from flash.
mcp2221_error mcp2221_init(void)
Initialise, must be called before anything else!
Definition: libmcp2221.h:97
mcp2221_error mcp2221_loadVIDPID(mcp2221_t *device, int *vid, int *pid)
Read VID and PID from flash.
mcp2221_error mcp2221_getADC(mcp2221_t *device, mcp2221_adc_ref_t *ref)
Get the current ADC voltage reference (SRAM)
mcp2221_error mcp2221_i2cReadPins(mcp2221_t *device, mcp2221_i2cpins_t *pins)
Read raw values of I2C pins. Allows using these pins as 2 additional input pins.
Definition: libmcp2221.h:211
TODO.
Definition: libmcp2221.h:272
mcp2221_error mcp2221_clearInterrupt(mcp2221_t *device)
Clear interrupt state.
mcp2221_error mcp2221_loadManufacturer(mcp2221_t *device, wchar_t *buffer)
Read manufacturer USB descriptor string from flash.
Definition: libmcp2221.h:160
mcp2221_clkdiv_t
Clock reference output divider from 48MHz.
Definition: libmcp2221.h:155
Definition: libmcp2221.h:174
mcp2221_gpio_direction_t
Direction when pin is set to GPIO mode.
Definition: libmcp2221.h:124
mcp2221_error mcp2221_i2cGet(mcp2221_t *device, void *data, int len)
Get the data that was read.
mcp2221_error mcp2221_saveMilliamps(mcp2221_t *device, int milliamps)
Set USB current limit.
Definition: libmcp2221.h:176
mcp2221_error mcp2221_saveInterrupt(mcp2221_t *device, mcp2221_int_trig_t trig)
Save the interrupt trigger mode to flash.
int pid
Definition: libmcp2221.h:230
mcp2221_error mcp2221_setInterrupt(mcp2221_t *device, mcp2221_int_trig_t trig, int clearInt)
Set the interrupt trigger mode and optionally clear pending interrupt (SRAM)
mcp2221_error mcp2221_saveVIDPID(mcp2221_t *device, int vid, int pid)
Save new VID and PID to flash.
#define MCP2221_ADC_COUNT
Definition: libmcp2221.h:18
Definition: libmcp2221.h:47
Definition: libmcp2221.h:96
mcp2221_int_trig_t
Trigger modes for interrupt.
Definition: libmcp2221.h:81
mcp2221_error mcp2221_loadSerial(mcp2221_t *device, wchar_t *buffer)
Read serial USB descriptor string from flash.
mcp2221_t * mcp2221_open(void)
Open first MCP2221 device found.
mcp2221_error mcp2221_savePolarity(mcp2221_t *device, mcp2221_dedipin_t pin, int polarity)
Save polarity of dedicated pin functions to flash.
int vid
Definition: libmcp2221.h:229
mcp2221_error mcp2221_loadProduct(mcp2221_t *device, wchar_t *buffer)
Read product USB descriptor string from flash.
Definition: libmcp2221.h:83
mcp2221_error mcp2221_savePowerSource(mcp2221_t *device, mcp2221_pwrsrc_t source)
Set power source.
mcp2221_error mcp2221_saveSerialEnumerate(mcp2221_t *device, int enumerate)
Enable/disable enumerating with serial number.
int mcp2221_find(int vid, int pid, wchar_t *manufacturer, wchar_t *product, wchar_t *serial)
Find all HIDs matching the supplied parameters, must be called before attempting to open a device...
mcp2221_pwrsrc_t powerSource
Definition: libmcp2221.h:231
mcp2221_dedipin_t
Used to select which dedicated pin to operate on (only used for setting/getting polarity) ...
Definition: libmcp2221.h:194
uint8_t SDA
Definition: libmcp2221.h:253
Definition: libmcp2221.h:115
Definition: libmcp2221.h:209
mcp2221_error mcp2221_loadPolarity(mcp2221_t *device, mcp2221_dedipin_t pin, int *polarity)
Read dedicated pin polarity from flash.
mcp2221_error mcp2221_loadSerialEnumerate(mcp2221_t *device, int *enumerate)
Read serial enumeration setting from flash.
Definition: libmcp2221.h:48
mcp2221_error mcp2221_saveADC(mcp2221_t *device, mcp2221_adc_ref_t ref)
Save the ADC voltage reference to flash.
Definition: libmcp2221.h:200
mcp2221_error mcp2221_loadClockOut(mcp2221_t *device, mcp2221_clkdiv_t *div, mcp2221_clkduty_t *duty)
Read clock output settings from flash.
mcp2221_error mcp2221_setGPIO(mcp2221_t *device, mcp2221_gpio_t pins, mcp2221_gpio_value_t value)
Set GPIO pin output values.
Definition: libmcp2221.h:74
Definition: libmcp2221.h:138
mcp2221_error mcp2221_saveRemoteWakeup(mcp2221_t *device, mcp2221_wakeup_t wakeup)
Enable/disable remote host wakeup.
Definition: libmcp2221.h:147
mcp2221_adc_ref_t
Reference voltages for ADC.
Definition: libmcp2221.h:68
Definition: libmcp2221.h:45
mcp2221_error mcp2221_i2cState(mcp2221_t *device, mcp2221_i2c_state_t *state)
TODO.
Definition: libmcp2221.h:60
mcp2221_gpio_direction_t direction
Definition: libmcp2221.h:263
mcp2221_error mcp2221_loadRemoteWakeup(mcp2221_t *device, mcp2221_wakeup_t *wakeup)
Read remote USB host wakeup from flash.
Definition: libmcp2221.h:61
TODO.
Definition: libmcp2221.h:240
Definition: libmcp2221.h:128
mcp2221_wakeup_t
Remote wakeup (wakeup the USB host from sleep mode)
Definition: libmcp2221.h:145
Definition: libmcp2221.h:127
Raw I2C pin values.
Definition: libmcp2221.h:251
Definition: libmcp2221.h:73
void mcp2221_exit(void)
TODO.
mcp2221_error mcp2221_readGPIO(mcp2221_t *device, mcp2221_gpio_value_t values[MCP2221_GPIO_COUNT])
Read GPIO values.
mcp2221_error mcp2221_saveDAC(mcp2221_t *device, mcp2221_dac_ref_t ref, int value)
Save the DAC voltage reference and output value to flash.
Definition: libmcp2221.h:185
mcp2221_t * mcp2221_open_bySerial(wchar_t *serial)
Open device with specified serial.
Definition: libmcp2221.h:98
Definition: libmcp2221.h:36
mcp2221_gpio_value_t
GPIO read values.
Definition: libmcp2221.h:113
Definition: libmcp2221.h:72
mcp2221_wakeup_t remoteWakeup
Definition: libmcp2221.h:232
mcp2221_t * mcp2221_open_byIndex(int idx)
Open device with specified index number (starting from 0 up to however many devices were found) ...
Definition: libmcp2221.h:100
Definition: libmcp2221.h:117
Definition: libmcp2221.h:210
Definition: libmcp2221.h:187