MCP2210 Library
An Open Source MCP2210 Library for Linux.
mcp2210.h File Reference
#include <cstdlib>
#include <stdio.h>
#include <wchar.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include "hidapi.h"

Go to the source code of this file.

Classes

struct  GPPin
struct  SPITransferSettingsDef
struct  ChipSettingsDef
struct  USBKeyParametersDef
struct  ManufacturerProductNameDef
struct  ChipStatusDef
struct  SPIDataTransferStatusDef
struct  ExternalInterruptPinStatusDef
struct  GPPinDef

Defines

#define MCP2210_VID   0x04d8
#define MCP2210_PID   0x00de
#define GPIO_DIRECTION_OUTPUT   0
#define GPIO_DIRECTION_INPUT   1
#define GP_PIN_DESIGNATION_GPIO   0x0
#define GP_PIN_DESIGNATION_CS   0x1
#define GP_PIN_DESIGNATION_DEDICATED   0x2
#define REMOTE_WAKEUP_DISABLED   0x0
#define REMOTE_WAVEUP_ENABLED   0x1
#define COUNT_HIGH_PULSES   0x4
#define COUNT_LOW_PULSES   0x3
#define COUNT_RISING_EDGES   0x2
#define COUNT_FALLING_EDGES   0x1
#define NO_INTERRUPT_COUNTING   0x0
#define CHIP_SETTINGS_NOT_PROTECTED   0x0
#define CHIP_SETTINGS_PROTECTED_BY_PWD   0x40
#define CHIP_SETTINGS_LOCKED   0x80
#define USB_STRING_DESCRIPTOR_ID   0x03
#define CMD_UNSPOORTED   0x0
#define CMD_GET_CHIP_STATUS   0x10
#define CMD_SPI_CANCEL   0x11
#define CMD_GET_NUM_EVENTS_FROM_INT_PIN   0x12
#define CMD_GET_GPIO_SETTING   0x20
#define CMD_SET_GPIO_SETTING   0x21
#define CMD_SET_GPIO_PIN_VAL   0x30
#define CMD_GET_GPIO_PIN_VAL   0x31
#define CMD_SET_GPIO_PIN_DIR   0x32
#define CMD_GET_GPIO_PIN_DIR   0x33
#define CMD_SET_SPI_SETTING   0x40
#define CMD_GET_SPI_SETTING   0x41
#define CMD_SPI_TRANSFER   0x42
#define CMD_READ_EEPROM_MEM   0x50
#define CMD_WRITE_EEPROM_MEM   0x51
#define CMD_SET_NVRAM_PARAM   0x60
#define CMD_GET_NVRAM_PARAM   0x61
#define CMD_SEND_PASSWORD   0x70
#define CMD_SPI_BUS_RELEASE_REQ   0x80
#define CMDSUB_SPI_POWERUP_XFER_SETTINGS   0x10
#define CMDSUB_POWERUP_CHIP_SETTINGS   0x20
#define CMDSUB_USB_KEY_PARAMETERS   0x30
#define CMDSUB_USB_PRODUCT_NAME   0x40
#define CMDSUB_USB_MANUFACTURER_NAME   0x50
#define OPERATION_SUCCESSFUL   0
#define ERROR_UNABLE_TO_OPEN_DEVICE   -1
#define ERROR_UNABLE_TO_WRITE_TO_DEVICE   -2
#define ERROR_UNABLE_TO_READ_FROM_DEVICE   -3
#define ERROR_INVALID_DEVICE_HANDLE   -99
#define COMMAND_BUFFER_LENGTH   64
#define RESPONSE_BUFFER_LENGTH   64
#define SPI_STATUS_FINISHED_NO_DATA_TO_SEND   0x10
#define SPI_STATUS_STARTED_NO_DATA_TO_RECEIVE   0x20
#define SPI_STATUS_SUCCESSFUL   0x30

Typedefs

typedef uint8_t byte

Functions

