//************************************************************************** // WriteXSeriesEEPROM //************************************************************************** // Intellisense comments /// <summary> /// Writes the specified values to the EEPROM of an X-Series device. /// Calls FT_EEPROM_Program in FTD2XX DLL /// </summary> /// <returns>FT_STATUS value from FT_EEPROM_Program in FTD2XX DLL</returns> /// <param name="eeX">The EEPROM settings to be written to the device</param> /// <remarks>If the strings are too long, they will be truncated to their maximum permitted lengths</remarks> /// <exception cref="FT_EXCEPTION">Thrown when the current device does not match the type required by this method.</exception> public FT_STATUS WriteXSeriesEEPROM(FT_XSERIES_EEPROM_STRUCTURE eeX) { // Initialise ftStatus to something other than FT_OK FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR; FT_ERROR ftErrorCondition = FT_ERROR.FT_NO_ERROR; byte[] manufacturer, manufacturerID, description, serialNumber; // If the DLL hasn't been loaded, just return here if (hFTD2XXDLL == IntPtr.Zero) return ftStatus; // Check for our required function pointers being set up if (pFT_EEPROM_Program != IntPtr.Zero) { tFT_EEPROM_Program FT_EEPROM_Program = (tFT_EEPROM_Program)Marshal.GetDelegateForFunctionPointer(pFT_EEPROM_Program, typeof(tFT_EEPROM_Program)); if (ftHandle != IntPtr.Zero) { FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN; // Check that it is an FT232H that we are trying to write GetDeviceType(ref DeviceType); if (DeviceType != FT_DEVICE.FT_DEVICE_X_SERIES) { // If it is not, throw an exception ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE; ErrorHandler(ftStatus, ftErrorCondition); } // Check for VID and PID of 0x0000 if ((eeX.VendorID == 0x0000) | (eeX.ProductID == 0x0000)) { // Do not allow users to program the device with VID or PID of 0x0000 return FT_STATUS.FT_INVALID_PARAMETER; } FT_XSERIES_DATA eeData = new FT_XSERIES_DATA(); // String manipulation... // Allocate space from unmanaged heap manufacturer = new byte[32]; manufacturerID = new byte[16]; description = new byte[64]; serialNumber = new byte[16]; // Check lengths of strings to make sure that they are within our limits // If not, trim them to make them our maximum length if (eeX.Manufacturer.Length > 32) eeX.Manufacturer = eeX.Manufacturer.Substring(0, 32); if (eeX.ManufacturerID.Length > 16) eeX.ManufacturerID = eeX.ManufacturerID.Substring(0, 16); if (eeX.Description.Length > 64) eeX.Description = eeX.Description.Substring(0, 64); if (eeX.SerialNumber.Length > 16) eeX.SerialNumber = eeX.SerialNumber.Substring(0, 16); // Set string values System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); manufacturer = encoding.GetBytes(eeX.Manufacturer); manufacturerID = encoding.GetBytes(eeX.ManufacturerID); description = encoding.GetBytes(eeX.Description); serialNumber = encoding.GetBytes(eeX.SerialNumber); // Map non-string elements to structure to be returned // Standard elements eeData.common.deviceType = (uint)FT_DEVICE.FT_DEVICE_X_SERIES; eeData.common.VendorId = eeX.VendorID; eeData.common.ProductId = eeX.ProductID; eeData.common.MaxPower = eeX.MaxPower; eeData.common.SelfPowered = Convert.ToByte(eeX.SelfPowered); eeData.common.RemoteWakeup = Convert.ToByte(eeX.RemoteWakeup); eeData.common.SerNumEnable = Convert.ToByte(eeX.SerNumEnable); eeData.common.PullDownEnable = Convert.ToByte(eeX.PullDownEnable); // X-Series specific fields // CBUS eeData.Cbus0 = eeX.Cbus0; eeData.Cbus1 = eeX.Cbus1; eeData.Cbus2 = eeX.Cbus2; eeData.Cbus3 = eeX.Cbus3; eeData.Cbus4 = eeX.Cbus4; eeData.Cbus5 = eeX.Cbus5; eeData.Cbus6 = eeX.Cbus6; // Drive Options eeData.ACDriveCurrent = eeX.ACDriveCurrent; eeData.ACSchmittInput = eeX.ACSchmittInput; eeData.ACSlowSlew = eeX.ACSlowSlew; eeData.ADDriveCurrent = eeX.ADDriveCurrent; eeData.ADSchmittInput = eeX.ADSchmittInput; eeData.ADSlowSlew = eeX.ADSlowSlew; // BCD eeData.BCDDisableSleep = eeX.BCDDisableSleep; eeData.BCDEnable = eeX.BCDEnable; eeData.BCDForceCbusPWREN = eeX.BCDForceCbusPWREN; // FT1248 eeData.FT1248Cpol = eeX.FT1248Cpol; eeData.FT1248FlowControl = eeX.FT1248FlowControl; eeData.FT1248Lsb = eeX.FT1248Lsb; // I2C eeData.I2CDeviceId = eeX.I2CDeviceId; eeData.I2CDisableSchmitt = eeX.I2CDisableSchmitt; eeData.I2CSlaveAddress = eeX.I2CSlaveAddress; // RS232 Signals eeData.InvertCTS = eeX.InvertCTS; eeData.InvertDCD = eeX.InvertDCD; eeData.InvertDSR = eeX.InvertDSR; eeData.InvertDTR = eeX.InvertDTR; eeData.InvertRI = eeX.InvertRI; eeData.InvertRTS = eeX.InvertRTS; eeData.InvertRXD = eeX.InvertRXD; eeData.InvertTXD = eeX.InvertTXD; // Hardware Options eeData.PowerSaveEnable = eeX.PowerSaveEnable; eeData.RS485EchoSuppress = eeX.RS485EchoSuppress; // Driver Option eeData.DriverType = eeX.IsVCP; // Check the size of the structure... int size = Marshal.SizeOf(eeData); // Allocate space for our pointer... IntPtr eeDataMarshal = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(eeData, eeDataMarshal, false); ftStatus = FT_EEPROM_Program(ftHandle, eeDataMarshal, (uint)size, manufacturer, manufacturerID, description, serialNumber); } } return FT_STATUS.FT_DEVICE_NOT_FOUND; }
//************************************************************************** // ReadXSeriesEEPROM //************************************************************************** // Intellisense comments /// <summary> /// Reads the EEPROM contents of an X-Series device. /// </summary> /// <returns>FT_STATUS value from FT_EEPROM_Read in FTD2XX DLL</returns> /// <param name="eeX">An FT_XSERIES_EEPROM_STRUCTURE which contains only the relevant information for an X-Series device.</param> /// <exception cref="FT_EXCEPTION">Thrown when the current device does not match the type required by this method.</exception> public FT_STATUS ReadXSeriesEEPROM(FT_XSERIES_EEPROM_STRUCTURE eeX) { // Initialise ftStatus to something other than FT_OK FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR; FT_ERROR ftErrorCondition = FT_ERROR.FT_NO_ERROR; // If the DLL hasn't been loaded, just return here if (hFTD2XXDLL == IntPtr.Zero) return ftStatus; // Check for our required function pointers being set up if (pFT_EEPROM_Read != IntPtr.Zero) { tFT_EEPROM_Read FT_EEPROM_Read = (tFT_EEPROM_Read)Marshal.GetDelegateForFunctionPointer(pFT_EEPROM_Read, typeof(tFT_EEPROM_Read)); if (ftHandle != IntPtr.Zero) { FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN; // Check that it is an FT232H that we are trying to read GetDeviceType(ref DeviceType); if (DeviceType != FT_DEVICE.FT_DEVICE_X_SERIES) { // If it is not, throw an exception ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE; ErrorHandler(ftStatus, ftErrorCondition); } FT_XSERIES_DATA eeData = new FT_XSERIES_DATA(); FT_EEPROM_HEADER eeHeader = new FT_EEPROM_HEADER(); byte[] manufacturer = new byte[32]; byte[] manufacturerID = new byte[16]; byte[] description = new byte[64]; byte[] serialNumber = new byte[16]; eeHeader.deviceType = (uint)FT_DEVICE.FT_DEVICE_X_SERIES; eeData.common = eeHeader; // Calculate the size of our data structure... int size = Marshal.SizeOf(eeData); // Allocate space for our pointer... IntPtr eeDataMarshal = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(eeData, eeDataMarshal, false); // Call FT_EEPROM_Read ftStatus = FT_EEPROM_Read(ftHandle, eeDataMarshal, (uint)size, manufacturer, manufacturerID, description, serialNumber); if (ftStatus == FT_STATUS.FT_OK) { // Get the data back from the pointer... eeData = (FT_XSERIES_DATA)Marshal.PtrToStructure(eeDataMarshal, typeof(FT_XSERIES_DATA)); // Retrieve string values System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding(); eeX.Manufacturer = enc.GetString(manufacturer); eeX.ManufacturerID = enc.GetString(manufacturerID); eeX.Description = enc.GetString(description); eeX.SerialNumber = enc.GetString(serialNumber); // Map non-string elements to structure to be returned // Standard elements eeX.VendorID = eeData.common.VendorId; eeX.ProductID = eeData.common.ProductId; eeX.MaxPower = eeData.common.MaxPower; eeX.SelfPowered = Convert.ToBoolean(eeData.common.SelfPowered); eeX.RemoteWakeup = Convert.ToBoolean(eeData.common.RemoteWakeup); eeX.SerNumEnable = Convert.ToBoolean(eeData.common.SerNumEnable); eeX.PullDownEnable = Convert.ToBoolean(eeData.common.PullDownEnable); // X-Series specific fields // CBUS eeX.Cbus0 = eeData.Cbus0; eeX.Cbus1 = eeData.Cbus1; eeX.Cbus2 = eeData.Cbus2; eeX.Cbus3 = eeData.Cbus3; eeX.Cbus4 = eeData.Cbus4; eeX.Cbus5 = eeData.Cbus5; eeX.Cbus6 = eeData.Cbus6; // Drive Options eeX.ACDriveCurrent = eeData.ACDriveCurrent; eeX.ACSchmittInput = eeData.ACSchmittInput; eeX.ACSlowSlew = eeData.ACSlowSlew; eeX.ADDriveCurrent = eeData.ADDriveCurrent; eeX.ADSchmittInput = eeData.ADSchmittInput; eeX.ADSlowSlew = eeData.ADSlowSlew; // BCD eeX.BCDDisableSleep = eeData.BCDDisableSleep; eeX.BCDEnable = eeData.BCDEnable; eeX.BCDForceCbusPWREN = eeData.BCDForceCbusPWREN; // FT1248 eeX.FT1248Cpol = eeData.FT1248Cpol; eeX.FT1248FlowControl = eeData.FT1248FlowControl; eeX.FT1248Lsb = eeData.FT1248Lsb; // I2C eeX.I2CDeviceId = eeData.I2CDeviceId; eeX.I2CDisableSchmitt = eeData.I2CDisableSchmitt; eeX.I2CSlaveAddress = eeData.I2CSlaveAddress; // RS232 Signals eeX.InvertCTS = eeData.InvertCTS; eeX.InvertDCD = eeData.InvertDCD; eeX.InvertDSR = eeData.InvertDSR; eeX.InvertDTR = eeData.InvertDTR; eeX.InvertRI = eeData.InvertRI; eeX.InvertRTS = eeData.InvertRTS; eeX.InvertRXD = eeData.InvertRXD; eeX.InvertTXD = eeData.InvertTXD; // Hardware Options eeX.PowerSaveEnable = eeData.PowerSaveEnable; eeX.RS485EchoSuppress = eeData.RS485EchoSuppress; // Driver Option eeX.IsVCP = eeData.DriverType; } } } else { if (pFT_EE_Read == IntPtr.Zero) { MessageBox.Show("Failed to load function FT_EE_Read."); } } return ftStatus; }