コード例 #1
0
        /// <summary>
        /// Create an SPI FT4222 class
        /// </summary>
        /// <param name="settings">SPI Connection Settings</param>
        public Ft4222Spi(SpiConnectionSettings settings)
        {
            _settings = settings;
            // Check device
            var devInfos = FtCommon.GetDevices();

            if (devInfos.Count == 0)
            {
                throw new IOException("No FTDI device available");
            }

            // Select the one from bus Id
            // FT4222 propose depending on the mode multiple interfaces. Only the A is available for SPI or where there is none as it's the only interface
            var devInfo = devInfos.Where(m => m.Description == "FT4222 A" || m.Description == "FT4222").ToArray();

            if ((devInfo.Length == 0) || (devInfo.Length < _settings.BusId))
            {
                throw new IOException($"Can't find a device to open SPI on index {_settings.BusId}");
            }

            DeviceInformation = devInfo[_settings.BusId];
            // Open device
            var ftStatus = FtFunction.FT_OpenEx(DeviceInformation.LocId, FtOpenType.OpenByLocation, out _ftHandle);

            if (ftStatus != FtStatus.Ok)
            {
                throw new IOException($"Failed to open device {DeviceInformation.Description} with error: {ftStatus}");
            }

            // Set the clock but we need some math
            var(ft4222Clock, tfSpiDiv) = CalculateBestClockRate();

            ftStatus = FtFunction.FT4222_SetClock(_ftHandle, ft4222Clock);
            if (ftStatus != FtStatus.Ok)
            {
                throw new IOException($"Failed to set clock rate {ft4222Clock} on device: {DeviceInformation.Description}with error: {ftStatus}");
            }

            SpiClockPolarity pol = SpiClockPolarity.ClockIdleLow;

            if ((_settings.Mode == SpiMode.Mode2) || (_settings.Mode == SpiMode.Mode3))
            {
                pol = SpiClockPolarity.ClockIdelHigh;
            }

            SpiClockPhase pha = SpiClockPhase.ClockLeading;

            if ((_settings.Mode == SpiMode.Mode1) || (_settings.Mode == SpiMode.Mode3))
            {
                pha = SpiClockPhase.ClockTailing;
            }

            // Configure the SPI
            ftStatus = FtFunction.FT4222_SPIMaster_Init(_ftHandle, SpiOperatingMode.Single, tfSpiDiv, pol, pha,
                                                        (byte)_settings.ChipSelectLine);
            if (ftStatus != FtStatus.Ok)
            {
                throw new IOException($"Failed setup SPI on device: {DeviceInformation.Description} with error: {ftStatus}");
            }
        }
コード例 #2
0
 /// <summary>
 /// Instantiates a DeviceInformation object.
 /// </summary>
 /// <param name="flags">Indicates device state.</param>
 /// <param name="type">Indicates the device type.</param>
 /// <param name="id">The Vendor ID and Product ID of the device.</param>
 /// <param name="locId">The physical location identifier of the device.</param>
 /// <param name="serialNumber">The device serial number.</param>
 /// <param name="description">The device description.</param>
 public DeviceInformation(FtFlag flags, FtDevice type, uint id, uint locId, string serialNumber, string description)
 {
     Flags        = flags;
     Type         = type;
     Id           = id;
     LocId        = locId;
     SerialNumber = serialNumber;
     Description  = description;
 }
コード例 #3
0
        /// <summary>
        /// Create a FT4222 GPIO driver
        /// </summary>
        /// <param name="deviceNumber">Number of the device in the device list, default 0</param>
        public Ft4222Gpio(int deviceNumber = 0)
        {
            // Check device
            var devInfos = FtCommon.GetDevices();

            if (devInfos.Count == 0)
            {
                throw new IOException("No FTDI device available");
            }

            // Select the deviceNumber, only the last one in Mode 0 and Mode 1 can be open.
            // The last one is either B if in Mode 0 or D in mode 1.
            string strMode = devInfos[0].Type == FtDeviceType.Ft4222HMode1or2With4Interfaces ? "FT4222 D" : "FT4222 B";

            var devInfo = devInfos.Where(m => m.Description == strMode).ToArray();

            if ((devInfo.Length == 0) || (devInfo.Length < deviceNumber))
            {
                throw new IOException($"Can't find a device to open GPIO on index {deviceNumber}");
            }

            DeviceInformation = devInfo[deviceNumber];
            // Open device
            var ftStatus = FtFunction.FT_OpenEx(DeviceInformation.LocId, FtOpenType.OpenByLocation, out _ftHandle);

            if (ftStatus != FtStatus.Ok)
            {
                throw new IOException($"Failed to open device {DeviceInformation.Description}, status: {ftStatus}");
            }

            ftStatus = FtFunction.FT4222_SetSuspendOut(_ftHandle, false);
            if (ftStatus != FtStatus.Ok)
            {
                throw new IOException($"Can't initialize GPIO for device {DeviceInformation.Description} changing Suspend out mode, status: {ftStatus}");
            }

            ftStatus = FtFunction.FT4222_SetWakeUpInterrupt(_ftHandle, false);
            if (ftStatus != FtStatus.Ok)
            {
                throw new IOException($"Can't initialize GPIO for device {DeviceInformation.Description} removing wake up interrupt, status: {ftStatus}");
            }

            for (int i = 0; i < PinCountConst; i++)
            {
                _gpioDirections[i] = GpioPinMode.Output;
            }

            ftStatus = FtFunction.FT4222_GPIO_Init(_ftHandle, _gpioDirections);

            if (ftStatus != FtStatus.Ok)
            {
                throw new IOException($"Can't initialize GPIO for device {DeviceInformation.Description}, status: {ftStatus}");
            }
        }
