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;
}