//#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); }
public static void Close() { if (DeviceOpen) { int rtval = MCP2221.M_Mcp2221_CloseAll(); if (rtval == I2C_NO_ERROR) { DeviceOpen = false; } } }