コード例 #4
0
        /// <summary>
        /// Returns the list of FT4222 connected
        /// </summary>
        /// <returns>A list of devices connected</returns>
        public static List <FtDevice> GetDevices()
        {
            List <FtDevice> devInfos = new List <FtDevice>();
            FtStatus        ftStatus = 0;

            // Check device
            uint numOfDevices;

            ftStatus = FtFunction.FT_CreateDeviceInfoList(out numOfDevices);

            Debug.WriteLine($"Number of devices: {numOfDevices}");
            if (numOfDevices == 0)
            {
                throw new IOException($"No device found");
            }

            Span <byte> sernum = stackalloc byte[16];
            Span <byte> desc   = stackalloc byte[64];

            for (uint i = 0; i < numOfDevices; i++)
            {
                uint         flags = 0;
                FtDeviceType ftDevice;
                uint         id;
                uint         locId;
                IntPtr       handle;
                ftStatus = FtFunction.FT_GetDeviceInfoDetail(i, out flags, out ftDevice, out id, out locId,
                                                             in MemoryMarshal.GetReference(sernum), in MemoryMarshal.GetReference(desc), out handle);
                if (ftStatus != FtStatus.Ok)
                {
                    throw new IOException($"Can't read device information on device index {i}, error {ftStatus}");
                }

                var devInfo = new FtDevice(
                    (FtFlag)flags,
                    ftDevice,
                    id,
                    locId,
                    Encoding.ASCII.GetString(sernum.ToArray(), 0, FindFirstZero(sernum)),
                    Encoding.ASCII.GetString(desc.ToArray(), 0, FindFirstZero(desc)));
                devInfos.Add(devInfo);
            }

            return(devInfos);
        }
コード例 #5
0
        /// <summary>
        /// Create a FT4222 I2C Device
        /// </summary>
        /// <param name="deviceInformation">Device information. Use FtCommon.GetDevices to get it.</param>
        public Ft4222I2cBus(FtDevice deviceInformation)
        {
            switch (deviceInformation.Type)
            {
            case FtDeviceType.Ft4222HMode0or2With2Interfaces:
            case FtDeviceType.Ft4222HMode1or2With4Interfaces:
            case FtDeviceType.Ft4222HMode3With1Interface:
                break;

            default: throw new ArgumentException($"Unknown device type: {deviceInformation.Type}");
            }

            DeviceInformation = deviceInformation;
            // Open device
            var ftStatus = FtFunction.FT_OpenEx(DeviceInformation.LocId, FtOpenType.OpenByLocation, out _ftHandle);

            if (ftStatus != FtStatus.Ok)
            {
                throw new IOException($"Failed to open device {DeviceInformation.Description}, status: {ftStatus}");
            }

            // Set the clock
            FtClockRate ft4222Clock = FtClockRate.Clock24MHz;

            ftStatus = FtFunction.FT4222_SetClock(_ftHandle, ft4222Clock);
            if (ftStatus != FtStatus.Ok)
            {
                throw new IOException($"Failed set clock rate {ft4222Clock} on device: {DeviceInformation.Description}, status: {ftStatus}");
            }

            // Set the device as I2C Master
            ftStatus = FtFunction.FT4222_I2CMaster_Init(_ftHandle, I2cMasterFrequencyKbps);
            if (ftStatus != FtStatus.Ok)
            {
                throw new IOException($"Failed to initialize I2C Master mode on device: {DeviceInformation.Description}, status: {ftStatus}");
            }
        }
コード例 #6
0
ファイル: FtFunction.cs プロジェクト: JohnXi001/iot-1
 public static extern FtStatus FT_GetDeviceInfoDetail(uint index, out uint flags, out FtDevice chiptype, out uint id, out uint locid, in byte serialnumber, in byte description, out IntPtr ftHandle);
コード例 #7
0
ファイル: Ft4222Device.cs プロジェクト: mmalyska/iot
 /// <summary>
 /// Instantiates a FT4222 Device object.
 /// </summary>
 /// <param name="ftdevice">a FT Device</param>
 public Ft4222Device(FtDevice ftdevice)
     : base(ftdevice.Flags, ftdevice.Type, ftdevice.Id, ftdevice.LocId, ftdevice.SerialNumber, ftdevice.Description)
 {
 }