hid_device_infoEnumerateMCP2210 ()
hid_deviceInitMCP2210 ()
hid_deviceInitMCP2210 (wchar_t *serialNumber)
hid_deviceInitMCP2210 (unsigned short vid, unsigned short pid, wchar_t *serialNumber)
void ReleaseMCP2210 (hid_device *handle)
int SendUSBCmd (hid_device *handle, byte *cmdBuf, byte *responseBuf)
SPITransferSettingsDef GetSPITransferSettings (hid_device *handle, bool isVolatile=true)
int SetSPITransferSettings (hid_device *handle, SPITransferSettingsDef def, bool isVolatile=true)
ChipSettingsDef GetChipSettings (hid_device *handle, bool isVolatile=true)
int SetChipSettings (hid_device *handle, ChipSettingsDef def, bool isVolatile=true)
USBKeyParametersDef GetUSBKeyParameters (hid_device *handle)
int SetUSBKeyParameters (hid_device *handle, USBKeyParametersDef def)
ManufacturerProductNameDef GetManufacturerProductName (hid_device *handle, unsigned int subCmdCode)
int SetManufacturerProductName (hid_device *handle, unsigned int subCmdCode, ManufacturerProductNameDef def)
int SendAccessPassword (hid_device *handle, char *pwd, int pwdLength)
int ReadEEPROM (hid_device *handle, byte addr, byte *val)
int WriteEEPROM (hid_device *handle, byte addr, byte val)
int RequestSPIBusRelease (hid_device *handle, byte val)
ChipStatusDef GetChipStatus (hid_device *handle)
ChipStatusDef CancelSPITransfer (hid_device *handle)
SPIDataTransferStatusDef SPIDataTransfer (hid_device *handle, byte *data, int length)
SPIDataTransferStatusDef SPISendReceive (hid_device *handle, byte *data, int cmdBufferLength, int dataLength=-1)
ExternalInterruptPinStatusDef GetNumOfEventsFromInterruptPin (hid_device *handle, byte resetCounter)
GPPinDef GetGPIOPinDirection (hid_device *handle)
int SetGPIOPinDirection (hid_device *handle, GPPinDef def)
GPPinDef GetGPIOPinValue (hid_device *handle)
int SetGPIOPinVal (hid_device *handle, GPPinDef def)

Define Documentation

#define CHIP_SETTINGS_LOCKED   0x80
#define CHIP_SETTINGS_NOT_PROTECTED   0x0
#define CHIP_SETTINGS_PROTECTED_BY_PWD   0x40
#define CMD_GET_CHIP_STATUS   0x10
#define CMD_GET_GPIO_PIN_DIR   0x33
#define CMD_GET_GPIO_PIN_VAL   0x31
#define CMD_GET_GPIO_SETTING   0x20
#define CMD_GET_NVRAM_PARAM   0x61
#define CMD_GET_SPI_SETTING   0x41
#define CMD_READ_EEPROM_MEM   0x50
#define CMD_SEND_PASSWORD   0x70
#define CMD_SET_GPIO_PIN_DIR   0x32
#define CMD_SET_GPIO_PIN_VAL   0x30
#define CMD_SET_GPIO_SETTING   0x21
#define CMD_SET_NVRAM_PARAM   0x60
#define CMD_SET_SPI_SETTING   0x40
#define CMD_SPI_BUS_RELEASE_REQ   0x80
#define CMD_SPI_CANCEL   0x11
#define CMD_SPI_TRANSFER   0x42
#define CMD_UNSPOORTED   0x0
#define CMD_WRITE_EEPROM_MEM   0x51
#define CMDSUB_POWERUP_CHIP_SETTINGS   0x20
#define CMDSUB_USB_KEY_PARAMETERS   0x30
#define CMDSUB_USB_MANUFACTURER_NAME   0x50
#define CMDSUB_USB_PRODUCT_NAME   0x40
#define COMMAND_BUFFER_LENGTH   64
#define COUNT_FALLING_EDGES   0x1
#define COUNT_HIGH_PULSES   0x4
#define COUNT_LOW_PULSES   0x3
#define COUNT_RISING_EDGES   0x2
#define ERROR_INVALID_DEVICE_HANDLE   -99
#define ERROR_UNABLE_TO_OPEN_DEVICE   -1
#define GP_PIN_DESIGNATION_CS   0x1
#define GP_PIN_DESIGNATION_DEDICATED   0x2
#define GP_PIN_DESIGNATION_GPIO   0x0

Response bit value for CMD_SET_NVRAM_PARAM 0x60, CMD_GET_NVRAM_PARAM 0x61 CMDSUB_POWERUP_CHIP_SETTINGS 0x20

#define GPIO_DIRECTION_INPUT   1
#define GPIO_DIRECTION_OUTPUT   0

GPIO pin directions

#define MCP2210_PID   0x00de
#define MCP2210_VID   0x04d8
#define NO_INTERRUPT_COUNTING   0x0
#define OPERATION_SUCCESSFUL   0
#define REMOTE_WAKEUP_DISABLED   0x0
#define REMOTE_WAVEUP_ENABLED   0x1
#define RESPONSE_BUFFER_LENGTH   64
#define SPI_STATUS_SUCCESSFUL   0x30
#define USB_STRING_DESCRIPTOR_ID   0x03

Typedef Documentation

typedef uint8_t byte

Copyright 2012, Kerry D. Wong

