示例#1
0
        //#region USB to I2C Bridge
        public static int Open()            // Open USB to I2C bridge
        {
            int  rtval;
            byte powerbyte   = 0;
            uint currentbyte = 0;

            byte[] FPins = new byte[4];
            byte[] DPins = new byte[4];
            byte[] VPins = new byte[4];
            rtval = OpenbyIndex();
            if (rtval == MCP2221.M_E_NO_ERR)
            {
                // Check is self power and wake on int is enabled
                rtval = MCP2221.M_Mcp2221_GetUsbPowerAttributes(mHandle, ref powerbyte, ref currentbyte);
                if ((powerbyte & (MCP2221.M_MCP2221_USB_SELF | MCP2221.M_MCP2221_USB_REMOTE)) != POWERCFG)
                {
                    // Set self power and wake on int
                    rtval = rtval | MCP2221.M_Mcp2221_SetUsbPowerAttributes(mHandle, POWERCFG, currentbyte);
                    MCP2221.M_Mcp2221_Close(mHandle);
                    if (OpenbyIndex() != MCP2221.M_E_NO_ERR)
                    {
                        DeviceOpen = false;
                        MCP2221.M_Mcp2221_Reset(mHandle);
                        MCP2221.M_Mcp2221_CloseAll();
                        return(I2C_NO_BRIDGE);
                    }
                }
                //int test = MCP2221.M_Mcp2221_SetAdvancedCommParams(mHandle, 3, 10);
                rtval = rtval | MCP2221.M_Mcp2221_GetGpioSettings(mHandle, SRAM, FPins, DPins, VPins);
                bool isFPins = FPins.SequenceEqual(GPIOFNC);
                bool isDpins = DPins.SequenceEqual(GPIODIR);
                if ((isFPins == false) || (isDpins == false))
                {
                    rtval = rtval | MCP2221.M_Mcp2221_SetGpioSettings(mHandle, FLASH, GPIOFNC, GPIODIR, GPIOVAL);
                    rtval = rtval | MCP2221.M_Mcp2221_SetGpioSettings(mHandle, SRAM, GPIOFNC, GPIODIR, GPIOVAL);
                }
                else
                {
                    rtval = rtval | MCP2221.M_Mcp2221_SetGpioValues(mHandle, GPIOVAL);
                }
                rtval = rtval | MCP2221.M_Mcp2221_ClearInterruptPinFlag(mHandle);
                if (rtval != MCP2221.M_E_NO_ERR)
                {
                    MCP2221.M_Mcp2221_Reset(mHandle);
                    return(I2C_CFGERR);
                }
                DeviceOpen = true;
            }
            else
            {
                DeviceOpen = false;
                return(I2C_NO_BRIDGE);
            }
            return(I2C_NO_ERROR);
        }
示例#2
0
        private static int OpenbyIndex()
        {
            int  rtval;
            uint nDev = 0;

            DeviceInfo.LibraryVersion = MCP2221.M_Mcp2221_GetLibraryVersion();
            rtval = MCP2221.M_Mcp2221_GetConnectedDevices(vid, pid, ref nDev);
            if (rtval == MCP2221.M_E_NO_ERR)
            {
                mHandle = MCP2221.M_Mcp2221_OpenByIndex(vid, pid, nDev - 1);
                rtval   = MCP2221.M_Mcp2221_GetLastError();
                if (rtval == MCP2221.M_E_NO_ERR)
                {
                    DeviceInfo.Manufacture = MCP2221.M_Mcp2221_GetManufacturerDescriptor(mHandle);
                    DeviceInfo.Product     = MCP2221.M_Mcp2221_GetProductDescriptor(mHandle);
                    DeviceInfo.Serial      = MCP2221.M_Mcp2221_GetSerialNumberDescriptor(mHandle);
                    DeviceInfo.FactorySN   = MCP2221.M_Mcp2221_GetFactorySerialNumber(mHandle);
                    DeviceInfo.HardwareVer = MCP2221.M_Mcp2221_GetHardwareRevision(mHandle);
                    DeviceInfo.FirmwareRev = MCP2221.M_Mcp2221_GetFirmwareRevision(mHandle);
                    rtval = MCP2221.M_Mcp2221_GetLastError();
                    if (rtval == MCP2221.M_E_NO_ERR)
                    {
                        int rval = MCP2221.M_Mcp2221_SetSpeed(mHandle, i2cspeed);
                        int loop = 100;
                        while (rval != MCP2221.M_E_NO_ERR)
                        {
                            MCP2221.M_Mcp2221_I2cCancelCurrentTransfer(mHandle);
                            MCP2221.M_Mcp2221_Reset(mHandle);
                            MCP2221.M_Mcp2221_GetConnectedDevices(vid, pid, ref nDev);
                            mHandle = MCP2221.M_Mcp2221_OpenByIndex(vid, pid, nDev - 1);
                            rval    = MCP2221.M_Mcp2221_SetSpeed(mHandle, i2cspeed);
                            loop--;
                            if (loop == 0)
                            {
                                break;
                            }
                        }
                    }
                }
            }
            return(rtval);
        }