Beispiel #1
0
        //**************************************************************************
        // WriteFT2232HEEPROM
        //**************************************************************************
        // Intellisense comments
        /// <summary>
        /// Writes the specified values to the EEPROM of an FT2232H device.
        /// Calls FT_EE_Program in FTD2XX DLL
        /// </summary>
        /// <returns>FT_STATUS value from FT_EE_Program in FTD2XX DLL</returns>
        /// <param name="ee2232h">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 WriteFT2232HEEPROM(FT2232H_EEPROM_STRUCTURE ee2232h)
        {
            // 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 FT2232H that we are trying to write
                    GetDeviceType(ref DeviceType);
                    if (DeviceType != FT_DEVICE.FT_DEVICE_2232H)
                    {
                        // If it is not, throw an exception
                        ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE;
                        ErrorHandler(ftStatus, ftErrorCondition);
                    }

                    // Check for VID and PID of 0x0000
                    if ((ee2232h.VendorID == 0x0000) | (ee2232h.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 = 3;

                    // 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 (ee2232h.Manufacturer.Length > 32)
                        ee2232h.Manufacturer = ee2232h.Manufacturer.Substring(0, 32);
                    if (ee2232h.ManufacturerID.Length > 16)
                        ee2232h.ManufacturerID = ee2232h.ManufacturerID.Substring(0, 16);
                    if (ee2232h.Description.Length > 64)
                        ee2232h.Description = ee2232h.Description.Substring(0, 64);
                    if (ee2232h.SerialNumber.Length > 16)
                        ee2232h.SerialNumber = ee2232h.SerialNumber.Substring(0, 16);

                    // Set string values
                    eedata.Manufacturer = Marshal.StringToHGlobalAnsi(ee2232h.Manufacturer);
                    eedata.ManufacturerID = Marshal.StringToHGlobalAnsi(ee2232h.ManufacturerID);
                    eedata.Description = Marshal.StringToHGlobalAnsi(ee2232h.Description);
                    eedata.SerialNumber = Marshal.StringToHGlobalAnsi(ee2232h.SerialNumber);

                    // Map non-string elements to structure
                    // Standard elements
                    eedata.VendorID = ee2232h.VendorID;
                    eedata.ProductID = ee2232h.ProductID;
                    eedata.MaxPower = ee2232h.MaxPower;
                    eedata.SelfPowered = Convert.ToUInt16(ee2232h.SelfPowered);
                    eedata.RemoteWakeup = Convert.ToUInt16(ee2232h.RemoteWakeup);
                    // 2232H specific fields
                    eedata.PullDownEnable7 = Convert.ToByte(ee2232h.PullDownEnable);
                    eedata.SerNumEnable7 = Convert.ToByte(ee2232h.SerNumEnable);
                    eedata.ALSlowSlew = Convert.ToByte(ee2232h.ALSlowSlew);
                    eedata.ALSchmittInput = Convert.ToByte(ee2232h.ALSchmittInput);
                    eedata.ALDriveCurrent = ee2232h.ALDriveCurrent;
                    eedata.AHSlowSlew = Convert.ToByte(ee2232h.AHSlowSlew);
                    eedata.AHSchmittInput = Convert.ToByte(ee2232h.AHSchmittInput);
                    eedata.AHDriveCurrent = ee2232h.AHDriveCurrent;
                    eedata.BLSlowSlew = Convert.ToByte(ee2232h.BLSlowSlew);
                    eedata.BLSchmittInput = Convert.ToByte(ee2232h.BLSchmittInput);
                    eedata.BLDriveCurrent = ee2232h.BLDriveCurrent;
                    eedata.BHSlowSlew = Convert.ToByte(ee2232h.BHSlowSlew);
                    eedata.BHSchmittInput = Convert.ToByte(ee2232h.BHSchmittInput);
                    eedata.BHDriveCurrent = ee2232h.BHDriveCurrent;
                    eedata.IFAIsFifo7 = Convert.ToByte(ee2232h.IFAIsFifo);
                    eedata.IFAIsFifoTar7 = Convert.ToByte(ee2232h.IFAIsFifoTar);
                    eedata.IFAIsFastSer7 = Convert.ToByte(ee2232h.IFAIsFastSer);
                    eedata.AIsVCP7 = Convert.ToByte(ee2232h.AIsVCP);
                    eedata.IFBIsFifo7 = Convert.ToByte(ee2232h.IFBIsFifo);
                    eedata.IFBIsFifoTar7 = Convert.ToByte(ee2232h.IFBIsFifoTar);
                    eedata.IFBIsFastSer7 = Convert.ToByte(ee2232h.IFBIsFastSer);
                    eedata.BIsVCP7 = Convert.ToByte(ee2232h.BIsVCP);
                    eedata.PowerSaveEnable = Convert.ToByte(ee2232h.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)
                {
                    MessageBox.Show("Failed to load function FT_EE_Program.");
                }
            }
            return ftStatus;
        }
Beispiel #2
0
        //**************************************************************************
        // ReadFT2232HEEPROM
        //**************************************************************************
        // Intellisense comments
        /// <summary>
        /// Reads the EEPROM contents of an FT2232H device.
        /// </summary>
        /// <returns>FT_STATUS value from FT_EE_Read in FTD2XX DLL</returns>
        /// <param name="ee2232h">An FT2232H_EEPROM_STRUCTURE which contains only the relevant information for an FT2232H device.</param>
        /// <exception cref="FT_EXCEPTION">Thrown when the current device does not match the type required by this method.</exception>
        public FT_STATUS ReadFT2232HEEPROM(FT2232H_EEPROM_STRUCTURE ee2232h)
        {
            // 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_Read != IntPtr.Zero)
            {
                tFT_EE_Read FT_EE_Read = (tFT_EE_Read)Marshal.GetDelegateForFunctionPointer(pFT_EE_Read, typeof(tFT_EE_Read));

                if (ftHandle != IntPtr.Zero)
                {
                    FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN;
                    // Check that it is an FT2232H that we are trying to read
                    GetDeviceType(ref DeviceType);
                    if (DeviceType != FT_DEVICE.FT_DEVICE_2232H)
                    {
                        // If it is not, throw an exception
                        ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE;
                        ErrorHandler(ftStatus, ftErrorCondition);
                    }

                    FT_PROGRAM_DATA eedata = new FT_PROGRAM_DATA();

                    // Set up structure headers
                    eedata.Signature1 = 0x00000000;
                    eedata.Signature2 = 0xFFFFFFFF;
                    eedata.Version = 3;

                    // 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);

                    // Call FT_EE_Read
                    ftStatus = FT_EE_Read(ftHandle, eedata);

                    // Retrieve string values
                    ee2232h.Manufacturer = Marshal.PtrToStringAnsi(eedata.Manufacturer);
                    ee2232h.ManufacturerID = Marshal.PtrToStringAnsi(eedata.ManufacturerID);
                    ee2232h.Description = Marshal.PtrToStringAnsi(eedata.Description);
                    ee2232h.SerialNumber = Marshal.PtrToStringAnsi(eedata.SerialNumber);

                    // Free unmanaged buffers
                    Marshal.FreeHGlobal(eedata.Manufacturer);
                    Marshal.FreeHGlobal(eedata.ManufacturerID);
                    Marshal.FreeHGlobal(eedata.Description);
                    Marshal.FreeHGlobal(eedata.SerialNumber);

                    // Map non-string elements to structure to be returned
                    // Standard elements
                    ee2232h.VendorID = eedata.VendorID;
                    ee2232h.ProductID = eedata.ProductID;
                    ee2232h.MaxPower = eedata.MaxPower;
                    ee2232h.SelfPowered = Convert.ToBoolean(eedata.SelfPowered);
                    ee2232h.RemoteWakeup = Convert.ToBoolean(eedata.RemoteWakeup);
                    // 2232H specific fields
                    ee2232h.PullDownEnable = Convert.ToBoolean(eedata.PullDownEnable7);
                    ee2232h.SerNumEnable = Convert.ToBoolean(eedata.SerNumEnable7);
                    ee2232h.ALSlowSlew = Convert.ToBoolean(eedata.ALSlowSlew);
                    ee2232h.ALSchmittInput = Convert.ToBoolean(eedata.ALSchmittInput);
                    ee2232h.ALDriveCurrent = eedata.ALDriveCurrent;
                    ee2232h.AHSlowSlew = Convert.ToBoolean(eedata.AHSlowSlew);
                    ee2232h.AHSchmittInput = Convert.ToBoolean(eedata.AHSchmittInput);
                    ee2232h.AHDriveCurrent = eedata.AHDriveCurrent;
                    ee2232h.BLSlowSlew = Convert.ToBoolean(eedata.BLSlowSlew);
                    ee2232h.BLSchmittInput = Convert.ToBoolean(eedata.BLSchmittInput);
                    ee2232h.BLDriveCurrent = eedata.BLDriveCurrent;
                    ee2232h.BHSlowSlew = Convert.ToBoolean(eedata.BHSlowSlew);
                    ee2232h.BHSchmittInput = Convert.ToBoolean(eedata.BHSchmittInput);
                    ee2232h.BHDriveCurrent = eedata.BHDriveCurrent;
                    ee2232h.IFAIsFifo = Convert.ToBoolean(eedata.IFAIsFifo7);
                    ee2232h.IFAIsFifoTar = Convert.ToBoolean(eedata.IFAIsFifoTar7);
                    ee2232h.IFAIsFastSer = Convert.ToBoolean(eedata.IFAIsFastSer7);
                    ee2232h.AIsVCP = Convert.ToBoolean(eedata.AIsVCP7);
                    ee2232h.IFBIsFifo = Convert.ToBoolean(eedata.IFBIsFifo7);
                    ee2232h.IFBIsFifoTar = Convert.ToBoolean(eedata.IFBIsFifoTar7);
                    ee2232h.IFBIsFastSer = Convert.ToBoolean(eedata.IFBIsFastSer7);
                    ee2232h.BIsVCP = Convert.ToBoolean(eedata.BIsVCP7);
                    ee2232h.PowerSaveEnable = Convert.ToBoolean(eedata.PowerSaveEnable);
                }
            }
            else
            {
                if (pFT_EE_Read == IntPtr.Zero)
                {
                    MessageBox.Show("Failed to load function FT_EE_Read.");
                }
            }
            return ftStatus;
        }