http://www.kerrywong.com

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


Function Documentation

Cancel the current SPI transfer

Parameters:
handleThe handle to the MCP2210 device
Returns:
See also:
ChipStatusDef

Enumerate the connected MCP2210's

Returns:
a pointer to hid_device_info
ChipSettingsDef GetChipSettings ( hid_device handle,
bool  isVolatile = true 
)

Get power-up settings

Parameters:
handleThe handle to the MCP2210 device
isVolatile(default true) Whether to make change permanently or in memory only.
Returns:
See also:
ChipSettingsDef

Get MCP2210 status

Parameters:
handleThe handle to the MCP2210 device
Returns:
See also:
ChipStatusDef

Get GPIO current pin direction from volatile memory

Parameters:
handleThe handle to the MCP2210 device
Returns:
See also:
GPPinDef note, only the direction member variable is affected.

Get GPIO current pin value

Parameters:
handleThe handle to the MCP2210 device
Returns:
See also:
GPPinDef note, only the value member variable is affected.
ManufacturerProductNameDef GetManufacturerProductName ( hid_device handle,
unsigned int  subCmdCode 
)

Set USB manufacturer name or product name

Parameters:
handleThe handle to the MCP2210 device
subCmdCodeThe sub command code which determines whether the action is to set manufacturer name or product name:

CMDSUB_USB_MANUFACTURER_NAME: set manufacturer name CMDSUB_USB_PRODUCT_NAME: set product name

Returns:
See also:
USBManufacturerProductNameDef

Get the current number of events from the interrupt pin

Note, GP6 must be configured to have its dedicated function active.

Parameters:
handleThe handle to the MCP2210 device
resetCounter0x0: reads, then resets the event counter >0: the event counter is read, however the counter is not reset
Returns:
0: Operation was successful <0: Other device errors (see error codes)
SPITransferSettingsDef GetSPITransferSettings ( hid_device handle,
bool  isVolatile = true 
)

Get SPI power-up transfer settings

Parameters:
handleThe handle to the MCP2210 device
isVolatile(default true) Whether to make change permanently or in memory only.

ture: in memory only. false: changes are written to NVRAM

Returns:
See also:
SPITransferSettingsDef

Get USB power-up key parameters

Parameters:
handleThe handle to the MCP2210 device
Returns:
See also:
USBKeyParametersDef

Initialize MCP2210 Vendor ID: 0x04d8 Product ID: 0x00de

Returns:
The handle to the MCP2210 device
hid_device* InitMCP2210 ( wchar_t *  serialNumber)

Initialize MCP2210 (if more than one is present) Vendor ID: 0x04d8 Product ID: 0x00de

Parameters:
serialNumberThe serial number of the MCP2210 device
Returns:
The handle to the MCP2210 device
hid_device* InitMCP2210 ( unsigned short  vid,
unsigned short  pid,
wchar_t *  serialNumber 
)

Initialize MCP2210 (using all parameters)

Parameters:
vidVender ID
pidProduct ID
serialNumberSerial number
Returns:
The handle to the MCP2210 device
int ReadEEPROM ( hid_device handle,
byte  addr,
byte val 
)

Read EEPROM Memory

Parameters:
handleThe handle to the MCP2210 device
addrThe address to be accessed (0-255)
valThe byte read at the specified address
Returns:
0: Operation was successful <0: Other device errors (see error codes)
void ReleaseMCP2210 ( hid_device handle)

Release the device handle and close the device

Parameters:
handleThe handle to the MCP2210 device
int RequestSPIBusRelease ( hid_device handle,
byte  val 
)

Request SPI bus release

Parameters:
handleThe handle to the MCP2210 device
valThe value of the SPI bus release ACK pin (only if GP7 is assigned to this dedicated function)
Returns:
0: Operation was successful 0xF8: SPI transfer in process (BUS not released) <0: Other device errors (see error codes)
int SendAccessPassword ( hid_device handle,
char *  pwd,
int  pwdLength 
)

Send access password

Parameters:
handleThe handle to the MCP2210 device
pwdpassword
pwdLengthpassword length (<=8)
Returns:
0: Operation was successful 0xFB: Chip conditional access is on, the password does not match and the number of attempts is above the accepted threshold of 5. The Access Password mechanism is temporarily blocked and no further password will be accepted until the next power-up. 0xFC: Access rejected 0XFD: Chip conditional access is on, the password does not match and the number of attempts is less than the accepted threshold of 5.
int SendUSBCmd ( hid_device handle,
byte cmdBuf,
byte responseBuf 
)

Send a USB command

