public FT_STATUS WriteFT232HEEPROM(FT232H_EEPROM_STRUCTURE ee232h)
{
// 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_EE_Program != IntPtr.Zero)
{
tFT_EE_Program FT_EE_Program = (tFT_EE_Program)Marshal.GetDelegateForFunctionPointer(pFT_EE_Program, typeof(tFT_EE_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_232H)
{
// If it is not, throw an exception
ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE;
ErrorHandler(ftStatus, ftErrorCondition);
}
// Check for VID and PID of 0x0000
if ((ee232h.VendorID == 0x0000) | (ee232h.ProductID == 0x0000))
{
// Do not allow users to program the device with VID or PID of 0x0000
return FT_STATUS.FT_INVALID_PARAMETER;
}
FT_PROGRAM_DATA eedata = new FT_PROGRAM_DATA();
// Set up structure headers
eedata.Signature1 = 0x00000000;
eedata.Signature2 = 0xFFFFFFFF;
eedata.Version = 5;
// Allocate space from unmanaged heap
eedata.Manufacturer = Marshal.AllocHGlobal(32);
eedata.ManufacturerID = Marshal.AllocHGlobal(16);
eedata.Description = Marshal.AllocHGlobal(64);
eedata.SerialNumber = Marshal.AllocHGlobal(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 (ee232h.Manufacturer.Length > 32)
ee232h.Manufacturer = ee232h.Manufacturer.Substring(0, 32);
if (ee232h.ManufacturerID.Length > 16)
ee232h.ManufacturerID = ee232h.ManufacturerID.Substring(0, 16);
if (ee232h.Description.Length > 64)
ee232h.Description = ee232h.Description.Substring(0, 64);
if (ee232h.SerialNumber.Length > 16)
ee232h.SerialNumber = ee232h.SerialNumber.Substring(0, 16);
// Set string values
eedata.Manufacturer = Marshal.StringToHGlobalAnsi(ee232h.Manufacturer);
eedata.ManufacturerID = Marshal.StringToHGlobalAnsi(ee232h.ManufacturerID);
eedata.Description = Marshal.StringToHGlobalAnsi(ee232h.Description);
eedata.SerialNumber = Marshal.StringToHGlobalAnsi(ee232h.SerialNumber);
// Map non-string elements to structure
// Standard elements
eedata.VendorID = ee232h.VendorID;
eedata.ProductID = ee232h.ProductID;
eedata.MaxPower = ee232h.MaxPower;
eedata.SelfPowered = Convert.ToUInt16(ee232h.SelfPowered);
eedata.RemoteWakeup = Convert.ToUInt16(ee232h.RemoteWakeup);
// 232H specific fields
eedata.PullDownEnableH = Convert.ToByte(ee232h.PullDownEnable);
eedata.SerNumEnableH = Convert.ToByte(ee232h.SerNumEnable);
eedata.ACSlowSlewH = Convert.ToByte(ee232h.ACSlowSlew);
eedata.ACSchmittInputH = Convert.ToByte(ee232h.ACSchmittInput);
eedata.ACDriveCurrentH = Convert.ToByte(ee232h.ACDriveCurrent);
eedata.ADSlowSlewH = Convert.ToByte(ee232h.ADSlowSlew);
eedata.ADSchmittInputH = Convert.ToByte(ee232h.ADSchmittInput);
eedata.ADDriveCurrentH = Convert.ToByte(ee232h.ADDriveCurrent);
eedata.Cbus0H = Convert.ToByte(ee232h.Cbus0);
eedata.Cbus1H = Convert.ToByte(ee232h.Cbus1);
eedata.Cbus2H = Convert.ToByte(ee232h.Cbus2);
eedata.Cbus3H = Convert.ToByte(ee232h.Cbus3);
eedata.Cbus4H = Convert.ToByte(ee232h.Cbus4);
eedata.Cbus5H = Convert.ToByte(ee232h.Cbus5);
eedata.Cbus6H = Convert.ToByte(ee232h.Cbus6);
eedata.Cbus7H = Convert.ToByte(ee232h.Cbus7);
eedata.Cbus8H = Convert.ToByte(ee232h.Cbus8);
eedata.Cbus9H = Convert.ToByte(ee232h.Cbus9);
eedata.IsFifoH = Convert.ToByte(ee232h.IsFifo);
eedata.IsFifoTarH = Convert.ToByte(ee232h.IsFifoTar);
eedata.IsFastSerH = Convert.ToByte(ee232h.IsFastSer);
eedata.IsFT1248H = Convert.ToByte(ee232h.IsFT1248);
eedata.FT1248CpolH = Convert.ToByte(ee232h.FT1248Cpol);
eedata.FT1248LsbH = Convert.ToByte(ee232h.FT1248Lsb);
eedata.FT1248FlowControlH = Convert.ToByte(ee232h.FT1248FlowControl);
eedata.IsVCPH = Convert.ToByte(ee232h.IsVCP);
eedata.PowerSaveEnableH = Convert.ToByte(ee232h.PowerSaveEnable);
// Call FT_EE_Program
ftStatus = FT_EE_Program(ftHandle, eedata);
// Free unmanaged buffers
Marshal.FreeHGlobal(eedata.Manufacturer);
Marshal.FreeHGlobal(eedata.ManufacturerID);
Marshal.FreeHGlobal(eedata.Description);
Marshal.FreeHGlobal(eedata.SerialNumber);
}
}
else
{
if (pFT_EE_Program == IntPtr.Zero)
{
LogB.Debug("FTD2XX: Failed to load function FT_EE_Program.");
}
}
return ftStatus;
}