After a couple of weeks’ coding and testing, I finally finished the initial version of the MCP2210 C++ library for Linux. This library utilizes functions from Signal 11‘s HID API (hidraw) to communicate over the USB HID interface.

You can get the latest source from here, or you can pull directly from github using the command below:

git clone git://github.com/kerrydwong/MCP2210-Library.git

This library exposes all the functionalities specified in the device datasheet.

The following code snippet shows how to generate a rectangular wave on the GP0 pin by rapidly toggling it on and off:

#include "mcp2210.h"

int main(int argc, char** argv) {
    int r = 0;

    hid_device *handle;

    /**
     * initializing the MCP2210 device.
     */
    handle = InitMCP2210();

    /**
     * Configure GPIO0 direction to output
     */
    GPPinDef def = GetGPIOPinDirection(handle);    
    def.GP[0].GPIODirection = GPIO_DIRECTION_OUTPUT;

    r = SetGPIOPinDirection(handle, def);

    ///< Generate a rectangular wave by toggling GP0.
    while (1)  {
        def.GP[0].GPIOOutput = 1-def.GP[0].GPIOOutput;
        r = SetGPIOPinVal(handle, def);
    }
    
    /**
     * release the handle
     */
    ReleaseMCP2210(handle);

    return 0;
}

You should see a rectangular wave at GP0 (Pin 5) with a frequency of roughly 160 Hz.

The function calls are pretty straight forward. Most “Set” functions take the current device handle and the input data definition (e.g. SPITransferSettingsDef, GPPinDef, etc.) and returns a status code. A typical “Get” function call takes the current device handle and returns the corresponding data definition.

All SPI data transfer is handled via a single function call SPIDataTransfer, the function definition is shown below:

SPIDataTransferStatusDef SPIDataTransfer(hid_device *handle, byte* data, int length);

Basically, you supply the beginning address of the data to be transferred and the length of the data and when the call returns, the SPIDataTransferStatusDef contains the result along with the call status.

struct SPIDataTransferStatusDef {
    /**
     * Number of bytes received
     */
    unsigned int NumberOfBytesReceived;
    
    /**
     * SPI transfer engine status
     * 0x10: SPI transfer finished, no data to send
     * 0x20: SPI transfer started, no data to receive
     * 0x30: SPI data accepted, command completed successfully
     *
     */
    unsigned int SPIEngineStatus;
    
    /**
     * The buffer contains received data. 
     * Actual amount of data is determined by NumberOfBytesReceived.
     */
    unsigned char DataReceived[60];
    
    /**
     * The error code returned
     */    
    int ErrorCode;
};

For more detailed information, you can take a look at the header file (mcp2210.h) or the doxygen generated documentation located under the doc directory.

The project was created using Netbeans 7.2 and was compiled under Ubuntu 12.04 (64 bit). To compile the code, make sure that you have the build essentials installed and also you will need libudev.

sudo apt-get install libudev0

Note, the process must have write access to the USB device under Linux. This means that either you will need to use sudo to run your code or you will need to setup rules to allow read/write access to the device using instructions provided on signal11. I have provided a UDEV rule file (99-hid.rules), and you may need to add it to the /etc/udev/rules.d directory.

I will provide more sample code in the coming weeks. Please drop me a line if you run into any issues.

Be Sociable, Share!