From d88cba13a901d79bca7770bf840b48da5f586795 Mon Sep 17 00:00:00 2001 From: Chris Tacke Date: Sun, 4 Feb 2024 19:39:23 -0600 Subject: [PATCH 01/11] ft23xx...maybe? --- .../Samples/Ili9488Charts_Sample/MeadowApp.cs | 4 +- .../Samples/MAX7219_Sample/MeadowApp.cs | 2 +- .../FTD2XX/FTDI.FT2232H_EEPROM_STRUCTURE.cs | 130 + .../FTD2XX/FTDI.FT2232_EEPROM_STRUCTURE.cs | 103 + .../FTD2XX/FTDI.FT232B_EEPROM_STRUCTURE.cs | 61 + .../FTD2XX/FTDI.FT232H_EEPROM_STRUCTURE.cs | 171 + .../FTD2XX/FTDI.FT232R_EEPROM_STRUCTURE.cs | 130 + .../FTD2XX/FTDI.FT4232H_EEPROM_STRUCTURE.cs | 126 + .../FTD2XX/FTDI.FT_232H_CBUS_OPTIONS.cs | 88 + .../Driver/FTD2XX/FTDI.FT_BIT_MODES.cs | 70 + .../Driver/FTD2XX/FTDI.FT_CBUS_OPTIONS.cs | 90 + .../Driver/FTD2XX/FTDI.FT_DATA_BITS.cs | 46 + .../Driver/FTD2XX/FTDI.FT_DEVICE.cs | 138 + .../Driver/FTD2XX/FTDI.FT_DEVICE_INFO_NODE.cs | 71 + .../Driver/FTD2XX/FTDI.FT_DRIVE_CURRENT.cs | 54 + .../Driver/FTD2XX/FTDI.FT_EEPROM_DATA.cs | 82 + .../Driver/FTD2XX/FTDI.FT_EEPROM_HEADER.cs | 51 + .../Driver/FTD2XX/FTDI.FT_ERROR.cs | 42 + .../Driver/FTD2XX/FTDI.FT_EVENTS.cs | 54 + .../Driver/FTD2XX/FTDI.FT_EXCEPTION.cs | 66 + .../Driver/FTD2XX/FTDI.FT_FLAGS.cs | 50 + .../Driver/FTD2XX/FTDI.FT_FLOW_CONTROL.cs | 58 + .../Driver/FTD2XX/FTDI.FT_LINE_STATUS.cs | 54 + .../Driver/FTD2XX/FTDI.FT_MODEM_STATUS.cs | 54 + .../Driver/FTD2XX/FTDI.FT_PARITY.cs | 58 + .../Driver/FTD2XX/FTDI.FT_PROGRAM_DATA.cs | 180 + .../Driver/FTD2XX/FTDI.FT_PURGE.cs | 46 + .../Driver/FTD2XX/FTDI.FT_STATUS.cs | 110 + .../Driver/FTD2XX/FTDI.FT_STOP_BITS.cs | 46 + .../FTD2XX/FTDI.FT_XSERIES_CBUS_OPTIONS.cs | 125 + .../Driver/FTD2XX/FTDI.FT_XSERIES_DATA.cs | 82 + .../FTDI.FT_XSERIES_EEPROM_STRUCTURE.cs | 218 + .../Driver/FTD2XX/FTDI.cs | 4901 +++++++++++++++++ .../Driver/Ft232Collection.cs | 140 + .../Driver/Ft232SpiBus.cs | 4 +- .../Driver/Ft232h.PinDefinitions.cs | 4 +- .../ICs.IOExpanders.Ft232h/Driver/Ft232h.cs | 20 +- .../Driver/Ft232h.mpsse.cs | 4 +- .../Driver/Native.Mpsse.cs | 2 +- .../ICs.IOExpanders.Ft232h/Driver/Native.cs | 2 +- .../Driver/SPITransferOptions.cs | 2 +- .../Driver/SpiConfigOptions.cs | 2 +- .../Samples/Ft232h_Windows_Sample/Program.cs | 14 +- 43 files changed, 7727 insertions(+), 28 deletions(-) create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT2232H_EEPROM_STRUCTURE.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT2232_EEPROM_STRUCTURE.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT232B_EEPROM_STRUCTURE.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT232H_EEPROM_STRUCTURE.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT232R_EEPROM_STRUCTURE.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT4232H_EEPROM_STRUCTURE.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_232H_CBUS_OPTIONS.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_BIT_MODES.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_CBUS_OPTIONS.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DATA_BITS.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DEVICE.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DEVICE_INFO_NODE.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DRIVE_CURRENT.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EEPROM_DATA.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EEPROM_HEADER.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_ERROR.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EVENTS.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EXCEPTION.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_FLAGS.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_FLOW_CONTROL.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_LINE_STATUS.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_MODEM_STATUS.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_PARITY.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_PROGRAM_DATA.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_PURGE.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_STATUS.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_STOP_BITS.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_XSERIES_CBUS_OPTIONS.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_XSERIES_DATA.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_XSERIES_EEPROM_STRUCTURE.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232Collection.cs diff --git a/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroLayout/Samples/Ili9488Charts_Sample/MeadowApp.cs b/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroLayout/Samples/Ili9488Charts_Sample/MeadowApp.cs index 197bf86bc6..d350442151 100644 --- a/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroLayout/Samples/Ili9488Charts_Sample/MeadowApp.cs +++ b/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroLayout/Samples/Ili9488Charts_Sample/MeadowApp.cs @@ -7,12 +7,12 @@ public class MeadowApp : App { - private Ft232h expander = new Ft232h(); + private Ft232h_old expander = new Ft232h_old(); private DisplayScreen? screen; public override Task Initialize() { - expander = new Ft232h(); + expander = new Ft232h_old(); var display = new Ili9341 ( diff --git a/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroLayout/Samples/MAX7219_Sample/MeadowApp.cs b/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroLayout/Samples/MAX7219_Sample/MeadowApp.cs index 61541c509e..d322b4b0d2 100644 --- a/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroLayout/Samples/MAX7219_Sample/MeadowApp.cs +++ b/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroLayout/Samples/MAX7219_Sample/MeadowApp.cs @@ -7,7 +7,7 @@ public class MeadowApp : App { - private readonly Ft232h expander = new Ft232h(); + private readonly Ft232h_old expander = new Ft232h_old(); private DisplayScreen? screen; public override Task Initialize() diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT2232H_EEPROM_STRUCTURE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT2232H_EEPROM_STRUCTURE.cs new file mode 100644 index 0000000000..f9e531949b --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT2232H_EEPROM_STRUCTURE.cs @@ -0,0 +1,130 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // EEPROM class for FT2232H + /// + /// EEPROM structure specific to FT2232H devices. + /// Inherits from FT_EEPROM_DATA. + /// + public class FT2232H_EEPROM_STRUCTURE : FT_EEPROM_DATA + { + /// + /// Determines if IOs are pulled down when the device is in suspend + /// + public bool PullDownEnable = false; + /// + /// Determines if the serial number is enabled + /// + public bool SerNumEnable = true; + /// + /// Determines if AL pins have a slow slew rate + /// + public bool ALSlowSlew = false; + /// + /// Determines if the AL pins have a Schmitt input + /// + public bool ALSchmittInput = false; + /// + /// Determines the AL pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA + /// + public byte ALDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA; + /// + /// Determines if AH pins have a slow slew rate + /// + public bool AHSlowSlew = false; + /// + /// Determines if the AH pins have a Schmitt input + /// + public bool AHSchmittInput = false; + /// + /// Determines the AH pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA + /// + public byte AHDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA; + /// + /// Determines if BL pins have a slow slew rate + /// + public bool BLSlowSlew = false; + /// + /// Determines if the BL pins have a Schmitt input + /// + public bool BLSchmittInput = false; + /// + /// Determines the BL pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA + /// + public byte BLDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA; + /// + /// Determines if BH pins have a slow slew rate + /// + public bool BHSlowSlew = false; + /// + /// Determines if the BH pins have a Schmitt input + /// + public bool BHSchmittInput = false; + /// + /// Determines the BH pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA + /// + public byte BHDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA; + /// + /// Determines if channel A is in FIFO mode + /// + public bool IFAIsFifo = false; + /// + /// Determines if channel A is in FIFO target mode + /// + public bool IFAIsFifoTar = false; + /// + /// Determines if channel A is in fast serial mode + /// + public bool IFAIsFastSer = false; + /// + /// Determines if channel A loads the VCP driver + /// + public bool AIsVCP = true; + /// + /// Determines if channel B is in FIFO mode + /// + public bool IFBIsFifo = false; + /// + /// Determines if channel B is in FIFO target mode + /// + public bool IFBIsFifoTar = false; + /// + /// Determines if channel B is in fast serial mode + /// + public bool IFBIsFastSer = false; + /// + /// Determines if channel B loads the VCP driver + /// + public bool BIsVCP = true; + /// + /// For self-powered designs, keeps the FT2232H in low power state until BCBUS7 is high + /// + public bool PowerSaveEnable = false; + } +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT2232_EEPROM_STRUCTURE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT2232_EEPROM_STRUCTURE.cs new file mode 100644 index 0000000000..a0aa852816 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT2232_EEPROM_STRUCTURE.cs @@ -0,0 +1,103 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + + +using System; + + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // EEPROM class for FT2232C, FT2232L and FT2232D + /// + /// EEPROM structure specific to FT2232 devices. + /// Inherits from FT_EEPROM_DATA. + /// + public class FT2232_EEPROM_STRUCTURE : FT_EEPROM_DATA + { + //private bool Rev5 = true; + //private bool IsoInA = false; + //private bool IsoInB = false; + //private bool IsoOutA = false; + //private bool IsoOutB = false; + /// + /// Determines if IOs are pulled down when the device is in suspend + /// + public bool PullDownEnable = false; + /// + /// Determines if the serial number is enabled + /// + public bool SerNumEnable = true; + /// + /// Determines if the USB version number is enabled + /// + public bool USBVersionEnable = true; + /// + /// The USB version number. Should be either 0x0110 (USB 1.1) or 0x0200 (USB 2.0) + /// + public UInt16 USBVersion = 0x0200; + /// + /// Enables high current IOs on channel A + /// + public bool AIsHighCurrent = false; + /// + /// Enables high current IOs on channel B + /// + public bool BIsHighCurrent = false; + /// + /// Determines if channel A is in FIFO mode + /// + public bool IFAIsFifo = false; + /// + /// Determines if channel A is in FIFO target mode + /// + public bool IFAIsFifoTar = false; + /// + /// Determines if channel A is in fast serial mode + /// + public bool IFAIsFastSer = false; + /// + /// Determines if channel A loads the VCP driver + /// + public bool AIsVCP = true; + /// + /// Determines if channel B is in FIFO mode + /// + public bool IFBIsFifo = false; + /// + /// Determines if channel B is in FIFO target mode + /// + public bool IFBIsFifoTar = false; + /// + /// Determines if channel B is in fast serial mode + /// + public bool IFBIsFastSer = false; + /// + /// Determines if channel B loads the VCP driver + /// + public bool BIsVCP = true; + } +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT232B_EEPROM_STRUCTURE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT232B_EEPROM_STRUCTURE.cs new file mode 100644 index 0000000000..021c462b1b --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT232B_EEPROM_STRUCTURE.cs @@ -0,0 +1,61 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + + +using System; + + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // EEPROM class for FT232B and FT245B + /// + /// EEPROM structure specific to FT232B and FT245B devices. + /// Inherits from FT_EEPROM_DATA. + /// + public class FT232B_EEPROM_STRUCTURE : FT_EEPROM_DATA + { + //private bool Rev4 = true; + //private bool IsoIn = false; + //private bool IsoOut = false; + /// + /// Determines if IOs are pulled down when the device is in suspend + /// + public bool PullDownEnable = false; + /// + /// Determines if the serial number is enabled + /// + public bool SerNumEnable = true; + /// + /// Determines if the USB version number is enabled + /// + public bool USBVersionEnable = true; + /// + /// The USB version number. Should be either 0x0110 (USB 1.1) or 0x0200 (USB 2.0) + /// + public UInt16 USBVersion = 0x0200; + } +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT232H_EEPROM_STRUCTURE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT232H_EEPROM_STRUCTURE.cs new file mode 100644 index 0000000000..67fe3c2a85 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT232H_EEPROM_STRUCTURE.cs @@ -0,0 +1,171 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // EEPROM class for FT232H + /// + /// EEPROM structure specific to FT232H devices. + /// Inherits from FT_EEPROM_DATA. + /// + public class FT232H_EEPROM_STRUCTURE : FT_EEPROM_DATA + { + /// + /// Determines if IOs are pulled down when the device is in suspend + /// + public bool PullDownEnable = false; + /// + /// Determines if the serial number is enabled + /// + public bool SerNumEnable = true; + /// + /// Determines if AC pins have a slow slew rate + /// + public bool ACSlowSlew = false; + /// + /// Determines if the AC pins have a Schmitt input + /// + public bool ACSchmittInput = false; + /// + /// Determines the AC pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA + /// + public byte ACDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA; + /// + /// Determines if AD pins have a slow slew rate + /// + public bool ADSlowSlew = false; + /// + /// Determines if the AD pins have a Schmitt input + /// + public bool ADSchmittInput = false; + /// + /// Determines the AD pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA + /// + public byte ADDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA; + /// + /// Sets the function of the CBUS0 pin for FT232H devices. + /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, + /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN, FT_CBUS_CLK30, + /// FT_CBUS_CLK15, FT_CBUS_CLK7_5 + /// + public byte Cbus0 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE; + /// + /// Sets the function of the CBUS1 pin for FT232H devices. + /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, + /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN, FT_CBUS_CLK30, + /// FT_CBUS_CLK15, FT_CBUS_CLK7_5 + /// + public byte Cbus1 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE; + /// + /// Sets the function of the CBUS2 pin for FT232H devices. + /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, + /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN + /// + public byte Cbus2 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE; + /// + /// Sets the function of the CBUS3 pin for FT232H devices. + /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, + /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN + /// + public byte Cbus3 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE; + /// + /// Sets the function of the CBUS4 pin for FT232H devices. + /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, + /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN + /// + public byte Cbus4 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE; + /// + /// Sets the function of the CBUS5 pin for FT232H devices. + /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, + /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_IOMODE, + /// FT_CBUS_TXDEN, FT_CBUS_CLK30, FT_CBUS_CLK15, FT_CBUS_CLK7_5 + /// + public byte Cbus5 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE; + /// + /// Sets the function of the CBUS6 pin for FT232H devices. + /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, + /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_IOMODE, + /// FT_CBUS_TXDEN, FT_CBUS_CLK30, FT_CBUS_CLK15, FT_CBUS_CLK7_5 + /// + public byte Cbus6 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE; + /// + /// Sets the function of the CBUS7 pin for FT232H devices. + /// Valid values are FT_CBUS_TRISTATE + /// + public byte Cbus7 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE; + /// + /// Sets the function of the CBUS8 pin for FT232H devices. + /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, + /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_IOMODE, + /// FT_CBUS_TXDEN, FT_CBUS_CLK30, FT_CBUS_CLK15, FT_CBUS_CLK7_5 + /// + public byte Cbus8 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE; + /// + /// Sets the function of the CBUS9 pin for FT232H devices. + /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, + /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_IOMODE, + /// FT_CBUS_TXDEN, FT_CBUS_CLK30, FT_CBUS_CLK15, FT_CBUS_CLK7_5 + /// + public byte Cbus9 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE; + /// + /// Determines if the device is in FIFO mode + /// + public bool IsFifo = false; + /// + /// Determines if the device is in FIFO target mode + /// + public bool IsFifoTar = false; + /// + /// Determines if the device is in fast serial mode + /// + public bool IsFastSer = false; + /// + /// Determines if the device is in FT1248 mode + /// + public bool IsFT1248 = false; + /// + /// Determines FT1248 mode clock polarity + /// + public bool FT1248Cpol = false; + /// + /// Determines if data is ent MSB (0) or LSB (1) in FT1248 mode + /// + public bool FT1248Lsb = false; + /// + /// Determines if FT1248 mode uses flow control + /// + public bool FT1248FlowControl = false; + /// + /// Determines if the VCP driver is loaded + /// + public bool IsVCP = true; + /// + /// For self-powered designs, keeps the FT232H in low power state until ACBUS7 is high + /// + public bool PowerSaveEnable = false; + } +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT232R_EEPROM_STRUCTURE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT232R_EEPROM_STRUCTURE.cs new file mode 100644 index 0000000000..ee6e968336 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT232R_EEPROM_STRUCTURE.cs @@ -0,0 +1,130 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // EEPROM class for FT232R and FT245R + /// + /// EEPROM structure specific to FT232R and FT245R devices. + /// Inherits from FT_EEPROM_DATA. + /// + public class FT232R_EEPROM_STRUCTURE : FT_EEPROM_DATA + { + /// + /// Disables the FT232R internal clock source. + /// If the device has external oscillator enabled it must have an external oscillator fitted to function + /// + public bool UseExtOsc = false; + /// + /// Enables high current IOs + /// + public bool HighDriveIOs = false; + /// + /// Sets the endpoint size. This should always be set to 64 + /// + public byte EndpointSize = 64; + /// + /// Determines if IOs are pulled down when the device is in suspend + /// + public bool PullDownEnable = false; + /// + /// Determines if the serial number is enabled + /// + public bool SerNumEnable = true; + /// + /// Inverts the sense of the TXD line + /// + public bool InvertTXD = false; + /// + /// Inverts the sense of the RXD line + /// + public bool InvertRXD = false; + /// + /// Inverts the sense of the RTS line + /// + public bool InvertRTS = false; + /// + /// Inverts the sense of the CTS line + /// + public bool InvertCTS = false; + /// + /// Inverts the sense of the DTR line + /// + public bool InvertDTR = false; + /// + /// Inverts the sense of the DSR line + /// + public bool InvertDSR = false; + /// + /// Inverts the sense of the DCD line + /// + public bool InvertDCD = false; + /// + /// Inverts the sense of the RI line + /// + public bool InvertRI = false; + /// + /// Sets the function of the CBUS0 pin for FT232R devices. + /// Valid values are FT_CBUS_TXDEN, FT_CBUS_PWRON , FT_CBUS_RXLED, FT_CBUS_TXLED, + /// FT_CBUS_TXRXLED, FT_CBUS_SLEEP, FT_CBUS_CLK48, FT_CBUS_CLK24, FT_CBUS_CLK12, + /// FT_CBUS_CLK6, FT_CBUS_IOMODE, FT_CBUS_BITBANG_WR, FT_CBUS_BITBANG_RD + /// + public byte Cbus0 = FT_CBUS_OPTIONS.FT_CBUS_SLEEP; + /// + /// Sets the function of the CBUS1 pin for FT232R devices. + /// Valid values are FT_CBUS_TXDEN, FT_CBUS_PWRON , FT_CBUS_RXLED, FT_CBUS_TXLED, + /// FT_CBUS_TXRXLED, FT_CBUS_SLEEP, FT_CBUS_CLK48, FT_CBUS_CLK24, FT_CBUS_CLK12, + /// FT_CBUS_CLK6, FT_CBUS_IOMODE, FT_CBUS_BITBANG_WR, FT_CBUS_BITBANG_RD + /// + public byte Cbus1 = FT_CBUS_OPTIONS.FT_CBUS_SLEEP; + /// + /// Sets the function of the CBUS2 pin for FT232R devices. + /// Valid values are FT_CBUS_TXDEN, FT_CBUS_PWRON , FT_CBUS_RXLED, FT_CBUS_TXLED, + /// FT_CBUS_TXRXLED, FT_CBUS_SLEEP, FT_CBUS_CLK48, FT_CBUS_CLK24, FT_CBUS_CLK12, + /// FT_CBUS_CLK6, FT_CBUS_IOMODE, FT_CBUS_BITBANG_WR, FT_CBUS_BITBANG_RD + /// + public byte Cbus2 = FT_CBUS_OPTIONS.FT_CBUS_SLEEP; + /// + /// Sets the function of the CBUS3 pin for FT232R devices. + /// Valid values are FT_CBUS_TXDEN, FT_CBUS_PWRON , FT_CBUS_RXLED, FT_CBUS_TXLED, + /// FT_CBUS_TXRXLED, FT_CBUS_SLEEP, FT_CBUS_CLK48, FT_CBUS_CLK24, FT_CBUS_CLK12, + /// FT_CBUS_CLK6, FT_CBUS_IOMODE, FT_CBUS_BITBANG_WR, FT_CBUS_BITBANG_RD + /// + public byte Cbus3 = FT_CBUS_OPTIONS.FT_CBUS_SLEEP; + /// + /// Sets the function of the CBUS4 pin for FT232R devices. + /// Valid values are FT_CBUS_TXDEN, FT_CBUS_PWRON , FT_CBUS_RXLED, FT_CBUS_TXLED, + /// FT_CBUS_TXRXLED, FT_CBUS_SLEEP, FT_CBUS_CLK48, FT_CBUS_CLK24, FT_CBUS_CLK12, + /// FT_CBUS_CLK6 + /// + public byte Cbus4 = FT_CBUS_OPTIONS.FT_CBUS_SLEEP; + /// + /// Determines if the VCP driver is loaded + /// + public bool RIsD2XX = false; + } +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT4232H_EEPROM_STRUCTURE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT4232H_EEPROM_STRUCTURE.cs new file mode 100644 index 0000000000..bc219ec651 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT4232H_EEPROM_STRUCTURE.cs @@ -0,0 +1,126 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // EEPROM class for FT4232H + /// + /// EEPROM structure specific to FT4232H devices. + /// Inherits from FT_EEPROM_DATA. + /// + public class FT4232H_EEPROM_STRUCTURE : FT_EEPROM_DATA + { + /// + /// Determines if IOs are pulled down when the device is in suspend + /// + public bool PullDownEnable = false; + /// + /// Determines if the serial number is enabled + /// + public bool SerNumEnable = true; + /// + /// Determines if A pins have a slow slew rate + /// + public bool ASlowSlew = false; + /// + /// Determines if the A pins have a Schmitt input + /// + public bool ASchmittInput = false; + /// + /// Determines the A pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA + /// + public byte ADriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA; + /// + /// Determines if B pins have a slow slew rate + /// + public bool BSlowSlew = false; + /// + /// Determines if the B pins have a Schmitt input + /// + public bool BSchmittInput = false; + /// + /// Determines the B pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA + /// + public byte BDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA; + /// + /// Determines if C pins have a slow slew rate + /// + public bool CSlowSlew = false; + /// + /// Determines if the C pins have a Schmitt input + /// + public bool CSchmittInput = false; + /// + /// Determines the C pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA + /// + public byte CDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA; + /// + /// Determines if D pins have a slow slew rate + /// + public bool DSlowSlew = false; + /// + /// Determines if the D pins have a Schmitt input + /// + public bool DSchmittInput = false; + /// + /// Determines the D pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA + /// + public byte DDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA; + /// + /// RI of port A acts as RS485 transmit enable (TXDEN) + /// + public bool ARIIsTXDEN = false; + /// + /// RI of port B acts as RS485 transmit enable (TXDEN) + /// + public bool BRIIsTXDEN = false; + /// + /// RI of port C acts as RS485 transmit enable (TXDEN) + /// + public bool CRIIsTXDEN = false; + /// + /// RI of port D acts as RS485 transmit enable (TXDEN) + /// + public bool DRIIsTXDEN = false; + /// + /// Determines if channel A loads the VCP driver + /// + public bool AIsVCP = true; + /// + /// Determines if channel B loads the VCP driver + /// + public bool BIsVCP = true; + /// + /// Determines if channel C loads the VCP driver + /// + public bool CIsVCP = true; + /// + /// Determines if channel D loads the VCP driver + /// + public bool DIsVCP = true; + } +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_232H_CBUS_OPTIONS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_232H_CBUS_OPTIONS.cs new file mode 100644 index 0000000000..154963096e --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_232H_CBUS_OPTIONS.cs @@ -0,0 +1,88 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // FT232H CBUS Options + /// + /// Available functions for the FT232H CBUS pins. Controlled by FT232H EEPROM settings + /// + public class FT_232H_CBUS_OPTIONS + { + /// + /// FT232H CBUS EEPROM options - Tristate + /// + public const byte FT_CBUS_TRISTATE = 0x00; + /// + /// FT232H CBUS EEPROM options - Rx LED + /// + public const byte FT_CBUS_RXLED = 0x01; + /// + /// FT232H CBUS EEPROM options - Tx LED + /// + public const byte FT_CBUS_TXLED = 0x02; + /// + /// FT232H CBUS EEPROM options - Tx and Rx LED + /// + public const byte FT_CBUS_TXRXLED = 0x03; + /// + /// FT232H CBUS EEPROM options - Power Enable# + /// + public const byte FT_CBUS_PWREN = 0x04; + /// + /// FT232H CBUS EEPROM options - Sleep + /// + public const byte FT_CBUS_SLEEP = 0x05; + /// + /// FT232H CBUS EEPROM options - Drive pin to logic 0 + /// + public const byte FT_CBUS_DRIVE_0 = 0x06; + /// + /// FT232H CBUS EEPROM options - Drive pin to logic 1 + /// + public const byte FT_CBUS_DRIVE_1 = 0x07; + /// + /// FT232H CBUS EEPROM options - IO Mode + /// + public const byte FT_CBUS_IOMODE = 0x08; + /// + /// FT232H CBUS EEPROM options - Tx Data Enable + /// + public const byte FT_CBUS_TXDEN = 0x09; + /// + /// FT232H CBUS EEPROM options - 30MHz clock + /// + public const byte FT_CBUS_CLK30 = 0x0A; + /// + /// FT232H CBUS EEPROM options - 15MHz clock + /// + public const byte FT_CBUS_CLK15 = 0x0B;/// + /// FT232H CBUS EEPROM options - 7.5MHz clock + /// + public const byte FT_CBUS_CLK7_5 = 0x0C; + } +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_BIT_MODES.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_BIT_MODES.cs new file mode 100644 index 0000000000..e37b3e7b64 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_BIT_MODES.cs @@ -0,0 +1,70 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // Bit modes + /// + /// Permitted bit mode values for FTDI devices. For use with SetBitMode + /// + public class FT_BIT_MODES + { + /// + /// Reset bit mode + /// + public const byte FT_BIT_MODE_RESET = 0x00; + /// + /// Asynchronous bit-bang mode + /// + public const byte FT_BIT_MODE_ASYNC_BITBANG = 0x01; + /// + /// MPSSE bit mode - only available on FT2232, FT2232H, FT4232H and FT232H + /// + public const byte FT_BIT_MODE_MPSSE = 0x02; + /// + /// Synchronous bit-bang mode + /// + public const byte FT_BIT_MODE_SYNC_BITBANG = 0x04; + /// + /// MCU host bus emulation mode - only available on FT2232, FT2232H, FT4232H and FT232H + /// + public const byte FT_BIT_MODE_MCU_HOST = 0x08; + /// + /// Fast opto-isolated serial mode - only available on FT2232, FT2232H, FT4232H and FT232H + /// + public const byte FT_BIT_MODE_FAST_SERIAL = 0x10; + /// + /// CBUS bit-bang mode - only available on FT232R and FT232H + /// + public const byte FT_BIT_MODE_CBUS_BITBANG = 0x20; + /// + /// Single channel synchronous 245 FIFO mode - only available on FT2232H channel A and FT232H + /// + public const byte FT_BIT_MODE_SYNC_FIFO = 0x40; + } + +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_CBUS_OPTIONS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_CBUS_OPTIONS.cs new file mode 100644 index 0000000000..d5add3a63e --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_CBUS_OPTIONS.cs @@ -0,0 +1,90 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // FT232R CBUS Options + /// + /// Available functions for the FT232R CBUS pins. Controlled by FT232R EEPROM settings + /// + public class FT_CBUS_OPTIONS + { + /// + /// FT232R CBUS EEPROM options - Tx Data Enable + /// + public const byte FT_CBUS_TXDEN = 0x00; + /// + /// FT232R CBUS EEPROM options - Power On + /// + public const byte FT_CBUS_PWRON = 0x01; + /// + /// FT232R CBUS EEPROM options - Rx LED + /// + public const byte FT_CBUS_RXLED = 0x02; + /// + /// FT232R CBUS EEPROM options - Tx LED + /// + public const byte FT_CBUS_TXLED = 0x03; + /// + /// FT232R CBUS EEPROM options - Tx and Rx LED + /// + public const byte FT_CBUS_TXRXLED = 0x04; + /// + /// FT232R CBUS EEPROM options - Sleep + /// + public const byte FT_CBUS_SLEEP = 0x05; + /// + /// FT232R CBUS EEPROM options - 48MHz clock + /// + public const byte FT_CBUS_CLK48 = 0x06; + /// + /// FT232R CBUS EEPROM options - 24MHz clock + /// + public const byte FT_CBUS_CLK24 = 0x07; + /// + /// FT232R CBUS EEPROM options - 12MHz clock + /// + public const byte FT_CBUS_CLK12 = 0x08; + /// + /// FT232R CBUS EEPROM options - 6MHz clock + /// + public const byte FT_CBUS_CLK6 = 0x09; + /// + /// FT232R CBUS EEPROM options - IO mode + /// + public const byte FT_CBUS_IOMODE = 0x0A; + /// + /// FT232R CBUS EEPROM options - Bit-bang write strobe + /// + public const byte FT_CBUS_BITBANG_WR = 0x0B; + /// + /// FT232R CBUS EEPROM options - Bit-bang read strobe + /// + public const byte FT_CBUS_BITBANG_RD = 0x0C; + } + +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DATA_BITS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DATA_BITS.cs new file mode 100644 index 0000000000..1a274c7952 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DATA_BITS.cs @@ -0,0 +1,46 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // Word Lengths + /// + /// Permitted data bits for FTDI devices + /// + public class FT_DATA_BITS + { + /// + /// 8 data bits + /// + public const byte FT_BITS_8 = 0x08; + /// + /// 7 data bits + /// + public const byte FT_BITS_7 = 0x07; + } + +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DEVICE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DEVICE.cs new file mode 100644 index 0000000000..9f7e16baa2 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DEVICE.cs @@ -0,0 +1,138 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // Device type identifiers for FT_GetDeviceInfoDetail and FT_GetDeviceInfo + /// + /// List of FTDI device types + /// + public enum FT_DEVICE + { + /// + /// FT232B or FT245B device + /// + FT_DEVICE_BM = 0, + /// + /// FT8U232AM or FT8U245AM device + /// + FT_DEVICE_AM, /// 1 + /// + /// FT8U100AX device + /// + FT_DEVICE_100AX, + /// + /// Unknown device + /// + FT_DEVICE_UNKNOWN, + /// + /// FT2232 device + /// + FT_DEVICE_2232, + /// + /// FT232R or FT245R device + /// + FT_DEVICE_232R, /// 5 + /// + /// FT2232H device + /// + FT_DEVICE_2232H, /// 6 + /// + /// FT4232H device + /// + FT_DEVICE_4232H, /// 7 + /// + /// FT232H device + /// + FT_DEVICE_232H, /// 8 + /// + /// FT X-Series device + /// + FT_DEVICE_X_SERIES, /// 9 + /// + /// FT4222 hi-speed device Mode 0 - 2 interfaces + /// + FT_DEVICE_4222H_0, /// 10 + /// + /// FT4222 hi-speed device Mode 1 or 2 - 4 interfaces + /// + FT_DEVICE_4222H_1_2, /// 11 + /// + /// FT4222 hi-speed device Mode 3 - 1 interface + /// + FT_DEVICE_4222H_3, /// 12 + /// + /// OTP programmer board for the FT4222. + /// + FT_DEVICE_4222_PROG, /// 13 + /// + /// OTP programmer board for the FT900. + /// + FT_DEVICE_FT900, /// 14 + /// + /// OTP programmer board for the FT930. + /// + FT_DEVICE_FT930, /// 15 + /// + /// Flash programmer board for the UMFTPD3A. + /// + FT_DEVICE_UMFTPD3A, /// 16 + /// + /// FT2233HP hi-speed device. + /// + FT_DEVICE_2233HP, /// 17 + /// + /// FT4233HP hi-speed device. + /// + FT_DEVICE_4233HP, /// 18 + /// + /// FT2233HP hi-speed device. + /// + FT_DEVICE_2232HP, /// 19 + /// + /// FT4233HP hi-speed device. + /// + FT_DEVICE_4232HP, /// 20 + /// + /// FT233HP hi-speed device. + /// + FT_DEVICE_233HP, /// 21 + /// + /// FT232HP hi-speed device. + /// + FT_DEVICE_232HP, /// 22 + /// + /// FT2233HA hi-speed device. + /// + FT_DEVICE_2232HA, /// 23 + /// + /// FT4233HA hi-speed device. + /// + FT_DEVICE_4232HA, /// 24 + }; + +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DEVICE_INFO_NODE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DEVICE_INFO_NODE.cs new file mode 100644 index 0000000000..b6e57f7d2d --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DEVICE_INFO_NODE.cs @@ -0,0 +1,71 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + + +using System; + + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + /// + /// Type that holds device information for GetDeviceInformation method. + /// Used with FT_GetDeviceInfo and FT_GetDeviceInfoDetail in FTD2XX.DLL + /// + public class FT_DEVICE_INFO_NODE + { + /// + /// Indicates device state. Can be any combination of the following: FT_FLAGS_OPENED, FT_FLAGS_HISPEED + /// + public UInt32 Flags; + /// + /// Indicates the device type. Can be one of the following: FT_DEVICE_232R, FT_DEVICE_2232C, FT_DEVICE_BM, FT_DEVICE_AM, FT_DEVICE_100AX or FT_DEVICE_UNKNOWN + /// + public FT_DEVICE Type; + /// + /// The Vendor ID and Product ID of the device + /// + public UInt32 ID; + /// + /// The physical location identifier of the device + /// + public UInt32 LocId; + /// + /// The device serial number + /// + public string SerialNumber; + /// + /// The device description + /// + public string Description; + /// + /// The device handle. This value is not used externally and is provided for information only. + /// If the device is not open, this value is 0. + /// + public IntPtr ftHandle; + } + +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DRIVE_CURRENT.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DRIVE_CURRENT.cs new file mode 100644 index 0000000000..ac8f845e1c --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DRIVE_CURRENT.cs @@ -0,0 +1,54 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // Valid drive current values for FT2232H, FT4232H and FT232H devices + /// + /// Valid values for drive current options on FT2232H, FT4232H and FT232H devices. + /// + public class FT_DRIVE_CURRENT + { + /// + /// 4mA drive current + /// + public const byte FT_DRIVE_CURRENT_4MA = 4; + /// + /// 8mA drive current + /// + public const byte FT_DRIVE_CURRENT_8MA = 8; + /// + /// 12mA drive current + /// + public const byte FT_DRIVE_CURRENT_12MA = 12; + /// + /// 16mA drive current + /// + public const byte FT_DRIVE_CURRENT_16MA = 16; + } + +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EEPROM_DATA.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EEPROM_DATA.cs new file mode 100644 index 0000000000..707f7f69a4 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EEPROM_DATA.cs @@ -0,0 +1,82 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + + +using System; + + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // Base class for EEPROM structures - these elements are common to all devices + /// + /// Common EEPROM elements for all devices. Inherited to specific device type EEPROMs. + /// + public class FT_EEPROM_DATA + { + //private const UInt32 Signature1 = 0x00000000; + //private const UInt32 Signature2 = 0xFFFFFFFF; + //private const UInt32 Version = 0x00000002; + /// + /// Vendor ID as supplied by the USB Implementers Forum + /// + public UInt16 VendorID = 0x0403; + /// + /// Product ID + /// + public UInt16 ProductID = 0x6001; + /// + /// Manufacturer name string + /// + public string Manufacturer = "FTDI"; + /// + /// Manufacturer name abbreviation to be used as a prefix for automatically generated serial numbers + /// + public string ManufacturerID = "FT"; + /// + /// Device description string + /// + public string Description = "USB-Serial Converter"; + /// + /// Device serial number string + /// + public string SerialNumber = ""; + /// + /// Maximum power the device needs + /// + public UInt16 MaxPower = 0x0090; + //private bool PnP = true; + /// + /// Indicates if the device has its own power supply (self-powered) or gets power from the USB port (bus-powered) + /// + public bool SelfPowered = false; + /// + /// Determines if the device can wake the host PC from suspend by toggling the RI line + /// + public bool RemoteWakeup = false; + } + +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EEPROM_HEADER.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EEPROM_HEADER.cs new file mode 100644 index 0000000000..67fd545b9a --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EEPROM_HEADER.cs @@ -0,0 +1,51 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + + +using System; +using System.Runtime.InteropServices; + + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + [StructLayout(LayoutKind.Sequential, Pack = 4)] + private struct FT_EEPROM_HEADER + { + public UInt32 deviceType; // FTxxxx device type to be programmed + // Device descriptor options + public UInt16 VendorId; // 0x0403 + public UInt16 ProductId; // 0x6001 + public byte SerNumEnable; // non-zero if serial number to be used + // Config descriptor options + public UInt16 MaxPower; // 0 < MaxPower <= 500 + public byte SelfPowered; // 0 = bus powered, 1 = self powered + public byte RemoteWakeup; // 0 = not capable, 1 = capable + // Hardware options + public byte PullDownEnable; // non-zero if pull down in suspend enabled + } + +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_ERROR.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_ERROR.cs new file mode 100644 index 0000000000..5334715b91 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_ERROR.cs @@ -0,0 +1,42 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // Constants for other error states internal to this class library + /// + /// Error states not supported by FTD2XX DLL. + /// + private enum FT_ERROR + { + FT_NO_ERROR = 0, + FT_INCORRECT_DEVICE, + FT_INVALID_BITMODE, + FT_BUFFER_SIZE + }; + +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EVENTS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EVENTS.cs new file mode 100644 index 0000000000..3ef585eb46 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EVENTS.cs @@ -0,0 +1,54 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + + +using System; + + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // Events + /// + /// FTDI device event types that can be monitored + /// + public class FT_EVENTS + { + /// + /// Event on receive character + /// + public const UInt32 FT_EVENT_RXCHAR = 0x00000001; + /// + /// Event on modem status change + /// + public const UInt32 FT_EVENT_MODEM_STATUS = 0x00000002; + /// + /// Event on line status change + /// + public const UInt32 FT_EVENT_LINE_STATUS = 0x00000004; + } + +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EXCEPTION.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EXCEPTION.cs new file mode 100644 index 0000000000..6f6e1200bb --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EXCEPTION.cs @@ -0,0 +1,66 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + + +using System; + + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + /// + /// Exceptions thrown by errors within the FTDI class. + /// + [global::System.Serializable] + public class FT_EXCEPTION : Exception + { + /// + /// + /// + public FT_EXCEPTION() { } + /// + /// + /// + /// + public FT_EXCEPTION(string message) : base(message) { } + /// + /// + /// + /// + /// + public FT_EXCEPTION(string message, Exception inner) : base(message, inner) { } + /// + /// + /// + /// + /// + protected FT_EXCEPTION( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) + : base(info, context) { } + } + +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_FLAGS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_FLAGS.cs new file mode 100644 index 0000000000..262ef59ff1 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_FLAGS.cs @@ -0,0 +1,50 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + + +using System; + + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // Flag values for FT_GetDeviceInfoDetail and FT_GetDeviceInfo + /// + /// Flags that provide information on the FTDI device state + /// + public class FT_FLAGS + { + /// + /// Indicates that the device is open + /// + public const UInt32 FT_FLAGS_OPENED = 0x00000001; + /// + /// Indicates that the device is enumerated as a hi-speed USB device + /// + public const UInt32 FT_FLAGS_HISPEED = 0x00000002; + } + +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_FLOW_CONTROL.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_FLOW_CONTROL.cs new file mode 100644 index 0000000000..f8786046e7 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_FLOW_CONTROL.cs @@ -0,0 +1,58 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + + +using System; + + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // Flow Control + /// + /// Permitted flow control values for FTDI devices + /// + public class FT_FLOW_CONTROL + { + /// + /// No flow control + /// + public const UInt16 FT_FLOW_NONE = 0x0000; + /// + /// RTS/CTS flow control + /// + public const UInt16 FT_FLOW_RTS_CTS = 0x0100; + /// + /// DTR/DSR flow control + /// + public const UInt16 FT_FLOW_DTR_DSR = 0x0200; + /// + /// Xon/Xoff flow control + /// + public const UInt16 FT_FLOW_XON_XOFF = 0x0400; + } + +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_LINE_STATUS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_LINE_STATUS.cs new file mode 100644 index 0000000000..0145f3d29f --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_LINE_STATUS.cs @@ -0,0 +1,54 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // Line Status bits + /// + /// Line status bit definitions + /// + public class FT_LINE_STATUS + { + /// + /// Overrun Error (OE) line status + /// + public const byte FT_OE = 0x02; + /// + /// Parity Error (PE) line status + /// + public const byte FT_PE = 0x04; + /// + /// Framing Error (FE) line status + /// + public const byte FT_FE = 0x08; + /// + /// Break Interrupt (BI) line status + /// + public const byte FT_BI = 0x10; + } + +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_MODEM_STATUS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_MODEM_STATUS.cs new file mode 100644 index 0000000000..e661aae62a --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_MODEM_STATUS.cs @@ -0,0 +1,54 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // Modem Status bits + /// + /// Modem status bit definitions + /// + public class FT_MODEM_STATUS + { + /// + /// Clear To Send (CTS) modem status + /// + public const byte FT_CTS = 0x10; + /// + /// Data Set Ready (DSR) modem status + /// + public const byte FT_DSR = 0x20; + /// + /// Ring Indicator (RI) modem status + /// + public const byte FT_RI = 0x40; + /// + /// Data Carrier Detect (DCD) modem status + /// + public const byte FT_DCD = 0x80; + } + +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_PARITY.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_PARITY.cs new file mode 100644 index 0000000000..0538201d62 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_PARITY.cs @@ -0,0 +1,58 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // Parity + /// + /// Permitted parity values for FTDI devices + /// + public class FT_PARITY + { + /// + /// No parity + /// + public const byte FT_PARITY_NONE = 0x00; + /// + /// Odd parity + /// + public const byte FT_PARITY_ODD = 0x01; + /// + /// Even parity + /// + public const byte FT_PARITY_EVEN = 0x02; + /// + /// Mark parity + /// + public const byte FT_PARITY_MARK = 0x03; + /// + /// Space parity + /// + public const byte FT_PARITY_SPACE = 0x04; + } + +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_PROGRAM_DATA.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_PROGRAM_DATA.cs new file mode 100644 index 0000000000..1581c0fae6 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_PROGRAM_DATA.cs @@ -0,0 +1,180 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + + +using System; +using System.Runtime.InteropServices; + + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // Internal structure for reading and writing EEPROM contents + // NOTE: NEED Pack=1 for byte alignment! Without this, data is garbage + [StructLayout(LayoutKind.Sequential, Pack = 4)] + private class FT_PROGRAM_DATA + { + public UInt32 Signature1; + public UInt32 Signature2; + public UInt32 Version; + public UInt16 VendorID; + public UInt16 ProductID; + + public IntPtr Manufacturer; + public IntPtr ManufacturerID; + public IntPtr Description; + public IntPtr SerialNumber; + + public UInt16 MaxPower; + public UInt16 PnP; + public UInt16 SelfPowered; + public UInt16 RemoteWakeup; + // FT232B extensions + public byte Rev4; + public byte IsoIn; + public byte IsoOut; + public byte PullDownEnable; + public byte SerNumEnable; + public byte USBVersionEnable; + public UInt16 USBVersion; + // FT2232D extensions + public byte Rev5; + public byte IsoInA; + public byte IsoInB; + public byte IsoOutA; + public byte IsoOutB; + public byte PullDownEnable5; + public byte SerNumEnable5; + public byte USBVersionEnable5; + public UInt16 USBVersion5; + public byte AIsHighCurrent; + public byte BIsHighCurrent; + public byte IFAIsFifo; + public byte IFAIsFifoTar; + public byte IFAIsFastSer; + public byte AIsVCP; + public byte IFBIsFifo; + public byte IFBIsFifoTar; + public byte IFBIsFastSer; + public byte BIsVCP; + // FT232R extensions + public byte UseExtOsc; + public byte HighDriveIOs; + public byte EndpointSize; + public byte PullDownEnableR; + public byte SerNumEnableR; + public byte InvertTXD; // non-zero if invert TXD + public byte InvertRXD; // non-zero if invert RXD + public byte InvertRTS; // non-zero if invert RTS + public byte InvertCTS; // non-zero if invert CTS + public byte InvertDTR; // non-zero if invert DTR + public byte InvertDSR; // non-zero if invert DSR + public byte InvertDCD; // non-zero if invert DCD + public byte InvertRI; // non-zero if invert RI + public byte Cbus0; // Cbus Mux control - Ignored for FT245R + public byte Cbus1; // Cbus Mux control - Ignored for FT245R + public byte Cbus2; // Cbus Mux control - Ignored for FT245R + public byte Cbus3; // Cbus Mux control - Ignored for FT245R + public byte Cbus4; // Cbus Mux control - Ignored for FT245R + public byte RIsD2XX; // Default to loading VCP + // FT2232H extensions + public byte PullDownEnable7; + public byte SerNumEnable7; + public byte ALSlowSlew; // non-zero if AL pins have slow slew + public byte ALSchmittInput; // non-zero if AL pins are Schmitt input + public byte ALDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + public byte AHSlowSlew; // non-zero if AH pins have slow slew + public byte AHSchmittInput; // non-zero if AH pins are Schmitt input + public byte AHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + public byte BLSlowSlew; // non-zero if BL pins have slow slew + public byte BLSchmittInput; // non-zero if BL pins are Schmitt input + public byte BLDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + public byte BHSlowSlew; // non-zero if BH pins have slow slew + public byte BHSchmittInput; // non-zero if BH pins are Schmitt input + public byte BHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + public byte IFAIsFifo7; // non-zero if interface is 245 FIFO + public byte IFAIsFifoTar7; // non-zero if interface is 245 FIFO CPU target + public byte IFAIsFastSer7; // non-zero if interface is Fast serial + public byte AIsVCP7; // non-zero if interface is to use VCP drivers + public byte IFBIsFifo7; // non-zero if interface is 245 FIFO + public byte IFBIsFifoTar7; // non-zero if interface is 245 FIFO CPU target + public byte IFBIsFastSer7; // non-zero if interface is Fast serial + public byte BIsVCP7; // non-zero if interface is to use VCP drivers + public byte PowerSaveEnable; // non-zero if using BCBUS7 to save power for self-powered designs + // FT4232H extensions + public byte PullDownEnable8; + public byte SerNumEnable8; + public byte ASlowSlew; // non-zero if AL pins have slow slew + public byte ASchmittInput; // non-zero if AL pins are Schmitt input + public byte ADriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + public byte BSlowSlew; // non-zero if AH pins have slow slew + public byte BSchmittInput; // non-zero if AH pins are Schmitt input + public byte BDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + public byte CSlowSlew; // non-zero if BL pins have slow slew + public byte CSchmittInput; // non-zero if BL pins are Schmitt input + public byte CDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + public byte DSlowSlew; // non-zero if BH pins have slow slew + public byte DSchmittInput; // non-zero if BH pins are Schmitt input + public byte DDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + public byte ARIIsTXDEN; + public byte BRIIsTXDEN; + public byte CRIIsTXDEN; + public byte DRIIsTXDEN; + public byte AIsVCP8; // non-zero if interface is to use VCP drivers + public byte BIsVCP8; // non-zero if interface is to use VCP drivers + public byte CIsVCP8; // non-zero if interface is to use VCP drivers + public byte DIsVCP8; // non-zero if interface is to use VCP drivers + // FT232H extensions + public byte PullDownEnableH; // non-zero if pull down enabled + public byte SerNumEnableH; // non-zero if serial number to be used + public byte ACSlowSlewH; // non-zero if AC pins have slow slew + public byte ACSchmittInputH; // non-zero if AC pins are Schmitt input + public byte ACDriveCurrentH; // valid values are 4mA, 8mA, 12mA, 16mA + public byte ADSlowSlewH; // non-zero if AD pins have slow slew + public byte ADSchmittInputH; // non-zero if AD pins are Schmitt input + public byte ADDriveCurrentH; // valid values are 4mA, 8mA, 12mA, 16mA + public byte Cbus0H; // Cbus Mux control + public byte Cbus1H; // Cbus Mux control + public byte Cbus2H; // Cbus Mux control + public byte Cbus3H; // Cbus Mux control + public byte Cbus4H; // Cbus Mux control + public byte Cbus5H; // Cbus Mux control + public byte Cbus6H; // Cbus Mux control + public byte Cbus7H; // Cbus Mux control + public byte Cbus8H; // Cbus Mux control + public byte Cbus9H; // Cbus Mux control + public byte IsFifoH; // non-zero if interface is 245 FIFO + public byte IsFifoTarH; // non-zero if interface is 245 FIFO CPU target + public byte IsFastSerH; // non-zero if interface is Fast serial + public byte IsFT1248H; // non-zero if interface is FT1248 + public byte FT1248CpolH; // FT1248 clock polarity + public byte FT1248LsbH; // FT1248 data is LSB (1) or MSB (0) + public byte FT1248FlowControlH; // FT1248 flow control enable + public byte IsVCPH; // non-zero if interface is to use VCP drivers + public byte PowerSaveEnableH; // non-zero if using ACBUS7 to save power for self-powered designs + } + +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_PURGE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_PURGE.cs new file mode 100644 index 0000000000..d8b8f7cce3 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_PURGE.cs @@ -0,0 +1,46 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // Purge Rx and Tx buffers + /// + /// Purge buffer constant definitions + /// + public class FT_PURGE + { + /// + /// Purge Rx buffer + /// + public const byte FT_PURGE_RX = 0x01; + /// + /// Purge Tx buffer + /// + public const byte FT_PURGE_TX = 0x02; + } + +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_STATUS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_STATUS.cs new file mode 100644 index 0000000000..42e5d380d8 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_STATUS.cs @@ -0,0 +1,110 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // Constants for FT_STATUS + /// + /// Status values for FTDI devices. + /// + public enum FT_STATUS + { + /// + /// Status OK + /// + FT_OK = 0, + /// + /// The device handle is invalid + /// + FT_INVALID_HANDLE, + /// + /// Device not found + /// + FT_DEVICE_NOT_FOUND, + /// + /// Device is not open + /// + FT_DEVICE_NOT_OPENED, + /// + /// IO error + /// + FT_IO_ERROR, + /// + /// Insufficient resources + /// + FT_INSUFFICIENT_RESOURCES, + /// + /// A parameter was invalid + /// + FT_INVALID_PARAMETER, + /// + /// The requested baud rate is invalid + /// + FT_INVALID_BAUD_RATE, + /// + /// Device not opened for erase + /// + FT_DEVICE_NOT_OPENED_FOR_ERASE, + /// + /// Device not poened for write + /// + FT_DEVICE_NOT_OPENED_FOR_WRITE, + /// + /// Failed to write to device + /// + FT_FAILED_TO_WRITE_DEVICE, + /// + /// Failed to read the device EEPROM + /// + FT_EEPROM_READ_FAILED, + /// + /// Failed to write the device EEPROM + /// + FT_EEPROM_WRITE_FAILED, + /// + /// Failed to erase the device EEPROM + /// + FT_EEPROM_ERASE_FAILED, + /// + /// An EEPROM is not fitted to the device + /// + FT_EEPROM_NOT_PRESENT, + /// + /// Device EEPROM is blank + /// + FT_EEPROM_NOT_PROGRAMMED, + /// + /// Invalid arguments + /// + FT_INVALID_ARGS, + /// + /// An other error has occurred + /// + FT_OTHER_ERROR + }; + +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_STOP_BITS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_STOP_BITS.cs new file mode 100644 index 0000000000..90e1a66af5 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_STOP_BITS.cs @@ -0,0 +1,46 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + // Stop Bits + /// + /// Permitted stop bits for FTDI devices + /// + public class FT_STOP_BITS + { + /// + /// 1 stop bit + /// + public const byte FT_STOP_BITS_1 = 0x00; + /// + /// 2 stop bits + /// + public const byte FT_STOP_BITS_2 = 0x02; + } + +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_XSERIES_CBUS_OPTIONS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_XSERIES_CBUS_OPTIONS.cs new file mode 100644 index 0000000000..c60a5e2385 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_XSERIES_CBUS_OPTIONS.cs @@ -0,0 +1,125 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + /// + /// Available functions for the X-Series CBUS pins. Controlled by X-Series EEPROM settings + /// + public class FT_XSERIES_CBUS_OPTIONS + { + /// + /// FT X-Series CBUS EEPROM options - Tristate + /// + public const byte FT_CBUS_TRISTATE = 0x00; + /// + /// FT X-Series CBUS EEPROM options - RxLED# + /// + public const byte FT_CBUS_RXLED = 0x01; + /// + /// FT X-Series CBUS EEPROM options - TxLED# + /// + public const byte FT_CBUS_TXLED = 0x02; + /// + /// FT X-Series CBUS EEPROM options - TxRxLED# + /// + public const byte FT_CBUS_TXRXLED = 0x03; + /// + /// FT X-Series CBUS EEPROM options - PwrEn# + /// + public const byte FT_CBUS_PWREN = 0x04; + /// + /// FT X-Series CBUS EEPROM options - Sleep# + /// + public const byte FT_CBUS_SLEEP = 0x05; + /// + /// FT X-Series CBUS EEPROM options - Drive_0 + /// + public const byte FT_CBUS_Drive_0 = 0x06; + /// + /// FT X-Series CBUS EEPROM options - Drive_1 + /// + public const byte FT_CBUS_Drive_1 = 0x07; + /// + /// FT X-Series CBUS EEPROM options - GPIO + /// + public const byte FT_CBUS_GPIO = 0x08; + /// + /// FT X-Series CBUS EEPROM options - TxdEn + /// + public const byte FT_CBUS_TXDEN = 0x09; + /// + /// FT X-Series CBUS EEPROM options - Clk24MHz + /// + public const byte FT_CBUS_CLK24MHz = 0x0A; + /// + /// FT X-Series CBUS EEPROM options - Clk12MHz + /// + public const byte FT_CBUS_CLK12MHz = 0x0B; + /// + /// FT X-Series CBUS EEPROM options - Clk6MHz + /// + public const byte FT_CBUS_CLK6MHz = 0x0C; + /// + /// FT X-Series CBUS EEPROM options - BCD_Charger + /// + public const byte FT_CBUS_BCD_Charger = 0x0D; + /// + /// FT X-Series CBUS EEPROM options - BCD_Charger# + /// + public const byte FT_CBUS_BCD_Charger_N = 0x0E; + /// + /// FT X-Series CBUS EEPROM options - I2C_TXE# + /// + public const byte FT_CBUS_I2C_TXE = 0x0F; + /// + /// FT X-Series CBUS EEPROM options - I2C_RXF# + /// + public const byte FT_CBUS_I2C_RXF = 0x10; + /// + /// FT X-Series CBUS EEPROM options - VBUS_Sense + /// + public const byte FT_CBUS_VBUS_Sense = 0x11; + /// + /// FT X-Series CBUS EEPROM options - BitBang_WR# + /// + public const byte FT_CBUS_BitBang_WR = 0x12; + /// + /// FT X-Series CBUS EEPROM options - BitBang_RD# + /// + public const byte FT_CBUS_BitBang_RD = 0x13; + /// + /// FT X-Series CBUS EEPROM options - Time_Stampe + /// + public const byte FT_CBUS_Time_Stamp = 0x14; + /// + /// FT X-Series CBUS EEPROM options - Keep_Awake# + /// + public const byte FT_CBUS_Keep_Awake = 0x15; + } + +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_XSERIES_DATA.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_XSERIES_DATA.cs new file mode 100644 index 0000000000..3cad746e44 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_XSERIES_DATA.cs @@ -0,0 +1,82 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + + +using System; +using System.Runtime.InteropServices; + + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + [StructLayout(LayoutKind.Sequential, Pack = 4)] + private struct FT_XSERIES_DATA + { + public FT_EEPROM_HEADER common; + + public byte ACSlowSlew; // non-zero if AC bus pins have slow slew + public byte ACSchmittInput; // non-zero if AC bus pins are Schmitt input + public byte ACDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + public byte ADSlowSlew; // non-zero if AD bus pins have slow slew + public byte ADSchmittInput; // non-zero if AD bus pins are Schmitt input + public byte ADDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + // CBUS options + public byte Cbus0; // Cbus Mux control + public byte Cbus1; // Cbus Mux control + public byte Cbus2; // Cbus Mux control + public byte Cbus3; // Cbus Mux control + public byte Cbus4; // Cbus Mux control + public byte Cbus5; // Cbus Mux control + public byte Cbus6; // Cbus Mux control + // UART signal options + public byte InvertTXD; // non-zero if invert TXD + public byte InvertRXD; // non-zero if invert RXD + public byte InvertRTS; // non-zero if invert RTS + public byte InvertCTS; // non-zero if invert CTS + public byte InvertDTR; // non-zero if invert DTR + public byte InvertDSR; // non-zero if invert DSR + public byte InvertDCD; // non-zero if invert DCD + public byte InvertRI; // non-zero if invert RI + // Battery Charge Detect options + public byte BCDEnable; // Enable Battery Charger Detection + public byte BCDForceCbusPWREN; // asserts the power enable signal on CBUS when charging port detected + public byte BCDDisableSleep; // forces the device never to go into sleep mode + // I2C options + public UInt16 I2CSlaveAddress; // I2C slave device address + public UInt32 I2CDeviceId; // I2C device ID + public byte I2CDisableSchmitt; // Disable I2C Schmitt trigger + // FT1248 options + public byte FT1248Cpol; // FT1248 clock polarity - clock idle high (1) or clock idle low (0) + public byte FT1248Lsb; // FT1248 data is LSB (1) or MSB (0) + public byte FT1248FlowControl; // FT1248 flow control enable + // Hardware options + public byte RS485EchoSuppress; // + public byte PowerSaveEnable; // + // Driver option + public byte DriverType; // + } + +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_XSERIES_EEPROM_STRUCTURE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_XSERIES_EEPROM_STRUCTURE.cs new file mode 100644 index 0000000000..f84729cb98 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_XSERIES_EEPROM_STRUCTURE.cs @@ -0,0 +1,218 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + + +using System; + + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + /// + /// EEPROM structure specific to X-Series devices. + /// Inherits from FT_EEPROM_DATA. + /// + public class FT_XSERIES_EEPROM_STRUCTURE : FT_EEPROM_DATA + { + /// + /// Determines if IOs are pulled down when the device is in suspend + /// + public bool PullDownEnable = false; + /// + /// Determines if the serial number is enabled + /// + public bool SerNumEnable = true; + /// + /// Determines if the USB version number is enabled + /// + public bool USBVersionEnable = true; + /// + /// The USB version number: 0x0200 (USB 2.0) + /// + public UInt16 USBVersion = 0x0200; + /// + /// Determines if AC pins have a slow slew rate + /// + public byte ACSlowSlew; + /// + /// Determines if the AC pins have a Schmitt input + /// + public byte ACSchmittInput; + /// + /// Determines the AC pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA + /// + public byte ACDriveCurrent; + /// + /// Determines if AD pins have a slow slew rate + /// + public byte ADSlowSlew; + /// + /// Determines if AD pins have a schmitt input + /// + public byte ADSchmittInput; + /// + /// Determines the AD pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA + /// + public byte ADDriveCurrent; + /// + /// Sets the function of the CBUS0 pin for FT232H devices. + /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, + /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_GPIO, FT_CBUS_TXDEN, FT_CBUS_CLK24, + /// FT_CBUS_CLK12, FT_CBUS_CLK6, FT_CBUS_BCD_CHARGER, FT_CBUS_BCD_CHARGER_N, FT_CBUS_VBUS_SENSE, FT_CBUS_BITBANG_WR, + /// FT_CBUS_BITBANG_RD, FT_CBUS_TIME_STAMP, FT_CBUS_KEEP_AWAKE + /// + public byte Cbus0; + /// + /// Sets the function of the CBUS1 pin for FT232H devices. + /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, + /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_GPIO, FT_CBUS_TXDEN, FT_CBUS_CLK24, + /// FT_CBUS_CLK12, FT_CBUS_CLK6, FT_CBUS_BCD_CHARGER, FT_CBUS_BCD_CHARGER_N, FT_CBUS_VBUS_SENSE, FT_CBUS_BITBANG_WR, + /// FT_CBUS_BITBANG_RD, FT_CBUS_TIME_STAMP, FT_CBUS_KEEP_AWAKE + /// + public byte Cbus1; + /// + /// Sets the function of the CBUS2 pin for FT232H devices. + /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, + /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_GPIO, FT_CBUS_TXDEN, FT_CBUS_CLK24, + /// FT_CBUS_CLK12, FT_CBUS_CLK6, FT_CBUS_BCD_CHARGER, FT_CBUS_BCD_CHARGER_N, FT_CBUS_VBUS_SENSE, FT_CBUS_BITBANG_WR, + /// FT_CBUS_BITBANG_RD, FT_CBUS_TIME_STAMP, FT_CBUS_KEEP_AWAKE + /// + public byte Cbus2; + /// + /// Sets the function of the CBUS3 pin for FT232H devices. + /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, + /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_GPIO, FT_CBUS_TXDEN, FT_CBUS_CLK24, + /// FT_CBUS_CLK12, FT_CBUS_CLK6, FT_CBUS_BCD_CHARGER, FT_CBUS_BCD_CHARGER_N, FT_CBUS_VBUS_SENSE, FT_CBUS_BITBANG_WR, + /// FT_CBUS_BITBANG_RD, FT_CBUS_TIME_STAMP, FT_CBUS_KEEP_AWAKE + /// + public byte Cbus3; + /// + /// Sets the function of the CBUS4 pin for FT232H devices. + /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, + /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN, FT_CBUS_CLK24, + /// FT_CBUS_CLK12, FT_CBUS_CLK6, FT_CBUS_BCD_CHARGER, FT_CBUS_BCD_CHARGER_N, FT_CBUS_VBUS_SENSE, FT_CBUS_BITBANG_WR, + /// FT_CBUS_BITBANG_RD, FT_CBUS_TIME_STAMP, FT_CBUS_KEEP_AWAKE + /// + public byte Cbus4; + /// + /// Sets the function of the CBUS5 pin for FT232H devices. + /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, + /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN, FT_CBUS_CLK24, + /// FT_CBUS_CLK12, FT_CBUS_CLK6, FT_CBUS_BCD_CHARGER, FT_CBUS_BCD_CHARGER_N, FT_CBUS_VBUS_SENSE, FT_CBUS_BITBANG_WR, + /// FT_CBUS_BITBANG_RD, FT_CBUS_TIME_STAMP, FT_CBUS_KEEP_AWAKE + /// + public byte Cbus5; + /// + /// Sets the function of the CBUS6 pin for FT232H devices. + /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, + /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN, FT_CBUS_CLK24, + /// FT_CBUS_CLK12, FT_CBUS_CLK6, FT_CBUS_BCD_CHARGER, FT_CBUS_BCD_CHARGER_N, FT_CBUS_VBUS_SENSE, FT_CBUS_BITBANG_WR, + /// FT_CBUS_BITBANG_RD, FT_CBUS_TIME_STAMP, FT_CBUS_KEEP_AWAKE + /// + public byte Cbus6; + /// + /// Inverts the sense of the TXD line + /// + public byte InvertTXD; + /// + /// Inverts the sense of the RXD line + /// + public byte InvertRXD; + /// + /// Inverts the sense of the RTS line + /// + public byte InvertRTS; + /// + /// Inverts the sense of the CTS line + /// + public byte InvertCTS; + /// + /// Inverts the sense of the DTR line + /// + public byte InvertDTR; + /// + /// Inverts the sense of the DSR line + /// + public byte InvertDSR; + /// + /// Inverts the sense of the DCD line + /// + public byte InvertDCD; + /// + /// Inverts the sense of the RI line + /// + public byte InvertRI; + /// + /// Determines whether the Battery Charge Detection option is enabled. + /// + public byte BCDEnable; + /// + /// Asserts the power enable signal on CBUS when charging port detected. + /// + public byte BCDForceCbusPWREN; + /// + /// Forces the device never to go into sleep mode. + /// + public byte BCDDisableSleep; + /// + /// I2C slave device address. + /// + public ushort I2CSlaveAddress; + /// + /// I2C device ID + /// + public UInt32 I2CDeviceId; + /// + /// Disable I2C Schmitt trigger. + /// + public byte I2CDisableSchmitt; + /// + /// FT1248 clock polarity - clock idle high (1) or clock idle low (0) + /// + public byte FT1248Cpol; + /// + /// FT1248 data is LSB (1) or MSB (0) + /// + public byte FT1248Lsb; + /// + /// FT1248 flow control enable. + /// + public byte FT1248FlowControl; + /// + /// Enable RS485 Echo Suppression + /// + public byte RS485EchoSuppress; + /// + /// Enable Power Save mode. + /// + public byte PowerSaveEnable; + /// + /// Determines whether the VCP driver is loaded. + /// + public byte IsVCP; + } + +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.cs new file mode 100644 index 0000000000..edc9218bbd --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.cs @@ -0,0 +1,4901 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + + +using System; +using System.IO; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading; + + +namespace Meadow.Foundation.ICs.IOExpanders; + +/// +/// Class wrapper for FTD2XX.DLL +/// +internal partial class FTDI +{ + // constructor + /// + /// Constructor for the FTDI class. + /// + public FTDI() + { + // If FTD2XX.DLL is NOT loaded already, load it + if (hFTD2XXDLL == IntPtr.Zero) + { + // Load our FTD2XX.DLL library + hFTD2XXDLL = LoadLibrary(@"FTD2XX.DLL"); + if (hFTD2XXDLL == IntPtr.Zero) + { + // Failed to load our FTD2XX.DLL library from System32 or the application directory + // Try the same directory that this FTD2XX_NET DLL is in + Console.WriteLine("Attempting to load FTD2XX.DLL from:\n" + Path.GetDirectoryName(GetType().Assembly.Location)); + hFTD2XXDLL = LoadLibrary(@Path.GetDirectoryName(GetType().Assembly.Location) + "\\FTD2XX.DLL"); + } + } + + // If we have succesfully loaded the library, get the function pointers set up + if (hFTD2XXDLL != IntPtr.Zero) + { + FindFunctionPointers(); + } + else + { + // Failed to load our DLL - alert the user + Console.WriteLine("Failed to load FTD2XX.DLL. Are the FTDI drivers installed?"); + } + } + + /// + /// Non default constructor allowing passing of string for dll handle. + /// + public FTDI(String path) + { + // If nonstandard.DLL is NOT loaded already, load it + if (path == "") + return; + + if (hFTD2XXDLL == IntPtr.Zero) + { + // Load our nonstandard.DLL library + hFTD2XXDLL = LoadLibrary(path); + if (hFTD2XXDLL == IntPtr.Zero) + { + // Failed to load our PathToDll library + // Give up :( + Console.WriteLine("Attempting to load FTD2XX.DLL from:\n" + Path.GetDirectoryName(GetType().Assembly.Location)); + } + } + + // If we have succesfully loaded the library, get the function pointers set up + if (hFTD2XXDLL != IntPtr.Zero) + { + FindFunctionPointers(); + } + else + { + Console.WriteLine("Failed to load FTD2XX.DLL. Are the FTDI drivers installed?"); + } + } + + private void FindFunctionPointers() + { + // Set up our function pointers for use through our exported methods + pFT_CreateDeviceInfoList = GetProcAddress(hFTD2XXDLL, "FT_CreateDeviceInfoList"); + pFT_GetDeviceInfoDetail = GetProcAddress(hFTD2XXDLL, "FT_GetDeviceInfoDetail"); + pFT_Open = GetProcAddress(hFTD2XXDLL, "FT_Open"); + pFT_OpenEx = GetProcAddress(hFTD2XXDLL, "FT_OpenEx"); + pFT_Close = GetProcAddress(hFTD2XXDLL, "FT_Close"); + pFT_Read = GetProcAddress(hFTD2XXDLL, "FT_Read"); + pFT_Write = GetProcAddress(hFTD2XXDLL, "FT_Write"); + pFT_GetQueueStatus = GetProcAddress(hFTD2XXDLL, "FT_GetQueueStatus"); + pFT_GetModemStatus = GetProcAddress(hFTD2XXDLL, "FT_GetModemStatus"); + pFT_GetStatus = GetProcAddress(hFTD2XXDLL, "FT_GetStatus"); + pFT_SetBaudRate = GetProcAddress(hFTD2XXDLL, "FT_SetBaudRate"); + pFT_SetDataCharacteristics = GetProcAddress(hFTD2XXDLL, "FT_SetDataCharacteristics"); + pFT_SetFlowControl = GetProcAddress(hFTD2XXDLL, "FT_SetFlowControl"); + pFT_SetDtr = GetProcAddress(hFTD2XXDLL, "FT_SetDtr"); + pFT_ClrDtr = GetProcAddress(hFTD2XXDLL, "FT_ClrDtr"); + pFT_SetRts = GetProcAddress(hFTD2XXDLL, "FT_SetRts"); + pFT_ClrRts = GetProcAddress(hFTD2XXDLL, "FT_ClrRts"); + pFT_ResetDevice = GetProcAddress(hFTD2XXDLL, "FT_ResetDevice"); + pFT_ResetPort = GetProcAddress(hFTD2XXDLL, "FT_ResetPort"); + pFT_CyclePort = GetProcAddress(hFTD2XXDLL, "FT_CyclePort"); + pFT_Rescan = GetProcAddress(hFTD2XXDLL, "FT_Rescan"); + pFT_Reload = GetProcAddress(hFTD2XXDLL, "FT_Reload"); + pFT_Purge = GetProcAddress(hFTD2XXDLL, "FT_Purge"); + pFT_SetTimeouts = GetProcAddress(hFTD2XXDLL, "FT_SetTimeouts"); + pFT_SetBreakOn = GetProcAddress(hFTD2XXDLL, "FT_SetBreakOn"); + pFT_SetBreakOff = GetProcAddress(hFTD2XXDLL, "FT_SetBreakOff"); + pFT_GetDeviceInfo = GetProcAddress(hFTD2XXDLL, "FT_GetDeviceInfo"); + pFT_SetResetPipeRetryCount = GetProcAddress(hFTD2XXDLL, "FT_SetResetPipeRetryCount"); + pFT_StopInTask = GetProcAddress(hFTD2XXDLL, "FT_StopInTask"); + pFT_RestartInTask = GetProcAddress(hFTD2XXDLL, "FT_RestartInTask"); + pFT_GetDriverVersion = GetProcAddress(hFTD2XXDLL, "FT_GetDriverVersion"); + pFT_GetLibraryVersion = GetProcAddress(hFTD2XXDLL, "FT_GetLibraryVersion"); + pFT_SetDeadmanTimeout = GetProcAddress(hFTD2XXDLL, "FT_SetDeadmanTimeout"); + pFT_SetChars = GetProcAddress(hFTD2XXDLL, "FT_SetChars"); + pFT_SetEventNotification = GetProcAddress(hFTD2XXDLL, "FT_SetEventNotification"); + pFT_GetComPortNumber = GetProcAddress(hFTD2XXDLL, "FT_GetComPortNumber"); + pFT_SetLatencyTimer = GetProcAddress(hFTD2XXDLL, "FT_SetLatencyTimer"); + pFT_GetLatencyTimer = GetProcAddress(hFTD2XXDLL, "FT_GetLatencyTimer"); + pFT_SetBitMode = GetProcAddress(hFTD2XXDLL, "FT_SetBitMode"); + pFT_GetBitMode = GetProcAddress(hFTD2XXDLL, "FT_GetBitMode"); + pFT_SetUSBParameters = GetProcAddress(hFTD2XXDLL, "FT_SetUSBParameters"); + pFT_ReadEE = GetProcAddress(hFTD2XXDLL, "FT_ReadEE"); + pFT_WriteEE = GetProcAddress(hFTD2XXDLL, "FT_WriteEE"); + pFT_EraseEE = GetProcAddress(hFTD2XXDLL, "FT_EraseEE"); + pFT_EE_UASize = GetProcAddress(hFTD2XXDLL, "FT_EE_UASize"); + pFT_EE_UARead = GetProcAddress(hFTD2XXDLL, "FT_EE_UARead"); + pFT_EE_UAWrite = GetProcAddress(hFTD2XXDLL, "FT_EE_UAWrite"); + pFT_EE_Read = GetProcAddress(hFTD2XXDLL, "FT_EE_Read"); + pFT_EE_Program = GetProcAddress(hFTD2XXDLL, "FT_EE_Program"); + pFT_EEPROM_Read = GetProcAddress(hFTD2XXDLL, "FT_EEPROM_Read"); + pFT_EEPROM_Program = GetProcAddress(hFTD2XXDLL, "FT_EEPROM_Program"); + pFT_VendorCmdGet = GetProcAddress(hFTD2XXDLL, "FT_VendorCmdGet"); + pFT_VendorCmdSet = GetProcAddress(hFTD2XXDLL, "FT_VendorCmdSet"); + pFT_VendorCmdSetX = GetProcAddress(hFTD2XXDLL, "FT_VendorCmdSetX"); + } + + /// + /// Destructor for the FTDI class. + /// + ~FTDI() + { + // FreeLibrary here - we should only do this if we are completely finished + FreeLibrary(hFTD2XXDLL); + hFTD2XXDLL = IntPtr.Zero; + } + + /// + /// Built-in Windows API functions to allow us to dynamically load our own DLL. + /// Will allow us to use old versions of the DLL that do not have all of these functions available. + /// + [DllImport("kernel32.dll")] + private static extern IntPtr LoadLibrary(string dllToLoad); + [DllImport("kernel32.dll")] + private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName); + [DllImport("kernel32.dll")] + private static extern bool FreeLibrary(IntPtr hModule); + + // Definitions for FTD2XX functions + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_CreateDeviceInfoList(ref uint numdevs); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_GetDeviceInfoDetail(uint index, ref uint flags, ref FT_DEVICE chiptype, ref uint id, ref uint locid, byte[] serialnumber, byte[] description, ref IntPtr ftHandle); + + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_Open(uint index, ref IntPtr ftHandle); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_OpenEx(string devstring, uint dwFlags, ref IntPtr ftHandle); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_OpenExLoc(uint devloc, uint dwFlags, ref IntPtr ftHandle); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_Close(IntPtr ftHandle); + + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_Read(IntPtr ftHandle, byte[] lpBuffer, uint dwBytesToRead, ref uint lpdwBytesReturned); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_Write(IntPtr ftHandle, byte[] lpBuffer, uint dwBytesToWrite, ref uint lpdwBytesWritten); + + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_GetQueueStatus(IntPtr ftHandle, ref uint lpdwAmountInRxQueue); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_GetModemStatus(IntPtr ftHandle, ref uint lpdwModemStatus); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_GetStatus(IntPtr ftHandle, ref uint lpdwAmountInRxQueue, ref uint lpdwAmountInTxQueue, ref uint lpdwEventStatus); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_SetBaudRate(IntPtr ftHandle, uint dwBaudRate); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_SetDataCharacteristics(IntPtr ftHandle, byte uWordLength, byte uStopBits, byte uParity); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_SetFlowControl(IntPtr ftHandle, UInt16 usFlowControl, byte uXon, byte uXoff); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_SetDtr(IntPtr ftHandle); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_ClrDtr(IntPtr ftHandle); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_SetRts(IntPtr ftHandle); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_ClrRts(IntPtr ftHandle); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_ResetDevice(IntPtr ftHandle); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_ResetPort(IntPtr ftHandle); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_CyclePort(IntPtr ftHandle); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_Rescan(); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_Reload(UInt16 wVID, UInt16 wPID); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_Purge(IntPtr ftHandle, uint dwMask); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_SetTimeouts(IntPtr ftHandle, uint dwReadTimeout, uint dwWriteTimeout); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_SetBreakOn(IntPtr ftHandle); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_SetBreakOff(IntPtr ftHandle); + + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_GetDeviceInfo(IntPtr ftHandle, ref FT_DEVICE pftType, ref uint lpdwID, byte[] pcSerialNumber, byte[] pcDescription, IntPtr pvDummy); + + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_SetResetPipeRetryCount(IntPtr ftHandle, uint dwCount); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_StopInTask(IntPtr ftHandle); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_RestartInTask(IntPtr ftHandle); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_GetDriverVersion(IntPtr ftHandle, ref uint lpdwDriverVersion); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_GetLibraryVersion(ref uint lpdwLibraryVersion); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_SetDeadmanTimeout(IntPtr ftHandle, uint dwDeadmanTimeout); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_SetChars(IntPtr ftHandle, byte uEventCh, byte uEventChEn, byte uErrorCh, byte uErrorChEn); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_SetEventNotification(IntPtr ftHandle, uint dwEventMask, SafeHandle hEvent); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_GetComPortNumber(IntPtr ftHandle, ref Int32 dwComPortNumber); + + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_SetLatencyTimer(IntPtr ftHandle, byte ucLatency); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_GetLatencyTimer(IntPtr ftHandle, ref byte ucLatency); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_SetBitMode(IntPtr ftHandle, byte ucMask, byte ucMode); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_GetBitMode(IntPtr ftHandle, ref byte ucMode); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_SetUSBParameters(IntPtr ftHandle, uint dwInTransferSize, uint dwOutTransferSize); + + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_ReadEE(IntPtr ftHandle, uint dwWordOffset, ref UInt16 lpwValue); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_WriteEE(IntPtr ftHandle, uint dwWordOffset, UInt16 wValue); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_EraseEE(IntPtr ftHandle); + + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_EE_UASize(IntPtr ftHandle, ref uint dwSize); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_EE_UARead(IntPtr ftHandle, byte[] pucData, Int32 dwDataLen, ref uint lpdwDataRead); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_EE_UAWrite(IntPtr ftHandle, byte[] pucData, Int32 dwDataLen); + + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_EE_Read(IntPtr ftHandle, FT_PROGRAM_DATA pData); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_EE_Program(IntPtr ftHandle, FT_PROGRAM_DATA pData); + + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_EEPROM_Read(IntPtr ftHandle, IntPtr eepromData, uint eepromDataSize, byte[] manufacturer, byte[] manufacturerID, byte[] description, byte[] serialnumber); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_EEPROM_Program(IntPtr ftHandle, IntPtr eepromData, uint eepromDataSize, byte[] manufacturer, byte[] manufacturerID, byte[] description, byte[] serialnumber); + + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_VendorCmdGet(IntPtr ftHandle, UInt16 request, byte[] buf, UInt16 len); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_VendorCmdSet(IntPtr ftHandle, UInt16 request, byte[] buf, UInt16 len); + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + private delegate FT_STATUS tFT_VendorCmdSetX(IntPtr ftHandle, UInt16 request, byte[] buf, UInt16 len); + + // Flags for FT_OpenEx + private const uint FT_OPEN_BY_SERIAL_NUMBER = 0x00000001; + private const uint FT_OPEN_BY_DESCRIPTION = 0x00000002; + private const uint FT_OPEN_BY_LOCATION = 0x00000004; + + private const uint FT_DEFAULT_BAUD_RATE = 9600; + private const uint FT_DEFAULT_DEADMAN_TIMEOUT = 5000; + private const Int32 FT_COM_PORT_NOT_ASSIGNED = -1; + private const uint FT_DEFAULT_IN_TRANSFER_SIZE = 0x1000; + private const uint FT_DEFAULT_OUT_TRANSFER_SIZE = 0x1000; + private const byte FT_DEFAULT_LATENCY = 16; + private const uint FT_DEFAULT_DEVICE_ID = 0x04036001; + + // Create private variables for the device within the class + private IntPtr ftHandle = IntPtr.Zero; + + // Handle to our DLL - used with GetProcAddress to load all of our functions + private IntPtr hFTD2XXDLL = IntPtr.Zero; + + // Declare pointers to each of the functions we are going to use in FT2DXX.DLL + // These are assigned in our constructor and freed in our destructor. + private IntPtr pFT_CreateDeviceInfoList = IntPtr.Zero; + private IntPtr pFT_GetDeviceInfoDetail = IntPtr.Zero; + private IntPtr pFT_Open = IntPtr.Zero; + private IntPtr pFT_OpenEx = IntPtr.Zero; + private IntPtr pFT_Close = IntPtr.Zero; + private IntPtr pFT_Read = IntPtr.Zero; + private IntPtr pFT_Write = IntPtr.Zero; + private IntPtr pFT_GetQueueStatus = IntPtr.Zero; + private IntPtr pFT_GetModemStatus = IntPtr.Zero; + private IntPtr pFT_GetStatus = IntPtr.Zero; + private IntPtr pFT_SetBaudRate = IntPtr.Zero; + private IntPtr pFT_SetDataCharacteristics = IntPtr.Zero; + private IntPtr pFT_SetFlowControl = IntPtr.Zero; + private IntPtr pFT_SetDtr = IntPtr.Zero; + private IntPtr pFT_ClrDtr = IntPtr.Zero; + private IntPtr pFT_SetRts = IntPtr.Zero; + private IntPtr pFT_ClrRts = IntPtr.Zero; + private IntPtr pFT_ResetDevice = IntPtr.Zero; + private IntPtr pFT_ResetPort = IntPtr.Zero; + private IntPtr pFT_CyclePort = IntPtr.Zero; + private IntPtr pFT_Rescan = IntPtr.Zero; + private IntPtr pFT_Reload = IntPtr.Zero; + private IntPtr pFT_Purge = IntPtr.Zero; + private IntPtr pFT_SetTimeouts = IntPtr.Zero; + private IntPtr pFT_SetBreakOn = IntPtr.Zero; + private IntPtr pFT_SetBreakOff = IntPtr.Zero; + private IntPtr pFT_GetDeviceInfo = IntPtr.Zero; + private IntPtr pFT_SetResetPipeRetryCount = IntPtr.Zero; + private IntPtr pFT_StopInTask = IntPtr.Zero; + private IntPtr pFT_RestartInTask = IntPtr.Zero; + private IntPtr pFT_GetDriverVersion = IntPtr.Zero; + private IntPtr pFT_GetLibraryVersion = IntPtr.Zero; + private IntPtr pFT_SetDeadmanTimeout = IntPtr.Zero; + private IntPtr pFT_SetChars = IntPtr.Zero; + private IntPtr pFT_SetEventNotification = IntPtr.Zero; + private IntPtr pFT_GetComPortNumber = IntPtr.Zero; + private IntPtr pFT_SetLatencyTimer = IntPtr.Zero; + private IntPtr pFT_GetLatencyTimer = IntPtr.Zero; + private IntPtr pFT_SetBitMode = IntPtr.Zero; + private IntPtr pFT_GetBitMode = IntPtr.Zero; + private IntPtr pFT_SetUSBParameters = IntPtr.Zero; + private IntPtr pFT_ReadEE = IntPtr.Zero; + private IntPtr pFT_WriteEE = IntPtr.Zero; + private IntPtr pFT_EraseEE = IntPtr.Zero; + private IntPtr pFT_EE_UASize = IntPtr.Zero; + private IntPtr pFT_EE_UARead = IntPtr.Zero; + private IntPtr pFT_EE_UAWrite = IntPtr.Zero; + private IntPtr pFT_EE_Read = IntPtr.Zero; + private IntPtr pFT_EE_Program = IntPtr.Zero; + private IntPtr pFT_EEPROM_Read = IntPtr.Zero; + private IntPtr pFT_EEPROM_Program = IntPtr.Zero; + private IntPtr pFT_VendorCmdGet = IntPtr.Zero; + private IntPtr pFT_VendorCmdSet = IntPtr.Zero; + private IntPtr pFT_VendorCmdSetX = IntPtr.Zero; + + //************************************************************************** + // GetNumberOfDevices + //************************************************************************** + // Intellisense comments + /// + /// Gets the number of FTDI devices available. + /// + /// FT_STATUS value from FT_CreateDeviceInfoList in FTD2XX.DLL + /// The number of FTDI devices available. + public FT_STATUS GetNumberOfDevices(ref uint devcount) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_CreateDeviceInfoList != IntPtr.Zero) + { + tFT_CreateDeviceInfoList FT_CreateDeviceInfoList = (tFT_CreateDeviceInfoList)Marshal.GetDelegateForFunctionPointer(pFT_CreateDeviceInfoList, typeof(tFT_CreateDeviceInfoList)); + + // Call FT_CreateDeviceInfoList + ftStatus = FT_CreateDeviceInfoList(ref devcount); + } + else + { + Console.WriteLine("Failed to load function FT_CreateDeviceInfoList."); + } + return ftStatus; + + } + + + //************************************************************************** + // GetDeviceList + //************************************************************************** + // Intellisense comments + /// + /// Gets information on all of the FTDI devices available. + /// + /// FT_STATUS value from FT_GetDeviceInfoDetail in FTD2XX.DLL + /// An array of type FT_DEVICE_INFO_NODE to contain the device information for all available devices. + /// Thrown when the supplied buffer is not large enough to contain the device info list. + public FT_STATUS GetDeviceList(FT_DEVICE_INFO_NODE[] devicelist) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR; + FT_ERROR ftErrorCondition = FT_ERROR.FT_NO_ERROR; + Int32 nullIndex = 0; + + // 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_CreateDeviceInfoList != IntPtr.Zero) & (pFT_GetDeviceInfoDetail != IntPtr.Zero)) + { + uint devcount = 0; + + tFT_CreateDeviceInfoList FT_CreateDeviceInfoList = (tFT_CreateDeviceInfoList)Marshal.GetDelegateForFunctionPointer(pFT_CreateDeviceInfoList, typeof(tFT_CreateDeviceInfoList)); + tFT_GetDeviceInfoDetail FT_GetDeviceInfoDetail = (tFT_GetDeviceInfoDetail)Marshal.GetDelegateForFunctionPointer(pFT_GetDeviceInfoDetail, typeof(tFT_GetDeviceInfoDetail)); + + // Call FT_CreateDeviceInfoList + ftStatus = FT_CreateDeviceInfoList(ref devcount); + + // Allocate the required storage for our list + + byte[] sernum = new byte[16]; + byte[] desc = new byte[64]; + + if (devcount > 0) + { + // Check the size of the buffer passed in is big enough + if (devicelist.Length < devcount) + { + // Buffer not big enough + ftErrorCondition = FT_ERROR.FT_BUFFER_SIZE; + // Throw exception + ErrorHandler(ftStatus, ftErrorCondition); + } + + // Instantiate the array elements as FT_DEVICE_INFO_NODE + for (uint i = 0; i < devcount; i++) + { + devicelist[i] = new FT_DEVICE_INFO_NODE(); + // Call FT_GetDeviceInfoDetail + ftStatus = FT_GetDeviceInfoDetail(i, ref devicelist[i].Flags, ref devicelist[i].Type, ref devicelist[i].ID, ref devicelist[i].LocId, sernum, desc, ref devicelist[i].ftHandle); + // Convert byte arrays to strings + devicelist[i].SerialNumber = Encoding.ASCII.GetString(sernum); + devicelist[i].Description = Encoding.ASCII.GetString(desc); + // Trim strings to first occurrence of a null terminator character + nullIndex = devicelist[i].SerialNumber.IndexOf('\0'); + if (nullIndex != -1) + devicelist[i].SerialNumber = devicelist[i].SerialNumber.Substring(0, nullIndex); + nullIndex = devicelist[i].Description.IndexOf('\0'); + if (nullIndex != -1) + devicelist[i].Description = devicelist[i].Description.Substring(0, nullIndex); + } + } + } + else + { + if (pFT_CreateDeviceInfoList == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_CreateDeviceInfoList."); + } + if (pFT_GetDeviceInfoDetail == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_GetDeviceInfoListDetail."); + } + } + return ftStatus; + } + + + //************************************************************************** + // OpenByIndex + //************************************************************************** + // Intellisense comments + /// + /// Opens the FTDI device with the specified index. + /// + /// FT_STATUS value from FT_Open in FTD2XX.DLL + /// Index of the device to open. + /// Note that this cannot be guaranteed to open a specific device. + /// Initialises the device to 8 data bits, 1 stop bit, no parity, no flow control and 9600 Baud. + public FT_STATUS OpenByIndex(uint index) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_Open != IntPtr.Zero) & (pFT_SetDataCharacteristics != IntPtr.Zero) & (pFT_SetFlowControl != IntPtr.Zero) & (pFT_SetBaudRate != IntPtr.Zero)) + { + tFT_Open FT_Open = (tFT_Open)Marshal.GetDelegateForFunctionPointer(pFT_Open, typeof(tFT_Open)); + tFT_SetDataCharacteristics FT_SetDataCharacteristics = (tFT_SetDataCharacteristics)Marshal.GetDelegateForFunctionPointer(pFT_SetDataCharacteristics, typeof(tFT_SetDataCharacteristics)); + tFT_SetFlowControl FT_SetFlowControl = (tFT_SetFlowControl)Marshal.GetDelegateForFunctionPointer(pFT_SetFlowControl, typeof(tFT_SetFlowControl)); + tFT_SetBaudRate FT_SetBaudRate = (tFT_SetBaudRate)Marshal.GetDelegateForFunctionPointer(pFT_SetBaudRate, typeof(tFT_SetBaudRate)); + + // Call FT_Open + ftStatus = FT_Open(index, ref ftHandle); + + // Appears that the handle value can be non-NULL on a fail, so set it explicitly + if (ftStatus != FT_STATUS.FT_OK) + ftHandle = IntPtr.Zero; + + if (ftHandle != IntPtr.Zero) + { + // Initialise port data characteristics + byte WordLength = FT_DATA_BITS.FT_BITS_8; + byte StopBits = FT_STOP_BITS.FT_STOP_BITS_1; + byte Parity = FT_PARITY.FT_PARITY_NONE; + ftStatus = FT_SetDataCharacteristics(ftHandle, WordLength, StopBits, Parity); + // Initialise to no flow control + UInt16 FlowControl = FT_FLOW_CONTROL.FT_FLOW_NONE; + byte Xon = 0x11; + byte Xoff = 0x13; + ftStatus = FT_SetFlowControl(ftHandle, FlowControl, Xon, Xoff); + // Initialise Baud rate + uint BaudRate = 9600; + ftStatus = FT_SetBaudRate(ftHandle, BaudRate); + } + } + else + { + if (pFT_Open == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_Open."); + } + if (pFT_SetDataCharacteristics == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetDataCharacteristics."); + } + if (pFT_SetFlowControl == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetFlowControl."); + } + if (pFT_SetBaudRate == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetBaudRate."); + } + } + return ftStatus; + } + + + //************************************************************************** + // OpenBySerialNumber + //************************************************************************** + // Intellisense comments + /// + /// Opens the FTDI device with the specified serial number. + /// + /// FT_STATUS value from FT_OpenEx in FTD2XX.DLL + /// Serial number of the device to open. + /// Initialises the device to 8 data bits, 1 stop bit, no parity, no flow control and 9600 Baud. + public FT_STATUS OpenBySerialNumber(string serialnumber) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_OpenEx != IntPtr.Zero) & (pFT_SetDataCharacteristics != IntPtr.Zero) & (pFT_SetFlowControl != IntPtr.Zero) & (pFT_SetBaudRate != IntPtr.Zero)) + { + tFT_OpenEx FT_OpenEx = (tFT_OpenEx)Marshal.GetDelegateForFunctionPointer(pFT_OpenEx, typeof(tFT_OpenEx)); + tFT_SetDataCharacteristics FT_SetDataCharacteristics = (tFT_SetDataCharacteristics)Marshal.GetDelegateForFunctionPointer(pFT_SetDataCharacteristics, typeof(tFT_SetDataCharacteristics)); + tFT_SetFlowControl FT_SetFlowControl = (tFT_SetFlowControl)Marshal.GetDelegateForFunctionPointer(pFT_SetFlowControl, typeof(tFT_SetFlowControl)); + tFT_SetBaudRate FT_SetBaudRate = (tFT_SetBaudRate)Marshal.GetDelegateForFunctionPointer(pFT_SetBaudRate, typeof(tFT_SetBaudRate)); + + // Call FT_OpenEx + ftStatus = FT_OpenEx(serialnumber, FT_OPEN_BY_SERIAL_NUMBER, ref ftHandle); + + // Appears that the handle value can be non-NULL on a fail, so set it explicitly + if (ftStatus != FT_STATUS.FT_OK) + ftHandle = IntPtr.Zero; + + if (ftHandle != IntPtr.Zero) + { + // Initialise port data characteristics + byte WordLength = FT_DATA_BITS.FT_BITS_8; + byte StopBits = FT_STOP_BITS.FT_STOP_BITS_1; + byte Parity = FT_PARITY.FT_PARITY_NONE; + ftStatus = FT_SetDataCharacteristics(ftHandle, WordLength, StopBits, Parity); + // Initialise to no flow control + UInt16 FlowControl = FT_FLOW_CONTROL.FT_FLOW_NONE; + byte Xon = 0x11; + byte Xoff = 0x13; + ftStatus = FT_SetFlowControl(ftHandle, FlowControl, Xon, Xoff); + // Initialise Baud rate + uint BaudRate = 9600; + ftStatus = FT_SetBaudRate(ftHandle, BaudRate); + } + } + else + { + if (pFT_OpenEx == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_OpenEx."); + } + if (pFT_SetDataCharacteristics == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetDataCharacteristics."); + } + if (pFT_SetFlowControl == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetFlowControl."); + } + if (pFT_SetBaudRate == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetBaudRate."); + } + } + return ftStatus; + } + + + //************************************************************************** + // OpenByDescription + //************************************************************************** + // Intellisense comments + /// + /// Opens the FTDI device with the specified description. + /// + /// FT_STATUS value from FT_OpenEx in FTD2XX.DLL + /// Description of the device to open. + /// Initialises the device to 8 data bits, 1 stop bit, no parity, no flow control and 9600 Baud. + public FT_STATUS OpenByDescription(string description) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_OpenEx != IntPtr.Zero) & (pFT_SetDataCharacteristics != IntPtr.Zero) & (pFT_SetFlowControl != IntPtr.Zero) & (pFT_SetBaudRate != IntPtr.Zero)) + { + tFT_OpenEx FT_OpenEx = (tFT_OpenEx)Marshal.GetDelegateForFunctionPointer(pFT_OpenEx, typeof(tFT_OpenEx)); + tFT_SetDataCharacteristics FT_SetDataCharacteristics = (tFT_SetDataCharacteristics)Marshal.GetDelegateForFunctionPointer(pFT_SetDataCharacteristics, typeof(tFT_SetDataCharacteristics)); + tFT_SetFlowControl FT_SetFlowControl = (tFT_SetFlowControl)Marshal.GetDelegateForFunctionPointer(pFT_SetFlowControl, typeof(tFT_SetFlowControl)); + tFT_SetBaudRate FT_SetBaudRate = (tFT_SetBaudRate)Marshal.GetDelegateForFunctionPointer(pFT_SetBaudRate, typeof(tFT_SetBaudRate)); + + // Call FT_OpenEx + ftStatus = FT_OpenEx(description, FT_OPEN_BY_DESCRIPTION, ref ftHandle); + + // Appears that the handle value can be non-NULL on a fail, so set it explicitly + if (ftStatus != FT_STATUS.FT_OK) + ftHandle = IntPtr.Zero; + + if (ftHandle != IntPtr.Zero) + { + // Initialise port data characteristics + byte WordLength = FT_DATA_BITS.FT_BITS_8; + byte StopBits = FT_STOP_BITS.FT_STOP_BITS_1; + byte Parity = FT_PARITY.FT_PARITY_NONE; + ftStatus = FT_SetDataCharacteristics(ftHandle, WordLength, StopBits, Parity); + // Initialise to no flow control + UInt16 FlowControl = FT_FLOW_CONTROL.FT_FLOW_NONE; + byte Xon = 0x11; + byte Xoff = 0x13; + ftStatus = FT_SetFlowControl(ftHandle, FlowControl, Xon, Xoff); + // Initialise Baud rate + uint BaudRate = 9600; + ftStatus = FT_SetBaudRate(ftHandle, BaudRate); + } + } + else + { + if (pFT_OpenEx == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_OpenEx."); + } + if (pFT_SetDataCharacteristics == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetDataCharacteristics."); + } + if (pFT_SetFlowControl == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetFlowControl."); + } + if (pFT_SetBaudRate == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetBaudRate."); + } + } + return ftStatus; + } + + + //************************************************************************** + // OpenByLocation + //************************************************************************** + // Intellisense comments + /// + /// Opens the FTDI device at the specified physical location. + /// + /// FT_STATUS value from FT_OpenEx in FTD2XX.DLL + /// Location of the device to open. + /// Initialises the device to 8 data bits, 1 stop bit, no parity, no flow control and 9600 Baud. + public FT_STATUS OpenByLocation(uint location) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_OpenEx != IntPtr.Zero) & (pFT_SetDataCharacteristics != IntPtr.Zero) & (pFT_SetFlowControl != IntPtr.Zero) & (pFT_SetBaudRate != IntPtr.Zero)) + { + tFT_OpenExLoc FT_OpenEx = (tFT_OpenExLoc)Marshal.GetDelegateForFunctionPointer(pFT_OpenEx, typeof(tFT_OpenExLoc)); + tFT_SetDataCharacteristics FT_SetDataCharacteristics = (tFT_SetDataCharacteristics)Marshal.GetDelegateForFunctionPointer(pFT_SetDataCharacteristics, typeof(tFT_SetDataCharacteristics)); + tFT_SetFlowControl FT_SetFlowControl = (tFT_SetFlowControl)Marshal.GetDelegateForFunctionPointer(pFT_SetFlowControl, typeof(tFT_SetFlowControl)); + tFT_SetBaudRate FT_SetBaudRate = (tFT_SetBaudRate)Marshal.GetDelegateForFunctionPointer(pFT_SetBaudRate, typeof(tFT_SetBaudRate)); + + // Call FT_OpenEx + ftStatus = FT_OpenEx(location, FT_OPEN_BY_LOCATION, ref ftHandle); + + // Appears that the handle value can be non-NULL on a fail, so set it explicitly + if (ftStatus != FT_STATUS.FT_OK) + ftHandle = IntPtr.Zero; + + if (ftHandle != IntPtr.Zero) + { + // Initialise port data characteristics + byte WordLength = FT_DATA_BITS.FT_BITS_8; + byte StopBits = FT_STOP_BITS.FT_STOP_BITS_1; + byte Parity = FT_PARITY.FT_PARITY_NONE; + FT_SetDataCharacteristics(ftHandle, WordLength, StopBits, Parity); + // Initialise to no flow control + UInt16 FlowControl = FT_FLOW_CONTROL.FT_FLOW_NONE; + byte Xon = 0x11; + byte Xoff = 0x13; + FT_SetFlowControl(ftHandle, FlowControl, Xon, Xoff); + // Initialise Baud rate + uint BaudRate = 9600; + FT_SetBaudRate(ftHandle, BaudRate); + } + } + else + { + if (pFT_OpenEx == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_OpenEx."); + } + if (pFT_SetDataCharacteristics == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetDataCharacteristics."); + } + if (pFT_SetFlowControl == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetFlowControl."); + } + if (pFT_SetBaudRate == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetBaudRate."); + } + } + return ftStatus; + } + + + //************************************************************************** + // Close + //************************************************************************** + // Intellisense comments + /// + /// Closes the handle to an open FTDI device. + /// + /// FT_STATUS value from FT_Close in FTD2XX.DLL + public FT_STATUS Close() + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_Close != IntPtr.Zero) + { + tFT_Close FT_Close = (tFT_Close)Marshal.GetDelegateForFunctionPointer(pFT_Close, typeof(tFT_Close)); + + // Call FT_Close + ftStatus = FT_Close(ftHandle); + + if (ftStatus == FT_STATUS.FT_OK) + { + ftHandle = IntPtr.Zero; + } + } + else + { + if (pFT_Close == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_Close."); + } + } + return ftStatus; + } + + + //************************************************************************** + // Read + //************************************************************************** + // Intellisense comments + /// + /// Read data from an open FTDI device. + /// + /// FT_STATUS value from FT_Read in FTD2XX.DLL + /// An array of bytes which will be populated with the data read from the device. + /// The number of bytes requested from the device. + /// The number of bytes actually read. + public FT_STATUS Read(byte[] dataBuffer, uint numBytesToRead, ref uint numBytesRead) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_Read != IntPtr.Zero) + { + + tFT_Read FT_Read = (tFT_Read)Marshal.GetDelegateForFunctionPointer(pFT_Read, typeof(tFT_Read)); + + // If the buffer is not big enough to receive the amount of data requested, adjust the number of bytes to read + if (dataBuffer.Length < numBytesToRead) + { + numBytesToRead = (uint)dataBuffer.Length; + } + + if (ftHandle != IntPtr.Zero) + { + // Call FT_Read + ftStatus = FT_Read(ftHandle, dataBuffer, numBytesToRead, ref numBytesRead); + } + } + else + { + if (pFT_Read == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_Read."); + } + } + return ftStatus; + } + + // Intellisense comments + /// + /// Read data from an open FTDI device. + /// + /// FT_STATUS value from FT_Read in FTD2XX.DLL + /// A string containing the data read + /// The number of bytes requested from the device. + /// The number of bytes actually read. + public FT_STATUS Read(out string dataBuffer, uint numBytesToRead, ref uint numBytesRead) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR; + + // As dataBuffer is an OUT parameter, needs to be assigned before returning + dataBuffer = string.Empty; + + // 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_Read != IntPtr.Zero) + { + tFT_Read FT_Read = (tFT_Read)Marshal.GetDelegateForFunctionPointer(pFT_Read, typeof(tFT_Read)); + + byte[] byteDataBuffer = new byte[numBytesToRead]; + + if (ftHandle != IntPtr.Zero) + { + // Call FT_Read + ftStatus = FT_Read(ftHandle, byteDataBuffer, numBytesToRead, ref numBytesRead); + + // Convert ASCII byte array back to Unicode string for passing back + dataBuffer = Encoding.ASCII.GetString(byteDataBuffer); + // Trim buffer to actual bytes read + dataBuffer = dataBuffer.Substring(0, (int)numBytesRead); + } + } + else + { + if (pFT_Read == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_Read."); + } + } + return ftStatus; + } + + //************************************************************************** + // Write + //************************************************************************** + // Intellisense comments + /// + /// Write data to an open FTDI device. + /// + /// FT_STATUS value from FT_Write in FTD2XX.DLL + /// An array of bytes which contains the data to be written to the device. + /// The number of bytes to be written to the device. + /// The number of bytes actually written to the device. + public FT_STATUS Write(byte[] dataBuffer, Int32 numBytesToWrite, ref uint numBytesWritten) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_Write != IntPtr.Zero) + { + tFT_Write FT_Write = (tFT_Write)Marshal.GetDelegateForFunctionPointer(pFT_Write, typeof(tFT_Write)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_Write + ftStatus = FT_Write(ftHandle, dataBuffer, (uint)numBytesToWrite, ref numBytesWritten); + } + } + else + { + if (pFT_Write == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_Write."); + } + } + return ftStatus; + } + + // Intellisense comments + /// + /// Write data to an open FTDI device. + /// + /// FT_STATUS value from FT_Write in FTD2XX.DLL + /// An array of bytes which contains the data to be written to the device. + /// The number of bytes to be written to the device. + /// The number of bytes actually written to the device. + public FT_STATUS Write(byte[] dataBuffer, uint numBytesToWrite, ref uint numBytesWritten) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_Write != IntPtr.Zero) + { + tFT_Write FT_Write = (tFT_Write)Marshal.GetDelegateForFunctionPointer(pFT_Write, typeof(tFT_Write)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_Write + ftStatus = FT_Write(ftHandle, dataBuffer, numBytesToWrite, ref numBytesWritten); + } + } + else + { + if (pFT_Write == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_Write."); + } + } + return ftStatus; + } + + // Intellisense comments + /// + /// Write data to an open FTDI device. + /// + /// FT_STATUS value from FT_Write in FTD2XX.DLL + /// A string which contains the data to be written to the device. + /// The number of bytes to be written to the device. + /// The number of bytes actually written to the device. + public FT_STATUS Write(string dataBuffer, Int32 numBytesToWrite, ref uint numBytesWritten) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_Write != IntPtr.Zero) + { + tFT_Write FT_Write = (tFT_Write)Marshal.GetDelegateForFunctionPointer(pFT_Write, typeof(tFT_Write)); + + // Convert Unicode string to ASCII byte array + byte[] byteDataBuffer = Encoding.ASCII.GetBytes(dataBuffer); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_Write + ftStatus = FT_Write(ftHandle, byteDataBuffer, (uint)numBytesToWrite, ref numBytesWritten); + } + } + else + { + if (pFT_Write == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_Write."); + } + } + return ftStatus; + } + + // Intellisense comments + /// + /// Write data to an open FTDI device. + /// + /// FT_STATUS value from FT_Write in FTD2XX.DLL + /// A string which contains the data to be written to the device. + /// The number of bytes to be written to the device. + /// The number of bytes actually written to the device. + public FT_STATUS Write(string dataBuffer, uint numBytesToWrite, ref uint numBytesWritten) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_Write != IntPtr.Zero) + { + tFT_Write FT_Write = (tFT_Write)Marshal.GetDelegateForFunctionPointer(pFT_Write, typeof(tFT_Write)); + + // Convert Unicode string to ASCII byte array + byte[] byteDataBuffer = Encoding.ASCII.GetBytes(dataBuffer); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_Write + ftStatus = FT_Write(ftHandle, byteDataBuffer, numBytesToWrite, ref numBytesWritten); + } + } + else + { + if (pFT_Write == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_Write."); + } + } + return ftStatus; + } + + //************************************************************************** + // ResetDevice + //************************************************************************** + // Intellisense comments + /// + /// Reset an open FTDI device. + /// + /// FT_STATUS value from FT_ResetDevice in FTD2XX.DLL + public FT_STATUS ResetDevice() + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_ResetDevice != IntPtr.Zero) + { + tFT_ResetDevice FT_ResetDevice = (tFT_ResetDevice)Marshal.GetDelegateForFunctionPointer(pFT_ResetDevice, typeof(tFT_ResetDevice)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_ResetDevice + ftStatus = FT_ResetDevice(ftHandle); + } + } + else + { + if (pFT_ResetDevice == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_ResetDevice."); + } + } + return ftStatus; + } + + //************************************************************************** + // Purge + //************************************************************************** + // Intellisense comments + /// + /// Purge data from the devices transmit and/or receive buffers. + /// + /// FT_STATUS value from FT_Purge in FTD2XX.DLL + /// Specifies which buffer(s) to be purged. Valid values are any combination of the following flags: FT_PURGE_RX, FT_PURGE_TX + public FT_STATUS Purge(uint purgemask) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_Purge != IntPtr.Zero) + { + tFT_Purge FT_Purge = (tFT_Purge)Marshal.GetDelegateForFunctionPointer(pFT_Purge, typeof(tFT_Purge)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_Purge + ftStatus = FT_Purge(ftHandle, purgemask); + } + } + else + { + if (pFT_Purge == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_Purge."); + } + } + return ftStatus; + } + + //************************************************************************** + // SetEventNotification + //************************************************************************** + // Intellisense comments + /// + /// Register for event notification. + /// + /// FT_STATUS value from FT_SetEventNotification in FTD2XX.DLL + /// After setting event notification, the event can be caught by executing the WaitOne() method of the EventWaitHandle. If multiple event types are being monitored, the event that fired can be determined from the GetEventType method. + /// The type of events to signal. Can be any combination of the following: FT_EVENT_RXCHAR, FT_EVENT_MODEM_STATUS, FT_EVENT_LINE_STATUS + /// Handle to the event that will receive the notification + public FT_STATUS SetEventNotification(uint eventmask, EventWaitHandle eventhandle) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetEventNotification != IntPtr.Zero) + { + tFT_SetEventNotification FT_SetEventNotification = (tFT_SetEventNotification)Marshal.GetDelegateForFunctionPointer(pFT_SetEventNotification, typeof(tFT_SetEventNotification)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_SetSetEventNotification + ftStatus = FT_SetEventNotification(ftHandle, eventmask, eventhandle.SafeWaitHandle); + } + } + else + { + if (pFT_SetEventNotification == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetEventNotification."); + } + } + return ftStatus; + } + + //************************************************************************** + // StopInTask + //************************************************************************** + // Intellisense comments + /// + /// Stops the driver issuing USB in requests. + /// + /// FT_STATUS value from FT_StopInTask in FTD2XX.DLL + public FT_STATUS StopInTask() + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_StopInTask != IntPtr.Zero) + { + tFT_StopInTask FT_StopInTask = (tFT_StopInTask)Marshal.GetDelegateForFunctionPointer(pFT_StopInTask, typeof(tFT_StopInTask)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_StopInTask + ftStatus = FT_StopInTask(ftHandle); + } + } + else + { + if (pFT_StopInTask == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_StopInTask."); + } + } + return ftStatus; + } + + //************************************************************************** + // RestartInTask + //************************************************************************** + // Intellisense comments + /// + /// Resumes the driver issuing USB in requests. + /// + /// FT_STATUS value from FT_RestartInTask in FTD2XX.DLL + public FT_STATUS RestartInTask() + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_RestartInTask != IntPtr.Zero) + { + tFT_RestartInTask FT_RestartInTask = (tFT_RestartInTask)Marshal.GetDelegateForFunctionPointer(pFT_RestartInTask, typeof(tFT_RestartInTask)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_RestartInTask + ftStatus = FT_RestartInTask(ftHandle); + } + } + else + { + if (pFT_RestartInTask == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_RestartInTask."); + } + } + return ftStatus; + } + + //************************************************************************** + // ResetPort + //************************************************************************** + // Intellisense comments + /// + /// Resets the device port. + /// + /// FT_STATUS value from FT_ResetPort in FTD2XX.DLL + public FT_STATUS ResetPort() + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_ResetPort != IntPtr.Zero) + { + tFT_ResetPort FT_ResetPort = (tFT_ResetPort)Marshal.GetDelegateForFunctionPointer(pFT_ResetPort, typeof(tFT_ResetPort)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_ResetPort + ftStatus = FT_ResetPort(ftHandle); + } + } + else + { + if (pFT_ResetPort == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_ResetPort."); + } + } + return ftStatus; + } + + //************************************************************************** + // CyclePort + //************************************************************************** + // Intellisense comments + /// + /// Causes the device to be re-enumerated on the USB bus. This is equivalent to unplugging and replugging the device. + /// Also calls FT_Close if FT_CyclePort is successful, so no need to call this separately in the application. + /// + /// FT_STATUS value from FT_CyclePort in FTD2XX.DLL + public FT_STATUS CyclePort() + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_CyclePort != IntPtr.Zero) & (pFT_Close != IntPtr.Zero)) + { + tFT_CyclePort FT_CyclePort = (tFT_CyclePort)Marshal.GetDelegateForFunctionPointer(pFT_CyclePort, typeof(tFT_CyclePort)); + tFT_Close FT_Close = (tFT_Close)Marshal.GetDelegateForFunctionPointer(pFT_Close, typeof(tFT_Close)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_CyclePort + ftStatus = FT_CyclePort(ftHandle); + if (ftStatus == FT_STATUS.FT_OK) + { + // If successful, call FT_Close + ftStatus = FT_Close(ftHandle); + if (ftStatus == FT_STATUS.FT_OK) + { + ftHandle = IntPtr.Zero; + } + } + } + } + else + { + if (pFT_CyclePort == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_CyclePort."); + } + if (pFT_Close == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_Close."); + } + } + return ftStatus; + } + + //************************************************************************** + // Rescan + //************************************************************************** + // Intellisense comments + /// + /// Causes the system to check for USB hardware changes. This is equivalent to clicking on the "Scan for hardware changes" button in the Device Manager. + /// + /// FT_STATUS value from FT_Rescan in FTD2XX.DLL + public FT_STATUS Rescan() + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_Rescan != IntPtr.Zero) + { + tFT_Rescan FT_Rescan = (tFT_Rescan)Marshal.GetDelegateForFunctionPointer(pFT_Rescan, typeof(tFT_Rescan)); + + // Call FT_Rescan + ftStatus = FT_Rescan(); + } + else + { + if (pFT_Rescan == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_Rescan."); + } + } + return ftStatus; + } + + //************************************************************************** + // Reload + //************************************************************************** + // Intellisense comments + /// + /// Forces a reload of the driver for devices with a specific VID and PID combination. + /// + /// FT_STATUS value from FT_Reload in FTD2XX.DLL + /// If the VID and PID parameters are 0, the drivers for USB root hubs will be reloaded, causing all USB devices connected to reload their drivers + /// Vendor ID of the devices to have the driver reloaded + /// Product ID of the devices to have the driver reloaded + public FT_STATUS Reload(UInt16 VendorID, UInt16 ProductID) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_Reload != IntPtr.Zero) + { + tFT_Reload FT_Reload = (tFT_Reload)Marshal.GetDelegateForFunctionPointer(pFT_Reload, typeof(tFT_Reload)); + + // Call FT_Reload + ftStatus = FT_Reload(VendorID, ProductID); + } + else + { + if (pFT_Reload == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_Reload."); + } + } + return ftStatus; + } + + //************************************************************************** + // SetBitMode + //************************************************************************** + // Intellisense comments + /// + /// Puts the device in a mode other than the default UART or FIFO mode. + /// + /// FT_STATUS value from FT_SetBitMode in FTD2XX.DLL + /// Sets up which bits are inputs and which are outputs. A bit value of 0 sets the corresponding pin to an input, a bit value of 1 sets the corresponding pin to an output. + /// In the case of CBUS Bit Bang, the upper nibble of this value controls which pins are inputs and outputs, while the lower nibble controls which of the outputs are high and low. + /// For FT232H devices, valid values are FT_BIT_MODE_RESET, FT_BIT_MODE_ASYNC_BITBANG, FT_BIT_MODE_MPSSE, FT_BIT_MODE_SYNC_BITBANG, FT_BIT_MODE_CBUS_BITBANG, FT_BIT_MODE_MCU_HOST, FT_BIT_MODE_FAST_SERIAL, FT_BIT_MODE_SYNC_FIFO. + /// For FT2232H devices, valid values are FT_BIT_MODE_RESET, FT_BIT_MODE_ASYNC_BITBANG, FT_BIT_MODE_MPSSE, FT_BIT_MODE_SYNC_BITBANG, FT_BIT_MODE_MCU_HOST, FT_BIT_MODE_FAST_SERIAL, FT_BIT_MODE_SYNC_FIFO. + /// For FT4232H devices, valid values are FT_BIT_MODE_RESET, FT_BIT_MODE_ASYNC_BITBANG, FT_BIT_MODE_MPSSE, FT_BIT_MODE_SYNC_BITBANG. + /// For FT232R devices, valid values are FT_BIT_MODE_RESET, FT_BIT_MODE_ASYNC_BITBANG, FT_BIT_MODE_SYNC_BITBANG, FT_BIT_MODE_CBUS_BITBANG. + /// For FT245R devices, valid values are FT_BIT_MODE_RESET, FT_BIT_MODE_ASYNC_BITBANG, FT_BIT_MODE_SYNC_BITBANG. + /// For FT2232 devices, valid values are FT_BIT_MODE_RESET, FT_BIT_MODE_ASYNC_BITBANG, FT_BIT_MODE_MPSSE, FT_BIT_MODE_SYNC_BITBANG, FT_BIT_MODE_MCU_HOST, FT_BIT_MODE_FAST_SERIAL. + /// For FT232B and FT245B devices, valid values are FT_BIT_MODE_RESET, FT_BIT_MODE_ASYNC_BITBANG. + /// Thrown when the current device does not support the requested bit mode. + public FT_STATUS SetBitMode(byte Mask, byte BitMode) + { + // 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_SetBitMode != IntPtr.Zero) + { + tFT_SetBitMode FT_SetBitMode = (tFT_SetBitMode)Marshal.GetDelegateForFunctionPointer(pFT_SetBitMode, typeof(tFT_SetBitMode)); + + if (ftHandle != IntPtr.Zero) + { + + FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN; + // Set Bit Mode does not apply to FT8U232AM, FT8U245AM or FT8U100AX devices + GetDeviceType(ref DeviceType); + if (DeviceType == FT_DEVICE.FT_DEVICE_AM) + { + // Throw an exception + ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE; + ErrorHandler(ftStatus, ftErrorCondition); + } + else if (DeviceType == FT_DEVICE.FT_DEVICE_100AX) + { + // Throw an exception + ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE; + ErrorHandler(ftStatus, ftErrorCondition); + } + else if ((DeviceType == FT_DEVICE.FT_DEVICE_BM) && (BitMode != FT_BIT_MODES.FT_BIT_MODE_RESET)) + { + if ((BitMode & (FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG)) == 0) + { + // Throw an exception + ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE; + ErrorHandler(ftStatus, ftErrorCondition); + } + } + else if ((DeviceType == FT_DEVICE.FT_DEVICE_2232) && (BitMode != FT_BIT_MODES.FT_BIT_MODE_RESET)) + { + if ((BitMode & (FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG | FT_BIT_MODES.FT_BIT_MODE_MPSSE | FT_BIT_MODES.FT_BIT_MODE_SYNC_BITBANG | FT_BIT_MODES.FT_BIT_MODE_MCU_HOST | FT_BIT_MODES.FT_BIT_MODE_FAST_SERIAL)) == 0) + { + // Throw an exception + ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE; + ErrorHandler(ftStatus, ftErrorCondition); + } + if ((BitMode == FT_BIT_MODES.FT_BIT_MODE_MPSSE) & (InterfaceIdentifier != "A")) + { + // MPSSE mode is only available on channel A + // Throw an exception + ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE; + ErrorHandler(ftStatus, ftErrorCondition); + } + } + else if ((DeviceType == FT_DEVICE.FT_DEVICE_232R) && (BitMode != FT_BIT_MODES.FT_BIT_MODE_RESET)) + { + if ((BitMode & (FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG | FT_BIT_MODES.FT_BIT_MODE_SYNC_BITBANG | FT_BIT_MODES.FT_BIT_MODE_CBUS_BITBANG)) == 0) + { + // Throw an exception + ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE; + ErrorHandler(ftStatus, ftErrorCondition); + } + } + else if (((DeviceType == FT_DEVICE.FT_DEVICE_2232H) + || (DeviceType == FT_DEVICE.FT_DEVICE_2232HP) + || (DeviceType == FT_DEVICE.FT_DEVICE_2233HP) + || (DeviceType == FT_DEVICE.FT_DEVICE_2232HA)) + && (BitMode != FT_BIT_MODES.FT_BIT_MODE_RESET)) + { + if ((BitMode & (FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG | FT_BIT_MODES.FT_BIT_MODE_MPSSE | FT_BIT_MODES.FT_BIT_MODE_SYNC_BITBANG | FT_BIT_MODES.FT_BIT_MODE_MCU_HOST | FT_BIT_MODES.FT_BIT_MODE_FAST_SERIAL | FT_BIT_MODES.FT_BIT_MODE_SYNC_FIFO)) == 0) + { + // Throw an exception + ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE; + ErrorHandler(ftStatus, ftErrorCondition); + } + if (((BitMode == FT_BIT_MODES.FT_BIT_MODE_MCU_HOST) | (BitMode == FT_BIT_MODES.FT_BIT_MODE_SYNC_FIFO)) & (InterfaceIdentifier != "A")) + { + // MCU Host Emulation and Single channel synchronous 245 FIFO mode is only available on channel A + // Throw an exception + ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE; + ErrorHandler(ftStatus, ftErrorCondition); + } + } + else if (((DeviceType == FT_DEVICE.FT_DEVICE_4232H) + || (DeviceType == FT_DEVICE.FT_DEVICE_4232HP) + || (DeviceType == FT_DEVICE.FT_DEVICE_4233HP) + || (DeviceType == FT_DEVICE.FT_DEVICE_4232HA)) + && (BitMode != FT_BIT_MODES.FT_BIT_MODE_RESET)) + { + if ((BitMode & (FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG | FT_BIT_MODES.FT_BIT_MODE_MPSSE | FT_BIT_MODES.FT_BIT_MODE_SYNC_BITBANG)) == 0) + { + // Throw an exception + ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE; + ErrorHandler(ftStatus, ftErrorCondition); + } + if ((BitMode == FT_BIT_MODES.FT_BIT_MODE_MPSSE) & ((InterfaceIdentifier != "A") & (InterfaceIdentifier != "B"))) + { + // MPSSE mode is only available on channel A and B + // Throw an exception + ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE; + ErrorHandler(ftStatus, ftErrorCondition); + } + } + else if (((DeviceType == FT_DEVICE.FT_DEVICE_232H) + || (DeviceType == FT_DEVICE.FT_DEVICE_232HP) + || (DeviceType == FT_DEVICE.FT_DEVICE_233HP)) + && (BitMode != FT_BIT_MODES.FT_BIT_MODE_RESET)) + { + // FT232H supports all current bit modes! + if (BitMode > FT_BIT_MODES.FT_BIT_MODE_SYNC_FIFO) + { + // Throw an exception + ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE; + ErrorHandler(ftStatus, ftErrorCondition); + } + } + + // Requested bit mode is supported + // Note FT_BIT_MODES.FT_BIT_MODE_RESET falls through to here - no bits set so cannot check for AND + // Call FT_SetBitMode + ftStatus = FT_SetBitMode(ftHandle, Mask, BitMode); + } + } + else + { + if (pFT_SetBitMode == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetBitMode."); + } + } + return ftStatus; + } + + //************************************************************************** + // GetPinStates + //************************************************************************** + // Intellisense comments + /// + /// Gets the instantaneous state of the device IO pins. + /// + /// FT_STATUS value from FT_GetBitMode in FTD2XX.DLL + /// A bitmap value containing the instantaneous state of the device IO pins + public FT_STATUS GetPinStates(ref byte BitMode) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_GetBitMode != IntPtr.Zero) + { + tFT_GetBitMode FT_GetBitMode = (tFT_GetBitMode)Marshal.GetDelegateForFunctionPointer(pFT_GetBitMode, typeof(tFT_GetBitMode)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_GetBitMode + ftStatus = FT_GetBitMode(ftHandle, ref BitMode); + } + } + else + { + if (pFT_GetBitMode == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_GetBitMode."); + } + } + return ftStatus; + } + + //************************************************************************** + // ReadEEPROMLocation + //************************************************************************** + // Intellisense comments + /// + /// Reads an individual word value from a specified location in the device's EEPROM. + /// + /// FT_STATUS value from FT_ReadEE in FTD2XX.DLL + /// The EEPROM location to read data from + /// The WORD value read from the EEPROM location specified in the Address paramter + public FT_STATUS ReadEEPROMLocation(uint Address, ref UInt16 EEValue) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_ReadEE != IntPtr.Zero) + { + tFT_ReadEE FT_ReadEE = (tFT_ReadEE)Marshal.GetDelegateForFunctionPointer(pFT_ReadEE, typeof(tFT_ReadEE)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_ReadEE + ftStatus = FT_ReadEE(ftHandle, Address, ref EEValue); + } + } + else + { + if (pFT_ReadEE == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_ReadEE."); + } + } + return ftStatus; + } + + //************************************************************************** + // WriteEEPROMLocation + //************************************************************************** + // Intellisense comments + /// + /// Writes an individual word value to a specified location in the device's EEPROM. + /// + /// FT_STATUS value from FT_WriteEE in FTD2XX.DLL + /// The EEPROM location to read data from + /// The WORD value to write to the EEPROM location specified by the Address parameter + public FT_STATUS WriteEEPROMLocation(uint Address, UInt16 EEValue) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_WriteEE != IntPtr.Zero) + { + tFT_WriteEE FT_WriteEE = (tFT_WriteEE)Marshal.GetDelegateForFunctionPointer(pFT_WriteEE, typeof(tFT_WriteEE)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_WriteEE + ftStatus = FT_WriteEE(ftHandle, Address, EEValue); + } + } + else + { + if (pFT_WriteEE == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_WriteEE."); + } + } + return ftStatus; + } + + //************************************************************************** + // EraseEEPROM + //************************************************************************** + // Intellisense comments + /// + /// Erases the device EEPROM. + /// + /// FT_STATUS value from FT_EraseEE in FTD2XX.DLL + /// Thrown when attempting to erase the EEPROM of a device with an internal EEPROM such as an FT232R or FT245R. + public FT_STATUS EraseEEPROM() + { + // 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_EraseEE != IntPtr.Zero) + { + tFT_EraseEE FT_EraseEE = (tFT_EraseEE)Marshal.GetDelegateForFunctionPointer(pFT_EraseEE, typeof(tFT_EraseEE)); + + if (ftHandle != IntPtr.Zero) + { + FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN; + // Check that it is not an FT232R or FT245R that we are trying to erase + GetDeviceType(ref DeviceType); + if (DeviceType == FT_DEVICE.FT_DEVICE_232R) + { + // If it is a device with an internal EEPROM, throw an exception + ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE; + ErrorHandler(ftStatus, ftErrorCondition); + } + + // Call FT_EraseEE + ftStatus = FT_EraseEE(ftHandle); + } + } + else + { + if (pFT_EraseEE == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_EraseEE."); + } + } + return ftStatus; + } + + //************************************************************************** + // ReadFT232BEEPROM + //************************************************************************** + // Intellisense comments + /// + /// Reads the EEPROM contents of an FT232B or FT245B device. + /// + /// FT_STATUS value from FT_EE_Read in FTD2XX DLL + /// An FT232B_EEPROM_STRUCTURE which contains only the relevant information for an FT232B and FT245B device. + /// Thrown when the current device does not match the type required by this method. + public FT_STATUS ReadFT232BEEPROM(FT232B_EEPROM_STRUCTURE ee232b) + { + // 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 FT232B or FT245B that we are trying to read + GetDeviceType(ref DeviceType); + if (DeviceType != FT_DEVICE.FT_DEVICE_BM) + { + // 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 = 2; + + // 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 + ee232b.Manufacturer = Marshal.PtrToStringAnsi(eedata.Manufacturer); + ee232b.ManufacturerID = Marshal.PtrToStringAnsi(eedata.ManufacturerID); + ee232b.Description = Marshal.PtrToStringAnsi(eedata.Description); + ee232b.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 + ee232b.VendorID = eedata.VendorID; + ee232b.ProductID = eedata.ProductID; + ee232b.MaxPower = eedata.MaxPower; + ee232b.SelfPowered = Convert.ToBoolean(eedata.SelfPowered); + ee232b.RemoteWakeup = Convert.ToBoolean(eedata.RemoteWakeup); + // B specific fields + ee232b.PullDownEnable = Convert.ToBoolean(eedata.PullDownEnable); + ee232b.SerNumEnable = Convert.ToBoolean(eedata.SerNumEnable); + ee232b.USBVersionEnable = Convert.ToBoolean(eedata.USBVersionEnable); + ee232b.USBVersion = eedata.USBVersion; + } + } + else + { + if (pFT_EE_Read == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_EE_Read."); + } + } + return ftStatus; + } + + //************************************************************************** + // ReadFT2232EEPROM + //************************************************************************** + // Intellisense comments + /// + /// Reads the EEPROM contents of an FT2232 device. + /// + /// FT_STATUS value from FT_EE_Read in FTD2XX DLL + /// An FT2232_EEPROM_STRUCTURE which contains only the relevant information for an FT2232 device. + /// Thrown when the current device does not match the type required by this method. + public FT_STATUS ReadFT2232EEPROM(FT2232_EEPROM_STRUCTURE ee2232) + { + // 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 FT2232 that we are trying to read + GetDeviceType(ref DeviceType); + if (DeviceType != FT_DEVICE.FT_DEVICE_2232) + { + // 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 = 2; + + // 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 + ee2232.Manufacturer = Marshal.PtrToStringAnsi(eedata.Manufacturer); + ee2232.ManufacturerID = Marshal.PtrToStringAnsi(eedata.ManufacturerID); + ee2232.Description = Marshal.PtrToStringAnsi(eedata.Description); + ee2232.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 + ee2232.VendorID = eedata.VendorID; + ee2232.ProductID = eedata.ProductID; + ee2232.MaxPower = eedata.MaxPower; + ee2232.SelfPowered = Convert.ToBoolean(eedata.SelfPowered); + ee2232.RemoteWakeup = Convert.ToBoolean(eedata.RemoteWakeup); + // 2232 specific fields + ee2232.PullDownEnable = Convert.ToBoolean(eedata.PullDownEnable5); + ee2232.SerNumEnable = Convert.ToBoolean(eedata.SerNumEnable5); + ee2232.USBVersionEnable = Convert.ToBoolean(eedata.USBVersionEnable5); + ee2232.USBVersion = eedata.USBVersion5; + ee2232.AIsHighCurrent = Convert.ToBoolean(eedata.AIsHighCurrent); + ee2232.BIsHighCurrent = Convert.ToBoolean(eedata.BIsHighCurrent); + ee2232.IFAIsFifo = Convert.ToBoolean(eedata.IFAIsFifo); + ee2232.IFAIsFifoTar = Convert.ToBoolean(eedata.IFAIsFifoTar); + ee2232.IFAIsFastSer = Convert.ToBoolean(eedata.IFAIsFastSer); + ee2232.AIsVCP = Convert.ToBoolean(eedata.AIsVCP); + ee2232.IFBIsFifo = Convert.ToBoolean(eedata.IFBIsFifo); + ee2232.IFBIsFifoTar = Convert.ToBoolean(eedata.IFBIsFifoTar); + ee2232.IFBIsFastSer = Convert.ToBoolean(eedata.IFBIsFastSer); + ee2232.BIsVCP = Convert.ToBoolean(eedata.BIsVCP); + } + } + else + { + if (pFT_EE_Read == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_EE_Read."); + } + } + return ftStatus; + } + + //************************************************************************** + // ReadFT232REEPROM + //************************************************************************** + // Intellisense comments + /// + /// Reads the EEPROM contents of an FT232R or FT245R device. + /// Calls FT_EE_Read in FTD2XX DLL + /// + /// An FT232R_EEPROM_STRUCTURE which contains only the relevant information for an FT232R and FT245R device. + /// Thrown when the current device does not match the type required by this method. + public FT_STATUS ReadFT232REEPROM(FT232R_EEPROM_STRUCTURE ee232r) + { + // 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 FT232R or FT245R that we are trying to read + GetDeviceType(ref DeviceType); + if (DeviceType != FT_DEVICE.FT_DEVICE_232R) + { + // 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 = 2; + + // 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 + ee232r.Manufacturer = Marshal.PtrToStringAnsi(eedata.Manufacturer); + ee232r.ManufacturerID = Marshal.PtrToStringAnsi(eedata.ManufacturerID); + ee232r.Description = Marshal.PtrToStringAnsi(eedata.Description); + ee232r.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 + ee232r.VendorID = eedata.VendorID; + ee232r.ProductID = eedata.ProductID; + ee232r.MaxPower = eedata.MaxPower; + ee232r.SelfPowered = Convert.ToBoolean(eedata.SelfPowered); + ee232r.RemoteWakeup = Convert.ToBoolean(eedata.RemoteWakeup); + // 232R specific fields + ee232r.UseExtOsc = Convert.ToBoolean(eedata.UseExtOsc); + ee232r.HighDriveIOs = Convert.ToBoolean(eedata.HighDriveIOs); + ee232r.EndpointSize = eedata.EndpointSize; + ee232r.PullDownEnable = Convert.ToBoolean(eedata.PullDownEnableR); + ee232r.SerNumEnable = Convert.ToBoolean(eedata.SerNumEnableR); + ee232r.InvertTXD = Convert.ToBoolean(eedata.InvertTXD); + ee232r.InvertRXD = Convert.ToBoolean(eedata.InvertRXD); + ee232r.InvertRTS = Convert.ToBoolean(eedata.InvertRTS); + ee232r.InvertCTS = Convert.ToBoolean(eedata.InvertCTS); + ee232r.InvertDTR = Convert.ToBoolean(eedata.InvertDTR); + ee232r.InvertDSR = Convert.ToBoolean(eedata.InvertDSR); + ee232r.InvertDCD = Convert.ToBoolean(eedata.InvertDCD); + ee232r.InvertRI = Convert.ToBoolean(eedata.InvertRI); + ee232r.Cbus0 = eedata.Cbus0; + ee232r.Cbus1 = eedata.Cbus1; + ee232r.Cbus2 = eedata.Cbus2; + ee232r.Cbus3 = eedata.Cbus3; + ee232r.Cbus4 = eedata.Cbus4; + ee232r.RIsD2XX = Convert.ToBoolean(eedata.RIsD2XX); + } + } + else + { + if (pFT_EE_Read == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_EE_Read."); + } + } + return ftStatus; + } + + //************************************************************************** + // ReadFT2232HEEPROM + //************************************************************************** + // Intellisense comments + /// + /// Reads the EEPROM contents of an FT2232H device. + /// + /// FT_STATUS value from FT_EE_Read in FTD2XX DLL + /// An FT2232H_EEPROM_STRUCTURE which contains only the relevant information for an FT2232H device. + /// Thrown when the current device does not match the type required by this method. + 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) + { + Console.WriteLine("Failed to load function FT_EE_Read."); + } + } + return ftStatus; + } + + //************************************************************************** + // ReadFT4232HEEPROM + //************************************************************************** + // Intellisense comments + /// + /// Reads the EEPROM contents of an FT4232H device. + /// + /// FT_STATUS value from FT_EE_Read in FTD2XX DLL + /// An FT4232H_EEPROM_STRUCTURE which contains only the relevant information for an FT4232H device. + /// Thrown when the current device does not match the type required by this method. + public FT_STATUS ReadFT4232HEEPROM(FT4232H_EEPROM_STRUCTURE ee4232h) + { + // 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 FT4232H that we are trying to read + GetDeviceType(ref DeviceType); + if (DeviceType != FT_DEVICE.FT_DEVICE_4232H) + { + // 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 = 4; + + // 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 + ee4232h.Manufacturer = Marshal.PtrToStringAnsi(eedata.Manufacturer); + ee4232h.ManufacturerID = Marshal.PtrToStringAnsi(eedata.ManufacturerID); + ee4232h.Description = Marshal.PtrToStringAnsi(eedata.Description); + ee4232h.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 + ee4232h.VendorID = eedata.VendorID; + ee4232h.ProductID = eedata.ProductID; + ee4232h.MaxPower = eedata.MaxPower; + ee4232h.SelfPowered = Convert.ToBoolean(eedata.SelfPowered); + ee4232h.RemoteWakeup = Convert.ToBoolean(eedata.RemoteWakeup); + // 4232H specific fields + ee4232h.PullDownEnable = Convert.ToBoolean(eedata.PullDownEnable8); + ee4232h.SerNumEnable = Convert.ToBoolean(eedata.SerNumEnable8); + ee4232h.ASlowSlew = Convert.ToBoolean(eedata.ASlowSlew); + ee4232h.ASchmittInput = Convert.ToBoolean(eedata.ASchmittInput); + ee4232h.ADriveCurrent = eedata.ADriveCurrent; + ee4232h.BSlowSlew = Convert.ToBoolean(eedata.BSlowSlew); + ee4232h.BSchmittInput = Convert.ToBoolean(eedata.BSchmittInput); + ee4232h.BDriveCurrent = eedata.BDriveCurrent; + ee4232h.CSlowSlew = Convert.ToBoolean(eedata.CSlowSlew); + ee4232h.CSchmittInput = Convert.ToBoolean(eedata.CSchmittInput); + ee4232h.CDriveCurrent = eedata.CDriveCurrent; + ee4232h.DSlowSlew = Convert.ToBoolean(eedata.DSlowSlew); + ee4232h.DSchmittInput = Convert.ToBoolean(eedata.DSchmittInput); + ee4232h.DDriveCurrent = eedata.DDriveCurrent; + ee4232h.ARIIsTXDEN = Convert.ToBoolean(eedata.ARIIsTXDEN); + ee4232h.BRIIsTXDEN = Convert.ToBoolean(eedata.BRIIsTXDEN); + ee4232h.CRIIsTXDEN = Convert.ToBoolean(eedata.CRIIsTXDEN); + ee4232h.DRIIsTXDEN = Convert.ToBoolean(eedata.DRIIsTXDEN); + ee4232h.AIsVCP = Convert.ToBoolean(eedata.AIsVCP8); + ee4232h.BIsVCP = Convert.ToBoolean(eedata.BIsVCP8); + ee4232h.CIsVCP = Convert.ToBoolean(eedata.CIsVCP8); + ee4232h.DIsVCP = Convert.ToBoolean(eedata.DIsVCP8); + } + } + else + { + if (pFT_EE_Read == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_EE_Read."); + } + } + return ftStatus; + } + + //************************************************************************** + // ReadFT232HEEPROM + //************************************************************************** + // Intellisense comments + /// + /// Reads the EEPROM contents of an FT232H device. + /// + /// FT_STATUS value from FT_EE_Read in FTD2XX DLL + /// An FT232H_EEPROM_STRUCTURE which contains only the relevant information for an FT232H device. + /// Thrown when the current device does not match the type required by this method. + public FT_STATUS ReadFT232HEEPROM(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_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 FT232H that we are trying to read + GetDeviceType(ref DeviceType); + if ((DeviceType != FT_DEVICE.FT_DEVICE_232H) + && (DeviceType != FT_DEVICE.FT_DEVICE_232HP) + && (DeviceType != FT_DEVICE.FT_DEVICE_233HP)) + { + // 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 = 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); + + // Call FT_EE_Read + ftStatus = FT_EE_Read(ftHandle, eedata); + + // Retrieve string values + ee232h.Manufacturer = Marshal.PtrToStringAnsi(eedata.Manufacturer); + ee232h.ManufacturerID = Marshal.PtrToStringAnsi(eedata.ManufacturerID); + ee232h.Description = Marshal.PtrToStringAnsi(eedata.Description); + ee232h.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 + ee232h.VendorID = eedata.VendorID; + ee232h.ProductID = eedata.ProductID; + ee232h.MaxPower = eedata.MaxPower; + ee232h.SelfPowered = Convert.ToBoolean(eedata.SelfPowered); + ee232h.RemoteWakeup = Convert.ToBoolean(eedata.RemoteWakeup); + // 232H specific fields + ee232h.PullDownEnable = Convert.ToBoolean(eedata.PullDownEnableH); + ee232h.SerNumEnable = Convert.ToBoolean(eedata.SerNumEnableH); + ee232h.ACSlowSlew = Convert.ToBoolean(eedata.ACSlowSlewH); + ee232h.ACSchmittInput = Convert.ToBoolean(eedata.ACSchmittInputH); + ee232h.ACDriveCurrent = eedata.ACDriveCurrentH; + ee232h.ADSlowSlew = Convert.ToBoolean(eedata.ADSlowSlewH); + ee232h.ADSchmittInput = Convert.ToBoolean(eedata.ADSchmittInputH); + ee232h.ADDriveCurrent = eedata.ADDriveCurrentH; + ee232h.Cbus0 = eedata.Cbus0H; + ee232h.Cbus1 = eedata.Cbus1H; + ee232h.Cbus2 = eedata.Cbus2H; + ee232h.Cbus3 = eedata.Cbus3H; + ee232h.Cbus4 = eedata.Cbus4H; + ee232h.Cbus5 = eedata.Cbus5H; + ee232h.Cbus6 = eedata.Cbus6H; + ee232h.Cbus7 = eedata.Cbus7H; + ee232h.Cbus8 = eedata.Cbus8H; + ee232h.Cbus9 = eedata.Cbus9H; + ee232h.IsFifo = Convert.ToBoolean(eedata.IsFifoH); + ee232h.IsFifoTar = Convert.ToBoolean(eedata.IsFifoTarH); + ee232h.IsFastSer = Convert.ToBoolean(eedata.IsFastSerH); + ee232h.IsFT1248 = Convert.ToBoolean(eedata.IsFT1248H); + ee232h.FT1248Cpol = Convert.ToBoolean(eedata.FT1248CpolH); + ee232h.FT1248Lsb = Convert.ToBoolean(eedata.FT1248LsbH); + ee232h.FT1248FlowControl = Convert.ToBoolean(eedata.FT1248FlowControlH); + ee232h.IsVCP = Convert.ToBoolean(eedata.IsVCPH); + ee232h.PowerSaveEnable = Convert.ToBoolean(eedata.PowerSaveEnableH); + } + } + else + { + if (pFT_EE_Read == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_EE_Read."); + } + } + return ftStatus; + } + + //************************************************************************** + // ReadXSeriesEEPROM + //************************************************************************** + // Intellisense comments + /// + /// Reads the EEPROM contents of an X-Series device. + /// + /// FT_STATUS value from FT_EEPROM_Read in FTD2XX DLL + /// An FT_XSERIES_EEPROM_STRUCTURE which contains only the relevant information for an X-Series device. + /// Thrown when the current device does not match the type required by this method. + 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) + { + Console.WriteLine("Failed to load function FT_EE_Read."); + } + } + return ftStatus; + } + + //************************************************************************** + // WriteFT232BEEPROM + //************************************************************************** + // Intellisense comments + /// + /// Writes the specified values to the EEPROM of an FT232B or FT245B device. + /// + /// FT_STATUS value from FT_EE_Program in FTD2XX DLL + /// The EEPROM settings to be written to the device + /// If the strings are too long, they will be truncated to their maximum permitted lengths + /// Thrown when the current device does not match the type required by this method. + public FT_STATUS WriteFT232BEEPROM(FT232B_EEPROM_STRUCTURE ee232b) + { + // 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 FT232B or FT245B that we are trying to write + GetDeviceType(ref DeviceType); + if (DeviceType != FT_DEVICE.FT_DEVICE_BM) + { + // If it is not, throw an exception + ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE; + ErrorHandler(ftStatus, ftErrorCondition); + } + + // Check for VID and PID of 0x0000 + if ((ee232b.VendorID == 0x0000) | (ee232b.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 = 2; + + // 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 (ee232b.Manufacturer.Length > 32) + ee232b.Manufacturer = ee232b.Manufacturer.Substring(0, 32); + if (ee232b.ManufacturerID.Length > 16) + ee232b.ManufacturerID = ee232b.ManufacturerID.Substring(0, 16); + if (ee232b.Description.Length > 64) + ee232b.Description = ee232b.Description.Substring(0, 64); + if (ee232b.SerialNumber.Length > 16) + ee232b.SerialNumber = ee232b.SerialNumber.Substring(0, 16); + + // Set string values + eedata.Manufacturer = Marshal.StringToHGlobalAnsi(ee232b.Manufacturer); + eedata.ManufacturerID = Marshal.StringToHGlobalAnsi(ee232b.ManufacturerID); + eedata.Description = Marshal.StringToHGlobalAnsi(ee232b.Description); + eedata.SerialNumber = Marshal.StringToHGlobalAnsi(ee232b.SerialNumber); + + // Map non-string elements to structure + // Standard elements + eedata.VendorID = ee232b.VendorID; + eedata.ProductID = ee232b.ProductID; + eedata.MaxPower = ee232b.MaxPower; + eedata.SelfPowered = Convert.ToUInt16(ee232b.SelfPowered); + eedata.RemoteWakeup = Convert.ToUInt16(ee232b.RemoteWakeup); + // B specific fields + eedata.Rev4 = Convert.ToByte(true); + eedata.PullDownEnable = Convert.ToByte(ee232b.PullDownEnable); + eedata.SerNumEnable = Convert.ToByte(ee232b.SerNumEnable); + eedata.USBVersionEnable = Convert.ToByte(ee232b.USBVersionEnable); + eedata.USBVersion = ee232b.USBVersion; + + // 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) + { + Console.WriteLine("Failed to load function FT_EE_Program."); + } + } + return ftStatus; + } + + //************************************************************************** + // WriteFT2232EEPROM + //************************************************************************** + // Intellisense comments + /// + /// Writes the specified values to the EEPROM of an FT2232 device. + /// Calls FT_EE_Program in FTD2XX DLL + /// + /// FT_STATUS value from FT_EE_Program in FTD2XX DLL + /// The EEPROM settings to be written to the device + /// If the strings are too long, they will be truncated to their maximum permitted lengths + /// Thrown when the current device does not match the type required by this method. + public FT_STATUS WriteFT2232EEPROM(FT2232_EEPROM_STRUCTURE ee2232) + { + // 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 FT2232 that we are trying to write + GetDeviceType(ref DeviceType); + if (DeviceType != FT_DEVICE.FT_DEVICE_2232) + { + // If it is not, throw an exception + ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE; + ErrorHandler(ftStatus, ftErrorCondition); + } + + // Check for VID and PID of 0x0000 + if ((ee2232.VendorID == 0x0000) | (ee2232.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 = 2; + + // 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 (ee2232.Manufacturer.Length > 32) + ee2232.Manufacturer = ee2232.Manufacturer.Substring(0, 32); + if (ee2232.ManufacturerID.Length > 16) + ee2232.ManufacturerID = ee2232.ManufacturerID.Substring(0, 16); + if (ee2232.Description.Length > 64) + ee2232.Description = ee2232.Description.Substring(0, 64); + if (ee2232.SerialNumber.Length > 16) + ee2232.SerialNumber = ee2232.SerialNumber.Substring(0, 16); + + // Set string values + eedata.Manufacturer = Marshal.StringToHGlobalAnsi(ee2232.Manufacturer); + eedata.ManufacturerID = Marshal.StringToHGlobalAnsi(ee2232.ManufacturerID); + eedata.Description = Marshal.StringToHGlobalAnsi(ee2232.Description); + eedata.SerialNumber = Marshal.StringToHGlobalAnsi(ee2232.SerialNumber); + + // Map non-string elements to structure + // Standard elements + eedata.VendorID = ee2232.VendorID; + eedata.ProductID = ee2232.ProductID; + eedata.MaxPower = ee2232.MaxPower; + eedata.SelfPowered = Convert.ToUInt16(ee2232.SelfPowered); + eedata.RemoteWakeup = Convert.ToUInt16(ee2232.RemoteWakeup); + // 2232 specific fields + eedata.Rev5 = Convert.ToByte(true); + eedata.PullDownEnable5 = Convert.ToByte(ee2232.PullDownEnable); + eedata.SerNumEnable5 = Convert.ToByte(ee2232.SerNumEnable); + eedata.USBVersionEnable5 = Convert.ToByte(ee2232.USBVersionEnable); + eedata.USBVersion5 = ee2232.USBVersion; + eedata.AIsHighCurrent = Convert.ToByte(ee2232.AIsHighCurrent); + eedata.BIsHighCurrent = Convert.ToByte(ee2232.BIsHighCurrent); + eedata.IFAIsFifo = Convert.ToByte(ee2232.IFAIsFifo); + eedata.IFAIsFifoTar = Convert.ToByte(ee2232.IFAIsFifoTar); + eedata.IFAIsFastSer = Convert.ToByte(ee2232.IFAIsFastSer); + eedata.AIsVCP = Convert.ToByte(ee2232.AIsVCP); + eedata.IFBIsFifo = Convert.ToByte(ee2232.IFBIsFifo); + eedata.IFBIsFifoTar = Convert.ToByte(ee2232.IFBIsFifoTar); + eedata.IFBIsFastSer = Convert.ToByte(ee2232.IFBIsFastSer); + eedata.BIsVCP = Convert.ToByte(ee2232.BIsVCP); + + // 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) + { + Console.WriteLine("Failed to load function FT_EE_Program."); + } + } + return ftStatus; + } + + //************************************************************************** + // WriteFT232REEPROM + //************************************************************************** + // Intellisense comments + /// + /// Writes the specified values to the EEPROM of an FT232R or FT245R device. + /// Calls FT_EE_Program in FTD2XX DLL + /// + /// FT_STATUS value from FT_EE_Program in FTD2XX DLL + /// The EEPROM settings to be written to the device + /// If the strings are too long, they will be truncated to their maximum permitted lengths + /// Thrown when the current device does not match the type required by this method. + public FT_STATUS WriteFT232REEPROM(FT232R_EEPROM_STRUCTURE ee232r) + { + // 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 FT232R or FT245R that we are trying to write + GetDeviceType(ref DeviceType); + if (DeviceType != FT_DEVICE.FT_DEVICE_232R) + { + // If it is not, throw an exception + ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE; + ErrorHandler(ftStatus, ftErrorCondition); + } + + // Check for VID and PID of 0x0000 + if ((ee232r.VendorID == 0x0000) | (ee232r.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 = 2; + + // 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 (ee232r.Manufacturer.Length > 32) + ee232r.Manufacturer = ee232r.Manufacturer.Substring(0, 32); + if (ee232r.ManufacturerID.Length > 16) + ee232r.ManufacturerID = ee232r.ManufacturerID.Substring(0, 16); + if (ee232r.Description.Length > 64) + ee232r.Description = ee232r.Description.Substring(0, 64); + if (ee232r.SerialNumber.Length > 16) + ee232r.SerialNumber = ee232r.SerialNumber.Substring(0, 16); + + // Set string values + eedata.Manufacturer = Marshal.StringToHGlobalAnsi(ee232r.Manufacturer); + eedata.ManufacturerID = Marshal.StringToHGlobalAnsi(ee232r.ManufacturerID); + eedata.Description = Marshal.StringToHGlobalAnsi(ee232r.Description); + eedata.SerialNumber = Marshal.StringToHGlobalAnsi(ee232r.SerialNumber); + + // Map non-string elements to structure + // Standard elements + eedata.VendorID = ee232r.VendorID; + eedata.ProductID = ee232r.ProductID; + eedata.MaxPower = ee232r.MaxPower; + eedata.SelfPowered = Convert.ToUInt16(ee232r.SelfPowered); + eedata.RemoteWakeup = Convert.ToUInt16(ee232r.RemoteWakeup); + // 232R specific fields + eedata.PullDownEnableR = Convert.ToByte(ee232r.PullDownEnable); + eedata.SerNumEnableR = Convert.ToByte(ee232r.SerNumEnable); + eedata.UseExtOsc = Convert.ToByte(ee232r.UseExtOsc); + eedata.HighDriveIOs = Convert.ToByte(ee232r.HighDriveIOs); + // Override any endpoint size the user has selected and force 64 bytes + // Some users have been known to wreck devices by setting 0 here... + eedata.EndpointSize = 64; + eedata.PullDownEnableR = Convert.ToByte(ee232r.PullDownEnable); + eedata.SerNumEnableR = Convert.ToByte(ee232r.SerNumEnable); + eedata.InvertTXD = Convert.ToByte(ee232r.InvertTXD); + eedata.InvertRXD = Convert.ToByte(ee232r.InvertRXD); + eedata.InvertRTS = Convert.ToByte(ee232r.InvertRTS); + eedata.InvertCTS = Convert.ToByte(ee232r.InvertCTS); + eedata.InvertDTR = Convert.ToByte(ee232r.InvertDTR); + eedata.InvertDSR = Convert.ToByte(ee232r.InvertDSR); + eedata.InvertDCD = Convert.ToByte(ee232r.InvertDCD); + eedata.InvertRI = Convert.ToByte(ee232r.InvertRI); + eedata.Cbus0 = ee232r.Cbus0; + eedata.Cbus1 = ee232r.Cbus1; + eedata.Cbus2 = ee232r.Cbus2; + eedata.Cbus3 = ee232r.Cbus3; + eedata.Cbus4 = ee232r.Cbus4; + eedata.RIsD2XX = Convert.ToByte(ee232r.RIsD2XX); + + // 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) + { + Console.WriteLine("Failed to load function FT_EE_Program."); + } + } + return ftStatus; + } + + //************************************************************************** + // WriteFT2232HEEPROM + //************************************************************************** + // Intellisense comments + /// + /// Writes the specified values to the EEPROM of an FT2232H device. + /// Calls FT_EE_Program in FTD2XX DLL + /// + /// FT_STATUS value from FT_EE_Program in FTD2XX DLL + /// The EEPROM settings to be written to the device + /// If the strings are too long, they will be truncated to their maximum permitted lengths + /// Thrown when the current device does not match the type required by this method. + 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) + { + Console.WriteLine("Failed to load function FT_EE_Program."); + } + } + return ftStatus; + } + + //************************************************************************** + // WriteFT4232HEEPROM + //************************************************************************** + // Intellisense comments + /// + /// Writes the specified values to the EEPROM of an FT4232H device. + /// Calls FT_EE_Program in FTD2XX DLL + /// + /// FT_STATUS value from FT_EE_Program in FTD2XX DLL + /// The EEPROM settings to be written to the device + /// If the strings are too long, they will be truncated to their maximum permitted lengths + /// Thrown when the current device does not match the type required by this method. + public FT_STATUS WriteFT4232HEEPROM(FT4232H_EEPROM_STRUCTURE ee4232h) + { + // 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 FT4232H that we are trying to write + GetDeviceType(ref DeviceType); + if (DeviceType != FT_DEVICE.FT_DEVICE_4232H) + { + // If it is not, throw an exception + ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE; + ErrorHandler(ftStatus, ftErrorCondition); + } + + // Check for VID and PID of 0x0000 + if ((ee4232h.VendorID == 0x0000) | (ee4232h.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 = 4; + + // 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 (ee4232h.Manufacturer.Length > 32) + ee4232h.Manufacturer = ee4232h.Manufacturer.Substring(0, 32); + if (ee4232h.ManufacturerID.Length > 16) + ee4232h.ManufacturerID = ee4232h.ManufacturerID.Substring(0, 16); + if (ee4232h.Description.Length > 64) + ee4232h.Description = ee4232h.Description.Substring(0, 64); + if (ee4232h.SerialNumber.Length > 16) + ee4232h.SerialNumber = ee4232h.SerialNumber.Substring(0, 16); + + // Set string values + eedata.Manufacturer = Marshal.StringToHGlobalAnsi(ee4232h.Manufacturer); + eedata.ManufacturerID = Marshal.StringToHGlobalAnsi(ee4232h.ManufacturerID); + eedata.Description = Marshal.StringToHGlobalAnsi(ee4232h.Description); + eedata.SerialNumber = Marshal.StringToHGlobalAnsi(ee4232h.SerialNumber); + + // Map non-string elements to structure + // Standard elements + eedata.VendorID = ee4232h.VendorID; + eedata.ProductID = ee4232h.ProductID; + eedata.MaxPower = ee4232h.MaxPower; + eedata.SelfPowered = Convert.ToUInt16(ee4232h.SelfPowered); + eedata.RemoteWakeup = Convert.ToUInt16(ee4232h.RemoteWakeup); + // 4232H specific fields + eedata.PullDownEnable8 = Convert.ToByte(ee4232h.PullDownEnable); + eedata.SerNumEnable8 = Convert.ToByte(ee4232h.SerNumEnable); + eedata.ASlowSlew = Convert.ToByte(ee4232h.ASlowSlew); + eedata.ASchmittInput = Convert.ToByte(ee4232h.ASchmittInput); + eedata.ADriveCurrent = ee4232h.ADriveCurrent; + eedata.BSlowSlew = Convert.ToByte(ee4232h.BSlowSlew); + eedata.BSchmittInput = Convert.ToByte(ee4232h.BSchmittInput); + eedata.BDriveCurrent = ee4232h.BDriveCurrent; + eedata.CSlowSlew = Convert.ToByte(ee4232h.CSlowSlew); + eedata.CSchmittInput = Convert.ToByte(ee4232h.CSchmittInput); + eedata.CDriveCurrent = ee4232h.CDriveCurrent; + eedata.DSlowSlew = Convert.ToByte(ee4232h.DSlowSlew); + eedata.DSchmittInput = Convert.ToByte(ee4232h.DSchmittInput); + eedata.DDriveCurrent = ee4232h.DDriveCurrent; + eedata.ARIIsTXDEN = Convert.ToByte(ee4232h.ARIIsTXDEN); + eedata.BRIIsTXDEN = Convert.ToByte(ee4232h.BRIIsTXDEN); + eedata.CRIIsTXDEN = Convert.ToByte(ee4232h.CRIIsTXDEN); + eedata.DRIIsTXDEN = Convert.ToByte(ee4232h.DRIIsTXDEN); + eedata.AIsVCP8 = Convert.ToByte(ee4232h.AIsVCP); + eedata.BIsVCP8 = Convert.ToByte(ee4232h.BIsVCP); + eedata.CIsVCP8 = Convert.ToByte(ee4232h.CIsVCP); + eedata.DIsVCP8 = Convert.ToByte(ee4232h.DIsVCP); + + // 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) + { + Console.WriteLine("Failed to load function FT_EE_Program."); + } + } + return ftStatus; + } + + //************************************************************************** + // WriteFT232HEEPROM + //************************************************************************** + // Intellisense comments + /// + /// Writes the specified values to the EEPROM of an FT232H device. + /// Calls FT_EE_Program in FTD2XX DLL + /// + /// FT_STATUS value from FT_EE_Program in FTD2XX DLL + /// The EEPROM settings to be written to the device + /// If the strings are too long, they will be truncated to their maximum permitted lengths + /// Thrown when the current device does not match the type required by this method. + 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) + { + Console.WriteLine("Failed to load function FT_EE_Program."); + } + } + return ftStatus; + } + + //************************************************************************** + // WriteXSeriesEEPROM + //************************************************************************** + // Intellisense comments + /// + /// Writes the specified values to the EEPROM of an X-Series device. + /// Calls FT_EEPROM_Program in FTD2XX DLL + /// + /// FT_STATUS value from FT_EEPROM_Program in FTD2XX DLL + /// The EEPROM settings to be written to the device + /// If the strings are too long, they will be truncated to their maximum permitted lengths + /// Thrown when the current device does not match the type required by this method. + 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 ftStatus; + } + + //************************************************************************** + // EEReadUserArea + //************************************************************************** + // Intellisense comments + /// + /// Reads data from the user area of the device EEPROM. + /// + /// FT_STATUS from FT_UARead in FTD2XX.DLL + /// An array of bytes which will be populated with the data read from the device EEPROM user area. + /// The number of bytes actually read from the EEPROM user area. + public FT_STATUS EEReadUserArea(byte[] UserAreaDataBuffer, ref uint numBytesRead) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_UASize != IntPtr.Zero) & (pFT_EE_UARead != IntPtr.Zero)) + { + tFT_EE_UASize FT_EE_UASize = (tFT_EE_UASize)Marshal.GetDelegateForFunctionPointer(pFT_EE_UASize, typeof(tFT_EE_UASize)); + tFT_EE_UARead FT_EE_UARead = (tFT_EE_UARead)Marshal.GetDelegateForFunctionPointer(pFT_EE_UARead, typeof(tFT_EE_UARead)); + + if (ftHandle != IntPtr.Zero) + { + uint UASize = 0; + // Get size of user area to allocate an array of the correct size. + // The application must also get the UA size for its copy + ftStatus = FT_EE_UASize(ftHandle, ref UASize); + + // Make sure we have enough storage for the whole user area + if (UserAreaDataBuffer.Length >= UASize) + { + // Call FT_EE_UARead + ftStatus = FT_EE_UARead(ftHandle, UserAreaDataBuffer, UserAreaDataBuffer.Length, ref numBytesRead); + } + } + } + else + { + if (pFT_EE_UASize == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_EE_UASize."); + } + if (pFT_EE_UARead == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_EE_UARead."); + } + } + return ftStatus; + } + + //************************************************************************** + // EEWriteUserArea + //************************************************************************** + // Intellisense comments + /// + /// Writes data to the user area of the device EEPROM. + /// + /// FT_STATUS value from FT_UAWrite in FTD2XX.DLL + /// An array of bytes which will be written to the device EEPROM user area. + public FT_STATUS EEWriteUserArea(byte[] UserAreaDataBuffer) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_UASize != IntPtr.Zero) & (pFT_EE_UAWrite != IntPtr.Zero)) + { + tFT_EE_UASize FT_EE_UASize = (tFT_EE_UASize)Marshal.GetDelegateForFunctionPointer(pFT_EE_UASize, typeof(tFT_EE_UASize)); + tFT_EE_UAWrite FT_EE_UAWrite = (tFT_EE_UAWrite)Marshal.GetDelegateForFunctionPointer(pFT_EE_UAWrite, typeof(tFT_EE_UAWrite)); + + if (ftHandle != IntPtr.Zero) + { + uint UASize = 0; + // Get size of user area to allocate an array of the correct size. + // The application must also get the UA size for its copy + ftStatus = FT_EE_UASize(ftHandle, ref UASize); + + // Make sure we have enough storage for all the data in the EEPROM + if (UserAreaDataBuffer.Length <= UASize) + { + // Call FT_EE_UAWrite + ftStatus = FT_EE_UAWrite(ftHandle, UserAreaDataBuffer, UserAreaDataBuffer.Length); + } + } + } + else + { + if (pFT_EE_UASize == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_EE_UASize."); + } + if (pFT_EE_UAWrite == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_EE_UAWrite."); + } + } + return ftStatus; + } + + //************************************************************************** + // GetDeviceType + //************************************************************************** + // Intellisense comments + /// + /// Gets the chip type of the current device. + /// + /// FT_STATUS value from FT_GetDeviceInfo in FTD2XX.DLL + /// The FTDI chip type of the current device. + public FT_STATUS GetDeviceType(ref FT_DEVICE DeviceType) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_GetDeviceInfo != IntPtr.Zero) + { + tFT_GetDeviceInfo FT_GetDeviceInfo = (tFT_GetDeviceInfo)Marshal.GetDelegateForFunctionPointer(pFT_GetDeviceInfo, typeof(tFT_GetDeviceInfo)); + + uint DeviceID = 0; + byte[] sernum = new byte[16]; + byte[] desc = new byte[64]; + + DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN; + + if (ftHandle != IntPtr.Zero) + { + // Call FT_GetDeviceInfo + ftStatus = FT_GetDeviceInfo(ftHandle, ref DeviceType, ref DeviceID, sernum, desc, IntPtr.Zero); + } + } + else + { + if (pFT_GetDeviceInfo == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_GetDeviceInfo."); + } + } + return ftStatus; + } + + //************************************************************************** + // GetDeviceID + //************************************************************************** + // Intellisense comments + /// + /// Gets the Vendor ID and Product ID of the current device. + /// + /// FT_STATUS value from FT_GetDeviceInfo in FTD2XX.DLL + /// The device ID (Vendor ID and Product ID) of the current device. + public FT_STATUS GetDeviceID(ref uint DeviceID) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_GetDeviceInfo != IntPtr.Zero) + { + tFT_GetDeviceInfo FT_GetDeviceInfo = (tFT_GetDeviceInfo)Marshal.GetDelegateForFunctionPointer(pFT_GetDeviceInfo, typeof(tFT_GetDeviceInfo)); + + FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN; + byte[] sernum = new byte[16]; + byte[] desc = new byte[64]; + + if (ftHandle != IntPtr.Zero) + { + // Call FT_GetDeviceInfo + ftStatus = FT_GetDeviceInfo(ftHandle, ref DeviceType, ref DeviceID, sernum, desc, IntPtr.Zero); + } + } + else + { + if (pFT_GetDeviceInfo == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_GetDeviceInfo."); + } + } + return ftStatus; + } + + //************************************************************************** + // GetDescription + //************************************************************************** + // Intellisense comments + /// + /// Gets the description of the current device. + /// + /// FT_STATUS value from FT_GetDeviceInfo in FTD2XX.DLL + /// The description of the current device. + public FT_STATUS GetDescription(out string Description) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR; + + Description = String.Empty; + + // 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_GetDeviceInfo != IntPtr.Zero) + { + tFT_GetDeviceInfo FT_GetDeviceInfo = (tFT_GetDeviceInfo)Marshal.GetDelegateForFunctionPointer(pFT_GetDeviceInfo, typeof(tFT_GetDeviceInfo)); + + uint DeviceID = 0; + FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN; + byte[] sernum = new byte[16]; + byte[] desc = new byte[64]; + + if (ftHandle != IntPtr.Zero) + { + // Call FT_GetDeviceInfo + ftStatus = FT_GetDeviceInfo(ftHandle, ref DeviceType, ref DeviceID, sernum, desc, IntPtr.Zero); + Description = Encoding.ASCII.GetString(desc); + Description = Description.Substring(0, Description.IndexOf('\0')); + } + } + else + { + if (pFT_GetDeviceInfo == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_GetDeviceInfo."); + } + } + return ftStatus; + } + + //************************************************************************** + // GetSerialNumber + //************************************************************************** + // Intellisense comments + /// + /// Gets the serial number of the current device. + /// + /// FT_STATUS value from FT_GetDeviceInfo in FTD2XX.DLL + /// The serial number of the current device. + public FT_STATUS GetSerialNumber(out string SerialNumber) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR; + + SerialNumber = String.Empty; + + // 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_GetDeviceInfo != IntPtr.Zero) + { + tFT_GetDeviceInfo FT_GetDeviceInfo = (tFT_GetDeviceInfo)Marshal.GetDelegateForFunctionPointer(pFT_GetDeviceInfo, typeof(tFT_GetDeviceInfo)); + + uint DeviceID = 0; + FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN; + byte[] sernum = new byte[16]; + byte[] desc = new byte[64]; + + if (ftHandle != IntPtr.Zero) + { + // Call FT_GetDeviceInfo + ftStatus = FT_GetDeviceInfo(ftHandle, ref DeviceType, ref DeviceID, sernum, desc, IntPtr.Zero); + SerialNumber = Encoding.ASCII.GetString(sernum); + SerialNumber = SerialNumber.Substring(0, SerialNumber.IndexOf('\0')); + } + } + else + { + if (pFT_GetDeviceInfo == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_GetDeviceInfo."); + } + } + return ftStatus; + } + + //************************************************************************** + // GetRxBytesAvailable + //************************************************************************** + // Intellisense comments + /// + /// Gets the number of bytes available in the receive buffer. + /// + /// FT_STATUS value from FT_GetQueueStatus in FTD2XX.DLL + /// The number of bytes available to be read. + public FT_STATUS GetRxBytesAvailable(ref uint RxQueue) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_GetQueueStatus != IntPtr.Zero) + { + tFT_GetQueueStatus FT_GetQueueStatus = (tFT_GetQueueStatus)Marshal.GetDelegateForFunctionPointer(pFT_GetQueueStatus, typeof(tFT_GetQueueStatus)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_GetQueueStatus + ftStatus = FT_GetQueueStatus(ftHandle, ref RxQueue); + } + } + else + { + if (pFT_GetQueueStatus == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_GetQueueStatus."); + } + } + return ftStatus; + } + + //************************************************************************** + // GetTxBytesWaiting + //************************************************************************** + // Intellisense comments + /// + /// Gets the number of bytes waiting in the transmit buffer. + /// + /// FT_STATUS value from FT_GetStatus in FTD2XX.DLL + /// The number of bytes waiting to be sent. + public FT_STATUS GetTxBytesWaiting(ref uint TxQueue) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_GetStatus != IntPtr.Zero) + { + tFT_GetStatus FT_GetStatus = (tFT_GetStatus)Marshal.GetDelegateForFunctionPointer(pFT_GetStatus, typeof(tFT_GetStatus)); + + uint RxQueue = 0; + uint EventStatus = 0; + + if (ftHandle != IntPtr.Zero) + { + // Call FT_GetStatus + ftStatus = FT_GetStatus(ftHandle, ref RxQueue, ref TxQueue, ref EventStatus); + } + } + else + { + if (pFT_GetStatus == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_GetStatus."); + } + } + return ftStatus; + } + + //************************************************************************** + // GetEventType + //************************************************************************** + // Intellisense comments + /// + /// Gets the event type after an event has fired. Can be used to distinguish which event has been triggered when waiting on multiple event types. + /// + /// FT_STATUS value from FT_GetStatus in FTD2XX.DLL + /// The type of event that has occurred. + public FT_STATUS GetEventType(ref uint EventType) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_GetStatus != IntPtr.Zero) + { + tFT_GetStatus FT_GetStatus = (tFT_GetStatus)Marshal.GetDelegateForFunctionPointer(pFT_GetStatus, typeof(tFT_GetStatus)); + + uint RxQueue = 0; + uint TxQueue = 0; + + if (ftHandle != IntPtr.Zero) + { + // Call FT_GetStatus + ftStatus = FT_GetStatus(ftHandle, ref RxQueue, ref TxQueue, ref EventType); + } + } + else + { + if (pFT_GetStatus == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_GetStatus."); + } + } + return ftStatus; + } + + //************************************************************************** + // GetModemStatus + //************************************************************************** + // Intellisense comments + /// + /// Gets the current modem status. + /// + /// FT_STATUS value from FT_GetModemStatus in FTD2XX.DLL + /// A bit map representaion of the current modem status. + public FT_STATUS GetModemStatus(ref byte ModemStatus) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_GetModemStatus != IntPtr.Zero) + { + tFT_GetModemStatus FT_GetModemStatus = (tFT_GetModemStatus)Marshal.GetDelegateForFunctionPointer(pFT_GetModemStatus, typeof(tFT_GetModemStatus)); + + uint ModemLineStatus = 0; + + if (ftHandle != IntPtr.Zero) + { + // Call FT_GetModemStatus + ftStatus = FT_GetModemStatus(ftHandle, ref ModemLineStatus); + + } + ModemStatus = Convert.ToByte(ModemLineStatus & 0x000000FF); + } + else + { + if (pFT_GetModemStatus == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_GetModemStatus."); + } + } + return ftStatus; + } + + //************************************************************************** + // GetLineStatus + //************************************************************************** + // Intellisense comments + /// + /// Gets the current line status. + /// + /// FT_STATUS value from FT_GetModemStatus in FTD2XX.DLL + /// A bit map representaion of the current line status. + public FT_STATUS GetLineStatus(ref byte LineStatus) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_GetModemStatus != IntPtr.Zero) + { + tFT_GetModemStatus FT_GetModemStatus = (tFT_GetModemStatus)Marshal.GetDelegateForFunctionPointer(pFT_GetModemStatus, typeof(tFT_GetModemStatus)); + + uint ModemLineStatus = 0; + + if (ftHandle != IntPtr.Zero) + { + // Call FT_GetModemStatus + ftStatus = FT_GetModemStatus(ftHandle, ref ModemLineStatus); + } + LineStatus = Convert.ToByte((ModemLineStatus >> 8) & 0x000000FF); + } + else + { + if (pFT_GetModemStatus == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_GetModemStatus."); + } + } + return ftStatus; + } + + //************************************************************************** + // SetBaudRate + //************************************************************************** + // Intellisense comments + /// + /// Sets the current Baud rate. + /// + /// FT_STATUS value from FT_SetBaudRate in FTD2XX.DLL + /// The desired Baud rate for the device. + public FT_STATUS SetBaudRate(uint BaudRate) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetBaudRate != IntPtr.Zero) + { + tFT_SetBaudRate FT_SetBaudRate = (tFT_SetBaudRate)Marshal.GetDelegateForFunctionPointer(pFT_SetBaudRate, typeof(tFT_SetBaudRate)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_SetBaudRate + ftStatus = FT_SetBaudRate(ftHandle, BaudRate); + } + } + else + { + if (pFT_SetBaudRate == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetBaudRate."); + } + } + return ftStatus; + } + + //************************************************************************** + // SetDataCharacteristics + //************************************************************************** + // Intellisense comments + /// + /// Sets the data bits, stop bits and parity for the device. + /// + /// FT_STATUS value from FT_SetDataCharacteristics in FTD2XX.DLL + /// The number of data bits for UART data. Valid values are FT_DATA_BITS.FT_DATA_7 or FT_DATA_BITS.FT_BITS_8 + /// The number of stop bits for UART data. Valid values are FT_STOP_BITS.FT_STOP_BITS_1 or FT_STOP_BITS.FT_STOP_BITS_2 + /// The parity of the UART data. Valid values are FT_PARITY.FT_PARITY_NONE, FT_PARITY.FT_PARITY_ODD, FT_PARITY.FT_PARITY_EVEN, FT_PARITY.FT_PARITY_MARK or FT_PARITY.FT_PARITY_SPACE + public FT_STATUS SetDataCharacteristics(byte DataBits, byte StopBits, byte Parity) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetDataCharacteristics != IntPtr.Zero) + { + tFT_SetDataCharacteristics FT_SetDataCharacteristics = (tFT_SetDataCharacteristics)Marshal.GetDelegateForFunctionPointer(pFT_SetDataCharacteristics, typeof(tFT_SetDataCharacteristics)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_SetDataCharacteristics + ftStatus = FT_SetDataCharacteristics(ftHandle, DataBits, StopBits, Parity); + } + } + else + { + if (pFT_SetDataCharacteristics == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetDataCharacteristics."); + } + } + return ftStatus; + } + + //************************************************************************** + // SetFlowControl + //************************************************************************** + // Intellisense comments + /// + /// Sets the flow control type. + /// + /// FT_STATUS value from FT_SetFlowControl in FTD2XX.DLL + /// The type of flow control for the UART. Valid values are FT_FLOW_CONTROL.FT_FLOW_NONE, FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, FT_FLOW_CONTROL.FT_FLOW_DTR_DSR or FT_FLOW_CONTROL.FT_FLOW_XON_XOFF + /// The Xon character for Xon/Xoff flow control. Ignored if not using Xon/XOff flow control. + /// The Xoff character for Xon/Xoff flow control. Ignored if not using Xon/XOff flow control. + public FT_STATUS SetFlowControl(UInt16 FlowControl, byte Xon, byte Xoff) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetFlowControl != IntPtr.Zero) + { + tFT_SetFlowControl FT_SetFlowControl = (tFT_SetFlowControl)Marshal.GetDelegateForFunctionPointer(pFT_SetFlowControl, typeof(tFT_SetFlowControl)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_SetFlowControl + ftStatus = FT_SetFlowControl(ftHandle, FlowControl, Xon, Xoff); + } + } + else + { + if (pFT_SetFlowControl == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetFlowControl."); + } + } + return ftStatus; + } + + //************************************************************************** + // SetRTS + //************************************************************************** + // Intellisense comments + /// + /// Asserts or de-asserts the Request To Send (RTS) line. + /// + /// FT_STATUS value from FT_SetRts or FT_ClrRts in FTD2XX.DLL + /// If true, asserts RTS. If false, de-asserts RTS + public FT_STATUS SetRTS(bool Enable) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetRts != IntPtr.Zero) & (pFT_ClrRts != IntPtr.Zero)) + { + tFT_SetRts FT_SetRts = (tFT_SetRts)Marshal.GetDelegateForFunctionPointer(pFT_SetRts, typeof(tFT_SetRts)); + tFT_ClrRts FT_ClrRts = (tFT_ClrRts)Marshal.GetDelegateForFunctionPointer(pFT_ClrRts, typeof(tFT_ClrRts)); + + if (ftHandle != IntPtr.Zero) + { + if (Enable) + { + // Call FT_SetRts + ftStatus = FT_SetRts(ftHandle); + } + else + { + // Call FT_ClrRts + ftStatus = FT_ClrRts(ftHandle); + } + } + } + else + { + if (pFT_SetRts == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetRts."); + } + if (pFT_ClrRts == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_ClrRts."); + } + } + return ftStatus; + } + + //************************************************************************** + // SetDTR + //************************************************************************** + // Intellisense comments + /// + /// Asserts or de-asserts the Data Terminal Ready (DTR) line. + /// + /// FT_STATUS value from FT_SetDtr or FT_ClrDtr in FTD2XX.DLL + /// If true, asserts DTR. If false, de-asserts DTR. + public FT_STATUS SetDTR(bool Enable) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetDtr != IntPtr.Zero) & (pFT_ClrDtr != IntPtr.Zero)) + { + tFT_SetDtr FT_SetDtr = (tFT_SetDtr)Marshal.GetDelegateForFunctionPointer(pFT_SetDtr, typeof(tFT_SetDtr)); + tFT_ClrDtr FT_ClrDtr = (tFT_ClrDtr)Marshal.GetDelegateForFunctionPointer(pFT_ClrDtr, typeof(tFT_ClrDtr)); + + if (ftHandle != IntPtr.Zero) + { + if (Enable) + { + // Call FT_SetDtr + ftStatus = FT_SetDtr(ftHandle); + } + else + { + // Call FT_ClrDtr + ftStatus = FT_ClrDtr(ftHandle); + } + } + } + else + { + if (pFT_SetDtr == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetDtr."); + } + if (pFT_ClrDtr == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_ClrDtr."); + } + } + return ftStatus; + } + + //************************************************************************** + // SetTimeouts + //************************************************************************** + // Intellisense comments + /// + /// Sets the read and write timeout values. + /// + /// FT_STATUS value from FT_SetTimeouts in FTD2XX.DLL + /// Read timeout value in ms. A value of 0 indicates an infinite timeout. + /// Write timeout value in ms. A value of 0 indicates an infinite timeout. + public FT_STATUS SetTimeouts(uint ReadTimeout, uint WriteTimeout) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetTimeouts != IntPtr.Zero) + { + tFT_SetTimeouts FT_SetTimeouts = (tFT_SetTimeouts)Marshal.GetDelegateForFunctionPointer(pFT_SetTimeouts, typeof(tFT_SetTimeouts)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_SetTimeouts + ftStatus = FT_SetTimeouts(ftHandle, ReadTimeout, WriteTimeout); + } + } + else + { + if (pFT_SetTimeouts == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetTimeouts."); + } + } + return ftStatus; + } + + //************************************************************************** + // SetBreak + //************************************************************************** + // Intellisense comments + /// + /// Sets or clears the break state. + /// + /// FT_STATUS value from FT_SetBreakOn or FT_SetBreakOff in FTD2XX.DLL + /// If true, sets break on. If false, sets break off. + public FT_STATUS SetBreak(bool Enable) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetBreakOn != IntPtr.Zero) & (pFT_SetBreakOff != IntPtr.Zero)) + { + tFT_SetBreakOn FT_SetBreakOn = (tFT_SetBreakOn)Marshal.GetDelegateForFunctionPointer(pFT_SetBreakOn, typeof(tFT_SetBreakOn)); + tFT_SetBreakOff FT_SetBreakOff = (tFT_SetBreakOff)Marshal.GetDelegateForFunctionPointer(pFT_SetBreakOff, typeof(tFT_SetBreakOff)); + + if (ftHandle != IntPtr.Zero) + { + if (Enable) + { + // Call FT_SetBreakOn + ftStatus = FT_SetBreakOn(ftHandle); + } + else + { + // Call FT_SetBreakOff + ftStatus = FT_SetBreakOff(ftHandle); + } + } + } + else + { + if (pFT_SetBreakOn == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetBreakOn."); + } + if (pFT_SetBreakOff == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetBreakOff."); + } + } + return ftStatus; + } + + //************************************************************************** + // SetResetPipeRetryCount + //************************************************************************** + // Intellisense comments + /// + /// Gets or sets the reset pipe retry count. Default value is 50. + /// + /// FT_STATUS vlaue from FT_SetResetPipeRetryCount in FTD2XX.DLL + /// The reset pipe retry count. + /// Electrically noisy environments may benefit from a larger value. + public FT_STATUS SetResetPipeRetryCount(uint ResetPipeRetryCount) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetResetPipeRetryCount != IntPtr.Zero) + { + tFT_SetResetPipeRetryCount FT_SetResetPipeRetryCount = (tFT_SetResetPipeRetryCount)Marshal.GetDelegateForFunctionPointer(pFT_SetResetPipeRetryCount, typeof(tFT_SetResetPipeRetryCount)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_SetResetPipeRetryCount + ftStatus = FT_SetResetPipeRetryCount(ftHandle, ResetPipeRetryCount); + } + } + else + { + if (pFT_SetResetPipeRetryCount == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetResetPipeRetryCount."); + } + } + return ftStatus; + } + + //************************************************************************** + // GetDriverVersion + //************************************************************************** + // Intellisense comments + /// + /// Gets the current FTDIBUS.SYS driver version number. + /// + /// FT_STATUS value from FT_GetDriverVersion in FTD2XX.DLL + /// The current driver version number. + public FT_STATUS GetDriverVersion(ref uint DriverVersion) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_GetDriverVersion != IntPtr.Zero) + { + tFT_GetDriverVersion FT_GetDriverVersion = (tFT_GetDriverVersion)Marshal.GetDelegateForFunctionPointer(pFT_GetDriverVersion, typeof(tFT_GetDriverVersion)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_GetDriverVersion + ftStatus = FT_GetDriverVersion(ftHandle, ref DriverVersion); + } + } + else + { + if (pFT_GetDriverVersion == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_GetDriverVersion."); + } + } + return ftStatus; + } + + //************************************************************************** + // GetLibraryVersion + //************************************************************************** + // Intellisense comments + /// + /// Gets the current FTD2XX.DLL driver version number. + /// + /// FT_STATUS value from FT_GetLibraryVersion in FTD2XX.DLL + /// The current library version. + public FT_STATUS GetLibraryVersion(ref uint LibraryVersion) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_GetLibraryVersion != IntPtr.Zero) + { + tFT_GetLibraryVersion FT_GetLibraryVersion = (tFT_GetLibraryVersion)Marshal.GetDelegateForFunctionPointer(pFT_GetLibraryVersion, typeof(tFT_GetLibraryVersion)); + + // Call FT_GetLibraryVersion + ftStatus = FT_GetLibraryVersion(ref LibraryVersion); + } + else + { + if (pFT_GetLibraryVersion == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_GetLibraryVersion."); + } + } + return ftStatus; + } + + //************************************************************************** + // SetDeadmanTimeout + //************************************************************************** + // Intellisense comments + /// + /// Sets the USB deadman timeout value. Default is 5000ms. + /// + /// FT_STATUS value from FT_SetDeadmanTimeout in FTD2XX.DLL + /// The deadman timeout value in ms. Default is 5000ms. + public FT_STATUS SetDeadmanTimeout(uint DeadmanTimeout) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetDeadmanTimeout != IntPtr.Zero) + { + tFT_SetDeadmanTimeout FT_SetDeadmanTimeout = (tFT_SetDeadmanTimeout)Marshal.GetDelegateForFunctionPointer(pFT_SetDeadmanTimeout, typeof(tFT_SetDeadmanTimeout)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_SetDeadmanTimeout + ftStatus = FT_SetDeadmanTimeout(ftHandle, DeadmanTimeout); + } + } + else + { + if (pFT_SetDeadmanTimeout == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetDeadmanTimeout."); + } + } + return ftStatus; + } + + //************************************************************************** + // SetLatency + //************************************************************************** + // Intellisense comments + /// + /// Sets the value of the latency timer. Default value is 16ms. + /// + /// FT_STATUS value from FT_SetLatencyTimer in FTD2XX.DLL + /// The latency timer value in ms. + /// Valid values are 2ms - 255ms for FT232BM, FT245BM and FT2232 devices. + /// Valid values are 0ms - 255ms for other devices. + public FT_STATUS SetLatency(byte Latency) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetLatencyTimer != IntPtr.Zero) + { + tFT_SetLatencyTimer FT_SetLatencyTimer = (tFT_SetLatencyTimer)Marshal.GetDelegateForFunctionPointer(pFT_SetLatencyTimer, typeof(tFT_SetLatencyTimer)); + + if (ftHandle != IntPtr.Zero) + { + FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN; + // Set Bit Mode does not apply to FT8U232AM, FT8U245AM or FT8U100AX devices + GetDeviceType(ref DeviceType); + if ((DeviceType == FT_DEVICE.FT_DEVICE_BM) || (DeviceType == FT_DEVICE.FT_DEVICE_2232)) + { + // Do not allow latency of 1ms or 0ms for older devices + // since this can cause problems/lock up due to buffering mechanism + if (Latency < 2) + Latency = 2; + } + + // Call FT_SetLatencyTimer + ftStatus = FT_SetLatencyTimer(ftHandle, Latency); + } + } + else + { + if (pFT_SetLatencyTimer == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetLatencyTimer."); + } + } + return ftStatus; + } + + //************************************************************************** + // GetLatency + //************************************************************************** + // Intellisense comments + /// + /// Gets the value of the latency timer. Default value is 16ms. + /// + /// FT_STATUS value from FT_GetLatencyTimer in FTD2XX.DLL + /// The latency timer value in ms. + public FT_STATUS GetLatency(ref byte Latency) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_GetLatencyTimer != IntPtr.Zero) + { + tFT_GetLatencyTimer FT_GetLatencyTimer = (tFT_GetLatencyTimer)Marshal.GetDelegateForFunctionPointer(pFT_GetLatencyTimer, typeof(tFT_GetLatencyTimer)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_GetLatencyTimer + ftStatus = FT_GetLatencyTimer(ftHandle, ref Latency); + } + } + else + { + if (pFT_GetLatencyTimer == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_GetLatencyTimer."); + } + } + return ftStatus; + } + + //************************************************************************** + // SetUSBTransferSizes + //************************************************************************** + // Intellisense comments + /// + /// Sets the USB IN and OUT transfer sizes. + /// + /// FT_STATUS value from FT_SetUSBParameters in FTD2XX.DLL + /// The USB IN transfer size in bytes. + public FT_STATUS InTransferSize(uint InTransferSize) + // Only support IN transfer sizes at the moment + //public uint InTransferSize(uint InTransferSize, uint OutTransferSize) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetUSBParameters != IntPtr.Zero) + { + tFT_SetUSBParameters FT_SetUSBParameters = (tFT_SetUSBParameters)Marshal.GetDelegateForFunctionPointer(pFT_SetUSBParameters, typeof(tFT_SetUSBParameters)); + + uint OutTransferSize = InTransferSize; + + if (ftHandle != IntPtr.Zero) + { + // Call FT_SetUSBParameters + ftStatus = FT_SetUSBParameters(ftHandle, InTransferSize, OutTransferSize); + } + } + else + { + if (pFT_SetUSBParameters == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetUSBParameters."); + } + } + return ftStatus; + } + + //************************************************************************** + // SetCharacters + //************************************************************************** + // Intellisense comments + /// + /// Sets an event character, an error character and enables or disables them. + /// + /// FT_STATUS value from FT_SetChars in FTD2XX.DLL + /// A character that will be tigger an IN to the host when this character is received. + /// Determines if the EventChar is enabled or disabled. + /// A character that will be inserted into the data stream to indicate that an error has occurred. + /// Determines if the ErrorChar is enabled or disabled. + public FT_STATUS SetCharacters(byte EventChar, bool EventCharEnable, byte ErrorChar, bool ErrorCharEnable) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetChars != IntPtr.Zero) + { + tFT_SetChars FT_SetChars = (tFT_SetChars)Marshal.GetDelegateForFunctionPointer(pFT_SetChars, typeof(tFT_SetChars)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_SetChars + ftStatus = FT_SetChars(ftHandle, EventChar, Convert.ToByte(EventCharEnable), ErrorChar, Convert.ToByte(ErrorCharEnable)); + } + } + else + { + if (pFT_SetChars == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_SetChars."); + } + } + return ftStatus; + } + + //************************************************************************** + // GetEEUserAreaSize + //************************************************************************** + // Intellisense comments + /// + /// Gets the size of the EEPROM user area. + /// + /// FT_STATUS value from FT_EE_UASize in FTD2XX.DLL + /// The EEPROM user area size in bytes. + public FT_STATUS EEUserAreaSize(ref uint UASize) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_UASize != IntPtr.Zero) + { + tFT_EE_UASize FT_EE_UASize = (tFT_EE_UASize)Marshal.GetDelegateForFunctionPointer(pFT_EE_UASize, typeof(tFT_EE_UASize)); + + if (ftHandle != IntPtr.Zero) + { + ftStatus = FT_EE_UASize(ftHandle, ref UASize); + } + } + else + { + if (pFT_EE_UASize == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_EE_UASize."); + } + } + return ftStatus; + } + + //************************************************************************** + // GetCOMPort + //************************************************************************** + // Intellisense comments + /// + /// Gets the corresponding COM port number for the current device. If no COM port is exposed, an empty string is returned. + /// + /// FT_STATUS value from FT_GetComPortNumber in FTD2XX.DLL + /// The COM port name corresponding to the current device. If no COM port is installed, an empty string is passed back. + public FT_STATUS GetCOMPort(out string ComPortName) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR; + + // As ComPortName is an OUT paremeter, has to be assigned before returning + ComPortName = string.Empty; + + // 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_GetComPortNumber != IntPtr.Zero) + { + tFT_GetComPortNumber FT_GetComPortNumber = (tFT_GetComPortNumber)Marshal.GetDelegateForFunctionPointer(pFT_GetComPortNumber, typeof(tFT_GetComPortNumber)); + + Int32 ComPortNumber = -1; + if (ftHandle != IntPtr.Zero) + { + // Call FT_GetComPortNumber + ftStatus = FT_GetComPortNumber(ftHandle, ref ComPortNumber); + } + + if (ComPortNumber == -1) + { + // If no COM port installed, return an empty string + ComPortName = string.Empty; + } + else + { + // If installed, return full COM string + // This can then be passed to an instance of the SerialPort class to assign the port number. + ComPortName = "COM" + ComPortNumber.ToString(); + } + } + else + { + if (pFT_GetComPortNumber == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_GetComPortNumber."); + } + } + return ftStatus; + } + + + //************************************************************************** + // VendorCmdGet + //************************************************************************** + // Intellisense comments + /// + /// Get data from the FT4222 using the vendor command interface. + /// + /// FT_STATUS value from FT_VendorCmdSet in FTD2XX.DLL + public FT_STATUS VendorCmdGet(UInt16 request, byte[] buf, UInt16 len) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_VendorCmdGet != IntPtr.Zero) + { + tFT_VendorCmdGet FT_VendorCmdGet = (tFT_VendorCmdGet)Marshal.GetDelegateForFunctionPointer(pFT_VendorCmdGet, typeof(tFT_VendorCmdGet)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_VendorCmdGet + ftStatus = FT_VendorCmdGet(ftHandle, request, buf, len); + } + } + else + { + if (pFT_VendorCmdGet == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_VendorCmdGet."); + } + } + return ftStatus; + } + + //************************************************************************** + // VendorCmdSet + //************************************************************************** + // Intellisense comments + /// + /// Set data from the FT4222 using the vendor command interface. + /// + /// FT_STATUS value from FT_VendorCmdSet in FTD2XX.DLL + public FT_STATUS VendorCmdSet(UInt16 request, byte[] buf, UInt16 len) + { + // Initialise ftStatus to something other than FT_OK + FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_VendorCmdSet != IntPtr.Zero) + { + tFT_VendorCmdSet FT_VendorCmdSet = (tFT_VendorCmdSet)Marshal.GetDelegateForFunctionPointer(pFT_VendorCmdSet, typeof(tFT_VendorCmdSet)); + + if (ftHandle != IntPtr.Zero) + { + // Call FT_VendorCmdSet + ftStatus = FT_VendorCmdSet(ftHandle, request, buf, len); + } + } + else + { + if (pFT_VendorCmdSet == IntPtr.Zero) + { + Console.WriteLine("Failed to load function FT_VendorCmdSet."); + } + } + return ftStatus; + } + + //************************************************************************** + // IsOpen + //************************************************************************** + // Intellisense comments + /// + /// Gets the open status of the device. + /// + public bool IsOpen + { + get + { + if (ftHandle == IntPtr.Zero) + return false; + else + return true; + } + } + + //************************************************************************** + // InterfaceIdentifier + //************************************************************************** + // Intellisense comments + /// + /// Gets the interface identifier. + /// + private string InterfaceIdentifier + { + get + { + string Identifier; + Identifier = String.Empty; + if (IsOpen) + { + FT_DEVICE deviceType = FT_DEVICE.FT_DEVICE_BM; + GetDeviceType(ref deviceType); + if (deviceType == FTDI.FT_DEVICE.FT_DEVICE_2232H || + deviceType == FTDI.FT_DEVICE.FT_DEVICE_4232H || + deviceType == FTDI.FT_DEVICE.FT_DEVICE_2233HP || + deviceType == FTDI.FT_DEVICE.FT_DEVICE_4233HP || + deviceType == FTDI.FT_DEVICE.FT_DEVICE_2232HP || + deviceType == FTDI.FT_DEVICE.FT_DEVICE_4232HP || + deviceType == FTDI.FT_DEVICE.FT_DEVICE_2232HA || + deviceType == FTDI.FT_DEVICE.FT_DEVICE_4232HA || + deviceType == FTDI.FT_DEVICE.FT_DEVICE_2232) + { + string Description; + GetDescription(out Description); + Identifier = Description.Substring((Description.Length - 1)); + return Identifier; + } + } + return Identifier; + } + } + + //************************************************************************** + // ErrorHandler + //************************************************************************** + /// + /// Method to check ftStatus and ftErrorCondition values for error conditions and throw exceptions accordingly. + /// + private void ErrorHandler(FT_STATUS ftStatus, FT_ERROR ftErrorCondition) + { + if (ftStatus != FT_STATUS.FT_OK) + { + // Check FT_STATUS values returned from FTD2XX DLL calls + switch (ftStatus) + { + case FT_STATUS.FT_DEVICE_NOT_FOUND: + { + throw new FT_EXCEPTION("FTDI device not found."); + } + case FT_STATUS.FT_DEVICE_NOT_OPENED: + { + throw new FT_EXCEPTION("FTDI device not opened."); + } + case FT_STATUS.FT_DEVICE_NOT_OPENED_FOR_ERASE: + { + throw new FT_EXCEPTION("FTDI device not opened for erase."); + } + case FT_STATUS.FT_DEVICE_NOT_OPENED_FOR_WRITE: + { + throw new FT_EXCEPTION("FTDI device not opened for write."); + } + case FT_STATUS.FT_EEPROM_ERASE_FAILED: + { + throw new FT_EXCEPTION("Failed to erase FTDI device EEPROM."); + } + case FT_STATUS.FT_EEPROM_NOT_PRESENT: + { + throw new FT_EXCEPTION("No EEPROM fitted to FTDI device."); + } + case FT_STATUS.FT_EEPROM_NOT_PROGRAMMED: + { + throw new FT_EXCEPTION("FTDI device EEPROM not programmed."); + } + case FT_STATUS.FT_EEPROM_READ_FAILED: + { + throw new FT_EXCEPTION("Failed to read FTDI device EEPROM."); + } + case FT_STATUS.FT_EEPROM_WRITE_FAILED: + { + throw new FT_EXCEPTION("Failed to write FTDI device EEPROM."); + } + case FT_STATUS.FT_FAILED_TO_WRITE_DEVICE: + { + throw new FT_EXCEPTION("Failed to write to FTDI device."); + } + case FT_STATUS.FT_INSUFFICIENT_RESOURCES: + { + throw new FT_EXCEPTION("Insufficient resources."); + } + case FT_STATUS.FT_INVALID_ARGS: + { + throw new FT_EXCEPTION("Invalid arguments for FTD2XX function call."); + } + case FT_STATUS.FT_INVALID_BAUD_RATE: + { + throw new FT_EXCEPTION("Invalid Baud rate for FTDI device."); + } + case FT_STATUS.FT_INVALID_HANDLE: + { + throw new FT_EXCEPTION("Invalid handle for FTDI device."); + } + case FT_STATUS.FT_INVALID_PARAMETER: + { + throw new FT_EXCEPTION("Invalid parameter for FTD2XX function call."); + } + case FT_STATUS.FT_IO_ERROR: + { + throw new FT_EXCEPTION("FTDI device IO error."); + } + case FT_STATUS.FT_OTHER_ERROR: + { + throw new FT_EXCEPTION("An unexpected error has occurred when trying to communicate with the FTDI device."); + } + default: + break; + } + } + if (ftErrorCondition != FT_ERROR.FT_NO_ERROR) + { + // Check for other error conditions not handled by FTD2XX DLL + switch (ftErrorCondition) + { + case FT_ERROR.FT_INCORRECT_DEVICE: + { + throw new FT_EXCEPTION("The current device type does not match the EEPROM structure."); + } + case FT_ERROR.FT_INVALID_BITMODE: + { + throw new FT_EXCEPTION("The requested bit mode is not valid for the current device."); + } + case FT_ERROR.FT_BUFFER_SIZE: + { + throw new FT_EXCEPTION("The supplied buffer is not big enough."); + } + + default: + break; + } + + } + + return; + } +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232Collection.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232Collection.cs new file mode 100644 index 0000000000..8d528cf324 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232Collection.cs @@ -0,0 +1,140 @@ +using Meadow.Hardware; +using System; +using System.Collections; +using System.Collections.Generic; + +namespace Meadow.Foundation.ICs.IOExpanders; + +public class Ft232h : FtdiExpander +{ + internal Ft232h(FTDI.FT_DEVICE_INFO_NODE info, FTDI native) + : base(info, native) + { + } + + public override II2cBus CreateI2cBus(int busNumber = 1, I2cBusSpeed busSpeed = I2cBusSpeed.Standard) + { + throw new NotImplementedException(); + } + + public override II2cBus CreateI2cBus(IPin[] pins, I2cBusSpeed busSpeed) + { + throw new NotImplementedException(); + } + + public override II2cBus CreateI2cBus(IPin clock, IPin data, I2cBusSpeed busSpeed) + { + throw new NotImplementedException(); + } +} + +public abstract class FtdiExpander : +// IDisposable, +// IDigitalInputOutputController, +// IDigitalOutputController, +// ISpiController, + II2cController +{ + public abstract II2cBus CreateI2cBus(int busNumber = 1, I2cBusSpeed busSpeed = I2cBusSpeed.Standard); + public abstract II2cBus CreateI2cBus(IPin[] pins, I2cBusSpeed busSpeed); + public abstract II2cBus CreateI2cBus(IPin clock, IPin data, I2cBusSpeed busSpeed); + + internal static FtdiExpander From(FTDI.FT_DEVICE_INFO_NODE info, FTDI native) + { + return info.Type switch + { + FTDI.FT_DEVICE.FT_DEVICE_232H => new Ft232h(info, native), + _ => throw new NotSupportedException(), + }; + } + + private FTDI.FT_DEVICE_INFO_NODE _infoNode; + private FTDI _native; + + public string SerialNumber => _infoNode.SerialNumber; + + internal FtdiExpander(FTDI.FT_DEVICE_INFO_NODE info, FTDI native) + { + _infoNode = info; + _native = native; + } +} + +public class Ft232Collection : IEnumerable +{ + private FTDI _native; + + private static Ft232Collection? _instance; + + private List _list = new List(); + + /// + /// Gets the number of FtdiExpander devices connected to the host machine. + /// + public int Count => _list.Count; + + /// + /// Gets the FtdiExpander device at the specified index in the collection. + /// + /// The index of the FtdiExpander device to retrieve. + public FtdiExpander this[int index] => _list[index]; + + private Ft232Collection() + { + _native = new FTDI(); + } + + private void CheckStatus(FTDI.FT_STATUS status) + { + if (status == FTDI.FT_STATUS.FT_OK) return; + + throw new Exception($"ftd2xx.dll returned '{status}'"); + } + + public void Refresh() + { + uint count = 0; + + CheckStatus(_native.GetNumberOfDevices(ref count)); + + _list.Clear(); + + if (count > 0) + { + var infos = new FTDI.FT_DEVICE_INFO_NODE[count]; + CheckStatus(_native.GetDeviceList(infos)); + + foreach (var info in infos) + { + _list.Add(FtdiExpander.From(info, _native)); + } + } + } + + /// + public IEnumerator GetEnumerator() + { + return _list.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + /// + /// Gets the singleton instance of Ft232Collection, initializing it if necessary. + /// + public static Ft232Collection Devices + { + get + { + if (_instance == null) + { + _instance = new Ft232Collection(); + _instance.Refresh(); + } + return _instance; + } + } +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232SpiBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232SpiBus.cs index 0dfeab01b4..c0e179cc18 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232SpiBus.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232SpiBus.cs @@ -2,7 +2,7 @@ using Meadow.Units; using System; using System.Runtime.InteropServices; -using static Meadow.Foundation.ICs.IOExpanders.Ft232h; +using static Meadow.Foundation.ICs.IOExpanders.Ft232h_old; using static Meadow.Foundation.ICs.IOExpanders.Native; namespace Meadow.Foundation.ICs.IOExpanders; @@ -153,7 +153,7 @@ private void CloseChannel() private SpiChannelConfig CreateChannelConfig(SpiClockConfiguration config) { // for now we support CS on D3 and that's it - Ft232h.SpiConfigOptions opts = SpiConfigOptions.CS_ACTIVELOW | SpiConfigOptions.CS_DBUS3; + Ft232h_old.SpiConfigOptions opts = SpiConfigOptions.CS_ACTIVELOW | SpiConfigOptions.CS_DBUS3; switch (config.SpiMode) { diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.PinDefinitions.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.PinDefinitions.cs index 7839cec451..bbcc82d5b1 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.PinDefinitions.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.PinDefinitions.cs @@ -7,7 +7,7 @@ namespace Meadow.Foundation.ICs.IOExpanders /// /// Represents the pin definitions for the Ft232h IC. /// - public partial class Ft232h + public partial class Ft232h_old { /// /// Defines the pin definitions for the Ft232h IC. @@ -37,7 +37,7 @@ public class PinDefinitions : IPinDefinitions /// Creates a new PinDefinitions object. /// /// The Ft232h controller associated with the pins. - internal PinDefinitions(Ft232h controller) + internal PinDefinitions(Ft232h_old controller) { UseMpsseKeys = controller.UsingMpsse; Controller = controller; diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.cs index b80a3f53f2..c7997ae5b8 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.cs @@ -5,14 +5,14 @@ namespace Meadow.Foundation.ICs.IOExpanders; /// -/// Represents a DS3502 digital potentiometer +/// Represents an FT232 IO Expander /// -public partial class Ft232h : - IDisposable, - IDigitalInputOutputController, - IDigitalOutputController, - ISpiController, - II2cController +public partial class Ft232h_old : +IDisposable, +IDigitalInputOutputController, +IDigitalOutputController, +ISpiController, +II2cController { private bool _isDisposed; private IFtdiImpl _impl; @@ -25,10 +25,10 @@ public partial class Ft232h : public PinDefinitions Pins { get; } /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// Specifies whether to use Multi-Protocol Synchronous Serial Engine (MPSSE) mode (default is false). - public Ft232h(bool useMPSSE = false) + public Ft232h_old(bool useMPSSE = false) { UsingMpsse = useMPSSE; @@ -145,7 +145,7 @@ protected virtual void Dispose(bool disposing) /// /// Finalizer for the Ft232h class, used to release unmanaged resources. /// - ~Ft232h() + ~Ft232h_old() { Dispose(false); } diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.mpsse.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.mpsse.cs index a69cf63072..f779130d0c 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.mpsse.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.mpsse.cs @@ -137,7 +137,7 @@ public IDigitalInputPort CreateDigitalInputPort(int channel, IPin pin, ResistorM if (_activeBus == null) { - var bus = CreateSpiBus(channel, Ft232h.DefaultClockConfiguration); + var bus = CreateSpiBus(channel, Ft232h_old.DefaultClockConfiguration); _spiBusAutoCreated = true; _activeBus = bus as IFt232Bus; } @@ -156,7 +156,7 @@ public IDigitalOutputPort CreateDigitalOutputPort(int channel, IPin pin, bool in if (_activeBus == null) { - var bus = CreateSpiBus(channel, Ft232h.DefaultClockConfiguration); + var bus = CreateSpiBus(channel, Ft232h_old.DefaultClockConfiguration); _spiBusAutoCreated = true; _activeBus = bus as IFt232Bus; } diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Native.Mpsse.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Native.Mpsse.cs index 01f0c95108..a17b5960b5 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Native.Mpsse.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Native.Mpsse.cs @@ -1,6 +1,6 @@ using System; using System.Runtime.InteropServices; -using static Meadow.Foundation.ICs.IOExpanders.Ft232h; +using static Meadow.Foundation.ICs.IOExpanders.Ft232h_old; namespace Meadow.Foundation.ICs.IOExpanders { diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Native.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Native.cs index b928a2ebe6..f6757a9ab8 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Native.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Native.cs @@ -259,7 +259,7 @@ specific to the protocol that are implemented in the chip */ public uint ClockRate; public byte LatencyTimer; - public Ft232h.SpiConfigOptions Options; + public Ft232h_old.SpiConfigOptions Options; public uint Pin; public ushort CurrentPinState; } diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SPITransferOptions.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SPITransferOptions.cs index 8e1055700f..32d6d29a20 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SPITransferOptions.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SPITransferOptions.cs @@ -2,7 +2,7 @@ namespace Meadow.Foundation.ICs.IOExpanders { - public partial class Ft232h + public partial class Ft232h_old { [Flags] internal enum SPITransferOptions diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SpiConfigOptions.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SpiConfigOptions.cs index f60142ae8e..0341701622 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SpiConfigOptions.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SpiConfigOptions.cs @@ -2,7 +2,7 @@ namespace Meadow.Foundation.ICs.IOExpanders { - public partial class Ft232h + public partial class Ft232h_old { /// /// Flags for configuring the SPI communication options. diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs index 14eaaffede..b4ab5c9ba0 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs @@ -8,13 +8,17 @@ Console.WriteLine("HELLO FROM THE WILDERNESS FT232H DRIVER!"); -var ft232 = new Ft232h(); +Ft232Collection.Devices.Refresh(); +var count = Ft232Collection.Devices.Count(); +var ft232 = Ft232Collection.Devices[0]; + +var ft232h = new Ft232h_old(); //await TestBME280(ft232); -await TestIli9341(ft232); +await TestIli9341(ft232h); //await TestGpio(ft232); -async Task TestBME280(Ft232h expander) +async Task TestBME280(Ft232h_old expander) { var bme = new Bme280(expander.CreateI2cBus()); @@ -26,7 +30,7 @@ async Task TestBME280(Ft232h expander) } } -async Task TestGpio(Ft232h expander) +async Task TestGpio(Ft232h_old expander) { var outputs = new List { @@ -56,7 +60,7 @@ async Task TestGpio(Ft232h expander) } } -async Task TestIli9341(Ft232h expander) +async Task TestIli9341(Ft232h_old expander) { var ili = new Ili9341( expander.CreateSpiBus(), From 1e03e7c793da34912c69480e38bc727bd19ed0f2 Mon Sep 17 00:00:00 2001 From: Chris Tacke Date: Mon, 19 Feb 2024 09:53:28 -0600 Subject: [PATCH 02/11] checkpoint --- .../Driver/Controls/Circle.cs | 93 ++++++++++++ .../Samples/Ili9488Charts_Sample/MeadowApp.cs | 4 +- .../Samples/MAX7219_Sample/MeadowApp.cs | 2 +- .../FTDI.FT2232H_EEPROM_STRUCTURE.cs | 0 .../FTDI.FT2232_EEPROM_STRUCTURE.cs | 0 .../FTDI.FT232B_EEPROM_STRUCTURE.cs | 0 .../FTDI.FT232H_EEPROM_STRUCTURE.cs | 0 .../FTDI.FT232R_EEPROM_STRUCTURE.cs | 0 .../FTDI.FT4232H_EEPROM_STRUCTURE.cs | 0 .../FTDI.FT_232H_CBUS_OPTIONS.cs | 0 .../{FTD2XX => FTDI}/FTDI.FT_BIT_MODES.cs | 0 .../{FTD2XX => FTDI}/FTDI.FT_CBUS_OPTIONS.cs | 0 .../{FTD2XX => FTDI}/FTDI.FT_DATA_BITS.cs | 0 .../Driver/{FTD2XX => FTDI}/FTDI.FT_DEVICE.cs | 0 .../FTDI.FT_DEVICE_INFO_NODE.cs | 0 .../{FTD2XX => FTDI}/FTDI.FT_DRIVE_CURRENT.cs | 0 .../{FTD2XX => FTDI}/FTDI.FT_EEPROM_DATA.cs | 0 .../{FTD2XX => FTDI}/FTDI.FT_EEPROM_HEADER.cs | 0 .../Driver/{FTD2XX => FTDI}/FTDI.FT_ERROR.cs | 0 .../Driver/{FTD2XX => FTDI}/FTDI.FT_EVENTS.cs | 0 .../{FTD2XX => FTDI}/FTDI.FT_EXCEPTION.cs | 0 .../Driver/{FTD2XX => FTDI}/FTDI.FT_FLAGS.cs | 0 .../{FTD2XX => FTDI}/FTDI.FT_FLOW_CONTROL.cs | 0 .../{FTD2XX => FTDI}/FTDI.FT_LINE_STATUS.cs | 0 .../{FTD2XX => FTDI}/FTDI.FT_MODEM_STATUS.cs | 0 .../Driver/FTDI/FTDI.FT_OPCODE.cs | 91 ++++++++++++ .../Driver/{FTD2XX => FTDI}/FTDI.FT_PARITY.cs | 0 .../{FTD2XX => FTDI}/FTDI.FT_PROGRAM_DATA.cs | 0 .../Driver/{FTD2XX => FTDI}/FTDI.FT_PURGE.cs | 0 .../Driver/{FTD2XX => FTDI}/FTDI.FT_STATUS.cs | 0 .../{FTD2XX => FTDI}/FTDI.FT_STOP_BITS.cs | 0 .../FTDI.FT_XSERIES_CBUS_OPTIONS.cs | 0 .../{FTD2XX => FTDI}/FTDI.FT_XSERIES_DATA.cs | 0 .../FTDI.FT_XSERIES_EEPROM_STRUCTURE.cs | 0 .../Driver/{FTD2XX => FTDI}/FTDI.cs | 0 .../Driver/Ft232Collection.cs | 140 ------------------ .../Driver/Ft232h.PinDefinitions.cs | 4 +- .../ICs.IOExpanders.Ft232h/Driver/Ft232h.cs | 20 +-- .../{ => Ft23xx}/Ft23xxDigitalInputPort.cs | 4 +- .../{ => Ft23xx}/Ft23xxDigitalOutputPort.cs | 4 +- .../Driver/{ => Ft23xx}/Ft23xxI2cBus.cs | 4 +- .../Driver/{ => Ft23xx}/Ft23xxSpiBus.cs | 4 +- .../Ftd2xxImpl.cs} | 4 +- .../Driver/{ => Ft23xx}/Native.Ftd2xx.cs | 0 .../Driver/FtdiExpander.cs | 97 ++++++++++++ .../Driver/FtdiExpanderCollection.cs | 84 +++++++++++ .../Driver/ICs.IOExpanders.Ft232h.csproj | 4 + .../MpsseChannel.Gpio.cs} | 2 +- .../MpsseChannel.I2c.cs} | 2 +- .../MpsseChannel.Spi.cs} | 2 +- .../{FtdiDevice.cs => MPSSE/MpsseChannel.cs} | 4 +- .../MpsseChannelCollection.cs} | 13 +- .../MpsseDigitalOutputPort.cs} | 0 .../{Ft232h.mpsse.cs => MPSSE/MpsseImpl.cs} | 12 +- .../{Ft232SpiBus.cs => MPSSE/MpsseSpiBus.cs} | 10 +- .../Driver/{ => MPSSE}/Native.Mpsse.cs | 2 +- .../ICs.IOExpanders.Ft232h/Driver/Native.cs | 2 +- .../Driver/SPITransferOptions.cs | 2 +- .../Driver/SpiConfigOptions.cs | 2 +- .../Samples/Ft232h_Windows_Sample/Program.cs | 14 +- .../Arducam_Sample - Backup.csproj | 26 ++++ 61 files changed, 453 insertions(+), 199 deletions(-) create mode 100644 Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroLayout/Driver/Controls/Circle.cs rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT2232H_EEPROM_STRUCTURE.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT2232_EEPROM_STRUCTURE.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT232B_EEPROM_STRUCTURE.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT232H_EEPROM_STRUCTURE.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT232R_EEPROM_STRUCTURE.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT4232H_EEPROM_STRUCTURE.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_232H_CBUS_OPTIONS.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_BIT_MODES.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_CBUS_OPTIONS.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_DATA_BITS.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_DEVICE.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_DEVICE_INFO_NODE.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_DRIVE_CURRENT.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_EEPROM_DATA.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_EEPROM_HEADER.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_ERROR.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_EVENTS.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_EXCEPTION.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_FLAGS.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_FLOW_CONTROL.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_LINE_STATUS.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_MODEM_STATUS.cs (100%) create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_OPCODE.cs rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_PARITY.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_PROGRAM_DATA.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_PURGE.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_STATUS.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_STOP_BITS.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_XSERIES_CBUS_OPTIONS.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_XSERIES_DATA.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.FT_XSERIES_EEPROM_STRUCTURE.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FTD2XX => FTDI}/FTDI.cs (100%) delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232Collection.cs rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{ => Ft23xx}/Ft23xxDigitalInputPort.cs (87%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{ => Ft23xx}/Ft23xxDigitalOutputPort.cs (87%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{ => Ft23xx}/Ft23xxI2cBus.cs (96%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{ => Ft23xx}/Ft23xxSpiBus.cs (94%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{Ft232h.ft23xx.cs => Ft23xx/Ftd2xxImpl.cs} (94%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{ => Ft23xx}/Native.Ftd2xx.cs (100%) create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpanderCollection.cs rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FtdiDevice.Gpio.cs => MPSSE/MpsseChannel.Gpio.cs} (98%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FtdiDevice.I2c.cs => MPSSE/MpsseChannel.I2c.cs} (99%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FtdiDevice.Spi.cs => MPSSE/MpsseChannel.Spi.cs} (99%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FtdiDevice.cs => MPSSE/MpsseChannel.cs} (98%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{FtdiDeviceCollection.cs => MPSSE/MpsseChannelCollection.cs} (81%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{Ft232DigitalOutputPort.cs => MPSSE/MpsseDigitalOutputPort.cs} (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{Ft232h.mpsse.cs => MPSSE/MpsseImpl.cs} (92%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{Ft232SpiBus.cs => MPSSE/MpsseSpiBus.cs} (96%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{ => MPSSE}/Native.Mpsse.cs (99%) create mode 100644 Source/Meadow.Foundation.Peripherals/Sensors.Camera.Arducam/Samples/Arducam_Sample/Arducam_Sample - Backup.csproj diff --git a/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroLayout/Driver/Controls/Circle.cs b/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroLayout/Driver/Controls/Circle.cs new file mode 100644 index 0000000000..dff34de8e5 --- /dev/null +++ b/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroLayout/Driver/Controls/Circle.cs @@ -0,0 +1,93 @@ +namespace Meadow.Foundation.Graphics.MicroLayout; + +public struct Coordinate2D +{ + public int X { get; set; } + public int Y { get; set; } +} + +/// +/// Represents a circle in the user interface. +/// +public class Circle : ThemedControl +{ + private Color _foreColor; + + /// + /// Gets or sets a value indicating whether the Circle is filled with the foreground color. + /// + public bool IsFilled { get; set; } = true; + + /// + /// Initializes a new instance of the class with the specified dimensions. + /// + /// The X coordinate of the circles's center. + /// The Y coordinate of the circles's center. + /// The radius of the circle. + public Circle(int centerX, int centerY, int radius) + : base(centerX - radius, centerY - radius, radius * 2, radius * 2) + { + } + + /// + /// Initializes a new instance of the class with the specified dimensions. + /// + /// The coordinate of the circles's center. + /// The radius of the circle. + public Circle(Coordinate2D center, int radius) + : base(center.X - radius, center.Y - radius, radius * 2, radius * 2) + { + } + + /// + /// Applies the specified display theme to the Circle. + /// + /// The display theme to apply. + public override void ApplyTheme(DisplayTheme theme) + { + if (theme != null) + { + if (theme.ForegroundColor != null) this.ForeColor = theme.ForegroundColor.Value; + } + } + + /// + /// Gets or sets the foreground color of the Circle. + /// + public Color ForeColor + { + get => _foreColor; + set => SetInvalidatingProperty(ref _foreColor, value); + } + + /// + /// Gets or sets the foreground color of the Circle. + /// + public int Radius + { + get => Width / 2; + set + { + // keep centered + var coeff = (value > Radius) ? -1 : 1; + var offset = value - Radius; + + Width = value * 2; + } + } + + /// + /// Draws the Circle on the specified surface. + /// + /// The surface to draw the Circle on. + protected override void OnDraw(MicroGraphics graphics) + { + if (ForeColor != Color.Transparent) + { + var radius = (Right - Left) / 2; + var centerX = Left + radius; + var centerY = Top + radius; + graphics.DrawCircle(centerX, centerY, radius, ForeColor, IsFilled); + } + } +} diff --git a/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroLayout/Samples/Ili9488Charts_Sample/MeadowApp.cs b/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroLayout/Samples/Ili9488Charts_Sample/MeadowApp.cs index 711322119c..99638a38f1 100644 --- a/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroLayout/Samples/Ili9488Charts_Sample/MeadowApp.cs +++ b/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroLayout/Samples/Ili9488Charts_Sample/MeadowApp.cs @@ -7,12 +7,12 @@ public class MeadowApp : App { - private Ft232h_old expander = new Ft232h_old(); + private Ft232h expander = new Ft232h(); private DisplayScreen? screen; public override Task Initialize() { - expander = new Ft232h_old(); + expander = new Ft232h(); var display = new Ili9341 ( diff --git a/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroLayout/Samples/MAX7219_Sample/MeadowApp.cs b/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroLayout/Samples/MAX7219_Sample/MeadowApp.cs index 9011992428..1a177067c8 100644 --- a/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroLayout/Samples/MAX7219_Sample/MeadowApp.cs +++ b/Source/Meadow.Foundation.Libraries_and_Frameworks/Graphics.MicroLayout/Samples/MAX7219_Sample/MeadowApp.cs @@ -6,7 +6,7 @@ public class MeadowApp : App { - private readonly Ft232h_old expander = new Ft232h_old(); + private readonly Ft232h expander = new Ft232h(); private DisplayScreen? screen; public override Task Initialize() diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT2232H_EEPROM_STRUCTURE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT2232H_EEPROM_STRUCTURE.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT2232H_EEPROM_STRUCTURE.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT2232H_EEPROM_STRUCTURE.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT2232_EEPROM_STRUCTURE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT2232_EEPROM_STRUCTURE.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT2232_EEPROM_STRUCTURE.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT2232_EEPROM_STRUCTURE.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT232B_EEPROM_STRUCTURE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT232B_EEPROM_STRUCTURE.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT232B_EEPROM_STRUCTURE.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT232B_EEPROM_STRUCTURE.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT232H_EEPROM_STRUCTURE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT232H_EEPROM_STRUCTURE.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT232H_EEPROM_STRUCTURE.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT232H_EEPROM_STRUCTURE.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT232R_EEPROM_STRUCTURE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT232R_EEPROM_STRUCTURE.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT232R_EEPROM_STRUCTURE.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT232R_EEPROM_STRUCTURE.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT4232H_EEPROM_STRUCTURE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT4232H_EEPROM_STRUCTURE.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT4232H_EEPROM_STRUCTURE.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT4232H_EEPROM_STRUCTURE.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_232H_CBUS_OPTIONS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_232H_CBUS_OPTIONS.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_232H_CBUS_OPTIONS.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_232H_CBUS_OPTIONS.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_BIT_MODES.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_BIT_MODES.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_BIT_MODES.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_BIT_MODES.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_CBUS_OPTIONS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_CBUS_OPTIONS.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_CBUS_OPTIONS.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_CBUS_OPTIONS.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DATA_BITS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DATA_BITS.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DATA_BITS.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DATA_BITS.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DEVICE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DEVICE.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DEVICE.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DEVICE.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DEVICE_INFO_NODE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DEVICE_INFO_NODE.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DEVICE_INFO_NODE.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DEVICE_INFO_NODE.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DRIVE_CURRENT.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DRIVE_CURRENT.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_DRIVE_CURRENT.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DRIVE_CURRENT.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EEPROM_DATA.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EEPROM_DATA.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EEPROM_DATA.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EEPROM_DATA.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EEPROM_HEADER.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EEPROM_HEADER.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EEPROM_HEADER.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EEPROM_HEADER.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_ERROR.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_ERROR.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_ERROR.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_ERROR.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EVENTS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EVENTS.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EVENTS.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EVENTS.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EXCEPTION.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EXCEPTION.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_EXCEPTION.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EXCEPTION.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_FLAGS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_FLAGS.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_FLAGS.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_FLAGS.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_FLOW_CONTROL.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_FLOW_CONTROL.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_FLOW_CONTROL.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_FLOW_CONTROL.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_LINE_STATUS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_LINE_STATUS.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_LINE_STATUS.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_LINE_STATUS.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_MODEM_STATUS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_MODEM_STATUS.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_MODEM_STATUS.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_MODEM_STATUS.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_OPCODE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_OPCODE.cs new file mode 100644 index 0000000000..ba794154a1 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_OPCODE.cs @@ -0,0 +1,91 @@ +/* +** FTD2XX_NET.cs +** +** Copyright © 2009-2021 Future Technology Devices International Limited +** +** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. +** Main module +** +** Author: FTDI +** Project: CDM Windows Driver Package +** Module: FTD2XX_NET Managed Wrapper +** Requires: +** Comments: +** +** History: +** 1.0.0 - Initial version +** 1.0.12 - Included support for the FT232H device. +** 1.0.14 - Included Support for the X-Series of devices. +** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. +** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. +** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. + +** Ported to NetStandard 2.1 2024, Wilderness Labs +*/ + +namespace Meadow.Foundation.ICs.IOExpanders; + +internal partial class FTDI +{ + internal enum FT_OPCODE + { + ClockDataBytesOutOnPlusVeClockMSBFirst = 0x10, + ClockDataBytesOutOnMinusVeClockMSBFirst = 0x11, + ClockDataBitsOutOnPlusVeClockMSBFirst = 0x12, + ClockDataBitsOutOnMinusVeClockMSBFirst = 0x13, + ClockDataBytesInOnPlusVeClockMSBFirst = 0x20, + ClockDataBytesInOnMinusVeClockMSBFirst = 0x24, + ClockDataBitsInOnPlusVeClockMSBFirst = 0x22, + ClockDataBitsInOnMinusVeClockMSBFirst = 0x26, + ClockDataBytesOutOnMinusBytesInOnPlusVeClockMSBFirst = 0x31, + ClockDataBytesOutOnPlusBytesInOnMinusVeClockMSBFirst = 0x34, + ClockDataBitsOutOnMinusBitsInOnPlusVeClockMSBFirst = 0x33, + ClockDataBitsOutOnPlusBitsInOnMinusVeClockMSBFirst = 0x36, + ClockDataBytesOutOnPlusVeClockLSBFirst = 0x18, + ClockDataBytesOutOnMinusVeClockLSBFirst = 0x19, + ClockDataBitsOutOnPlusVeClockLSBFirst = 0x1A, + ClockDataBitsOutOnMinusVeClockLSBFirst = 0x1B, + ClockDataBytesInOnPlusVeClockLSBFirst = 0x28, + ClockDataBytesInOnMinusVeClockLSBFirst = 0x2C, + ClockDataBitsInOnPlusVeClockLSBFirst = 0x2A, + ClockDataBitsInOnMinusVeClockSBFirst = 0x2E, + ClockDataBytesOutOnMinusBytesInOnPlusVeClockLSBFirst = 0x39, + ClockDataBytesOutOnPlusBytesInOnMinusVeClockLSBFirst = 0x3C, + ClockDataBitsOutOnMinusBitsInOnPlusVeClockLSBFirst = 0x3B, + ClockDataBitsOutOnPlusBitsInOnMinusVeClockLSBFirst = 0x3E, + ClockDataBytesOutOnPlusVeClockTMSPinLSBFirst = 0x4A, + ClockDataBytesOutOnMinusVeClockTMSPinSBFirst = 0x4B, + ClockDataBytesOutOnPlusDataInOnPlusVeClockTMSPinSBFirst = 0x6A, + ClockDataBytesOutOnMinusDataInOnPlusVeClockTMSPinSBFirst = 0x6B, + ClockDataBytesOutOnPlusDataInOnMinusVeClockTMSPinSBFirst = 0x6E, + ClockDataBytesOutOnMinusDataInOnMinusVeClockTMSPinSBFirst = 0x6F, + SetDataBitsLowByte = 0x80, + SetDataBitsHighByte = 0x82, + ReadDataBitsLowByte = 0x81, + ReadDataBitsHighByte = 0x83, + ConnectTDItoTDOforLoopback = 0x84, + DisconnectTDItoTDOforLoopback = 0x85, + SetTCKSKDivisor = 0x86, + SetClockDivisor = 0x86, + CPUModeReadShortAddress = 0x90, + CPUModeReadExtendedAddress = 0x91, + CPUModeWriteShortAddress = 0x92, + CPUModeWriteExtendedAddress = 0x93, + SendImmediate = 0x87, + WaitOnIOHigh = 0x88, + WaitOnIOLow = 0x89, + DisableClockDivideBy5 = 0x8A, + EnableClockDivideBy5 = 0x8B, + Enable3PhaseDataClocking = 0x8C, + Disable3PhaseDataClocking = 0x8D, + ClockForNBitsWithNoDataTransfer = 0x8E, + ClockForNx8BitsWithNoDataTransfer = 0x8F, + ClockContinuouslyAndWaitOnIOHigh = 0x94, + ClockContinuouslyAndWaitOnIOLow = 0x95, + TurnOnAdaptiveClocking = 0x96, + TurnOffAdaptiveClocking = 0x97, + ClockForNx8BitsWithNoDataTransferOrUntilGPIOL1IsHigh = 0x9C, + ClockForNx8BitsWithNoDataTransferOrUntilGPIOL1IsLow = 0x9D, + SetIOOnlyDriveOn0AndTristateOn1 = 0x9E, + } +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_PARITY.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_PARITY.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_PARITY.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_PARITY.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_PROGRAM_DATA.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_PROGRAM_DATA.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_PROGRAM_DATA.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_PROGRAM_DATA.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_PURGE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_PURGE.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_PURGE.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_PURGE.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_STATUS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_STATUS.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_STATUS.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_STATUS.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_STOP_BITS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_STOP_BITS.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_STOP_BITS.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_STOP_BITS.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_XSERIES_CBUS_OPTIONS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_XSERIES_CBUS_OPTIONS.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_XSERIES_CBUS_OPTIONS.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_XSERIES_CBUS_OPTIONS.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_XSERIES_DATA.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_XSERIES_DATA.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_XSERIES_DATA.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_XSERIES_DATA.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_XSERIES_EEPROM_STRUCTURE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_XSERIES_EEPROM_STRUCTURE.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.FT_XSERIES_EEPROM_STRUCTURE.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_XSERIES_EEPROM_STRUCTURE.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTD2XX/FTDI.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232Collection.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232Collection.cs deleted file mode 100644 index 8d528cf324..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232Collection.cs +++ /dev/null @@ -1,140 +0,0 @@ -using Meadow.Hardware; -using System; -using System.Collections; -using System.Collections.Generic; - -namespace Meadow.Foundation.ICs.IOExpanders; - -public class Ft232h : FtdiExpander -{ - internal Ft232h(FTDI.FT_DEVICE_INFO_NODE info, FTDI native) - : base(info, native) - { - } - - public override II2cBus CreateI2cBus(int busNumber = 1, I2cBusSpeed busSpeed = I2cBusSpeed.Standard) - { - throw new NotImplementedException(); - } - - public override II2cBus CreateI2cBus(IPin[] pins, I2cBusSpeed busSpeed) - { - throw new NotImplementedException(); - } - - public override II2cBus CreateI2cBus(IPin clock, IPin data, I2cBusSpeed busSpeed) - { - throw new NotImplementedException(); - } -} - -public abstract class FtdiExpander : -// IDisposable, -// IDigitalInputOutputController, -// IDigitalOutputController, -// ISpiController, - II2cController -{ - public abstract II2cBus CreateI2cBus(int busNumber = 1, I2cBusSpeed busSpeed = I2cBusSpeed.Standard); - public abstract II2cBus CreateI2cBus(IPin[] pins, I2cBusSpeed busSpeed); - public abstract II2cBus CreateI2cBus(IPin clock, IPin data, I2cBusSpeed busSpeed); - - internal static FtdiExpander From(FTDI.FT_DEVICE_INFO_NODE info, FTDI native) - { - return info.Type switch - { - FTDI.FT_DEVICE.FT_DEVICE_232H => new Ft232h(info, native), - _ => throw new NotSupportedException(), - }; - } - - private FTDI.FT_DEVICE_INFO_NODE _infoNode; - private FTDI _native; - - public string SerialNumber => _infoNode.SerialNumber; - - internal FtdiExpander(FTDI.FT_DEVICE_INFO_NODE info, FTDI native) - { - _infoNode = info; - _native = native; - } -} - -public class Ft232Collection : IEnumerable -{ - private FTDI _native; - - private static Ft232Collection? _instance; - - private List _list = new List(); - - /// - /// Gets the number of FtdiExpander devices connected to the host machine. - /// - public int Count => _list.Count; - - /// - /// Gets the FtdiExpander device at the specified index in the collection. - /// - /// The index of the FtdiExpander device to retrieve. - public FtdiExpander this[int index] => _list[index]; - - private Ft232Collection() - { - _native = new FTDI(); - } - - private void CheckStatus(FTDI.FT_STATUS status) - { - if (status == FTDI.FT_STATUS.FT_OK) return; - - throw new Exception($"ftd2xx.dll returned '{status}'"); - } - - public void Refresh() - { - uint count = 0; - - CheckStatus(_native.GetNumberOfDevices(ref count)); - - _list.Clear(); - - if (count > 0) - { - var infos = new FTDI.FT_DEVICE_INFO_NODE[count]; - CheckStatus(_native.GetDeviceList(infos)); - - foreach (var info in infos) - { - _list.Add(FtdiExpander.From(info, _native)); - } - } - } - - /// - public IEnumerator GetEnumerator() - { - return _list.GetEnumerator(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - - /// - /// Gets the singleton instance of Ft232Collection, initializing it if necessary. - /// - public static Ft232Collection Devices - { - get - { - if (_instance == null) - { - _instance = new Ft232Collection(); - _instance.Refresh(); - } - return _instance; - } - } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.PinDefinitions.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.PinDefinitions.cs index bbcc82d5b1..7839cec451 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.PinDefinitions.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.PinDefinitions.cs @@ -7,7 +7,7 @@ namespace Meadow.Foundation.ICs.IOExpanders /// /// Represents the pin definitions for the Ft232h IC. /// - public partial class Ft232h_old + public partial class Ft232h { /// /// Defines the pin definitions for the Ft232h IC. @@ -37,7 +37,7 @@ public class PinDefinitions : IPinDefinitions /// Creates a new PinDefinitions object. /// /// The Ft232h controller associated with the pins. - internal PinDefinitions(Ft232h_old controller) + internal PinDefinitions(Ft232h controller) { UseMpsseKeys = controller.UsingMpsse; Controller = controller; diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.cs index c7997ae5b8..2b6896a12e 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.cs @@ -7,12 +7,12 @@ namespace Meadow.Foundation.ICs.IOExpanders; /// /// Represents an FT232 IO Expander /// -public partial class Ft232h_old : -IDisposable, -IDigitalInputOutputController, -IDigitalOutputController, -ISpiController, -II2cController +public partial class Ft232h : + IDisposable, + IDigitalInputOutputController, + IDigitalOutputController, + ISpiController, + II2cController { private bool _isDisposed; private IFtdiImpl _impl; @@ -25,10 +25,10 @@ public partial class Ft232h_old : public PinDefinitions Pins { get; } /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// Specifies whether to use Multi-Protocol Synchronous Serial Engine (MPSSE) mode (default is false). - public Ft232h_old(bool useMPSSE = false) + public Ft232h(bool useMPSSE = false) { UsingMpsse = useMPSSE; @@ -108,7 +108,7 @@ public ISpiBus CreateSpiBus(int busNumber, SpiClockConfiguration config) public static SpiClockConfiguration DefaultClockConfiguration { get => new SpiClockConfiguration( - new Frequency(Ft232SpiBus.DefaultClockRate, Frequency.UnitType.Hertz)); + new Frequency(MpsseSpiBus.DefaultClockRate, Frequency.UnitType.Hertz)); } /// @@ -145,7 +145,7 @@ protected virtual void Dispose(bool disposing) /// /// Finalizer for the Ft232h class, used to release unmanaged resources. /// - ~Ft232h_old() + ~Ft232h() { Dispose(false); } diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xxDigitalInputPort.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxDigitalInputPort.cs similarity index 87% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xxDigitalInputPort.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxDigitalInputPort.cs index 4226f634de..93820dfbc2 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xxDigitalInputPort.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxDigitalInputPort.cs @@ -8,10 +8,10 @@ namespace Meadow.Foundation.ICs.IOExpanders; /// public sealed class Ft23xxDigitalInputPort : DigitalInputPortBase { - private FtdiDevice _device; + private MpsseChannel _device; private ResistorMode _resistor; - internal Ft23xxDigitalInputPort(FtdiDevice device, IPin pin, ResistorMode resistorMode, IDigitalChannelInfo channel) + internal Ft23xxDigitalInputPort(MpsseChannel device, IPin pin, ResistorMode resistorMode, IDigitalChannelInfo channel) : base(pin, channel) { Resistor = resistorMode; diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xxDigitalOutputPort.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxDigitalOutputPort.cs similarity index 87% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xxDigitalOutputPort.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxDigitalOutputPort.cs index 4f3982d9e2..9f16b7a0de 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xxDigitalOutputPort.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxDigitalOutputPort.cs @@ -8,12 +8,12 @@ namespace Meadow.Foundation.ICs.IOExpanders; /// public sealed class Ft23xxDigitalOutputPort : DigitalOutputPortBase { - private FtdiDevice _device; + private MpsseChannel _device; private bool _state; private bool _isHighByte; private byte _key; - internal Ft23xxDigitalOutputPort(FtdiDevice device, IPin pin, IDigitalChannelInfo channel, bool initialState, OutputType initialOutputType) + internal Ft23xxDigitalOutputPort(MpsseChannel device, IPin pin, IDigitalChannelInfo channel, bool initialState, OutputType initialOutputType) : base(pin, channel, initialState, initialOutputType) { _device = device; diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xxI2cBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxI2cBus.cs similarity index 96% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xxI2cBus.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxI2cBus.cs index 54b7964b06..7c7492d466 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xxI2cBus.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxI2cBus.cs @@ -9,7 +9,7 @@ namespace Meadow.Foundation.ICs.IOExpanders; /// public sealed class Ft23xxI2cBus : II2cBus, IDisposable { - private FtdiDevice _device; + private MpsseChannel _device; /// /// Gets the handle to the FT23xx device used by the I2C bus. @@ -19,7 +19,7 @@ public sealed class Ft23xxI2cBus : II2cBus, IDisposable /// public I2cBusSpeed BusSpeed { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - internal Ft23xxI2cBus(FtdiDevice device) + internal Ft23xxI2cBus(MpsseChannel device) { if (device.Handle == IntPtr.Zero) { diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xxSpiBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxSpiBus.cs similarity index 94% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xxSpiBus.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxSpiBus.cs index 66923ba9c9..4c4c79885b 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xxSpiBus.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxSpiBus.cs @@ -9,7 +9,7 @@ namespace Meadow.Foundation.ICs.IOExpanders; /// public sealed class Ft23xxSpiBus : IFt232Bus, ISpiBus, IDisposable { - private FtdiDevice _device; + private MpsseChannel _device; /// /// Gets the handle to the FT23xx device used by the SPI bus. @@ -28,7 +28,7 @@ public sealed class Ft23xxSpiBus : IFt232Bus, ISpiBus, IDisposable /// public SpiClockConfiguration Configuration { get; } - internal Ft23xxSpiBus(FtdiDevice device, SpiClockConfiguration config) + internal Ft23xxSpiBus(MpsseChannel device, SpiClockConfiguration config) { Configuration = config; diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.ft23xx.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ftd2xxImpl.cs similarity index 94% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.ft23xx.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ftd2xxImpl.cs index 88c3d1595e..a761cf7dbe 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.ft23xx.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ftd2xxImpl.cs @@ -4,11 +4,11 @@ namespace Meadow.Foundation.ICs.IOExpanders; internal class Ftd2xxImpl : IFtdiImpl { - private FtdiDeviceCollection _devices = default!; + private MpsseChannelCollection _devices = default!; public void Initialize() { - _devices = new FtdiDeviceCollection(); + _devices = new MpsseChannelCollection(); _devices.Refresh(); } diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Native.Ftd2xx.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Native.Ftd2xx.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Native.Ftd2xx.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Native.Ftd2xx.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs new file mode 100644 index 0000000000..c2ccc2ae15 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs @@ -0,0 +1,97 @@ +using System; + +namespace Meadow.Foundation.ICs.IOExpanders; + +public class Ft232HExpander : FtdiExpander +{ + internal Ft232HExpander() + { + } +} + +public abstract class FtdiExpander +// IDisposable, +// IDigitalInputOutputController, +// IDigitalOutputController, +// ISpiController, +// II2cController +{ + // public abstract II2cBus CreateI2cBus(int busNumber = 1, I2cBusSpeed busSpeed = I2cBusSpeed.Standard); + // public abstract II2cBus CreateI2cBus(IPin[] pins, I2cBusSpeed busSpeed); + // public abstract II2cBus CreateI2cBus(IPin clock, IPin data, I2cBusSpeed busSpeed); + + private FTDI.FT_DEVICE_INFO_NODE _infoNode; + private FTDI _native; + + public string SerialNumber => _infoNode.SerialNumber; + + internal static FtdiExpander From(FTDI.FT_DEVICE_INFO_NODE info, FTDI native) + { + return info.Type switch + { + FTDI.FT_DEVICE.FT_DEVICE_232H => new Ft232HExpander() + { + _native = native, + _infoNode = info, + }, + _ => throw new NotSupportedException(), + }; + } + + internal FtdiExpander() + { + } + + private void InitializeGpio() + { + CheckStatus(_native.SetBitMode(0, FTDI.FT_BIT_MODES.FT_BIT_MODE_RESET)); + CheckStatus(_native.SetBitMode(0, FTDI.FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG)); + + ClearInputBuffer(); + } + + private bool CheckStatus(FTDI.FT_STATUS status) + { + if (status == FTDI.FT_STATUS.FT_OK) + { + return true; + } + + throw new Exception($"Native error: {status}"); + } + + private void ClearInputBuffer() + { + var available = GetAvailableBytes(); + + if (available > 0) + { + var rxBuffer = new byte[available]; + uint bytesRead = 0; + CheckStatus( + _native.Read(rxBuffer, bytesRead, ref bytesRead)); + } + } + + private uint GetAvailableBytes() + { + uint availableBytes = 0; + + CheckStatus( + _native.GetRxBytesAvailable(ref availableBytes)); + + return availableBytes; + } + + internal void SetGpioState(bool lowByte, byte direction, byte state) + { + Span outBuffer = stackalloc byte[3]; + outBuffer[0] = (byte)(lowByte ? FTDI.FT_OPCODE.SetDataBitsLowByte : FTDI.FT_OPCODE.SetDataBitsHighByte); + outBuffer[1] = state; //data + outBuffer[2] = direction; //direction 1 == output, 0 == input + uint written = 0; + CheckStatus( + _native.Write(outBuffer.ToArray(), 3, ref written)); + return; + } +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpanderCollection.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpanderCollection.cs new file mode 100644 index 0000000000..11d33017e2 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpanderCollection.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace Meadow.Foundation.ICs.IOExpanders; + +public class FtdiExpanderCollection : IEnumerable +{ + private FTDI _native; + + private static FtdiExpanderCollection? _instance; + + private List _list = new List(); + + /// + /// Gets the number of FtdiExpander devices connected to the host machine. + /// + public int Count => _list.Count; + + /// + /// Gets the FtdiExpander device at the specified index in the collection. + /// + /// The index of the FtdiExpander device to retrieve. + public FtdiExpander this[int index] => _list[index]; + + private FtdiExpanderCollection() + { + _native = new FTDI(); + } + + private void CheckStatus(FTDI.FT_STATUS status) + { + if (status == FTDI.FT_STATUS.FT_OK) return; + + throw new Exception($"ftd2xx.dll returned '{status}'"); + } + + public void Refresh() + { + uint count = 0; + + CheckStatus(_native.GetNumberOfDevices(ref count)); + + _list.Clear(); + + if (count > 0) + { + var infos = new FTDI.FT_DEVICE_INFO_NODE[count]; + CheckStatus(_native.GetDeviceList(infos)); + + foreach (var info in infos) + { + _list.Add(FtdiExpander.From(info, _native)); + } + } + } + + /// + public IEnumerator GetEnumerator() + { + return _list.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + /// + /// Gets the singleton instance of Ft232Collection, initializing it if necessary. + /// + public static FtdiExpanderCollection Devices + { + get + { + if (_instance == null) + { + _instance = new FtdiExpanderCollection(); + _instance.Refresh(); + } + return _instance; + } + } +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/ICs.IOExpanders.Ft232h.csproj b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/ICs.IOExpanders.Ft232h.csproj index 54ce741fe0..07c4042c18 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/ICs.IOExpanders.Ft232h.csproj +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/ICs.IOExpanders.Ft232h.csproj @@ -19,8 +19,12 @@ true Ft232h USB IOExpander for GPIO, I2C, SPI on Windows + + + + diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiDevice.Gpio.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.Gpio.cs similarity index 98% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiDevice.Gpio.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.Gpio.cs index 4aeb29edee..7bd558a6f1 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiDevice.Gpio.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.Gpio.cs @@ -5,7 +5,7 @@ namespace Meadow.Foundation.ICs.IOExpanders; -internal partial class FtdiDevice +internal partial class MpsseChannel { private bool? _useMpseeForGpio = null; diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiDevice.I2c.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.I2c.cs similarity index 99% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiDevice.I2c.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.I2c.cs index 296ebb9eba..2742f03da2 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiDevice.I2c.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.I2c.cs @@ -4,7 +4,7 @@ namespace Meadow.Foundation.ICs.IOExpanders; -internal partial class FtdiDevice +internal partial class MpsseChannel { private byte GpioLowData = 0; private byte GpioLowDir = 0; diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiDevice.Spi.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.Spi.cs similarity index 99% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiDevice.Spi.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.Spi.cs index 147811933c..dfb132cd94 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiDevice.Spi.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.Spi.cs @@ -6,7 +6,7 @@ namespace Meadow.Foundation.ICs.IOExpanders; -internal partial class FtdiDevice +internal partial class MpsseChannel { private SpiClockConfiguration? _spiConfig; diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiDevice.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.cs similarity index 98% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiDevice.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.cs index 99dfcffacd..1e99ad31cd 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiDevice.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.cs @@ -6,7 +6,7 @@ namespace Meadow.Foundation.ICs.IOExpanders; -internal partial class FtdiDevice +internal partial class MpsseChannel { private const int DefaultTimeoutMs = 5000; private const int DefaultLatencyTimer = 16; @@ -20,7 +20,7 @@ internal partial class FtdiDevice internal string Description { get; } internal IntPtr Handle { get; private set; } - internal FtdiDevice( + internal MpsseChannel( uint index, uint flags, FtDeviceType deviceType, diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiDeviceCollection.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannelCollection.cs similarity index 81% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiDeviceCollection.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannelCollection.cs index 0ea1c4c4f3..d7ca87aa29 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiDeviceCollection.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannelCollection.cs @@ -3,17 +3,16 @@ using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; -using static Meadow.Foundation.ICs.IOExpanders.Native.Ftd2xx; namespace Meadow.Foundation.ICs.IOExpanders; -internal class FtdiDeviceCollection : IEnumerable +internal class MpsseChannelCollection : IEnumerable { - private List _devices = new(); + private List _devices = new(); public int Count => _devices.Count; - public FtdiDevice this[int index] + public MpsseChannel this[int index] { get => _devices[index]; } @@ -26,7 +25,7 @@ public void Refresh() try { - Native.CheckStatus(FT_CreateDeviceInfoList(out count)); + Native.CheckStatus(Native.Mpsse.FT_CreateDeviceInfoList(out count)); } catch (DllNotFoundException) { @@ -64,11 +63,11 @@ in MemoryMarshal.GetReference(descriptionBuffer), var serialNumber = Encoding.ASCII.GetString(serialNumberBuffer.ToArray(), 0, serialNumberBuffer.IndexOf((byte)0)); var description = Encoding.ASCII.GetString(descriptionBuffer.ToArray(), 0, descriptionBuffer.IndexOf((byte)0)); - _devices.Add(new FtdiDevice(index, flags, deviceType, id, locid, serialNumber, description, handle)); + _devices.Add(new MpsseChannel(index, flags, deviceType, id, locid, serialNumber, description, handle)); } } - public IEnumerator GetEnumerator() + public IEnumerator GetEnumerator() { return _devices.GetEnumerator(); } diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232DigitalOutputPort.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseDigitalOutputPort.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232DigitalOutputPort.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseDigitalOutputPort.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.mpsse.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseImpl.cs similarity index 92% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.mpsse.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseImpl.cs index f779130d0c..c718b7256e 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.mpsse.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseImpl.cs @@ -11,7 +11,7 @@ internal class MpsseImpl : IFtdiImpl private static int _instanceCount = 0; private bool _isDisposed = false; private Dictionary _i2cBuses = new Dictionary(); - private Dictionary _spiBuses = new Dictionary(); + private Dictionary _spiBuses = new Dictionary(); private bool _spiBusAutoCreated = false; private IFt232Bus? _activeBus = null; @@ -54,9 +54,9 @@ private Dictionary GetI2CBuses() return result; } - private Dictionary GetSpiBuses() + private Dictionary GetSpiBuses() { - Dictionary result = new Dictionary(); + Dictionary result = new Dictionary(); if (Native.CheckStatus(Native.Mpsse.SPI_GetNumChannels(out int channels))) { @@ -66,7 +66,7 @@ private Dictionary GetSpiBuses() { if (Native.CheckStatus(Native.Mpsse.SPI_GetChannelInfo(c, out Native.FT_DEVICE_LIST_INFO_NODE info))) { - result.Add(c, new Ft232SpiBus(c, info)); + result.Add(c, new MpsseSpiBus(c, info)); } } } @@ -137,7 +137,7 @@ public IDigitalInputPort CreateDigitalInputPort(int channel, IPin pin, ResistorM if (_activeBus == null) { - var bus = CreateSpiBus(channel, Ft232h_old.DefaultClockConfiguration); + var bus = CreateSpiBus(channel, Ft232h.DefaultClockConfiguration); _spiBusAutoCreated = true; _activeBus = bus as IFt232Bus; } @@ -156,7 +156,7 @@ public IDigitalOutputPort CreateDigitalOutputPort(int channel, IPin pin, bool in if (_activeBus == null) { - var bus = CreateSpiBus(channel, Ft232h_old.DefaultClockConfiguration); + var bus = CreateSpiBus(channel, Ft232h.DefaultClockConfiguration); _spiBusAutoCreated = true; _activeBus = bus as IFt232Bus; } diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232SpiBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseSpiBus.cs similarity index 96% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232SpiBus.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseSpiBus.cs index c0e179cc18..1ef2801383 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232SpiBus.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseSpiBus.cs @@ -2,7 +2,7 @@ using Meadow.Units; using System; using System.Runtime.InteropServices; -using static Meadow.Foundation.ICs.IOExpanders.Ft232h_old; +using static Meadow.Foundation.ICs.IOExpanders.Ft232h; using static Meadow.Foundation.ICs.IOExpanders.Native; namespace Meadow.Foundation.ICs.IOExpanders; @@ -10,7 +10,7 @@ namespace Meadow.Foundation.ICs.IOExpanders; /// /// Represents an SPI bus implementation using the FT232 device. /// -public sealed class Ft232SpiBus : IFt232Bus, ISpiBus, IDisposable +public sealed class MpsseSpiBus : IFt232Bus, ISpiBus, IDisposable { /// /// The default SPI clock rate for the FT232H @@ -36,7 +36,7 @@ public sealed class Ft232SpiBus : IFt232Bus, ISpiBus, IDisposable /// public Frequency[] SupportedSpeeds => new Frequency[] { new Frequency(30d, Frequency.UnitType.Megahertz) }; - internal Ft232SpiBus(int channelNumber, FT_DEVICE_LIST_INFO_NODE info) + internal MpsseSpiBus(int channelNumber, FT_DEVICE_LIST_INFO_NODE info) { ChannelNumber = channelNumber; InfoNode = info; @@ -55,7 +55,7 @@ private void Dispose(bool disposing) /// /// Finalizer for the Ft232SpiBus class, used to release unmanaged resources. /// - ~Ft232SpiBus() + ~MpsseSpiBus() { // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method Dispose(false); @@ -153,7 +153,7 @@ private void CloseChannel() private SpiChannelConfig CreateChannelConfig(SpiClockConfiguration config) { // for now we support CS on D3 and that's it - Ft232h_old.SpiConfigOptions opts = SpiConfigOptions.CS_ACTIVELOW | SpiConfigOptions.CS_DBUS3; + Ft232h.SpiConfigOptions opts = SpiConfigOptions.CS_ACTIVELOW | SpiConfigOptions.CS_DBUS3; switch (config.SpiMode) { diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Native.Mpsse.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/Native.Mpsse.cs similarity index 99% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Native.Mpsse.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/Native.Mpsse.cs index a17b5960b5..01f0c95108 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Native.Mpsse.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/Native.Mpsse.cs @@ -1,6 +1,6 @@ using System; using System.Runtime.InteropServices; -using static Meadow.Foundation.ICs.IOExpanders.Ft232h_old; +using static Meadow.Foundation.ICs.IOExpanders.Ft232h; namespace Meadow.Foundation.ICs.IOExpanders { diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Native.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Native.cs index f6757a9ab8..b928a2ebe6 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Native.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Native.cs @@ -259,7 +259,7 @@ specific to the protocol that are implemented in the chip */ public uint ClockRate; public byte LatencyTimer; - public Ft232h_old.SpiConfigOptions Options; + public Ft232h.SpiConfigOptions Options; public uint Pin; public ushort CurrentPinState; } diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SPITransferOptions.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SPITransferOptions.cs index 32d6d29a20..8e1055700f 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SPITransferOptions.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SPITransferOptions.cs @@ -2,7 +2,7 @@ namespace Meadow.Foundation.ICs.IOExpanders { - public partial class Ft232h_old + public partial class Ft232h { [Flags] internal enum SPITransferOptions diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SpiConfigOptions.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SpiConfigOptions.cs index 0341701622..f60142ae8e 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SpiConfigOptions.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SpiConfigOptions.cs @@ -2,7 +2,7 @@ namespace Meadow.Foundation.ICs.IOExpanders { - public partial class Ft232h_old + public partial class Ft232h { /// /// Flags for configuring the SPI communication options. diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs index b4ab5c9ba0..a72e119116 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs @@ -8,17 +8,17 @@ Console.WriteLine("HELLO FROM THE WILDERNESS FT232H DRIVER!"); -Ft232Collection.Devices.Refresh(); -var count = Ft232Collection.Devices.Count(); -var ft232 = Ft232Collection.Devices[0]; +FtdiExpanderCollection.Devices.Refresh(); +var count = FtdiExpanderCollection.Devices.Count(); +var ft232 = FtdiExpanderCollection.Devices[0]; -var ft232h = new Ft232h_old(); +var ft232h = new Ft232h(); //await TestBME280(ft232); await TestIli9341(ft232h); //await TestGpio(ft232); -async Task TestBME280(Ft232h_old expander) +async Task TestBME280(Ft232h expander) { var bme = new Bme280(expander.CreateI2cBus()); @@ -30,7 +30,7 @@ async Task TestBME280(Ft232h_old expander) } } -async Task TestGpio(Ft232h_old expander) +async Task TestGpio(Ft232h expander) { var outputs = new List { @@ -60,7 +60,7 @@ async Task TestGpio(Ft232h_old expander) } } -async Task TestIli9341(Ft232h_old expander) +async Task TestIli9341(Ft232h expander) { var ili = new Ili9341( expander.CreateSpiBus(), diff --git a/Source/Meadow.Foundation.Peripherals/Sensors.Camera.Arducam/Samples/Arducam_Sample/Arducam_Sample - Backup.csproj b/Source/Meadow.Foundation.Peripherals/Sensors.Camera.Arducam/Samples/Arducam_Sample/Arducam_Sample - Backup.csproj new file mode 100644 index 0000000000..619e6f3a3c --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/Sensors.Camera.Arducam/Samples/Arducam_Sample/Arducam_Sample - Backup.csproj @@ -0,0 +1,26 @@ + + + https://github.com/WildernessLabs/Meadow.Foundation + Wilderness Labs, Inc + Wilderness Labs, Inc + true + netstandard2.1 + Library + App + + + true + + + + + + + + + + + Always + + + From 301b0b879e90cf5f96164534a1cf996e2743b6f7 Mon Sep 17 00:00:00 2001 From: Chris Tacke Date: Mon, 19 Feb 2024 16:45:11 -0600 Subject: [PATCH 03/11] multi-FT232 GPIO support --- .../Driver/FTDI/FTDI.cs | 2 +- .../Driver/Ft232h.PinDefinitions.cs | 290 ---------------- .../Driver/FtdiExpander.PinDefinitions.cs | 317 ++++++++++++++++++ .../Driver/FtdiExpander.cs | 203 +++++++++-- .../Driver/FtdiExpanderCollection.cs | 61 ++-- .../Driver/ICs.IOExpanders.Ft232h.csproj | 36 +- .../{ => MPSSE}/MpsseDigitalInputPort.cs | 0 .../Driver/{ => MPSSE}/MpsseI2cBus.cs | 0 .../Samples/Ft232h_Windows_Sample/Program.cs | 95 +++--- 9 files changed, 616 insertions(+), 388 deletions(-) delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.PinDefinitions.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.PinDefinitions.cs rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{ => MPSSE}/MpsseDigitalInputPort.cs (100%) rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{ => MPSSE}/MpsseI2cBus.cs (100%) diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.cs index edc9218bbd..36d9e16b9b 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.cs @@ -319,7 +319,7 @@ private void FindFunctionPointers() private const uint FT_DEFAULT_DEVICE_ID = 0x04036001; // Create private variables for the device within the class - private IntPtr ftHandle = IntPtr.Zero; + public IntPtr ftHandle = IntPtr.Zero; // Handle to our DLL - used with GetProcAddress to load all of our functions private IntPtr hFTD2XXDLL = IntPtr.Zero; diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.PinDefinitions.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.PinDefinitions.cs deleted file mode 100644 index 7839cec451..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.PinDefinitions.cs +++ /dev/null @@ -1,290 +0,0 @@ -using Meadow.Hardware; -using System.Collections; -using System.Collections.Generic; - -namespace Meadow.Foundation.ICs.IOExpanders -{ - /// - /// Represents the pin definitions for the Ft232h IC. - /// - public partial class Ft232h - { - /// - /// Defines the pin definitions for the Ft232h IC. - /// - public class PinDefinitions : IPinDefinitions - { - /// - /// Gets an enumerator for all the pins. - /// - public IEnumerator GetEnumerator() => AllPins.GetEnumerator(); - - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - - /// - /// Collection of all pins. - /// - public IList AllPins { get; } = new List(); - - /// - /// The pin controller - /// - public IPinController? Controller { get; set; } - - private bool UseMpsseKeys { get; } - - /// - /// Creates a new PinDefinitions object. - /// - /// The Ft232h controller associated with the pins. - internal PinDefinitions(Ft232h controller) - { - UseMpsseKeys = controller.UsingMpsse; - Controller = controller; - InitAllPins(); - } - - // Aliases - /// - /// Gets the IPin representing the SPI clock (SCK) pin. - /// - public IPin SPI_SCK => D0; - - /// - /// Gets the IPin representing the SPI data out (COPI) pin. - /// - public IPin SPI_COPI => D1; - - /// - /// Gets the IPin representing the SPI data in (CIPO) pin. - /// - public IPin SPI_CIPO => D2; - - /// - /// Gets the IPin representing the SPI chip select (CS0) pin. - /// - public IPin SPI_CS0 => D3; - - /// - /// Gets the IPin representing the I2C clock (SCL) pin. - /// - public IPin I2C_SCL => D0; - - /// - /// Gets the IPin representing the I2C data (SDA) pin. - /// - public IPin I2C_SDA => D1; - - - /// - /// Pin D0 definition. - /// - public IPin D0 => new Pin( - Controller, - "D0", - (byte)0x10, - new List { - new SpiChannelInfo("SPI_SCK", SpiLineType.Clock), - new I2cChannelInfo("I2C_SCL", I2cChannelFunctionType.Clock) - }); - - /// - /// Pin D1 definition. - /// - public IPin D1 => new Pin( - Controller, - "D1", - (byte)0x11, - new List { - new SpiChannelInfo("SPI_COPI", SpiLineType.COPI), - new I2cChannelInfo("I2C_SDA", I2cChannelFunctionType.Data) - }); - - /// - /// Pin D2 definition. - /// - public IPin D2 => new Pin( - Controller, - "D2", - (byte)0x12, - new List { - new SpiChannelInfo("SPI_CIPO", SpiLineType.CIPO) - }); - - /// - /// Pin D3 definition. - /// - public IPin D3 => new Pin( - Controller, - "D3", - (byte)0x13, - new List { - new SpiChannelInfo("SPI_CS0", SpiLineType.ChipSelect) - }); - - /// - /// Pin D4 definition. - /// - public IPin D4 => new Pin( - Controller, - "D4", - UseMpsseKeys ? (byte)0x14 : (ushort)(1 << 4), - new List { - new DigitalChannelInfo("D4", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) - }); - - /// - /// Pin D5 definition. - /// - public IPin D5 => new Pin( - Controller, - "D5", - UseMpsseKeys ? (byte)0x15 : (ushort)(1 << 5), - new List { - new DigitalChannelInfo("D5", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) - }); - - /// - /// Pin D6 definition. - /// - public IPin D6 => new Pin( - Controller, - "D6", - UseMpsseKeys ? (byte)0x16 : (ushort)(1 << 6), - new List { - new DigitalChannelInfo("D6", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) - }); - - /// - /// Pin D7 definition. - /// - public IPin D7 => new Pin( - Controller, - "D7", - UseMpsseKeys ? (byte)0x17 : (ushort)(1 << 7), - new List { - new DigitalChannelInfo("D7", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) - }); - - /// - /// Pin SPI_COPI_D1 definition. - /// - public IPin SPI_COPI_D1 => new Pin( - Controller, - "SPI_COPI_D1", - (byte)0x00, - new List { - new DigitalChannelInfo("D1", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) - }); - - /// - /// Pin C0 definition. - /// - public IPin C0 => new Pin( - Controller, - "C0", - UseMpsseKeys ? (byte)(1 << 0) : (ushort)(1 << 8), - new List { - new DigitalChannelInfo("C0", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) - }); - - /// - /// Pin C1 definition. - /// - public IPin C1 => new Pin( - Controller, - "C1", - UseMpsseKeys ? (byte)(1 << 1) : (ushort)(1 << 9), - new List { - new DigitalChannelInfo("C1", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) - }); - - /// - /// Pin C2 definition. - /// - public IPin C2 => new Pin( - Controller, - "C2", - UseMpsseKeys ? (byte)(1 << 2) : (ushort)(1 << 10), - new List { - new DigitalChannelInfo("C2", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) - }); - - /// - /// Pin C3 definition. - /// - public IPin C3 => new Pin( - Controller, - "C3", - UseMpsseKeys ? (byte)(1 << 3) : (ushort)(1 << 11), - new List { - new DigitalChannelInfo("C3", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) - }); - - /// - /// Pin C4 definition. - /// - public IPin C4 => new Pin( - Controller, - "C4", - UseMpsseKeys ? (byte)(1 << 4) : (ushort)(1 << 12), - new List { - new DigitalChannelInfo("C4", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) - }); - - /// - /// Pin C5 definition. - /// - public IPin C5 => new Pin( - Controller, - "C5", - UseMpsseKeys ? (byte)(1 << 5) : (ushort)(1 << 13), - new List { - new DigitalChannelInfo("C5", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) - }); - - /// - /// Pin C6 definition. - /// - public IPin C6 => new Pin( - Controller, - "C6", - UseMpsseKeys ? (byte)(1 << 6) : (ushort)(1 << 14), - new List { - new DigitalChannelInfo("C6", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) - }); - - /// - /// Pin C7 definition. - /// - public IPin C7 => new Pin( - Controller, - "C7", - UseMpsseKeys ? (byte)(1 << 7) : (ushort)(1 << 15), - new List { - new DigitalChannelInfo("C7", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) - }); - - /// - /// Initializes all serial wombat pins. - /// - protected void InitAllPins() - { - // Add all our pins to the collection - AllPins.Add(D0); - AllPins.Add(D1); - AllPins.Add(D2); - AllPins.Add(D3); - - AllPins.Add(C0); - AllPins.Add(C1); - AllPins.Add(C2); - AllPins.Add(C3); - AllPins.Add(C4); - AllPins.Add(C5); - AllPins.Add(C6); - AllPins.Add(C7); - } - } - } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.PinDefinitions.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.PinDefinitions.cs new file mode 100644 index 0000000000..b47f20f9f0 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.PinDefinitions.cs @@ -0,0 +1,317 @@ +using Meadow.Hardware; +using System.Collections; +using System.Collections.Generic; + +namespace Meadow.Foundation.ICs.IOExpanders; + +public class FtdiPin : Pin +{ + public bool IsLowByte { get; set; } + + internal FtdiPin(bool isLowByte, IPinController? controller, string name, object key, IList? supportedChannels) + : base(controller, name, key, supportedChannels) + { + IsLowByte = isLowByte; + } +} + +/// +/// Represents the pin definitions for the Ft232h IC. +/// +public partial class FtdiExpander +{ + /// + /// Defines the pin definitions for the Ft232h IC. + /// + public class PinDefinitions : IPinDefinitions + { + /// + /// Gets an enumerator for all the pins. + /// + public IEnumerator GetEnumerator() => AllPins.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + /// + /// Collection of all pins. + /// + public IList AllPins { get; } = new List(); + + /// + /// The pin controller + /// + public IPinController? Controller { get; set; } + + /// + /// Creates a new PinDefinitions object. + /// + /// The FtdiExpander controller associated with the pins. + internal PinDefinitions(FtdiExpander controller) + { + Controller = controller; + InitAllPins(); + } + + // Aliases + /// + /// Gets the IPin representing the SPI clock (SCK) pin. + /// + public IPin SPI_SCK => D0; + + /// + /// Gets the IPin representing the SPI data out (COPI) pin. + /// + public IPin SPI_COPI => D1; + + /// + /// Gets the IPin representing the SPI data in (CIPO) pin. + /// + public IPin SPI_CIPO => D2; + + /// + /// Gets the IPin representing the SPI chip select (CS0) pin. + /// + public IPin SPI_CS0 => D3; + + /// + /// Gets the IPin representing the I2C clock (SCL) pin. + /// + public IPin I2C_SCL => D0; + + /// + /// Gets the IPin representing the I2C data (SDA) pin. + /// + public IPin I2C_SDA => D1; + + + /// + /// Pin D0 definition. + /// + public IPin D0 => new FtdiPin( + true, + Controller, + "D0", + (byte)(1 << 0), + new List { + new SpiChannelInfo("SPI_SCK", SpiLineType.Clock), + new I2cChannelInfo("I2C_SCL", I2cChannelFunctionType.Clock) + }); + + /// + /// Pin D1 definition. + /// + public IPin D1 => new FtdiPin( + true, + Controller, + "D1", + (byte)(1 << 1), + new List { + new SpiChannelInfo("SPI_COPI", SpiLineType.COPI), + new I2cChannelInfo("I2C_SDA", I2cChannelFunctionType.Data) + }); + + /// + /// Pin D2 definition. + /// + public IPin D2 => new FtdiPin( + true, + Controller, + "D2", + (byte)(1 << 2), + new List { + new SpiChannelInfo("SPI_CIPO", SpiLineType.CIPO) + }); + + /// + /// Pin D3 definition. + /// + public IPin D3 => new FtdiPin( + true, + Controller, + "D3", + (byte)(1 << 3), + new List { + new SpiChannelInfo("SPI_CS0", SpiLineType.ChipSelect) + }); + + /// + /// Pin D4 definition. + /// + public IPin D4 => new FtdiPin( + true, + Controller, + "D4", + (byte)(1 << 4), + new List { + new DigitalChannelInfo("D4", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) + }); + + /// + /// Pin D5 definition. + /// + public IPin D5 => new FtdiPin( + true, + Controller, + "D5", + (byte)(1 << 5), + new List { + new DigitalChannelInfo("D5", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) + }); + + /// + /// Pin D6 definition. + /// + public IPin D6 => new FtdiPin( + true, + Controller, + "D6", + (byte)(1 << 6), + new List { + new DigitalChannelInfo("D6", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) + }); + + /// + /// Pin D7 definition. + /// + public IPin D7 => new FtdiPin( + true, + Controller, + "D7", + (byte)(1 << 7), + new List { + new DigitalChannelInfo("D7", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) + }); + + /// + /// Pin SPI_COPI_D1 definition. + /// + public IPin SPI_COPI_D1 => new Pin( + Controller, + "SPI_COPI_D1", + (byte)0x00, + new List { + new DigitalChannelInfo("D1", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) + }); + + /// + /// Pin C0 definition. + /// + public IPin C0 => new FtdiPin( + false, + Controller, + "C0", + (byte)(1 << 0), + new List { + new DigitalChannelInfo("C0", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) + }); + + /// + /// Pin C1 definition. + /// + public IPin C1 => new FtdiPin( + false, + Controller, + "C1", + (byte)(1 << 1), + new List { + new DigitalChannelInfo("C1", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) + }); + + /// + /// Pin C2 definition. + /// + public IPin C2 => new FtdiPin( + false, + Controller, + "C2", + (byte)(1 << 2), + new List { + new DigitalChannelInfo("C2", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) + }); + + /// + /// Pin C3 definition. + /// + public IPin C3 => new FtdiPin( + false, + Controller, + "C3", + (byte)(1 << 3), + new List { + new DigitalChannelInfo("C3", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) + }); + + /// + /// Pin C4 definition. + /// + public IPin C4 => new FtdiPin( + false, + Controller, + "C4", + (byte)(1 << 4), + new List { + new DigitalChannelInfo("C4", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) + }); + + /// + /// Pin C5 definition. + /// + public IPin C5 => new FtdiPin( + false, + Controller, + "C5", + (byte)(1 << 5), + new List { + new DigitalChannelInfo("C5", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) + }); + + /// + /// Pin C6 definition. + /// + public IPin C6 => new FtdiPin( + false, + Controller, + "C6", + (byte)(1 << 6), + new List { + new DigitalChannelInfo("C6", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) + }); + + /// + /// Pin C7 definition. + /// + public IPin C7 => new FtdiPin( + false, + Controller, + "C7", + (byte)(1 << 7), + new List { + new DigitalChannelInfo("C7", interruptCapable: false, pullUpCapable: false, pullDownCapable: false) + }); + + /// + /// Initializes all Ft23xxx pins. + /// + protected void InitAllPins() + { + // Add all our pins to the collection + AllPins.Add(D0); + AllPins.Add(D1); + AllPins.Add(D2); + AllPins.Add(D3); + AllPins.Add(D7); + AllPins.Add(D5); + AllPins.Add(D6); + AllPins.Add(D7); + + AllPins.Add(C0); + AllPins.Add(C1); + AllPins.Add(C2); + AllPins.Add(C3); + AllPins.Add(C4); + AllPins.Add(C5); + AllPins.Add(C6); + AllPins.Add(C7); + } + } +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs index c2ccc2ae15..dad1748bc3 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs @@ -1,7 +1,55 @@ -using System; +using Meadow.Hardware; +using System; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using System.Threading; +using static Meadow.Foundation.ICs.IOExpanders.Native.Ftd2xx; namespace Meadow.Foundation.ICs.IOExpanders; +public sealed class FtdiDigitalOutputPort : DigitalOutputPortBase +{ + private FtdiExpander _expander; + private bool _state; + private byte _key; + private bool _lowByte; + + internal FtdiDigitalOutputPort(FtdiExpander expander, IPin pin, IDigitalChannelInfo channel, bool initialState, OutputType initialOutputType) + : base(pin, channel, initialState, initialOutputType) + { + _expander = expander; + + if (pin is FtdiPin p) + { + _key = (byte)p.Key; + _lowByte = p.IsLowByte; + } + } + + public override bool State + { + get => _state; + set + { + byte s = _expander.GpioState; + + if (value) + { + s |= _key; + } + else + { + s &= (byte)~_key; + } + + _expander.SetGpioDirectionAndState(_lowByte, _expander.GpioDirectionMask, s); + _expander.GpioState = s; + _state = value; + } + } +} + public class Ft232HExpander : FtdiExpander { internal Ft232HExpander() @@ -9,10 +57,11 @@ internal Ft232HExpander() } } -public abstract class FtdiExpander +public abstract partial class FtdiExpander : + IPinController, // IDisposable, // IDigitalInputOutputController, -// IDigitalOutputController, + IDigitalOutputController // ISpiController, // II2cController { @@ -20,34 +69,78 @@ public abstract class FtdiExpander // public abstract II2cBus CreateI2cBus(IPin[] pins, I2cBusSpeed busSpeed); // public abstract II2cBus CreateI2cBus(IPin clock, IPin data, I2cBusSpeed busSpeed); - private FTDI.FT_DEVICE_INFO_NODE _infoNode; - private FTDI _native; + internal byte GpioDirectionMask { get; set; } + internal byte GpioState { get; set; } - public string SerialNumber => _infoNode.SerialNumber; + internal uint Index { get; private set; } + internal uint Flags { get; private set; } + internal uint ID { get; private set; } + internal uint LocID { get; private set; } + internal string SerialNumber { get; private set; } + internal string Description { get; private set; } + internal IntPtr Handle { get; private set; } - internal static FtdiExpander From(FTDI.FT_DEVICE_INFO_NODE info, FTDI native) + /// + /// The pins + /// + public PinDefinitions Pins { get; } + + internal static FtdiExpander Create( + uint index, + uint flags, + FtDeviceType deviceType, + uint id, + uint locid, + string serialNumber, + string description, + IntPtr handle) { - return info.Type switch + var device = deviceType switch { - FTDI.FT_DEVICE.FT_DEVICE_232H => new Ft232HExpander() + FtDeviceType.Ft232H => new Ft232HExpander { - _native = native, - _infoNode = info, + Index = index, + Flags = flags, + ID = id, + LocID = locid, + SerialNumber = serialNumber, + Description = description, + Handle = handle }, _ => throw new NotSupportedException(), }; + + device.Open(); + device.InitializeGpio(); + + return device; + } + + private void Open() + { + if (Handle == IntPtr.Zero) + { + Native.CheckStatus( + FT_OpenEx(LocID, Native.FT_OPEN_TYPE.FT_OPEN_BY_LOCATION, out IntPtr handle) + ); + Handle = handle; + } } internal FtdiExpander() { + Pins = new PinDefinitions(this); } private void InitializeGpio() { - CheckStatus(_native.SetBitMode(0, FTDI.FT_BIT_MODES.FT_BIT_MODE_RESET)); - CheckStatus(_native.SetBitMode(0, FTDI.FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG)); + Native.CheckStatus(Native.Ftd2xx.FT_SetBitMode(Handle, 0x00, Native.FT_BITMODE.FT_BITMODE_RESET)); + Native.CheckStatus(Native.Ftd2xx.FT_SetBitMode(Handle, 0x00, Native.FT_BITMODE.FT_BITMODE_MPSSE)); + + Thread.Sleep(50); ClearInputBuffer(); + InitializeMpsse(); } private bool CheckStatus(FTDI.FT_STATUS status) @@ -68,8 +161,8 @@ private void ClearInputBuffer() { var rxBuffer = new byte[available]; uint bytesRead = 0; - CheckStatus( - _native.Read(rxBuffer, bytesRead, ref bytesRead)); + Native.CheckStatus( + FT_Read(Handle, in rxBuffer[0], available, ref bytesRead)); } } @@ -77,21 +170,89 @@ private uint GetAvailableBytes() { uint availableBytes = 0; - CheckStatus( - _native.GetRxBytesAvailable(ref availableBytes)); + Native.CheckStatus( + FT_GetQueueStatus(Handle, ref availableBytes)); return availableBytes; } - internal void SetGpioState(bool lowByte, byte direction, byte state) + internal void SetGpioDirectionAndState(bool lowByte, byte direction, byte state) { Span outBuffer = stackalloc byte[3]; outBuffer[0] = (byte)(lowByte ? FTDI.FT_OPCODE.SetDataBitsLowByte : FTDI.FT_OPCODE.SetDataBitsHighByte); outBuffer[1] = state; //data outBuffer[2] = direction; //direction 1 == output, 0 == input + + // Console.WriteLine($"{(BitConverter.ToString(outBuffer.ToArray()))}"); + Write(outBuffer); + } + + private void InitializeMpsse() + { + Span writeBuffer = stackalloc byte[1]; + writeBuffer[0] = 0xAA; + Write(writeBuffer); + Span readBuffer = stackalloc byte[2]; + ReadInto(readBuffer); + if (!((readBuffer[0] == 0xFA) && (readBuffer[1] == 0xAA))) + { + throw new IOException($"Failed to setup device in MPSSE mode using magic 0xAA sync"); + } + + // Second with 0xAB + writeBuffer[0] = 0xAB; + Write(writeBuffer); + ReadInto(readBuffer); + if (!((readBuffer[0] == 0xFA) && (readBuffer[1] == 0xAB))) + { + throw new IOException($"Failed to setup device in MPSSE mode using magic 0xAB sync"); + } + } + + public int ReadInto(Span buffer) + { + var totalRead = 0; + uint read = 0; + + while (totalRead < buffer.Length) + { + var available = GetAvailableBytes(); + if (available > 0) + { + Native.CheckStatus( + FT_Read(Handle, in buffer[totalRead], available, ref read)); + + totalRead += (int)read; + } + } + + return totalRead; + } + + public void Write(ReadOnlySpan data) + { uint written = 0; - CheckStatus( - _native.Write(outBuffer.ToArray(), 3, ref written)); - return; + + Native.CheckStatus( + FT_Write(Handle, in MemoryMarshal.GetReference(data), (ushort)data.Length, ref written)); + } + + public IDigitalOutputPort CreateDigitalOutputPort(IPin pin, bool initialState = false, OutputType initialOutputType = OutputType.PushPull) + { + var p = pin as FtdiPin; + + // TODO: make sure the pin isn't in use + + // update the expanders direction mask to make this an output + GpioDirectionMask |= (byte)pin.Key; + + // update the direction + SetGpioDirectionAndState( + p.IsLowByte, + GpioDirectionMask, + GpioState); + + var channel = p.SupportedChannels.FirstOrDefault(channel => channel is IDigitalChannelInfo) as IDigitalChannelInfo; + return new FtdiDigitalOutputPort(this, pin, channel, initialState, initialOutputType); } } diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpanderCollection.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpanderCollection.cs index 11d33017e2..d01575c325 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpanderCollection.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpanderCollection.cs @@ -1,64 +1,79 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Text; +using static Meadow.Foundation.ICs.IOExpanders.Native.Ftd2xx; namespace Meadow.Foundation.ICs.IOExpanders; public class FtdiExpanderCollection : IEnumerable { - private FTDI _native; - private static FtdiExpanderCollection? _instance; - private List _list = new List(); + private List _expanders = new List(); /// /// Gets the number of FtdiExpander devices connected to the host machine. /// - public int Count => _list.Count; + public int Count => _expanders.Count; /// /// Gets the FtdiExpander device at the specified index in the collection. /// /// The index of the FtdiExpander device to retrieve. - public FtdiExpander this[int index] => _list[index]; + public FtdiExpander this[int index] => _expanders[index]; private FtdiExpanderCollection() { - _native = new FTDI(); - } - - private void CheckStatus(FTDI.FT_STATUS status) - { - if (status == FTDI.FT_STATUS.FT_OK) return; - - throw new Exception($"ftd2xx.dll returned '{status}'"); } public void Refresh() { - uint count = 0; + Native.CheckStatus( + Native.Ftd2xx.FT_CreateDeviceInfoList(out uint count)); - CheckStatus(_native.GetNumberOfDevices(ref count)); + _expanders.Clear(); - _list.Clear(); + ReadOnlySpan serialNumberBuffer = stackalloc byte[16]; + ReadOnlySpan descriptionBuffer = stackalloc byte[64]; - if (count > 0) + for (uint index = 0; index < count; index++) { - var infos = new FTDI.FT_DEVICE_INFO_NODE[count]; - CheckStatus(_native.GetDeviceList(infos)); - - foreach (var info in infos) + Native.CheckStatus(FT_GetDeviceInfoDetail( + index, + out uint flags, + out FtDeviceType deviceType, + out uint id, + out uint locid, + in MemoryMarshal.GetReference(serialNumberBuffer), + in MemoryMarshal.GetReference(descriptionBuffer), + out IntPtr handle)); + + switch (deviceType) { - _list.Add(FtdiExpander.From(info, _native)); + case FtDeviceType.Ft232H: + case FtDeviceType.Ft2232: + case FtDeviceType.Ft2232H: + case FtDeviceType.Ft4232H: + // valid, add to list + break; + default: + continue; } + + // no idea why the buffer isn't all zeros after the null terminator - thanks FTDI! + var serialNumber = Encoding.ASCII.GetString(serialNumberBuffer.ToArray(), 0, serialNumberBuffer.IndexOf((byte)0)); + var description = Encoding.ASCII.GetString(descriptionBuffer.ToArray(), 0, descriptionBuffer.IndexOf((byte)0)); + + _expanders.Add(FtdiExpander.Create(index, flags, deviceType, id, locid, serialNumber, description, handle)); } } /// public IEnumerator GetEnumerator() { - return _list.GetEnumerator(); + return _expanders.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/ICs.IOExpanders.Ft232h.csproj b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/ICs.IOExpanders.Ft232h.csproj index 07c4042c18..d34dc629e5 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/ICs.IOExpanders.Ft232h.csproj +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/ICs.IOExpanders.Ft232h.csproj @@ -20,11 +20,43 @@ Ft232h USB IOExpander for GPIO, I2C, SPI on Windows - + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MpsseDigitalInputPort.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseDigitalInputPort.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MpsseDigitalInputPort.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseDigitalInputPort.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MpsseI2cBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseI2cBus.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MpsseI2cBus.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseI2cBus.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs index a72e119116..5a85731d51 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs @@ -1,48 +1,41 @@ // See https://aka.ms/new-console-template for more information -using Meadow; -using Meadow.Foundation.Displays; using Meadow.Foundation.ICs.IOExpanders; -using Meadow.Foundation.Sensors.Atmospheric; using Meadow.Hardware; -using System.Diagnostics; Console.WriteLine("HELLO FROM THE WILDERNESS FT232H DRIVER!"); -FtdiExpanderCollection.Devices.Refresh(); var count = FtdiExpanderCollection.Devices.Count(); -var ft232 = FtdiExpanderCollection.Devices[0]; - -var ft232h = new Ft232h(); +var expander = FtdiExpanderCollection.Devices[0]; //await TestBME280(ft232); -await TestIli9341(ft232h); -//await TestGpio(ft232); +//await TestIli9341(ft232h); +await TestGpio(FtdiExpanderCollection.Devices); -async Task TestBME280(Ft232h expander) -{ - var bme = new Bme280(expander.CreateI2cBus()); - while (true) - { - var reading = await bme.Read(); - Debug.WriteLine($"Temp: {reading.Temperature.Value.Fahrenheit}F Humidity: {reading.Humidity.Value.Percent}%"); - await Task.Delay(1000); - } -} +//async Task TestBME280(Ft232h expander) +//{ +// var bme = new Bme280(expander.CreateI2cBus()); -async Task TestGpio(Ft232h expander) +// while (true) +// { +// var reading = await bme.Read(); +// Debug.WriteLine($"Temp: {reading.Temperature.Value.Fahrenheit}F Humidity: {reading.Humidity.Value.Percent}%"); +// await Task.Delay(1000); +// } +//} + +async Task TestGpio(IEnumerable expanders) { - var outputs = new List + + var outputs = new List(); + + foreach (var expander in expanders) { - expander.CreateDigitalOutputPort(expander.Pins.C0), - expander.CreateDigitalOutputPort(expander.Pins.C1), - expander.CreateDigitalOutputPort(expander.Pins.C2), - expander.CreateDigitalOutputPort(expander.Pins.C3), - expander.CreateDigitalOutputPort(expander.Pins.C4), - expander.CreateDigitalOutputPort(expander.Pins.C5), - expander.CreateDigitalOutputPort(expander.Pins.C6), - expander.CreateDigitalOutputPort(expander.Pins.D7), -}; + outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C0)); + outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C7)); + outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.D3)); + outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.D7)); + } var s = false; @@ -60,24 +53,24 @@ async Task TestGpio(Ft232h expander) } } -async Task TestIli9341(Ft232h expander) -{ - var ili = new Ili9341( - expander.CreateSpiBus(), - expander.CreateDigitalOutputPort(expander.Pins.C0), - expander.CreateDigitalOutputPort(expander.Pins.C2), - expander.CreateDigitalOutputPort(expander.Pins.C1), - 480, - 320 - ); +//async Task TestIli9341(Ft232h expander) +//{ +// var ili = new Ili9341( +// expander.CreateSpiBus(), +// expander.CreateDigitalOutputPort(expander.Pins.C0), +// expander.CreateDigitalOutputPort(expander.Pins.C2), +// expander.CreateDigitalOutputPort(expander.Pins.C1), +// 480, +// 320 +// ); - while (true) - { - ili.Fill(Color.Red); - await Task.Delay(1000); - ili.Fill(Color.Green); - await Task.Delay(1000); - ili.Fill(Color.Blue); - await Task.Delay(1000); - } -} \ No newline at end of file +// while (true) +// { +// ili.Fill(Color.Red); +// await Task.Delay(1000); +// ili.Fill(Color.Green); +// await Task.Delay(1000); +// ili.Fill(Color.Blue); +// await Task.Delay(1000); +// } +//} From 557ac7cb03a0c1e6344f5321ee4ab99a5816e7b3 Mon Sep 17 00:00:00 2001 From: Chris Tacke Date: Tue, 20 Feb 2024 17:24:41 -0600 Subject: [PATCH 04/11] Continuing the FT232 journey --- .../Driver/Ft232hExpander.cs | 8 + .../Driver/Ft23xx/Ft23xxI2cBus.cs | 8 +- .../Driver/FtdiDigitalOutputPort.cs | 45 ++++ .../Driver/FtdiExpander.Ft232hI2cBus.cs | 158 ++++++++++++++ .../Driver/FtdiExpander.Ft23xxxI2cBus.cs | 186 +++++++++++++++++ .../Driver/FtdiExpander.I2CBus.cs | 120 +++++++++++ .../Driver/FtdiExpander.cs | 196 ++++++++++++------ .../Ft232h_Windows_Sample.csproj | 2 + .../Samples/Ft232h_Windows_Sample/Program.cs | 10 +- .../Samples/Sgp40_Sample/MeadowApp.cs | 93 ++++----- 10 files changed, 706 insertions(+), 120 deletions(-) create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232hExpander.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiDigitalOutputPort.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hI2cBus.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft23xxxI2cBus.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.I2CBus.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232hExpander.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232hExpander.cs new file mode 100644 index 0000000000..fc4e806f9d --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232hExpander.cs @@ -0,0 +1,8 @@ +namespace Meadow.Foundation.ICs.IOExpanders; + +public partial class Ft232hExpander : FtdiExpander +{ + internal Ft232hExpander() + { + } +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxI2cBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxI2cBus.cs index 7c7492d466..1ef74cc788 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxI2cBus.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxI2cBus.cs @@ -1,8 +1,4 @@ -using Meadow.Hardware; -using System; -using System.IO; - -namespace Meadow.Foundation.ICs.IOExpanders; +namespace Meadow.Foundation.ICs.IOExpanders; /// /// Represents an I2C bus implementation using the FT23xx device. @@ -52,7 +48,7 @@ public void Read(byte peripheralAddress, Span readBuffer) if (!ack) { _device.I2cStop(); - throw new IOException($"Error reading device while setting up address"); + throw new IOException($"NACK on address. No peripheral detected."); } for (int i = 0; i < readBuffer.Length - 1; i++) diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiDigitalOutputPort.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiDigitalOutputPort.cs new file mode 100644 index 0000000000..e6d8e02911 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiDigitalOutputPort.cs @@ -0,0 +1,45 @@ +using Meadow.Hardware; + +namespace Meadow.Foundation.ICs.IOExpanders; + +public sealed class FtdiDigitalOutputPort : DigitalOutputPortBase +{ + private FtdiExpander _expander; + private bool _state; + private byte _key; + private bool _lowByte; + + internal FtdiDigitalOutputPort(FtdiExpander expander, IPin pin, IDigitalChannelInfo channel, bool initialState, OutputType initialOutputType) + : base(pin, channel, initialState, initialOutputType) + { + _expander = expander; + + if (pin is FtdiPin p) + { + _key = (byte)p.Key; + _lowByte = p.IsLowByte; + } + } + + public override bool State + { + get => _state; + set + { + byte s = _expander.GpioStateLow; + + if (value) + { + s |= _key; + } + else + { + s &= (byte)~_key; + } + + _expander.SetGpioDirectionAndState(_lowByte, _expander.GpioDirectionLow, s); + _expander.GpioStateLow = s; + _state = value; + } + } +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hI2cBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hI2cBus.cs new file mode 100644 index 0000000000..d2f180ba26 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hI2cBus.cs @@ -0,0 +1,158 @@ +using System; + +namespace Meadow.Foundation.ICs.IOExpanders; + +public abstract partial class FtdiExpander +{ + public class Ft232hI2cBus : I2CBus + { + private FtdiExpander _expander; + + internal Ft232hI2cBus(FtdiExpander expander) + { + _expander = expander; + } + + public override void Configure() + { + // Setup the clock and other elements + Span toSend = stackalloc byte[13]; + int idx = 0; + // Disable clock divide by 5 for 60Mhz master clock + toSend[idx++] = (byte)Native.FT_OPCODE.DisableClockDivideBy5; + // Turn off adaptive clocking + toSend[idx++] = (byte)Native.FT_OPCODE.TurnOffAdaptiveClocking; + // Enable 3 phase data clock, used by I2C to allow data on both clock edges + toSend[idx++] = (byte)Native.FT_OPCODE.Enable3PhaseDataClocking; + // The SK clock frequency can be worked out by below algorithm with divide by 5 set as off + // TCK period = 60MHz / (( 1 + [ (0xValueH * 256) OR 0xValueL] ) * 2) + // Command to set clock divisor + toSend[idx++] = (byte)Native.FT_OPCODE.SetClockDivisor; + uint clockDivisor = (60000 / (_expander.I2cBusFrequencyKbps * 2)) - 1; + toSend[idx++] = (byte)(clockDivisor & 0x00FF); + toSend[idx++] = (byte)((clockDivisor >> 8) & 0x00FF); + // loopback off + toSend[idx++] = (byte)Native.FT_OPCODE.DisconnectTDItoTDOforLoopback; + // Enable the FT232H's drive-zero mode with the following enable mask + toSend[idx++] = (byte)Native.FT_OPCODE.SetIOOnlyDriveOn0AndTristateOn1; + // Low byte (ADx) enables - bits 0, 1 and 2 + toSend[idx++] = 0x07; + // High byte (ACx) enables - all off + toSend[idx++] = 0x00; + // Command to set directions of lower 8 pins and force value on bits set as output + toSend[idx++] = (byte)Native.FT_OPCODE.SetDataBitsLowByte; + + // modify the GPIO state and direction without breaking other stuff + // SDA and SCL both output high(open drain) + _expander.GpioStateLow = (byte)(PinData.SDAhiSCLhi | (_expander.GpioStateLow & MaskGpio)); + _expander.GpioDirectionLow = (byte)(PinDirection.SDAoutSCLout | (_expander.GpioDirectionLow & MaskGpio)); + + toSend[idx++] = _expander.GpioStateLow; + toSend[idx++] = _expander.GpioDirectionLow; + _expander.Write(toSend); + } + + public override void Start() + { + // SDA high, SCL high + var direction = (byte)(PinDirection.SDAoutSCLout | (_expander.GpioDirectionLow & MaskGpio)); + Idle(); + Wait(6); + + // SDA lo, SCL high + var state = (byte)(PinData.SDAloSCLhi | (_expander.GpioStateLow & MaskGpio)); + _expander.SetGpioDirectionAndState(true, direction, state); + Wait(6); + + // SDA lo, SCL lo + state = (byte)(PinData.SDAloSCLlo | (_expander.GpioStateLow & MaskGpio)); + _expander.SetGpioDirectionAndState(true, direction, state); + Wait(6); + + // Release SDA + state = (byte)(PinData.SDAhiSCLlo | (_expander.GpioStateLow & MaskGpio)); + _expander.SetGpioDirectionAndState(true, direction, state); + Wait(6); + } + + public override void Stop() + { + // SDA low, SCL low + var state = (byte)(PinData.SDAloSCLlo | (_expander.GpioStateLow & MaskGpio)); + var direction = (byte)(PinDirection.SDAoutSCLout | (_expander.GpioDirectionLow & MaskGpio)); + + // SDA low, SCL high + state = (byte)(PinData.SDAloSCLhi | (_expander.GpioStateLow & MaskGpio)); + _expander.SetGpioDirectionAndState(true, direction, state); + Wait(6); + + // SDA high, SCL high + state = (byte)(PinData.SDAhiSCLhi | (_expander.GpioStateLow & MaskGpio)); + _expander.SetGpioDirectionAndState(true, direction, state); + Wait(6); + } + + public override void Idle() + { + // SDA high, SCL high + // FT232H always output due to open drain capability + var state = (byte)(PinData.SDAhiSCLhi | (_expander.GpioStateLow & MaskGpio)); + var direction = (byte)(PinDirection.SDAoutSCLout | (_expander.GpioDirectionLow & MaskGpio)); + _expander.SetGpioDirectionAndState(true, direction, state); + } + + public override TransferStatus SendDataByte(byte data) + { + Span txBuffer = stackalloc byte[10]; + Span rxBuffer = stackalloc byte[1]; + var idx = 0; + + // Just clock with one byte (0 = 1 byte) + txBuffer[idx++] = (byte)Native.FT_OPCODE.ClockDataBytesOutOnMinusVeClockMSBFirst; + txBuffer[idx++] = 0; + txBuffer[idx++] = 0; + txBuffer[idx++] = data; + // Put line back to idle (data released, clock pulled low) + _expander.GpioStateLow = (byte)(PinData.SDAhiSCLlo | (_expander.GpioStateLow & MaskGpio)); + _expander.GpioDirectionLow = (byte)(PinDirection.SDAoutSCLout | (_expander.GpioDirectionLow & MaskGpio)); + txBuffer[idx++] = (byte)Native.FT_OPCODE.SetDataBitsLowByte; + txBuffer[idx++] = _expander.GpioStateLow; + txBuffer[idx++] = _expander.GpioDirectionLow; + // Clock in (0 = 1 bit) + txBuffer[idx++] = (byte)Native.FT_OPCODE.ClockDataBitsInOnPlusVeClockMSBFirst; + txBuffer[idx++] = 0; + txBuffer[idx++] = (byte)Native.FT_OPCODE.SendImmediate; + _expander.Write(txBuffer); + _expander.ReadInto(rxBuffer); + + return (rxBuffer[0] & 0x01) == 0 ? TransferStatus.Ack : TransferStatus.Nack; + } + + public override byte ReadDataByte(bool ackAfterRead) + { + int idx = 0; + Span toSend = stackalloc byte[10]; + Span toRead = stackalloc byte[1]; + + // The behavior is a bit different for FT232H and FT2232H/FT4232H + // Read one byte + toSend[idx++] = (byte)Native.FT_OPCODE.ClockDataBytesInOnPlusVeClockMSBFirst; + toSend[idx++] = 0; + toSend[idx++] = 0; + // Send out either ack either nak + toSend[idx++] = (byte)Native.FT_OPCODE.ClockDataBitsOutOnMinusVeClockMSBFirst; + toSend[idx++] = 0; + toSend[idx++] = (byte)(ackAfterRead ? 0x00 : 0xFF); + // I2C lines back to idle state + toSend[idx++] = (byte)Native.FT_OPCODE.SetDataBitsLowByte; + _expander.GpioStateLow = (byte)(PinData.SDAhiSCLlo | (_expander.GpioStateLow & MaskGpio)); + _expander.GpioDirectionLow = (byte)(PinDirection.SDAoutSCLout | (_expander.GpioDirectionLow & MaskGpio)); + toSend[idx++] = _expander.GpioStateLow; + toSend[idx++] = _expander.GpioDirectionLow; + toSend[idx++] = (byte)Native.FT_OPCODE.SendImmediate; + _expander.Write(toSend); + _expander.ReadInto(toRead); + return toRead[0]; + } + } +} \ No newline at end of file diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft23xxxI2cBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft23xxxI2cBus.cs new file mode 100644 index 0000000000..2bcb957c8b --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft23xxxI2cBus.cs @@ -0,0 +1,186 @@ +using Meadow.Hardware; +using System; + +namespace Meadow.Foundation.ICs.IOExpanders; + +public abstract partial class FtdiExpander +{ + public class Ft23xxxI2cBus : I2CBus, II2cBus + { + private FtdiExpander _expander; + + public I2cBusSpeed BusSpeed { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + + internal Ft23xxxI2cBus(FtdiExpander expander) + { + _expander = expander; + } + + public override void Configure() + { + // Setup the clock and other elements + Span toSend = stackalloc byte[13]; + int idx = 0; + // Disable clock divide by 5 for 60Mhz master clock + toSend[idx++] = (byte)Native.FT_OPCODE.DisableClockDivideBy5; + // Turn off adaptive clocking + toSend[idx++] = (byte)Native.FT_OPCODE.TurnOffAdaptiveClocking; + // Enable 3 phase data clock, used by I2C to allow data on both clock edges + toSend[idx++] = (byte)Native.FT_OPCODE.Enable3PhaseDataClocking; + // The SK clock frequency can be worked out by below algorithm with divide by 5 set as off + // TCK period = 60MHz / (( 1 + [ (0xValueH * 256) OR 0xValueL] ) * 2) + // Command to set clock divisor + toSend[idx++] = (byte)Native.FT_OPCODE.SetClockDivisor; + uint clockDivisor = (60000 / (_expander.I2cBusFrequencyKbps * 2)) - 1; + toSend[idx++] = (byte)(clockDivisor & 0x00FF); + toSend[idx++] = (byte)((clockDivisor >> 8) & 0x00FF); + // loopback off + toSend[idx++] = (byte)Native.FT_OPCODE.DisconnectTDItoTDOforLoopback; + // Enable the FT232H's drive-zero mode with the following enable mask + toSend[idx++] = (byte)Native.FT_OPCODE.SetIOOnlyDriveOn0AndTristateOn1; + // Low byte (ADx) enables - bits 0, 1 and 2 + toSend[idx++] = 0x07; + // High byte (ACx) enables - all off + toSend[idx++] = 0x00; + // Command to set directions of lower 8 pins and force value on bits set as output + toSend[idx++] = (byte)Native.FT_OPCODE.SetDataBitsLowByte; + + // modify the GPIO state and direction without breaking other stuff + // SDA and SCL set low but as input to mimic open drain + _expander.GpioStateLow = (byte)(PinData.SDAloSCLlo | (_expander.GpioStateLow & MaskGpio)); + _expander.GpioDirectionLow = (byte)(PinDirection.SDAinSCLin | (_expander.GpioDirectionLow & MaskGpio)); + + toSend[idx++] = _expander.GpioStateLow; + toSend[idx++] = _expander.GpioDirectionLow; + _expander.Write(toSend); + } + + public override void Start() + { + // Both SDA and SCL high (setting to input simulates open drain high) + var state = (byte)(PinData.SDAloSCLlo | (_expander.GpioStateLow & MaskGpio)); + Idle(); + Wait(6); + + // SDA low, SCL high (setting to input simulates open drain high) + var direction = (byte)(0x00 | PinDirection.SDAoutSCLin | (_expander.GpioDirectionLow & 0xF8)); + _expander.SetGpioDirectionAndState(true, direction, state); + Wait(6); + + // SDA low, SCL low + direction = (byte)(0x00 | PinDirection.SDAoutSCLout | (_expander.GpioDirectionLow & 0xF8)); + _expander.SetGpioDirectionAndState(true, direction, state); + Wait(6); + + // Release SDA (setting to input simulates open drain high) + direction = (byte)(0x00 | PinDirection.SDAinSCLout | (_expander.GpioDirectionLow & 0xF8)); + _expander.SetGpioDirectionAndState(true, direction, state); + Wait(6); + } + + public override void Stop() + { + // SDA low, SCL low + var state = (byte)(PinData.SDAloSCLlo | (_expander.GpioStateLow & MaskGpio)); + var direction = (byte)(PinDirection.SDAoutSCLout | (_expander.GpioDirectionLow & MaskGpio)); + _expander.SetGpioDirectionAndState(true, direction, state); + Wait(6); + + + // SDA low, SCL high (note: setting to input simulates open drain high) + direction = (byte)(PinDirection.SDAoutSCLin | (_expander.GpioDirectionLow & MaskGpio)); + _expander.SetGpioDirectionAndState(true, direction, state); + Wait(6); + + // SDA high, SCL high (note: setting to input simulates open drain high) + direction = (byte)(PinDirection.SDAinSCLin | (_expander.GpioDirectionLow & MaskGpio)); + _expander.SetGpioDirectionAndState(true, direction, state); + Wait(6); + } + + public override void Idle() + { + // SDA high, SCL high + var state = (byte)(PinData.SDAloSCLlo | (_expander.GpioStateLow & MaskGpio)); + // FT2232H/FT4232H use input to mimic open drain + var direction = (byte)(PinDirection.SDAinSCLin | (_expander.GpioDirectionLow & MaskGpio)); + _expander.SetGpioDirectionAndState(true, direction, state); + } + + public override TransferStatus SendDataByte(byte data) + { + Span txBuffer = stackalloc byte[13]; + Span rxBuffer = stackalloc byte[1]; + var idx = 0; + + _expander.GpioStateLow = (byte)(0x00 | PinData.SDAloSCLlo | (_expander.GpioStateLow & 0xF8)); + _expander.GpioDirectionLow = (byte)(0x00 | PinDirection.SDAoutSCLout | (_expander.GpioDirectionLow & 0xF8));// back to output + txBuffer[idx++] = (byte)Native.FT_OPCODE.SetDataBitsLowByte; // Command - set low byte + txBuffer[idx++] = _expander.GpioStateLow; // Set the values + txBuffer[idx++] = _expander.GpioDirectionLow; // Set the directions + // clock out one byte + txBuffer[idx++] = (byte)Native.FT_OPCODE.ClockDataBytesOutOnMinusVeClockMSBFirst; // clock data byte out + txBuffer[idx++] = 0x00; // + txBuffer[idx++] = 0x00; // Data length of 0x0000 means 1 byte data to clock in + txBuffer[idx++] = data; // Byte to send + + // Put line back to idle (data released, clock pulled low) so that sensor can drive data line + _expander.GpioStateLow = (byte)(0x00 | PinData.SDAloSCLlo | (_expander.GpioStateLow & 0xF8)); + _expander.GpioDirectionLow = (byte)(0x00 | PinDirection.SDAinSCLout | (_expander.GpioDirectionLow & 0xF8)); // make data input + txBuffer[idx++] = 0x80; // Command - set low byte + txBuffer[idx++] = _expander.GpioStateLow; // Set the values + txBuffer[idx++] = _expander.GpioDirectionLow; // Set the directions + + // CLOCK IN ACK (0 == 1 bit) + txBuffer[idx++] = (byte)Native.FT_OPCODE.ClockDataBitsInOnPlusVeClockMSBFirst; // clock data byte in + txBuffer[idx++] = 0x00; // Length of 0 means 1 bit + + // This command then tells the MPSSE to send any results gathered (in this case the ack bit) back immediately + txBuffer[idx++] = (byte)Native.FT_OPCODE.SendImmediate; // ' Send answer back immediate command + _expander.Write(txBuffer); + _expander.ReadInto(rxBuffer); + + return (rxBuffer[0] & 0x01) == 0 ? TransferStatus.Ack : TransferStatus.Nack; + } + + public override byte ReadDataByte(bool ackAfterRead) + { + int idx = 0; + Span toSend = stackalloc byte[16]; + Span toRead = stackalloc byte[1]; + + // Make sure no open gain + _expander.GpioStateLow = (byte)(PinData.SDAloSCLlo | (_expander.GpioStateLow & MaskGpio)); + _expander.GpioDirectionLow = (byte)(PinDirection.SDAinSCLout | (_expander.GpioDirectionLow & MaskGpio)); + toSend[idx++] = (byte)Native.FT_OPCODE.SetDataBitsLowByte; + toSend[idx++] = _expander.GpioStateLow; + toSend[idx++] = _expander.GpioDirectionLow; + // Read one byte + toSend[idx++] = (byte)Native.FT_OPCODE.ClockDataBytesInOnPlusVeClockMSBFirst; + toSend[idx++] = 0; + toSend[idx++] = 0; + // Change direction + _expander.GpioStateLow = (byte)(PinData.SDAloSCLlo | (_expander.GpioStateLow & MaskGpio)); + _expander.GpioDirectionLow = (byte)(PinDirection.SDAoutSCLout | (_expander.GpioDirectionLow & MaskGpio)); + toSend[idx++] = (byte)Native.FT_OPCODE.SetDataBitsLowByte; + toSend[idx++] = _expander.GpioStateLow; + toSend[idx++] = _expander.GpioDirectionLow; + // Send out either ack either nak + toSend[idx++] = (byte)Native.FT_OPCODE.ClockDataBitsOutOnMinusVeClockMSBFirst; + toSend[idx++] = 0; + toSend[idx++] = (byte)(ackAfterRead ? 0x00 : 0xFF); + // I2C lines back to idle state + toSend[idx++] = (byte)Native.FT_OPCODE.SetDataBitsLowByte; + _expander.GpioStateLow = (byte)(PinData.SDAhiSCLlo | (_expander.GpioStateLow & MaskGpio)); + _expander.GpioDirectionLow = (byte)(PinDirection.SDAinSCLout | (_expander.GpioDirectionLow & MaskGpio)); + toSend[idx++] = _expander.GpioStateLow; + toSend[idx++] = _expander.GpioDirectionLow; + + // And ask it right away + toSend[idx++] = (byte)Native.FT_OPCODE.SendImmediate; + _expander.Write(toSend); + _expander.ReadInto(toRead); + return toRead[0]; + } + } +} \ No newline at end of file diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.I2CBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.I2CBus.cs new file mode 100644 index 0000000000..70c604f804 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.I2CBus.cs @@ -0,0 +1,120 @@ +using Meadow.Hardware; +using System; +using System.IO; +using System.Threading; + +namespace Meadow.Foundation.ICs.IOExpanders; + +public abstract partial class FtdiExpander +{ + public abstract class I2CBus : II2cBus + { + public I2cBusSpeed BusSpeed { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + + private SpinWait _spinWait = new(); + + public abstract void Configure(); + public abstract void Start(); + public abstract void Stop(); + public abstract void Idle(); + public abstract TransferStatus SendDataByte(byte data); + public abstract byte ReadDataByte(bool ackAfterRead); + protected const byte NumberCycles = 6; + protected const byte MaskGpio = 0xF8; + + public enum TransferStatus + { + Ack = 0, + Nack + } + + protected static class PinDirection + { + public const byte SDAinSCLin = 0x00; + public const byte SDAinSCLout = 0x01; + public const byte SDAoutSCLin = 0x02; + public const byte SDAoutSCLout = 0x03; + } + + protected static class PinData + { + public const byte SDAloSCLhi = 0x01; + public const byte SDAhiSCLhi = 0x03; + public const byte SDAloSCLlo = 0x00; + public const byte SDAhiSCLlo = 0x02; + } + + protected void Wait(int spinCount) + { + for (var i = 0; i < spinCount; i++) + { + _spinWait.SpinOnce(); + } + } + + private TransferStatus SendAddressByte(byte address, bool isRead) + { + // Set address for read or write + address <<= 1; + if (isRead == true) + { + address |= 0x01; + } + + return SendDataByte(address); + } + + public void Write(byte peripheralAddress, Span writeBuffer) + { + Start(); + if (SendAddressByte(peripheralAddress, false) == TransferStatus.Nack) + { + Stop(); + throw new IOException($"Error writing device while setting up address"); + } + + for (int i = 0; i < writeBuffer.Length; i++) + { + if (SendDataByte(writeBuffer[i]) == TransferStatus.Nack) + { + Stop(); + throw new IOException($"Error writing device on byte {i}"); + } + } + + Stop(); + } + + public void Dispose() + { + } + + public void Exchange(byte peripheralAddress, Span writeBuffer, Span readBuffer) + { + Write(peripheralAddress, writeBuffer); + Read(peripheralAddress, readBuffer); + } + + public void Read(byte peripheralAddress, Span readBuffer) + { + Start(); + if (SendAddressByte(peripheralAddress, true) == TransferStatus.Nack) + { + Stop(); + throw new IOException($"Error reading device while setting up address"); + } + + for (int i = 0; i < readBuffer.Length - 1; i++) + { + readBuffer[i] = ReadDataByte(true); + } + + if (readBuffer.Length > 0) + { + readBuffer[readBuffer.Length - 1] = ReadDataByte(false); + } + + Stop(); + } + } +} \ No newline at end of file diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs index dad1748bc3..66b26a6692 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs @@ -8,69 +8,37 @@ namespace Meadow.Foundation.ICs.IOExpanders; -public sealed class FtdiDigitalOutputPort : DigitalOutputPortBase +public abstract partial class FtdiExpander : + IPinController, +// IDisposable, +// IDigitalInputOutputController, + IDigitalOutputController, +// ISpiController, + II2cController { - private FtdiExpander _expander; - private bool _state; - private byte _key; - private bool _lowByte; - - internal FtdiDigitalOutputPort(FtdiExpander expander, IPin pin, IDigitalChannelInfo channel, bool initialState, OutputType initialOutputType) - : base(pin, channel, initialState, initialOutputType) + public II2cBus CreateI2cBus(int busNumber = 1, I2cBusSpeed busSpeed = I2cBusSpeed.Standard) { - _expander = expander; - - if (pin is FtdiPin p) - { - _key = (byte)p.Key; - _lowByte = p.IsLowByte; - } + // TODO: depends on part + // TODO: make sure no SPI is in use + var bus = new Ft232hI2cBus(this); + bus.Configure(); + return bus; } - public override bool State + public II2cBus CreateI2cBus(IPin[] pins, I2cBusSpeed busSpeed) { - get => _state; - set - { - byte s = _expander.GpioState; - - if (value) - { - s |= _key; - } - else - { - s &= (byte)~_key; - } - - _expander.SetGpioDirectionAndState(_lowByte, _expander.GpioDirectionMask, s); - _expander.GpioState = s; - _state = value; - } + return CreateI2cBus(1); } -} -public class Ft232HExpander : FtdiExpander -{ - internal Ft232HExpander() + public II2cBus CreateI2cBus(IPin clock, IPin data, I2cBusSpeed busSpeed) { + return CreateI2cBus(1); } -} - -public abstract partial class FtdiExpander : - IPinController, -// IDisposable, -// IDigitalInputOutputController, - IDigitalOutputController -// ISpiController, -// II2cController -{ - // public abstract II2cBus CreateI2cBus(int busNumber = 1, I2cBusSpeed busSpeed = I2cBusSpeed.Standard); - // public abstract II2cBus CreateI2cBus(IPin[] pins, I2cBusSpeed busSpeed); - // public abstract II2cBus CreateI2cBus(IPin clock, IPin data, I2cBusSpeed busSpeed); - internal byte GpioDirectionMask { get; set; } - internal byte GpioState { get; set; } + internal byte GpioDirectionLow { get; set; } + internal byte GpioStateLow { get; set; } + internal byte GpioDirectionHigh { get; set; } + internal byte GpioStateHigh { get; set; } internal uint Index { get; private set; } internal uint Flags { get; private set; } @@ -80,6 +48,8 @@ public abstract partial class FtdiExpander : internal string Description { get; private set; } internal IntPtr Handle { get; private set; } + internal uint I2cBusFrequencyKbps { get; private set; } = 400; + /// /// The pins /// @@ -95,9 +65,9 @@ internal static FtdiExpander Create( string description, IntPtr handle) { - var device = deviceType switch + var expander = deviceType switch { - FtDeviceType.Ft232H => new Ft232HExpander + FtDeviceType.Ft232H => new Ft232hExpander { Index = index, Flags = flags, @@ -110,10 +80,10 @@ internal static FtdiExpander Create( _ => throw new NotSupportedException(), }; - device.Open(); - device.InitializeGpio(); + expander.Open(); + expander.InitializeGpio(); - return device; + return expander; } private void Open() @@ -134,6 +104,14 @@ internal FtdiExpander() private void InitializeGpio() { + // for I2C: + Native.Ftd2xx.FT_SetTimeouts(Handle, 5000, 5000); + Native.Ftd2xx.FT_SetLatencyTimer(Handle, 16); + Native.Ftd2xx.FT_SetFlowControl(Handle, Native.FT_FLOWCONTROL.FT_FLOW_RTS_CTS, 0, 0); + //ftStatus |= myFtdiDevice.SetTimeouts(5000, 5000); + //ftStatus |= myFtdiDevice.SetLatency(16); + //ftStatus |= myFtdiDevice.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x00, 0x00); + Native.CheckStatus(Native.Ftd2xx.FT_SetBitMode(Handle, 0x00, Native.FT_BITMODE.FT_BITMODE_RESET)); Native.CheckStatus(Native.Ftd2xx.FT_SetBitMode(Handle, 0x00, Native.FT_BITMODE.FT_BITMODE_MPSSE)); @@ -183,6 +161,17 @@ internal void SetGpioDirectionAndState(bool lowByte, byte direction, byte state) outBuffer[1] = state; //data outBuffer[2] = direction; //direction 1 == output, 0 == input + if (lowByte) + { + GpioStateLow = state; + GpioDirectionLow = direction; + } + else + { + GpioStateHigh = state; + GpioDirectionHigh = direction; + } + // Console.WriteLine($"{(BitConverter.ToString(outBuffer.ToArray()))}"); Write(outBuffer); } @@ -229,7 +218,7 @@ public int ReadInto(Span buffer) return totalRead; } - public void Write(ReadOnlySpan data) + internal void Write(ReadOnlySpan data) { uint written = 0; @@ -237,20 +226,95 @@ public void Write(ReadOnlySpan data) FT_Write(Handle, in MemoryMarshal.GetReference(data), (ushort)data.Length, ref written)); } + /* + private byte Receive_Data(uint BytesToRead) + { + uint NumBytesInQueue = 0; + uint QueueTimeOut = 0; + uint Buffer1Index = 0; + uint Buffer2Index = 0; + uint TotalBytesRead = 0; + bool QueueTimeoutFlag = false; + uint NumBytesRxd = 0; + + // Keep looping until all requested bytes are received or we've tried 5000 times (value can be chosen as required) + while ((TotalBytesRead < BytesToRead) && (QueueTimeoutFlag == false)) + { + ftStatus = myFtdiDevice.GetRxBytesAvailable(ref NumBytesInQueue); // Check bytes available + + if ((NumBytesInQueue > 0) && (ftStatus == FTDI.FT_STATUS.FT_OK)) + { + ftStatus = myFtdiDevice.Read(InputBuffer, NumBytesInQueue, ref NumBytesRxd); // if any available read them + + if ((NumBytesInQueue == NumBytesRxd) && (ftStatus == FTDI.FT_STATUS.FT_OK)) + { + Buffer1Index = 0; + + while (Buffer1Index < NumBytesRxd) + { + InputBuffer2[Buffer2Index] = InputBuffer[Buffer1Index]; // copy into main overall application buffer + Buffer1Index++; + Buffer2Index++; + } + TotalBytesRead = TotalBytesRead + NumBytesRxd; // Keep track of total + } + else + return 1; + + QueueTimeOut++; + if (QueueTimeOut == 5000) + QueueTimeoutFlag = true; + else + Thread.Sleep(0); // Avoids running Queue status checks back to back + } + } + // returning globals NumBytesRead and the buffer InputBuffer2 + NumBytesRead = TotalBytesRead; + + if (QueueTimeoutFlag == true) + return 1; + else + return 0; + } + */ + public IDigitalOutputPort CreateDigitalOutputPort(IPin pin, bool initialState = false, OutputType initialOutputType = OutputType.PushPull) { var p = pin as FtdiPin; // TODO: make sure the pin isn't in use - // update the expanders direction mask to make this an output - GpioDirectionMask |= (byte)pin.Key; + if (p.IsLowByte) + { + // update the expanders direction mask to make this an output + GpioDirectionLow |= (byte)pin.Key; - // update the direction - SetGpioDirectionAndState( - p.IsLowByte, - GpioDirectionMask, - GpioState); + // update initial state + if (initialState) + { + GpioStateLow |= (byte)pin.Key; + } + + SetGpioDirectionAndState( + p.IsLowByte, + GpioDirectionLow, + GpioStateLow); + } + else + { + GpioDirectionHigh |= (byte)pin.Key; + + // update initial state + if (initialState) + { + GpioStateHigh |= (byte)pin.Key; + } + + SetGpioDirectionAndState( + p.IsLowByte, + GpioDirectionHigh, + GpioStateHigh); + } var channel = p.SupportedChannels.FirstOrDefault(channel => channel is IDigitalChannelInfo) as IDigitalChannelInfo; return new FtdiDigitalOutputPort(this, pin, channel, initialState, initialOutputType); diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Ft232h_Windows_Sample.csproj b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Ft232h_Windows_Sample.csproj index b50f7e4a35..918d1fe541 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Ft232h_Windows_Sample.csproj +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Ft232h_Windows_Sample.csproj @@ -16,7 +16,9 @@ + + diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs index 5a85731d51..303a0e6f7f 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs @@ -1,5 +1,6 @@ // See https://aka.ms/new-console-template for more information using Meadow.Foundation.ICs.IOExpanders; +using Meadow.Foundation.Sensors.Temperature; using Meadow.Hardware; Console.WriteLine("HELLO FROM THE WILDERNESS FT232H DRIVER!"); @@ -9,7 +10,8 @@ //await TestBME280(ft232); //await TestIli9341(ft232h); -await TestGpio(FtdiExpanderCollection.Devices); +//await TestGpio(FtdiExpanderCollection.Devices); +await TestI2C(FtdiExpanderCollection.Devices[0]); //async Task TestBME280(Ft232h expander) @@ -24,6 +26,12 @@ // } //} +async Task TestI2C(FtdiExpander expander) +{ + var sensor = new Pct2075(expander.CreateI2cBus()); + await sensor.Read(); +} + async Task TestGpio(IEnumerable expanders) { diff --git a/Source/Meadow.Foundation.Peripherals/Sensors.Atmospheric.Sgp40/Samples/Sgp40_Sample/MeadowApp.cs b/Source/Meadow.Foundation.Peripherals/Sensors.Atmospheric.Sgp40/Samples/Sgp40_Sample/MeadowApp.cs index 49e8cc954a..042c1ccfff 100644 --- a/Source/Meadow.Foundation.Peripherals/Sensors.Atmospheric.Sgp40/Samples/Sgp40_Sample/MeadowApp.cs +++ b/Source/Meadow.Foundation.Peripherals/Sensors.Atmospheric.Sgp40/Samples/Sgp40_Sample/MeadowApp.cs @@ -4,67 +4,66 @@ using System; using System.Threading.Tasks; -namespace BasicSensors.Atmospheric.SI7021_Sample +namespace BasicSensors.Atmospheric.Sgp40_Sample; + +public class MeadowApp : App { - public class MeadowApp : App - { - // + // - Sgp40? sensor; + private Sgp40? sensor; - public override Task Initialize() - { - Resolver.Log.Info("Initializing..."); + public override Task Initialize() + { + Resolver.Log.Info("Initializing..."); + + sensor = new Sgp40(Device.CreateI2cBus()); - sensor = new Sgp40(Device.CreateI2cBus()); + Resolver.Log.Info($"Sensor SN: {sensor.SerialNumber:x6}"); - Resolver.Log.Info($"Sensor SN: {sensor.SerialNumber:x6}"); + if (sensor.RunSelfTest()) + { + Resolver.Log.Info("Self test successful"); + } + else + { + Resolver.Log.Warn("Self test failed"); + } - if (sensor.RunSelfTest()) + var consumer = Sgp40.CreateObserver( + handler: result => { - Resolver.Log.Info("Self test successful"); - } - else + Resolver.Log.Info($"Observer: VOC changed by threshold; new index: {result.New}"); + }, + filter: result => { - Resolver.Log.Warn("Self test failed"); + return Math.Abs(result.New - result.Old ?? 0) > 10; } + ); + sensor.Subscribe(consumer); - var consumer = Sgp40.CreateObserver( - handler: result => - { - Resolver.Log.Info($"Observer: VOC changed by threshold; new index: {result.New}"); - }, - filter: result => - { - return Math.Abs(result.New - result.Old ?? 0) > 10; - } - ); - sensor.Subscribe(consumer); - - sensor.Updated += (sender, result) => - { - Resolver.Log.Info($" VOC: {result.New}"); - }; - - return base.Initialize(); - } - - public override async Task Run() + sensor.Updated += (sender, result) => { - await ReadConditions(); + Resolver.Log.Info($" VOC: {result.New}"); + }; - sensor?.StartUpdating(TimeSpan.FromSeconds(1)); - } + return base.Initialize(); + } - async Task ReadConditions() - { - if (sensor == null) { return; } + public override async Task Run() + { + await ReadConditions(); - var result = await sensor.Read(); - Resolver.Log.Info("Initial Readings:"); - Resolver.Log.Info($" Temperature: {result}"); - } + sensor?.StartUpdating(TimeSpan.FromSeconds(1)); + } - // + private async Task ReadConditions() + { + if (sensor == null) { return; } + + var result = await sensor.Read(); + Resolver.Log.Info("Initial Readings:"); + Resolver.Log.Info($" Temperature: {result}"); } + + // } \ No newline at end of file From de5e0b2ac99e4c48006ae07747ab4f7e1e1729e4 Mon Sep 17 00:00:00 2001 From: Chris Tacke Date: Wed, 21 Feb 2024 09:42:44 -0600 Subject: [PATCH 05/11] continued FT232 work --- .../Driver/FtdiExpander.Ft232hI2cBus.cs | 41 ++++++++++++------- .../Driver/FtdiExpander.Ft23xxxI2cBus.cs | 8 ++-- .../Driver/FtdiExpander.cs | 25 ++++++----- .../Samples/Ft232h_Windows_Sample/Program.cs | 5 ++- .../Driver/Bme280.cs | 2 +- 5 files changed, 48 insertions(+), 33 deletions(-) diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hI2cBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hI2cBus.cs index d2f180ba26..4ee425147e 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hI2cBus.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hI2cBus.cs @@ -1,4 +1,5 @@ using System; +using System.Threading; namespace Meadow.Foundation.ICs.IOExpanders; @@ -16,7 +17,7 @@ internal Ft232hI2cBus(FtdiExpander expander) public override void Configure() { // Setup the clock and other elements - Span toSend = stackalloc byte[13]; + Span toSend = stackalloc byte[10]; int idx = 0; // Disable clock divide by 5 for 60Mhz master clock toSend[idx++] = (byte)Native.FT_OPCODE.DisableClockDivideBy5; @@ -24,6 +25,7 @@ public override void Configure() toSend[idx++] = (byte)Native.FT_OPCODE.TurnOffAdaptiveClocking; // Enable 3 phase data clock, used by I2C to allow data on both clock edges toSend[idx++] = (byte)Native.FT_OPCODE.Enable3PhaseDataClocking; + // The SK clock frequency can be worked out by below algorithm with divide by 5 set as off // TCK period = 60MHz / (( 1 + [ (0xValueH * 256) OR 0xValueL] ) * 2) // Command to set clock divisor @@ -31,25 +33,34 @@ public override void Configure() uint clockDivisor = (60000 / (_expander.I2cBusFrequencyKbps * 2)) - 1; toSend[idx++] = (byte)(clockDivisor & 0x00FF); toSend[idx++] = (byte)((clockDivisor >> 8) & 0x00FF); + // loopback off toSend[idx++] = (byte)Native.FT_OPCODE.DisconnectTDItoTDOforLoopback; + + // DEV NOTE (21 Fed 20204): + // the following is in FTDI's FT232H samples, but makes the clock signal an ugly sawtooth instead of squares + // Enable the FT232H's drive-zero mode with the following enable mask - toSend[idx++] = (byte)Native.FT_OPCODE.SetIOOnlyDriveOn0AndTristateOn1; + //toSend[idx++] = (byte)Native.FT_OPCODE.SetIOOnlyDriveOn0AndTristateOn1; // Low byte (ADx) enables - bits 0, 1 and 2 - toSend[idx++] = 0x07; + //toSend[idx++] = 0x07; // High byte (ACx) enables - all off - toSend[idx++] = 0x00; - // Command to set directions of lower 8 pins and force value on bits set as output - toSend[idx++] = (byte)Native.FT_OPCODE.SetDataBitsLowByte; + //toSend[idx++] = 0x00; + // modify the GPIO state and direction without breaking other stuff // SDA and SCL both output high(open drain) + toSend[idx++] = (byte)Native.FT_OPCODE.SetDataBitsLowByte; _expander.GpioStateLow = (byte)(PinData.SDAhiSCLhi | (_expander.GpioStateLow & MaskGpio)); _expander.GpioDirectionLow = (byte)(PinDirection.SDAoutSCLout | (_expander.GpioDirectionLow & MaskGpio)); - toSend[idx++] = _expander.GpioStateLow; toSend[idx++] = _expander.GpioDirectionLow; + _expander.Write(toSend); + + Thread.Sleep(20); + Idle(); + Thread.Sleep(30); } public override void Start() @@ -57,22 +68,22 @@ public override void Start() // SDA high, SCL high var direction = (byte)(PinDirection.SDAoutSCLout | (_expander.GpioDirectionLow & MaskGpio)); Idle(); - Wait(6); + Wait(2); // SDA lo, SCL high var state = (byte)(PinData.SDAloSCLhi | (_expander.GpioStateLow & MaskGpio)); _expander.SetGpioDirectionAndState(true, direction, state); - Wait(6); + Wait(2); // SDA lo, SCL lo state = (byte)(PinData.SDAloSCLlo | (_expander.GpioStateLow & MaskGpio)); _expander.SetGpioDirectionAndState(true, direction, state); - Wait(6); + Wait(2); // Release SDA - state = (byte)(PinData.SDAhiSCLlo | (_expander.GpioStateLow & MaskGpio)); - _expander.SetGpioDirectionAndState(true, direction, state); - Wait(6); + // state = (byte)(PinData.SDAhiSCLlo | (_expander.GpioStateLow & MaskGpio)); + // _expander.SetGpioDirectionAndState(true, direction, state); + // Wait(6); } public override void Stop() @@ -113,8 +124,8 @@ public override TransferStatus SendDataByte(byte data) txBuffer[idx++] = 0; txBuffer[idx++] = data; // Put line back to idle (data released, clock pulled low) - _expander.GpioStateLow = (byte)(PinData.SDAhiSCLlo | (_expander.GpioStateLow & MaskGpio)); - _expander.GpioDirectionLow = (byte)(PinDirection.SDAoutSCLout | (_expander.GpioDirectionLow & MaskGpio)); + _expander.GpioStateLow = (byte)(PinData.SDAloSCLlo | (_expander.GpioStateLow & MaskGpio)); + _expander.GpioDirectionLow = (byte)(PinDirection.SDAinSCLout | (_expander.GpioDirectionLow & MaskGpio)); txBuffer[idx++] = (byte)Native.FT_OPCODE.SetDataBitsLowByte; txBuffer[idx++] = _expander.GpioStateLow; txBuffer[idx++] = _expander.GpioDirectionLow; diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft23xxxI2cBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft23xxxI2cBus.cs index 2bcb957c8b..1970ed4335 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft23xxxI2cBus.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft23xxxI2cBus.cs @@ -19,7 +19,7 @@ internal Ft23xxxI2cBus(FtdiExpander expander) public override void Configure() { // Setup the clock and other elements - Span toSend = stackalloc byte[13]; + Span toSend = stackalloc byte[10]; int idx = 0; // Disable clock divide by 5 for 60Mhz master clock toSend[idx++] = (byte)Native.FT_OPCODE.DisableClockDivideBy5; @@ -37,11 +37,11 @@ public override void Configure() // loopback off toSend[idx++] = (byte)Native.FT_OPCODE.DisconnectTDItoTDOforLoopback; // Enable the FT232H's drive-zero mode with the following enable mask - toSend[idx++] = (byte)Native.FT_OPCODE.SetIOOnlyDriveOn0AndTristateOn1; + // toSend[idx++] = (byte)Native.FT_OPCODE.SetIOOnlyDriveOn0AndTristateOn1; // Low byte (ADx) enables - bits 0, 1 and 2 - toSend[idx++] = 0x07; + // toSend[idx++] = 0x07; // High byte (ACx) enables - all off - toSend[idx++] = 0x00; + // toSend[idx++] = 0x00; // Command to set directions of lower 8 pins and force value on bits set as output toSend[idx++] = (byte)Native.FT_OPCODE.SetDataBitsLowByte; diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs index 66b26a6692..abfe6aa828 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs @@ -104,18 +104,20 @@ internal FtdiExpander() private void InitializeGpio() { - // for I2C: - Native.Ftd2xx.FT_SetTimeouts(Handle, 5000, 5000); - Native.Ftd2xx.FT_SetLatencyTimer(Handle, 16); - Native.Ftd2xx.FT_SetFlowControl(Handle, Native.FT_FLOWCONTROL.FT_FLOW_RTS_CTS, 0, 0); - //ftStatus |= myFtdiDevice.SetTimeouts(5000, 5000); - //ftStatus |= myFtdiDevice.SetLatency(16); - //ftStatus |= myFtdiDevice.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x00, 0x00); + Native.FT_STATUS status; + status = Native.Ftd2xx.FT_SetUSBParameters(Handle, 65536, 65536); // Set USB request transfer sizes + status |= Native.Ftd2xx.FT_SetChars(Handle, 0, 0, 0, 0); // Disable event and error characters + status |= Native.Ftd2xx.FT_SetTimeouts(Handle, 5000, 5000); // Set the read and write timeouts to 5 seconds + status |= Native.Ftd2xx.FT_SetLatencyTimer(Handle, 16); // Keep the latency timer at default of 16ms - Native.CheckStatus(Native.Ftd2xx.FT_SetBitMode(Handle, 0x00, Native.FT_BITMODE.FT_BITMODE_RESET)); - Native.CheckStatus(Native.Ftd2xx.FT_SetBitMode(Handle, 0x00, Native.FT_BITMODE.FT_BITMODE_MPSSE)); + status |= Native.Ftd2xx.FT_SetFlowControl(Handle, Native.FT_FLOWCONTROL.FT_FLOW_RTS_CTS, 0, 0); - Thread.Sleep(50); + status |= Native.Ftd2xx.FT_SetBitMode(Handle, 0x00, Native.FT_BITMODE.FT_BITMODE_RESET); // Reset the mode to whatever is set in EEPROM + status |= Native.Ftd2xx.FT_SetBitMode(Handle, 0x00, Native.FT_BITMODE.FT_BITMODE_MPSSE); // Enable MPSSE mode + + Native.CheckStatus(status); + + Thread.Sleep(50); // the FTDI C example does this, so we keep it ClearInputBuffer(); InitializeMpsse(); @@ -178,6 +180,7 @@ internal void SetGpioDirectionAndState(bool lowByte, byte direction, byte state) private void InitializeMpsse() { + // Synchronise the MPSSE by sending bad command AA to it Span writeBuffer = stackalloc byte[1]; writeBuffer[0] = 0xAA; Write(writeBuffer); @@ -188,7 +191,7 @@ private void InitializeMpsse() throw new IOException($"Failed to setup device in MPSSE mode using magic 0xAA sync"); } - // Second with 0xAB + // Synchronise the MPSSE by sending bad command AB to it writeBuffer[0] = 0xAB; Write(writeBuffer); ReadInto(readBuffer); diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs index 303a0e6f7f..ab0d8188e9 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs @@ -1,6 +1,6 @@ // See https://aka.ms/new-console-template for more information using Meadow.Foundation.ICs.IOExpanders; -using Meadow.Foundation.Sensors.Temperature; +using Meadow.Foundation.Sensors.Atmospheric; using Meadow.Hardware; Console.WriteLine("HELLO FROM THE WILDERNESS FT232H DRIVER!"); @@ -28,7 +28,8 @@ async Task TestI2C(FtdiExpander expander) { - var sensor = new Pct2075(expander.CreateI2cBus()); + var sensor = new Bme280(expander.CreateI2cBus()); + //var sensor = new Pct2075(expander.CreateI2cBus()); await sensor.Read(); } diff --git a/Source/Meadow.Foundation.Peripherals/Sensors.Atmospheric.Bme280/Driver/Bme280.cs b/Source/Meadow.Foundation.Peripherals/Sensors.Atmospheric.Bme280/Driver/Bme280.cs index f5ce9cc992..702a5db41f 100644 --- a/Source/Meadow.Foundation.Peripherals/Sensors.Atmospheric.Bme280/Driver/Bme280.cs +++ b/Source/Meadow.Foundation.Peripherals/Sensors.Atmospheric.Bme280/Driver/Bme280.cs @@ -148,7 +148,7 @@ public SpiClockConfiguration.Mode SpiBusMode /// Initializes a new instance of the BME280 class /// /// I2C Bus to use for communicating with the sensor - /// I2C address of the sensor (default = 0x77) + /// I2C address of the sensor (default = 0x76) public Bme280(II2cBus i2cBus, byte address = (byte)Addresses.Default) { bme280Comms = new I2cCommunications(i2cBus, address); From cfa500b25c11fe8ebb5fd5977458138beac6a08d Mon Sep 17 00:00:00 2001 From: Chris Tacke Date: Wed, 21 Feb 2024 11:54:29 -0600 Subject: [PATCH 06/11] first I2C success with FT232 --- .../Driver/FtdiExpander.Ft232hI2cBus.cs | 8 ++++---- .../Driver/FtdiExpander.I2CBus.cs | 12 +++++++++-- .../Driver/FtdiExpander.cs | 2 +- .../Samples/Ft232h_Windows_Sample/Program.cs | 20 ++++++++++++++++--- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hI2cBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hI2cBus.cs index 4ee425147e..3415a36ffb 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hI2cBus.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hI2cBus.cs @@ -68,17 +68,17 @@ public override void Start() // SDA high, SCL high var direction = (byte)(PinDirection.SDAoutSCLout | (_expander.GpioDirectionLow & MaskGpio)); Idle(); - Wait(2); + // Wait(2); // SDA lo, SCL high var state = (byte)(PinData.SDAloSCLhi | (_expander.GpioStateLow & MaskGpio)); _expander.SetGpioDirectionAndState(true, direction, state); - Wait(2); + // Wait(2); // SDA lo, SCL lo state = (byte)(PinData.SDAloSCLlo | (_expander.GpioStateLow & MaskGpio)); _expander.SetGpioDirectionAndState(true, direction, state); - Wait(2); + // Wait(2); // Release SDA // state = (byte)(PinData.SDAhiSCLlo | (_expander.GpioStateLow & MaskGpio)); @@ -125,7 +125,7 @@ public override TransferStatus SendDataByte(byte data) txBuffer[idx++] = data; // Put line back to idle (data released, clock pulled low) _expander.GpioStateLow = (byte)(PinData.SDAloSCLlo | (_expander.GpioStateLow & MaskGpio)); - _expander.GpioDirectionLow = (byte)(PinDirection.SDAinSCLout | (_expander.GpioDirectionLow & MaskGpio)); + _expander.GpioDirectionLow = (byte)(PinDirection.SDAoutSCLout | (_expander.GpioDirectionLow & MaskGpio)); txBuffer[idx++] = (byte)Native.FT_OPCODE.SetDataBitsLowByte; txBuffer[idx++] = _expander.GpioStateLow; txBuffer[idx++] = _expander.GpioDirectionLow; diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.I2CBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.I2CBus.cs index 70c604f804..6adb107c05 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.I2CBus.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.I2CBus.cs @@ -65,6 +65,11 @@ private TransferStatus SendAddressByte(byte address, bool isRead) } public void Write(byte peripheralAddress, Span writeBuffer) + { + Write(peripheralAddress, writeBuffer, true); + } + + public void Write(byte peripheralAddress, Span writeBuffer, bool terminatingStop) { Start(); if (SendAddressByte(peripheralAddress, false) == TransferStatus.Nack) @@ -82,7 +87,10 @@ public void Write(byte peripheralAddress, Span writeBuffer) } } - Stop(); + if (terminatingStop) + { + Stop(); + } } public void Dispose() @@ -91,7 +99,7 @@ public void Dispose() public void Exchange(byte peripheralAddress, Span writeBuffer, Span readBuffer) { - Write(peripheralAddress, writeBuffer); + Write(peripheralAddress, writeBuffer, false); Read(peripheralAddress, readBuffer); } diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs index abfe6aa828..031985ccbb 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs @@ -48,7 +48,7 @@ public II2cBus CreateI2cBus(IPin clock, IPin data, I2cBusSpeed busSpeed) internal string Description { get; private set; } internal IntPtr Handle { get; private set; } - internal uint I2cBusFrequencyKbps { get; private set; } = 400; + internal uint I2cBusFrequencyKbps { get; private set; } = 100; /// /// The pins diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs index ab0d8188e9..ab3a72c826 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs @@ -1,7 +1,8 @@ // See https://aka.ms/new-console-template for more information using Meadow.Foundation.ICs.IOExpanders; -using Meadow.Foundation.Sensors.Atmospheric; +using Meadow.Foundation.Sensors.Light; using Meadow.Hardware; +using System.Diagnostics; Console.WriteLine("HELLO FROM THE WILDERNESS FT232H DRIVER!"); @@ -28,9 +29,22 @@ async Task TestI2C(FtdiExpander expander) { - var sensor = new Bme280(expander.CreateI2cBus()); + var sensor = new Veml7700(expander.CreateI2cBus()); //var sensor = new Pct2075(expander.CreateI2cBus()); - await sensor.Read(); + while (true) + { + Debug.WriteLine("Reading..."); + try + { + var t = await sensor.Read(); + Debug.WriteLine($"{t.Lux} lux"); + } + catch + { + } + + await Task.Delay(1000); + } } async Task TestGpio(IEnumerable expanders) From 3387a936cab2086cd233d7232d1d57ac2508265d Mon Sep 17 00:00:00 2001 From: Chris Tacke Date: Thu, 22 Feb 2024 10:37:31 -0600 Subject: [PATCH 07/11] bug fixes in FT232 GPIO --- .../Driver/Drivers/Mcp3201.cs | 2 - .../Driver/FtdiDigitalOutputPort.cs | 45 ------- .../Driver/FtdiExpander.DigitalOutputPort.cs | 59 +++++++++ .../Driver/FtdiExpander.Ft232hSpiBus.cs | 104 +++++++++++++++ .../Driver/FtdiExpander.SpiBus.cs | 19 +++ .../Driver/FtdiExpander.cs | 123 +++++++----------- .../Ft232h_Windows_Sample.csproj | 1 + .../Samples/Ft232h_Windows_Sample/Program.cs | 43 +++++- 8 files changed, 267 insertions(+), 129 deletions(-) delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiDigitalOutputPort.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.DigitalOutputPort.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hSpiBus.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.SpiBus.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.ADCs.Mcp3xxx/Driver/Drivers/Mcp3201.cs b/Source/Meadow.Foundation.Peripherals/ICs.ADCs.Mcp3xxx/Driver/Drivers/Mcp3201.cs index 44b173455f..7ed03b3fda 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.ADCs.Mcp3xxx/Driver/Drivers/Mcp3201.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.ADCs.Mcp3xxx/Driver/Drivers/Mcp3201.cs @@ -80,9 +80,7 @@ protected override int ReadInternal(int channel, InputType inputType, int adcRes var data = (buffer[0] & 0x1f) << 8 | buffer[1]; // then shift back right 1 bit var result = data >> 1; - Console.WriteLine($"raw: {result}"); return result; - } } } \ No newline at end of file diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiDigitalOutputPort.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiDigitalOutputPort.cs deleted file mode 100644 index e6d8e02911..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiDigitalOutputPort.cs +++ /dev/null @@ -1,45 +0,0 @@ -using Meadow.Hardware; - -namespace Meadow.Foundation.ICs.IOExpanders; - -public sealed class FtdiDigitalOutputPort : DigitalOutputPortBase -{ - private FtdiExpander _expander; - private bool _state; - private byte _key; - private bool _lowByte; - - internal FtdiDigitalOutputPort(FtdiExpander expander, IPin pin, IDigitalChannelInfo channel, bool initialState, OutputType initialOutputType) - : base(pin, channel, initialState, initialOutputType) - { - _expander = expander; - - if (pin is FtdiPin p) - { - _key = (byte)p.Key; - _lowByte = p.IsLowByte; - } - } - - public override bool State - { - get => _state; - set - { - byte s = _expander.GpioStateLow; - - if (value) - { - s |= _key; - } - else - { - s &= (byte)~_key; - } - - _expander.SetGpioDirectionAndState(_lowByte, _expander.GpioDirectionLow, s); - _expander.GpioStateLow = s; - _state = value; - } - } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.DigitalOutputPort.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.DigitalOutputPort.cs new file mode 100644 index 0000000000..b8be7d7cca --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.DigitalOutputPort.cs @@ -0,0 +1,59 @@ +using Meadow.Hardware; +using System; + +namespace Meadow.Foundation.ICs.IOExpanders; + +public abstract partial class FtdiExpander +{ + public sealed class DigitalOutputPort : DigitalOutputPortBase + { + private FtdiExpander _expander; + private bool _state; + private FtdiPin _pin; + + internal DigitalOutputPort(FtdiExpander expander, IPin pin, IDigitalChannelInfo channel, bool initialState, OutputType initialOutputType) + : base(pin, channel, initialState, initialOutputType) + { + _expander = expander; + + if (pin is FtdiPin p) + { + _pin = p; + } + else + { + throw new ArgumentException("Invalid pin"); + } + } + + public override bool State + { + get => _state; + set + { + byte s = _pin.IsLowByte ? _expander.GpioStateLow : _expander.GpioStateHigh; + + if (value) + { + s |= (byte)_pin.Key; + } + else + { + s &= (byte)~(byte)_pin.Key; + } + + if (_pin.IsLowByte) + { + // Dxxx pins + _expander.SetGpioDirectionAndState(true, _expander.GpioDirectionLow, s); + } + else + { + // Cxxx pins + _expander.SetGpioDirectionAndState(false, _expander.GpioDirectionHigh, s); + } + _state = value; + } + } + } +} \ No newline at end of file diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hSpiBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hSpiBus.cs new file mode 100644 index 0000000000..686198fefd --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hSpiBus.cs @@ -0,0 +1,104 @@ +using Meadow.Hardware; +using Meadow.Units; +using System; + +namespace Meadow.Foundation.ICs.IOExpanders; + +public abstract partial class FtdiExpander +{ + public class Ft232hSpiBus : SpiBus + { + private FtdiExpander _expander; + private SpiClockConfiguration _configuration; + + public override Frequency[] SupportedSpeeds => throw new NotImplementedException(); + + public override SpiClockConfiguration Configuration => _configuration; + + internal Ft232hSpiBus(FtdiExpander expander) + { + _configuration = new SpiClockConfiguration(1000000.Hertz()); + _expander = expander; + } + + public override void Configure() + { + // Setup the clock and other elements + Span toSend = stackalloc byte[5]; + int idx = 0; + // Disable clock divide by 5 for 60Mhz master clock + toSend[idx++] = (byte)Native.FT_OPCODE.DisableClockDivideBy5; + // Turn off adaptive clocking + toSend[idx++] = (byte)Native.FT_OPCODE.TurnOffAdaptiveClocking; + // set SPI clock rate + toSend[idx++] = (byte)Native.FT_OPCODE.SetClockDivisor; + uint clockDivisor = (12000 / (_expander.SpiBusFrequencyKbps * 2)) - 1; + toSend[idx++] = (byte)(clockDivisor & 0x00FF); + toSend[idx++] = (byte)((clockDivisor >> 8) & 0x00FF); + + _expander.Write(toSend); + } + + /* + # Set SPI clock rate + def set_spi_clock(d, hz): + div = int((12000000 / (hz * 2)) - 1) # Set SPI clock + ft_write(d, (0x86, div%256, div//256)) + +if dev: + print("FTDI device opened") + set_bitmode(dev, OPS, 2) # Set SPI mode + set_spi_clock(dev, 1000000) # Set SPI clock + ft_write(dev, (0x80, 0, OPS+OE+LE)) # Set outputs + data = dig_segs[DIG1], dig_segs[DIG2] # Convert digits to segs + ft_write_cmd_bytes(dev, 0x11, data) # Write seg bit data + ft_write(dev, (0x80, LE, OPS+OE+LE)) # Latch = 1 + ft_write(dev, (0x80, OE, OPS+OE+LE)) # Latch = 0, disp = 1 + print("Displaying '%u%u'" % (DIG2, DIG1)) + time.sleep(1) + ft_write(dev, (0x80, 0, OPS+OE+LE)) # Latch = disp = 0 + print("Display off") + dev.close() + */ + + public override void Exchange(IDigitalOutputPort? chipSelect, Span writeBuffer, Span readBuffer, ChipSelectMode csMode = ChipSelectMode.ActiveLow) + { + throw new NotImplementedException(); + } + + public override void Read(IDigitalOutputPort? chipSelect, Span readBuffer, ChipSelectMode csMode = ChipSelectMode.ActiveLow) + { + if (chipSelect != null) + { + chipSelect.State = csMode == ChipSelectMode.ActiveLow ? false : true; + } + + Span toSend = stackalloc byte[5]; + var idx = 0; + toSend[idx++] = (byte)Native.FT_OPCODE.ClockDataBytesInOnMinusVeClockMSBFirst; // clock in on falling edge + toSend[idx++] = (byte)(readBuffer.Length % 256); // LSB of length to read + toSend[idx++] = 0; // MSB of length to read + toSend[idx++] = (byte)Native.FT_OPCODE.SendImmediate; // read now + _expander.Write(toSend); + var readCount = _expander.ReadInto(readBuffer); + + if (chipSelect != null) + { + chipSelect.State = csMode == ChipSelectMode.ActiveLow ? true : false; + } + } + + public override void Write(IDigitalOutputPort? chipSelect, Span writeBuffer, ChipSelectMode csMode = ChipSelectMode.ActiveLow) + { + if (chipSelect != null) + { + chipSelect.State = csMode == ChipSelectMode.ActiveLow ? false : true; + } + + if (chipSelect != null) + { + chipSelect.State = csMode == ChipSelectMode.ActiveLow ? true : false; + } + } + } +} \ No newline at end of file diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.SpiBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.SpiBus.cs new file mode 100644 index 0000000000..d53aeae04f --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.SpiBus.cs @@ -0,0 +1,19 @@ +using Meadow.Hardware; +using Meadow.Units; +using System; + +namespace Meadow.Foundation.ICs.IOExpanders; + +public abstract partial class FtdiExpander +{ + public abstract class SpiBus : ISpiBus + { + public abstract Frequency[] SupportedSpeeds { get; } + public abstract SpiClockConfiguration Configuration { get; } + + public abstract void Configure(); + public abstract void Exchange(IDigitalOutputPort? chipSelect, Span writeBuffer, Span readBuffer, ChipSelectMode csMode = ChipSelectMode.ActiveLow); + public abstract void Read(IDigitalOutputPort? chipSelect, Span readBuffer, ChipSelectMode csMode = ChipSelectMode.ActiveLow); + public abstract void Write(IDigitalOutputPort? chipSelect, Span writeBuffer, ChipSelectMode csMode = ChipSelectMode.ActiveLow); + } +} \ No newline at end of file diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs index 031985ccbb..dd21f73408 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs @@ -1,4 +1,5 @@ using Meadow.Hardware; +using Meadow.Units; using System; using System.IO; using System.Linq; @@ -13,28 +14,9 @@ public abstract partial class FtdiExpander : // IDisposable, // IDigitalInputOutputController, IDigitalOutputController, -// ISpiController, + ISpiController, II2cController { - public II2cBus CreateI2cBus(int busNumber = 1, I2cBusSpeed busSpeed = I2cBusSpeed.Standard) - { - // TODO: depends on part - // TODO: make sure no SPI is in use - var bus = new Ft232hI2cBus(this); - bus.Configure(); - return bus; - } - - public II2cBus CreateI2cBus(IPin[] pins, I2cBusSpeed busSpeed) - { - return CreateI2cBus(1); - } - - public II2cBus CreateI2cBus(IPin clock, IPin data, I2cBusSpeed busSpeed) - { - return CreateI2cBus(1); - } - internal byte GpioDirectionLow { get; set; } internal byte GpioStateLow { get; set; } internal byte GpioDirectionHigh { get; set; } @@ -48,7 +30,8 @@ public II2cBus CreateI2cBus(IPin clock, IPin data, I2cBusSpeed busSpeed) internal string Description { get; private set; } internal IntPtr Handle { get; private set; } - internal uint I2cBusFrequencyKbps { get; private set; } = 100; + internal uint I2cBusFrequencyKbps { get; private set; } = 400; + internal uint SpiBusFrequencyKbps { get; private set; } = 1000; /// /// The pins @@ -163,6 +146,9 @@ internal void SetGpioDirectionAndState(bool lowByte, byte direction, byte state) outBuffer[1] = state; //data outBuffer[2] = direction; //direction 1 == output, 0 == input + // Console.WriteLine($"{(BitConverter.ToString(outBuffer.ToArray()))}"); + Write(outBuffer); + if (lowByte) { GpioStateLow = state; @@ -173,9 +159,6 @@ internal void SetGpioDirectionAndState(bool lowByte, byte direction, byte state) GpioStateHigh = state; GpioDirectionHigh = direction; } - - // Console.WriteLine($"{(BitConverter.ToString(outBuffer.ToArray()))}"); - Write(outBuffer); } private void InitializeMpsse() @@ -229,58 +212,6 @@ internal void Write(ReadOnlySpan data) FT_Write(Handle, in MemoryMarshal.GetReference(data), (ushort)data.Length, ref written)); } - /* - private byte Receive_Data(uint BytesToRead) - { - uint NumBytesInQueue = 0; - uint QueueTimeOut = 0; - uint Buffer1Index = 0; - uint Buffer2Index = 0; - uint TotalBytesRead = 0; - bool QueueTimeoutFlag = false; - uint NumBytesRxd = 0; - - // Keep looping until all requested bytes are received or we've tried 5000 times (value can be chosen as required) - while ((TotalBytesRead < BytesToRead) && (QueueTimeoutFlag == false)) - { - ftStatus = myFtdiDevice.GetRxBytesAvailable(ref NumBytesInQueue); // Check bytes available - - if ((NumBytesInQueue > 0) && (ftStatus == FTDI.FT_STATUS.FT_OK)) - { - ftStatus = myFtdiDevice.Read(InputBuffer, NumBytesInQueue, ref NumBytesRxd); // if any available read them - - if ((NumBytesInQueue == NumBytesRxd) && (ftStatus == FTDI.FT_STATUS.FT_OK)) - { - Buffer1Index = 0; - - while (Buffer1Index < NumBytesRxd) - { - InputBuffer2[Buffer2Index] = InputBuffer[Buffer1Index]; // copy into main overall application buffer - Buffer1Index++; - Buffer2Index++; - } - TotalBytesRead = TotalBytesRead + NumBytesRxd; // Keep track of total - } - else - return 1; - - QueueTimeOut++; - if (QueueTimeOut == 5000) - QueueTimeoutFlag = true; - else - Thread.Sleep(0); // Avoids running Queue status checks back to back - } - } - // returning globals NumBytesRead and the buffer InputBuffer2 - NumBytesRead = TotalBytesRead; - - if (QueueTimeoutFlag == true) - return 1; - else - return 0; - } - */ - public IDigitalOutputPort CreateDigitalOutputPort(IPin pin, bool initialState = false, OutputType initialOutputType = OutputType.PushPull) { var p = pin as FtdiPin; @@ -320,6 +251,44 @@ public IDigitalOutputPort CreateDigitalOutputPort(IPin pin, bool initialState = } var channel = p.SupportedChannels.FirstOrDefault(channel => channel is IDigitalChannelInfo) as IDigitalChannelInfo; - return new FtdiDigitalOutputPort(this, pin, channel, initialState, initialOutputType); + return new DigitalOutputPort(this, pin, channel, initialState, initialOutputType); + } + + public II2cBus CreateI2cBus(int busNumber = 1, I2cBusSpeed busSpeed = I2cBusSpeed.Standard) + { + // TODO: depends on part + // TODO: make sure no SPI is in use + var bus = new Ft232hI2cBus(this); + bus.Configure(); + return bus; + } + + public II2cBus CreateI2cBus(IPin[] pins, I2cBusSpeed busSpeed) + { + return CreateI2cBus(1); + } + + public II2cBus CreateI2cBus(IPin clock, IPin data, I2cBusSpeed busSpeed) + { + return CreateI2cBus(1); + } + + public ISpiBus CreateSpiBus(IPin clock, IPin copi, IPin cipo, SpiClockConfiguration config) + { + // TODO: depends on part + // TODO: make sure no SPI is in use + var bus = new Ft232hSpiBus(this); + bus.Configure(); + return bus; + } + + public ISpiBus CreateSpiBus(IPin clock, IPin copi, IPin cipo, Frequency speed) + { + return CreateSpiBus(clock, copi, cipo, new SpiClockConfiguration(speed)); + } + + public ISpiBus CreateSpiBus() + { + return CreateSpiBus(null, null, null, 1000000.Hertz()); } } diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Ft232h_Windows_Sample.csproj b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Ft232h_Windows_Sample.csproj index 918d1fe541..fb9aa90c24 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Ft232h_Windows_Sample.csproj +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Ft232h_Windows_Sample.csproj @@ -15,6 +15,7 @@ + diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs index ab3a72c826..62b0352a90 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs @@ -1,4 +1,5 @@ // See https://aka.ms/new-console-template for more information +using Meadow; using Meadow.Foundation.ICs.IOExpanders; using Meadow.Foundation.Sensors.Light; using Meadow.Hardware; @@ -11,8 +12,9 @@ //await TestBME280(ft232); //await TestIli9341(ft232h); -//await TestGpio(FtdiExpanderCollection.Devices); -await TestI2C(FtdiExpanderCollection.Devices[0]); +await TestGpio(FtdiExpanderCollection.Devices); +//await TestI2C(FtdiExpanderCollection.Devices[0]); +//await TestSPI(FtdiExpanderCollection.Devices[0]); //async Task TestBME280(Ft232h expander) @@ -26,6 +28,28 @@ // await Task.Delay(1000); // } //} +async Task TestSPI(FtdiExpander expander) +{ + var gpio = expander.CreateDigitalOutputPort(expander.Pins.C0); + + while (true) + { + gpio.State = true; + await Task.Delay(1000); + gpio.State = false; + await Task.Delay(1000); + } + + + var mcp = new Mcp3201( + expander.CreateSpiBus(), + expander.Pins.C1.CreateDigitalOutputPort()); + + var inp = mcp.CreateAnalogInputPort(); + + await inp.Read(); + +} async Task TestI2C(FtdiExpander expander) { @@ -55,9 +79,18 @@ async Task TestGpio(IEnumerable expanders) foreach (var expander in expanders) { outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C0)); - outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C7)); - outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.D3)); - outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.D7)); + outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C1)); + //outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C2)); + //outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C3)); + //outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C4)); + //outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C5)); + //outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C6)); + //outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C7)); + //outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.D3)); + //outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.D4)); + //outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.D5)); + //outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.D6)); + //outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.D7)); } var s = false; From 4d34ef09669ce10372b9fb90e22e438842f4a63c Mon Sep 17 00:00:00 2001 From: Chris Tacke Date: Thu, 22 Feb 2024 12:11:04 -0600 Subject: [PATCH 08/11] bug fix for ref voltage in MCP3xxx --- .../ICs.ADCs.Mcp3xxx/Driver/Drivers/Mcp3001.cs | 2 +- .../ICs.ADCs.Mcp3xxx/Driver/Drivers/Mcp3201.cs | 2 +- .../ICs.ADCs.Mcp3xxx/Driver/Mcp3xxx.cs | 11 ++++++++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Source/Meadow.Foundation.Peripherals/ICs.ADCs.Mcp3xxx/Driver/Drivers/Mcp3001.cs b/Source/Meadow.Foundation.Peripherals/ICs.ADCs.Mcp3xxx/Driver/Drivers/Mcp3001.cs index 84d1627b28..e48df4a641 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.ADCs.Mcp3xxx/Driver/Drivers/Mcp3001.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.ADCs.Mcp3xxx/Driver/Drivers/Mcp3001.cs @@ -51,7 +51,7 @@ public override bool IsInputTypeSupported(InputType inputType) /// public IAnalogInputPort CreateAnalogInputPort(int sampleCount = 64) { - return CreateAnalogInputPort(sampleCount, TimeSpan.FromSeconds(1), new Voltage(0)); + return CreateAnalogInputPort(sampleCount, TimeSpan.FromSeconds(1), DefaultReferenceVoltage); } /// diff --git a/Source/Meadow.Foundation.Peripherals/ICs.ADCs.Mcp3xxx/Driver/Drivers/Mcp3201.cs b/Source/Meadow.Foundation.Peripherals/ICs.ADCs.Mcp3xxx/Driver/Drivers/Mcp3201.cs index 7ed03b3fda..3315a68302 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.ADCs.Mcp3xxx/Driver/Drivers/Mcp3201.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.ADCs.Mcp3xxx/Driver/Drivers/Mcp3201.cs @@ -51,7 +51,7 @@ public override bool IsInputTypeSupported(InputType inputType) /// public IAnalogInputPort CreateAnalogInputPort(int sampleCount = 64) { - return CreateAnalogInputPort(sampleCount, TimeSpan.FromSeconds(1), new Voltage(0)); + return CreateAnalogInputPort(sampleCount, TimeSpan.FromSeconds(1), DefaultReferenceVoltage); } /// diff --git a/Source/Meadow.Foundation.Peripherals/ICs.ADCs.Mcp3xxx/Driver/Mcp3xxx.cs b/Source/Meadow.Foundation.Peripherals/ICs.ADCs.Mcp3xxx/Driver/Mcp3xxx.cs index f9d9ab5645..5ea1343599 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.ADCs.Mcp3xxx/Driver/Mcp3xxx.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.ADCs.Mcp3xxx/Driver/Mcp3xxx.cs @@ -18,7 +18,7 @@ public abstract partial class Mcp3xxx : IAnalogInputController, ISpiPeripheral, /// /// Did we create the port(s) used by the peripheral /// - readonly bool createdPort = false; + private readonly bool createdPort = false; /// /// Gets the underlying ISpiCommunications instance @@ -35,6 +35,11 @@ public abstract partial class Mcp3xxx : IAnalogInputController, ISpiPeripheral, /// public Frequency DefaultSpiBusSpeed => new(10000, Frequency.UnitType.Kilohertz); + /// + /// The default reference voltage for the device + /// + public Voltage DefaultReferenceVoltage => 3.3.Volts(); + /// /// The SPI bus speed for the device /// @@ -69,7 +74,7 @@ public SpiClockConfiguration.Mode SpiBusMode /// internal int AdcMaxValue { get; set; } - IDigitalOutputPort chipSelectPort; + private IDigitalOutputPort chipSelectPort; /// /// Mcp3xxx base class constructor @@ -113,7 +118,7 @@ protected Mcp3xxx(ISpiBus spiBus, /// An instance of that represents the analog input on the specified pin protected IAnalogInputPort CreateAnalogInputPort(IPin pin, int sampleCount = 64) { - return CreateAnalogInputPort(pin, sampleCount, TimeSpan.FromSeconds(1), new Voltage(0)); + return CreateAnalogInputPort(pin, sampleCount, TimeSpan.FromSeconds(1), DefaultReferenceVoltage); } /// From 8765e65094cefc78351f33f47558195887105dcf Mon Sep 17 00:00:00 2001 From: Chris Tacke Date: Thu, 22 Feb 2024 12:11:47 -0600 Subject: [PATCH 09/11] added SPI reads to FT232 --- .../Driver/FtdiExpander.Ft232hSpiBus.cs | 7 ++- .../Samples/Ft232h_Windows_Sample/Program.cs | 52 ++++++++++--------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hSpiBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hSpiBus.cs index 686198fefd..d85300ec35 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hSpiBus.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hSpiBus.cs @@ -37,6 +37,9 @@ public override void Configure() toSend[idx++] = (byte)((clockDivisor >> 8) & 0x00FF); _expander.Write(toSend); + + // make the SCK and SDO lines outputs + _expander.SetGpioDirectionAndState(true, _expander.GpioDirectionLow |= 0x03, _expander.GpioStateLow); } /* @@ -73,10 +76,10 @@ public override void Read(IDigitalOutputPort? chipSelect, Span readBuffer, chipSelect.State = csMode == ChipSelectMode.ActiveLow ? false : true; } - Span toSend = stackalloc byte[5]; + Span toSend = stackalloc byte[4]; var idx = 0; toSend[idx++] = (byte)Native.FT_OPCODE.ClockDataBytesInOnMinusVeClockMSBFirst; // clock in on falling edge - toSend[idx++] = (byte)(readBuffer.Length % 256); // LSB of length to read + toSend[idx++] = (byte)(readBuffer.Length % 256 - 1); // LSB of length to read toSend[idx++] = 0; // MSB of length to read toSend[idx++] = (byte)Native.FT_OPCODE.SendImmediate; // read now _expander.Write(toSend); diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs index 62b0352a90..ef7baf3c25 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Samples/Ft232h_Windows_Sample/Program.cs @@ -12,9 +12,9 @@ //await TestBME280(ft232); //await TestIli9341(ft232h); -await TestGpio(FtdiExpanderCollection.Devices); +//await TestGpio(FtdiExpanderCollection.Devices); //await TestI2C(FtdiExpanderCollection.Devices[0]); -//await TestSPI(FtdiExpanderCollection.Devices[0]); +await TestSPI(FtdiExpanderCollection.Devices[0]); //async Task TestBME280(Ft232h expander) @@ -30,24 +30,26 @@ //} async Task TestSPI(FtdiExpander expander) { - var gpio = expander.CreateDigitalOutputPort(expander.Pins.C0); - - while (true) - { - gpio.State = true; - await Task.Delay(1000); - gpio.State = false; - await Task.Delay(1000); - } - - var mcp = new Mcp3201( expander.CreateSpiBus(), expander.Pins.C1.CreateDigitalOutputPort()); var inp = mcp.CreateAnalogInputPort(); - await inp.Read(); + while (true) + { + Debug.WriteLine("Reading..."); + try + { + var t = await inp.Read(); + Debug.WriteLine($"{t.Volts} V"); + } + catch + { + } + + await Task.Delay(1000); + } } @@ -80,17 +82,17 @@ async Task TestGpio(IEnumerable expanders) { outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C0)); outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C1)); - //outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C2)); - //outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C3)); - //outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C4)); - //outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C5)); - //outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C6)); - //outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C7)); - //outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.D3)); - //outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.D4)); - //outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.D5)); - //outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.D6)); - //outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.D7)); + outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C2)); + outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C3)); + outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C4)); + outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C5)); + outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C6)); + outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.C7)); + outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.D3)); + outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.D4)); + outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.D5)); + outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.D6)); + outputs.Add(expander.CreateDigitalOutputPort(expander.Pins.D7)); } var s = false; From b82aa258aff643ac604eea3c02b77db6566b9b9f Mon Sep 17 00:00:00 2001 From: Chris Tacke Date: Thu, 22 Feb 2024 15:52:46 -0600 Subject: [PATCH 10/11] Cleaning out unused FT232 garbage --- .../FTDI/FTDI.FT2232H_EEPROM_STRUCTURE.cs | 130 - .../FTDI/FTDI.FT2232_EEPROM_STRUCTURE.cs | 103 - .../FTDI/FTDI.FT232B_EEPROM_STRUCTURE.cs | 61 - .../FTDI/FTDI.FT232H_EEPROM_STRUCTURE.cs | 171 - .../FTDI/FTDI.FT232R_EEPROM_STRUCTURE.cs | 130 - .../FTDI/FTDI.FT4232H_EEPROM_STRUCTURE.cs | 126 - .../Driver/FTDI/FTDI.FT_232H_CBUS_OPTIONS.cs | 88 - .../Driver/FTDI/FTDI.FT_BIT_MODES.cs | 70 - .../Driver/FTDI/FTDI.FT_CBUS_OPTIONS.cs | 90 - .../Driver/FTDI/FTDI.FT_DATA_BITS.cs | 46 - .../Driver/FTDI/FTDI.FT_DEVICE.cs | 138 - .../Driver/FTDI/FTDI.FT_DEVICE_INFO_NODE.cs | 71 - .../Driver/FTDI/FTDI.FT_DRIVE_CURRENT.cs | 54 - .../Driver/FTDI/FTDI.FT_EEPROM_DATA.cs | 82 - .../Driver/FTDI/FTDI.FT_EEPROM_HEADER.cs | 51 - .../Driver/FTDI/FTDI.FT_ERROR.cs | 42 - .../Driver/FTDI/FTDI.FT_EVENTS.cs | 54 - .../Driver/FTDI/FTDI.FT_EXCEPTION.cs | 66 - .../Driver/FTDI/FTDI.FT_FLAGS.cs | 50 - .../Driver/FTDI/FTDI.FT_FLOW_CONTROL.cs | 58 - .../Driver/FTDI/FTDI.FT_LINE_STATUS.cs | 54 - .../Driver/FTDI/FTDI.FT_MODEM_STATUS.cs | 54 - .../Driver/FTDI/FTDI.FT_OPCODE.cs | 91 - .../Driver/FTDI/FTDI.FT_PARITY.cs | 58 - .../Driver/FTDI/FTDI.FT_PROGRAM_DATA.cs | 180 - .../Driver/FTDI/FTDI.FT_PURGE.cs | 46 - .../Driver/FTDI/FTDI.FT_STATUS.cs | 110 - .../Driver/FTDI/FTDI.FT_STOP_BITS.cs | 46 - .../FTDI/FTDI.FT_XSERIES_CBUS_OPTIONS.cs | 125 - .../Driver/FTDI/FTDI.FT_XSERIES_DATA.cs | 82 - .../FTDI/FTDI.FT_XSERIES_EEPROM_STRUCTURE.cs | 218 - .../Driver/FTDI/FTDI.cs | 4901 ----------------- .../Driver/Ft2232Expander.cs | 29 + .../ICs.IOExpanders.Ft232h/Driver/Ft232h.cs | 159 - .../Driver/Ft232hExpander.cs | 26 +- .../Driver/Ft23xx/Ft23xxDigitalInputPort.cs | 50 - .../Driver/Ft23xx/Ft23xxDigitalOutputPort.cs | 59 - .../Driver/Ft23xx/Ft23xxI2cBus.cs | 90 - .../Driver/Ft23xx/Ft23xxSpiBus.cs | 67 - .../Driver/Ft23xx/Ftd2xxImpl.cs | 69 - .../Driver/FtdiExpander.Ft232hI2cBus.cs | 21 +- .../Driver/FtdiExpander.Ft232hSpiBus.cs | 121 +- .../Driver/FtdiExpander.Ft23xxxI2cBus.cs | 22 +- .../Driver/FtdiExpander.I2CBus.cs | 27 +- .../Driver/FtdiExpander.SpiBus.cs | 8 +- .../Driver/FtdiExpander.cs | 63 +- .../Driver/ICs.IOExpanders.Ft232h.csproj | 36 - .../Driver/IFt232Bus.cs | 24 - .../Driver/IFtdiImpl.cs | 13 - .../Driver/MPSSE/MpsseChannel.Gpio.cs | 93 - .../Driver/MPSSE/MpsseChannel.I2c.cs | 398 -- .../Driver/MPSSE/MpsseChannel.Spi.cs | 210 - .../Driver/MPSSE/MpsseChannel.cs | 138 - .../Driver/MPSSE/MpsseChannelCollection.cs | 79 - .../Driver/MPSSE/MpsseDigitalInputPort.cs | 70 - .../Driver/MPSSE/MpsseDigitalOutputPort.cs | 64 - .../Driver/MPSSE/MpsseI2cBus.cs | 137 - .../Driver/MPSSE/MpsseImpl.cs | 197 - .../Driver/MPSSE/MpsseSpiBus.cs | 277 - .../Driver/MPSSE/Native.Mpsse.cs | 115 - .../Driver/{Ft23xx => }/Native.Ftd2xx.cs | 0 .../ICs.IOExpanders.Ft232h/Driver/Native.cs | 407 +- .../Driver/SPITransferOptions.cs | 22 - .../Driver/SpiConfigOptions.cs | 69 - 64 files changed, 375 insertions(+), 10431 deletions(-) delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT2232H_EEPROM_STRUCTURE.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT2232_EEPROM_STRUCTURE.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT232B_EEPROM_STRUCTURE.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT232H_EEPROM_STRUCTURE.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT232R_EEPROM_STRUCTURE.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT4232H_EEPROM_STRUCTURE.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_232H_CBUS_OPTIONS.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_BIT_MODES.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_CBUS_OPTIONS.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DATA_BITS.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DEVICE.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DEVICE_INFO_NODE.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DRIVE_CURRENT.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EEPROM_DATA.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EEPROM_HEADER.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_ERROR.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EVENTS.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EXCEPTION.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_FLAGS.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_FLOW_CONTROL.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_LINE_STATUS.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_MODEM_STATUS.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_OPCODE.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_PARITY.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_PROGRAM_DATA.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_PURGE.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_STATUS.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_STOP_BITS.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_XSERIES_CBUS_OPTIONS.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_XSERIES_DATA.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_XSERIES_EEPROM_STRUCTURE.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.cs create mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft2232Expander.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxDigitalInputPort.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxDigitalOutputPort.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxI2cBus.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxSpiBus.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ftd2xxImpl.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/IFt232Bus.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/IFtdiImpl.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.Gpio.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.I2c.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.Spi.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannelCollection.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseDigitalInputPort.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseDigitalOutputPort.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseI2cBus.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseImpl.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseSpiBus.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/Native.Mpsse.cs rename Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/{Ft23xx => }/Native.Ftd2xx.cs (100%) delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SPITransferOptions.cs delete mode 100644 Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SpiConfigOptions.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT2232H_EEPROM_STRUCTURE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT2232H_EEPROM_STRUCTURE.cs deleted file mode 100644 index f9e531949b..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT2232H_EEPROM_STRUCTURE.cs +++ /dev/null @@ -1,130 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // EEPROM class for FT2232H - /// - /// EEPROM structure specific to FT2232H devices. - /// Inherits from FT_EEPROM_DATA. - /// - public class FT2232H_EEPROM_STRUCTURE : FT_EEPROM_DATA - { - /// - /// Determines if IOs are pulled down when the device is in suspend - /// - public bool PullDownEnable = false; - /// - /// Determines if the serial number is enabled - /// - public bool SerNumEnable = true; - /// - /// Determines if AL pins have a slow slew rate - /// - public bool ALSlowSlew = false; - /// - /// Determines if the AL pins have a Schmitt input - /// - public bool ALSchmittInput = false; - /// - /// Determines the AL pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA - /// - public byte ALDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA; - /// - /// Determines if AH pins have a slow slew rate - /// - public bool AHSlowSlew = false; - /// - /// Determines if the AH pins have a Schmitt input - /// - public bool AHSchmittInput = false; - /// - /// Determines the AH pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA - /// - public byte AHDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA; - /// - /// Determines if BL pins have a slow slew rate - /// - public bool BLSlowSlew = false; - /// - /// Determines if the BL pins have a Schmitt input - /// - public bool BLSchmittInput = false; - /// - /// Determines the BL pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA - /// - public byte BLDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA; - /// - /// Determines if BH pins have a slow slew rate - /// - public bool BHSlowSlew = false; - /// - /// Determines if the BH pins have a Schmitt input - /// - public bool BHSchmittInput = false; - /// - /// Determines the BH pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA - /// - public byte BHDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA; - /// - /// Determines if channel A is in FIFO mode - /// - public bool IFAIsFifo = false; - /// - /// Determines if channel A is in FIFO target mode - /// - public bool IFAIsFifoTar = false; - /// - /// Determines if channel A is in fast serial mode - /// - public bool IFAIsFastSer = false; - /// - /// Determines if channel A loads the VCP driver - /// - public bool AIsVCP = true; - /// - /// Determines if channel B is in FIFO mode - /// - public bool IFBIsFifo = false; - /// - /// Determines if channel B is in FIFO target mode - /// - public bool IFBIsFifoTar = false; - /// - /// Determines if channel B is in fast serial mode - /// - public bool IFBIsFastSer = false; - /// - /// Determines if channel B loads the VCP driver - /// - public bool BIsVCP = true; - /// - /// For self-powered designs, keeps the FT2232H in low power state until BCBUS7 is high - /// - public bool PowerSaveEnable = false; - } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT2232_EEPROM_STRUCTURE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT2232_EEPROM_STRUCTURE.cs deleted file mode 100644 index a0aa852816..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT2232_EEPROM_STRUCTURE.cs +++ /dev/null @@ -1,103 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - - -using System; - - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // EEPROM class for FT2232C, FT2232L and FT2232D - /// - /// EEPROM structure specific to FT2232 devices. - /// Inherits from FT_EEPROM_DATA. - /// - public class FT2232_EEPROM_STRUCTURE : FT_EEPROM_DATA - { - //private bool Rev5 = true; - //private bool IsoInA = false; - //private bool IsoInB = false; - //private bool IsoOutA = false; - //private bool IsoOutB = false; - /// - /// Determines if IOs are pulled down when the device is in suspend - /// - public bool PullDownEnable = false; - /// - /// Determines if the serial number is enabled - /// - public bool SerNumEnable = true; - /// - /// Determines if the USB version number is enabled - /// - public bool USBVersionEnable = true; - /// - /// The USB version number. Should be either 0x0110 (USB 1.1) or 0x0200 (USB 2.0) - /// - public UInt16 USBVersion = 0x0200; - /// - /// Enables high current IOs on channel A - /// - public bool AIsHighCurrent = false; - /// - /// Enables high current IOs on channel B - /// - public bool BIsHighCurrent = false; - /// - /// Determines if channel A is in FIFO mode - /// - public bool IFAIsFifo = false; - /// - /// Determines if channel A is in FIFO target mode - /// - public bool IFAIsFifoTar = false; - /// - /// Determines if channel A is in fast serial mode - /// - public bool IFAIsFastSer = false; - /// - /// Determines if channel A loads the VCP driver - /// - public bool AIsVCP = true; - /// - /// Determines if channel B is in FIFO mode - /// - public bool IFBIsFifo = false; - /// - /// Determines if channel B is in FIFO target mode - /// - public bool IFBIsFifoTar = false; - /// - /// Determines if channel B is in fast serial mode - /// - public bool IFBIsFastSer = false; - /// - /// Determines if channel B loads the VCP driver - /// - public bool BIsVCP = true; - } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT232B_EEPROM_STRUCTURE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT232B_EEPROM_STRUCTURE.cs deleted file mode 100644 index 021c462b1b..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT232B_EEPROM_STRUCTURE.cs +++ /dev/null @@ -1,61 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - - -using System; - - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // EEPROM class for FT232B and FT245B - /// - /// EEPROM structure specific to FT232B and FT245B devices. - /// Inherits from FT_EEPROM_DATA. - /// - public class FT232B_EEPROM_STRUCTURE : FT_EEPROM_DATA - { - //private bool Rev4 = true; - //private bool IsoIn = false; - //private bool IsoOut = false; - /// - /// Determines if IOs are pulled down when the device is in suspend - /// - public bool PullDownEnable = false; - /// - /// Determines if the serial number is enabled - /// - public bool SerNumEnable = true; - /// - /// Determines if the USB version number is enabled - /// - public bool USBVersionEnable = true; - /// - /// The USB version number. Should be either 0x0110 (USB 1.1) or 0x0200 (USB 2.0) - /// - public UInt16 USBVersion = 0x0200; - } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT232H_EEPROM_STRUCTURE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT232H_EEPROM_STRUCTURE.cs deleted file mode 100644 index 67fe3c2a85..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT232H_EEPROM_STRUCTURE.cs +++ /dev/null @@ -1,171 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // EEPROM class for FT232H - /// - /// EEPROM structure specific to FT232H devices. - /// Inherits from FT_EEPROM_DATA. - /// - public class FT232H_EEPROM_STRUCTURE : FT_EEPROM_DATA - { - /// - /// Determines if IOs are pulled down when the device is in suspend - /// - public bool PullDownEnable = false; - /// - /// Determines if the serial number is enabled - /// - public bool SerNumEnable = true; - /// - /// Determines if AC pins have a slow slew rate - /// - public bool ACSlowSlew = false; - /// - /// Determines if the AC pins have a Schmitt input - /// - public bool ACSchmittInput = false; - /// - /// Determines the AC pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA - /// - public byte ACDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA; - /// - /// Determines if AD pins have a slow slew rate - /// - public bool ADSlowSlew = false; - /// - /// Determines if the AD pins have a Schmitt input - /// - public bool ADSchmittInput = false; - /// - /// Determines the AD pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA - /// - public byte ADDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA; - /// - /// Sets the function of the CBUS0 pin for FT232H devices. - /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, - /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN, FT_CBUS_CLK30, - /// FT_CBUS_CLK15, FT_CBUS_CLK7_5 - /// - public byte Cbus0 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE; - /// - /// Sets the function of the CBUS1 pin for FT232H devices. - /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, - /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN, FT_CBUS_CLK30, - /// FT_CBUS_CLK15, FT_CBUS_CLK7_5 - /// - public byte Cbus1 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE; - /// - /// Sets the function of the CBUS2 pin for FT232H devices. - /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, - /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN - /// - public byte Cbus2 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE; - /// - /// Sets the function of the CBUS3 pin for FT232H devices. - /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, - /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN - /// - public byte Cbus3 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE; - /// - /// Sets the function of the CBUS4 pin for FT232H devices. - /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, - /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN - /// - public byte Cbus4 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE; - /// - /// Sets the function of the CBUS5 pin for FT232H devices. - /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, - /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_IOMODE, - /// FT_CBUS_TXDEN, FT_CBUS_CLK30, FT_CBUS_CLK15, FT_CBUS_CLK7_5 - /// - public byte Cbus5 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE; - /// - /// Sets the function of the CBUS6 pin for FT232H devices. - /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, - /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_IOMODE, - /// FT_CBUS_TXDEN, FT_CBUS_CLK30, FT_CBUS_CLK15, FT_CBUS_CLK7_5 - /// - public byte Cbus6 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE; - /// - /// Sets the function of the CBUS7 pin for FT232H devices. - /// Valid values are FT_CBUS_TRISTATE - /// - public byte Cbus7 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE; - /// - /// Sets the function of the CBUS8 pin for FT232H devices. - /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, - /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_IOMODE, - /// FT_CBUS_TXDEN, FT_CBUS_CLK30, FT_CBUS_CLK15, FT_CBUS_CLK7_5 - /// - public byte Cbus8 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE; - /// - /// Sets the function of the CBUS9 pin for FT232H devices. - /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, - /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_IOMODE, - /// FT_CBUS_TXDEN, FT_CBUS_CLK30, FT_CBUS_CLK15, FT_CBUS_CLK7_5 - /// - public byte Cbus9 = FT_232H_CBUS_OPTIONS.FT_CBUS_TRISTATE; - /// - /// Determines if the device is in FIFO mode - /// - public bool IsFifo = false; - /// - /// Determines if the device is in FIFO target mode - /// - public bool IsFifoTar = false; - /// - /// Determines if the device is in fast serial mode - /// - public bool IsFastSer = false; - /// - /// Determines if the device is in FT1248 mode - /// - public bool IsFT1248 = false; - /// - /// Determines FT1248 mode clock polarity - /// - public bool FT1248Cpol = false; - /// - /// Determines if data is ent MSB (0) or LSB (1) in FT1248 mode - /// - public bool FT1248Lsb = false; - /// - /// Determines if FT1248 mode uses flow control - /// - public bool FT1248FlowControl = false; - /// - /// Determines if the VCP driver is loaded - /// - public bool IsVCP = true; - /// - /// For self-powered designs, keeps the FT232H in low power state until ACBUS7 is high - /// - public bool PowerSaveEnable = false; - } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT232R_EEPROM_STRUCTURE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT232R_EEPROM_STRUCTURE.cs deleted file mode 100644 index ee6e968336..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT232R_EEPROM_STRUCTURE.cs +++ /dev/null @@ -1,130 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // EEPROM class for FT232R and FT245R - /// - /// EEPROM structure specific to FT232R and FT245R devices. - /// Inherits from FT_EEPROM_DATA. - /// - public class FT232R_EEPROM_STRUCTURE : FT_EEPROM_DATA - { - /// - /// Disables the FT232R internal clock source. - /// If the device has external oscillator enabled it must have an external oscillator fitted to function - /// - public bool UseExtOsc = false; - /// - /// Enables high current IOs - /// - public bool HighDriveIOs = false; - /// - /// Sets the endpoint size. This should always be set to 64 - /// - public byte EndpointSize = 64; - /// - /// Determines if IOs are pulled down when the device is in suspend - /// - public bool PullDownEnable = false; - /// - /// Determines if the serial number is enabled - /// - public bool SerNumEnable = true; - /// - /// Inverts the sense of the TXD line - /// - public bool InvertTXD = false; - /// - /// Inverts the sense of the RXD line - /// - public bool InvertRXD = false; - /// - /// Inverts the sense of the RTS line - /// - public bool InvertRTS = false; - /// - /// Inverts the sense of the CTS line - /// - public bool InvertCTS = false; - /// - /// Inverts the sense of the DTR line - /// - public bool InvertDTR = false; - /// - /// Inverts the sense of the DSR line - /// - public bool InvertDSR = false; - /// - /// Inverts the sense of the DCD line - /// - public bool InvertDCD = false; - /// - /// Inverts the sense of the RI line - /// - public bool InvertRI = false; - /// - /// Sets the function of the CBUS0 pin for FT232R devices. - /// Valid values are FT_CBUS_TXDEN, FT_CBUS_PWRON , FT_CBUS_RXLED, FT_CBUS_TXLED, - /// FT_CBUS_TXRXLED, FT_CBUS_SLEEP, FT_CBUS_CLK48, FT_CBUS_CLK24, FT_CBUS_CLK12, - /// FT_CBUS_CLK6, FT_CBUS_IOMODE, FT_CBUS_BITBANG_WR, FT_CBUS_BITBANG_RD - /// - public byte Cbus0 = FT_CBUS_OPTIONS.FT_CBUS_SLEEP; - /// - /// Sets the function of the CBUS1 pin for FT232R devices. - /// Valid values are FT_CBUS_TXDEN, FT_CBUS_PWRON , FT_CBUS_RXLED, FT_CBUS_TXLED, - /// FT_CBUS_TXRXLED, FT_CBUS_SLEEP, FT_CBUS_CLK48, FT_CBUS_CLK24, FT_CBUS_CLK12, - /// FT_CBUS_CLK6, FT_CBUS_IOMODE, FT_CBUS_BITBANG_WR, FT_CBUS_BITBANG_RD - /// - public byte Cbus1 = FT_CBUS_OPTIONS.FT_CBUS_SLEEP; - /// - /// Sets the function of the CBUS2 pin for FT232R devices. - /// Valid values are FT_CBUS_TXDEN, FT_CBUS_PWRON , FT_CBUS_RXLED, FT_CBUS_TXLED, - /// FT_CBUS_TXRXLED, FT_CBUS_SLEEP, FT_CBUS_CLK48, FT_CBUS_CLK24, FT_CBUS_CLK12, - /// FT_CBUS_CLK6, FT_CBUS_IOMODE, FT_CBUS_BITBANG_WR, FT_CBUS_BITBANG_RD - /// - public byte Cbus2 = FT_CBUS_OPTIONS.FT_CBUS_SLEEP; - /// - /// Sets the function of the CBUS3 pin for FT232R devices. - /// Valid values are FT_CBUS_TXDEN, FT_CBUS_PWRON , FT_CBUS_RXLED, FT_CBUS_TXLED, - /// FT_CBUS_TXRXLED, FT_CBUS_SLEEP, FT_CBUS_CLK48, FT_CBUS_CLK24, FT_CBUS_CLK12, - /// FT_CBUS_CLK6, FT_CBUS_IOMODE, FT_CBUS_BITBANG_WR, FT_CBUS_BITBANG_RD - /// - public byte Cbus3 = FT_CBUS_OPTIONS.FT_CBUS_SLEEP; - /// - /// Sets the function of the CBUS4 pin for FT232R devices. - /// Valid values are FT_CBUS_TXDEN, FT_CBUS_PWRON , FT_CBUS_RXLED, FT_CBUS_TXLED, - /// FT_CBUS_TXRXLED, FT_CBUS_SLEEP, FT_CBUS_CLK48, FT_CBUS_CLK24, FT_CBUS_CLK12, - /// FT_CBUS_CLK6 - /// - public byte Cbus4 = FT_CBUS_OPTIONS.FT_CBUS_SLEEP; - /// - /// Determines if the VCP driver is loaded - /// - public bool RIsD2XX = false; - } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT4232H_EEPROM_STRUCTURE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT4232H_EEPROM_STRUCTURE.cs deleted file mode 100644 index bc219ec651..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT4232H_EEPROM_STRUCTURE.cs +++ /dev/null @@ -1,126 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // EEPROM class for FT4232H - /// - /// EEPROM structure specific to FT4232H devices. - /// Inherits from FT_EEPROM_DATA. - /// - public class FT4232H_EEPROM_STRUCTURE : FT_EEPROM_DATA - { - /// - /// Determines if IOs are pulled down when the device is in suspend - /// - public bool PullDownEnable = false; - /// - /// Determines if the serial number is enabled - /// - public bool SerNumEnable = true; - /// - /// Determines if A pins have a slow slew rate - /// - public bool ASlowSlew = false; - /// - /// Determines if the A pins have a Schmitt input - /// - public bool ASchmittInput = false; - /// - /// Determines the A pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA - /// - public byte ADriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA; - /// - /// Determines if B pins have a slow slew rate - /// - public bool BSlowSlew = false; - /// - /// Determines if the B pins have a Schmitt input - /// - public bool BSchmittInput = false; - /// - /// Determines the B pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA - /// - public byte BDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA; - /// - /// Determines if C pins have a slow slew rate - /// - public bool CSlowSlew = false; - /// - /// Determines if the C pins have a Schmitt input - /// - public bool CSchmittInput = false; - /// - /// Determines the C pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA - /// - public byte CDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA; - /// - /// Determines if D pins have a slow slew rate - /// - public bool DSlowSlew = false; - /// - /// Determines if the D pins have a Schmitt input - /// - public bool DSchmittInput = false; - /// - /// Determines the D pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA - /// - public byte DDriveCurrent = FT_DRIVE_CURRENT.FT_DRIVE_CURRENT_4MA; - /// - /// RI of port A acts as RS485 transmit enable (TXDEN) - /// - public bool ARIIsTXDEN = false; - /// - /// RI of port B acts as RS485 transmit enable (TXDEN) - /// - public bool BRIIsTXDEN = false; - /// - /// RI of port C acts as RS485 transmit enable (TXDEN) - /// - public bool CRIIsTXDEN = false; - /// - /// RI of port D acts as RS485 transmit enable (TXDEN) - /// - public bool DRIIsTXDEN = false; - /// - /// Determines if channel A loads the VCP driver - /// - public bool AIsVCP = true; - /// - /// Determines if channel B loads the VCP driver - /// - public bool BIsVCP = true; - /// - /// Determines if channel C loads the VCP driver - /// - public bool CIsVCP = true; - /// - /// Determines if channel D loads the VCP driver - /// - public bool DIsVCP = true; - } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_232H_CBUS_OPTIONS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_232H_CBUS_OPTIONS.cs deleted file mode 100644 index 154963096e..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_232H_CBUS_OPTIONS.cs +++ /dev/null @@ -1,88 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // FT232H CBUS Options - /// - /// Available functions for the FT232H CBUS pins. Controlled by FT232H EEPROM settings - /// - public class FT_232H_CBUS_OPTIONS - { - /// - /// FT232H CBUS EEPROM options - Tristate - /// - public const byte FT_CBUS_TRISTATE = 0x00; - /// - /// FT232H CBUS EEPROM options - Rx LED - /// - public const byte FT_CBUS_RXLED = 0x01; - /// - /// FT232H CBUS EEPROM options - Tx LED - /// - public const byte FT_CBUS_TXLED = 0x02; - /// - /// FT232H CBUS EEPROM options - Tx and Rx LED - /// - public const byte FT_CBUS_TXRXLED = 0x03; - /// - /// FT232H CBUS EEPROM options - Power Enable# - /// - public const byte FT_CBUS_PWREN = 0x04; - /// - /// FT232H CBUS EEPROM options - Sleep - /// - public const byte FT_CBUS_SLEEP = 0x05; - /// - /// FT232H CBUS EEPROM options - Drive pin to logic 0 - /// - public const byte FT_CBUS_DRIVE_0 = 0x06; - /// - /// FT232H CBUS EEPROM options - Drive pin to logic 1 - /// - public const byte FT_CBUS_DRIVE_1 = 0x07; - /// - /// FT232H CBUS EEPROM options - IO Mode - /// - public const byte FT_CBUS_IOMODE = 0x08; - /// - /// FT232H CBUS EEPROM options - Tx Data Enable - /// - public const byte FT_CBUS_TXDEN = 0x09; - /// - /// FT232H CBUS EEPROM options - 30MHz clock - /// - public const byte FT_CBUS_CLK30 = 0x0A; - /// - /// FT232H CBUS EEPROM options - 15MHz clock - /// - public const byte FT_CBUS_CLK15 = 0x0B;/// - /// FT232H CBUS EEPROM options - 7.5MHz clock - /// - public const byte FT_CBUS_CLK7_5 = 0x0C; - } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_BIT_MODES.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_BIT_MODES.cs deleted file mode 100644 index e37b3e7b64..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_BIT_MODES.cs +++ /dev/null @@ -1,70 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // Bit modes - /// - /// Permitted bit mode values for FTDI devices. For use with SetBitMode - /// - public class FT_BIT_MODES - { - /// - /// Reset bit mode - /// - public const byte FT_BIT_MODE_RESET = 0x00; - /// - /// Asynchronous bit-bang mode - /// - public const byte FT_BIT_MODE_ASYNC_BITBANG = 0x01; - /// - /// MPSSE bit mode - only available on FT2232, FT2232H, FT4232H and FT232H - /// - public const byte FT_BIT_MODE_MPSSE = 0x02; - /// - /// Synchronous bit-bang mode - /// - public const byte FT_BIT_MODE_SYNC_BITBANG = 0x04; - /// - /// MCU host bus emulation mode - only available on FT2232, FT2232H, FT4232H and FT232H - /// - public const byte FT_BIT_MODE_MCU_HOST = 0x08; - /// - /// Fast opto-isolated serial mode - only available on FT2232, FT2232H, FT4232H and FT232H - /// - public const byte FT_BIT_MODE_FAST_SERIAL = 0x10; - /// - /// CBUS bit-bang mode - only available on FT232R and FT232H - /// - public const byte FT_BIT_MODE_CBUS_BITBANG = 0x20; - /// - /// Single channel synchronous 245 FIFO mode - only available on FT2232H channel A and FT232H - /// - public const byte FT_BIT_MODE_SYNC_FIFO = 0x40; - } - -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_CBUS_OPTIONS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_CBUS_OPTIONS.cs deleted file mode 100644 index d5add3a63e..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_CBUS_OPTIONS.cs +++ /dev/null @@ -1,90 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // FT232R CBUS Options - /// - /// Available functions for the FT232R CBUS pins. Controlled by FT232R EEPROM settings - /// - public class FT_CBUS_OPTIONS - { - /// - /// FT232R CBUS EEPROM options - Tx Data Enable - /// - public const byte FT_CBUS_TXDEN = 0x00; - /// - /// FT232R CBUS EEPROM options - Power On - /// - public const byte FT_CBUS_PWRON = 0x01; - /// - /// FT232R CBUS EEPROM options - Rx LED - /// - public const byte FT_CBUS_RXLED = 0x02; - /// - /// FT232R CBUS EEPROM options - Tx LED - /// - public const byte FT_CBUS_TXLED = 0x03; - /// - /// FT232R CBUS EEPROM options - Tx and Rx LED - /// - public const byte FT_CBUS_TXRXLED = 0x04; - /// - /// FT232R CBUS EEPROM options - Sleep - /// - public const byte FT_CBUS_SLEEP = 0x05; - /// - /// FT232R CBUS EEPROM options - 48MHz clock - /// - public const byte FT_CBUS_CLK48 = 0x06; - /// - /// FT232R CBUS EEPROM options - 24MHz clock - /// - public const byte FT_CBUS_CLK24 = 0x07; - /// - /// FT232R CBUS EEPROM options - 12MHz clock - /// - public const byte FT_CBUS_CLK12 = 0x08; - /// - /// FT232R CBUS EEPROM options - 6MHz clock - /// - public const byte FT_CBUS_CLK6 = 0x09; - /// - /// FT232R CBUS EEPROM options - IO mode - /// - public const byte FT_CBUS_IOMODE = 0x0A; - /// - /// FT232R CBUS EEPROM options - Bit-bang write strobe - /// - public const byte FT_CBUS_BITBANG_WR = 0x0B; - /// - /// FT232R CBUS EEPROM options - Bit-bang read strobe - /// - public const byte FT_CBUS_BITBANG_RD = 0x0C; - } - -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DATA_BITS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DATA_BITS.cs deleted file mode 100644 index 1a274c7952..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DATA_BITS.cs +++ /dev/null @@ -1,46 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // Word Lengths - /// - /// Permitted data bits for FTDI devices - /// - public class FT_DATA_BITS - { - /// - /// 8 data bits - /// - public const byte FT_BITS_8 = 0x08; - /// - /// 7 data bits - /// - public const byte FT_BITS_7 = 0x07; - } - -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DEVICE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DEVICE.cs deleted file mode 100644 index 9f7e16baa2..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DEVICE.cs +++ /dev/null @@ -1,138 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // Device type identifiers for FT_GetDeviceInfoDetail and FT_GetDeviceInfo - /// - /// List of FTDI device types - /// - public enum FT_DEVICE - { - /// - /// FT232B or FT245B device - /// - FT_DEVICE_BM = 0, - /// - /// FT8U232AM or FT8U245AM device - /// - FT_DEVICE_AM, /// 1 - /// - /// FT8U100AX device - /// - FT_DEVICE_100AX, - /// - /// Unknown device - /// - FT_DEVICE_UNKNOWN, - /// - /// FT2232 device - /// - FT_DEVICE_2232, - /// - /// FT232R or FT245R device - /// - FT_DEVICE_232R, /// 5 - /// - /// FT2232H device - /// - FT_DEVICE_2232H, /// 6 - /// - /// FT4232H device - /// - FT_DEVICE_4232H, /// 7 - /// - /// FT232H device - /// - FT_DEVICE_232H, /// 8 - /// - /// FT X-Series device - /// - FT_DEVICE_X_SERIES, /// 9 - /// - /// FT4222 hi-speed device Mode 0 - 2 interfaces - /// - FT_DEVICE_4222H_0, /// 10 - /// - /// FT4222 hi-speed device Mode 1 or 2 - 4 interfaces - /// - FT_DEVICE_4222H_1_2, /// 11 - /// - /// FT4222 hi-speed device Mode 3 - 1 interface - /// - FT_DEVICE_4222H_3, /// 12 - /// - /// OTP programmer board for the FT4222. - /// - FT_DEVICE_4222_PROG, /// 13 - /// - /// OTP programmer board for the FT900. - /// - FT_DEVICE_FT900, /// 14 - /// - /// OTP programmer board for the FT930. - /// - FT_DEVICE_FT930, /// 15 - /// - /// Flash programmer board for the UMFTPD3A. - /// - FT_DEVICE_UMFTPD3A, /// 16 - /// - /// FT2233HP hi-speed device. - /// - FT_DEVICE_2233HP, /// 17 - /// - /// FT4233HP hi-speed device. - /// - FT_DEVICE_4233HP, /// 18 - /// - /// FT2233HP hi-speed device. - /// - FT_DEVICE_2232HP, /// 19 - /// - /// FT4233HP hi-speed device. - /// - FT_DEVICE_4232HP, /// 20 - /// - /// FT233HP hi-speed device. - /// - FT_DEVICE_233HP, /// 21 - /// - /// FT232HP hi-speed device. - /// - FT_DEVICE_232HP, /// 22 - /// - /// FT2233HA hi-speed device. - /// - FT_DEVICE_2232HA, /// 23 - /// - /// FT4233HA hi-speed device. - /// - FT_DEVICE_4232HA, /// 24 - }; - -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DEVICE_INFO_NODE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DEVICE_INFO_NODE.cs deleted file mode 100644 index b6e57f7d2d..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DEVICE_INFO_NODE.cs +++ /dev/null @@ -1,71 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - - -using System; - - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - /// - /// Type that holds device information for GetDeviceInformation method. - /// Used with FT_GetDeviceInfo and FT_GetDeviceInfoDetail in FTD2XX.DLL - /// - public class FT_DEVICE_INFO_NODE - { - /// - /// Indicates device state. Can be any combination of the following: FT_FLAGS_OPENED, FT_FLAGS_HISPEED - /// - public UInt32 Flags; - /// - /// Indicates the device type. Can be one of the following: FT_DEVICE_232R, FT_DEVICE_2232C, FT_DEVICE_BM, FT_DEVICE_AM, FT_DEVICE_100AX or FT_DEVICE_UNKNOWN - /// - public FT_DEVICE Type; - /// - /// The Vendor ID and Product ID of the device - /// - public UInt32 ID; - /// - /// The physical location identifier of the device - /// - public UInt32 LocId; - /// - /// The device serial number - /// - public string SerialNumber; - /// - /// The device description - /// - public string Description; - /// - /// The device handle. This value is not used externally and is provided for information only. - /// If the device is not open, this value is 0. - /// - public IntPtr ftHandle; - } - -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DRIVE_CURRENT.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DRIVE_CURRENT.cs deleted file mode 100644 index ac8f845e1c..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_DRIVE_CURRENT.cs +++ /dev/null @@ -1,54 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // Valid drive current values for FT2232H, FT4232H and FT232H devices - /// - /// Valid values for drive current options on FT2232H, FT4232H and FT232H devices. - /// - public class FT_DRIVE_CURRENT - { - /// - /// 4mA drive current - /// - public const byte FT_DRIVE_CURRENT_4MA = 4; - /// - /// 8mA drive current - /// - public const byte FT_DRIVE_CURRENT_8MA = 8; - /// - /// 12mA drive current - /// - public const byte FT_DRIVE_CURRENT_12MA = 12; - /// - /// 16mA drive current - /// - public const byte FT_DRIVE_CURRENT_16MA = 16; - } - -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EEPROM_DATA.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EEPROM_DATA.cs deleted file mode 100644 index 707f7f69a4..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EEPROM_DATA.cs +++ /dev/null @@ -1,82 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - - -using System; - - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // Base class for EEPROM structures - these elements are common to all devices - /// - /// Common EEPROM elements for all devices. Inherited to specific device type EEPROMs. - /// - public class FT_EEPROM_DATA - { - //private const UInt32 Signature1 = 0x00000000; - //private const UInt32 Signature2 = 0xFFFFFFFF; - //private const UInt32 Version = 0x00000002; - /// - /// Vendor ID as supplied by the USB Implementers Forum - /// - public UInt16 VendorID = 0x0403; - /// - /// Product ID - /// - public UInt16 ProductID = 0x6001; - /// - /// Manufacturer name string - /// - public string Manufacturer = "FTDI"; - /// - /// Manufacturer name abbreviation to be used as a prefix for automatically generated serial numbers - /// - public string ManufacturerID = "FT"; - /// - /// Device description string - /// - public string Description = "USB-Serial Converter"; - /// - /// Device serial number string - /// - public string SerialNumber = ""; - /// - /// Maximum power the device needs - /// - public UInt16 MaxPower = 0x0090; - //private bool PnP = true; - /// - /// Indicates if the device has its own power supply (self-powered) or gets power from the USB port (bus-powered) - /// - public bool SelfPowered = false; - /// - /// Determines if the device can wake the host PC from suspend by toggling the RI line - /// - public bool RemoteWakeup = false; - } - -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EEPROM_HEADER.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EEPROM_HEADER.cs deleted file mode 100644 index 67fd545b9a..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EEPROM_HEADER.cs +++ /dev/null @@ -1,51 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - - -using System; -using System.Runtime.InteropServices; - - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - [StructLayout(LayoutKind.Sequential, Pack = 4)] - private struct FT_EEPROM_HEADER - { - public UInt32 deviceType; // FTxxxx device type to be programmed - // Device descriptor options - public UInt16 VendorId; // 0x0403 - public UInt16 ProductId; // 0x6001 - public byte SerNumEnable; // non-zero if serial number to be used - // Config descriptor options - public UInt16 MaxPower; // 0 < MaxPower <= 500 - public byte SelfPowered; // 0 = bus powered, 1 = self powered - public byte RemoteWakeup; // 0 = not capable, 1 = capable - // Hardware options - public byte PullDownEnable; // non-zero if pull down in suspend enabled - } - -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_ERROR.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_ERROR.cs deleted file mode 100644 index 5334715b91..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_ERROR.cs +++ /dev/null @@ -1,42 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // Constants for other error states internal to this class library - /// - /// Error states not supported by FTD2XX DLL. - /// - private enum FT_ERROR - { - FT_NO_ERROR = 0, - FT_INCORRECT_DEVICE, - FT_INVALID_BITMODE, - FT_BUFFER_SIZE - }; - -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EVENTS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EVENTS.cs deleted file mode 100644 index 3ef585eb46..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EVENTS.cs +++ /dev/null @@ -1,54 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - - -using System; - - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // Events - /// - /// FTDI device event types that can be monitored - /// - public class FT_EVENTS - { - /// - /// Event on receive character - /// - public const UInt32 FT_EVENT_RXCHAR = 0x00000001; - /// - /// Event on modem status change - /// - public const UInt32 FT_EVENT_MODEM_STATUS = 0x00000002; - /// - /// Event on line status change - /// - public const UInt32 FT_EVENT_LINE_STATUS = 0x00000004; - } - -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EXCEPTION.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EXCEPTION.cs deleted file mode 100644 index 6f6e1200bb..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_EXCEPTION.cs +++ /dev/null @@ -1,66 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - - -using System; - - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - /// - /// Exceptions thrown by errors within the FTDI class. - /// - [global::System.Serializable] - public class FT_EXCEPTION : Exception - { - /// - /// - /// - public FT_EXCEPTION() { } - /// - /// - /// - /// - public FT_EXCEPTION(string message) : base(message) { } - /// - /// - /// - /// - /// - public FT_EXCEPTION(string message, Exception inner) : base(message, inner) { } - /// - /// - /// - /// - /// - protected FT_EXCEPTION( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) - : base(info, context) { } - } - -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_FLAGS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_FLAGS.cs deleted file mode 100644 index 262ef59ff1..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_FLAGS.cs +++ /dev/null @@ -1,50 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - - -using System; - - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // Flag values for FT_GetDeviceInfoDetail and FT_GetDeviceInfo - /// - /// Flags that provide information on the FTDI device state - /// - public class FT_FLAGS - { - /// - /// Indicates that the device is open - /// - public const UInt32 FT_FLAGS_OPENED = 0x00000001; - /// - /// Indicates that the device is enumerated as a hi-speed USB device - /// - public const UInt32 FT_FLAGS_HISPEED = 0x00000002; - } - -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_FLOW_CONTROL.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_FLOW_CONTROL.cs deleted file mode 100644 index f8786046e7..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_FLOW_CONTROL.cs +++ /dev/null @@ -1,58 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - - -using System; - - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // Flow Control - /// - /// Permitted flow control values for FTDI devices - /// - public class FT_FLOW_CONTROL - { - /// - /// No flow control - /// - public const UInt16 FT_FLOW_NONE = 0x0000; - /// - /// RTS/CTS flow control - /// - public const UInt16 FT_FLOW_RTS_CTS = 0x0100; - /// - /// DTR/DSR flow control - /// - public const UInt16 FT_FLOW_DTR_DSR = 0x0200; - /// - /// Xon/Xoff flow control - /// - public const UInt16 FT_FLOW_XON_XOFF = 0x0400; - } - -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_LINE_STATUS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_LINE_STATUS.cs deleted file mode 100644 index 0145f3d29f..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_LINE_STATUS.cs +++ /dev/null @@ -1,54 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // Line Status bits - /// - /// Line status bit definitions - /// - public class FT_LINE_STATUS - { - /// - /// Overrun Error (OE) line status - /// - public const byte FT_OE = 0x02; - /// - /// Parity Error (PE) line status - /// - public const byte FT_PE = 0x04; - /// - /// Framing Error (FE) line status - /// - public const byte FT_FE = 0x08; - /// - /// Break Interrupt (BI) line status - /// - public const byte FT_BI = 0x10; - } - -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_MODEM_STATUS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_MODEM_STATUS.cs deleted file mode 100644 index e661aae62a..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_MODEM_STATUS.cs +++ /dev/null @@ -1,54 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // Modem Status bits - /// - /// Modem status bit definitions - /// - public class FT_MODEM_STATUS - { - /// - /// Clear To Send (CTS) modem status - /// - public const byte FT_CTS = 0x10; - /// - /// Data Set Ready (DSR) modem status - /// - public const byte FT_DSR = 0x20; - /// - /// Ring Indicator (RI) modem status - /// - public const byte FT_RI = 0x40; - /// - /// Data Carrier Detect (DCD) modem status - /// - public const byte FT_DCD = 0x80; - } - -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_OPCODE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_OPCODE.cs deleted file mode 100644 index ba794154a1..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_OPCODE.cs +++ /dev/null @@ -1,91 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - internal enum FT_OPCODE - { - ClockDataBytesOutOnPlusVeClockMSBFirst = 0x10, - ClockDataBytesOutOnMinusVeClockMSBFirst = 0x11, - ClockDataBitsOutOnPlusVeClockMSBFirst = 0x12, - ClockDataBitsOutOnMinusVeClockMSBFirst = 0x13, - ClockDataBytesInOnPlusVeClockMSBFirst = 0x20, - ClockDataBytesInOnMinusVeClockMSBFirst = 0x24, - ClockDataBitsInOnPlusVeClockMSBFirst = 0x22, - ClockDataBitsInOnMinusVeClockMSBFirst = 0x26, - ClockDataBytesOutOnMinusBytesInOnPlusVeClockMSBFirst = 0x31, - ClockDataBytesOutOnPlusBytesInOnMinusVeClockMSBFirst = 0x34, - ClockDataBitsOutOnMinusBitsInOnPlusVeClockMSBFirst = 0x33, - ClockDataBitsOutOnPlusBitsInOnMinusVeClockMSBFirst = 0x36, - ClockDataBytesOutOnPlusVeClockLSBFirst = 0x18, - ClockDataBytesOutOnMinusVeClockLSBFirst = 0x19, - ClockDataBitsOutOnPlusVeClockLSBFirst = 0x1A, - ClockDataBitsOutOnMinusVeClockLSBFirst = 0x1B, - ClockDataBytesInOnPlusVeClockLSBFirst = 0x28, - ClockDataBytesInOnMinusVeClockLSBFirst = 0x2C, - ClockDataBitsInOnPlusVeClockLSBFirst = 0x2A, - ClockDataBitsInOnMinusVeClockSBFirst = 0x2E, - ClockDataBytesOutOnMinusBytesInOnPlusVeClockLSBFirst = 0x39, - ClockDataBytesOutOnPlusBytesInOnMinusVeClockLSBFirst = 0x3C, - ClockDataBitsOutOnMinusBitsInOnPlusVeClockLSBFirst = 0x3B, - ClockDataBitsOutOnPlusBitsInOnMinusVeClockLSBFirst = 0x3E, - ClockDataBytesOutOnPlusVeClockTMSPinLSBFirst = 0x4A, - ClockDataBytesOutOnMinusVeClockTMSPinSBFirst = 0x4B, - ClockDataBytesOutOnPlusDataInOnPlusVeClockTMSPinSBFirst = 0x6A, - ClockDataBytesOutOnMinusDataInOnPlusVeClockTMSPinSBFirst = 0x6B, - ClockDataBytesOutOnPlusDataInOnMinusVeClockTMSPinSBFirst = 0x6E, - ClockDataBytesOutOnMinusDataInOnMinusVeClockTMSPinSBFirst = 0x6F, - SetDataBitsLowByte = 0x80, - SetDataBitsHighByte = 0x82, - ReadDataBitsLowByte = 0x81, - ReadDataBitsHighByte = 0x83, - ConnectTDItoTDOforLoopback = 0x84, - DisconnectTDItoTDOforLoopback = 0x85, - SetTCKSKDivisor = 0x86, - SetClockDivisor = 0x86, - CPUModeReadShortAddress = 0x90, - CPUModeReadExtendedAddress = 0x91, - CPUModeWriteShortAddress = 0x92, - CPUModeWriteExtendedAddress = 0x93, - SendImmediate = 0x87, - WaitOnIOHigh = 0x88, - WaitOnIOLow = 0x89, - DisableClockDivideBy5 = 0x8A, - EnableClockDivideBy5 = 0x8B, - Enable3PhaseDataClocking = 0x8C, - Disable3PhaseDataClocking = 0x8D, - ClockForNBitsWithNoDataTransfer = 0x8E, - ClockForNx8BitsWithNoDataTransfer = 0x8F, - ClockContinuouslyAndWaitOnIOHigh = 0x94, - ClockContinuouslyAndWaitOnIOLow = 0x95, - TurnOnAdaptiveClocking = 0x96, - TurnOffAdaptiveClocking = 0x97, - ClockForNx8BitsWithNoDataTransferOrUntilGPIOL1IsHigh = 0x9C, - ClockForNx8BitsWithNoDataTransferOrUntilGPIOL1IsLow = 0x9D, - SetIOOnlyDriveOn0AndTristateOn1 = 0x9E, - } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_PARITY.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_PARITY.cs deleted file mode 100644 index 0538201d62..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_PARITY.cs +++ /dev/null @@ -1,58 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // Parity - /// - /// Permitted parity values for FTDI devices - /// - public class FT_PARITY - { - /// - /// No parity - /// - public const byte FT_PARITY_NONE = 0x00; - /// - /// Odd parity - /// - public const byte FT_PARITY_ODD = 0x01; - /// - /// Even parity - /// - public const byte FT_PARITY_EVEN = 0x02; - /// - /// Mark parity - /// - public const byte FT_PARITY_MARK = 0x03; - /// - /// Space parity - /// - public const byte FT_PARITY_SPACE = 0x04; - } - -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_PROGRAM_DATA.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_PROGRAM_DATA.cs deleted file mode 100644 index 1581c0fae6..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_PROGRAM_DATA.cs +++ /dev/null @@ -1,180 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - - -using System; -using System.Runtime.InteropServices; - - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // Internal structure for reading and writing EEPROM contents - // NOTE: NEED Pack=1 for byte alignment! Without this, data is garbage - [StructLayout(LayoutKind.Sequential, Pack = 4)] - private class FT_PROGRAM_DATA - { - public UInt32 Signature1; - public UInt32 Signature2; - public UInt32 Version; - public UInt16 VendorID; - public UInt16 ProductID; - - public IntPtr Manufacturer; - public IntPtr ManufacturerID; - public IntPtr Description; - public IntPtr SerialNumber; - - public UInt16 MaxPower; - public UInt16 PnP; - public UInt16 SelfPowered; - public UInt16 RemoteWakeup; - // FT232B extensions - public byte Rev4; - public byte IsoIn; - public byte IsoOut; - public byte PullDownEnable; - public byte SerNumEnable; - public byte USBVersionEnable; - public UInt16 USBVersion; - // FT2232D extensions - public byte Rev5; - public byte IsoInA; - public byte IsoInB; - public byte IsoOutA; - public byte IsoOutB; - public byte PullDownEnable5; - public byte SerNumEnable5; - public byte USBVersionEnable5; - public UInt16 USBVersion5; - public byte AIsHighCurrent; - public byte BIsHighCurrent; - public byte IFAIsFifo; - public byte IFAIsFifoTar; - public byte IFAIsFastSer; - public byte AIsVCP; - public byte IFBIsFifo; - public byte IFBIsFifoTar; - public byte IFBIsFastSer; - public byte BIsVCP; - // FT232R extensions - public byte UseExtOsc; - public byte HighDriveIOs; - public byte EndpointSize; - public byte PullDownEnableR; - public byte SerNumEnableR; - public byte InvertTXD; // non-zero if invert TXD - public byte InvertRXD; // non-zero if invert RXD - public byte InvertRTS; // non-zero if invert RTS - public byte InvertCTS; // non-zero if invert CTS - public byte InvertDTR; // non-zero if invert DTR - public byte InvertDSR; // non-zero if invert DSR - public byte InvertDCD; // non-zero if invert DCD - public byte InvertRI; // non-zero if invert RI - public byte Cbus0; // Cbus Mux control - Ignored for FT245R - public byte Cbus1; // Cbus Mux control - Ignored for FT245R - public byte Cbus2; // Cbus Mux control - Ignored for FT245R - public byte Cbus3; // Cbus Mux control - Ignored for FT245R - public byte Cbus4; // Cbus Mux control - Ignored for FT245R - public byte RIsD2XX; // Default to loading VCP - // FT2232H extensions - public byte PullDownEnable7; - public byte SerNumEnable7; - public byte ALSlowSlew; // non-zero if AL pins have slow slew - public byte ALSchmittInput; // non-zero if AL pins are Schmitt input - public byte ALDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - public byte AHSlowSlew; // non-zero if AH pins have slow slew - public byte AHSchmittInput; // non-zero if AH pins are Schmitt input - public byte AHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - public byte BLSlowSlew; // non-zero if BL pins have slow slew - public byte BLSchmittInput; // non-zero if BL pins are Schmitt input - public byte BLDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - public byte BHSlowSlew; // non-zero if BH pins have slow slew - public byte BHSchmittInput; // non-zero if BH pins are Schmitt input - public byte BHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - public byte IFAIsFifo7; // non-zero if interface is 245 FIFO - public byte IFAIsFifoTar7; // non-zero if interface is 245 FIFO CPU target - public byte IFAIsFastSer7; // non-zero if interface is Fast serial - public byte AIsVCP7; // non-zero if interface is to use VCP drivers - public byte IFBIsFifo7; // non-zero if interface is 245 FIFO - public byte IFBIsFifoTar7; // non-zero if interface is 245 FIFO CPU target - public byte IFBIsFastSer7; // non-zero if interface is Fast serial - public byte BIsVCP7; // non-zero if interface is to use VCP drivers - public byte PowerSaveEnable; // non-zero if using BCBUS7 to save power for self-powered designs - // FT4232H extensions - public byte PullDownEnable8; - public byte SerNumEnable8; - public byte ASlowSlew; // non-zero if AL pins have slow slew - public byte ASchmittInput; // non-zero if AL pins are Schmitt input - public byte ADriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - public byte BSlowSlew; // non-zero if AH pins have slow slew - public byte BSchmittInput; // non-zero if AH pins are Schmitt input - public byte BDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - public byte CSlowSlew; // non-zero if BL pins have slow slew - public byte CSchmittInput; // non-zero if BL pins are Schmitt input - public byte CDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - public byte DSlowSlew; // non-zero if BH pins have slow slew - public byte DSchmittInput; // non-zero if BH pins are Schmitt input - public byte DDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - public byte ARIIsTXDEN; - public byte BRIIsTXDEN; - public byte CRIIsTXDEN; - public byte DRIIsTXDEN; - public byte AIsVCP8; // non-zero if interface is to use VCP drivers - public byte BIsVCP8; // non-zero if interface is to use VCP drivers - public byte CIsVCP8; // non-zero if interface is to use VCP drivers - public byte DIsVCP8; // non-zero if interface is to use VCP drivers - // FT232H extensions - public byte PullDownEnableH; // non-zero if pull down enabled - public byte SerNumEnableH; // non-zero if serial number to be used - public byte ACSlowSlewH; // non-zero if AC pins have slow slew - public byte ACSchmittInputH; // non-zero if AC pins are Schmitt input - public byte ACDriveCurrentH; // valid values are 4mA, 8mA, 12mA, 16mA - public byte ADSlowSlewH; // non-zero if AD pins have slow slew - public byte ADSchmittInputH; // non-zero if AD pins are Schmitt input - public byte ADDriveCurrentH; // valid values are 4mA, 8mA, 12mA, 16mA - public byte Cbus0H; // Cbus Mux control - public byte Cbus1H; // Cbus Mux control - public byte Cbus2H; // Cbus Mux control - public byte Cbus3H; // Cbus Mux control - public byte Cbus4H; // Cbus Mux control - public byte Cbus5H; // Cbus Mux control - public byte Cbus6H; // Cbus Mux control - public byte Cbus7H; // Cbus Mux control - public byte Cbus8H; // Cbus Mux control - public byte Cbus9H; // Cbus Mux control - public byte IsFifoH; // non-zero if interface is 245 FIFO - public byte IsFifoTarH; // non-zero if interface is 245 FIFO CPU target - public byte IsFastSerH; // non-zero if interface is Fast serial - public byte IsFT1248H; // non-zero if interface is FT1248 - public byte FT1248CpolH; // FT1248 clock polarity - public byte FT1248LsbH; // FT1248 data is LSB (1) or MSB (0) - public byte FT1248FlowControlH; // FT1248 flow control enable - public byte IsVCPH; // non-zero if interface is to use VCP drivers - public byte PowerSaveEnableH; // non-zero if using ACBUS7 to save power for self-powered designs - } - -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_PURGE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_PURGE.cs deleted file mode 100644 index d8b8f7cce3..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_PURGE.cs +++ /dev/null @@ -1,46 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // Purge Rx and Tx buffers - /// - /// Purge buffer constant definitions - /// - public class FT_PURGE - { - /// - /// Purge Rx buffer - /// - public const byte FT_PURGE_RX = 0x01; - /// - /// Purge Tx buffer - /// - public const byte FT_PURGE_TX = 0x02; - } - -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_STATUS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_STATUS.cs deleted file mode 100644 index 42e5d380d8..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_STATUS.cs +++ /dev/null @@ -1,110 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // Constants for FT_STATUS - /// - /// Status values for FTDI devices. - /// - public enum FT_STATUS - { - /// - /// Status OK - /// - FT_OK = 0, - /// - /// The device handle is invalid - /// - FT_INVALID_HANDLE, - /// - /// Device not found - /// - FT_DEVICE_NOT_FOUND, - /// - /// Device is not open - /// - FT_DEVICE_NOT_OPENED, - /// - /// IO error - /// - FT_IO_ERROR, - /// - /// Insufficient resources - /// - FT_INSUFFICIENT_RESOURCES, - /// - /// A parameter was invalid - /// - FT_INVALID_PARAMETER, - /// - /// The requested baud rate is invalid - /// - FT_INVALID_BAUD_RATE, - /// - /// Device not opened for erase - /// - FT_DEVICE_NOT_OPENED_FOR_ERASE, - /// - /// Device not poened for write - /// - FT_DEVICE_NOT_OPENED_FOR_WRITE, - /// - /// Failed to write to device - /// - FT_FAILED_TO_WRITE_DEVICE, - /// - /// Failed to read the device EEPROM - /// - FT_EEPROM_READ_FAILED, - /// - /// Failed to write the device EEPROM - /// - FT_EEPROM_WRITE_FAILED, - /// - /// Failed to erase the device EEPROM - /// - FT_EEPROM_ERASE_FAILED, - /// - /// An EEPROM is not fitted to the device - /// - FT_EEPROM_NOT_PRESENT, - /// - /// Device EEPROM is blank - /// - FT_EEPROM_NOT_PROGRAMMED, - /// - /// Invalid arguments - /// - FT_INVALID_ARGS, - /// - /// An other error has occurred - /// - FT_OTHER_ERROR - }; - -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_STOP_BITS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_STOP_BITS.cs deleted file mode 100644 index 90e1a66af5..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_STOP_BITS.cs +++ /dev/null @@ -1,46 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - // Stop Bits - /// - /// Permitted stop bits for FTDI devices - /// - public class FT_STOP_BITS - { - /// - /// 1 stop bit - /// - public const byte FT_STOP_BITS_1 = 0x00; - /// - /// 2 stop bits - /// - public const byte FT_STOP_BITS_2 = 0x02; - } - -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_XSERIES_CBUS_OPTIONS.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_XSERIES_CBUS_OPTIONS.cs deleted file mode 100644 index c60a5e2385..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_XSERIES_CBUS_OPTIONS.cs +++ /dev/null @@ -1,125 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - /// - /// Available functions for the X-Series CBUS pins. Controlled by X-Series EEPROM settings - /// - public class FT_XSERIES_CBUS_OPTIONS - { - /// - /// FT X-Series CBUS EEPROM options - Tristate - /// - public const byte FT_CBUS_TRISTATE = 0x00; - /// - /// FT X-Series CBUS EEPROM options - RxLED# - /// - public const byte FT_CBUS_RXLED = 0x01; - /// - /// FT X-Series CBUS EEPROM options - TxLED# - /// - public const byte FT_CBUS_TXLED = 0x02; - /// - /// FT X-Series CBUS EEPROM options - TxRxLED# - /// - public const byte FT_CBUS_TXRXLED = 0x03; - /// - /// FT X-Series CBUS EEPROM options - PwrEn# - /// - public const byte FT_CBUS_PWREN = 0x04; - /// - /// FT X-Series CBUS EEPROM options - Sleep# - /// - public const byte FT_CBUS_SLEEP = 0x05; - /// - /// FT X-Series CBUS EEPROM options - Drive_0 - /// - public const byte FT_CBUS_Drive_0 = 0x06; - /// - /// FT X-Series CBUS EEPROM options - Drive_1 - /// - public const byte FT_CBUS_Drive_1 = 0x07; - /// - /// FT X-Series CBUS EEPROM options - GPIO - /// - public const byte FT_CBUS_GPIO = 0x08; - /// - /// FT X-Series CBUS EEPROM options - TxdEn - /// - public const byte FT_CBUS_TXDEN = 0x09; - /// - /// FT X-Series CBUS EEPROM options - Clk24MHz - /// - public const byte FT_CBUS_CLK24MHz = 0x0A; - /// - /// FT X-Series CBUS EEPROM options - Clk12MHz - /// - public const byte FT_CBUS_CLK12MHz = 0x0B; - /// - /// FT X-Series CBUS EEPROM options - Clk6MHz - /// - public const byte FT_CBUS_CLK6MHz = 0x0C; - /// - /// FT X-Series CBUS EEPROM options - BCD_Charger - /// - public const byte FT_CBUS_BCD_Charger = 0x0D; - /// - /// FT X-Series CBUS EEPROM options - BCD_Charger# - /// - public const byte FT_CBUS_BCD_Charger_N = 0x0E; - /// - /// FT X-Series CBUS EEPROM options - I2C_TXE# - /// - public const byte FT_CBUS_I2C_TXE = 0x0F; - /// - /// FT X-Series CBUS EEPROM options - I2C_RXF# - /// - public const byte FT_CBUS_I2C_RXF = 0x10; - /// - /// FT X-Series CBUS EEPROM options - VBUS_Sense - /// - public const byte FT_CBUS_VBUS_Sense = 0x11; - /// - /// FT X-Series CBUS EEPROM options - BitBang_WR# - /// - public const byte FT_CBUS_BitBang_WR = 0x12; - /// - /// FT X-Series CBUS EEPROM options - BitBang_RD# - /// - public const byte FT_CBUS_BitBang_RD = 0x13; - /// - /// FT X-Series CBUS EEPROM options - Time_Stampe - /// - public const byte FT_CBUS_Time_Stamp = 0x14; - /// - /// FT X-Series CBUS EEPROM options - Keep_Awake# - /// - public const byte FT_CBUS_Keep_Awake = 0x15; - } - -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_XSERIES_DATA.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_XSERIES_DATA.cs deleted file mode 100644 index 3cad746e44..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_XSERIES_DATA.cs +++ /dev/null @@ -1,82 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - - -using System; -using System.Runtime.InteropServices; - - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - [StructLayout(LayoutKind.Sequential, Pack = 4)] - private struct FT_XSERIES_DATA - { - public FT_EEPROM_HEADER common; - - public byte ACSlowSlew; // non-zero if AC bus pins have slow slew - public byte ACSchmittInput; // non-zero if AC bus pins are Schmitt input - public byte ACDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - public byte ADSlowSlew; // non-zero if AD bus pins have slow slew - public byte ADSchmittInput; // non-zero if AD bus pins are Schmitt input - public byte ADDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - // CBUS options - public byte Cbus0; // Cbus Mux control - public byte Cbus1; // Cbus Mux control - public byte Cbus2; // Cbus Mux control - public byte Cbus3; // Cbus Mux control - public byte Cbus4; // Cbus Mux control - public byte Cbus5; // Cbus Mux control - public byte Cbus6; // Cbus Mux control - // UART signal options - public byte InvertTXD; // non-zero if invert TXD - public byte InvertRXD; // non-zero if invert RXD - public byte InvertRTS; // non-zero if invert RTS - public byte InvertCTS; // non-zero if invert CTS - public byte InvertDTR; // non-zero if invert DTR - public byte InvertDSR; // non-zero if invert DSR - public byte InvertDCD; // non-zero if invert DCD - public byte InvertRI; // non-zero if invert RI - // Battery Charge Detect options - public byte BCDEnable; // Enable Battery Charger Detection - public byte BCDForceCbusPWREN; // asserts the power enable signal on CBUS when charging port detected - public byte BCDDisableSleep; // forces the device never to go into sleep mode - // I2C options - public UInt16 I2CSlaveAddress; // I2C slave device address - public UInt32 I2CDeviceId; // I2C device ID - public byte I2CDisableSchmitt; // Disable I2C Schmitt trigger - // FT1248 options - public byte FT1248Cpol; // FT1248 clock polarity - clock idle high (1) or clock idle low (0) - public byte FT1248Lsb; // FT1248 data is LSB (1) or MSB (0) - public byte FT1248FlowControl; // FT1248 flow control enable - // Hardware options - public byte RS485EchoSuppress; // - public byte PowerSaveEnable; // - // Driver option - public byte DriverType; // - } - -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_XSERIES_EEPROM_STRUCTURE.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_XSERIES_EEPROM_STRUCTURE.cs deleted file mode 100644 index f84729cb98..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.FT_XSERIES_EEPROM_STRUCTURE.cs +++ /dev/null @@ -1,218 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - - -using System; - - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class FTDI -{ - /// - /// EEPROM structure specific to X-Series devices. - /// Inherits from FT_EEPROM_DATA. - /// - public class FT_XSERIES_EEPROM_STRUCTURE : FT_EEPROM_DATA - { - /// - /// Determines if IOs are pulled down when the device is in suspend - /// - public bool PullDownEnable = false; - /// - /// Determines if the serial number is enabled - /// - public bool SerNumEnable = true; - /// - /// Determines if the USB version number is enabled - /// - public bool USBVersionEnable = true; - /// - /// The USB version number: 0x0200 (USB 2.0) - /// - public UInt16 USBVersion = 0x0200; - /// - /// Determines if AC pins have a slow slew rate - /// - public byte ACSlowSlew; - /// - /// Determines if the AC pins have a Schmitt input - /// - public byte ACSchmittInput; - /// - /// Determines the AC pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA - /// - public byte ACDriveCurrent; - /// - /// Determines if AD pins have a slow slew rate - /// - public byte ADSlowSlew; - /// - /// Determines if AD pins have a schmitt input - /// - public byte ADSchmittInput; - /// - /// Determines the AD pins drive current in mA. Valid values are FT_DRIVE_CURRENT_4MA, FT_DRIVE_CURRENT_8MA, FT_DRIVE_CURRENT_12MA or FT_DRIVE_CURRENT_16MA - /// - public byte ADDriveCurrent; - /// - /// Sets the function of the CBUS0 pin for FT232H devices. - /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, - /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_GPIO, FT_CBUS_TXDEN, FT_CBUS_CLK24, - /// FT_CBUS_CLK12, FT_CBUS_CLK6, FT_CBUS_BCD_CHARGER, FT_CBUS_BCD_CHARGER_N, FT_CBUS_VBUS_SENSE, FT_CBUS_BITBANG_WR, - /// FT_CBUS_BITBANG_RD, FT_CBUS_TIME_STAMP, FT_CBUS_KEEP_AWAKE - /// - public byte Cbus0; - /// - /// Sets the function of the CBUS1 pin for FT232H devices. - /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, - /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_GPIO, FT_CBUS_TXDEN, FT_CBUS_CLK24, - /// FT_CBUS_CLK12, FT_CBUS_CLK6, FT_CBUS_BCD_CHARGER, FT_CBUS_BCD_CHARGER_N, FT_CBUS_VBUS_SENSE, FT_CBUS_BITBANG_WR, - /// FT_CBUS_BITBANG_RD, FT_CBUS_TIME_STAMP, FT_CBUS_KEEP_AWAKE - /// - public byte Cbus1; - /// - /// Sets the function of the CBUS2 pin for FT232H devices. - /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, - /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_GPIO, FT_CBUS_TXDEN, FT_CBUS_CLK24, - /// FT_CBUS_CLK12, FT_CBUS_CLK6, FT_CBUS_BCD_CHARGER, FT_CBUS_BCD_CHARGER_N, FT_CBUS_VBUS_SENSE, FT_CBUS_BITBANG_WR, - /// FT_CBUS_BITBANG_RD, FT_CBUS_TIME_STAMP, FT_CBUS_KEEP_AWAKE - /// - public byte Cbus2; - /// - /// Sets the function of the CBUS3 pin for FT232H devices. - /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, - /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_GPIO, FT_CBUS_TXDEN, FT_CBUS_CLK24, - /// FT_CBUS_CLK12, FT_CBUS_CLK6, FT_CBUS_BCD_CHARGER, FT_CBUS_BCD_CHARGER_N, FT_CBUS_VBUS_SENSE, FT_CBUS_BITBANG_WR, - /// FT_CBUS_BITBANG_RD, FT_CBUS_TIME_STAMP, FT_CBUS_KEEP_AWAKE - /// - public byte Cbus3; - /// - /// Sets the function of the CBUS4 pin for FT232H devices. - /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, - /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN, FT_CBUS_CLK24, - /// FT_CBUS_CLK12, FT_CBUS_CLK6, FT_CBUS_BCD_CHARGER, FT_CBUS_BCD_CHARGER_N, FT_CBUS_VBUS_SENSE, FT_CBUS_BITBANG_WR, - /// FT_CBUS_BITBANG_RD, FT_CBUS_TIME_STAMP, FT_CBUS_KEEP_AWAKE - /// - public byte Cbus4; - /// - /// Sets the function of the CBUS5 pin for FT232H devices. - /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, - /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN, FT_CBUS_CLK24, - /// FT_CBUS_CLK12, FT_CBUS_CLK6, FT_CBUS_BCD_CHARGER, FT_CBUS_BCD_CHARGER_N, FT_CBUS_VBUS_SENSE, FT_CBUS_BITBANG_WR, - /// FT_CBUS_BITBANG_RD, FT_CBUS_TIME_STAMP, FT_CBUS_KEEP_AWAKE - /// - public byte Cbus5; - /// - /// Sets the function of the CBUS6 pin for FT232H devices. - /// Valid values are FT_CBUS_TRISTATE, FT_CBUS_RXLED, FT_CBUS_TXLED, FT_CBUS_TXRXLED, - /// FT_CBUS_PWREN, FT_CBUS_SLEEP, FT_CBUS_DRIVE_0, FT_CBUS_DRIVE_1, FT_CBUS_TXDEN, FT_CBUS_CLK24, - /// FT_CBUS_CLK12, FT_CBUS_CLK6, FT_CBUS_BCD_CHARGER, FT_CBUS_BCD_CHARGER_N, FT_CBUS_VBUS_SENSE, FT_CBUS_BITBANG_WR, - /// FT_CBUS_BITBANG_RD, FT_CBUS_TIME_STAMP, FT_CBUS_KEEP_AWAKE - /// - public byte Cbus6; - /// - /// Inverts the sense of the TXD line - /// - public byte InvertTXD; - /// - /// Inverts the sense of the RXD line - /// - public byte InvertRXD; - /// - /// Inverts the sense of the RTS line - /// - public byte InvertRTS; - /// - /// Inverts the sense of the CTS line - /// - public byte InvertCTS; - /// - /// Inverts the sense of the DTR line - /// - public byte InvertDTR; - /// - /// Inverts the sense of the DSR line - /// - public byte InvertDSR; - /// - /// Inverts the sense of the DCD line - /// - public byte InvertDCD; - /// - /// Inverts the sense of the RI line - /// - public byte InvertRI; - /// - /// Determines whether the Battery Charge Detection option is enabled. - /// - public byte BCDEnable; - /// - /// Asserts the power enable signal on CBUS when charging port detected. - /// - public byte BCDForceCbusPWREN; - /// - /// Forces the device never to go into sleep mode. - /// - public byte BCDDisableSleep; - /// - /// I2C slave device address. - /// - public ushort I2CSlaveAddress; - /// - /// I2C device ID - /// - public UInt32 I2CDeviceId; - /// - /// Disable I2C Schmitt trigger. - /// - public byte I2CDisableSchmitt; - /// - /// FT1248 clock polarity - clock idle high (1) or clock idle low (0) - /// - public byte FT1248Cpol; - /// - /// FT1248 data is LSB (1) or MSB (0) - /// - public byte FT1248Lsb; - /// - /// FT1248 flow control enable. - /// - public byte FT1248FlowControl; - /// - /// Enable RS485 Echo Suppression - /// - public byte RS485EchoSuppress; - /// - /// Enable Power Save mode. - /// - public byte PowerSaveEnable; - /// - /// Determines whether the VCP driver is loaded. - /// - public byte IsVCP; - } - -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.cs deleted file mode 100644 index 36d9e16b9b..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FTDI/FTDI.cs +++ /dev/null @@ -1,4901 +0,0 @@ -/* -** FTD2XX_NET.cs -** -** Copyright © 2009-2021 Future Technology Devices International Limited -** -** C# Source file for .NET wrapper of the Windows FTD2XX.dll API calls. -** Main module -** -** Author: FTDI -** Project: CDM Windows Driver Package -** Module: FTD2XX_NET Managed Wrapper -** Requires: -** Comments: -** -** History: -** 1.0.0 - Initial version -** 1.0.12 - Included support for the FT232H device. -** 1.0.14 - Included Support for the X-Series of devices. -** 1.0.16 - Overloaded constructor to allow a path to the driver to be passed. -** 1.1.0 - Handle full 16 character Serial Number and support FT4222 programming board. -** 1.1.2 - Add new devices and change NULL string for .NET 5 compaibility. - -** Ported to NetStandard 2.1 2024, Wilderness Labs -*/ - - -using System; -using System.IO; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading; - - -namespace Meadow.Foundation.ICs.IOExpanders; - -/// -/// Class wrapper for FTD2XX.DLL -/// -internal partial class FTDI -{ - // constructor - /// - /// Constructor for the FTDI class. - /// - public FTDI() - { - // If FTD2XX.DLL is NOT loaded already, load it - if (hFTD2XXDLL == IntPtr.Zero) - { - // Load our FTD2XX.DLL library - hFTD2XXDLL = LoadLibrary(@"FTD2XX.DLL"); - if (hFTD2XXDLL == IntPtr.Zero) - { - // Failed to load our FTD2XX.DLL library from System32 or the application directory - // Try the same directory that this FTD2XX_NET DLL is in - Console.WriteLine("Attempting to load FTD2XX.DLL from:\n" + Path.GetDirectoryName(GetType().Assembly.Location)); - hFTD2XXDLL = LoadLibrary(@Path.GetDirectoryName(GetType().Assembly.Location) + "\\FTD2XX.DLL"); - } - } - - // If we have succesfully loaded the library, get the function pointers set up - if (hFTD2XXDLL != IntPtr.Zero) - { - FindFunctionPointers(); - } - else - { - // Failed to load our DLL - alert the user - Console.WriteLine("Failed to load FTD2XX.DLL. Are the FTDI drivers installed?"); - } - } - - /// - /// Non default constructor allowing passing of string for dll handle. - /// - public FTDI(String path) - { - // If nonstandard.DLL is NOT loaded already, load it - if (path == "") - return; - - if (hFTD2XXDLL == IntPtr.Zero) - { - // Load our nonstandard.DLL library - hFTD2XXDLL = LoadLibrary(path); - if (hFTD2XXDLL == IntPtr.Zero) - { - // Failed to load our PathToDll library - // Give up :( - Console.WriteLine("Attempting to load FTD2XX.DLL from:\n" + Path.GetDirectoryName(GetType().Assembly.Location)); - } - } - - // If we have succesfully loaded the library, get the function pointers set up - if (hFTD2XXDLL != IntPtr.Zero) - { - FindFunctionPointers(); - } - else - { - Console.WriteLine("Failed to load FTD2XX.DLL. Are the FTDI drivers installed?"); - } - } - - private void FindFunctionPointers() - { - // Set up our function pointers for use through our exported methods - pFT_CreateDeviceInfoList = GetProcAddress(hFTD2XXDLL, "FT_CreateDeviceInfoList"); - pFT_GetDeviceInfoDetail = GetProcAddress(hFTD2XXDLL, "FT_GetDeviceInfoDetail"); - pFT_Open = GetProcAddress(hFTD2XXDLL, "FT_Open"); - pFT_OpenEx = GetProcAddress(hFTD2XXDLL, "FT_OpenEx"); - pFT_Close = GetProcAddress(hFTD2XXDLL, "FT_Close"); - pFT_Read = GetProcAddress(hFTD2XXDLL, "FT_Read"); - pFT_Write = GetProcAddress(hFTD2XXDLL, "FT_Write"); - pFT_GetQueueStatus = GetProcAddress(hFTD2XXDLL, "FT_GetQueueStatus"); - pFT_GetModemStatus = GetProcAddress(hFTD2XXDLL, "FT_GetModemStatus"); - pFT_GetStatus = GetProcAddress(hFTD2XXDLL, "FT_GetStatus"); - pFT_SetBaudRate = GetProcAddress(hFTD2XXDLL, "FT_SetBaudRate"); - pFT_SetDataCharacteristics = GetProcAddress(hFTD2XXDLL, "FT_SetDataCharacteristics"); - pFT_SetFlowControl = GetProcAddress(hFTD2XXDLL, "FT_SetFlowControl"); - pFT_SetDtr = GetProcAddress(hFTD2XXDLL, "FT_SetDtr"); - pFT_ClrDtr = GetProcAddress(hFTD2XXDLL, "FT_ClrDtr"); - pFT_SetRts = GetProcAddress(hFTD2XXDLL, "FT_SetRts"); - pFT_ClrRts = GetProcAddress(hFTD2XXDLL, "FT_ClrRts"); - pFT_ResetDevice = GetProcAddress(hFTD2XXDLL, "FT_ResetDevice"); - pFT_ResetPort = GetProcAddress(hFTD2XXDLL, "FT_ResetPort"); - pFT_CyclePort = GetProcAddress(hFTD2XXDLL, "FT_CyclePort"); - pFT_Rescan = GetProcAddress(hFTD2XXDLL, "FT_Rescan"); - pFT_Reload = GetProcAddress(hFTD2XXDLL, "FT_Reload"); - pFT_Purge = GetProcAddress(hFTD2XXDLL, "FT_Purge"); - pFT_SetTimeouts = GetProcAddress(hFTD2XXDLL, "FT_SetTimeouts"); - pFT_SetBreakOn = GetProcAddress(hFTD2XXDLL, "FT_SetBreakOn"); - pFT_SetBreakOff = GetProcAddress(hFTD2XXDLL, "FT_SetBreakOff"); - pFT_GetDeviceInfo = GetProcAddress(hFTD2XXDLL, "FT_GetDeviceInfo"); - pFT_SetResetPipeRetryCount = GetProcAddress(hFTD2XXDLL, "FT_SetResetPipeRetryCount"); - pFT_StopInTask = GetProcAddress(hFTD2XXDLL, "FT_StopInTask"); - pFT_RestartInTask = GetProcAddress(hFTD2XXDLL, "FT_RestartInTask"); - pFT_GetDriverVersion = GetProcAddress(hFTD2XXDLL, "FT_GetDriverVersion"); - pFT_GetLibraryVersion = GetProcAddress(hFTD2XXDLL, "FT_GetLibraryVersion"); - pFT_SetDeadmanTimeout = GetProcAddress(hFTD2XXDLL, "FT_SetDeadmanTimeout"); - pFT_SetChars = GetProcAddress(hFTD2XXDLL, "FT_SetChars"); - pFT_SetEventNotification = GetProcAddress(hFTD2XXDLL, "FT_SetEventNotification"); - pFT_GetComPortNumber = GetProcAddress(hFTD2XXDLL, "FT_GetComPortNumber"); - pFT_SetLatencyTimer = GetProcAddress(hFTD2XXDLL, "FT_SetLatencyTimer"); - pFT_GetLatencyTimer = GetProcAddress(hFTD2XXDLL, "FT_GetLatencyTimer"); - pFT_SetBitMode = GetProcAddress(hFTD2XXDLL, "FT_SetBitMode"); - pFT_GetBitMode = GetProcAddress(hFTD2XXDLL, "FT_GetBitMode"); - pFT_SetUSBParameters = GetProcAddress(hFTD2XXDLL, "FT_SetUSBParameters"); - pFT_ReadEE = GetProcAddress(hFTD2XXDLL, "FT_ReadEE"); - pFT_WriteEE = GetProcAddress(hFTD2XXDLL, "FT_WriteEE"); - pFT_EraseEE = GetProcAddress(hFTD2XXDLL, "FT_EraseEE"); - pFT_EE_UASize = GetProcAddress(hFTD2XXDLL, "FT_EE_UASize"); - pFT_EE_UARead = GetProcAddress(hFTD2XXDLL, "FT_EE_UARead"); - pFT_EE_UAWrite = GetProcAddress(hFTD2XXDLL, "FT_EE_UAWrite"); - pFT_EE_Read = GetProcAddress(hFTD2XXDLL, "FT_EE_Read"); - pFT_EE_Program = GetProcAddress(hFTD2XXDLL, "FT_EE_Program"); - pFT_EEPROM_Read = GetProcAddress(hFTD2XXDLL, "FT_EEPROM_Read"); - pFT_EEPROM_Program = GetProcAddress(hFTD2XXDLL, "FT_EEPROM_Program"); - pFT_VendorCmdGet = GetProcAddress(hFTD2XXDLL, "FT_VendorCmdGet"); - pFT_VendorCmdSet = GetProcAddress(hFTD2XXDLL, "FT_VendorCmdSet"); - pFT_VendorCmdSetX = GetProcAddress(hFTD2XXDLL, "FT_VendorCmdSetX"); - } - - /// - /// Destructor for the FTDI class. - /// - ~FTDI() - { - // FreeLibrary here - we should only do this if we are completely finished - FreeLibrary(hFTD2XXDLL); - hFTD2XXDLL = IntPtr.Zero; - } - - /// - /// Built-in Windows API functions to allow us to dynamically load our own DLL. - /// Will allow us to use old versions of the DLL that do not have all of these functions available. - /// - [DllImport("kernel32.dll")] - private static extern IntPtr LoadLibrary(string dllToLoad); - [DllImport("kernel32.dll")] - private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName); - [DllImport("kernel32.dll")] - private static extern bool FreeLibrary(IntPtr hModule); - - // Definitions for FTD2XX functions - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_CreateDeviceInfoList(ref uint numdevs); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_GetDeviceInfoDetail(uint index, ref uint flags, ref FT_DEVICE chiptype, ref uint id, ref uint locid, byte[] serialnumber, byte[] description, ref IntPtr ftHandle); - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_Open(uint index, ref IntPtr ftHandle); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_OpenEx(string devstring, uint dwFlags, ref IntPtr ftHandle); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_OpenExLoc(uint devloc, uint dwFlags, ref IntPtr ftHandle); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_Close(IntPtr ftHandle); - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_Read(IntPtr ftHandle, byte[] lpBuffer, uint dwBytesToRead, ref uint lpdwBytesReturned); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_Write(IntPtr ftHandle, byte[] lpBuffer, uint dwBytesToWrite, ref uint lpdwBytesWritten); - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_GetQueueStatus(IntPtr ftHandle, ref uint lpdwAmountInRxQueue); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_GetModemStatus(IntPtr ftHandle, ref uint lpdwModemStatus); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_GetStatus(IntPtr ftHandle, ref uint lpdwAmountInRxQueue, ref uint lpdwAmountInTxQueue, ref uint lpdwEventStatus); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_SetBaudRate(IntPtr ftHandle, uint dwBaudRate); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_SetDataCharacteristics(IntPtr ftHandle, byte uWordLength, byte uStopBits, byte uParity); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_SetFlowControl(IntPtr ftHandle, UInt16 usFlowControl, byte uXon, byte uXoff); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_SetDtr(IntPtr ftHandle); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_ClrDtr(IntPtr ftHandle); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_SetRts(IntPtr ftHandle); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_ClrRts(IntPtr ftHandle); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_ResetDevice(IntPtr ftHandle); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_ResetPort(IntPtr ftHandle); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_CyclePort(IntPtr ftHandle); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_Rescan(); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_Reload(UInt16 wVID, UInt16 wPID); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_Purge(IntPtr ftHandle, uint dwMask); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_SetTimeouts(IntPtr ftHandle, uint dwReadTimeout, uint dwWriteTimeout); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_SetBreakOn(IntPtr ftHandle); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_SetBreakOff(IntPtr ftHandle); - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_GetDeviceInfo(IntPtr ftHandle, ref FT_DEVICE pftType, ref uint lpdwID, byte[] pcSerialNumber, byte[] pcDescription, IntPtr pvDummy); - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_SetResetPipeRetryCount(IntPtr ftHandle, uint dwCount); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_StopInTask(IntPtr ftHandle); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_RestartInTask(IntPtr ftHandle); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_GetDriverVersion(IntPtr ftHandle, ref uint lpdwDriverVersion); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_GetLibraryVersion(ref uint lpdwLibraryVersion); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_SetDeadmanTimeout(IntPtr ftHandle, uint dwDeadmanTimeout); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_SetChars(IntPtr ftHandle, byte uEventCh, byte uEventChEn, byte uErrorCh, byte uErrorChEn); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_SetEventNotification(IntPtr ftHandle, uint dwEventMask, SafeHandle hEvent); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_GetComPortNumber(IntPtr ftHandle, ref Int32 dwComPortNumber); - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_SetLatencyTimer(IntPtr ftHandle, byte ucLatency); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_GetLatencyTimer(IntPtr ftHandle, ref byte ucLatency); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_SetBitMode(IntPtr ftHandle, byte ucMask, byte ucMode); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_GetBitMode(IntPtr ftHandle, ref byte ucMode); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_SetUSBParameters(IntPtr ftHandle, uint dwInTransferSize, uint dwOutTransferSize); - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_ReadEE(IntPtr ftHandle, uint dwWordOffset, ref UInt16 lpwValue); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_WriteEE(IntPtr ftHandle, uint dwWordOffset, UInt16 wValue); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_EraseEE(IntPtr ftHandle); - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_EE_UASize(IntPtr ftHandle, ref uint dwSize); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_EE_UARead(IntPtr ftHandle, byte[] pucData, Int32 dwDataLen, ref uint lpdwDataRead); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_EE_UAWrite(IntPtr ftHandle, byte[] pucData, Int32 dwDataLen); - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_EE_Read(IntPtr ftHandle, FT_PROGRAM_DATA pData); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_EE_Program(IntPtr ftHandle, FT_PROGRAM_DATA pData); - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_EEPROM_Read(IntPtr ftHandle, IntPtr eepromData, uint eepromDataSize, byte[] manufacturer, byte[] manufacturerID, byte[] description, byte[] serialnumber); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_EEPROM_Program(IntPtr ftHandle, IntPtr eepromData, uint eepromDataSize, byte[] manufacturer, byte[] manufacturerID, byte[] description, byte[] serialnumber); - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_VendorCmdGet(IntPtr ftHandle, UInt16 request, byte[] buf, UInt16 len); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_VendorCmdSet(IntPtr ftHandle, UInt16 request, byte[] buf, UInt16 len); - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate FT_STATUS tFT_VendorCmdSetX(IntPtr ftHandle, UInt16 request, byte[] buf, UInt16 len); - - // Flags for FT_OpenEx - private const uint FT_OPEN_BY_SERIAL_NUMBER = 0x00000001; - private const uint FT_OPEN_BY_DESCRIPTION = 0x00000002; - private const uint FT_OPEN_BY_LOCATION = 0x00000004; - - private const uint FT_DEFAULT_BAUD_RATE = 9600; - private const uint FT_DEFAULT_DEADMAN_TIMEOUT = 5000; - private const Int32 FT_COM_PORT_NOT_ASSIGNED = -1; - private const uint FT_DEFAULT_IN_TRANSFER_SIZE = 0x1000; - private const uint FT_DEFAULT_OUT_TRANSFER_SIZE = 0x1000; - private const byte FT_DEFAULT_LATENCY = 16; - private const uint FT_DEFAULT_DEVICE_ID = 0x04036001; - - // Create private variables for the device within the class - public IntPtr ftHandle = IntPtr.Zero; - - // Handle to our DLL - used with GetProcAddress to load all of our functions - private IntPtr hFTD2XXDLL = IntPtr.Zero; - - // Declare pointers to each of the functions we are going to use in FT2DXX.DLL - // These are assigned in our constructor and freed in our destructor. - private IntPtr pFT_CreateDeviceInfoList = IntPtr.Zero; - private IntPtr pFT_GetDeviceInfoDetail = IntPtr.Zero; - private IntPtr pFT_Open = IntPtr.Zero; - private IntPtr pFT_OpenEx = IntPtr.Zero; - private IntPtr pFT_Close = IntPtr.Zero; - private IntPtr pFT_Read = IntPtr.Zero; - private IntPtr pFT_Write = IntPtr.Zero; - private IntPtr pFT_GetQueueStatus = IntPtr.Zero; - private IntPtr pFT_GetModemStatus = IntPtr.Zero; - private IntPtr pFT_GetStatus = IntPtr.Zero; - private IntPtr pFT_SetBaudRate = IntPtr.Zero; - private IntPtr pFT_SetDataCharacteristics = IntPtr.Zero; - private IntPtr pFT_SetFlowControl = IntPtr.Zero; - private IntPtr pFT_SetDtr = IntPtr.Zero; - private IntPtr pFT_ClrDtr = IntPtr.Zero; - private IntPtr pFT_SetRts = IntPtr.Zero; - private IntPtr pFT_ClrRts = IntPtr.Zero; - private IntPtr pFT_ResetDevice = IntPtr.Zero; - private IntPtr pFT_ResetPort = IntPtr.Zero; - private IntPtr pFT_CyclePort = IntPtr.Zero; - private IntPtr pFT_Rescan = IntPtr.Zero; - private IntPtr pFT_Reload = IntPtr.Zero; - private IntPtr pFT_Purge = IntPtr.Zero; - private IntPtr pFT_SetTimeouts = IntPtr.Zero; - private IntPtr pFT_SetBreakOn = IntPtr.Zero; - private IntPtr pFT_SetBreakOff = IntPtr.Zero; - private IntPtr pFT_GetDeviceInfo = IntPtr.Zero; - private IntPtr pFT_SetResetPipeRetryCount = IntPtr.Zero; - private IntPtr pFT_StopInTask = IntPtr.Zero; - private IntPtr pFT_RestartInTask = IntPtr.Zero; - private IntPtr pFT_GetDriverVersion = IntPtr.Zero; - private IntPtr pFT_GetLibraryVersion = IntPtr.Zero; - private IntPtr pFT_SetDeadmanTimeout = IntPtr.Zero; - private IntPtr pFT_SetChars = IntPtr.Zero; - private IntPtr pFT_SetEventNotification = IntPtr.Zero; - private IntPtr pFT_GetComPortNumber = IntPtr.Zero; - private IntPtr pFT_SetLatencyTimer = IntPtr.Zero; - private IntPtr pFT_GetLatencyTimer = IntPtr.Zero; - private IntPtr pFT_SetBitMode = IntPtr.Zero; - private IntPtr pFT_GetBitMode = IntPtr.Zero; - private IntPtr pFT_SetUSBParameters = IntPtr.Zero; - private IntPtr pFT_ReadEE = IntPtr.Zero; - private IntPtr pFT_WriteEE = IntPtr.Zero; - private IntPtr pFT_EraseEE = IntPtr.Zero; - private IntPtr pFT_EE_UASize = IntPtr.Zero; - private IntPtr pFT_EE_UARead = IntPtr.Zero; - private IntPtr pFT_EE_UAWrite = IntPtr.Zero; - private IntPtr pFT_EE_Read = IntPtr.Zero; - private IntPtr pFT_EE_Program = IntPtr.Zero; - private IntPtr pFT_EEPROM_Read = IntPtr.Zero; - private IntPtr pFT_EEPROM_Program = IntPtr.Zero; - private IntPtr pFT_VendorCmdGet = IntPtr.Zero; - private IntPtr pFT_VendorCmdSet = IntPtr.Zero; - private IntPtr pFT_VendorCmdSetX = IntPtr.Zero; - - //************************************************************************** - // GetNumberOfDevices - //************************************************************************** - // Intellisense comments - /// - /// Gets the number of FTDI devices available. - /// - /// FT_STATUS value from FT_CreateDeviceInfoList in FTD2XX.DLL - /// The number of FTDI devices available. - public FT_STATUS GetNumberOfDevices(ref uint devcount) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_CreateDeviceInfoList != IntPtr.Zero) - { - tFT_CreateDeviceInfoList FT_CreateDeviceInfoList = (tFT_CreateDeviceInfoList)Marshal.GetDelegateForFunctionPointer(pFT_CreateDeviceInfoList, typeof(tFT_CreateDeviceInfoList)); - - // Call FT_CreateDeviceInfoList - ftStatus = FT_CreateDeviceInfoList(ref devcount); - } - else - { - Console.WriteLine("Failed to load function FT_CreateDeviceInfoList."); - } - return ftStatus; - - } - - - //************************************************************************** - // GetDeviceList - //************************************************************************** - // Intellisense comments - /// - /// Gets information on all of the FTDI devices available. - /// - /// FT_STATUS value from FT_GetDeviceInfoDetail in FTD2XX.DLL - /// An array of type FT_DEVICE_INFO_NODE to contain the device information for all available devices. - /// Thrown when the supplied buffer is not large enough to contain the device info list. - public FT_STATUS GetDeviceList(FT_DEVICE_INFO_NODE[] devicelist) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR; - FT_ERROR ftErrorCondition = FT_ERROR.FT_NO_ERROR; - Int32 nullIndex = 0; - - // 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_CreateDeviceInfoList != IntPtr.Zero) & (pFT_GetDeviceInfoDetail != IntPtr.Zero)) - { - uint devcount = 0; - - tFT_CreateDeviceInfoList FT_CreateDeviceInfoList = (tFT_CreateDeviceInfoList)Marshal.GetDelegateForFunctionPointer(pFT_CreateDeviceInfoList, typeof(tFT_CreateDeviceInfoList)); - tFT_GetDeviceInfoDetail FT_GetDeviceInfoDetail = (tFT_GetDeviceInfoDetail)Marshal.GetDelegateForFunctionPointer(pFT_GetDeviceInfoDetail, typeof(tFT_GetDeviceInfoDetail)); - - // Call FT_CreateDeviceInfoList - ftStatus = FT_CreateDeviceInfoList(ref devcount); - - // Allocate the required storage for our list - - byte[] sernum = new byte[16]; - byte[] desc = new byte[64]; - - if (devcount > 0) - { - // Check the size of the buffer passed in is big enough - if (devicelist.Length < devcount) - { - // Buffer not big enough - ftErrorCondition = FT_ERROR.FT_BUFFER_SIZE; - // Throw exception - ErrorHandler(ftStatus, ftErrorCondition); - } - - // Instantiate the array elements as FT_DEVICE_INFO_NODE - for (uint i = 0; i < devcount; i++) - { - devicelist[i] = new FT_DEVICE_INFO_NODE(); - // Call FT_GetDeviceInfoDetail - ftStatus = FT_GetDeviceInfoDetail(i, ref devicelist[i].Flags, ref devicelist[i].Type, ref devicelist[i].ID, ref devicelist[i].LocId, sernum, desc, ref devicelist[i].ftHandle); - // Convert byte arrays to strings - devicelist[i].SerialNumber = Encoding.ASCII.GetString(sernum); - devicelist[i].Description = Encoding.ASCII.GetString(desc); - // Trim strings to first occurrence of a null terminator character - nullIndex = devicelist[i].SerialNumber.IndexOf('\0'); - if (nullIndex != -1) - devicelist[i].SerialNumber = devicelist[i].SerialNumber.Substring(0, nullIndex); - nullIndex = devicelist[i].Description.IndexOf('\0'); - if (nullIndex != -1) - devicelist[i].Description = devicelist[i].Description.Substring(0, nullIndex); - } - } - } - else - { - if (pFT_CreateDeviceInfoList == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_CreateDeviceInfoList."); - } - if (pFT_GetDeviceInfoDetail == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_GetDeviceInfoListDetail."); - } - } - return ftStatus; - } - - - //************************************************************************** - // OpenByIndex - //************************************************************************** - // Intellisense comments - /// - /// Opens the FTDI device with the specified index. - /// - /// FT_STATUS value from FT_Open in FTD2XX.DLL - /// Index of the device to open. - /// Note that this cannot be guaranteed to open a specific device. - /// Initialises the device to 8 data bits, 1 stop bit, no parity, no flow control and 9600 Baud. - public FT_STATUS OpenByIndex(uint index) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_Open != IntPtr.Zero) & (pFT_SetDataCharacteristics != IntPtr.Zero) & (pFT_SetFlowControl != IntPtr.Zero) & (pFT_SetBaudRate != IntPtr.Zero)) - { - tFT_Open FT_Open = (tFT_Open)Marshal.GetDelegateForFunctionPointer(pFT_Open, typeof(tFT_Open)); - tFT_SetDataCharacteristics FT_SetDataCharacteristics = (tFT_SetDataCharacteristics)Marshal.GetDelegateForFunctionPointer(pFT_SetDataCharacteristics, typeof(tFT_SetDataCharacteristics)); - tFT_SetFlowControl FT_SetFlowControl = (tFT_SetFlowControl)Marshal.GetDelegateForFunctionPointer(pFT_SetFlowControl, typeof(tFT_SetFlowControl)); - tFT_SetBaudRate FT_SetBaudRate = (tFT_SetBaudRate)Marshal.GetDelegateForFunctionPointer(pFT_SetBaudRate, typeof(tFT_SetBaudRate)); - - // Call FT_Open - ftStatus = FT_Open(index, ref ftHandle); - - // Appears that the handle value can be non-NULL on a fail, so set it explicitly - if (ftStatus != FT_STATUS.FT_OK) - ftHandle = IntPtr.Zero; - - if (ftHandle != IntPtr.Zero) - { - // Initialise port data characteristics - byte WordLength = FT_DATA_BITS.FT_BITS_8; - byte StopBits = FT_STOP_BITS.FT_STOP_BITS_1; - byte Parity = FT_PARITY.FT_PARITY_NONE; - ftStatus = FT_SetDataCharacteristics(ftHandle, WordLength, StopBits, Parity); - // Initialise to no flow control - UInt16 FlowControl = FT_FLOW_CONTROL.FT_FLOW_NONE; - byte Xon = 0x11; - byte Xoff = 0x13; - ftStatus = FT_SetFlowControl(ftHandle, FlowControl, Xon, Xoff); - // Initialise Baud rate - uint BaudRate = 9600; - ftStatus = FT_SetBaudRate(ftHandle, BaudRate); - } - } - else - { - if (pFT_Open == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_Open."); - } - if (pFT_SetDataCharacteristics == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetDataCharacteristics."); - } - if (pFT_SetFlowControl == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetFlowControl."); - } - if (pFT_SetBaudRate == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetBaudRate."); - } - } - return ftStatus; - } - - - //************************************************************************** - // OpenBySerialNumber - //************************************************************************** - // Intellisense comments - /// - /// Opens the FTDI device with the specified serial number. - /// - /// FT_STATUS value from FT_OpenEx in FTD2XX.DLL - /// Serial number of the device to open. - /// Initialises the device to 8 data bits, 1 stop bit, no parity, no flow control and 9600 Baud. - public FT_STATUS OpenBySerialNumber(string serialnumber) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_OpenEx != IntPtr.Zero) & (pFT_SetDataCharacteristics != IntPtr.Zero) & (pFT_SetFlowControl != IntPtr.Zero) & (pFT_SetBaudRate != IntPtr.Zero)) - { - tFT_OpenEx FT_OpenEx = (tFT_OpenEx)Marshal.GetDelegateForFunctionPointer(pFT_OpenEx, typeof(tFT_OpenEx)); - tFT_SetDataCharacteristics FT_SetDataCharacteristics = (tFT_SetDataCharacteristics)Marshal.GetDelegateForFunctionPointer(pFT_SetDataCharacteristics, typeof(tFT_SetDataCharacteristics)); - tFT_SetFlowControl FT_SetFlowControl = (tFT_SetFlowControl)Marshal.GetDelegateForFunctionPointer(pFT_SetFlowControl, typeof(tFT_SetFlowControl)); - tFT_SetBaudRate FT_SetBaudRate = (tFT_SetBaudRate)Marshal.GetDelegateForFunctionPointer(pFT_SetBaudRate, typeof(tFT_SetBaudRate)); - - // Call FT_OpenEx - ftStatus = FT_OpenEx(serialnumber, FT_OPEN_BY_SERIAL_NUMBER, ref ftHandle); - - // Appears that the handle value can be non-NULL on a fail, so set it explicitly - if (ftStatus != FT_STATUS.FT_OK) - ftHandle = IntPtr.Zero; - - if (ftHandle != IntPtr.Zero) - { - // Initialise port data characteristics - byte WordLength = FT_DATA_BITS.FT_BITS_8; - byte StopBits = FT_STOP_BITS.FT_STOP_BITS_1; - byte Parity = FT_PARITY.FT_PARITY_NONE; - ftStatus = FT_SetDataCharacteristics(ftHandle, WordLength, StopBits, Parity); - // Initialise to no flow control - UInt16 FlowControl = FT_FLOW_CONTROL.FT_FLOW_NONE; - byte Xon = 0x11; - byte Xoff = 0x13; - ftStatus = FT_SetFlowControl(ftHandle, FlowControl, Xon, Xoff); - // Initialise Baud rate - uint BaudRate = 9600; - ftStatus = FT_SetBaudRate(ftHandle, BaudRate); - } - } - else - { - if (pFT_OpenEx == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_OpenEx."); - } - if (pFT_SetDataCharacteristics == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetDataCharacteristics."); - } - if (pFT_SetFlowControl == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetFlowControl."); - } - if (pFT_SetBaudRate == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetBaudRate."); - } - } - return ftStatus; - } - - - //************************************************************************** - // OpenByDescription - //************************************************************************** - // Intellisense comments - /// - /// Opens the FTDI device with the specified description. - /// - /// FT_STATUS value from FT_OpenEx in FTD2XX.DLL - /// Description of the device to open. - /// Initialises the device to 8 data bits, 1 stop bit, no parity, no flow control and 9600 Baud. - public FT_STATUS OpenByDescription(string description) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_OpenEx != IntPtr.Zero) & (pFT_SetDataCharacteristics != IntPtr.Zero) & (pFT_SetFlowControl != IntPtr.Zero) & (pFT_SetBaudRate != IntPtr.Zero)) - { - tFT_OpenEx FT_OpenEx = (tFT_OpenEx)Marshal.GetDelegateForFunctionPointer(pFT_OpenEx, typeof(tFT_OpenEx)); - tFT_SetDataCharacteristics FT_SetDataCharacteristics = (tFT_SetDataCharacteristics)Marshal.GetDelegateForFunctionPointer(pFT_SetDataCharacteristics, typeof(tFT_SetDataCharacteristics)); - tFT_SetFlowControl FT_SetFlowControl = (tFT_SetFlowControl)Marshal.GetDelegateForFunctionPointer(pFT_SetFlowControl, typeof(tFT_SetFlowControl)); - tFT_SetBaudRate FT_SetBaudRate = (tFT_SetBaudRate)Marshal.GetDelegateForFunctionPointer(pFT_SetBaudRate, typeof(tFT_SetBaudRate)); - - // Call FT_OpenEx - ftStatus = FT_OpenEx(description, FT_OPEN_BY_DESCRIPTION, ref ftHandle); - - // Appears that the handle value can be non-NULL on a fail, so set it explicitly - if (ftStatus != FT_STATUS.FT_OK) - ftHandle = IntPtr.Zero; - - if (ftHandle != IntPtr.Zero) - { - // Initialise port data characteristics - byte WordLength = FT_DATA_BITS.FT_BITS_8; - byte StopBits = FT_STOP_BITS.FT_STOP_BITS_1; - byte Parity = FT_PARITY.FT_PARITY_NONE; - ftStatus = FT_SetDataCharacteristics(ftHandle, WordLength, StopBits, Parity); - // Initialise to no flow control - UInt16 FlowControl = FT_FLOW_CONTROL.FT_FLOW_NONE; - byte Xon = 0x11; - byte Xoff = 0x13; - ftStatus = FT_SetFlowControl(ftHandle, FlowControl, Xon, Xoff); - // Initialise Baud rate - uint BaudRate = 9600; - ftStatus = FT_SetBaudRate(ftHandle, BaudRate); - } - } - else - { - if (pFT_OpenEx == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_OpenEx."); - } - if (pFT_SetDataCharacteristics == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetDataCharacteristics."); - } - if (pFT_SetFlowControl == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetFlowControl."); - } - if (pFT_SetBaudRate == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetBaudRate."); - } - } - return ftStatus; - } - - - //************************************************************************** - // OpenByLocation - //************************************************************************** - // Intellisense comments - /// - /// Opens the FTDI device at the specified physical location. - /// - /// FT_STATUS value from FT_OpenEx in FTD2XX.DLL - /// Location of the device to open. - /// Initialises the device to 8 data bits, 1 stop bit, no parity, no flow control and 9600 Baud. - public FT_STATUS OpenByLocation(uint location) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_OpenEx != IntPtr.Zero) & (pFT_SetDataCharacteristics != IntPtr.Zero) & (pFT_SetFlowControl != IntPtr.Zero) & (pFT_SetBaudRate != IntPtr.Zero)) - { - tFT_OpenExLoc FT_OpenEx = (tFT_OpenExLoc)Marshal.GetDelegateForFunctionPointer(pFT_OpenEx, typeof(tFT_OpenExLoc)); - tFT_SetDataCharacteristics FT_SetDataCharacteristics = (tFT_SetDataCharacteristics)Marshal.GetDelegateForFunctionPointer(pFT_SetDataCharacteristics, typeof(tFT_SetDataCharacteristics)); - tFT_SetFlowControl FT_SetFlowControl = (tFT_SetFlowControl)Marshal.GetDelegateForFunctionPointer(pFT_SetFlowControl, typeof(tFT_SetFlowControl)); - tFT_SetBaudRate FT_SetBaudRate = (tFT_SetBaudRate)Marshal.GetDelegateForFunctionPointer(pFT_SetBaudRate, typeof(tFT_SetBaudRate)); - - // Call FT_OpenEx - ftStatus = FT_OpenEx(location, FT_OPEN_BY_LOCATION, ref ftHandle); - - // Appears that the handle value can be non-NULL on a fail, so set it explicitly - if (ftStatus != FT_STATUS.FT_OK) - ftHandle = IntPtr.Zero; - - if (ftHandle != IntPtr.Zero) - { - // Initialise port data characteristics - byte WordLength = FT_DATA_BITS.FT_BITS_8; - byte StopBits = FT_STOP_BITS.FT_STOP_BITS_1; - byte Parity = FT_PARITY.FT_PARITY_NONE; - FT_SetDataCharacteristics(ftHandle, WordLength, StopBits, Parity); - // Initialise to no flow control - UInt16 FlowControl = FT_FLOW_CONTROL.FT_FLOW_NONE; - byte Xon = 0x11; - byte Xoff = 0x13; - FT_SetFlowControl(ftHandle, FlowControl, Xon, Xoff); - // Initialise Baud rate - uint BaudRate = 9600; - FT_SetBaudRate(ftHandle, BaudRate); - } - } - else - { - if (pFT_OpenEx == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_OpenEx."); - } - if (pFT_SetDataCharacteristics == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetDataCharacteristics."); - } - if (pFT_SetFlowControl == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetFlowControl."); - } - if (pFT_SetBaudRate == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetBaudRate."); - } - } - return ftStatus; - } - - - //************************************************************************** - // Close - //************************************************************************** - // Intellisense comments - /// - /// Closes the handle to an open FTDI device. - /// - /// FT_STATUS value from FT_Close in FTD2XX.DLL - public FT_STATUS Close() - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_Close != IntPtr.Zero) - { - tFT_Close FT_Close = (tFT_Close)Marshal.GetDelegateForFunctionPointer(pFT_Close, typeof(tFT_Close)); - - // Call FT_Close - ftStatus = FT_Close(ftHandle); - - if (ftStatus == FT_STATUS.FT_OK) - { - ftHandle = IntPtr.Zero; - } - } - else - { - if (pFT_Close == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_Close."); - } - } - return ftStatus; - } - - - //************************************************************************** - // Read - //************************************************************************** - // Intellisense comments - /// - /// Read data from an open FTDI device. - /// - /// FT_STATUS value from FT_Read in FTD2XX.DLL - /// An array of bytes which will be populated with the data read from the device. - /// The number of bytes requested from the device. - /// The number of bytes actually read. - public FT_STATUS Read(byte[] dataBuffer, uint numBytesToRead, ref uint numBytesRead) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_Read != IntPtr.Zero) - { - - tFT_Read FT_Read = (tFT_Read)Marshal.GetDelegateForFunctionPointer(pFT_Read, typeof(tFT_Read)); - - // If the buffer is not big enough to receive the amount of data requested, adjust the number of bytes to read - if (dataBuffer.Length < numBytesToRead) - { - numBytesToRead = (uint)dataBuffer.Length; - } - - if (ftHandle != IntPtr.Zero) - { - // Call FT_Read - ftStatus = FT_Read(ftHandle, dataBuffer, numBytesToRead, ref numBytesRead); - } - } - else - { - if (pFT_Read == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_Read."); - } - } - return ftStatus; - } - - // Intellisense comments - /// - /// Read data from an open FTDI device. - /// - /// FT_STATUS value from FT_Read in FTD2XX.DLL - /// A string containing the data read - /// The number of bytes requested from the device. - /// The number of bytes actually read. - public FT_STATUS Read(out string dataBuffer, uint numBytesToRead, ref uint numBytesRead) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR; - - // As dataBuffer is an OUT parameter, needs to be assigned before returning - dataBuffer = string.Empty; - - // 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_Read != IntPtr.Zero) - { - tFT_Read FT_Read = (tFT_Read)Marshal.GetDelegateForFunctionPointer(pFT_Read, typeof(tFT_Read)); - - byte[] byteDataBuffer = new byte[numBytesToRead]; - - if (ftHandle != IntPtr.Zero) - { - // Call FT_Read - ftStatus = FT_Read(ftHandle, byteDataBuffer, numBytesToRead, ref numBytesRead); - - // Convert ASCII byte array back to Unicode string for passing back - dataBuffer = Encoding.ASCII.GetString(byteDataBuffer); - // Trim buffer to actual bytes read - dataBuffer = dataBuffer.Substring(0, (int)numBytesRead); - } - } - else - { - if (pFT_Read == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_Read."); - } - } - return ftStatus; - } - - //************************************************************************** - // Write - //************************************************************************** - // Intellisense comments - /// - /// Write data to an open FTDI device. - /// - /// FT_STATUS value from FT_Write in FTD2XX.DLL - /// An array of bytes which contains the data to be written to the device. - /// The number of bytes to be written to the device. - /// The number of bytes actually written to the device. - public FT_STATUS Write(byte[] dataBuffer, Int32 numBytesToWrite, ref uint numBytesWritten) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_Write != IntPtr.Zero) - { - tFT_Write FT_Write = (tFT_Write)Marshal.GetDelegateForFunctionPointer(pFT_Write, typeof(tFT_Write)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_Write - ftStatus = FT_Write(ftHandle, dataBuffer, (uint)numBytesToWrite, ref numBytesWritten); - } - } - else - { - if (pFT_Write == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_Write."); - } - } - return ftStatus; - } - - // Intellisense comments - /// - /// Write data to an open FTDI device. - /// - /// FT_STATUS value from FT_Write in FTD2XX.DLL - /// An array of bytes which contains the data to be written to the device. - /// The number of bytes to be written to the device. - /// The number of bytes actually written to the device. - public FT_STATUS Write(byte[] dataBuffer, uint numBytesToWrite, ref uint numBytesWritten) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_Write != IntPtr.Zero) - { - tFT_Write FT_Write = (tFT_Write)Marshal.GetDelegateForFunctionPointer(pFT_Write, typeof(tFT_Write)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_Write - ftStatus = FT_Write(ftHandle, dataBuffer, numBytesToWrite, ref numBytesWritten); - } - } - else - { - if (pFT_Write == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_Write."); - } - } - return ftStatus; - } - - // Intellisense comments - /// - /// Write data to an open FTDI device. - /// - /// FT_STATUS value from FT_Write in FTD2XX.DLL - /// A string which contains the data to be written to the device. - /// The number of bytes to be written to the device. - /// The number of bytes actually written to the device. - public FT_STATUS Write(string dataBuffer, Int32 numBytesToWrite, ref uint numBytesWritten) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_Write != IntPtr.Zero) - { - tFT_Write FT_Write = (tFT_Write)Marshal.GetDelegateForFunctionPointer(pFT_Write, typeof(tFT_Write)); - - // Convert Unicode string to ASCII byte array - byte[] byteDataBuffer = Encoding.ASCII.GetBytes(dataBuffer); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_Write - ftStatus = FT_Write(ftHandle, byteDataBuffer, (uint)numBytesToWrite, ref numBytesWritten); - } - } - else - { - if (pFT_Write == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_Write."); - } - } - return ftStatus; - } - - // Intellisense comments - /// - /// Write data to an open FTDI device. - /// - /// FT_STATUS value from FT_Write in FTD2XX.DLL - /// A string which contains the data to be written to the device. - /// The number of bytes to be written to the device. - /// The number of bytes actually written to the device. - public FT_STATUS Write(string dataBuffer, uint numBytesToWrite, ref uint numBytesWritten) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_Write != IntPtr.Zero) - { - tFT_Write FT_Write = (tFT_Write)Marshal.GetDelegateForFunctionPointer(pFT_Write, typeof(tFT_Write)); - - // Convert Unicode string to ASCII byte array - byte[] byteDataBuffer = Encoding.ASCII.GetBytes(dataBuffer); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_Write - ftStatus = FT_Write(ftHandle, byteDataBuffer, numBytesToWrite, ref numBytesWritten); - } - } - else - { - if (pFT_Write == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_Write."); - } - } - return ftStatus; - } - - //************************************************************************** - // ResetDevice - //************************************************************************** - // Intellisense comments - /// - /// Reset an open FTDI device. - /// - /// FT_STATUS value from FT_ResetDevice in FTD2XX.DLL - public FT_STATUS ResetDevice() - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_ResetDevice != IntPtr.Zero) - { - tFT_ResetDevice FT_ResetDevice = (tFT_ResetDevice)Marshal.GetDelegateForFunctionPointer(pFT_ResetDevice, typeof(tFT_ResetDevice)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_ResetDevice - ftStatus = FT_ResetDevice(ftHandle); - } - } - else - { - if (pFT_ResetDevice == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_ResetDevice."); - } - } - return ftStatus; - } - - //************************************************************************** - // Purge - //************************************************************************** - // Intellisense comments - /// - /// Purge data from the devices transmit and/or receive buffers. - /// - /// FT_STATUS value from FT_Purge in FTD2XX.DLL - /// Specifies which buffer(s) to be purged. Valid values are any combination of the following flags: FT_PURGE_RX, FT_PURGE_TX - public FT_STATUS Purge(uint purgemask) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_Purge != IntPtr.Zero) - { - tFT_Purge FT_Purge = (tFT_Purge)Marshal.GetDelegateForFunctionPointer(pFT_Purge, typeof(tFT_Purge)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_Purge - ftStatus = FT_Purge(ftHandle, purgemask); - } - } - else - { - if (pFT_Purge == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_Purge."); - } - } - return ftStatus; - } - - //************************************************************************** - // SetEventNotification - //************************************************************************** - // Intellisense comments - /// - /// Register for event notification. - /// - /// FT_STATUS value from FT_SetEventNotification in FTD2XX.DLL - /// After setting event notification, the event can be caught by executing the WaitOne() method of the EventWaitHandle. If multiple event types are being monitored, the event that fired can be determined from the GetEventType method. - /// The type of events to signal. Can be any combination of the following: FT_EVENT_RXCHAR, FT_EVENT_MODEM_STATUS, FT_EVENT_LINE_STATUS - /// Handle to the event that will receive the notification - public FT_STATUS SetEventNotification(uint eventmask, EventWaitHandle eventhandle) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetEventNotification != IntPtr.Zero) - { - tFT_SetEventNotification FT_SetEventNotification = (tFT_SetEventNotification)Marshal.GetDelegateForFunctionPointer(pFT_SetEventNotification, typeof(tFT_SetEventNotification)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_SetSetEventNotification - ftStatus = FT_SetEventNotification(ftHandle, eventmask, eventhandle.SafeWaitHandle); - } - } - else - { - if (pFT_SetEventNotification == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetEventNotification."); - } - } - return ftStatus; - } - - //************************************************************************** - // StopInTask - //************************************************************************** - // Intellisense comments - /// - /// Stops the driver issuing USB in requests. - /// - /// FT_STATUS value from FT_StopInTask in FTD2XX.DLL - public FT_STATUS StopInTask() - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_StopInTask != IntPtr.Zero) - { - tFT_StopInTask FT_StopInTask = (tFT_StopInTask)Marshal.GetDelegateForFunctionPointer(pFT_StopInTask, typeof(tFT_StopInTask)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_StopInTask - ftStatus = FT_StopInTask(ftHandle); - } - } - else - { - if (pFT_StopInTask == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_StopInTask."); - } - } - return ftStatus; - } - - //************************************************************************** - // RestartInTask - //************************************************************************** - // Intellisense comments - /// - /// Resumes the driver issuing USB in requests. - /// - /// FT_STATUS value from FT_RestartInTask in FTD2XX.DLL - public FT_STATUS RestartInTask() - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_RestartInTask != IntPtr.Zero) - { - tFT_RestartInTask FT_RestartInTask = (tFT_RestartInTask)Marshal.GetDelegateForFunctionPointer(pFT_RestartInTask, typeof(tFT_RestartInTask)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_RestartInTask - ftStatus = FT_RestartInTask(ftHandle); - } - } - else - { - if (pFT_RestartInTask == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_RestartInTask."); - } - } - return ftStatus; - } - - //************************************************************************** - // ResetPort - //************************************************************************** - // Intellisense comments - /// - /// Resets the device port. - /// - /// FT_STATUS value from FT_ResetPort in FTD2XX.DLL - public FT_STATUS ResetPort() - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_ResetPort != IntPtr.Zero) - { - tFT_ResetPort FT_ResetPort = (tFT_ResetPort)Marshal.GetDelegateForFunctionPointer(pFT_ResetPort, typeof(tFT_ResetPort)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_ResetPort - ftStatus = FT_ResetPort(ftHandle); - } - } - else - { - if (pFT_ResetPort == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_ResetPort."); - } - } - return ftStatus; - } - - //************************************************************************** - // CyclePort - //************************************************************************** - // Intellisense comments - /// - /// Causes the device to be re-enumerated on the USB bus. This is equivalent to unplugging and replugging the device. - /// Also calls FT_Close if FT_CyclePort is successful, so no need to call this separately in the application. - /// - /// FT_STATUS value from FT_CyclePort in FTD2XX.DLL - public FT_STATUS CyclePort() - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_CyclePort != IntPtr.Zero) & (pFT_Close != IntPtr.Zero)) - { - tFT_CyclePort FT_CyclePort = (tFT_CyclePort)Marshal.GetDelegateForFunctionPointer(pFT_CyclePort, typeof(tFT_CyclePort)); - tFT_Close FT_Close = (tFT_Close)Marshal.GetDelegateForFunctionPointer(pFT_Close, typeof(tFT_Close)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_CyclePort - ftStatus = FT_CyclePort(ftHandle); - if (ftStatus == FT_STATUS.FT_OK) - { - // If successful, call FT_Close - ftStatus = FT_Close(ftHandle); - if (ftStatus == FT_STATUS.FT_OK) - { - ftHandle = IntPtr.Zero; - } - } - } - } - else - { - if (pFT_CyclePort == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_CyclePort."); - } - if (pFT_Close == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_Close."); - } - } - return ftStatus; - } - - //************************************************************************** - // Rescan - //************************************************************************** - // Intellisense comments - /// - /// Causes the system to check for USB hardware changes. This is equivalent to clicking on the "Scan for hardware changes" button in the Device Manager. - /// - /// FT_STATUS value from FT_Rescan in FTD2XX.DLL - public FT_STATUS Rescan() - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_Rescan != IntPtr.Zero) - { - tFT_Rescan FT_Rescan = (tFT_Rescan)Marshal.GetDelegateForFunctionPointer(pFT_Rescan, typeof(tFT_Rescan)); - - // Call FT_Rescan - ftStatus = FT_Rescan(); - } - else - { - if (pFT_Rescan == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_Rescan."); - } - } - return ftStatus; - } - - //************************************************************************** - // Reload - //************************************************************************** - // Intellisense comments - /// - /// Forces a reload of the driver for devices with a specific VID and PID combination. - /// - /// FT_STATUS value from FT_Reload in FTD2XX.DLL - /// If the VID and PID parameters are 0, the drivers for USB root hubs will be reloaded, causing all USB devices connected to reload their drivers - /// Vendor ID of the devices to have the driver reloaded - /// Product ID of the devices to have the driver reloaded - public FT_STATUS Reload(UInt16 VendorID, UInt16 ProductID) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_Reload != IntPtr.Zero) - { - tFT_Reload FT_Reload = (tFT_Reload)Marshal.GetDelegateForFunctionPointer(pFT_Reload, typeof(tFT_Reload)); - - // Call FT_Reload - ftStatus = FT_Reload(VendorID, ProductID); - } - else - { - if (pFT_Reload == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_Reload."); - } - } - return ftStatus; - } - - //************************************************************************** - // SetBitMode - //************************************************************************** - // Intellisense comments - /// - /// Puts the device in a mode other than the default UART or FIFO mode. - /// - /// FT_STATUS value from FT_SetBitMode in FTD2XX.DLL - /// Sets up which bits are inputs and which are outputs. A bit value of 0 sets the corresponding pin to an input, a bit value of 1 sets the corresponding pin to an output. - /// In the case of CBUS Bit Bang, the upper nibble of this value controls which pins are inputs and outputs, while the lower nibble controls which of the outputs are high and low. - /// For FT232H devices, valid values are FT_BIT_MODE_RESET, FT_BIT_MODE_ASYNC_BITBANG, FT_BIT_MODE_MPSSE, FT_BIT_MODE_SYNC_BITBANG, FT_BIT_MODE_CBUS_BITBANG, FT_BIT_MODE_MCU_HOST, FT_BIT_MODE_FAST_SERIAL, FT_BIT_MODE_SYNC_FIFO. - /// For FT2232H devices, valid values are FT_BIT_MODE_RESET, FT_BIT_MODE_ASYNC_BITBANG, FT_BIT_MODE_MPSSE, FT_BIT_MODE_SYNC_BITBANG, FT_BIT_MODE_MCU_HOST, FT_BIT_MODE_FAST_SERIAL, FT_BIT_MODE_SYNC_FIFO. - /// For FT4232H devices, valid values are FT_BIT_MODE_RESET, FT_BIT_MODE_ASYNC_BITBANG, FT_BIT_MODE_MPSSE, FT_BIT_MODE_SYNC_BITBANG. - /// For FT232R devices, valid values are FT_BIT_MODE_RESET, FT_BIT_MODE_ASYNC_BITBANG, FT_BIT_MODE_SYNC_BITBANG, FT_BIT_MODE_CBUS_BITBANG. - /// For FT245R devices, valid values are FT_BIT_MODE_RESET, FT_BIT_MODE_ASYNC_BITBANG, FT_BIT_MODE_SYNC_BITBANG. - /// For FT2232 devices, valid values are FT_BIT_MODE_RESET, FT_BIT_MODE_ASYNC_BITBANG, FT_BIT_MODE_MPSSE, FT_BIT_MODE_SYNC_BITBANG, FT_BIT_MODE_MCU_HOST, FT_BIT_MODE_FAST_SERIAL. - /// For FT232B and FT245B devices, valid values are FT_BIT_MODE_RESET, FT_BIT_MODE_ASYNC_BITBANG. - /// Thrown when the current device does not support the requested bit mode. - public FT_STATUS SetBitMode(byte Mask, byte BitMode) - { - // 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_SetBitMode != IntPtr.Zero) - { - tFT_SetBitMode FT_SetBitMode = (tFT_SetBitMode)Marshal.GetDelegateForFunctionPointer(pFT_SetBitMode, typeof(tFT_SetBitMode)); - - if (ftHandle != IntPtr.Zero) - { - - FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN; - // Set Bit Mode does not apply to FT8U232AM, FT8U245AM or FT8U100AX devices - GetDeviceType(ref DeviceType); - if (DeviceType == FT_DEVICE.FT_DEVICE_AM) - { - // Throw an exception - ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE; - ErrorHandler(ftStatus, ftErrorCondition); - } - else if (DeviceType == FT_DEVICE.FT_DEVICE_100AX) - { - // Throw an exception - ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE; - ErrorHandler(ftStatus, ftErrorCondition); - } - else if ((DeviceType == FT_DEVICE.FT_DEVICE_BM) && (BitMode != FT_BIT_MODES.FT_BIT_MODE_RESET)) - { - if ((BitMode & (FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG)) == 0) - { - // Throw an exception - ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE; - ErrorHandler(ftStatus, ftErrorCondition); - } - } - else if ((DeviceType == FT_DEVICE.FT_DEVICE_2232) && (BitMode != FT_BIT_MODES.FT_BIT_MODE_RESET)) - { - if ((BitMode & (FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG | FT_BIT_MODES.FT_BIT_MODE_MPSSE | FT_BIT_MODES.FT_BIT_MODE_SYNC_BITBANG | FT_BIT_MODES.FT_BIT_MODE_MCU_HOST | FT_BIT_MODES.FT_BIT_MODE_FAST_SERIAL)) == 0) - { - // Throw an exception - ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE; - ErrorHandler(ftStatus, ftErrorCondition); - } - if ((BitMode == FT_BIT_MODES.FT_BIT_MODE_MPSSE) & (InterfaceIdentifier != "A")) - { - // MPSSE mode is only available on channel A - // Throw an exception - ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE; - ErrorHandler(ftStatus, ftErrorCondition); - } - } - else if ((DeviceType == FT_DEVICE.FT_DEVICE_232R) && (BitMode != FT_BIT_MODES.FT_BIT_MODE_RESET)) - { - if ((BitMode & (FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG | FT_BIT_MODES.FT_BIT_MODE_SYNC_BITBANG | FT_BIT_MODES.FT_BIT_MODE_CBUS_BITBANG)) == 0) - { - // Throw an exception - ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE; - ErrorHandler(ftStatus, ftErrorCondition); - } - } - else if (((DeviceType == FT_DEVICE.FT_DEVICE_2232H) - || (DeviceType == FT_DEVICE.FT_DEVICE_2232HP) - || (DeviceType == FT_DEVICE.FT_DEVICE_2233HP) - || (DeviceType == FT_DEVICE.FT_DEVICE_2232HA)) - && (BitMode != FT_BIT_MODES.FT_BIT_MODE_RESET)) - { - if ((BitMode & (FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG | FT_BIT_MODES.FT_BIT_MODE_MPSSE | FT_BIT_MODES.FT_BIT_MODE_SYNC_BITBANG | FT_BIT_MODES.FT_BIT_MODE_MCU_HOST | FT_BIT_MODES.FT_BIT_MODE_FAST_SERIAL | FT_BIT_MODES.FT_BIT_MODE_SYNC_FIFO)) == 0) - { - // Throw an exception - ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE; - ErrorHandler(ftStatus, ftErrorCondition); - } - if (((BitMode == FT_BIT_MODES.FT_BIT_MODE_MCU_HOST) | (BitMode == FT_BIT_MODES.FT_BIT_MODE_SYNC_FIFO)) & (InterfaceIdentifier != "A")) - { - // MCU Host Emulation and Single channel synchronous 245 FIFO mode is only available on channel A - // Throw an exception - ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE; - ErrorHandler(ftStatus, ftErrorCondition); - } - } - else if (((DeviceType == FT_DEVICE.FT_DEVICE_4232H) - || (DeviceType == FT_DEVICE.FT_DEVICE_4232HP) - || (DeviceType == FT_DEVICE.FT_DEVICE_4233HP) - || (DeviceType == FT_DEVICE.FT_DEVICE_4232HA)) - && (BitMode != FT_BIT_MODES.FT_BIT_MODE_RESET)) - { - if ((BitMode & (FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG | FT_BIT_MODES.FT_BIT_MODE_MPSSE | FT_BIT_MODES.FT_BIT_MODE_SYNC_BITBANG)) == 0) - { - // Throw an exception - ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE; - ErrorHandler(ftStatus, ftErrorCondition); - } - if ((BitMode == FT_BIT_MODES.FT_BIT_MODE_MPSSE) & ((InterfaceIdentifier != "A") & (InterfaceIdentifier != "B"))) - { - // MPSSE mode is only available on channel A and B - // Throw an exception - ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE; - ErrorHandler(ftStatus, ftErrorCondition); - } - } - else if (((DeviceType == FT_DEVICE.FT_DEVICE_232H) - || (DeviceType == FT_DEVICE.FT_DEVICE_232HP) - || (DeviceType == FT_DEVICE.FT_DEVICE_233HP)) - && (BitMode != FT_BIT_MODES.FT_BIT_MODE_RESET)) - { - // FT232H supports all current bit modes! - if (BitMode > FT_BIT_MODES.FT_BIT_MODE_SYNC_FIFO) - { - // Throw an exception - ftErrorCondition = FT_ERROR.FT_INVALID_BITMODE; - ErrorHandler(ftStatus, ftErrorCondition); - } - } - - // Requested bit mode is supported - // Note FT_BIT_MODES.FT_BIT_MODE_RESET falls through to here - no bits set so cannot check for AND - // Call FT_SetBitMode - ftStatus = FT_SetBitMode(ftHandle, Mask, BitMode); - } - } - else - { - if (pFT_SetBitMode == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetBitMode."); - } - } - return ftStatus; - } - - //************************************************************************** - // GetPinStates - //************************************************************************** - // Intellisense comments - /// - /// Gets the instantaneous state of the device IO pins. - /// - /// FT_STATUS value from FT_GetBitMode in FTD2XX.DLL - /// A bitmap value containing the instantaneous state of the device IO pins - public FT_STATUS GetPinStates(ref byte BitMode) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_GetBitMode != IntPtr.Zero) - { - tFT_GetBitMode FT_GetBitMode = (tFT_GetBitMode)Marshal.GetDelegateForFunctionPointer(pFT_GetBitMode, typeof(tFT_GetBitMode)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_GetBitMode - ftStatus = FT_GetBitMode(ftHandle, ref BitMode); - } - } - else - { - if (pFT_GetBitMode == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_GetBitMode."); - } - } - return ftStatus; - } - - //************************************************************************** - // ReadEEPROMLocation - //************************************************************************** - // Intellisense comments - /// - /// Reads an individual word value from a specified location in the device's EEPROM. - /// - /// FT_STATUS value from FT_ReadEE in FTD2XX.DLL - /// The EEPROM location to read data from - /// The WORD value read from the EEPROM location specified in the Address paramter - public FT_STATUS ReadEEPROMLocation(uint Address, ref UInt16 EEValue) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_ReadEE != IntPtr.Zero) - { - tFT_ReadEE FT_ReadEE = (tFT_ReadEE)Marshal.GetDelegateForFunctionPointer(pFT_ReadEE, typeof(tFT_ReadEE)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_ReadEE - ftStatus = FT_ReadEE(ftHandle, Address, ref EEValue); - } - } - else - { - if (pFT_ReadEE == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_ReadEE."); - } - } - return ftStatus; - } - - //************************************************************************** - // WriteEEPROMLocation - //************************************************************************** - // Intellisense comments - /// - /// Writes an individual word value to a specified location in the device's EEPROM. - /// - /// FT_STATUS value from FT_WriteEE in FTD2XX.DLL - /// The EEPROM location to read data from - /// The WORD value to write to the EEPROM location specified by the Address parameter - public FT_STATUS WriteEEPROMLocation(uint Address, UInt16 EEValue) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_WriteEE != IntPtr.Zero) - { - tFT_WriteEE FT_WriteEE = (tFT_WriteEE)Marshal.GetDelegateForFunctionPointer(pFT_WriteEE, typeof(tFT_WriteEE)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_WriteEE - ftStatus = FT_WriteEE(ftHandle, Address, EEValue); - } - } - else - { - if (pFT_WriteEE == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_WriteEE."); - } - } - return ftStatus; - } - - //************************************************************************** - // EraseEEPROM - //************************************************************************** - // Intellisense comments - /// - /// Erases the device EEPROM. - /// - /// FT_STATUS value from FT_EraseEE in FTD2XX.DLL - /// Thrown when attempting to erase the EEPROM of a device with an internal EEPROM such as an FT232R or FT245R. - public FT_STATUS EraseEEPROM() - { - // 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_EraseEE != IntPtr.Zero) - { - tFT_EraseEE FT_EraseEE = (tFT_EraseEE)Marshal.GetDelegateForFunctionPointer(pFT_EraseEE, typeof(tFT_EraseEE)); - - if (ftHandle != IntPtr.Zero) - { - FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN; - // Check that it is not an FT232R or FT245R that we are trying to erase - GetDeviceType(ref DeviceType); - if (DeviceType == FT_DEVICE.FT_DEVICE_232R) - { - // If it is a device with an internal EEPROM, throw an exception - ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE; - ErrorHandler(ftStatus, ftErrorCondition); - } - - // Call FT_EraseEE - ftStatus = FT_EraseEE(ftHandle); - } - } - else - { - if (pFT_EraseEE == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_EraseEE."); - } - } - return ftStatus; - } - - //************************************************************************** - // ReadFT232BEEPROM - //************************************************************************** - // Intellisense comments - /// - /// Reads the EEPROM contents of an FT232B or FT245B device. - /// - /// FT_STATUS value from FT_EE_Read in FTD2XX DLL - /// An FT232B_EEPROM_STRUCTURE which contains only the relevant information for an FT232B and FT245B device. - /// Thrown when the current device does not match the type required by this method. - public FT_STATUS ReadFT232BEEPROM(FT232B_EEPROM_STRUCTURE ee232b) - { - // 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 FT232B or FT245B that we are trying to read - GetDeviceType(ref DeviceType); - if (DeviceType != FT_DEVICE.FT_DEVICE_BM) - { - // 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 = 2; - - // 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 - ee232b.Manufacturer = Marshal.PtrToStringAnsi(eedata.Manufacturer); - ee232b.ManufacturerID = Marshal.PtrToStringAnsi(eedata.ManufacturerID); - ee232b.Description = Marshal.PtrToStringAnsi(eedata.Description); - ee232b.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 - ee232b.VendorID = eedata.VendorID; - ee232b.ProductID = eedata.ProductID; - ee232b.MaxPower = eedata.MaxPower; - ee232b.SelfPowered = Convert.ToBoolean(eedata.SelfPowered); - ee232b.RemoteWakeup = Convert.ToBoolean(eedata.RemoteWakeup); - // B specific fields - ee232b.PullDownEnable = Convert.ToBoolean(eedata.PullDownEnable); - ee232b.SerNumEnable = Convert.ToBoolean(eedata.SerNumEnable); - ee232b.USBVersionEnable = Convert.ToBoolean(eedata.USBVersionEnable); - ee232b.USBVersion = eedata.USBVersion; - } - } - else - { - if (pFT_EE_Read == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_EE_Read."); - } - } - return ftStatus; - } - - //************************************************************************** - // ReadFT2232EEPROM - //************************************************************************** - // Intellisense comments - /// - /// Reads the EEPROM contents of an FT2232 device. - /// - /// FT_STATUS value from FT_EE_Read in FTD2XX DLL - /// An FT2232_EEPROM_STRUCTURE which contains only the relevant information for an FT2232 device. - /// Thrown when the current device does not match the type required by this method. - public FT_STATUS ReadFT2232EEPROM(FT2232_EEPROM_STRUCTURE ee2232) - { - // 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 FT2232 that we are trying to read - GetDeviceType(ref DeviceType); - if (DeviceType != FT_DEVICE.FT_DEVICE_2232) - { - // 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 = 2; - - // 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 - ee2232.Manufacturer = Marshal.PtrToStringAnsi(eedata.Manufacturer); - ee2232.ManufacturerID = Marshal.PtrToStringAnsi(eedata.ManufacturerID); - ee2232.Description = Marshal.PtrToStringAnsi(eedata.Description); - ee2232.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 - ee2232.VendorID = eedata.VendorID; - ee2232.ProductID = eedata.ProductID; - ee2232.MaxPower = eedata.MaxPower; - ee2232.SelfPowered = Convert.ToBoolean(eedata.SelfPowered); - ee2232.RemoteWakeup = Convert.ToBoolean(eedata.RemoteWakeup); - // 2232 specific fields - ee2232.PullDownEnable = Convert.ToBoolean(eedata.PullDownEnable5); - ee2232.SerNumEnable = Convert.ToBoolean(eedata.SerNumEnable5); - ee2232.USBVersionEnable = Convert.ToBoolean(eedata.USBVersionEnable5); - ee2232.USBVersion = eedata.USBVersion5; - ee2232.AIsHighCurrent = Convert.ToBoolean(eedata.AIsHighCurrent); - ee2232.BIsHighCurrent = Convert.ToBoolean(eedata.BIsHighCurrent); - ee2232.IFAIsFifo = Convert.ToBoolean(eedata.IFAIsFifo); - ee2232.IFAIsFifoTar = Convert.ToBoolean(eedata.IFAIsFifoTar); - ee2232.IFAIsFastSer = Convert.ToBoolean(eedata.IFAIsFastSer); - ee2232.AIsVCP = Convert.ToBoolean(eedata.AIsVCP); - ee2232.IFBIsFifo = Convert.ToBoolean(eedata.IFBIsFifo); - ee2232.IFBIsFifoTar = Convert.ToBoolean(eedata.IFBIsFifoTar); - ee2232.IFBIsFastSer = Convert.ToBoolean(eedata.IFBIsFastSer); - ee2232.BIsVCP = Convert.ToBoolean(eedata.BIsVCP); - } - } - else - { - if (pFT_EE_Read == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_EE_Read."); - } - } - return ftStatus; - } - - //************************************************************************** - // ReadFT232REEPROM - //************************************************************************** - // Intellisense comments - /// - /// Reads the EEPROM contents of an FT232R or FT245R device. - /// Calls FT_EE_Read in FTD2XX DLL - /// - /// An FT232R_EEPROM_STRUCTURE which contains only the relevant information for an FT232R and FT245R device. - /// Thrown when the current device does not match the type required by this method. - public FT_STATUS ReadFT232REEPROM(FT232R_EEPROM_STRUCTURE ee232r) - { - // 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 FT232R or FT245R that we are trying to read - GetDeviceType(ref DeviceType); - if (DeviceType != FT_DEVICE.FT_DEVICE_232R) - { - // 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 = 2; - - // 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 - ee232r.Manufacturer = Marshal.PtrToStringAnsi(eedata.Manufacturer); - ee232r.ManufacturerID = Marshal.PtrToStringAnsi(eedata.ManufacturerID); - ee232r.Description = Marshal.PtrToStringAnsi(eedata.Description); - ee232r.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 - ee232r.VendorID = eedata.VendorID; - ee232r.ProductID = eedata.ProductID; - ee232r.MaxPower = eedata.MaxPower; - ee232r.SelfPowered = Convert.ToBoolean(eedata.SelfPowered); - ee232r.RemoteWakeup = Convert.ToBoolean(eedata.RemoteWakeup); - // 232R specific fields - ee232r.UseExtOsc = Convert.ToBoolean(eedata.UseExtOsc); - ee232r.HighDriveIOs = Convert.ToBoolean(eedata.HighDriveIOs); - ee232r.EndpointSize = eedata.EndpointSize; - ee232r.PullDownEnable = Convert.ToBoolean(eedata.PullDownEnableR); - ee232r.SerNumEnable = Convert.ToBoolean(eedata.SerNumEnableR); - ee232r.InvertTXD = Convert.ToBoolean(eedata.InvertTXD); - ee232r.InvertRXD = Convert.ToBoolean(eedata.InvertRXD); - ee232r.InvertRTS = Convert.ToBoolean(eedata.InvertRTS); - ee232r.InvertCTS = Convert.ToBoolean(eedata.InvertCTS); - ee232r.InvertDTR = Convert.ToBoolean(eedata.InvertDTR); - ee232r.InvertDSR = Convert.ToBoolean(eedata.InvertDSR); - ee232r.InvertDCD = Convert.ToBoolean(eedata.InvertDCD); - ee232r.InvertRI = Convert.ToBoolean(eedata.InvertRI); - ee232r.Cbus0 = eedata.Cbus0; - ee232r.Cbus1 = eedata.Cbus1; - ee232r.Cbus2 = eedata.Cbus2; - ee232r.Cbus3 = eedata.Cbus3; - ee232r.Cbus4 = eedata.Cbus4; - ee232r.RIsD2XX = Convert.ToBoolean(eedata.RIsD2XX); - } - } - else - { - if (pFT_EE_Read == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_EE_Read."); - } - } - return ftStatus; - } - - //************************************************************************** - // ReadFT2232HEEPROM - //************************************************************************** - // Intellisense comments - /// - /// Reads the EEPROM contents of an FT2232H device. - /// - /// FT_STATUS value from FT_EE_Read in FTD2XX DLL - /// An FT2232H_EEPROM_STRUCTURE which contains only the relevant information for an FT2232H device. - /// Thrown when the current device does not match the type required by this method. - 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) - { - Console.WriteLine("Failed to load function FT_EE_Read."); - } - } - return ftStatus; - } - - //************************************************************************** - // ReadFT4232HEEPROM - //************************************************************************** - // Intellisense comments - /// - /// Reads the EEPROM contents of an FT4232H device. - /// - /// FT_STATUS value from FT_EE_Read in FTD2XX DLL - /// An FT4232H_EEPROM_STRUCTURE which contains only the relevant information for an FT4232H device. - /// Thrown when the current device does not match the type required by this method. - public FT_STATUS ReadFT4232HEEPROM(FT4232H_EEPROM_STRUCTURE ee4232h) - { - // 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 FT4232H that we are trying to read - GetDeviceType(ref DeviceType); - if (DeviceType != FT_DEVICE.FT_DEVICE_4232H) - { - // 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 = 4; - - // 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 - ee4232h.Manufacturer = Marshal.PtrToStringAnsi(eedata.Manufacturer); - ee4232h.ManufacturerID = Marshal.PtrToStringAnsi(eedata.ManufacturerID); - ee4232h.Description = Marshal.PtrToStringAnsi(eedata.Description); - ee4232h.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 - ee4232h.VendorID = eedata.VendorID; - ee4232h.ProductID = eedata.ProductID; - ee4232h.MaxPower = eedata.MaxPower; - ee4232h.SelfPowered = Convert.ToBoolean(eedata.SelfPowered); - ee4232h.RemoteWakeup = Convert.ToBoolean(eedata.RemoteWakeup); - // 4232H specific fields - ee4232h.PullDownEnable = Convert.ToBoolean(eedata.PullDownEnable8); - ee4232h.SerNumEnable = Convert.ToBoolean(eedata.SerNumEnable8); - ee4232h.ASlowSlew = Convert.ToBoolean(eedata.ASlowSlew); - ee4232h.ASchmittInput = Convert.ToBoolean(eedata.ASchmittInput); - ee4232h.ADriveCurrent = eedata.ADriveCurrent; - ee4232h.BSlowSlew = Convert.ToBoolean(eedata.BSlowSlew); - ee4232h.BSchmittInput = Convert.ToBoolean(eedata.BSchmittInput); - ee4232h.BDriveCurrent = eedata.BDriveCurrent; - ee4232h.CSlowSlew = Convert.ToBoolean(eedata.CSlowSlew); - ee4232h.CSchmittInput = Convert.ToBoolean(eedata.CSchmittInput); - ee4232h.CDriveCurrent = eedata.CDriveCurrent; - ee4232h.DSlowSlew = Convert.ToBoolean(eedata.DSlowSlew); - ee4232h.DSchmittInput = Convert.ToBoolean(eedata.DSchmittInput); - ee4232h.DDriveCurrent = eedata.DDriveCurrent; - ee4232h.ARIIsTXDEN = Convert.ToBoolean(eedata.ARIIsTXDEN); - ee4232h.BRIIsTXDEN = Convert.ToBoolean(eedata.BRIIsTXDEN); - ee4232h.CRIIsTXDEN = Convert.ToBoolean(eedata.CRIIsTXDEN); - ee4232h.DRIIsTXDEN = Convert.ToBoolean(eedata.DRIIsTXDEN); - ee4232h.AIsVCP = Convert.ToBoolean(eedata.AIsVCP8); - ee4232h.BIsVCP = Convert.ToBoolean(eedata.BIsVCP8); - ee4232h.CIsVCP = Convert.ToBoolean(eedata.CIsVCP8); - ee4232h.DIsVCP = Convert.ToBoolean(eedata.DIsVCP8); - } - } - else - { - if (pFT_EE_Read == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_EE_Read."); - } - } - return ftStatus; - } - - //************************************************************************** - // ReadFT232HEEPROM - //************************************************************************** - // Intellisense comments - /// - /// Reads the EEPROM contents of an FT232H device. - /// - /// FT_STATUS value from FT_EE_Read in FTD2XX DLL - /// An FT232H_EEPROM_STRUCTURE which contains only the relevant information for an FT232H device. - /// Thrown when the current device does not match the type required by this method. - public FT_STATUS ReadFT232HEEPROM(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_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 FT232H that we are trying to read - GetDeviceType(ref DeviceType); - if ((DeviceType != FT_DEVICE.FT_DEVICE_232H) - && (DeviceType != FT_DEVICE.FT_DEVICE_232HP) - && (DeviceType != FT_DEVICE.FT_DEVICE_233HP)) - { - // 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 = 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); - - // Call FT_EE_Read - ftStatus = FT_EE_Read(ftHandle, eedata); - - // Retrieve string values - ee232h.Manufacturer = Marshal.PtrToStringAnsi(eedata.Manufacturer); - ee232h.ManufacturerID = Marshal.PtrToStringAnsi(eedata.ManufacturerID); - ee232h.Description = Marshal.PtrToStringAnsi(eedata.Description); - ee232h.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 - ee232h.VendorID = eedata.VendorID; - ee232h.ProductID = eedata.ProductID; - ee232h.MaxPower = eedata.MaxPower; - ee232h.SelfPowered = Convert.ToBoolean(eedata.SelfPowered); - ee232h.RemoteWakeup = Convert.ToBoolean(eedata.RemoteWakeup); - // 232H specific fields - ee232h.PullDownEnable = Convert.ToBoolean(eedata.PullDownEnableH); - ee232h.SerNumEnable = Convert.ToBoolean(eedata.SerNumEnableH); - ee232h.ACSlowSlew = Convert.ToBoolean(eedata.ACSlowSlewH); - ee232h.ACSchmittInput = Convert.ToBoolean(eedata.ACSchmittInputH); - ee232h.ACDriveCurrent = eedata.ACDriveCurrentH; - ee232h.ADSlowSlew = Convert.ToBoolean(eedata.ADSlowSlewH); - ee232h.ADSchmittInput = Convert.ToBoolean(eedata.ADSchmittInputH); - ee232h.ADDriveCurrent = eedata.ADDriveCurrentH; - ee232h.Cbus0 = eedata.Cbus0H; - ee232h.Cbus1 = eedata.Cbus1H; - ee232h.Cbus2 = eedata.Cbus2H; - ee232h.Cbus3 = eedata.Cbus3H; - ee232h.Cbus4 = eedata.Cbus4H; - ee232h.Cbus5 = eedata.Cbus5H; - ee232h.Cbus6 = eedata.Cbus6H; - ee232h.Cbus7 = eedata.Cbus7H; - ee232h.Cbus8 = eedata.Cbus8H; - ee232h.Cbus9 = eedata.Cbus9H; - ee232h.IsFifo = Convert.ToBoolean(eedata.IsFifoH); - ee232h.IsFifoTar = Convert.ToBoolean(eedata.IsFifoTarH); - ee232h.IsFastSer = Convert.ToBoolean(eedata.IsFastSerH); - ee232h.IsFT1248 = Convert.ToBoolean(eedata.IsFT1248H); - ee232h.FT1248Cpol = Convert.ToBoolean(eedata.FT1248CpolH); - ee232h.FT1248Lsb = Convert.ToBoolean(eedata.FT1248LsbH); - ee232h.FT1248FlowControl = Convert.ToBoolean(eedata.FT1248FlowControlH); - ee232h.IsVCP = Convert.ToBoolean(eedata.IsVCPH); - ee232h.PowerSaveEnable = Convert.ToBoolean(eedata.PowerSaveEnableH); - } - } - else - { - if (pFT_EE_Read == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_EE_Read."); - } - } - return ftStatus; - } - - //************************************************************************** - // ReadXSeriesEEPROM - //************************************************************************** - // Intellisense comments - /// - /// Reads the EEPROM contents of an X-Series device. - /// - /// FT_STATUS value from FT_EEPROM_Read in FTD2XX DLL - /// An FT_XSERIES_EEPROM_STRUCTURE which contains only the relevant information for an X-Series device. - /// Thrown when the current device does not match the type required by this method. - 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) - { - Console.WriteLine("Failed to load function FT_EE_Read."); - } - } - return ftStatus; - } - - //************************************************************************** - // WriteFT232BEEPROM - //************************************************************************** - // Intellisense comments - /// - /// Writes the specified values to the EEPROM of an FT232B or FT245B device. - /// - /// FT_STATUS value from FT_EE_Program in FTD2XX DLL - /// The EEPROM settings to be written to the device - /// If the strings are too long, they will be truncated to their maximum permitted lengths - /// Thrown when the current device does not match the type required by this method. - public FT_STATUS WriteFT232BEEPROM(FT232B_EEPROM_STRUCTURE ee232b) - { - // 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 FT232B or FT245B that we are trying to write - GetDeviceType(ref DeviceType); - if (DeviceType != FT_DEVICE.FT_DEVICE_BM) - { - // If it is not, throw an exception - ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE; - ErrorHandler(ftStatus, ftErrorCondition); - } - - // Check for VID and PID of 0x0000 - if ((ee232b.VendorID == 0x0000) | (ee232b.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 = 2; - - // 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 (ee232b.Manufacturer.Length > 32) - ee232b.Manufacturer = ee232b.Manufacturer.Substring(0, 32); - if (ee232b.ManufacturerID.Length > 16) - ee232b.ManufacturerID = ee232b.ManufacturerID.Substring(0, 16); - if (ee232b.Description.Length > 64) - ee232b.Description = ee232b.Description.Substring(0, 64); - if (ee232b.SerialNumber.Length > 16) - ee232b.SerialNumber = ee232b.SerialNumber.Substring(0, 16); - - // Set string values - eedata.Manufacturer = Marshal.StringToHGlobalAnsi(ee232b.Manufacturer); - eedata.ManufacturerID = Marshal.StringToHGlobalAnsi(ee232b.ManufacturerID); - eedata.Description = Marshal.StringToHGlobalAnsi(ee232b.Description); - eedata.SerialNumber = Marshal.StringToHGlobalAnsi(ee232b.SerialNumber); - - // Map non-string elements to structure - // Standard elements - eedata.VendorID = ee232b.VendorID; - eedata.ProductID = ee232b.ProductID; - eedata.MaxPower = ee232b.MaxPower; - eedata.SelfPowered = Convert.ToUInt16(ee232b.SelfPowered); - eedata.RemoteWakeup = Convert.ToUInt16(ee232b.RemoteWakeup); - // B specific fields - eedata.Rev4 = Convert.ToByte(true); - eedata.PullDownEnable = Convert.ToByte(ee232b.PullDownEnable); - eedata.SerNumEnable = Convert.ToByte(ee232b.SerNumEnable); - eedata.USBVersionEnable = Convert.ToByte(ee232b.USBVersionEnable); - eedata.USBVersion = ee232b.USBVersion; - - // 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) - { - Console.WriteLine("Failed to load function FT_EE_Program."); - } - } - return ftStatus; - } - - //************************************************************************** - // WriteFT2232EEPROM - //************************************************************************** - // Intellisense comments - /// - /// Writes the specified values to the EEPROM of an FT2232 device. - /// Calls FT_EE_Program in FTD2XX DLL - /// - /// FT_STATUS value from FT_EE_Program in FTD2XX DLL - /// The EEPROM settings to be written to the device - /// If the strings are too long, they will be truncated to their maximum permitted lengths - /// Thrown when the current device does not match the type required by this method. - public FT_STATUS WriteFT2232EEPROM(FT2232_EEPROM_STRUCTURE ee2232) - { - // 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 FT2232 that we are trying to write - GetDeviceType(ref DeviceType); - if (DeviceType != FT_DEVICE.FT_DEVICE_2232) - { - // If it is not, throw an exception - ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE; - ErrorHandler(ftStatus, ftErrorCondition); - } - - // Check for VID and PID of 0x0000 - if ((ee2232.VendorID == 0x0000) | (ee2232.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 = 2; - - // 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 (ee2232.Manufacturer.Length > 32) - ee2232.Manufacturer = ee2232.Manufacturer.Substring(0, 32); - if (ee2232.ManufacturerID.Length > 16) - ee2232.ManufacturerID = ee2232.ManufacturerID.Substring(0, 16); - if (ee2232.Description.Length > 64) - ee2232.Description = ee2232.Description.Substring(0, 64); - if (ee2232.SerialNumber.Length > 16) - ee2232.SerialNumber = ee2232.SerialNumber.Substring(0, 16); - - // Set string values - eedata.Manufacturer = Marshal.StringToHGlobalAnsi(ee2232.Manufacturer); - eedata.ManufacturerID = Marshal.StringToHGlobalAnsi(ee2232.ManufacturerID); - eedata.Description = Marshal.StringToHGlobalAnsi(ee2232.Description); - eedata.SerialNumber = Marshal.StringToHGlobalAnsi(ee2232.SerialNumber); - - // Map non-string elements to structure - // Standard elements - eedata.VendorID = ee2232.VendorID; - eedata.ProductID = ee2232.ProductID; - eedata.MaxPower = ee2232.MaxPower; - eedata.SelfPowered = Convert.ToUInt16(ee2232.SelfPowered); - eedata.RemoteWakeup = Convert.ToUInt16(ee2232.RemoteWakeup); - // 2232 specific fields - eedata.Rev5 = Convert.ToByte(true); - eedata.PullDownEnable5 = Convert.ToByte(ee2232.PullDownEnable); - eedata.SerNumEnable5 = Convert.ToByte(ee2232.SerNumEnable); - eedata.USBVersionEnable5 = Convert.ToByte(ee2232.USBVersionEnable); - eedata.USBVersion5 = ee2232.USBVersion; - eedata.AIsHighCurrent = Convert.ToByte(ee2232.AIsHighCurrent); - eedata.BIsHighCurrent = Convert.ToByte(ee2232.BIsHighCurrent); - eedata.IFAIsFifo = Convert.ToByte(ee2232.IFAIsFifo); - eedata.IFAIsFifoTar = Convert.ToByte(ee2232.IFAIsFifoTar); - eedata.IFAIsFastSer = Convert.ToByte(ee2232.IFAIsFastSer); - eedata.AIsVCP = Convert.ToByte(ee2232.AIsVCP); - eedata.IFBIsFifo = Convert.ToByte(ee2232.IFBIsFifo); - eedata.IFBIsFifoTar = Convert.ToByte(ee2232.IFBIsFifoTar); - eedata.IFBIsFastSer = Convert.ToByte(ee2232.IFBIsFastSer); - eedata.BIsVCP = Convert.ToByte(ee2232.BIsVCP); - - // 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) - { - Console.WriteLine("Failed to load function FT_EE_Program."); - } - } - return ftStatus; - } - - //************************************************************************** - // WriteFT232REEPROM - //************************************************************************** - // Intellisense comments - /// - /// Writes the specified values to the EEPROM of an FT232R or FT245R device. - /// Calls FT_EE_Program in FTD2XX DLL - /// - /// FT_STATUS value from FT_EE_Program in FTD2XX DLL - /// The EEPROM settings to be written to the device - /// If the strings are too long, they will be truncated to their maximum permitted lengths - /// Thrown when the current device does not match the type required by this method. - public FT_STATUS WriteFT232REEPROM(FT232R_EEPROM_STRUCTURE ee232r) - { - // 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 FT232R or FT245R that we are trying to write - GetDeviceType(ref DeviceType); - if (DeviceType != FT_DEVICE.FT_DEVICE_232R) - { - // If it is not, throw an exception - ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE; - ErrorHandler(ftStatus, ftErrorCondition); - } - - // Check for VID and PID of 0x0000 - if ((ee232r.VendorID == 0x0000) | (ee232r.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 = 2; - - // 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 (ee232r.Manufacturer.Length > 32) - ee232r.Manufacturer = ee232r.Manufacturer.Substring(0, 32); - if (ee232r.ManufacturerID.Length > 16) - ee232r.ManufacturerID = ee232r.ManufacturerID.Substring(0, 16); - if (ee232r.Description.Length > 64) - ee232r.Description = ee232r.Description.Substring(0, 64); - if (ee232r.SerialNumber.Length > 16) - ee232r.SerialNumber = ee232r.SerialNumber.Substring(0, 16); - - // Set string values - eedata.Manufacturer = Marshal.StringToHGlobalAnsi(ee232r.Manufacturer); - eedata.ManufacturerID = Marshal.StringToHGlobalAnsi(ee232r.ManufacturerID); - eedata.Description = Marshal.StringToHGlobalAnsi(ee232r.Description); - eedata.SerialNumber = Marshal.StringToHGlobalAnsi(ee232r.SerialNumber); - - // Map non-string elements to structure - // Standard elements - eedata.VendorID = ee232r.VendorID; - eedata.ProductID = ee232r.ProductID; - eedata.MaxPower = ee232r.MaxPower; - eedata.SelfPowered = Convert.ToUInt16(ee232r.SelfPowered); - eedata.RemoteWakeup = Convert.ToUInt16(ee232r.RemoteWakeup); - // 232R specific fields - eedata.PullDownEnableR = Convert.ToByte(ee232r.PullDownEnable); - eedata.SerNumEnableR = Convert.ToByte(ee232r.SerNumEnable); - eedata.UseExtOsc = Convert.ToByte(ee232r.UseExtOsc); - eedata.HighDriveIOs = Convert.ToByte(ee232r.HighDriveIOs); - // Override any endpoint size the user has selected and force 64 bytes - // Some users have been known to wreck devices by setting 0 here... - eedata.EndpointSize = 64; - eedata.PullDownEnableR = Convert.ToByte(ee232r.PullDownEnable); - eedata.SerNumEnableR = Convert.ToByte(ee232r.SerNumEnable); - eedata.InvertTXD = Convert.ToByte(ee232r.InvertTXD); - eedata.InvertRXD = Convert.ToByte(ee232r.InvertRXD); - eedata.InvertRTS = Convert.ToByte(ee232r.InvertRTS); - eedata.InvertCTS = Convert.ToByte(ee232r.InvertCTS); - eedata.InvertDTR = Convert.ToByte(ee232r.InvertDTR); - eedata.InvertDSR = Convert.ToByte(ee232r.InvertDSR); - eedata.InvertDCD = Convert.ToByte(ee232r.InvertDCD); - eedata.InvertRI = Convert.ToByte(ee232r.InvertRI); - eedata.Cbus0 = ee232r.Cbus0; - eedata.Cbus1 = ee232r.Cbus1; - eedata.Cbus2 = ee232r.Cbus2; - eedata.Cbus3 = ee232r.Cbus3; - eedata.Cbus4 = ee232r.Cbus4; - eedata.RIsD2XX = Convert.ToByte(ee232r.RIsD2XX); - - // 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) - { - Console.WriteLine("Failed to load function FT_EE_Program."); - } - } - return ftStatus; - } - - //************************************************************************** - // WriteFT2232HEEPROM - //************************************************************************** - // Intellisense comments - /// - /// Writes the specified values to the EEPROM of an FT2232H device. - /// Calls FT_EE_Program in FTD2XX DLL - /// - /// FT_STATUS value from FT_EE_Program in FTD2XX DLL - /// The EEPROM settings to be written to the device - /// If the strings are too long, they will be truncated to their maximum permitted lengths - /// Thrown when the current device does not match the type required by this method. - 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) - { - Console.WriteLine("Failed to load function FT_EE_Program."); - } - } - return ftStatus; - } - - //************************************************************************** - // WriteFT4232HEEPROM - //************************************************************************** - // Intellisense comments - /// - /// Writes the specified values to the EEPROM of an FT4232H device. - /// Calls FT_EE_Program in FTD2XX DLL - /// - /// FT_STATUS value from FT_EE_Program in FTD2XX DLL - /// The EEPROM settings to be written to the device - /// If the strings are too long, they will be truncated to their maximum permitted lengths - /// Thrown when the current device does not match the type required by this method. - public FT_STATUS WriteFT4232HEEPROM(FT4232H_EEPROM_STRUCTURE ee4232h) - { - // 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 FT4232H that we are trying to write - GetDeviceType(ref DeviceType); - if (DeviceType != FT_DEVICE.FT_DEVICE_4232H) - { - // If it is not, throw an exception - ftErrorCondition = FT_ERROR.FT_INCORRECT_DEVICE; - ErrorHandler(ftStatus, ftErrorCondition); - } - - // Check for VID and PID of 0x0000 - if ((ee4232h.VendorID == 0x0000) | (ee4232h.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 = 4; - - // 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 (ee4232h.Manufacturer.Length > 32) - ee4232h.Manufacturer = ee4232h.Manufacturer.Substring(0, 32); - if (ee4232h.ManufacturerID.Length > 16) - ee4232h.ManufacturerID = ee4232h.ManufacturerID.Substring(0, 16); - if (ee4232h.Description.Length > 64) - ee4232h.Description = ee4232h.Description.Substring(0, 64); - if (ee4232h.SerialNumber.Length > 16) - ee4232h.SerialNumber = ee4232h.SerialNumber.Substring(0, 16); - - // Set string values - eedata.Manufacturer = Marshal.StringToHGlobalAnsi(ee4232h.Manufacturer); - eedata.ManufacturerID = Marshal.StringToHGlobalAnsi(ee4232h.ManufacturerID); - eedata.Description = Marshal.StringToHGlobalAnsi(ee4232h.Description); - eedata.SerialNumber = Marshal.StringToHGlobalAnsi(ee4232h.SerialNumber); - - // Map non-string elements to structure - // Standard elements - eedata.VendorID = ee4232h.VendorID; - eedata.ProductID = ee4232h.ProductID; - eedata.MaxPower = ee4232h.MaxPower; - eedata.SelfPowered = Convert.ToUInt16(ee4232h.SelfPowered); - eedata.RemoteWakeup = Convert.ToUInt16(ee4232h.RemoteWakeup); - // 4232H specific fields - eedata.PullDownEnable8 = Convert.ToByte(ee4232h.PullDownEnable); - eedata.SerNumEnable8 = Convert.ToByte(ee4232h.SerNumEnable); - eedata.ASlowSlew = Convert.ToByte(ee4232h.ASlowSlew); - eedata.ASchmittInput = Convert.ToByte(ee4232h.ASchmittInput); - eedata.ADriveCurrent = ee4232h.ADriveCurrent; - eedata.BSlowSlew = Convert.ToByte(ee4232h.BSlowSlew); - eedata.BSchmittInput = Convert.ToByte(ee4232h.BSchmittInput); - eedata.BDriveCurrent = ee4232h.BDriveCurrent; - eedata.CSlowSlew = Convert.ToByte(ee4232h.CSlowSlew); - eedata.CSchmittInput = Convert.ToByte(ee4232h.CSchmittInput); - eedata.CDriveCurrent = ee4232h.CDriveCurrent; - eedata.DSlowSlew = Convert.ToByte(ee4232h.DSlowSlew); - eedata.DSchmittInput = Convert.ToByte(ee4232h.DSchmittInput); - eedata.DDriveCurrent = ee4232h.DDriveCurrent; - eedata.ARIIsTXDEN = Convert.ToByte(ee4232h.ARIIsTXDEN); - eedata.BRIIsTXDEN = Convert.ToByte(ee4232h.BRIIsTXDEN); - eedata.CRIIsTXDEN = Convert.ToByte(ee4232h.CRIIsTXDEN); - eedata.DRIIsTXDEN = Convert.ToByte(ee4232h.DRIIsTXDEN); - eedata.AIsVCP8 = Convert.ToByte(ee4232h.AIsVCP); - eedata.BIsVCP8 = Convert.ToByte(ee4232h.BIsVCP); - eedata.CIsVCP8 = Convert.ToByte(ee4232h.CIsVCP); - eedata.DIsVCP8 = Convert.ToByte(ee4232h.DIsVCP); - - // 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) - { - Console.WriteLine("Failed to load function FT_EE_Program."); - } - } - return ftStatus; - } - - //************************************************************************** - // WriteFT232HEEPROM - //************************************************************************** - // Intellisense comments - /// - /// Writes the specified values to the EEPROM of an FT232H device. - /// Calls FT_EE_Program in FTD2XX DLL - /// - /// FT_STATUS value from FT_EE_Program in FTD2XX DLL - /// The EEPROM settings to be written to the device - /// If the strings are too long, they will be truncated to their maximum permitted lengths - /// Thrown when the current device does not match the type required by this method. - 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) - { - Console.WriteLine("Failed to load function FT_EE_Program."); - } - } - return ftStatus; - } - - //************************************************************************** - // WriteXSeriesEEPROM - //************************************************************************** - // Intellisense comments - /// - /// Writes the specified values to the EEPROM of an X-Series device. - /// Calls FT_EEPROM_Program in FTD2XX DLL - /// - /// FT_STATUS value from FT_EEPROM_Program in FTD2XX DLL - /// The EEPROM settings to be written to the device - /// If the strings are too long, they will be truncated to their maximum permitted lengths - /// Thrown when the current device does not match the type required by this method. - 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 ftStatus; - } - - //************************************************************************** - // EEReadUserArea - //************************************************************************** - // Intellisense comments - /// - /// Reads data from the user area of the device EEPROM. - /// - /// FT_STATUS from FT_UARead in FTD2XX.DLL - /// An array of bytes which will be populated with the data read from the device EEPROM user area. - /// The number of bytes actually read from the EEPROM user area. - public FT_STATUS EEReadUserArea(byte[] UserAreaDataBuffer, ref uint numBytesRead) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_UASize != IntPtr.Zero) & (pFT_EE_UARead != IntPtr.Zero)) - { - tFT_EE_UASize FT_EE_UASize = (tFT_EE_UASize)Marshal.GetDelegateForFunctionPointer(pFT_EE_UASize, typeof(tFT_EE_UASize)); - tFT_EE_UARead FT_EE_UARead = (tFT_EE_UARead)Marshal.GetDelegateForFunctionPointer(pFT_EE_UARead, typeof(tFT_EE_UARead)); - - if (ftHandle != IntPtr.Zero) - { - uint UASize = 0; - // Get size of user area to allocate an array of the correct size. - // The application must also get the UA size for its copy - ftStatus = FT_EE_UASize(ftHandle, ref UASize); - - // Make sure we have enough storage for the whole user area - if (UserAreaDataBuffer.Length >= UASize) - { - // Call FT_EE_UARead - ftStatus = FT_EE_UARead(ftHandle, UserAreaDataBuffer, UserAreaDataBuffer.Length, ref numBytesRead); - } - } - } - else - { - if (pFT_EE_UASize == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_EE_UASize."); - } - if (pFT_EE_UARead == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_EE_UARead."); - } - } - return ftStatus; - } - - //************************************************************************** - // EEWriteUserArea - //************************************************************************** - // Intellisense comments - /// - /// Writes data to the user area of the device EEPROM. - /// - /// FT_STATUS value from FT_UAWrite in FTD2XX.DLL - /// An array of bytes which will be written to the device EEPROM user area. - public FT_STATUS EEWriteUserArea(byte[] UserAreaDataBuffer) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_UASize != IntPtr.Zero) & (pFT_EE_UAWrite != IntPtr.Zero)) - { - tFT_EE_UASize FT_EE_UASize = (tFT_EE_UASize)Marshal.GetDelegateForFunctionPointer(pFT_EE_UASize, typeof(tFT_EE_UASize)); - tFT_EE_UAWrite FT_EE_UAWrite = (tFT_EE_UAWrite)Marshal.GetDelegateForFunctionPointer(pFT_EE_UAWrite, typeof(tFT_EE_UAWrite)); - - if (ftHandle != IntPtr.Zero) - { - uint UASize = 0; - // Get size of user area to allocate an array of the correct size. - // The application must also get the UA size for its copy - ftStatus = FT_EE_UASize(ftHandle, ref UASize); - - // Make sure we have enough storage for all the data in the EEPROM - if (UserAreaDataBuffer.Length <= UASize) - { - // Call FT_EE_UAWrite - ftStatus = FT_EE_UAWrite(ftHandle, UserAreaDataBuffer, UserAreaDataBuffer.Length); - } - } - } - else - { - if (pFT_EE_UASize == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_EE_UASize."); - } - if (pFT_EE_UAWrite == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_EE_UAWrite."); - } - } - return ftStatus; - } - - //************************************************************************** - // GetDeviceType - //************************************************************************** - // Intellisense comments - /// - /// Gets the chip type of the current device. - /// - /// FT_STATUS value from FT_GetDeviceInfo in FTD2XX.DLL - /// The FTDI chip type of the current device. - public FT_STATUS GetDeviceType(ref FT_DEVICE DeviceType) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_GetDeviceInfo != IntPtr.Zero) - { - tFT_GetDeviceInfo FT_GetDeviceInfo = (tFT_GetDeviceInfo)Marshal.GetDelegateForFunctionPointer(pFT_GetDeviceInfo, typeof(tFT_GetDeviceInfo)); - - uint DeviceID = 0; - byte[] sernum = new byte[16]; - byte[] desc = new byte[64]; - - DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN; - - if (ftHandle != IntPtr.Zero) - { - // Call FT_GetDeviceInfo - ftStatus = FT_GetDeviceInfo(ftHandle, ref DeviceType, ref DeviceID, sernum, desc, IntPtr.Zero); - } - } - else - { - if (pFT_GetDeviceInfo == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_GetDeviceInfo."); - } - } - return ftStatus; - } - - //************************************************************************** - // GetDeviceID - //************************************************************************** - // Intellisense comments - /// - /// Gets the Vendor ID and Product ID of the current device. - /// - /// FT_STATUS value from FT_GetDeviceInfo in FTD2XX.DLL - /// The device ID (Vendor ID and Product ID) of the current device. - public FT_STATUS GetDeviceID(ref uint DeviceID) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_GetDeviceInfo != IntPtr.Zero) - { - tFT_GetDeviceInfo FT_GetDeviceInfo = (tFT_GetDeviceInfo)Marshal.GetDelegateForFunctionPointer(pFT_GetDeviceInfo, typeof(tFT_GetDeviceInfo)); - - FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN; - byte[] sernum = new byte[16]; - byte[] desc = new byte[64]; - - if (ftHandle != IntPtr.Zero) - { - // Call FT_GetDeviceInfo - ftStatus = FT_GetDeviceInfo(ftHandle, ref DeviceType, ref DeviceID, sernum, desc, IntPtr.Zero); - } - } - else - { - if (pFT_GetDeviceInfo == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_GetDeviceInfo."); - } - } - return ftStatus; - } - - //************************************************************************** - // GetDescription - //************************************************************************** - // Intellisense comments - /// - /// Gets the description of the current device. - /// - /// FT_STATUS value from FT_GetDeviceInfo in FTD2XX.DLL - /// The description of the current device. - public FT_STATUS GetDescription(out string Description) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR; - - Description = String.Empty; - - // 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_GetDeviceInfo != IntPtr.Zero) - { - tFT_GetDeviceInfo FT_GetDeviceInfo = (tFT_GetDeviceInfo)Marshal.GetDelegateForFunctionPointer(pFT_GetDeviceInfo, typeof(tFT_GetDeviceInfo)); - - uint DeviceID = 0; - FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN; - byte[] sernum = new byte[16]; - byte[] desc = new byte[64]; - - if (ftHandle != IntPtr.Zero) - { - // Call FT_GetDeviceInfo - ftStatus = FT_GetDeviceInfo(ftHandle, ref DeviceType, ref DeviceID, sernum, desc, IntPtr.Zero); - Description = Encoding.ASCII.GetString(desc); - Description = Description.Substring(0, Description.IndexOf('\0')); - } - } - else - { - if (pFT_GetDeviceInfo == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_GetDeviceInfo."); - } - } - return ftStatus; - } - - //************************************************************************** - // GetSerialNumber - //************************************************************************** - // Intellisense comments - /// - /// Gets the serial number of the current device. - /// - /// FT_STATUS value from FT_GetDeviceInfo in FTD2XX.DLL - /// The serial number of the current device. - public FT_STATUS GetSerialNumber(out string SerialNumber) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR; - - SerialNumber = String.Empty; - - // 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_GetDeviceInfo != IntPtr.Zero) - { - tFT_GetDeviceInfo FT_GetDeviceInfo = (tFT_GetDeviceInfo)Marshal.GetDelegateForFunctionPointer(pFT_GetDeviceInfo, typeof(tFT_GetDeviceInfo)); - - uint DeviceID = 0; - FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN; - byte[] sernum = new byte[16]; - byte[] desc = new byte[64]; - - if (ftHandle != IntPtr.Zero) - { - // Call FT_GetDeviceInfo - ftStatus = FT_GetDeviceInfo(ftHandle, ref DeviceType, ref DeviceID, sernum, desc, IntPtr.Zero); - SerialNumber = Encoding.ASCII.GetString(sernum); - SerialNumber = SerialNumber.Substring(0, SerialNumber.IndexOf('\0')); - } - } - else - { - if (pFT_GetDeviceInfo == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_GetDeviceInfo."); - } - } - return ftStatus; - } - - //************************************************************************** - // GetRxBytesAvailable - //************************************************************************** - // Intellisense comments - /// - /// Gets the number of bytes available in the receive buffer. - /// - /// FT_STATUS value from FT_GetQueueStatus in FTD2XX.DLL - /// The number of bytes available to be read. - public FT_STATUS GetRxBytesAvailable(ref uint RxQueue) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_GetQueueStatus != IntPtr.Zero) - { - tFT_GetQueueStatus FT_GetQueueStatus = (tFT_GetQueueStatus)Marshal.GetDelegateForFunctionPointer(pFT_GetQueueStatus, typeof(tFT_GetQueueStatus)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_GetQueueStatus - ftStatus = FT_GetQueueStatus(ftHandle, ref RxQueue); - } - } - else - { - if (pFT_GetQueueStatus == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_GetQueueStatus."); - } - } - return ftStatus; - } - - //************************************************************************** - // GetTxBytesWaiting - //************************************************************************** - // Intellisense comments - /// - /// Gets the number of bytes waiting in the transmit buffer. - /// - /// FT_STATUS value from FT_GetStatus in FTD2XX.DLL - /// The number of bytes waiting to be sent. - public FT_STATUS GetTxBytesWaiting(ref uint TxQueue) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_GetStatus != IntPtr.Zero) - { - tFT_GetStatus FT_GetStatus = (tFT_GetStatus)Marshal.GetDelegateForFunctionPointer(pFT_GetStatus, typeof(tFT_GetStatus)); - - uint RxQueue = 0; - uint EventStatus = 0; - - if (ftHandle != IntPtr.Zero) - { - // Call FT_GetStatus - ftStatus = FT_GetStatus(ftHandle, ref RxQueue, ref TxQueue, ref EventStatus); - } - } - else - { - if (pFT_GetStatus == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_GetStatus."); - } - } - return ftStatus; - } - - //************************************************************************** - // GetEventType - //************************************************************************** - // Intellisense comments - /// - /// Gets the event type after an event has fired. Can be used to distinguish which event has been triggered when waiting on multiple event types. - /// - /// FT_STATUS value from FT_GetStatus in FTD2XX.DLL - /// The type of event that has occurred. - public FT_STATUS GetEventType(ref uint EventType) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_GetStatus != IntPtr.Zero) - { - tFT_GetStatus FT_GetStatus = (tFT_GetStatus)Marshal.GetDelegateForFunctionPointer(pFT_GetStatus, typeof(tFT_GetStatus)); - - uint RxQueue = 0; - uint TxQueue = 0; - - if (ftHandle != IntPtr.Zero) - { - // Call FT_GetStatus - ftStatus = FT_GetStatus(ftHandle, ref RxQueue, ref TxQueue, ref EventType); - } - } - else - { - if (pFT_GetStatus == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_GetStatus."); - } - } - return ftStatus; - } - - //************************************************************************** - // GetModemStatus - //************************************************************************** - // Intellisense comments - /// - /// Gets the current modem status. - /// - /// FT_STATUS value from FT_GetModemStatus in FTD2XX.DLL - /// A bit map representaion of the current modem status. - public FT_STATUS GetModemStatus(ref byte ModemStatus) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_GetModemStatus != IntPtr.Zero) - { - tFT_GetModemStatus FT_GetModemStatus = (tFT_GetModemStatus)Marshal.GetDelegateForFunctionPointer(pFT_GetModemStatus, typeof(tFT_GetModemStatus)); - - uint ModemLineStatus = 0; - - if (ftHandle != IntPtr.Zero) - { - // Call FT_GetModemStatus - ftStatus = FT_GetModemStatus(ftHandle, ref ModemLineStatus); - - } - ModemStatus = Convert.ToByte(ModemLineStatus & 0x000000FF); - } - else - { - if (pFT_GetModemStatus == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_GetModemStatus."); - } - } - return ftStatus; - } - - //************************************************************************** - // GetLineStatus - //************************************************************************** - // Intellisense comments - /// - /// Gets the current line status. - /// - /// FT_STATUS value from FT_GetModemStatus in FTD2XX.DLL - /// A bit map representaion of the current line status. - public FT_STATUS GetLineStatus(ref byte LineStatus) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_GetModemStatus != IntPtr.Zero) - { - tFT_GetModemStatus FT_GetModemStatus = (tFT_GetModemStatus)Marshal.GetDelegateForFunctionPointer(pFT_GetModemStatus, typeof(tFT_GetModemStatus)); - - uint ModemLineStatus = 0; - - if (ftHandle != IntPtr.Zero) - { - // Call FT_GetModemStatus - ftStatus = FT_GetModemStatus(ftHandle, ref ModemLineStatus); - } - LineStatus = Convert.ToByte((ModemLineStatus >> 8) & 0x000000FF); - } - else - { - if (pFT_GetModemStatus == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_GetModemStatus."); - } - } - return ftStatus; - } - - //************************************************************************** - // SetBaudRate - //************************************************************************** - // Intellisense comments - /// - /// Sets the current Baud rate. - /// - /// FT_STATUS value from FT_SetBaudRate in FTD2XX.DLL - /// The desired Baud rate for the device. - public FT_STATUS SetBaudRate(uint BaudRate) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetBaudRate != IntPtr.Zero) - { - tFT_SetBaudRate FT_SetBaudRate = (tFT_SetBaudRate)Marshal.GetDelegateForFunctionPointer(pFT_SetBaudRate, typeof(tFT_SetBaudRate)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_SetBaudRate - ftStatus = FT_SetBaudRate(ftHandle, BaudRate); - } - } - else - { - if (pFT_SetBaudRate == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetBaudRate."); - } - } - return ftStatus; - } - - //************************************************************************** - // SetDataCharacteristics - //************************************************************************** - // Intellisense comments - /// - /// Sets the data bits, stop bits and parity for the device. - /// - /// FT_STATUS value from FT_SetDataCharacteristics in FTD2XX.DLL - /// The number of data bits for UART data. Valid values are FT_DATA_BITS.FT_DATA_7 or FT_DATA_BITS.FT_BITS_8 - /// The number of stop bits for UART data. Valid values are FT_STOP_BITS.FT_STOP_BITS_1 or FT_STOP_BITS.FT_STOP_BITS_2 - /// The parity of the UART data. Valid values are FT_PARITY.FT_PARITY_NONE, FT_PARITY.FT_PARITY_ODD, FT_PARITY.FT_PARITY_EVEN, FT_PARITY.FT_PARITY_MARK or FT_PARITY.FT_PARITY_SPACE - public FT_STATUS SetDataCharacteristics(byte DataBits, byte StopBits, byte Parity) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetDataCharacteristics != IntPtr.Zero) - { - tFT_SetDataCharacteristics FT_SetDataCharacteristics = (tFT_SetDataCharacteristics)Marshal.GetDelegateForFunctionPointer(pFT_SetDataCharacteristics, typeof(tFT_SetDataCharacteristics)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_SetDataCharacteristics - ftStatus = FT_SetDataCharacteristics(ftHandle, DataBits, StopBits, Parity); - } - } - else - { - if (pFT_SetDataCharacteristics == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetDataCharacteristics."); - } - } - return ftStatus; - } - - //************************************************************************** - // SetFlowControl - //************************************************************************** - // Intellisense comments - /// - /// Sets the flow control type. - /// - /// FT_STATUS value from FT_SetFlowControl in FTD2XX.DLL - /// The type of flow control for the UART. Valid values are FT_FLOW_CONTROL.FT_FLOW_NONE, FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, FT_FLOW_CONTROL.FT_FLOW_DTR_DSR or FT_FLOW_CONTROL.FT_FLOW_XON_XOFF - /// The Xon character for Xon/Xoff flow control. Ignored if not using Xon/XOff flow control. - /// The Xoff character for Xon/Xoff flow control. Ignored if not using Xon/XOff flow control. - public FT_STATUS SetFlowControl(UInt16 FlowControl, byte Xon, byte Xoff) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetFlowControl != IntPtr.Zero) - { - tFT_SetFlowControl FT_SetFlowControl = (tFT_SetFlowControl)Marshal.GetDelegateForFunctionPointer(pFT_SetFlowControl, typeof(tFT_SetFlowControl)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_SetFlowControl - ftStatus = FT_SetFlowControl(ftHandle, FlowControl, Xon, Xoff); - } - } - else - { - if (pFT_SetFlowControl == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetFlowControl."); - } - } - return ftStatus; - } - - //************************************************************************** - // SetRTS - //************************************************************************** - // Intellisense comments - /// - /// Asserts or de-asserts the Request To Send (RTS) line. - /// - /// FT_STATUS value from FT_SetRts or FT_ClrRts in FTD2XX.DLL - /// If true, asserts RTS. If false, de-asserts RTS - public FT_STATUS SetRTS(bool Enable) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetRts != IntPtr.Zero) & (pFT_ClrRts != IntPtr.Zero)) - { - tFT_SetRts FT_SetRts = (tFT_SetRts)Marshal.GetDelegateForFunctionPointer(pFT_SetRts, typeof(tFT_SetRts)); - tFT_ClrRts FT_ClrRts = (tFT_ClrRts)Marshal.GetDelegateForFunctionPointer(pFT_ClrRts, typeof(tFT_ClrRts)); - - if (ftHandle != IntPtr.Zero) - { - if (Enable) - { - // Call FT_SetRts - ftStatus = FT_SetRts(ftHandle); - } - else - { - // Call FT_ClrRts - ftStatus = FT_ClrRts(ftHandle); - } - } - } - else - { - if (pFT_SetRts == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetRts."); - } - if (pFT_ClrRts == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_ClrRts."); - } - } - return ftStatus; - } - - //************************************************************************** - // SetDTR - //************************************************************************** - // Intellisense comments - /// - /// Asserts or de-asserts the Data Terminal Ready (DTR) line. - /// - /// FT_STATUS value from FT_SetDtr or FT_ClrDtr in FTD2XX.DLL - /// If true, asserts DTR. If false, de-asserts DTR. - public FT_STATUS SetDTR(bool Enable) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetDtr != IntPtr.Zero) & (pFT_ClrDtr != IntPtr.Zero)) - { - tFT_SetDtr FT_SetDtr = (tFT_SetDtr)Marshal.GetDelegateForFunctionPointer(pFT_SetDtr, typeof(tFT_SetDtr)); - tFT_ClrDtr FT_ClrDtr = (tFT_ClrDtr)Marshal.GetDelegateForFunctionPointer(pFT_ClrDtr, typeof(tFT_ClrDtr)); - - if (ftHandle != IntPtr.Zero) - { - if (Enable) - { - // Call FT_SetDtr - ftStatus = FT_SetDtr(ftHandle); - } - else - { - // Call FT_ClrDtr - ftStatus = FT_ClrDtr(ftHandle); - } - } - } - else - { - if (pFT_SetDtr == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetDtr."); - } - if (pFT_ClrDtr == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_ClrDtr."); - } - } - return ftStatus; - } - - //************************************************************************** - // SetTimeouts - //************************************************************************** - // Intellisense comments - /// - /// Sets the read and write timeout values. - /// - /// FT_STATUS value from FT_SetTimeouts in FTD2XX.DLL - /// Read timeout value in ms. A value of 0 indicates an infinite timeout. - /// Write timeout value in ms. A value of 0 indicates an infinite timeout. - public FT_STATUS SetTimeouts(uint ReadTimeout, uint WriteTimeout) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetTimeouts != IntPtr.Zero) - { - tFT_SetTimeouts FT_SetTimeouts = (tFT_SetTimeouts)Marshal.GetDelegateForFunctionPointer(pFT_SetTimeouts, typeof(tFT_SetTimeouts)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_SetTimeouts - ftStatus = FT_SetTimeouts(ftHandle, ReadTimeout, WriteTimeout); - } - } - else - { - if (pFT_SetTimeouts == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetTimeouts."); - } - } - return ftStatus; - } - - //************************************************************************** - // SetBreak - //************************************************************************** - // Intellisense comments - /// - /// Sets or clears the break state. - /// - /// FT_STATUS value from FT_SetBreakOn or FT_SetBreakOff in FTD2XX.DLL - /// If true, sets break on. If false, sets break off. - public FT_STATUS SetBreak(bool Enable) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetBreakOn != IntPtr.Zero) & (pFT_SetBreakOff != IntPtr.Zero)) - { - tFT_SetBreakOn FT_SetBreakOn = (tFT_SetBreakOn)Marshal.GetDelegateForFunctionPointer(pFT_SetBreakOn, typeof(tFT_SetBreakOn)); - tFT_SetBreakOff FT_SetBreakOff = (tFT_SetBreakOff)Marshal.GetDelegateForFunctionPointer(pFT_SetBreakOff, typeof(tFT_SetBreakOff)); - - if (ftHandle != IntPtr.Zero) - { - if (Enable) - { - // Call FT_SetBreakOn - ftStatus = FT_SetBreakOn(ftHandle); - } - else - { - // Call FT_SetBreakOff - ftStatus = FT_SetBreakOff(ftHandle); - } - } - } - else - { - if (pFT_SetBreakOn == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetBreakOn."); - } - if (pFT_SetBreakOff == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetBreakOff."); - } - } - return ftStatus; - } - - //************************************************************************** - // SetResetPipeRetryCount - //************************************************************************** - // Intellisense comments - /// - /// Gets or sets the reset pipe retry count. Default value is 50. - /// - /// FT_STATUS vlaue from FT_SetResetPipeRetryCount in FTD2XX.DLL - /// The reset pipe retry count. - /// Electrically noisy environments may benefit from a larger value. - public FT_STATUS SetResetPipeRetryCount(uint ResetPipeRetryCount) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetResetPipeRetryCount != IntPtr.Zero) - { - tFT_SetResetPipeRetryCount FT_SetResetPipeRetryCount = (tFT_SetResetPipeRetryCount)Marshal.GetDelegateForFunctionPointer(pFT_SetResetPipeRetryCount, typeof(tFT_SetResetPipeRetryCount)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_SetResetPipeRetryCount - ftStatus = FT_SetResetPipeRetryCount(ftHandle, ResetPipeRetryCount); - } - } - else - { - if (pFT_SetResetPipeRetryCount == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetResetPipeRetryCount."); - } - } - return ftStatus; - } - - //************************************************************************** - // GetDriverVersion - //************************************************************************** - // Intellisense comments - /// - /// Gets the current FTDIBUS.SYS driver version number. - /// - /// FT_STATUS value from FT_GetDriverVersion in FTD2XX.DLL - /// The current driver version number. - public FT_STATUS GetDriverVersion(ref uint DriverVersion) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_GetDriverVersion != IntPtr.Zero) - { - tFT_GetDriverVersion FT_GetDriverVersion = (tFT_GetDriverVersion)Marshal.GetDelegateForFunctionPointer(pFT_GetDriverVersion, typeof(tFT_GetDriverVersion)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_GetDriverVersion - ftStatus = FT_GetDriverVersion(ftHandle, ref DriverVersion); - } - } - else - { - if (pFT_GetDriverVersion == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_GetDriverVersion."); - } - } - return ftStatus; - } - - //************************************************************************** - // GetLibraryVersion - //************************************************************************** - // Intellisense comments - /// - /// Gets the current FTD2XX.DLL driver version number. - /// - /// FT_STATUS value from FT_GetLibraryVersion in FTD2XX.DLL - /// The current library version. - public FT_STATUS GetLibraryVersion(ref uint LibraryVersion) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_GetLibraryVersion != IntPtr.Zero) - { - tFT_GetLibraryVersion FT_GetLibraryVersion = (tFT_GetLibraryVersion)Marshal.GetDelegateForFunctionPointer(pFT_GetLibraryVersion, typeof(tFT_GetLibraryVersion)); - - // Call FT_GetLibraryVersion - ftStatus = FT_GetLibraryVersion(ref LibraryVersion); - } - else - { - if (pFT_GetLibraryVersion == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_GetLibraryVersion."); - } - } - return ftStatus; - } - - //************************************************************************** - // SetDeadmanTimeout - //************************************************************************** - // Intellisense comments - /// - /// Sets the USB deadman timeout value. Default is 5000ms. - /// - /// FT_STATUS value from FT_SetDeadmanTimeout in FTD2XX.DLL - /// The deadman timeout value in ms. Default is 5000ms. - public FT_STATUS SetDeadmanTimeout(uint DeadmanTimeout) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetDeadmanTimeout != IntPtr.Zero) - { - tFT_SetDeadmanTimeout FT_SetDeadmanTimeout = (tFT_SetDeadmanTimeout)Marshal.GetDelegateForFunctionPointer(pFT_SetDeadmanTimeout, typeof(tFT_SetDeadmanTimeout)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_SetDeadmanTimeout - ftStatus = FT_SetDeadmanTimeout(ftHandle, DeadmanTimeout); - } - } - else - { - if (pFT_SetDeadmanTimeout == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetDeadmanTimeout."); - } - } - return ftStatus; - } - - //************************************************************************** - // SetLatency - //************************************************************************** - // Intellisense comments - /// - /// Sets the value of the latency timer. Default value is 16ms. - /// - /// FT_STATUS value from FT_SetLatencyTimer in FTD2XX.DLL - /// The latency timer value in ms. - /// Valid values are 2ms - 255ms for FT232BM, FT245BM and FT2232 devices. - /// Valid values are 0ms - 255ms for other devices. - public FT_STATUS SetLatency(byte Latency) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetLatencyTimer != IntPtr.Zero) - { - tFT_SetLatencyTimer FT_SetLatencyTimer = (tFT_SetLatencyTimer)Marshal.GetDelegateForFunctionPointer(pFT_SetLatencyTimer, typeof(tFT_SetLatencyTimer)); - - if (ftHandle != IntPtr.Zero) - { - FT_DEVICE DeviceType = FT_DEVICE.FT_DEVICE_UNKNOWN; - // Set Bit Mode does not apply to FT8U232AM, FT8U245AM or FT8U100AX devices - GetDeviceType(ref DeviceType); - if ((DeviceType == FT_DEVICE.FT_DEVICE_BM) || (DeviceType == FT_DEVICE.FT_DEVICE_2232)) - { - // Do not allow latency of 1ms or 0ms for older devices - // since this can cause problems/lock up due to buffering mechanism - if (Latency < 2) - Latency = 2; - } - - // Call FT_SetLatencyTimer - ftStatus = FT_SetLatencyTimer(ftHandle, Latency); - } - } - else - { - if (pFT_SetLatencyTimer == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetLatencyTimer."); - } - } - return ftStatus; - } - - //************************************************************************** - // GetLatency - //************************************************************************** - // Intellisense comments - /// - /// Gets the value of the latency timer. Default value is 16ms. - /// - /// FT_STATUS value from FT_GetLatencyTimer in FTD2XX.DLL - /// The latency timer value in ms. - public FT_STATUS GetLatency(ref byte Latency) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_GetLatencyTimer != IntPtr.Zero) - { - tFT_GetLatencyTimer FT_GetLatencyTimer = (tFT_GetLatencyTimer)Marshal.GetDelegateForFunctionPointer(pFT_GetLatencyTimer, typeof(tFT_GetLatencyTimer)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_GetLatencyTimer - ftStatus = FT_GetLatencyTimer(ftHandle, ref Latency); - } - } - else - { - if (pFT_GetLatencyTimer == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_GetLatencyTimer."); - } - } - return ftStatus; - } - - //************************************************************************** - // SetUSBTransferSizes - //************************************************************************** - // Intellisense comments - /// - /// Sets the USB IN and OUT transfer sizes. - /// - /// FT_STATUS value from FT_SetUSBParameters in FTD2XX.DLL - /// The USB IN transfer size in bytes. - public FT_STATUS InTransferSize(uint InTransferSize) - // Only support IN transfer sizes at the moment - //public uint InTransferSize(uint InTransferSize, uint OutTransferSize) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetUSBParameters != IntPtr.Zero) - { - tFT_SetUSBParameters FT_SetUSBParameters = (tFT_SetUSBParameters)Marshal.GetDelegateForFunctionPointer(pFT_SetUSBParameters, typeof(tFT_SetUSBParameters)); - - uint OutTransferSize = InTransferSize; - - if (ftHandle != IntPtr.Zero) - { - // Call FT_SetUSBParameters - ftStatus = FT_SetUSBParameters(ftHandle, InTransferSize, OutTransferSize); - } - } - else - { - if (pFT_SetUSBParameters == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetUSBParameters."); - } - } - return ftStatus; - } - - //************************************************************************** - // SetCharacters - //************************************************************************** - // Intellisense comments - /// - /// Sets an event character, an error character and enables or disables them. - /// - /// FT_STATUS value from FT_SetChars in FTD2XX.DLL - /// A character that will be tigger an IN to the host when this character is received. - /// Determines if the EventChar is enabled or disabled. - /// A character that will be inserted into the data stream to indicate that an error has occurred. - /// Determines if the ErrorChar is enabled or disabled. - public FT_STATUS SetCharacters(byte EventChar, bool EventCharEnable, byte ErrorChar, bool ErrorCharEnable) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_SetChars != IntPtr.Zero) - { - tFT_SetChars FT_SetChars = (tFT_SetChars)Marshal.GetDelegateForFunctionPointer(pFT_SetChars, typeof(tFT_SetChars)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_SetChars - ftStatus = FT_SetChars(ftHandle, EventChar, Convert.ToByte(EventCharEnable), ErrorChar, Convert.ToByte(ErrorCharEnable)); - } - } - else - { - if (pFT_SetChars == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_SetChars."); - } - } - return ftStatus; - } - - //************************************************************************** - // GetEEUserAreaSize - //************************************************************************** - // Intellisense comments - /// - /// Gets the size of the EEPROM user area. - /// - /// FT_STATUS value from FT_EE_UASize in FTD2XX.DLL - /// The EEPROM user area size in bytes. - public FT_STATUS EEUserAreaSize(ref uint UASize) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_UASize != IntPtr.Zero) - { - tFT_EE_UASize FT_EE_UASize = (tFT_EE_UASize)Marshal.GetDelegateForFunctionPointer(pFT_EE_UASize, typeof(tFT_EE_UASize)); - - if (ftHandle != IntPtr.Zero) - { - ftStatus = FT_EE_UASize(ftHandle, ref UASize); - } - } - else - { - if (pFT_EE_UASize == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_EE_UASize."); - } - } - return ftStatus; - } - - //************************************************************************** - // GetCOMPort - //************************************************************************** - // Intellisense comments - /// - /// Gets the corresponding COM port number for the current device. If no COM port is exposed, an empty string is returned. - /// - /// FT_STATUS value from FT_GetComPortNumber in FTD2XX.DLL - /// The COM port name corresponding to the current device. If no COM port is installed, an empty string is passed back. - public FT_STATUS GetCOMPort(out string ComPortName) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR; - - // As ComPortName is an OUT paremeter, has to be assigned before returning - ComPortName = string.Empty; - - // 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_GetComPortNumber != IntPtr.Zero) - { - tFT_GetComPortNumber FT_GetComPortNumber = (tFT_GetComPortNumber)Marshal.GetDelegateForFunctionPointer(pFT_GetComPortNumber, typeof(tFT_GetComPortNumber)); - - Int32 ComPortNumber = -1; - if (ftHandle != IntPtr.Zero) - { - // Call FT_GetComPortNumber - ftStatus = FT_GetComPortNumber(ftHandle, ref ComPortNumber); - } - - if (ComPortNumber == -1) - { - // If no COM port installed, return an empty string - ComPortName = string.Empty; - } - else - { - // If installed, return full COM string - // This can then be passed to an instance of the SerialPort class to assign the port number. - ComPortName = "COM" + ComPortNumber.ToString(); - } - } - else - { - if (pFT_GetComPortNumber == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_GetComPortNumber."); - } - } - return ftStatus; - } - - - //************************************************************************** - // VendorCmdGet - //************************************************************************** - // Intellisense comments - /// - /// Get data from the FT4222 using the vendor command interface. - /// - /// FT_STATUS value from FT_VendorCmdSet in FTD2XX.DLL - public FT_STATUS VendorCmdGet(UInt16 request, byte[] buf, UInt16 len) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_VendorCmdGet != IntPtr.Zero) - { - tFT_VendorCmdGet FT_VendorCmdGet = (tFT_VendorCmdGet)Marshal.GetDelegateForFunctionPointer(pFT_VendorCmdGet, typeof(tFT_VendorCmdGet)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_VendorCmdGet - ftStatus = FT_VendorCmdGet(ftHandle, request, buf, len); - } - } - else - { - if (pFT_VendorCmdGet == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_VendorCmdGet."); - } - } - return ftStatus; - } - - //************************************************************************** - // VendorCmdSet - //************************************************************************** - // Intellisense comments - /// - /// Set data from the FT4222 using the vendor command interface. - /// - /// FT_STATUS value from FT_VendorCmdSet in FTD2XX.DLL - public FT_STATUS VendorCmdSet(UInt16 request, byte[] buf, UInt16 len) - { - // Initialise ftStatus to something other than FT_OK - FT_STATUS ftStatus = FT_STATUS.FT_OTHER_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_VendorCmdSet != IntPtr.Zero) - { - tFT_VendorCmdSet FT_VendorCmdSet = (tFT_VendorCmdSet)Marshal.GetDelegateForFunctionPointer(pFT_VendorCmdSet, typeof(tFT_VendorCmdSet)); - - if (ftHandle != IntPtr.Zero) - { - // Call FT_VendorCmdSet - ftStatus = FT_VendorCmdSet(ftHandle, request, buf, len); - } - } - else - { - if (pFT_VendorCmdSet == IntPtr.Zero) - { - Console.WriteLine("Failed to load function FT_VendorCmdSet."); - } - } - return ftStatus; - } - - //************************************************************************** - // IsOpen - //************************************************************************** - // Intellisense comments - /// - /// Gets the open status of the device. - /// - public bool IsOpen - { - get - { - if (ftHandle == IntPtr.Zero) - return false; - else - return true; - } - } - - //************************************************************************** - // InterfaceIdentifier - //************************************************************************** - // Intellisense comments - /// - /// Gets the interface identifier. - /// - private string InterfaceIdentifier - { - get - { - string Identifier; - Identifier = String.Empty; - if (IsOpen) - { - FT_DEVICE deviceType = FT_DEVICE.FT_DEVICE_BM; - GetDeviceType(ref deviceType); - if (deviceType == FTDI.FT_DEVICE.FT_DEVICE_2232H || - deviceType == FTDI.FT_DEVICE.FT_DEVICE_4232H || - deviceType == FTDI.FT_DEVICE.FT_DEVICE_2233HP || - deviceType == FTDI.FT_DEVICE.FT_DEVICE_4233HP || - deviceType == FTDI.FT_DEVICE.FT_DEVICE_2232HP || - deviceType == FTDI.FT_DEVICE.FT_DEVICE_4232HP || - deviceType == FTDI.FT_DEVICE.FT_DEVICE_2232HA || - deviceType == FTDI.FT_DEVICE.FT_DEVICE_4232HA || - deviceType == FTDI.FT_DEVICE.FT_DEVICE_2232) - { - string Description; - GetDescription(out Description); - Identifier = Description.Substring((Description.Length - 1)); - return Identifier; - } - } - return Identifier; - } - } - - //************************************************************************** - // ErrorHandler - //************************************************************************** - /// - /// Method to check ftStatus and ftErrorCondition values for error conditions and throw exceptions accordingly. - /// - private void ErrorHandler(FT_STATUS ftStatus, FT_ERROR ftErrorCondition) - { - if (ftStatus != FT_STATUS.FT_OK) - { - // Check FT_STATUS values returned from FTD2XX DLL calls - switch (ftStatus) - { - case FT_STATUS.FT_DEVICE_NOT_FOUND: - { - throw new FT_EXCEPTION("FTDI device not found."); - } - case FT_STATUS.FT_DEVICE_NOT_OPENED: - { - throw new FT_EXCEPTION("FTDI device not opened."); - } - case FT_STATUS.FT_DEVICE_NOT_OPENED_FOR_ERASE: - { - throw new FT_EXCEPTION("FTDI device not opened for erase."); - } - case FT_STATUS.FT_DEVICE_NOT_OPENED_FOR_WRITE: - { - throw new FT_EXCEPTION("FTDI device not opened for write."); - } - case FT_STATUS.FT_EEPROM_ERASE_FAILED: - { - throw new FT_EXCEPTION("Failed to erase FTDI device EEPROM."); - } - case FT_STATUS.FT_EEPROM_NOT_PRESENT: - { - throw new FT_EXCEPTION("No EEPROM fitted to FTDI device."); - } - case FT_STATUS.FT_EEPROM_NOT_PROGRAMMED: - { - throw new FT_EXCEPTION("FTDI device EEPROM not programmed."); - } - case FT_STATUS.FT_EEPROM_READ_FAILED: - { - throw new FT_EXCEPTION("Failed to read FTDI device EEPROM."); - } - case FT_STATUS.FT_EEPROM_WRITE_FAILED: - { - throw new FT_EXCEPTION("Failed to write FTDI device EEPROM."); - } - case FT_STATUS.FT_FAILED_TO_WRITE_DEVICE: - { - throw new FT_EXCEPTION("Failed to write to FTDI device."); - } - case FT_STATUS.FT_INSUFFICIENT_RESOURCES: - { - throw new FT_EXCEPTION("Insufficient resources."); - } - case FT_STATUS.FT_INVALID_ARGS: - { - throw new FT_EXCEPTION("Invalid arguments for FTD2XX function call."); - } - case FT_STATUS.FT_INVALID_BAUD_RATE: - { - throw new FT_EXCEPTION("Invalid Baud rate for FTDI device."); - } - case FT_STATUS.FT_INVALID_HANDLE: - { - throw new FT_EXCEPTION("Invalid handle for FTDI device."); - } - case FT_STATUS.FT_INVALID_PARAMETER: - { - throw new FT_EXCEPTION("Invalid parameter for FTD2XX function call."); - } - case FT_STATUS.FT_IO_ERROR: - { - throw new FT_EXCEPTION("FTDI device IO error."); - } - case FT_STATUS.FT_OTHER_ERROR: - { - throw new FT_EXCEPTION("An unexpected error has occurred when trying to communicate with the FTDI device."); - } - default: - break; - } - } - if (ftErrorCondition != FT_ERROR.FT_NO_ERROR) - { - // Check for other error conditions not handled by FTD2XX DLL - switch (ftErrorCondition) - { - case FT_ERROR.FT_INCORRECT_DEVICE: - { - throw new FT_EXCEPTION("The current device type does not match the EEPROM structure."); - } - case FT_ERROR.FT_INVALID_BITMODE: - { - throw new FT_EXCEPTION("The requested bit mode is not valid for the current device."); - } - case FT_ERROR.FT_BUFFER_SIZE: - { - throw new FT_EXCEPTION("The supplied buffer is not big enough."); - } - - default: - break; - } - - } - - return; - } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft2232Expander.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft2232Expander.cs new file mode 100644 index 0000000000..fef53e9d97 --- /dev/null +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft2232Expander.cs @@ -0,0 +1,29 @@ +using Meadow.Hardware; +using System; + +namespace Meadow.Foundation.ICs.IOExpanders; + +public class Ft2232Expander : FtdiExpander +{ + internal Ft2232Expander() + { + } + + /// + public override II2cBus CreateI2cBus(int channel = 0, I2cBusSpeed busSpeed = I2cBusSpeed.Standard) + { + // TODO: depends on part + // TODO: make sure no SPI is in use + var bus = new Ft23xxxI2cBus(this, busSpeed); + bus.Configure(); + return bus; + } + + /// + public override ISpiBus CreateSpiBus(int channel, SpiClockConfiguration configuration) + { + // TODO: make sure no SPI is in use + + throw new NotSupportedException(); + } +} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.cs deleted file mode 100644 index 2b6896a12e..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232h.cs +++ /dev/null @@ -1,159 +0,0 @@ -using Meadow.Hardware; -using Meadow.Units; -using System; - -namespace Meadow.Foundation.ICs.IOExpanders; - -/// -/// Represents an FT232 IO Expander -/// -public partial class Ft232h : - IDisposable, - IDigitalInputOutputController, - IDigitalOutputController, - ISpiController, - II2cController -{ - private bool _isDisposed; - private IFtdiImpl _impl; - - internal bool UsingMpsse { get; } - - /// - /// The pins - /// - public PinDefinitions Pins { get; } - - /// - /// Initializes a new instance of the class. - /// - /// Specifies whether to use Multi-Protocol Synchronous Serial Engine (MPSSE) mode (default is false). - public Ft232h(bool useMPSSE = false) - { - UsingMpsse = useMPSSE; - - _impl = UsingMpsse ? new MpsseImpl() : new Ftd2xxImpl(); - _impl.Initialize(); - - Pins = new PinDefinitions(this); - } - - /// - public II2cBus CreateI2cBus(int busNumber = 0) - { - return CreateI2cBus(busNumber, I2CClockRate.Standard); - } - - /// - public II2cBus CreateI2cBus(int busNumber, I2cBusSpeed busSpeed) - { - // TODO: convert frequency - return CreateI2cBus(busNumber, I2CClockRate.Standard); - } - - /// - public II2cBus CreateI2cBus(IPin[] pins, I2cBusSpeed busSpeed) - { - // TODO: map the pins to the bus number - // TODO: convert frequency - return CreateI2cBus(0, I2CClockRate.Standard); - } - - /// - public II2cBus CreateI2cBus(IPin clock, IPin data, I2cBusSpeed busSpeed) - { - // TODO: map the pins to the bus number - // TODO: convert frequency - return CreateI2cBus(0, I2CClockRate.Standard); - } - - private II2cBus CreateI2cBus(int busNumber, I2CClockRate clock) - { - return _impl.CreateI2cBus(busNumber, clock); - } - - /// - public ISpiBus CreateSpiBus() - { - return CreateSpiBus(0, DefaultClockConfiguration); - } - - /// - public ISpiBus CreateSpiBus(IPin clock, IPin mosi, IPin miso, SpiClockConfiguration config) - { - if (!clock.Supports(c => c.LineTypes.HasFlag(SpiLineType.Clock))) - { - throw new ArgumentException("Invalid Clock line"); - } - - // TODO: map the pins to the bus number - return CreateSpiBus(0, config); - } - - /// - public ISpiBus CreateSpiBus(IPin clock, IPin mosi, IPin miso, Frequency speed) - { - // TODO: map the pins to the bus number - var config = new SpiClockConfiguration(speed); - return CreateSpiBus(0, config); - } - - /// - public ISpiBus CreateSpiBus(int busNumber, SpiClockConfiguration config) - { - return _impl.CreateSpiBus(busNumber, config); - } - - /// - public static SpiClockConfiguration DefaultClockConfiguration - { - get => new SpiClockConfiguration( - new Frequency(MpsseSpiBus.DefaultClockRate, Frequency.UnitType.Hertz)); - } - - /// - public IDigitalInputPort CreateDigitalInputPort(IPin pin) - { - return CreateDigitalInputPort(pin, ResistorMode.Disabled); - } - - /// - public IDigitalInputPort CreateDigitalInputPort(IPin pin, ResistorMode resistorMode) - { - // TODO: need to select the proper channel based on pin - return _impl.CreateDigitalInputPort(0, pin, resistorMode); - } - - /// - public IDigitalOutputPort CreateDigitalOutputPort(IPin pin, bool initialState = false, OutputType initialOutputType = OutputType.PushPull) - { - // TODO: need to select the proper channel based on pin - return _impl.CreateDigitalOutputPort(0, pin, initialState, initialOutputType); - } - - /// - protected virtual void Dispose(bool disposing) - { - if (!_isDisposed) - { - _impl.Dispose(); - - _isDisposed = true; - } - } - - /// - /// Finalizer for the Ft232h class, used to release unmanaged resources. - /// - ~Ft232h() - { - Dispose(false); - } - - /// - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } -} \ No newline at end of file diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232hExpander.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232hExpander.cs index fc4e806f9d..c55211e98d 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232hExpander.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft232hExpander.cs @@ -1,8 +1,30 @@ -namespace Meadow.Foundation.ICs.IOExpanders; +using Meadow.Hardware; -public partial class Ft232hExpander : FtdiExpander +namespace Meadow.Foundation.ICs.IOExpanders; + +public class Ft232hExpander : FtdiExpander { internal Ft232hExpander() { } + + /// + public override II2cBus CreateI2cBus(int channel = 0, I2cBusSpeed busSpeed = I2cBusSpeed.Standard) + { + // TODO: depends on part + // TODO: make sure no SPI is in use + var bus = new Ft232hI2cBus(this, busSpeed); + bus.Configure(); + return bus; + } + + /// + public override ISpiBus CreateSpiBus(int channel, SpiClockConfiguration configuration) + { + // TODO: make sure no SPI is in use + + var bus = new Ft232hSpiBus(this, configuration); + bus.Configure(); + return bus; + } } diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxDigitalInputPort.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxDigitalInputPort.cs deleted file mode 100644 index 93820dfbc2..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxDigitalInputPort.cs +++ /dev/null @@ -1,50 +0,0 @@ -using Meadow.Hardware; -using System; - -namespace Meadow.Foundation.ICs.IOExpanders; - -/// -/// Represents a digital input port implementation for the FT23xx device. -/// -public sealed class Ft23xxDigitalInputPort : DigitalInputPortBase -{ - private MpsseChannel _device; - private ResistorMode _resistor; - - internal Ft23xxDigitalInputPort(MpsseChannel device, IPin pin, ResistorMode resistorMode, IDigitalChannelInfo channel) - : base(pin, channel) - { - Resistor = resistorMode; - _device = device; - } - - /// - public override bool State - { - get - { - // reads all 8 pis at once - var state = _device.GetGpioState(true); - // the pin key is the mask - return (state & (byte)Pin.Key) != 0; - } - } - - /// - public override ResistorMode Resistor - { - get => _resistor; - set - { - switch (value) - { - case ResistorMode.InternalPullUp: - case ResistorMode.InternalPullDown: - throw new NotSupportedException("Internal resistors are not supported"); - default: - _resistor = value; - break; - } - } - } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxDigitalOutputPort.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxDigitalOutputPort.cs deleted file mode 100644 index 9f16b7a0de..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxDigitalOutputPort.cs +++ /dev/null @@ -1,59 +0,0 @@ -using Meadow.Hardware; -using System; - -namespace Meadow.Foundation.ICs.IOExpanders; - -/// -/// Represents a digital output port implementation for the FT23xx device. -/// -public sealed class Ft23xxDigitalOutputPort : DigitalOutputPortBase -{ - private MpsseChannel _device; - private bool _state; - private bool _isHighByte; - private byte _key; - - internal Ft23xxDigitalOutputPort(MpsseChannel device, IPin pin, IDigitalChannelInfo channel, bool initialState, OutputType initialOutputType) - : base(pin, channel, initialState, initialOutputType) - { - _device = device; - - // TODO: make sure the pin isn't already in use - var key = Convert.ToUInt16(Pin.Key); - if (key > 255) - { - _isHighByte = true; - _key = (byte)(key >> 8); - } - else - { - _isHighByte = false; - _key = (byte)(key & 0xff); - } - - _device.GpioDirectionMask |= _key; - } - - /// - public override bool State - { - get => _state; - set - { - byte s = _device.GpioState; - - if (value) - { - s |= _key; - } - else - { - s &= (byte)~_key; - } - - _device.SetGpioState(!_isHighByte, _device.GpioDirectionMask, s); - _device.GpioState = s; - _state = value; - } - } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxI2cBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxI2cBus.cs deleted file mode 100644 index 1ef74cc788..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxI2cBus.cs +++ /dev/null @@ -1,90 +0,0 @@ -namespace Meadow.Foundation.ICs.IOExpanders; - -/// -/// Represents an I2C bus implementation using the FT23xx device. -/// -public sealed class Ft23xxI2cBus : II2cBus, IDisposable -{ - private MpsseChannel _device; - - /// - /// Gets the handle to the FT23xx device used by the I2C bus. - /// - public IntPtr Handle => _device.Handle; - - /// - public I2cBusSpeed BusSpeed { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - - internal Ft23xxI2cBus(MpsseChannel device) - { - if (device.Handle == IntPtr.Zero) - { - device.Open(); - } - - _device = device; - - _device.InitializeI2C(); - } - - /// - public void Dispose() - { - _device.Close(); - } - - /// - public void Exchange(byte peripheralAddress, Span writeBuffer, Span readBuffer) - { - Write(peripheralAddress, writeBuffer); - Read(peripheralAddress, readBuffer); - } - - /// - public void Read(byte peripheralAddress, Span readBuffer) - { - _device.I2cStart(); - var ack = _device.I2cSendDeviceAddrAndCheckACK(peripheralAddress, true); - if (!ack) - { - _device.I2cStop(); - throw new IOException($"NACK on address. No peripheral detected."); - } - - for (int i = 0; i < readBuffer.Length - 1; i++) - { - readBuffer[i] = _device.I2CReadByte(true); - } - - if (readBuffer.Length > 0) - { - readBuffer[readBuffer.Length - 1] = _device.I2CReadByte(false); - } - - _device.I2cStop(); - } - - /// - public void Write(byte peripheralAddress, Span writeBuffer) - { - _device.I2cStart(); - var ack = _device.I2cSendDeviceAddrAndCheckACK(peripheralAddress, false); - if (!ack) - { - _device.I2cStop(); - throw new IOException($"Error writing device while setting up address"); - } - - for (int i = 0; i < writeBuffer.Length; i++) - { - ack = _device.I2cSendByteAndCheckACK(writeBuffer[i]); - if (!ack) - { - _device.I2cStop(); - throw new IOException($"Error writing device on byte {i}"); - } - } - - _device.I2cStop(); - } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxSpiBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxSpiBus.cs deleted file mode 100644 index 4c4c79885b..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ft23xxSpiBus.cs +++ /dev/null @@ -1,67 +0,0 @@ -using Meadow.Hardware; -using Meadow.Units; -using System; - -namespace Meadow.Foundation.ICs.IOExpanders; - -/// -/// Represents an SPI bus implementation using the FT23xx device. -/// -public sealed class Ft23xxSpiBus : IFt232Bus, ISpiBus, IDisposable -{ - private MpsseChannel _device; - - /// - /// Gets the handle to the FT23xx device used by the SPI bus. - /// - public IntPtr Handle => _device.Handle; - - /// - public byte GpioDirectionMask { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - - /// - public byte GpioState { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - - /// - public Frequency[] SupportedSpeeds => throw new NotImplementedException(); - - /// - public SpiClockConfiguration Configuration { get; } - - internal Ft23xxSpiBus(MpsseChannel device, SpiClockConfiguration config) - { - Configuration = config; - - if (device.Handle == IntPtr.Zero) - { - device.Open(); - } - - _device = device; - - _device.InitializeSpi(Configuration); - } - - /// - public void Dispose() - { - } - - /// - public void Exchange(IDigitalOutputPort? chipSelect, Span writeBuffer, Span readBuffer, ChipSelectMode csMode) - { - _device.SpiExchange(chipSelect, writeBuffer, readBuffer, csMode); - } - - /// - public void Read(IDigitalOutputPort? chipSelect, Span readBuffer, ChipSelectMode csMode) - { - _device.SpiRead(chipSelect, readBuffer, csMode); - } - - /// - public void Write(IDigitalOutputPort? chipSelect, Span writeBuffer, ChipSelectMode csMode) - { - _device.SpiWrite(chipSelect, writeBuffer, csMode); - } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ftd2xxImpl.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ftd2xxImpl.cs deleted file mode 100644 index a761cf7dbe..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Ftd2xxImpl.cs +++ /dev/null @@ -1,69 +0,0 @@ -using Meadow.Hardware; - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal class Ftd2xxImpl : IFtdiImpl -{ - private MpsseChannelCollection _devices = default!; - - public void Initialize() - { - _devices = new MpsseChannelCollection(); - _devices.Refresh(); - } - - public II2cBus CreateI2cBus(int channel, I2CClockRate clock) - { - if (_devices.Count == 0) - { - throw new DeviceNotFoundException(); - } - - _devices[channel].Open(); - - return new Ft23xxI2cBus(_devices[channel]); - } - - public ISpiBus CreateSpiBus(int channel, SpiClockConfiguration config) - { - if (_devices.Count == 0) - { - throw new DeviceNotFoundException(); - } - - _devices[channel].Open(); - - return new Ft23xxSpiBus(_devices[channel], config); - } - - public IDigitalInputPort CreateDigitalInputPort(int channel, IPin pin, ResistorMode resistorMode) - { - if (_devices.Count == 0) - { - throw new DeviceNotFoundException(); - } - - return new Ft23xxDigitalInputPort(_devices[channel], pin, resistorMode, - new DigitalChannelInfo(pin.Name, true, true, false, false, false, false)); - } - - public IDigitalOutputPort CreateDigitalOutputPort(int channel, IPin pin, bool initialState = false, OutputType initialOutputType = OutputType.PushPull) - { - if (_devices.Count == 0) - { - throw new DeviceNotFoundException(); - } - - return new Ft23xxDigitalOutputPort(_devices[channel], pin, - new DigitalChannelInfo(pin.Name, true, true, false, false, false, false), - initialState, initialOutputType); - } - - public void Dispose() - { - foreach (var d in _devices) - { - d.Close(); - } - } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hI2cBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hI2cBus.cs index 3415a36ffb..0797c298ee 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hI2cBus.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hI2cBus.cs @@ -1,4 +1,5 @@ -using System; +using Meadow.Hardware; +using System; using System.Threading; namespace Meadow.Foundation.ICs.IOExpanders; @@ -9,12 +10,12 @@ public class Ft232hI2cBus : I2CBus { private FtdiExpander _expander; - internal Ft232hI2cBus(FtdiExpander expander) + internal Ft232hI2cBus(FtdiExpander expander, I2cBusSpeed busSpeed) + : base(expander, busSpeed) { - _expander = expander; } - public override void Configure() + internal override void Configure() { // Setup the clock and other elements Span toSend = stackalloc byte[10]; @@ -30,7 +31,7 @@ public override void Configure() // TCK period = 60MHz / (( 1 + [ (0xValueH * 256) OR 0xValueL] ) * 2) // Command to set clock divisor toSend[idx++] = (byte)Native.FT_OPCODE.SetClockDivisor; - uint clockDivisor = (60000 / (_expander.I2cBusFrequencyKbps * 2)) - 1; + uint clockDivisor = (60000 / (((uint)BusSpeed / 1000) * 2)) - 1; toSend[idx++] = (byte)(clockDivisor & 0x00FF); toSend[idx++] = (byte)((clockDivisor >> 8) & 0x00FF); @@ -63,7 +64,7 @@ public override void Configure() Thread.Sleep(30); } - public override void Start() + internal override void Start() { // SDA high, SCL high var direction = (byte)(PinDirection.SDAoutSCLout | (_expander.GpioDirectionLow & MaskGpio)); @@ -86,7 +87,7 @@ public override void Start() // Wait(6); } - public override void Stop() + internal override void Stop() { // SDA low, SCL low var state = (byte)(PinData.SDAloSCLlo | (_expander.GpioStateLow & MaskGpio)); @@ -103,7 +104,7 @@ public override void Stop() Wait(6); } - public override void Idle() + internal override void Idle() { // SDA high, SCL high // FT232H always output due to open drain capability @@ -112,7 +113,7 @@ public override void Idle() _expander.SetGpioDirectionAndState(true, direction, state); } - public override TransferStatus SendDataByte(byte data) + internal override TransferStatus SendDataByte(byte data) { Span txBuffer = stackalloc byte[10]; Span rxBuffer = stackalloc byte[1]; @@ -139,7 +140,7 @@ public override TransferStatus SendDataByte(byte data) return (rxBuffer[0] & 0x01) == 0 ? TransferStatus.Ack : TransferStatus.Nack; } - public override byte ReadDataByte(bool ackAfterRead) + internal override byte ReadDataByte(bool ackAfterRead) { int idx = 0; Span toSend = stackalloc byte[10]; diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hSpiBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hSpiBus.cs index d85300ec35..f465de581b 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hSpiBus.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft232hSpiBus.cs @@ -11,17 +11,23 @@ public class Ft232hSpiBus : SpiBus private FtdiExpander _expander; private SpiClockConfiguration _configuration; - public override Frequency[] SupportedSpeeds => throw new NotImplementedException(); + /// + public override Frequency[] SupportedSpeeds => + new Frequency[] + { + 1000000.Hertz() + }; + /// public override SpiClockConfiguration Configuration => _configuration; - internal Ft232hSpiBus(FtdiExpander expander) + internal Ft232hSpiBus(FtdiExpander expander, SpiClockConfiguration configuration) { - _configuration = new SpiClockConfiguration(1000000.Hertz()); + _configuration = configuration; _expander = expander; } - public override void Configure() + internal override void Configure() { // Setup the clock and other elements Span toSend = stackalloc byte[5]; @@ -32,7 +38,7 @@ public override void Configure() toSend[idx++] = (byte)Native.FT_OPCODE.TurnOffAdaptiveClocking; // set SPI clock rate toSend[idx++] = (byte)Native.FT_OPCODE.SetClockDivisor; - uint clockDivisor = (12000 / (_expander.SpiBusFrequencyKbps * 2)) - 1; + uint clockDivisor = (uint)(12000 / (_configuration.Speed.Kilohertz * 2)) - 1; toSend[idx++] = (byte)(clockDivisor & 0x00FF); toSend[idx++] = (byte)((clockDivisor >> 8) & 0x00FF); @@ -42,35 +48,60 @@ public override void Configure() _expander.SetGpioDirectionAndState(true, _expander.GpioDirectionLow |= 0x03, _expander.GpioStateLow); } - /* - # Set SPI clock rate - def set_spi_clock(d, hz): - div = int((12000000 / (hz * 2)) - 1) # Set SPI clock - ft_write(d, (0x86, div%256, div//256)) - -if dev: - print("FTDI device opened") - set_bitmode(dev, OPS, 2) # Set SPI mode - set_spi_clock(dev, 1000000) # Set SPI clock - ft_write(dev, (0x80, 0, OPS+OE+LE)) # Set outputs - data = dig_segs[DIG1], dig_segs[DIG2] # Convert digits to segs - ft_write_cmd_bytes(dev, 0x11, data) # Write seg bit data - ft_write(dev, (0x80, LE, OPS+OE+LE)) # Latch = 1 - ft_write(dev, (0x80, OE, OPS+OE+LE)) # Latch = 0, disp = 1 - print("Displaying '%u%u'" % (DIG2, DIG1)) - time.sleep(1) - ft_write(dev, (0x80, 0, OPS+OE+LE)) # Latch = disp = 0 - print("Display off") - dev.close() - */ - + /// public override void Exchange(IDigitalOutputPort? chipSelect, Span writeBuffer, Span readBuffer, ChipSelectMode csMode = ChipSelectMode.ActiveLow) { - throw new NotImplementedException(); + byte clock; + switch (_configuration.SpiMode) + { + default: + case SpiClockConfiguration.Mode.Mode3: + case SpiClockConfiguration.Mode.Mode0: + clock = (byte)Native.FT_OPCODE.ClockDataBytesOutOnMinusVeClockMSBFirst; + break; + case SpiClockConfiguration.Mode.Mode2: + case SpiClockConfiguration.Mode.Mode1: + clock = (byte)Native.FT_OPCODE.ClockDataBytesOutOnPlusVeClockMSBFirst; + break; + } + + if (chipSelect != null) + { + chipSelect.State = csMode == ChipSelectMode.ActiveLow ? false : true; + } + + int idx = 0; + Span toSend = stackalloc byte[3 + writeBuffer.Length]; + toSend[idx++] = clock; + toSend[idx++] = (byte)((writeBuffer.Length - 1) & 0xff); // LSB of length to write + toSend[idx++] = (byte)((writeBuffer.Length - 1) >> 8); ; // MSB of length to write + writeBuffer.CopyTo(toSend[3..]); + _expander.Write(toSend); + _expander.ReadInto(readBuffer); + + if (chipSelect != null) + { + chipSelect.State = csMode == ChipSelectMode.ActiveLow ? true : false; + } } + /// public override void Read(IDigitalOutputPort? chipSelect, Span readBuffer, ChipSelectMode csMode = ChipSelectMode.ActiveLow) { + byte clock; + switch (_configuration.SpiMode) + { + default: + case SpiClockConfiguration.Mode.Mode3: + case SpiClockConfiguration.Mode.Mode0: + clock = (byte)Native.FT_OPCODE.ClockDataBytesInOnPlusVeClockMSBFirst; + break; + case SpiClockConfiguration.Mode.Mode2: + case SpiClockConfiguration.Mode.Mode1: + clock = (byte)Native.FT_OPCODE.ClockDataBytesInOnMinusVeClockMSBFirst; + break; + } + if (chipSelect != null) { chipSelect.State = csMode == ChipSelectMode.ActiveLow ? false : true; @@ -78,9 +109,9 @@ public override void Read(IDigitalOutputPort? chipSelect, Span readBuffer, Span toSend = stackalloc byte[4]; var idx = 0; - toSend[idx++] = (byte)Native.FT_OPCODE.ClockDataBytesInOnMinusVeClockMSBFirst; // clock in on falling edge - toSend[idx++] = (byte)(readBuffer.Length % 256 - 1); // LSB of length to read - toSend[idx++] = 0; // MSB of length to read + toSend[idx++] = clock; + toSend[idx++] = (byte)((readBuffer.Length - 1) & 0xff); // LSB of length to read + toSend[idx++] = (byte)((readBuffer.Length - 1) >> 8); ; // MSB of length to read toSend[idx++] = (byte)Native.FT_OPCODE.SendImmediate; // read now _expander.Write(toSend); var readCount = _expander.ReadInto(readBuffer); @@ -91,13 +122,41 @@ public override void Read(IDigitalOutputPort? chipSelect, Span readBuffer, } } + /// public override void Write(IDigitalOutputPort? chipSelect, Span writeBuffer, ChipSelectMode csMode = ChipSelectMode.ActiveLow) { + if (writeBuffer.Length > 65535) + { + throw new ArgumentException("Buffer too large, maximum size if 65535"); + } + + byte clock; + switch (_configuration.SpiMode) + { + default: + case SpiClockConfiguration.Mode.Mode3: + case SpiClockConfiguration.Mode.Mode0: + clock = (byte)Native.FT_OPCODE.ClockDataBytesOutOnMinusVeClockMSBFirst; + break; + case SpiClockConfiguration.Mode.Mode2: + case SpiClockConfiguration.Mode.Mode1: + clock = (byte)Native.FT_OPCODE.ClockDataBytesOutOnPlusVeClockMSBFirst; + break; + } + if (chipSelect != null) { chipSelect.State = csMode == ChipSelectMode.ActiveLow ? false : true; } + int idx = 0; + Span toSend = stackalloc byte[3 + writeBuffer.Length]; + toSend[idx++] = clock; + toSend[idx++] = (byte)((writeBuffer.Length - 1) & 0xff); // LSB of length to write + toSend[idx++] = (byte)((writeBuffer.Length - 1) >> 8); ; // MSB of length to write + writeBuffer.CopyTo(toSend[3..]); + _expander.Write(toSend); + if (chipSelect != null) { chipSelect.State = csMode == ChipSelectMode.ActiveLow ? true : false; diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft23xxxI2cBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft23xxxI2cBus.cs index 1970ed4335..216e5f4523 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft23xxxI2cBus.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.Ft23xxxI2cBus.cs @@ -7,16 +7,12 @@ public abstract partial class FtdiExpander { public class Ft23xxxI2cBus : I2CBus, II2cBus { - private FtdiExpander _expander; - - public I2cBusSpeed BusSpeed { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - - internal Ft23xxxI2cBus(FtdiExpander expander) + internal Ft23xxxI2cBus(FtdiExpander expander, I2cBusSpeed busSpeed) + : base(expander, busSpeed) { - _expander = expander; } - public override void Configure() + internal override void Configure() { // Setup the clock and other elements Span toSend = stackalloc byte[10]; @@ -31,7 +27,7 @@ public override void Configure() // TCK period = 60MHz / (( 1 + [ (0xValueH * 256) OR 0xValueL] ) * 2) // Command to set clock divisor toSend[idx++] = (byte)Native.FT_OPCODE.SetClockDivisor; - uint clockDivisor = (60000 / (_expander.I2cBusFrequencyKbps * 2)) - 1; + uint clockDivisor = (60000 / (((uint)BusSpeed / 1000) * 2)) - 1; toSend[idx++] = (byte)(clockDivisor & 0x00FF); toSend[idx++] = (byte)((clockDivisor >> 8) & 0x00FF); // loopback off @@ -55,7 +51,7 @@ public override void Configure() _expander.Write(toSend); } - public override void Start() + internal override void Start() { // Both SDA and SCL high (setting to input simulates open drain high) var state = (byte)(PinData.SDAloSCLlo | (_expander.GpioStateLow & MaskGpio)); @@ -78,7 +74,7 @@ public override void Start() Wait(6); } - public override void Stop() + internal override void Stop() { // SDA low, SCL low var state = (byte)(PinData.SDAloSCLlo | (_expander.GpioStateLow & MaskGpio)); @@ -98,7 +94,7 @@ public override void Stop() Wait(6); } - public override void Idle() + internal override void Idle() { // SDA high, SCL high var state = (byte)(PinData.SDAloSCLlo | (_expander.GpioStateLow & MaskGpio)); @@ -107,7 +103,7 @@ public override void Idle() _expander.SetGpioDirectionAndState(true, direction, state); } - public override TransferStatus SendDataByte(byte data) + internal override TransferStatus SendDataByte(byte data) { Span txBuffer = stackalloc byte[13]; Span rxBuffer = stackalloc byte[1]; @@ -143,7 +139,7 @@ public override TransferStatus SendDataByte(byte data) return (rxBuffer[0] & 0x01) == 0 ? TransferStatus.Ack : TransferStatus.Nack; } - public override byte ReadDataByte(bool ackAfterRead) + internal override byte ReadDataByte(bool ackAfterRead) { int idx = 0; Span toSend = stackalloc byte[16]; diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.I2CBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.I2CBus.cs index 6adb107c05..5dc5000436 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.I2CBus.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.I2CBus.cs @@ -9,20 +9,27 @@ public abstract partial class FtdiExpander { public abstract class I2CBus : II2cBus { - public I2cBusSpeed BusSpeed { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + protected internal readonly FtdiExpander _expander; + + public I2cBusSpeed BusSpeed { get; set; } private SpinWait _spinWait = new(); - public abstract void Configure(); - public abstract void Start(); - public abstract void Stop(); - public abstract void Idle(); - public abstract TransferStatus SendDataByte(byte data); - public abstract byte ReadDataByte(bool ackAfterRead); - protected const byte NumberCycles = 6; - protected const byte MaskGpio = 0xF8; + internal abstract void Configure(); + internal abstract void Start(); + internal abstract void Stop(); + internal abstract void Idle(); + internal abstract TransferStatus SendDataByte(byte data); + internal abstract byte ReadDataByte(bool ackAfterRead); + protected internal const byte MaskGpio = 0xF8; + + internal I2CBus(FtdiExpander expander, I2cBusSpeed busSpeed) + { + _expander = expander; + BusSpeed = busSpeed; + } - public enum TransferStatus + internal enum TransferStatus { Ack = 0, Nack diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.SpiBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.SpiBus.cs index d53aeae04f..3877bdb667 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.SpiBus.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.SpiBus.cs @@ -8,12 +8,18 @@ public abstract partial class FtdiExpander { public abstract class SpiBus : ISpiBus { + /// public abstract Frequency[] SupportedSpeeds { get; } + /// public abstract SpiClockConfiguration Configuration { get; } - public abstract void Configure(); + internal abstract void Configure(); + + /// public abstract void Exchange(IDigitalOutputPort? chipSelect, Span writeBuffer, Span readBuffer, ChipSelectMode csMode = ChipSelectMode.ActiveLow); + /// public abstract void Read(IDigitalOutputPort? chipSelect, Span readBuffer, ChipSelectMode csMode = ChipSelectMode.ActiveLow); + /// public abstract void Write(IDigitalOutputPort? chipSelect, Span writeBuffer, ChipSelectMode csMode = ChipSelectMode.ActiveLow); } } \ No newline at end of file diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs index dd21f73408..ed4f640a9a 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/FtdiExpander.cs @@ -30,8 +30,8 @@ public abstract partial class FtdiExpander : internal string Description { get; private set; } internal IntPtr Handle { get; private set; } - internal uint I2cBusFrequencyKbps { get; private set; } = 400; - internal uint SpiBusFrequencyKbps { get; private set; } = 1000; + public abstract II2cBus CreateI2cBus(int channel = 0, I2cBusSpeed busSpeed = I2cBusSpeed.Standard); + public abstract ISpiBus CreateSpiBus(int channel, SpiClockConfiguration configuration); /// /// The pins @@ -48,7 +48,7 @@ internal static FtdiExpander Create( string description, IntPtr handle) { - var expander = deviceType switch + FtdiExpander expander = deviceType switch { FtDeviceType.Ft232H => new Ft232hExpander { @@ -60,6 +60,18 @@ internal static FtdiExpander Create( Description = description, Handle = handle }, + FtDeviceType.Ft2232 => new Ft2232Expander + { + Index = index, + Flags = flags, + ID = id, + LocID = locid, + SerialNumber = serialNumber, + Description = description, + Handle = handle + }, + FtDeviceType.Ft2232H => throw new NotImplementedException(), + FtDeviceType.Ft4232H => throw new NotImplementedException(), _ => throw new NotSupportedException(), }; @@ -106,16 +118,6 @@ private void InitializeGpio() InitializeMpsse(); } - private bool CheckStatus(FTDI.FT_STATUS status) - { - if (status == FTDI.FT_STATUS.FT_OK) - { - return true; - } - - throw new Exception($"Native error: {status}"); - } - private void ClearInputBuffer() { var available = GetAvailableBytes(); @@ -142,7 +144,7 @@ private uint GetAvailableBytes() internal void SetGpioDirectionAndState(bool lowByte, byte direction, byte state) { Span outBuffer = stackalloc byte[3]; - outBuffer[0] = (byte)(lowByte ? FTDI.FT_OPCODE.SetDataBitsLowByte : FTDI.FT_OPCODE.SetDataBitsHighByte); + outBuffer[0] = (byte)(lowByte ? Native.FT_OPCODE.SetDataBitsLowByte : Native.FT_OPCODE.SetDataBitsHighByte); outBuffer[1] = state; //data outBuffer[2] = direction; //direction 1 == output, 0 == input @@ -184,7 +186,7 @@ private void InitializeMpsse() } } - public int ReadInto(Span buffer) + internal int ReadInto(Span buffer) { var totalRead = 0; uint read = 0; @@ -212,6 +214,7 @@ internal void Write(ReadOnlySpan data) FT_Write(Handle, in MemoryMarshal.GetReference(data), (ushort)data.Length, ref written)); } + /// public IDigitalOutputPort CreateDigitalOutputPort(IPin pin, bool initialState = false, OutputType initialOutputType = OutputType.PushPull) { var p = pin as FtdiPin; @@ -254,41 +257,37 @@ public IDigitalOutputPort CreateDigitalOutputPort(IPin pin, bool initialState = return new DigitalOutputPort(this, pin, channel, initialState, initialOutputType); } - public II2cBus CreateI2cBus(int busNumber = 1, I2cBusSpeed busSpeed = I2cBusSpeed.Standard) - { - // TODO: depends on part - // TODO: make sure no SPI is in use - var bus = new Ft232hI2cBus(this); - bus.Configure(); - return bus; - } - + /// public II2cBus CreateI2cBus(IPin[] pins, I2cBusSpeed busSpeed) { return CreateI2cBus(1); } + /// public II2cBus CreateI2cBus(IPin clock, IPin data, I2cBusSpeed busSpeed) { return CreateI2cBus(1); } + /// public ISpiBus CreateSpiBus(IPin clock, IPin copi, IPin cipo, SpiClockConfiguration config) { - // TODO: depends on part - // TODO: make sure no SPI is in use - var bus = new Ft232hSpiBus(this); - bus.Configure(); - return bus; + return CreateSpiBus(0, config); } + /// public ISpiBus CreateSpiBus(IPin clock, IPin copi, IPin cipo, Frequency speed) { - return CreateSpiBus(clock, copi, cipo, new SpiClockConfiguration(speed)); + return CreateSpiBus(0, new SpiClockConfiguration(speed)); + } + + public ISpiBus CreateSpiBus(SpiClockConfiguration configuration) + { + return CreateSpiBus(0, configuration); } - public ISpiBus CreateSpiBus() + public ISpiBus CreateSpiBus(int channel = 0) { - return CreateSpiBus(null, null, null, 1000000.Hertz()); + return CreateSpiBus(channel, new SpiClockConfiguration(1000000.Hertz())); } } diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/ICs.IOExpanders.Ft232h.csproj b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/ICs.IOExpanders.Ft232h.csproj index d34dc629e5..54ce741fe0 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/ICs.IOExpanders.Ft232h.csproj +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/ICs.IOExpanders.Ft232h.csproj @@ -19,44 +19,8 @@ true Ft232h USB IOExpander for GPIO, I2C, SPI on Windows - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/IFt232Bus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/IFt232Bus.cs deleted file mode 100644 index bade933078..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/IFt232Bus.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; - -namespace Meadow.Foundation.ICs.IOExpanders; - -/// -/// Represents an interface for interacting with an FT232 bus. -/// -internal interface IFt232Bus -{ - /// - /// Gets the handle to the FT232 device. - /// - IntPtr Handle { get; } - - /// - /// Gets or sets the GPIO direction mask for the FT232 device. - /// - byte GpioDirectionMask { get; set; } - - /// - /// Gets or sets the state of the GPIO pins on the FT232 device. - /// - byte GpioState { get; set; } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/IFtdiImpl.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/IFtdiImpl.cs deleted file mode 100644 index 0ce8faca27..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/IFtdiImpl.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Meadow.Hardware; -using System; - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal interface IFtdiImpl : IDisposable -{ - void Initialize(); - II2cBus CreateI2cBus(int busNumber, I2CClockRate clock); - ISpiBus CreateSpiBus(int busNumber, SpiClockConfiguration config); - IDigitalInputPort CreateDigitalInputPort(int channel, IPin pin, ResistorMode resistorMode); - IDigitalOutputPort CreateDigitalOutputPort(int channel, IPin pin, bool initialState = false, OutputType initialOutputType = OutputType.PushPull); -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.Gpio.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.Gpio.cs deleted file mode 100644 index 7bd558a6f1..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.Gpio.cs +++ /dev/null @@ -1,93 +0,0 @@ -using Meadow.Hardware; -using System; -using System.Threading; -using static Meadow.Foundation.ICs.IOExpanders.Native.Ftd2xx; - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class MpsseChannel -{ - private bool? _useMpseeForGpio = null; - - public byte GpioDirectionMask { get; set; } - public byte GpioState { get; set; } - - public I2cBusSpeed BusSpeed { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } - - internal void InitializeGpio() - { - // for now we don't support GPIO channels C and D on the FT4232H - - if (Handle == IntPtr.Zero) Open(); - - _useMpseeForGpio = true; - - if (_useMpseeForGpio ?? true) - { - // Reset - Native.CheckStatus( - FT_SetBitMode(Handle, 0x00, Native.FT_BITMODE.FT_BITMODE_RESET)); - - // Enable MPSSE mode - Native.CheckStatus( - FT_SetBitMode(Handle, 0x00, Native.FT_BITMODE.FT_BITMODE_MPSSE)); - - Thread.Sleep(50); - - ClearInputBuffer(); - InitializeMpsse(); - } - else - { - Native.CheckStatus( - FT_SetBitMode(Handle, 0x00, Native.FT_BITMODE.FT_BITMODE_RESET)); - - // Enable asynchronous bit bang mode, thise does allow to have different pin modes, put all pins as input - Native.CheckStatus( - FT_SetBitMode(Handle, 0x00, Native.FT_BITMODE.FT_BITMODE_ASYNC_BITBANG)); - - ClearInputBuffer(); - } - } - - internal byte GetGpioState(bool lowByte) - { - if (!_useMpseeForGpio.HasValue) - { - InitializeGpio(); - } - - if (_useMpseeForGpio ?? false) - { - Span outBuffer = stackalloc byte[2]; - Span inBuffer = stackalloc byte[1]; - outBuffer[0] = (byte)(lowByte ? Native.FT_OPCODE.ReadDataBitsLowByte : Native.FT_OPCODE.ReadDataBitsHighByte); - outBuffer[1] = (byte)Native.FT_OPCODE.SendImmediate; - Write(outBuffer); - ReadInto(inBuffer); - return inBuffer[0]; - } - - throw new NotImplementedException(); - } - - internal void SetGpioState(bool lowByte, byte direction, byte state) - { - if (!_useMpseeForGpio.HasValue) - { - InitializeGpio(); - } - - if (_useMpseeForGpio ?? false) - { - Span outBuffer = stackalloc byte[3]; - outBuffer[0] = (byte)(lowByte ? Native.FT_OPCODE.SetDataBitsLowByte : Native.FT_OPCODE.SetDataBitsHighByte); - outBuffer[1] = state; //data - outBuffer[2] = direction; //direction 1 == output, 0 == input - Write(outBuffer); - return; - } - - throw new NotImplementedException(); - } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.I2c.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.I2c.cs deleted file mode 100644 index 2742f03da2..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.I2c.cs +++ /dev/null @@ -1,398 +0,0 @@ -using System; -using static Meadow.Foundation.ICs.IOExpanders.Native; -using static Meadow.Foundation.ICs.IOExpanders.Native.Ftd2xx; - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class MpsseChannel -{ - private byte GpioLowData = 0; - private byte GpioLowDir = 0; - // private byte GpioHighData = 0; - // private byte GpioHighDir = 0; - private uint _i2cFreqKbps = 400; - - private const byte NumberCycles = 6; - private const byte MaskGpio = 0xF8; - - internal static class PinDirection - { - public const byte SDAinSCLin = 0x00; - public const byte SDAinSCLout = 0x01; - public const byte SDAoutSCLin = 0x02; - public const byte SDAoutSCLout = 0x03; - } - - internal static class PinData - { - public const byte SDAloSCLhi = 0x01; - public const byte SDAhiSCLhi = 0x03; - public const byte SDAloSCLlo = 0x00; - public const byte SDAhiSCLlo = 0x02; - } - - public uint I2cBusFrequencyKbps - { - get => _i2cFreqKbps; - set - { - _i2cFreqKbps |= value; - InitializeI2CClocks(); - } - } - - public void InitializeI2C() - { - // TODO: make sure we're not already set up for SPI - - CheckStatus( - FT_SetTimeouts(Handle, DefaultTimeoutMs, DefaultTimeoutMs)); - CheckStatus( - FT_SetLatencyTimer(Handle, DefaultLatencyTimer)); - CheckStatus( - FT_SetFlowControl(Handle, FT_FLOWCONTROL.FT_FLOW_RTS_CTS, 0x00, 0x00)); - CheckStatus( - FT_SetBitMode(Handle, 0x00, FT_BITMODE.FT_BITMODE_RESET)); - CheckStatus( - FT_SetBitMode(Handle, 0x00, FT_BITMODE.FT_BITMODE_MPSSE)); - - ClearInputBuffer(); - InitializeI2CClocks(); - InitializeMpsse(); - } - - private void InitializeI2CClocks() - { - // Now setup the clock and other elements - Span toSend = stackalloc byte[13]; - int idx = 0; - // Disable clock divide by 5 for 60Mhz master clock - toSend[idx++] = (byte)FT_OPCODE.DisableClockDivideBy5; - // Turn off adaptive clocking - toSend[idx++] = (byte)FT_OPCODE.TurnOffAdaptiveClocking; - // Enable 3 phase data clock, used by I2C to allow data on both clock edges - toSend[idx++] = (byte)FT_OPCODE.Enable3PhaseDataClocking; - // The SK clock frequency can be worked out by below algorithm with divide by 5 set as off - // TCK period = 60MHz / (( 1 + [ (0xValueH * 256) OR 0xValueL] ) * 2) - // Command to set clock divisor - toSend[idx++] = (byte)FT_OPCODE.SetClockDivisor; - uint clockDivisor = (60000 / (I2cBusFrequencyKbps * 2)) - 1; - toSend[idx++] = (byte)(clockDivisor & 0x00FF); - toSend[idx++] = (byte)((clockDivisor >> 8) & 0x00FF); - // loopback off - toSend[idx++] = (byte)FT_OPCODE.DisconnectTDItoTDOforLoopback; - // Enable the FT232H's drive-zero mode with the following enable mask - toSend[idx++] = (byte)FT_OPCODE.SetIOOnlyDriveOn0AndTristateOn1; - // Low byte (ADx) enables - bits 0, 1 and 2 - toSend[idx++] = 0x07; - // High byte (ACx) enables - all off - toSend[idx++] = 0x00; - // Command to set directions of lower 8 pins and force value on bits set as output - toSend[idx++] = (byte)FT_OPCODE.SetDataBitsLowByte; - if (DeviceType == FtDeviceType.Ft232H) - { - // SDA and SCL both output high(open drain) - GpioLowData = (byte)(PinData.SDAhiSCLhi | (GpioLowData & MaskGpio)); - GpioLowDir = (byte)(PinDirection.SDAoutSCLout | (GpioLowDir & MaskGpio)); - } - else - { - // SDA and SCL set low but as input to mimic open drain - GpioLowData = (byte)(PinData.SDAloSCLlo | (GpioLowData & MaskGpio)); - GpioLowDir = (byte)(PinDirection.SDAinSCLin | (GpioLowDir & MaskGpio)); - } - - toSend[idx++] = GpioLowData; - toSend[idx++] = GpioLowDir; - Write(toSend); - } - - internal void I2cStart() - { - int count; - int idx = 0; - // SDA high, SCL high - // The behavior is a bit different for FT232H and FT2232H/FT4232H - if (DeviceType == FtDeviceType.Ft232H) - { - GpioLowData = (byte)(PinData.SDAhiSCLhi | (GpioLowData & MaskGpio)); - GpioLowDir = (byte)(PinDirection.SDAoutSCLout | (GpioLowDir & MaskGpio)); - } - else - { - GpioLowData = (byte)(PinData.SDAloSCLlo | (GpioLowData & MaskGpio)); - GpioLowDir = (byte)(PinDirection.SDAinSCLin | (GpioLowDir & MaskGpio)); - } - - Span toSend = stackalloc byte[(NumberCycles * 3 * 3) + 3]; - for (count = 0; count < NumberCycles; count++) - { - toSend[idx++] = (byte)FT_OPCODE.SetDataBitsLowByte; - toSend[idx++] = GpioLowData; - toSend[idx++] = GpioLowDir; - } - - // SDA lo, SCL high - // The behavior is a bit different for FT232H and FT2232H/FT4232H - if (DeviceType == FtDeviceType.Ft232H) - { - GpioLowData = (byte)(PinData.SDAloSCLhi | (GpioLowData & MaskGpio)); - } - else - { - GpioLowDir = (byte)(PinDirection.SDAoutSCLin | (GpioLowDir & MaskGpio)); - } - - for (count = 0; count < NumberCycles; count++) - { - toSend[idx++] = (byte)FT_OPCODE.SetDataBitsLowByte; - toSend[idx++] = GpioLowData; - toSend[idx++] = GpioLowDir; - } - - // SDA lo, SCL lo - // The behavior is a bit different for FT232H and FT2232H/FT4232H - if (DeviceType == FtDeviceType.Ft232H) - { - GpioLowData = (byte)(PinData.SDAloSCLlo | (GpioLowData & MaskGpio)); - } - else - { - GpioLowDir = (byte)(PinDirection.SDAoutSCLout | (GpioLowDir & MaskGpio)); - } - - for (count = 0; count < NumberCycles; count++) - { - toSend[idx++] = (byte)FT_OPCODE.SetDataBitsLowByte; - toSend[idx++] = GpioLowData; - toSend[idx++] = GpioLowDir; - } - - // Release SDA - // The behavior is a bit different for FT232H and FT2232H/FT4232H - if (DeviceType == FtDeviceType.Ft232H) - { - GpioLowData = (byte)(PinData.SDAhiSCLlo | (GpioLowData & MaskGpio)); - } - else - { - GpioLowDir = (byte)(PinDirection.SDAinSCLout | (GpioLowDir & MaskGpio)); - } - - toSend[idx++] = (byte)FT_OPCODE.SetDataBitsLowByte; - toSend[idx++] = GpioLowData; - toSend[idx++] = GpioLowDir; - - Write(toSend); - } - - internal void I2cStop() - { - int count; - int idx = 0; - // SDA low, SCL low - GpioLowData = (byte)(PinData.SDAloSCLlo | (GpioLowData & MaskGpio)); - GpioLowDir = (byte)(PinDirection.SDAoutSCLout | (GpioLowDir & MaskGpio)); - - Span toSend = stackalloc byte[NumberCycles * 3 * 3]; - for (count = 0; count < NumberCycles; count++) - { - toSend[idx++] = (byte)FT_OPCODE.SetDataBitsLowByte; - toSend[idx++] = GpioLowData; - toSend[idx++] = GpioLowDir; - } - - // SDA low, SCL high - // The behavior is a bit different for FT232H and FT2232H/FT4232H - if (DeviceType == FtDeviceType.Ft232H) - { - GpioLowData = (byte)(PinData.SDAloSCLhi | (GpioLowData & MaskGpio)); - GpioLowDir = (byte)(PinDirection.SDAoutSCLout | (GpioLowDir & MaskGpio)); - } - else - { - GpioLowData = (byte)(PinData.SDAloSCLlo | (GpioLowData & MaskGpio)); - GpioLowDir = (byte)(PinDirection.SDAoutSCLin | (GpioLowDir & MaskGpio)); - } - - for (count = 0; count < NumberCycles; count++) - { - toSend[idx++] = (byte)FT_OPCODE.SetDataBitsLowByte; - toSend[idx++] = GpioLowData; - toSend[idx++] = GpioLowDir; - } - - // SDA high, SCL high - // The behavior is a bit different for FT232H and FT2232H/FT4232H - if (DeviceType == FtDeviceType.Ft232H) - { - GpioLowData = (byte)(PinData.SDAhiSCLhi | (GpioLowData & MaskGpio)); - GpioLowDir = (byte)(PinDirection.SDAoutSCLout | (GpioLowDir & MaskGpio)); - } - else - { - GpioLowData = (byte)(PinData.SDAloSCLlo | (GpioLowData & MaskGpio)); - GpioLowDir = (byte)(PinDirection.SDAinSCLin | (GpioLowDir & MaskGpio)); - } - - for (count = 0; count < NumberCycles; count++) - { - toSend[idx++] = (byte)FT_OPCODE.SetDataBitsLowByte; - toSend[idx++] = GpioLowData; - toSend[idx++] = GpioLowDir; - } - - Write(toSend); - } - - internal void I2cLineIdle() - { - int idx = 0; - // SDA low, SCL low - // The behavior is a bit different for FT232H and FT2232H/FT4232H - if (DeviceType == FtDeviceType.Ft232H) - { - GpioLowData = (byte)(PinData.SDAhiSCLhi | (GpioLowData & MaskGpio)); - GpioLowDir = (byte)(PinDirection.SDAoutSCLout | (GpioLowDir & MaskGpio)); - } - else - { - GpioLowData = (byte)(PinData.SDAloSCLlo | (GpioLowData & MaskGpio)); - GpioLowDir = (byte)(PinDirection.SDAinSCLin | (GpioLowDir & MaskGpio)); - } - - Span toSend = stackalloc byte[3]; - toSend[idx++] = (byte)FT_OPCODE.SetDataBitsLowByte; - toSend[idx++] = GpioLowData; - toSend[idx++] = GpioLowDir; - Write(toSend); - } - - internal bool I2cSendByteAndCheckACK(byte data) - { - int idx = 0; - Span toSend = stackalloc byte[DeviceType == FtDeviceType.Ft232H ? 10 : 13]; - Span toRead = stackalloc byte[1]; - // The behavior is a bit different for FT232H and FT2232H/FT4232H - if (DeviceType == FtDeviceType.Ft232H) - { - // Just clock with one byte (0 = 1 byte) - toSend[idx++] = (byte)FT_OPCODE.ClockDataBytesOutOnMinusVeClockMSBFirst; - toSend[idx++] = 0; - toSend[idx++] = 0; - toSend[idx++] = data; - // Put line back to idle (data released, clock pulled low) - GpioLowData = (byte)(PinData.SDAhiSCLlo | (GpioLowData & MaskGpio)); - GpioLowDir = (byte)(PinDirection.SDAoutSCLout | (GpioLowDir & MaskGpio)); - toSend[idx++] = (byte)FT_OPCODE.SetDataBitsLowByte; - toSend[idx++] = GpioLowData; - toSend[idx++] = GpioLowDir; - // Clock in (0 = 1 byte) - toSend[idx++] = (byte)FT_OPCODE.ClockDataBitsInOnPlusVeClockMSBFirst; - toSend[idx++] = 0; - } - else - { - // Set directions and clock data - GpioLowData = (byte)(PinData.SDAloSCLlo | (GpioLowData & MaskGpio)); - GpioLowDir = (byte)(PinDirection.SDAoutSCLout | (GpioLowDir & MaskGpio)); - toSend[idx++] = (byte)FT_OPCODE.SetDataBitsLowByte; - toSend[idx++] = GpioLowData; - toSend[idx++] = GpioLowDir; - // Just clock with one byte (0 = 1 byte) - toSend[idx++] = (byte)FT_OPCODE.ClockDataBytesOutOnMinusVeClockMSBFirst; - toSend[idx++] = 0; - toSend[idx++] = 0; - toSend[idx++] = data; - // Put line back to idle (data released, clock pulled low) - // Set directions and clock data - GpioLowData = (byte)(PinData.SDAloSCLlo | (GpioLowData & MaskGpio)); - GpioLowDir = (byte)(PinDirection.SDAinSCLout | (GpioLowDir & MaskGpio)); - toSend[idx++] = (byte)FT_OPCODE.SetDataBitsLowByte; - toSend[idx++] = GpioLowData; - toSend[idx++] = GpioLowDir; - // Clock in (0 = 1 byte) - toSend[idx++] = (byte)FT_OPCODE.ClockDataBitsInOnPlusVeClockMSBFirst; - toSend[idx++] = 0; - } - - // And ask it right away - toSend[idx++] = (byte)FT_OPCODE.SendImmediate; - Write(toSend); - ReadInto(toRead); - // Bit 0 equivalent to acknowledge, otherwise nack - return (toRead[0] & 0x01) == 0; - } - - internal bool I2cSendDeviceAddrAndCheckACK(byte Address, bool Read) - { - // Set address for read or write - Address <<= 1; - if (Read == true) - { - Address |= 0x01; - } - - return I2cSendByteAndCheckACK(Address); - } - - internal byte I2CReadByte(bool ack) - { - int idx = 0; - Span toSend = stackalloc byte[DeviceType == FtDeviceType.Ft232H ? 10 : 16]; - Span toRead = stackalloc byte[1]; - // The behavior is a bit different for FT232H and FT2232H/FT4232H - if (DeviceType == FtDeviceType.Ft232H) - { - // Read one byte - toSend[idx++] = (byte)FT_OPCODE.ClockDataBytesInOnPlusVeClockMSBFirst; - toSend[idx++] = 0; - toSend[idx++] = 0; - // Send out either ack either nak - toSend[idx++] = (byte)FT_OPCODE.ClockDataBitsOutOnMinusVeClockMSBFirst; - toSend[idx++] = 0; - toSend[idx++] = (byte)(ack ? 0x00 : 0xFF); - // I2C lines back to idle state - toSend[idx++] = (byte)FT_OPCODE.SetDataBitsLowByte; - GpioLowData = (byte)(PinData.SDAhiSCLlo | (GpioLowData & MaskGpio)); - GpioLowDir = (byte)(PinDirection.SDAoutSCLout | (GpioLowDir & MaskGpio)); - toSend[idx++] = GpioLowData; - toSend[idx++] = GpioLowDir; - } - else - { - // Make sure no open gain - GpioLowData = (byte)(PinData.SDAloSCLlo | (GpioLowData & MaskGpio)); - GpioLowDir = (byte)(PinDirection.SDAinSCLout | (GpioLowDir & MaskGpio)); - toSend[idx++] = (byte)FT_OPCODE.SetDataBitsLowByte; - toSend[idx++] = GpioLowData; - toSend[idx++] = GpioLowDir; - // Read one byte - toSend[idx++] = (byte)FT_OPCODE.ClockDataBytesInOnPlusVeClockMSBFirst; - toSend[idx++] = 0; - toSend[idx++] = 0; - // Change direction - GpioLowData = (byte)(PinData.SDAloSCLlo | (GpioLowData & MaskGpio)); - GpioLowDir = (byte)(PinDirection.SDAoutSCLout | (GpioLowDir & MaskGpio)); - toSend[idx++] = (byte)FT_OPCODE.SetDataBitsLowByte; - toSend[idx++] = GpioLowData; - toSend[idx++] = GpioLowDir; - // Send out either ack either nak - toSend[idx++] = (byte)FT_OPCODE.ClockDataBitsOutOnMinusVeClockMSBFirst; - toSend[idx++] = 0; - toSend[idx++] = (byte)(ack ? 0x00 : 0xFF); - // I2C lines back to idle state - toSend[idx++] = (byte)FT_OPCODE.SetDataBitsLowByte; - GpioLowData = (byte)(PinData.SDAhiSCLlo | (GpioLowData & MaskGpio)); - GpioLowDir = (byte)(PinDirection.SDAinSCLout | (GpioLowDir & MaskGpio)); - toSend[idx++] = GpioLowData; - toSend[idx++] = GpioLowDir; - } - - // And ask it right away - toSend[idx++] = (byte)FT_OPCODE.SendImmediate; - Write(toSend); - ReadInto(toRead); - return toRead[0]; - } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.Spi.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.Spi.cs deleted file mode 100644 index dfb132cd94..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.Spi.cs +++ /dev/null @@ -1,210 +0,0 @@ -using Meadow.Hardware; -using System; -using System.Threading; -using static Meadow.Foundation.ICs.IOExpanders.Native; -using static Meadow.Foundation.ICs.IOExpanders.Native.Ftd2xx; - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class MpsseChannel -{ - private SpiClockConfiguration? _spiConfig; - - internal void InitializeSpi(SpiClockConfiguration config) - { - if (_spiConfig != null) return; - - // TODO: make sure we're not already initialized for I2C - - _spiConfig = config; - - if (Handle == IntPtr.Zero) Open(); - - CheckStatus( - FT_SetLatencyTimer(Handle, 1)); - CheckStatus( - FT_SetUSBParameters(Handle, 65535, 65535)); - CheckStatus( - FT_SetChars(Handle, 0, 0, 0, 0)); - CheckStatus( - FT_SetTimeouts(Handle, 3000, 3000)); - CheckStatus( - FT_SetLatencyTimer(Handle, 1)); - // Reset - CheckStatus( - FT_SetBitMode(Handle, 0x00, FT_BITMODE.FT_BITMODE_RESET)); - // Enable MPSSE mode - CheckStatus( - FT_SetBitMode(Handle, 0x00, FT_BITMODE.FT_BITMODE_MPSSE)); - - // 50 ms according to thr doc for all USB to complete - Thread.Sleep(50); - ClearInputBuffer(); - InitializeMpsse(); - - int idx = 0; - Span toSend = stackalloc byte[10]; - toSend[idx++] = (byte)FT_OPCODE.DisableClockDivideBy5; - toSend[idx++] = (byte)FT_OPCODE.TurnOffAdaptiveClocking; - toSend[idx++] = (byte)FT_OPCODE.Disable3PhaseDataClocking; - toSend[idx++] = (byte)FT_OPCODE.SetDataBitsLowByte; - // Pin clock output, MISO output, MOSI input - GpioLowDir = (byte)((GpioLowDir & MaskGpio) | 0x03); - // clock, MOSI and MISO to 0 - GpioLowData = (byte)(GpioLowData & MaskGpio); - toSend[idx++] = GpioLowDir; - toSend[idx++] = GpioLowData; - // The SK clock frequency can be worked out by below algorithm with divide by 5 set as off - // TCK period = 60MHz / (( 1 + [ (0xValueH * 256) OR 0xValueL] ) * 2) - // Command to set clock divisor - toSend[idx++] = (byte)FT_OPCODE.SetClockDivisor; - uint clockDivisor = (uint)((60000 / (_spiConfig.Speed.Hertz / 1000 * 2)) - 1); - toSend[idx++] = (byte)(clockDivisor & 0xFF); - toSend[idx++] = (byte)(clockDivisor >> 8); - // loopback off - toSend[idx++] = (byte)FT_OPCODE.DisconnectTDItoTDOforLoopback; - Write(toSend); - // Delay as in the documentation - Thread.Sleep(30); - } - - internal void SpiWrite(IDigitalOutputPort? chipSelect, ReadOnlySpan writeBuffer, ChipSelectMode csMode) - { - if (_spiConfig == null) - { - throw new Exception("SPI not configured"); - } - - if (writeBuffer.Length > 65535) - { - throw new ArgumentException("Buffer too large, maximum size if 65535"); - } - - byte clock; - switch (_spiConfig.SpiMode) - { - default: - case SpiClockConfiguration.Mode.Mode3: - case SpiClockConfiguration.Mode.Mode0: - clock = (byte)FT_OPCODE.ClockDataBytesOutOnMinusVeClockMSBFirst; - break; - case SpiClockConfiguration.Mode.Mode2: - case SpiClockConfiguration.Mode.Mode1: - clock = (byte)FT_OPCODE.ClockDataBytesOutOnPlusVeClockMSBFirst; - break; - } - - if (chipSelect != null) - { - // assert - chipSelect.State = csMode == ChipSelectMode.ActiveHigh ? true : false; - } - - int idx = 0; - Span toSend = stackalloc byte[3 + writeBuffer.Length]; - toSend[idx++] = clock; - toSend[idx++] = (byte)((writeBuffer.Length - 1) & 0xFF); - toSend[idx++] = (byte)((writeBuffer.Length - 1) >> 8); - writeBuffer.CopyTo(toSend.Slice(3)); - Write(toSend); - if (chipSelect != null) - { - // deassert - chipSelect.State = csMode == ChipSelectMode.ActiveHigh ? false : true; - } - } - - internal void SpiRead(IDigitalOutputPort? chipSelect, Span readBuffer, ChipSelectMode csMode) - { - if (_spiConfig == null) - { - throw new Exception("SPI not configured"); - } - - if (readBuffer.Length > 65535) - { - throw new ArgumentException("Buffer too large, maximum size if 65535"); - } - - byte clock; - switch (_spiConfig.SpiMode) - { - default: - case SpiClockConfiguration.Mode.Mode3: - case SpiClockConfiguration.Mode.Mode0: - clock = (byte)FT_OPCODE.ClockDataBytesInOnPlusVeClockMSBFirst; - break; - case SpiClockConfiguration.Mode.Mode2: - case SpiClockConfiguration.Mode.Mode1: - clock = (byte)FT_OPCODE.ClockDataBytesInOnMinusVeClockMSBFirst; - break; - } - - if (chipSelect != null) - { - // assert - chipSelect.State = csMode == ChipSelectMode.ActiveHigh ? true : false; - } - - int idx = 0; - Span toSend = stackalloc byte[3]; - toSend[idx++] = clock; - toSend[idx++] = (byte)((readBuffer.Length - 1) & 0xFF); - toSend[idx++] = (byte)((readBuffer.Length - 1) >> 8); - Write(toSend); - ReadInto(readBuffer); - - if (chipSelect != null) - { - // deassert - chipSelect.State = csMode == ChipSelectMode.ActiveHigh ? false : true; - } - } - - internal void SpiExchange(IDigitalOutputPort? chipSelect, ReadOnlySpan writeBuffer, Span readBuffer, ChipSelectMode csMode) - { - if (_spiConfig == null) - { - throw new Exception("SPI not configured"); - } - - if ((readBuffer.Length > 65535) || (writeBuffer.Length > 65535)) - { - throw new ArgumentException("Buffer too large, maximum size if 65535"); - } - - byte clock; - switch (_spiConfig.SpiMode) - { - default: - case SpiClockConfiguration.Mode.Mode3: - case SpiClockConfiguration.Mode.Mode0: - clock = (byte)FT_OPCODE.ClockDataBytesOutOnMinusBytesInOnPlusVeClockMSBFirst; - break; - case SpiClockConfiguration.Mode.Mode2: - case SpiClockConfiguration.Mode.Mode1: - clock = (byte)FT_OPCODE.ClockDataBytesOutOnPlusBytesInOnMinusVeClockMSBFirst; - break; - } - - if (chipSelect != null) - { - // assert - chipSelect.State = csMode == ChipSelectMode.ActiveHigh ? true : false; - } - - int idx = 0; - Span toSend = stackalloc byte[3 + writeBuffer.Length]; - toSend[idx++] = clock; - toSend[idx++] = (byte)((writeBuffer.Length - 1) & 0xFF); - toSend[idx++] = (byte)((writeBuffer.Length - 1) >> 8); - writeBuffer.CopyTo(toSend.Slice(3)); - Write(toSend); - ReadInto(readBuffer); - if (chipSelect != null) - { - // deassert - chipSelect.State = csMode == ChipSelectMode.ActiveHigh ? false : true; - } - } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.cs deleted file mode 100644 index 1e99ad31cd..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannel.cs +++ /dev/null @@ -1,138 +0,0 @@ -using System; -using System.IO; -using System.Runtime.InteropServices; -using static Meadow.Foundation.ICs.IOExpanders.Native; -using static Meadow.Foundation.ICs.IOExpanders.Native.Ftd2xx; - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal partial class MpsseChannel -{ - private const int DefaultTimeoutMs = 5000; - private const int DefaultLatencyTimer = 16; - - internal uint Index { get; } - internal uint Flags { get; } - internal FtDeviceType DeviceType { get; } - internal uint ID { get; } - internal uint LocID { get; } - internal string SerialNumber { get; } - internal string Description { get; } - internal IntPtr Handle { get; private set; } - - internal MpsseChannel( - uint index, - uint flags, - FtDeviceType deviceType, - uint id, - uint locid, - string serialNumber, - string description, - IntPtr handle - ) - { - Index = index; - Flags = flags; - DeviceType = deviceType; - ID = id; - LocID = locid; - SerialNumber = serialNumber; - Description = description; - Handle = handle; - } - - public void Open() - { - if (Handle == IntPtr.Zero) - { - Native.CheckStatus( - FT_OpenEx(LocID, Native.FT_OPEN_TYPE.FT_OPEN_BY_LOCATION, out IntPtr handle) - ); - Handle = handle; - } - } - - private void InitializeMpsse() - { - Span writeBuffer = stackalloc byte[1]; - writeBuffer[0] = 0xAA; - Write(writeBuffer); - Span readBuffer = stackalloc byte[2]; - ReadInto(readBuffer); - if (!((readBuffer[0] == 0xFA) && (readBuffer[1] == 0xAA))) - { - throw new IOException($"Failed to setup device {Description} in MPSSE mode using magic 0xAA sync"); - } - - // Second with 0xAB - writeBuffer[0] = 0xAB; - Write(writeBuffer); - ReadInto(readBuffer); - if (!((readBuffer[0] == 0xFA) && (readBuffer[1] == 0xAB))) - { - throw new IOException($"Failed to setup device {Description}, status in MPSSE mode using magic 0xAB sync"); - } - } - - public int ReadInto(Span buffer) - { - var totalRead = 0; - uint read = 0; - - while (totalRead < buffer.Length) - { - var available = GetAvailableBytes(); - if (available > 0) - { - CheckStatus( - FT_Read(Handle, in buffer[totalRead], available, ref read)); - - totalRead += (int)read; - } - } - - return totalRead; - } - - public void Write(ReadOnlySpan data) - { - uint written = 0; - - CheckStatus( - FT_Write(Handle, in MemoryMarshal.GetReference(data), (ushort)data.Length, ref written)); - } - - private void ClearInputBuffer() - { - var available = GetAvailableBytes(); - - if (available > 0) - { - var toRead = new byte[available]; - uint bytesRead = 0; - CheckStatus( - FT_Read(Handle, in toRead[0], available, ref bytesRead)); - } - } - - private uint GetAvailableBytes() - { - uint availableBytes = 0; - - CheckStatus( - FT_GetQueueStatus(Handle, ref availableBytes)); - - return availableBytes; - } - - public void Close() - { - if (Handle != IntPtr.Zero) - { - CheckStatus( - FT_Close(Handle)); - - Handle = IntPtr.Zero; - } - } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannelCollection.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannelCollection.cs deleted file mode 100644 index d7ca87aa29..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseChannelCollection.cs +++ /dev/null @@ -1,79 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Runtime.InteropServices; -using System.Text; - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal class MpsseChannelCollection : IEnumerable -{ - private List _devices = new(); - - public int Count => _devices.Count; - - public MpsseChannel this[int index] - { - get => _devices[index]; - } - - public void Refresh() - { - _devices.Clear(); - - uint count; - - try - { - Native.CheckStatus(Native.Mpsse.FT_CreateDeviceInfoList(out count)); - } - catch (DllNotFoundException) - { - throw new DriverNotInstalledException(); - } - - ReadOnlySpan serialNumberBuffer = stackalloc byte[16]; - ReadOnlySpan descriptionBuffer = stackalloc byte[64]; - - for (uint index = 0; index < count; index++) - { - Native.CheckStatus(FT_GetDeviceInfoDetail( - index, - out uint flags, - out FtDeviceType deviceType, - out uint id, - out uint locid, - in MemoryMarshal.GetReference(serialNumberBuffer), - in MemoryMarshal.GetReference(descriptionBuffer), - out IntPtr handle)); - - switch (deviceType) - { - case FtDeviceType.Ft232H: - case FtDeviceType.Ft2232: - case FtDeviceType.Ft2232H: - case FtDeviceType.Ft4232H: - // valid, add to list - break; - default: - continue; - } - - // no idea why the buffer isn't all zeros after the null terminator - thanks FTDI! - var serialNumber = Encoding.ASCII.GetString(serialNumberBuffer.ToArray(), 0, serialNumberBuffer.IndexOf((byte)0)); - var description = Encoding.ASCII.GetString(descriptionBuffer.ToArray(), 0, descriptionBuffer.IndexOf((byte)0)); - - _devices.Add(new MpsseChannel(index, flags, deviceType, id, locid, serialNumber, description, handle)); - } - } - - public IEnumerator GetEnumerator() - { - return _devices.GetEnumerator(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseDigitalInputPort.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseDigitalInputPort.cs deleted file mode 100644 index 312af6ae0b..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseDigitalInputPort.cs +++ /dev/null @@ -1,70 +0,0 @@ -using Meadow.Hardware; -using System; - -namespace Meadow.Foundation.ICs.IOExpanders -{ - /// - /// Represents a digital input port implementation for the FT232 bus. - /// - public sealed class MpsseDigitalInputPort : DigitalInterruptPortBase - { - private readonly IFt232Bus _bus; - - /// - /// Instantiates a . - /// - /// The pin connected to the input port. - /// The digital channel info associated with the pin. - /// The FT232 bus instance. - internal MpsseDigitalInputPort(IPin pin, IDigitalChannelInfo info, IFt232Bus bus) - : base(pin, info) - { - _bus = bus; - } - - /// - /// Gets the current state of the input port. - /// - /// The current state of the input port. - public override bool State - { - get - { - // reads all 8 pis at once - Native.Mpsse.FT_ReadGPIO(_bus.Handle, out byte state); - // the pin key is the mask - return (state & (byte)Pin.Key) != 0; - } - } - - /// - /// Gets or sets the resistor mode of the input port. - /// - /// The FT232 does not support internal resistors. - public override ResistorMode Resistor - { - get => ResistorMode.Disabled; - set => throw new NotSupportedException("The FT232 does not support internal resistors"); - } - - /// - /// Gets or sets the debounce duration of the input port. - /// - /// The FT232 does not support debounce. - public override TimeSpan DebounceDuration - { - get => TimeSpan.Zero; - set => throw new NotSupportedException("The FT232 does not support debounce"); - } - - /// - /// Gets or sets the glitch duration of the input port. - /// - /// The FT232 does not support glitch filtering. - public override TimeSpan GlitchDuration - { - get => TimeSpan.Zero; - set => throw new NotSupportedException("The FT232 does not support glitch filtering"); - } - } -} \ No newline at end of file diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseDigitalOutputPort.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseDigitalOutputPort.cs deleted file mode 100644 index 0ef7b5ec56..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseDigitalOutputPort.cs +++ /dev/null @@ -1,64 +0,0 @@ -using Meadow.Hardware; -using System; - -namespace Meadow.Foundation.ICs.IOExpanders -{ - /// - /// Digital output port for FT232 devices. - /// - public sealed class MpsseDigitalOutputPort : DigitalOutputPortBase - { - private readonly IFt232Bus _bus; - private bool _state; - - /// - /// Initializes a new instance of the class. - /// - /// The pin to use. - /// The digital channel info. - /// The initial state of the output port. - /// The initial output type. - /// The FT232 bus. - internal MpsseDigitalOutputPort(IPin pin, IDigitalChannelInfo info, bool initialState, OutputType initialOutputType, IFt232Bus bus) - : base(pin, info, initialState, initialOutputType) - { - if (initialOutputType != OutputType.PushPull) - { - throw new NotSupportedException("The FT232 only supports push-pull outputs"); - } - - _bus = bus; - State = initialState; - } - - /// - /// Gets or sets the state of the digital output port. - /// - /// - /// The state of the digital output port. - /// - public override bool State - { - get => _state; - set - { - byte s = _bus.GpioState; - - if (value) - { - s |= (byte)Pin.Key; - } - else - { - s &= (byte)~(byte)Pin.Key; - } - - var result = Native.Mpsse.FT_WriteGPIO(_bus.Handle, _bus.GpioDirectionMask, s); - Native.CheckStatus(result); - - _bus.GpioState = s; - _state = value; - } - } - } -} \ No newline at end of file diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseI2cBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseI2cBus.cs deleted file mode 100644 index 1e7b13911c..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseI2cBus.cs +++ /dev/null @@ -1,137 +0,0 @@ -using Meadow.Hardware; -using System; -using System.Diagnostics; -using System.Runtime.InteropServices; -using static Meadow.Foundation.ICs.IOExpanders.Native; - -namespace Meadow.Foundation.ICs.IOExpanders; - -/// -/// Represents an I2C bus implementation using the MPSSE mode of the FT232 device. -/// -public sealed class MpsseI2cBus : IFt232Bus, II2cBus, IDisposable -{ - private const byte DefaultLatencyTimer = 10; - private const I2CChannelOptions DefaultChannelOptions = I2CChannelOptions.None; - - private bool _isDisposed; - - /// - public IntPtr Handle { get; private set; } - - /// - public byte GpioDirectionMask { get; set; } - - /// - public byte GpioState { get; set; } - internal bool IsOpen { get; private set; } = false; - internal int ChannelNumber { get; } - private FT_DEVICE_LIST_INFO_NODE InfoNode { get; } - - internal MpsseI2cBus(int channelNumber, FT_DEVICE_LIST_INFO_NODE info) - { - ChannelNumber = channelNumber; - InfoNode = info; - } - - /// - public I2cBusSpeed BusSpeed { get; set; } - - private void Dispose(bool disposing) - { - if (!_isDisposed) - { - CloseChannel(); - - _isDisposed = true; - } - } - - /// - /// Finalizer for the MpsseI2cBus class, used to release unmanaged resources. - /// - ~MpsseI2cBus() - { - // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method - Dispose(false); - } - - /// - public void Dispose() - { - // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method - Dispose(true); - GC.SuppressFinalize(this); - } - - internal void Open(I2CClockRate clockRate = I2CClockRate.Standard) - { - if (CheckStatus(Mpsse.I2C_OpenChannel(ChannelNumber, out IntPtr handle))) - { - Handle = handle; - - var config = new I2CChannelConfig - { - ClockRate = clockRate, - LatencyTimer = DefaultLatencyTimer, - Options = DefaultChannelOptions - }; - - CheckStatus(Mpsse.I2C_InitChannel(Handle, ref config)); - - IsOpen = true; - } - } - - private void CloseChannel() - { - if (Handle != IntPtr.Zero) - { - CheckStatus(Mpsse.I2C_CloseChannel(Handle)); - Handle = IntPtr.Zero; - } - } - - /// - public void Exchange(byte peripheralAddress, Span writeBuffer, Span readBuffer) - { - Write(peripheralAddress, writeBuffer); - Read(peripheralAddress, readBuffer); - } - - /// - public void Read(byte peripheralAddress, Span readBuffer) - { - var status = Mpsse.I2C_DeviceRead( - Handle, - peripheralAddress, - readBuffer.Length, - MemoryMarshal.GetReference(readBuffer), - out int transferred, - I2CTransferOptions.FAST_TRANSFER | I2CTransferOptions.FAST_TRANSFER_BYTES - //I2CTransferOptions.START_BIT | I2CTransferOptions.STOP_BIT | I2CTransferOptions.NACK_LAST_BYTE - // I2CTransferOptions.START_BIT | I2CTransferOptions.STOP_BIT | I2CTransferOptions.FAST_TRANSFER | I2CTransferOptions.NACK_LAST_BYTE - ); - - Debug.WriteLine($"transferred: {transferred}"); - CheckStatus(status); - } - - /// - public void Write(byte peripheralAddress, Span writeBuffer) - { - var status = Mpsse.I2C_DeviceWrite( - Handle, - peripheralAddress, - writeBuffer.Length, - MemoryMarshal.GetReference(writeBuffer), - out int transferred, - I2CTransferOptions.FAST_TRANSFER | I2CTransferOptions.FAST_TRANSFER_BYTES - //I2CTransferOptions.START_BIT | I2CTransferOptions.BREAK_ON_NACK - //I2CTransferOptions.START_BIT | I2CTransferOptions.STOP_BIT | I2CTransferOptions.NACK_LAST_BYTE - ); - - Debug.WriteLine($"transferred: {transferred}"); - // CheckStatus(status); - } -} \ No newline at end of file diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseImpl.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseImpl.cs deleted file mode 100644 index c718b7256e..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseImpl.cs +++ /dev/null @@ -1,197 +0,0 @@ -using Meadow.Hardware; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; - -namespace Meadow.Foundation.ICs.IOExpanders; - -internal class MpsseImpl : IFtdiImpl -{ - private static int _instanceCount = 0; - private bool _isDisposed = false; - private Dictionary _i2cBuses = new Dictionary(); - private Dictionary _spiBuses = new Dictionary(); - private bool _spiBusAutoCreated = false; - - private IFt232Bus? _activeBus = null; - - public void Initialize() - { - if (Interlocked.Increment(ref _instanceCount) == 1) - { - // only do this one time (no matter how many instances are created instances) - Native.Mpsse.Init_libMPSSE(); - } - - EnumerateBuses(); - } - - private void EnumerateBuses() - { - _i2cBuses = GetI2CBuses(); - _spiBuses = GetSpiBuses(); - } - - private Dictionary GetI2CBuses() - { - Dictionary result = new Dictionary(); - - if (Native.CheckStatus(Native.Mpsse.I2C_GetNumChannels(out int channels))) - { - if (channels > 0) - { - for (var c = 0; c < channels; c++) - { - if (Native.CheckStatus(Native.Mpsse.I2C_GetChannelInfo(c, out Native.FT_DEVICE_LIST_INFO_NODE info))) - { - result.Add(c, new MpsseI2cBus(c, info)); - } - } - } - } - - return result; - } - - private Dictionary GetSpiBuses() - { - Dictionary result = new Dictionary(); - - if (Native.CheckStatus(Native.Mpsse.SPI_GetNumChannels(out int channels))) - { - if (channels > 0) - { - for (var c = 0; c < channels; c++) - { - if (Native.CheckStatus(Native.Mpsse.SPI_GetChannelInfo(c, out Native.FT_DEVICE_LIST_INFO_NODE info))) - { - result.Add(c, new MpsseSpiBus(c, info)); - } - } - } - } - - return result; - } - - public II2cBus CreateI2cBus(int busNumber, I2CClockRate clock) - { - // dev note: this fails on WIndows in all my testing - // it's a bug in the MPSSE DLL delivered by FTDI - // even using their C example, compiling it myself, it fails - - if (_activeBus != null) - { - throw new InvalidOperationException("The FT232 allows only one bus to be active at a time."); - } - - if (_i2cBuses.Count == 0) - { - throw new InvalidOperationException("No I2C Busses found! Is the FT232 properly connected?"); - } - - if (!_i2cBuses.ContainsKey(busNumber)) throw new ArgumentOutOfRangeException(nameof(busNumber)); - - var bus = _i2cBuses[busNumber]; - if (!bus.IsOpen) - { - bus.Open(clock); - } - - _activeBus = bus; - - return bus; - } - - public ISpiBus CreateSpiBus(int busNumber, SpiClockConfiguration config) - { - if (_activeBus != null) - { - throw new InvalidOperationException("The FT232 allows only one bus to be active at a time."); - } - - if (_spiBuses.Count == 0) - { - throw new InvalidOperationException("No SPI Busses found! Is the FT232 properly connected?"); - } - - if (!_spiBuses.ContainsKey(busNumber)) throw new ArgumentOutOfRangeException(nameof(busNumber)); - - var bus = _spiBuses[busNumber]; - if (!bus.IsOpen) - { - bus.Open(config); - } - - _activeBus = bus; - - return bus; - } - - public IDigitalInputPort CreateDigitalInputPort(int channel, IPin pin, ResistorMode resistorMode) - { - // MPSSE requires a bus, it can be either I2C or SPI, but that bus must be created before you can use GPIO - // if no bus is yet open, we'll default to a SPI bus. - // If this is created before an I2C comms bus, we need to let the caller know to create the comms bus first - - if (_activeBus == null) - { - var bus = CreateSpiBus(channel, Ft232h.DefaultClockConfiguration); - _spiBusAutoCreated = true; - _activeBus = bus as IFt232Bus; - } - - // TODO: do we need to set the direction make (see outpuuts) or are they defaulted to input? - - var info = pin.SupportedChannels?.FirstOrDefault(c => c is IDigitalChannelInfo) as IDigitalChannelInfo; - return new MpsseDigitalInputPort(pin, info!, _activeBus!); - } - - public IDigitalOutputPort CreateDigitalOutputPort(int channel, IPin pin, bool initialState = false, OutputType initialOutputType = OutputType.PushPull) - { - // MPSSE requires a bus, it can be either I2C or SPI, but that bus must be created before you can use GPIO - // if no bus is yet open, we'll default to a SPI bus. - // If this is created before an I2C comms bus, we need to let the caller know to create the comms bus first - - if (_activeBus == null) - { - var bus = CreateSpiBus(channel, Ft232h.DefaultClockConfiguration); - _spiBusAutoCreated = true; - _activeBus = bus as IFt232Bus; - } - - // update the global mask to make this an output - _activeBus!.GpioDirectionMask |= (byte)pin.Key; - - // update the direction - Native.Mpsse.FT_WriteGPIO(_activeBus.Handle, _activeBus.GpioDirectionMask, 0); - - var info = pin.SupportedChannels?.FirstOrDefault(c => c is IDigitalChannelInfo) as IDigitalChannelInfo; - return new MpsseDigitalOutputPort(pin, info!, initialState, initialOutputType, _activeBus); - } - - public void Dispose() - { - if (!_isDisposed) - { - foreach (var bus in _spiBuses) - { - bus.Value?.Dispose(); - } - - if (Interlocked.Decrement(ref _instanceCount) == 0) - { - if (_spiBusAutoCreated) - { - // TODO: - } - - // last instance was disposed, clean house - Native.Mpsse.Cleanup_libMPSSE(); - } - - _isDisposed = true; - } - } -} diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseSpiBus.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseSpiBus.cs deleted file mode 100644 index 1ef2801383..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/MpsseSpiBus.cs +++ /dev/null @@ -1,277 +0,0 @@ -using Meadow.Hardware; -using Meadow.Units; -using System; -using System.Runtime.InteropServices; -using static Meadow.Foundation.ICs.IOExpanders.Ft232h; -using static Meadow.Foundation.ICs.IOExpanders.Native; - -namespace Meadow.Foundation.ICs.IOExpanders; - -/// -/// Represents an SPI bus implementation using the FT232 device. -/// -public sealed class MpsseSpiBus : IFt232Bus, ISpiBus, IDisposable -{ - /// - /// The default SPI clock rate for the FT232H - /// - public const uint DefaultClockRate = 25000000; - private const byte DefaultLatencyTimer = 10; // from the FTDI sample - - private bool _isDisposed; - - private SpiClockConfiguration _config = default!; - private SpiChannelConfig _channelConfig; - - /// - public IntPtr Handle { get; private set; } - /// - public byte GpioDirectionMask { get; set; } - /// - public byte GpioState { get; set; } - internal bool IsOpen { get; private set; } = false; - internal int ChannelNumber { get; } - private FT_DEVICE_LIST_INFO_NODE InfoNode { get; } - - /// - public Frequency[] SupportedSpeeds => new Frequency[] { new Frequency(30d, Frequency.UnitType.Megahertz) }; - - internal MpsseSpiBus(int channelNumber, FT_DEVICE_LIST_INFO_NODE info) - { - ChannelNumber = channelNumber; - InfoNode = info; - } - - private void Dispose(bool disposing) - { - if (!_isDisposed) - { - CloseChannel(); - - _isDisposed = true; - } - } - - /// - /// Finalizer for the Ft232SpiBus class, used to release unmanaged resources. - /// - ~MpsseSpiBus() - { - // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method - Dispose(false); - } - - /// - public SpiClockConfiguration Configuration - { - get { return _config; } - set - { - _channelConfig = CreateChannelConfig(value); - _config = value; - this.Configuration.Changed += OnConfigurationChanged; - } - } - - private void OnConfigurationChanged(object sender, EventArgs e) - { - var changed = false; - - if (Configuration.Speed.Hertz != _channelConfig.ClockRate) - { - _channelConfig.ClockRate = (uint)Configuration.Speed.Hertz; - changed = true; - } - - switch (Configuration.SpiMode) - { - case SpiClockConfiguration.Mode.Mode0: - if ((_channelConfig.Options & SpiConfigOptions.MODE3) != SpiConfigOptions.MODE0) - { - _channelConfig.Options |= SpiConfigOptions.MODE0; - changed = true; - } - break; - case SpiClockConfiguration.Mode.Mode1: - if ((_channelConfig.Options & SpiConfigOptions.MODE3) != SpiConfigOptions.MODE1) - { - _channelConfig.Options = (_channelConfig.Options & ~SpiConfigOptions.MODE3) | SpiConfigOptions.MODE1; - changed = true; - } - break; - case SpiClockConfiguration.Mode.Mode2: - if ((_channelConfig.Options & SpiConfigOptions.MODE3) != SpiConfigOptions.MODE2) - { - _channelConfig.Options = (_channelConfig.Options & ~SpiConfigOptions.MODE3) | SpiConfigOptions.MODE2; - changed = true; - } - break; - case SpiClockConfiguration.Mode.Mode3: - if ((_channelConfig.Options & SpiConfigOptions.MODE3) != SpiConfigOptions.MODE3) - { - _channelConfig.Options = (_channelConfig.Options & ~SpiConfigOptions.MODE3) | SpiConfigOptions.MODE3; - changed = true; - } - break; - } - - if (changed) - { - CheckStatus(Mpsse.SPI_InitChannel(Handle, ref _channelConfig)); - } - } - - /// - public void Dispose() - { - // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method - Dispose(true); - GC.SuppressFinalize(this); - } - - internal void Open(SpiClockConfiguration config) - { - Configuration = config; - - if (CheckStatus(Mpsse.SPI_OpenChannel(ChannelNumber, out IntPtr handle))) - { - Handle = handle; - - CheckStatus(Mpsse.SPI_InitChannel(Handle, ref _channelConfig)); - } - } - - private void CloseChannel() - { - if (Handle != IntPtr.Zero) - { - CheckStatus(Mpsse.SPI_CloseChannel(Handle)); - Handle = IntPtr.Zero; - } - } - - private SpiChannelConfig CreateChannelConfig(SpiClockConfiguration config) - { - // for now we support CS on D3 and that's it - Ft232h.SpiConfigOptions opts = SpiConfigOptions.CS_ACTIVELOW | SpiConfigOptions.CS_DBUS3; - - switch (config.SpiMode) - { - case SpiClockConfiguration.Mode.Mode0: - opts = SpiConfigOptions.MODE0; - break; - case SpiClockConfiguration.Mode.Mode1: - opts = SpiConfigOptions.MODE1; - break; - case SpiClockConfiguration.Mode.Mode2: - opts = SpiConfigOptions.MODE2; - break; - case SpiClockConfiguration.Mode.Mode3: - opts = SpiConfigOptions.MODE3; - break; - } - - return new SpiChannelConfig - { - ClockRate = (uint)config.Speed.Hertz, - LatencyTimer = DefaultLatencyTimer, - Options = opts - }; - } - - private SPITransferOptions CreateTransferOptions(ChipSelectMode mode) - { - SPITransferOptions opts = SPITransferOptions.SIZE_IN_BYTES; - - switch (mode) - { - case ChipSelectMode.ActiveLow: - opts |= SPITransferOptions.CHIPSELECT_DISABLE; - break; - case ChipSelectMode.ActiveHigh: - opts |= SPITransferOptions.CHIPSELECT_ENABLE; - break; - } - - return opts; - } - - /// - public void Read(IDigitalOutputPort? chipSelect, Span readBuffer, ChipSelectMode csMode = ChipSelectMode.ActiveLow) - { - var options = CreateTransferOptions(csMode); - - if (chipSelect != null) - { - chipSelect.State = csMode == ChipSelectMode.ActiveLow ? false : true; - } - - var status = Mpsse.SPI_Read( - Handle, - MemoryMarshal.GetReference(readBuffer), - readBuffer.Length, - out _, - options - ); - - if (chipSelect != null) - { - chipSelect.State = csMode == ChipSelectMode.ActiveLow ? true : false; - } - - CheckStatus(status); - } - - /// - public void Write(IDigitalOutputPort? chipSelect, Span writeBuffer, ChipSelectMode csMode = ChipSelectMode.ActiveLow) - { - var options = CreateTransferOptions(csMode); - - if (chipSelect != null) - { - chipSelect.State = csMode == ChipSelectMode.ActiveLow ? false : true; - } - - var status = Mpsse.SPI_Write( - Handle, - MemoryMarshal.GetReference(writeBuffer), - writeBuffer.Length, - out _, - options - ); - - if (chipSelect != null) - { - chipSelect.State = csMode == ChipSelectMode.ActiveLow ? true : false; - } - - CheckStatus(status); - } - - /// - public void Exchange(IDigitalOutputPort? chipSelect, Span writeBuffer, Span readBuffer, ChipSelectMode csMode = ChipSelectMode.ActiveLow) - { - var options = CreateTransferOptions(csMode); - - if (chipSelect != null) - { - chipSelect.State = csMode == ChipSelectMode.ActiveLow ? false : true; - } - - var status = Mpsse.SPI_ReadWrite( - Handle, - MemoryMarshal.GetReference(readBuffer), - MemoryMarshal.GetReference(writeBuffer), - writeBuffer.Length, - out _, - options - ); - - if (chipSelect != null) - { - chipSelect.State = csMode == ChipSelectMode.ActiveLow ? true : false; - } - - CheckStatus(status); - } -} \ No newline at end of file diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/Native.Mpsse.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/Native.Mpsse.cs deleted file mode 100644 index 01f0c95108..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/MPSSE/Native.Mpsse.cs +++ /dev/null @@ -1,115 +0,0 @@ -using System; -using System.Runtime.InteropServices; -using static Meadow.Foundation.ICs.IOExpanders.Ft232h; - -namespace Meadow.Foundation.ICs.IOExpanders -{ - internal static partial class Native - { - public static class Mpsse - { - private const string MPSSE_LIB = "libmpsse"; - - // FTDIMPSSE_API void Init_libMPSSE(void); - [DllImport(MPSSE_LIB, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - public static extern void Init_libMPSSE(); - - // FTDIMPSSE_API void Cleanup_libMPSSE(void); - [DllImport(MPSSE_LIB, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - public static extern void Cleanup_libMPSSE(); - - // TDIMPSSE_API FT_STATUS Ver_libMPSSE(LPDWORD libmpsse, LPDWORD libftd2xx); - [DllImport(MPSSE_LIB, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - public static extern FT_STATUS Ver_libMPSSE(out uint libmpsse, out uint libftd2xx); - - // === GPIO === - - // FTDIMPSSE_API FT_STATUS FT_WriteGPIO(FT_HANDLE handle, UCHAR dir, UCHAR value); - [DllImport(MPSSE_LIB, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - public static extern FT_STATUS FT_WriteGPIO(IntPtr handle, byte dir, byte value); - - // FTDIMPSSE_API FT_STATUS FT_ReadGPIO(FT_HANDLE handle, UCHAR *value); - [DllImport(MPSSE_LIB, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - public static extern FT_STATUS FT_ReadGPIO(IntPtr handle, out byte value); - - // === I2C === - - // FTDIMPSSE_API FT_STATUS I2C_GetNumChannels(DWORD *numChannels); - [DllImport(MPSSE_LIB, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - public static extern FT_STATUS I2C_GetNumChannels(out int numChannels); - - // FTDIMPSSE_API FT_STATUS I2C_GetChannelInfo(DWORD index, FT_DEVICE_LIST_INFO_NODE* chanInfo); - [DllImport(MPSSE_LIB, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - public static extern FT_STATUS I2C_GetChannelInfo(int index, out FT_DEVICE_LIST_INFO_NODE chanInfo); - - // FTDIMPSSE_API FT_STATUS I2C_OpenChannel(DWORD index, FT_HANDLE *handle); - [DllImport(MPSSE_LIB, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - public static extern FT_STATUS I2C_OpenChannel(int index, out IntPtr handle); - - // FTDIMPSSE_API FT_STATUS I2C_InitChannel(FT_HANDLE handle, ChannelConfig *config); - [DllImport(MPSSE_LIB, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - public static extern FT_STATUS I2C_InitChannel(IntPtr handle, ref I2CChannelConfig config); - - // FTDIMPSSE_API FT_STATUS I2C_CloseChannel(FT_HANDLE handle); - [DllImport(MPSSE_LIB, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - public static extern FT_STATUS I2C_CloseChannel(IntPtr handle); - - // FTDIMPSSE_API FT_STATUS I2C_DeviceWrite(FT_HANDLE handle, UCHAR deviceAddress, DWORD sizeToTransfer, UCHAR *buffer, LPDWORD sizeTransfered, DWORD options); - [DllImport(MPSSE_LIB, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - public static extern FT_STATUS I2C_DeviceWrite(IntPtr handle, byte deviceAddress, int sizeToTransfer, in byte buffer, out int sizeTransfered, I2CTransferOptions options); - - // FTDIMPSSE_API FT_STATUS I2C_DeviceRead(FT_HANDLE handle, UCHAR deviceAddress, DWORD sizeToTransfer, UCHAR *buffer, LPDWORD sizeTransfered, DWORD options); - [DllImport(MPSSE_LIB, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - public static extern FT_STATUS I2C_DeviceRead(IntPtr handle, byte deviceAddress, int sizeToTransfer, in byte buffer, out int sizeTransfered, I2CTransferOptions options); - - // === SPI === - - // FTDIMPSSE_API FT_STATUS SPI_GetNumChannels(DWORD *numChannels); - [DllImport(MPSSE_LIB, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - public static extern FT_STATUS SPI_GetNumChannels(out int numChannels); - - // FTDIMPSSE_API FT_STATUS SPI_GetChannelInfo(DWORD index, FT_DEVICE_LIST_INFO_NODE* chanInfo); - [DllImport(MPSSE_LIB, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - public static extern FT_STATUS SPI_GetChannelInfo(int index, out FT_DEVICE_LIST_INFO_NODE chanInfo); - - // FTDIMPSSE_API FT_STATUS SPI_OpenChannel(DWORD index, FT_HANDLE *handle); - [DllImport(MPSSE_LIB, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - public static extern FT_STATUS SPI_OpenChannel(int index, out IntPtr handle); - - // FTDIMPSSE_API FT_STATUS SPI_InitChannel(FT_HANDLE handle, ChannelConfig *config); - [DllImport(MPSSE_LIB, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - public static extern FT_STATUS SPI_InitChannel(IntPtr handle, ref SpiChannelConfig config); - - // FTDIMPSSE_API FT_STATUS I2C_CloseChannel(FT_HANDLE handle); - [DllImport(MPSSE_LIB, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - public static extern FT_STATUS SPI_CloseChannel(IntPtr handle); - - // FTDIMPSSE_API FT_STATUS SPI_Read(FT_HANDLE handle, UCHAR *buffer, DWORD sizeToTransfer, LPDWORD sizeTransfered, DWORD options); - [DllImport(MPSSE_LIB, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - public static extern FT_STATUS SPI_Read(IntPtr handle, in byte buffer, int sizeToTransfer, out int sizeTransfered, SPITransferOptions options); - - // FTDIMPSSE_API FT_STATUS SPI_Write(FT_HANDLE handle, UCHAR *buffer, DWORD sizeToTransfer, LPDWORD sizeTransfered, DWORD options); - [DllImport(MPSSE_LIB, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - public static extern FT_STATUS SPI_Write(IntPtr handle, in byte buffer, int sizeToTransfer, out int sizeTransfered, SPITransferOptions options); - - // FTDIMPSSE_API FT_STATUS SPI_ReadWrite(FT_HANDLE handle, UCHAR *inBuffer, UCHAR* outBuffer, DWORD sizeToTransfer, LPDWORD sizeTransferred, DWORD transferOptions); - [DllImport(MPSSE_LIB, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - public static extern FT_STATUS SPI_ReadWrite(IntPtr handle, in byte inBuffer, in byte outBuffer, int sizeToTransfer, out int sizeTransferred, SPITransferOptions transferOptions); - - ///Reads the logic state of the SPI MISO line without clocking the bus - // FTDIMPSSE_API FT_STATUS SPI_IsBusy(FT_HANDLE handle, BOOL *state); - [DllImport(MPSSE_LIB, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - public static extern FT_STATUS SPI_IsBusy(IntPtr handle, out bool state); - - /// This function changes the chip select line that is to be used to communicate to the SPI slave - // FTDIMPSSE_API FT_STATUS SPI_ChangeCS(FT_HANDLE handle, DWORD configOptions); - [DllImport(MPSSE_LIB, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - public static extern FT_STATUS SPI_ChangeCS(IntPtr handle, SpiConfigOptions configOptions); - - ///This function turns ON/OFF the chip select line associated with the channel - // FTDIMPSSE_API FT_STATUS SPI_ToggleCS(FT_HANDLE handle, BOOL state); - [DllImport(MPSSE_LIB, CallingConvention = CallingConvention.Cdecl, SetLastError = true)] - public static extern FT_STATUS SPI_ToggleCS(IntPtr handle, bool state); - } - } -} \ No newline at end of file diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Native.Ftd2xx.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Native.Ftd2xx.cs similarity index 100% rename from Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Ft23xx/Native.Ftd2xx.cs rename to Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Native.Ftd2xx.cs diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Native.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Native.cs index b928a2ebe6..61ff653fb0 100644 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Native.cs +++ b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/Native.cs @@ -1,267 +1,174 @@ using System; -using System.Runtime.InteropServices; -namespace Meadow.Foundation.ICs.IOExpanders +namespace Meadow.Foundation.ICs.IOExpanders; + +internal static partial class Native { - internal static partial class Native + public static bool CheckStatus(FT_STATUS status) { - public static bool CheckStatus(Native.FT_STATUS status) - { - if (status == Native.FT_STATUS.FT_OK) - { - return true; - } - - throw new Exception($"Native error: {status}"); - } - - public enum FT_OPEN_TYPE - { - FT_OPEN_BY_SERIAL_NUMBER = 1, - FT_OPEN_BY_DESCRIPTION = 2, - FT_OPEN_BY_LOCATION = 4 - } - - public enum FT_DEVICE_TYPE - { - FT_DEVICE_BM, - FT_DEVICE_AM, - FT_DEVICE_100AX, - FT_DEVICE_UNKNOWN, - FT_DEVICE_2232C, - FT_DEVICE_232R, - FT_DEVICE_2232H, - FT_DEVICE_4232H, - FT_DEVICE_232H, - FT_DEVICE_X_SERIES, - FT_DEVICE_4222H_0, - FT_DEVICE_4222H_1_2, - FT_DEVICE_4222H_3, - FT_DEVICE_4222_PROG, - FT_DEVICE_900, - FT_DEVICE_930, - FT_DEVICE_UMFTPD3A, - FT_DEVICE_2233HP, - FT_DEVICE_4233HP, - FT_DEVICE_2232HP, - FT_DEVICE_4232HP, - FT_DEVICE_233HP, - FT_DEVICE_232HP, - FT_DEVICE_2232HA, - FT_DEVICE_4232HA, - } - - public enum FT_DRIVER_TYPE - { - FT_DRIVER_TYPE_D2XX = 0, - FT_DRIVER_TYPE_VCP = 1 - } - - [Flags] - public enum FT_FLAGS - { - FT_FLAGS_OPENED = 1, - FT_FLAGS_HISPEED = 2 - } - - [Flags] - public enum FT_FLOWCONTROL : ushort + if (status == FT_STATUS.FT_OK) { - FT_FLOW_NONE = 0x0000, - FT_FLOW_RTS_CTS = 0x0100, - FT_FLOW_DTR_DSR = 0x0200, - FT_FLOW_XON_XOFF = 0x0400, + return true; } - [Flags] - public enum FT_BITMODE - { - FT_BITMODE_RESET = 0x00, - FT_BITMODE_ASYNC_BITBANG = 0x01, - FT_BITMODE_MPSSE = 0x02, - FT_BITMODE_SYNC_BITBANG = 0x04, - FT_BITMODE_MCU_HOST = 0x08, - FT_BITMODE_FAST_SERIAL = 0x10, - FT_BITMODE_CBUS_BITBANG = 0x20, - FT_BITMODE_SYNC_FIFO = 0x40, + throw new Exception($"Native error: {status}"); + } - } - internal enum FT_OPCODE - { - ClockDataBytesOutOnPlusVeClockMSBFirst = 0x10, - ClockDataBytesOutOnMinusVeClockMSBFirst = 0x11, - ClockDataBitsOutOnPlusVeClockMSBFirst = 0x12, - ClockDataBitsOutOnMinusVeClockMSBFirst = 0x13, - ClockDataBytesInOnPlusVeClockMSBFirst = 0x20, - ClockDataBytesInOnMinusVeClockMSBFirst = 0x24, - ClockDataBitsInOnPlusVeClockMSBFirst = 0x22, - ClockDataBitsInOnMinusVeClockMSBFirst = 0x26, - ClockDataBytesOutOnMinusBytesInOnPlusVeClockMSBFirst = 0x31, - ClockDataBytesOutOnPlusBytesInOnMinusVeClockMSBFirst = 0x34, - ClockDataBitsOutOnMinusBitsInOnPlusVeClockMSBFirst = 0x33, - ClockDataBitsOutOnPlusBitsInOnMinusVeClockMSBFirst = 0x36, - ClockDataBytesOutOnPlusVeClockLSBFirst = 0x18, - ClockDataBytesOutOnMinusVeClockLSBFirst = 0x19, - ClockDataBitsOutOnPlusVeClockLSBFirst = 0x1A, - ClockDataBitsOutOnMinusVeClockLSBFirst = 0x1B, - ClockDataBytesInOnPlusVeClockLSBFirst = 0x28, - ClockDataBytesInOnMinusVeClockLSBFirst = 0x2C, - ClockDataBitsInOnPlusVeClockLSBFirst = 0x2A, - ClockDataBitsInOnMinusVeClockSBFirst = 0x2E, - ClockDataBytesOutOnMinusBytesInOnPlusVeClockLSBFirst = 0x39, - ClockDataBytesOutOnPlusBytesInOnMinusVeClockLSBFirst = 0x3C, - ClockDataBitsOutOnMinusBitsInOnPlusVeClockLSBFirst = 0x3B, - ClockDataBitsOutOnPlusBitsInOnMinusVeClockLSBFirst = 0x3E, - ClockDataBytesOutOnPlusVeClockTMSPinLSBFirst = 0x4A, - ClockDataBytesOutOnMinusVeClockTMSPinSBFirst = 0x4B, - ClockDataBytesOutOnPlusDataInOnPlusVeClockTMSPinSBFirst = 0x6A, - ClockDataBytesOutOnMinusDataInOnPlusVeClockTMSPinSBFirst = 0x6B, - ClockDataBytesOutOnPlusDataInOnMinusVeClockTMSPinSBFirst = 0x6E, - ClockDataBytesOutOnMinusDataInOnMinusVeClockTMSPinSBFirst = 0x6F, - SetDataBitsLowByte = 0x80, - SetDataBitsHighByte = 0x82, - ReadDataBitsLowByte = 0x81, - ReadDataBitsHighByte = 0x83, - ConnectTDItoTDOforLoopback = 0x84, - DisconnectTDItoTDOforLoopback = 0x85, - SetTCKSKDivisor = 0x86, - SetClockDivisor = 0x86, - CPUModeReadShortAddress = 0x90, - CPUModeReadExtendedAddress = 0x91, - CPUModeWriteShortAddress = 0x92, - CPUModeWriteExtendedAddress = 0x93, - SendImmediate = 0x87, - WaitOnIOHigh = 0x88, - WaitOnIOLow = 0x89, - DisableClockDivideBy5 = 0x8A, - EnableClockDivideBy5 = 0x8B, - Enable3PhaseDataClocking = 0x8C, - Disable3PhaseDataClocking = 0x8D, - ClockForNBitsWithNoDataTransfer = 0x8E, - ClockForNx8BitsWithNoDataTransfer = 0x8F, - ClockContinuouslyAndWaitOnIOHigh = 0x94, - ClockContinuouslyAndWaitOnIOLow = 0x95, - TurnOnAdaptiveClocking = 0x96, - TurnOffAdaptiveClocking = 0x97, - ClockForNx8BitsWithNoDataTransferOrUntilGPIOL1IsHigh = 0x9C, - ClockForNx8BitsWithNoDataTransferOrUntilGPIOL1IsLow = 0x9D, - SetIOOnlyDriveOn0AndTristateOn1 = 0x9E, - } + public enum FT_OPEN_TYPE + { + FT_OPEN_BY_SERIAL_NUMBER = 1, + FT_OPEN_BY_DESCRIPTION = 2, + FT_OPEN_BY_LOCATION = 4 + } - public enum FT_STATUS - { - FT_OK, - FT_INVALID_HANDLE, - FT_DEVICE_NOT_FOUND, - FT_DEVICE_NOT_OPENED, - FT_IO_ERROR, - FT_INSUFFICIENT_RESOURCES, - FT_INVALID_PARAMETER, - FT_INVALID_BAUD_RATE, + public enum FT_DEVICE_TYPE + { + FT_DEVICE_BM, + FT_DEVICE_AM, + FT_DEVICE_100AX, + FT_DEVICE_UNKNOWN, + FT_DEVICE_2232C, + FT_DEVICE_232R, + FT_DEVICE_2232H, + FT_DEVICE_4232H, + FT_DEVICE_232H, + FT_DEVICE_X_SERIES, + FT_DEVICE_4222H_0, + FT_DEVICE_4222H_1_2, + FT_DEVICE_4222H_3, + FT_DEVICE_4222_PROG, + FT_DEVICE_900, + FT_DEVICE_930, + FT_DEVICE_UMFTPD3A, + FT_DEVICE_2233HP, + FT_DEVICE_4233HP, + FT_DEVICE_2232HP, + FT_DEVICE_4232HP, + FT_DEVICE_233HP, + FT_DEVICE_232HP, + FT_DEVICE_2232HA, + FT_DEVICE_4232HA, + } - FT_DEVICE_NOT_OPENED_FOR_ERASE, - FT_DEVICE_NOT_OPENED_FOR_WRITE, - FT_FAILED_TO_WRITE_DEVICE, - FT_EEPROM_READ_FAILED, - FT_EEPROM_WRITE_FAILED, - FT_EEPROM_ERASE_FAILED, - FT_EEPROM_NOT_PRESENT, - FT_EEPROM_NOT_PROGRAMMED, - FT_INVALID_ARGS, - FT_NOT_SUPPORTED, - FT_OTHER_ERROR, - FT_DEVICE_LIST_NOT_READY, - } + public enum FT_DRIVER_TYPE + { + FT_DRIVER_TYPE_D2XX = 0, + FT_DRIVER_TYPE_VCP = 1 + } - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] - public struct FT_DEVICE_LIST_INFO_NODE - { - /* - typedef struct _ft_device_list_info_node { - ULONG Flags; - ULONG Type; - ULONG ID; - DWORD LocId; - char SerialNumber[16]; - char Description[64]; - FT_HANDLE ftHandle; - } FT_DEVICE_LIST_INFO_NODE; - */ - public FT_FLAGS Flags; - public FT_DEVICE_TYPE Type; - public uint ID; - public uint LocId; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)] - public string SerialNumber; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)] - public string Description; - public IntPtr ftHandle; - } + [Flags] + public enum FT_FLAGS + { + FT_FLAGS_OPENED = 1, + FT_FLAGS_HISPEED = 2 + } - [Flags] - public enum I2CChannelOptions - { - None = 0, - ThreePhaseClocking = 1 << 0, - Loopback = 1 << 1, - ClockStretching = 1 << 2, - } + [Flags] + public enum FT_FLOWCONTROL : ushort + { + FT_FLOW_NONE = 0x0000, + FT_FLOW_RTS_CTS = 0x0100, + FT_FLOW_DTR_DSR = 0x0200, + FT_FLOW_XON_XOFF = 0x0400, + } - public struct I2CChannelConfig - { - /* - typedef struct ChannelConfig_t - { - I2C_CLOCKRATE ClockRate; - UCHAR LatencyTimer; // Valid range is 2 � 255 + [Flags] + public enum FT_BITMODE + { + FT_BITMODE_RESET = 0x00, + FT_BITMODE_ASYNC_BITBANG = 0x01, + FT_BITMODE_MPSSE = 0x02, + FT_BITMODE_SYNC_BITBANG = 0x04, + FT_BITMODE_MCU_HOST = 0x08, + FT_BITMODE_FAST_SERIAL = 0x10, + FT_BITMODE_CBUS_BITBANG = 0x20, + FT_BITMODE_SYNC_FIFO = 0x40, - DWORD Options; - /** This member provides a way to enable/disable features - specific to the protocol that are implemented in the chip - BIT0 : 3PhaseDataClocking - Setting this bit will turn on 3 phase data clocking for a - FT2232H dual hi-speed device or FT4232H quad hi-speed device. Three phase - data clocking, ensures the data is valid on both edges of a clock - BIT1 : Loopback - BIT2 : Clock stretching - BIT3 -BIT31 : Reserved - } ChannelConfig; - */ - public I2CClockRate ClockRate; - public byte LatencyTimer; - public I2CChannelOptions Options; - } + } + internal enum FT_OPCODE + { + ClockDataBytesOutOnPlusVeClockMSBFirst = 0x10, + ClockDataBytesOutOnMinusVeClockMSBFirst = 0x11, + ClockDataBitsOutOnPlusVeClockMSBFirst = 0x12, + ClockDataBitsOutOnMinusVeClockMSBFirst = 0x13, + ClockDataBytesInOnPlusVeClockMSBFirst = 0x20, + ClockDataBytesInOnMinusVeClockMSBFirst = 0x24, + ClockDataBitsInOnPlusVeClockMSBFirst = 0x22, + ClockDataBitsInOnMinusVeClockMSBFirst = 0x26, + ClockDataBytesOutOnMinusBytesInOnPlusVeClockMSBFirst = 0x31, + ClockDataBytesOutOnPlusBytesInOnMinusVeClockMSBFirst = 0x34, + ClockDataBitsOutOnMinusBitsInOnPlusVeClockMSBFirst = 0x33, + ClockDataBitsOutOnPlusBitsInOnMinusVeClockMSBFirst = 0x36, + ClockDataBytesOutOnPlusVeClockLSBFirst = 0x18, + ClockDataBytesOutOnMinusVeClockLSBFirst = 0x19, + ClockDataBitsOutOnPlusVeClockLSBFirst = 0x1A, + ClockDataBitsOutOnMinusVeClockLSBFirst = 0x1B, + ClockDataBytesInOnPlusVeClockLSBFirst = 0x28, + ClockDataBytesInOnMinusVeClockLSBFirst = 0x2C, + ClockDataBitsInOnPlusVeClockLSBFirst = 0x2A, + ClockDataBitsInOnMinusVeClockSBFirst = 0x2E, + ClockDataBytesOutOnMinusBytesInOnPlusVeClockLSBFirst = 0x39, + ClockDataBytesOutOnPlusBytesInOnMinusVeClockLSBFirst = 0x3C, + ClockDataBitsOutOnMinusBitsInOnPlusVeClockLSBFirst = 0x3B, + ClockDataBitsOutOnPlusBitsInOnMinusVeClockLSBFirst = 0x3E, + ClockDataBytesOutOnPlusVeClockTMSPinLSBFirst = 0x4A, + ClockDataBytesOutOnMinusVeClockTMSPinSBFirst = 0x4B, + ClockDataBytesOutOnPlusDataInOnPlusVeClockTMSPinSBFirst = 0x6A, + ClockDataBytesOutOnMinusDataInOnPlusVeClockTMSPinSBFirst = 0x6B, + ClockDataBytesOutOnPlusDataInOnMinusVeClockTMSPinSBFirst = 0x6E, + ClockDataBytesOutOnMinusDataInOnMinusVeClockTMSPinSBFirst = 0x6F, + SetDataBitsLowByte = 0x80, + SetDataBitsHighByte = 0x82, + ReadDataBitsLowByte = 0x81, + ReadDataBitsHighByte = 0x83, + ConnectTDItoTDOforLoopback = 0x84, + DisconnectTDItoTDOforLoopback = 0x85, + SetTCKSKDivisor = 0x86, + SetClockDivisor = 0x86, + CPUModeReadShortAddress = 0x90, + CPUModeReadExtendedAddress = 0x91, + CPUModeWriteShortAddress = 0x92, + CPUModeWriteExtendedAddress = 0x93, + SendImmediate = 0x87, + WaitOnIOHigh = 0x88, + WaitOnIOLow = 0x89, + DisableClockDivideBy5 = 0x8A, + EnableClockDivideBy5 = 0x8B, + Enable3PhaseDataClocking = 0x8C, + Disable3PhaseDataClocking = 0x8D, + ClockForNBitsWithNoDataTransfer = 0x8E, + ClockForNx8BitsWithNoDataTransfer = 0x8F, + ClockContinuouslyAndWaitOnIOHigh = 0x94, + ClockContinuouslyAndWaitOnIOLow = 0x95, + TurnOnAdaptiveClocking = 0x96, + TurnOffAdaptiveClocking = 0x97, + ClockForNx8BitsWithNoDataTransferOrUntilGPIOL1IsHigh = 0x9C, + ClockForNx8BitsWithNoDataTransferOrUntilGPIOL1IsLow = 0x9D, + SetIOOnlyDriveOn0AndTristateOn1 = 0x9E, + } - public struct SpiChannelConfig - { - /* - DWORD ClockRate; /* SPI clock rate, value should be <= 30000000 - UCHAR LatencyTimer; /* value in milliseconds, maximum value should be <= 255 - DWORD configOptions; /* This member provides a way to enable/disable features - specific to the protocol that are implemented in the chip - BIT1-0=CPOL-CPHA: 00 - MODE0 - data captured on rising edge, propagated on falling - 01 - MODE1 - data captured on falling edge, propagated on rising - 10 - MODE2 - data captured on falling edge, propagated on rising - 11 - MODE3 - data captured on rising edge, propagated on falling - BIT4-BIT2: 000 - A/B/C/D_DBUS3=ChipSelect - : 001 - A/B/C/D_DBUS4=ChipSelect - : 010 - A/B/C/D_DBUS5=ChipSelect - : 011 - A/B/C/D_DBUS6=ChipSelect - : 100 - A/B/C/D_DBUS7=ChipSelect - BIT5: ChipSelect is active high if this bit is 0 - BIT6 -BIT31 : Reserved - - DWORD Pin;/* BIT7 -BIT0: Initial direction of the pins - /* BIT15 -BIT8: Initial values of the pins - /* BIT23 -BIT16: Final direction of the pins - /* BIT31 -BIT24: Final values of the pins - USHORT currentPinState;/* BIT7 -BIT0: Current direction of the pins - /* BIT15 -BIT8: Current values of the pins - */ - public uint ClockRate; - public byte LatencyTimer; - public Ft232h.SpiConfigOptions Options; - public uint Pin; - public ushort CurrentPinState; - } + public enum FT_STATUS + { + FT_OK, + FT_INVALID_HANDLE, + FT_DEVICE_NOT_FOUND, + FT_DEVICE_NOT_OPENED, + FT_IO_ERROR, + FT_INSUFFICIENT_RESOURCES, + FT_INVALID_PARAMETER, + FT_INVALID_BAUD_RATE, + + FT_DEVICE_NOT_OPENED_FOR_ERASE, + FT_DEVICE_NOT_OPENED_FOR_WRITE, + FT_FAILED_TO_WRITE_DEVICE, + FT_EEPROM_READ_FAILED, + FT_EEPROM_WRITE_FAILED, + FT_EEPROM_ERASE_FAILED, + FT_EEPROM_NOT_PRESENT, + FT_EEPROM_NOT_PROGRAMMED, + FT_INVALID_ARGS, + FT_NOT_SUPPORTED, + FT_OTHER_ERROR, + FT_DEVICE_LIST_NOT_READY, } } \ No newline at end of file diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SPITransferOptions.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SPITransferOptions.cs deleted file mode 100644 index 8e1055700f..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SPITransferOptions.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System; - -namespace Meadow.Foundation.ICs.IOExpanders -{ - public partial class Ft232h - { - [Flags] - internal enum SPITransferOptions - { - /* transferOptions-Bit0: If this bit is 0 then it means that the transfer size provided is in bytes */ - SIZE_IN_BYTES = 0x00000000, - /* transferOptions-Bit0: If this bit is 1 then it means that the transfer size provided is in bytes */ - SIZE_IN_BITS = 0x00000001, - /* transferOptions-Bit1: if BIT1 is 1 then CHIP_SELECT line will be enabled at start of transfer */ - CHIPSELECT_ENABLE = 0x00000002, - /* transferOptions-Bit2: if BIT2 is 1 then CHIP_SELECT line will be disabled at end of transfer */ - CHIPSELECT_DISABLE = 0x00000004, - /* transferOptions-Bit3: if BIT3 is 1 then LSB will be processed first */ - LSB_FIRST = 0x00000008 - } - } -} \ No newline at end of file diff --git a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SpiConfigOptions.cs b/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SpiConfigOptions.cs deleted file mode 100644 index f60142ae8e..0000000000 --- a/Source/Meadow.Foundation.Peripherals/ICs.IOExpanders.Ft232h/Driver/SpiConfigOptions.cs +++ /dev/null @@ -1,69 +0,0 @@ -using System; - -namespace Meadow.Foundation.ICs.IOExpanders -{ - public partial class Ft232h - { - /// - /// Flags for configuring the SPI communication options. - /// - [Flags] - public enum SpiConfigOptions - { - /// - /// SPI Mode 0: CPOL=0, CPHA=0. - /// - MODE0 = 0x00000000, - - /// - /// SPI Mode 1: CPOL=0, CPHA=1. - /// - MODE1 = 0x00000001, - - /// - /// SPI Mode 2: CPOL=1, CPHA=0. - /// - MODE2 = 0x00000002, - - /// - /// SPI Mode 3: CPOL=1, CPHA=1. - /// - MODE3 = 0x00000003, - - /// - /// Chip Select on D3. - /// - CS_DBUS3 = 0x00000000, - - /// - /// Chip Select on D4. - /// - CS_DBUS4 = 0x00000004, - - /// - /// Chip Select on D5. - /// - CS_DBUS5 = 0x00000008, - - /// - /// Chip Select on D6. - /// - CS_DBUS6 = 0x0000000C, - - /// - /// Chip Select on D7. - /// - CS_DBUS7 = 0x00000010, - - /// - /// Chip Select active high. - /// - CS_ACTIVEHIGH = 0x00000000, - - /// - /// Chip Select active low. - /// - CS_ACTIVELOW = 0x00000020 - } - } -} From d373ab6be7b6b93a6e975efee9104df3db33a1b7 Mon Sep 17 00:00:00 2001 From: Chris Tacke Date: Thu, 22 Feb 2024 16:11:47 -0600 Subject: [PATCH 11/11] deleted unneeded file --- .../Arducam_Sample - Backup.csproj | 26 ------------------- 1 file changed, 26 deletions(-) delete mode 100644 Source/Meadow.Foundation.Peripherals/Sensors.Camera.Arducam/Samples/Arducam_Sample/Arducam_Sample - Backup.csproj diff --git a/Source/Meadow.Foundation.Peripherals/Sensors.Camera.Arducam/Samples/Arducam_Sample/Arducam_Sample - Backup.csproj b/Source/Meadow.Foundation.Peripherals/Sensors.Camera.Arducam/Samples/Arducam_Sample/Arducam_Sample - Backup.csproj deleted file mode 100644 index 619e6f3a3c..0000000000 --- a/Source/Meadow.Foundation.Peripherals/Sensors.Camera.Arducam/Samples/Arducam_Sample/Arducam_Sample - Backup.csproj +++ /dev/null @@ -1,26 +0,0 @@ - - - https://github.com/WildernessLabs/Meadow.Foundation - Wilderness Labs, Inc - Wilderness Labs, Inc - true - netstandard2.1 - Library - App - - - true - - - - - - - - - - - Always - - -