Parameters:
handleThe handle to the MCP2210 device
cmdBufcommand buffer (64 bytes), unused/reserved entries must be filled with zero.
responseBufthe buffer (64 bytes) that contains the response.
Returns:
0: Operation was successful <0: Other device errors (see error codes)
int SetChipSettings ( hid_device handle,
ChipSettingsDef  def,
bool  isVolatile = true 
)

Set chip settings power-up default

Parameters:
handleThe handle to the MCP2210 device
def
See also:
ChipSettingDef
Parameters:
isVolatile(default true) Whether to make change permanently or in memory only.

ture: in memory only. false: changes are written to NVRAM

Returns:
0: Operation was successful 0xFB: The provided password does not match the one stored in the chip or the settings are permanently locked. <0: Other device errors (see error codes)
int SetGPIOPinDirection ( hid_device handle,
GPPinDef  def 
)

Set GPIO current pin direction in volatile memory

Parameters:
handleThe handle to the MCP2210 device
def
See also:
GPPinDef note, only the direction member variable is used.
Returns:
0: Operation was successful <0: Other device errors (see error codes)
int SetGPIOPinVal ( hid_device handle,
GPPinDef  def 
)

Set GPIO current pin value

Parameters:
handleThe handle to the MCP2210 device
def
See also:
GPPinDef note, only the value member variable is used.
Returns:
0: Operation was successful <0: Other device errors (see error codes)
int SetManufacturerProductName ( hid_device handle,
unsigned int  subCmdCode,
ManufacturerProductNameDef  def 
)

Set USB manufacturer name or product name

Parameters:
handleThe handle to the MCP2210 device
subCmdCodeThe sub command code which determines whether the action is to set manufacturer name or product name:

CMDSUB_USB_MANUFACTURER_NAME: set manufacturer name CMDSUB_USB_PRODUCT_NAME: set product name

Parameters:
def
See also:
USBManufacturerProductNameDef
Returns:
0: Operation was successful 0xFB: The provided password does not match the one stored in the chip or the settings are permanently locked. <0: Other device errors (see error codes)
int SetSPITransferSettings ( hid_device handle,
SPITransferSettingsDef  def,
bool  isVolatile = true 
)

Set SPI power-up transfer settings

Parameters:
handleThe handle to the MCP2210 device
def
See also:
SPITransferSettingsDef
Parameters:
isVolatile(default true) Whether to make change permanently or in memory only.

ture: in memory only. false: changes are written to NVRAM

Returns:
0: Operation was successful 0xFB: The provided password does not match the one stored in the chip or the settings are permanently locked. <0: Other device errors (see error codes)
int SetUSBKeyParameters ( hid_device handle,
USBKeyParametersDef  def 
)

Set USB power-up key parameters

Parameters:
handleThe handle to the MCP2210 device
def
See also:
USBKeyParametersDef
Returns:
0: Operation was successful 0xFB: The provided password does not match the one stored in the chip or the settings are permanently locked. <0: Other device errors (see error codes)
SPIDataTransferStatusDef SPIDataTransfer ( hid_device handle,
byte data,
int  length 
)

SPI Data Transfer

Parameters:
handleThe handle to the MCP2210 device
dataa pointer to the data array to be transfered
lengthnumber of bytes to be transfered
Returns:
See also:
SPIDataTransferStatusDef ErrorCode meaning: 0xF7: SPI bus not available 0xF8: SPI transfer in progress (cannot accept any data for the moment)
SPIDataTransferStatusDef SPISendReceive ( hid_device handle,
byte data,
int  cmdBufferLength,
int  dataLength = -1 
)

Send data and wait till results are received from the SPI bus.

Note: this method differers from SPIDataTransfer in that it also waits till the transfer is finished. In SPIDataTransfer, the SPIEngineStatus in the returned data structure tells the status of the SPI engine, and the call returns after the data is sent, regardless of whether the data has been received.

Parameters:
handleThe handle to the MCP2210 device
dataa pointer to the data array to be transfered
cmdBufferLengthnumber of command bytes to be transfered
dataLength(optional) number of data elements to be returned

if this parameter is not supplied the default length is set to be the same as the command buffer length

Returns:
See also:
SPIDataTransferStatusDef ErrorCode meaning: 0xF7: SPI bus not available 0xF8: SPI transfer in progress (cannot accept any data for the moment)
int WriteEEPROM ( hid_device handle,
byte  addr,
byte  val 
)

Write EEPROM Memory

Parameters:
handleThe handle to the MCP2210 device
addrThe address to be accessed (0-255)
valThe byte to be written to the specified address.
Returns:
0: Operation was successful 0xFA: EEPROM Write failure 0xFB: EEPROM is password protected or permanently locked. <0: Other device errors (see error codes)
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines