MCP2210 Library
An Open Source MCP2210 Library for Linux.
|
00001 00019 #ifndef MCP2210_H 00020 #define MCP2210_H 00021 00022 #include <cstdlib> 00023 #include <stdio.h> 00024 #include <wchar.h> 00025 #include <string.h> 00026 #include <stdlib.h> 00027 #include <unistd.h> 00028 #include <stdint.h> 00029 00030 #include "hidapi.h" 00031 00032 typedef uint8_t byte; 00033 00034 #define MCP2210_VID 0x04d8 00035 #define MCP2210_PID 0x00de 00036 00040 #define GPIO_DIRECTION_OUTPUT 0 00041 #define GPIO_DIRECTION_INPUT 1 00042 00048 #define GP_PIN_DESIGNATION_GPIO 0x0 00049 #define GP_PIN_DESIGNATION_CS 0x1 00050 #define GP_PIN_DESIGNATION_DEDICATED 0x2 00051 #define REMOTE_WAKEUP_DISABLED 0x0 00052 #define REMOTE_WAVEUP_ENABLED 0x1 00053 #define COUNT_HIGH_PULSES 0x4 00054 #define COUNT_LOW_PULSES 0x3 00055 #define COUNT_RISING_EDGES 0x2 00056 #define COUNT_FALLING_EDGES 0x1 00057 #define NO_INTERRUPT_COUNTING 0x0 00058 #define CHIP_SETTINGS_NOT_PROTECTED 0x0 00059 #define CHIP_SETTINGS_PROTECTED_BY_PWD 0x40 00060 #define CHIP_SETTINGS_LOCKED 0x80 00061 00062 #define USB_STRING_DESCRIPTOR_ID 0x03 00063 00064 #define CMD_UNSPOORTED 0x0 00065 #define CMD_GET_CHIP_STATUS 0x10 00066 #define CMD_SPI_CANCEL 0x11 00067 #define CMD_GET_NUM_EVENTS_FROM_INT_PIN 0x12 00068 #define CMD_GET_GPIO_SETTING 0x20 00069 #define CMD_SET_GPIO_SETTING 0x21 00070 #define CMD_SET_GPIO_PIN_VAL 0x30 00071 #define CMD_GET_GPIO_PIN_VAL 0x31 00072 #define CMD_SET_GPIO_PIN_DIR 0x32 00073 #define CMD_GET_GPIO_PIN_DIR 0x33 00074 #define CMD_SET_SPI_SETTING 0x40 00075 #define CMD_GET_SPI_SETTING 0x41 00076 #define CMD_SPI_TRANSFER 0x42 00077 #define CMD_READ_EEPROM_MEM 0x50 00078 #define CMD_WRITE_EEPROM_MEM 0x51 00079 #define CMD_SET_NVRAM_PARAM 0x60 00080 #define CMD_GET_NVRAM_PARAM 0x61 00081 #define CMD_SEND_PASSWORD 0x70 00082 #define CMD_SPI_BUS_RELEASE_REQ 0x80 00083 00084 #define CMDSUB_SPI_POWERUP_XFER_SETTINGS 0x10 00085 #define CMDSUB_POWERUP_CHIP_SETTINGS 0x20 00086 #define CMDSUB_USB_KEY_PARAMETERS 0x30 00087 #define CMDSUB_USB_PRODUCT_NAME 0x40 00088 #define CMDSUB_USB_MANUFACTURER_NAME 0x50 00089 00090 #define OPERATION_SUCCESSFUL 0 00091 #define ERROR_UNABLE_TO_OPEN_DEVICE -1 00092 #define ERROR_UNABLE_TO_WRITE_TO_DEVICE -2 00093 #define ERROR_UNABLE_TO_READ_FROM_DEVICE -3 00094 #define ERROR_INVALID_DEVICE_HANDLE -99 00095 00096 #define COMMAND_BUFFER_LENGTH 64 00097 #define RESPONSE_BUFFER_LENGTH 64 00098 00099 #define SPI_STATUS_FINISHED_NO_DATA_TO_SEND 0x10 00100 #define SPI_STATUS_STARTED_NO_DATA_TO_RECEIVE 0x20 00101 #define SPI_STATUS_SUCCESSFUL 0x30 00102 00106 struct GPPin { 00113 unsigned int PinDesignation; 00114 00120 unsigned int GPIOOutput; 00121 00127 unsigned int GPIODirection; 00128 }; 00129 00133 struct SPITransferSettingsDef { 00137 unsigned long BitRate; 00138 00143 unsigned int IdleChipSelectValue; 00144 00149 unsigned int ActiveChipSelectValue; 00150 00155 unsigned int CSToDataDelay; 00156 00161 unsigned int LastDataByteToCSDelay; 00162 00167 unsigned int SubsequentDataByteDelay; 00168 00172 unsigned int BytesPerSPITransfer; 00173 00181 unsigned int SPIMode; 00182 00186 int ErrorCode; 00187 }; 00188 00192 struct ChipSettingsDef { 00196 GPPin GP[9]; 00197 00203 unsigned int RemoteWakeUpEnabled; 00204 00214 unsigned int DedicatedFunctionInterruptPinMode; 00215 00222 unsigned int SPIBusReleaseMode; 00223 00231 unsigned int NVRamChipParamAccessControl; 00232 00238 char password[8]; 00239 int ErrorCode; 00240 }; 00241 00245 struct USBKeyParametersDef { 00249 unsigned int VID; 00250 00254 unsigned int PID; 00255 00261 unsigned int HostPowered; 00262 00268 unsigned int SelfPowered; 00269 00275 unsigned int RemoteWakeupCapable; 00276 00281 unsigned int RequestedCurrentAmountFromHost; 00282 00286 int ErrorCode; 00287 }; 00288 00292 struct ManufacturerProductNameDef { 00299 unsigned int USBStringDescriptorLength; 00300 00305 unsigned int USBStringDescriptorID; 00306 00310 char ManufacturerProductName[59]; 00311 00315 int ErrorCode; 00316 }; 00317 00321 struct ChipStatusDef { 00328 unsigned int SPIBusReleaseExtReqStat; 00329 00337 unsigned int SPIBusCurrentOwner; 00338 00343 unsigned int AttemptedPWDAccesses; 00344 00351 unsigned int PasswordGuessed; 00352 00356 int ErrorCode; 00357 }; 00358 00362 struct SPIDataTransferStatusDef { 00366 unsigned int NumberOfBytesReceived; 00367 00376 unsigned int SPIEngineStatus; 00377 00382 unsigned char DataReceived[60]; 00383 00387 int ErrorCode; 00388 }; 00389 00393 struct ExternalInterruptPinStatusDef { 00397 unsigned int InterruptEventCounter; 00398 00402 int ErrorCode; 00403 }; 00404 00408 struct GPPinDef { 00413 GPPin GP[9]; 00414 00418 int ErrorCode; 00419 }; 00420 00427 hid_device_info* EnumerateMCP2210(); 00428 00437 hid_device* InitMCP2210(); 00438 00450 hid_device* InitMCP2210(wchar_t* serialNumber); 00451 00463 hid_device* InitMCP2210(unsigned short vid, unsigned short pid, wchar_t* serialNumber); 00464 00471 void ReleaseMCP2210(hid_device *handle); 00472 00486 int SendUSBCmd(hid_device *handle, byte *cmdBuf, byte *responseBuf); 00487 00500 SPITransferSettingsDef GetSPITransferSettings(hid_device *handle, bool isVolatile = true); 00501 00519 int SetSPITransferSettings(hid_device *handle, SPITransferSettingsDef def, bool isVolatile = true); 00520 00531 ChipSettingsDef GetChipSettings(hid_device *handle, bool isVolatile = true); 00532 00551 int SetChipSettings(hid_device *handle, ChipSettingsDef def, bool isVolatile = true); 00552 00561 USBKeyParametersDef GetUSBKeyParameters(hid_device *handle); 00562 00576 int SetUSBKeyParameters(hid_device *handle, USBKeyParametersDef def); 00577 00591 ManufacturerProductNameDef GetManufacturerProductName(hid_device *handle, unsigned int subCmdCode); 00592 00612 int SetManufacturerProductName(hid_device *handle, unsigned int subCmdCode, ManufacturerProductNameDef def); 00613 00635 int SendAccessPassword(hid_device *handle, char *pwd, int pwdLength); 00636 00650 int ReadEEPROM(hid_device *handle, byte addr, byte* val); 00651 00667 int WriteEEPROM(hid_device *handle, byte addr, byte val); 00668 00682 int RequestSPIBusRelease(hid_device *handle, byte val); 00683 00692 ChipStatusDef GetChipStatus(hid_device *handle); 00693 00702 ChipStatusDef CancelSPITransfer(hid_device *handle); 00703 00719 SPIDataTransferStatusDef SPIDataTransfer(hid_device *handle, byte* data, int length); 00720 00747 SPIDataTransferStatusDef SPISendReceive(hid_device *handle, byte* data, int cmdBufferLength, int dataLength = -1); 00748 00763 ExternalInterruptPinStatusDef GetNumOfEventsFromInterruptPin(hid_device *handle, byte resetCounter); 00764 00774 GPPinDef GetGPIOPinDirection(hid_device *handle); 00775 00788 int SetGPIOPinDirection(hid_device *handle, GPPinDef def); 00789 00799 GPPinDef GetGPIOPinValue(hid_device *handle); 00800 00812 int SetGPIOPinVal(hid_device *handle, GPPinDef def); 00813 00814 #endif