Ejemplo n.º 1
0
 public static extern FT4222_STATUS FT4222_GetClock(IntPtr ftHandle, ref FT4222_ClockRate clk);
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            // variable
            IntPtr ftHandle = new IntPtr();

            FTDI.FT_STATUS ftStatus   = 0;
            FT4222_STATUS  ft42Status = 0;

            // Check device
            UInt32 numOfDevices = 0;

            ftStatus = FT_CreateDeviceInfoList(ref numOfDevices);

            if (numOfDevices > 0)
            {
                Console.WriteLine("Devices: {0}", numOfDevices);

                // Check for valid devices to add to array
                for (uint n = 0; n < numOfDevices; n++)
                {
                    FTDI.FT_DEVICE_INFO_NODE devInfo = new FTDI.FT_DEVICE_INFO_NODE();

                    byte[] sernum = new byte[16];
                    byte[] desc   = new byte[64];

                    ftStatus = FT_GetDeviceInfoDetail(n, ref devInfo.Flags, ref devInfo.Type, ref devInfo.ID, ref devInfo.LocId,
                                                      sernum, desc, ref devInfo.ftHandle);

                    devInfo.SerialNumber = Encoding.ASCII.GetString(sernum, 0, 16);
                    devInfo.Description  = Encoding.ASCII.GetString(desc, 0, 64);
                    devInfo.SerialNumber = devInfo.SerialNumber.Substring(0, devInfo.SerialNumber.IndexOf("\0"));
                    devInfo.Description  = devInfo.Description.Substring(0, devInfo.Description.IndexOf("\0"));

                    if (FTDI.FT_STATUS.FT_OK == ftStatus)
                    {
                        Console.WriteLine("Device {0}", n);
                        Console.WriteLine("  Flags= 0x{0:X8}, ({1})", devInfo.Flags, devInfo.Flags.ToString());
                        if (devInfo.Type <= FTDI.FT_DEVICE.FT_DEVICE_BM || devInfo.Type <= FTDI.FT_DEVICE.FT_DEVICE_232H)
                        {
                            Console.WriteLine("  Type= {0}", devInfo.Type.ToString());
                        }
                        else
                        {
                            Console.WriteLine("  Type= {0}", devInfo.Type);
                        }
                        Console.WriteLine("  ID= 0x{0:X8}", devInfo.ID);
                        Console.WriteLine("  LocId= 0x{0:X8}", devInfo.LocId);
                        Console.WriteLine("  SerialNumber= {0}", devInfo.SerialNumber);
                        Console.WriteLine("  Description= {0}", devInfo.Description);
                        Console.WriteLine("  ftHandle= 0x{0:X8}", devInfo.ftHandle);

                        if (devInfo.Description.Equals("FT4222 A"))
                        {
                            deviceA = devInfo;
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("No FTDI device");
                Console.WriteLine("NG! Press Enter to continue.");
                Console.ReadLine();
                return;
            }


            // Open device "FT4222 A"
            ftStatus = FT_OpenEx(deviceA.LocId, FT_OPEN_BY_LOCATION, ref ftHandle);

            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                Console.WriteLine("Open NG: {0}", ftStatus);
                Console.WriteLine("NG! Press Enter to continue.");
                Console.ReadLine();
                return;
            }


            // Set FT4222 clock
            FT4222_ClockRate ft4222_Clock = FT4222_ClockRate.SYS_CLK_24;

            ft42Status = FT4222_SetClock(ftHandle, FT4222_ClockRate.SYS_CLK_24);
            if (ft42Status != FT4222_STATUS.FT4222_OK)
            {
                Console.WriteLine("SetClock NG: {0}. Press Enter to continue.", ft42Status);
                Console.ReadLine();
                return;
            }
            else
            {
                Console.WriteLine("SetClock OK");

                ft42Status = FT4222_GetClock(ftHandle, ref ft4222_Clock);
                if (ft42Status != FT4222_STATUS.FT4222_OK)
                {
                    Console.WriteLine("GetClock NG: {0}. Press Enter to continue.", ft42Status);
                    Console.ReadLine();
                    return;
                }
                else
                {
                    Console.WriteLine("GetClock:" + ft4222_Clock);
                }
            }



            // Init FT4222 SPI Master
            ft42Status = FT4222_SPIMaster_Init(ftHandle, FT4222_SPIMode.SPI_IO_SINGLE, FT4222_SPIClock.CLK_DIV_16, FT4222_SPICPOL.CLK_IDLE_LOW, FT4222_SPICPHA.CLK_LEADING, 0x01);
            if (ft42Status != FT4222_STATUS.FT4222_OK)
            {
                Console.WriteLine("Open NG: {0}", ft42Status);
                Console.WriteLine("NG! Press Enter to continue.");
                Console.ReadLine();
                return;
            }
            else
            {
                Console.WriteLine("Init FT4222 SPI Master OK");
            }

            ft42Status = FT4222_SPI_SetDrivingStrength(ftHandle, SPI_DrivingStrength.DS_12MA, SPI_DrivingStrength.DS_12MA, SPI_DrivingStrength.DS_16MA);
            if (ft42Status != FT4222_STATUS.FT4222_OK)
            {
                Console.WriteLine("SetDrivingStrength NG: {0}", ft42Status);
                Console.WriteLine("NG! Press Enter to continue.");
                Console.ReadLine();
                return;
            }


            // Read/Write data
            Console.WriteLine("Prepare to read/write data. Press Enter to continue.");
            Console.ReadLine();

            ushort sizeTransferred = 0;

            byte[] readBuf  = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
            byte[] writeBuf = new byte[] { 0x46, 0x54, 0x34, 0x32, 0x32, 0x32 };

            ft42Status = FT4222_SPIMaster_SingleReadWrite(ftHandle, ref readBuf[0], ref writeBuf[0], (ushort)writeBuf.Length, ref sizeTransferred, true);

            if (ft42Status != FT4222_STATUS.FT4222_OK)
            {
                Console.WriteLine("Write NG: {0}", ft42Status);
            }
            else
            {
                // Show read/write data
                string strR = System.Text.Encoding.Default.GetString(readBuf);
                Console.WriteLine("R:[" + strR + "]");

                string strW = System.Text.Encoding.Default.GetString(writeBuf);
                Console.WriteLine("W:[" + strW + "]");

                Console.WriteLine("Read/Write OK, sizeTransferred: {0}", sizeTransferred);
            }

            Console.WriteLine("Done. Press Enter to continue.");
            Console.ReadLine();

            //End
            return;
        }
Ejemplo n.º 3
0
        private void button1_Click(object sender, EventArgs e)
        {
            deviceA = new FTDI.FT_DEVICE_INFO_NODE();

            // variable
            IntPtr ftHandle = new IntPtr();

            FTDI.FT_STATUS ftStatus   = 0;
            FT4222_STATUS  ft42Status = 0;

            // Check device
            UInt32 numOfDevices = 0;

            ftStatus = FT_CreateDeviceInfoList(ref numOfDevices);

            if (numOfDevices > 0)
            {
                textBox1.AppendText($"Devices: {numOfDevices}\n");

                // Check for valid devices to add to array
                for (uint n = 0; n < numOfDevices; n++)
                {
                    FTDI.FT_DEVICE_INFO_NODE devInfo = new FTDI.FT_DEVICE_INFO_NODE();

                    byte[] sernum = new byte[16];
                    byte[] desc   = new byte[64];

                    ftStatus = FT_GetDeviceInfoDetail(n, ref devInfo.Flags, ref devInfo.Type, ref devInfo.ID, ref devInfo.LocId,
                                                      sernum, desc, ref devInfo.ftHandle);

                    devInfo.SerialNumber = Encoding.ASCII.GetString(sernum, 0, 16);
                    devInfo.Description  = Encoding.ASCII.GetString(desc, 0, 64);
                    devInfo.SerialNumber = devInfo.SerialNumber.Substring(0, devInfo.SerialNumber.IndexOf("\0"));
                    devInfo.Description  = devInfo.Description.Substring(0, devInfo.Description.IndexOf("\0"));

                    if (FTDI.FT_STATUS.FT_OK == ftStatus)
                    {
                        textBox1.AppendText($"Device {n}\n");
                        string flags = string.Format("  Flags= 0x{0:X8}, ({1})\n", devInfo.Flags, devInfo.Flags.ToString());
                        textBox1.AppendText(flags);
                        if (devInfo.Type <= FTDI.FT_DEVICE.FT_DEVICE_BM || devInfo.Type <= FTDI.FT_DEVICE.FT_DEVICE_232H)
                        {
                            textBox1.AppendText($"  Type= {devInfo.Type.ToString()}\n");
                        }
                        else
                        {
                            textBox1.AppendText($"  Type= {devInfo.Type}\n");
                        }
                        string id = string.Format($"  ID= 0x{0:X8}\n", devInfo.ID);
                        textBox1.AppendText(id);
                        string locId = string.Format($"  LocId= 0x{0:X8}\n", devInfo.LocId);
                        textBox1.AppendText(locId);
                        textBox1.AppendText($"  SerialNumber= {devInfo.SerialNumber}\n");
                        textBox1.AppendText($"  Description= {devInfo.Description}\n");
                        string fthndl = string.Format("  ftHandle= 0x{0:X8}\n", devInfo.ftHandle);
                        textBox1.AppendText(fthndl);

                        if (devInfo.Description.Equals("FT4222 A"))
                        {
                            deviceA = devInfo;
                        }
                    }
                }
            }
            else
            {
                textBox1.AppendText("No FTDI device\n");
                textBox1.AppendText("NG! Press Go to try again.\n");
                return;
            }


            // Open device "FT4222 A"
            ftStatus = FT_OpenEx(deviceA.LocId, FT_OPEN_BY_LOCATION, ref ftHandle);

            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                textBox1.AppendText($"Open NG: {ftStatus}\n");
                textBox1.AppendText("NG! Press Go to try again.\n");
                return;
            }


            // Set FT4222 clock
            FT4222_ClockRate ft4222_Clock = FT4222_ClockRate.SYS_CLK_24;

            ft42Status = FT4222_SetClock(ftHandle, FT4222_ClockRate.SYS_CLK_24);
            if (ft42Status != FT4222_STATUS.FT4222_OK)
            {
                textBox1.AppendText($"SetClock NG: {ft42Status}. Press Go to try again.\n");
                return;
            }
            else
            {
                textBox1.AppendText("SetClock OK\n");

                ft42Status = FT4222_GetClock(ftHandle, ref ft4222_Clock);
                if (ft42Status != FT4222_STATUS.FT4222_OK)
                {
                    textBox1.AppendText($"GetClock NG: {ft42Status}. Press Go to try again.\n");
                    return;
                }
                else
                {
                    textBox1.AppendText("GetClock:" + ft4222_Clock + "\n");
                }
            }

            // Init FT4222 SPI Master
            ft42Status = FT4222_SPIMaster_Init(ftHandle, FT4222_SPIMode.SPI_IO_SINGLE, FT4222_SPIClock.CLK_DIV_16, FT4222_SPICPOL.CLK_IDLE_LOW, FT4222_SPICPHA.CLK_LEADING, 0x01);
            if (ft42Status != FT4222_STATUS.FT4222_OK)
            {
                textBox1.AppendText($"Open NG: {ft42Status}\n");
                textBox1.AppendText("NG! Press Go to try again.\n");
                return;
            }
            else
            {
                textBox1.AppendText("Init FT4222 SPI Master OK\n");
            }

            ft42Status = FT4222_SPI_SetDrivingStrength(ftHandle, SPI_DrivingStrength.DS_12MA, SPI_DrivingStrength.DS_12MA, SPI_DrivingStrength.DS_16MA);
            if (ft42Status != FT4222_STATUS.FT4222_OK)
            {
                textBox1.AppendText($"SetDrivingStrength NG: {ft42Status}\n");
                textBox1.AppendText("NG! Press Go to try again.\n");
                return;
            }


            // Read/Write data
            // Check for loop-back test of all values
            if (radioButton1.Checked)
            {
                // This code outputs all possible byte values.
                // If you have MISO and MOSI connected, it will also read in the values that were written.

                for (ushort txValue = 0; txValue <= byte.MaxValue; txValue++)
                {
                    byte          byteRx      = 0;
                    byte          byteTx      = (byte)txValue;
                    ushort        transferred = 0;
                    FT4222_STATUS status      = FT4222_SPIMaster_SingleReadWrite(ftHandle, ref byteRx, ref byteTx, 1, ref transferred, true);
                    string        byteRxHex   = string.Format("0x{0:X2}", byteRx);
                    string        byteTxHex   = string.Format("0x{0:X2}", byteTx);
                    textBox1.AppendText($"Status = {status}, byteRx = {byteRxHex}, byteTx = {byteTxHex}, transferred = {transferred}\n");
                }
            }
            // Check for 2-byte transfer
            else if (radioButton3.Checked)
            {
                bool validated = ValidateTextBox1(4);

                if (validated)
                {
                    ushort wordTx = Convert.ToUInt16(textBox2.Text, 16);
                    byte[] tx     = BitConverter.GetBytes(wordTx);
                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(tx);
                    }
                    byte[]        rx          = new byte[2];
                    ushort        transferred = 0;
                    FT4222_STATUS status      = FT4222_SPIMaster_SingleReadWrite(ftHandle, ref rx[0], ref tx[0], 2, ref transferred, true);
                    ushort        wordRx      = BitConverter.ToUInt16(new byte[2] {
                        rx[1], rx[0]
                    }, 0);
                    string wordRxStr = "0x" + wordRx.ToString("X4");
                    string wordTxStr = "0x" + wordTx.ToString("X4");
                    textBox1.AppendText($"Status = {status}, byteRx = {wordRxStr}, byteTx = {wordTxStr}, transferred = {transferred}\n");
                }
            }
            // Check for 1-byte transfer
            else if (radioButton2.Checked)
            {
                bool validated = ValidateTextBox1(2);

                if (validated)
                {
                    byte          tx          = Convert.ToByte(textBox2.Text, 16);
                    byte          rx          = 0;
                    ushort        transferred = 0;
                    FT4222_STATUS status      = FT4222_SPIMaster_SingleReadWrite(ftHandle, ref rx, ref tx, 1, ref transferred, true);
                    string        byteRxStr   = "0x" + rx.ToString("X2");
                    string        byteTxStr   = "0x" + tx.ToString("X2");
                    textBox1.AppendText($"Status = {status}, byteRx = {byteRxStr}, byteTx = {byteTxStr}, transferred = {transferred}\n");
                }
            }

            textBox1.AppendText("\n\r\n\r*** Done. Press Go to try again.*** \n\r\n\r\n\r");

            FT4222_UnInitialize(ftHandle);
            FT_Close(ftHandle);
        }
        public static void Init(ref List <byte> configurationString)
        {
            // Check device
            UInt32 numOfDevices = 0;

            ftStatus = FT_CreateDeviceInfoList(ref numOfDevices);

            if (numOfDevices > 0)
            {
                byte[] sernum = new byte[16];
                byte[] desc   = new byte[64];

                ftStatus = FT_GetDeviceInfoDetail(0, ref devInfo.Flags, ref devInfo.Type, ref devInfo.ID, ref devInfo.LocId,
                                                  sernum, desc, ref devInfo.ftHandle);

                devInfo.SerialNumber = Encoding.ASCII.GetString(sernum, 0, 16);
                devInfo.Description  = Encoding.ASCII.GetString(desc, 0, 64);
                devInfo.SerialNumber = devInfo.SerialNumber.Substring(0, devInfo.SerialNumber.IndexOf("\0"));
                devInfo.Description  = devInfo.Description.Substring(0, devInfo.Description.IndexOf("\0"));

                Console.WriteLine("Device Number: {0}", numOfDevices);
            }
            else
            {
                Console.WriteLine("No FTDI device");
                Console.WriteLine("NG! Press Enter to continue.");
                return;
            }


            // Open device
            ftStatus = FT_OpenEx(devInfo.LocId, FT_OPEN_BY_LOCATION, ref ftHandle);

            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                Console.WriteLine("Open NG: {0}", ftStatus);
                Console.WriteLine("NG! Press Enter to continue.");
                return;
            }


            // Set FT4222 clock
            FT4222_ClockRate ft4222_Clock = FT4222_ClockRate.SYS_CLK_24;

            ft42Status = FT4222_SetClock(ftHandle, FT4222_ClockRate.SYS_CLK_24);
            if (ft42Status != FT4222_STATUS.FT4222_OK)
            {
                Console.WriteLine("SetClock NG: {0}. Press Enter to continue.", ft42Status);
                return;
            }
            else
            {
                Console.WriteLine("SetClock OK");

                ft42Status = FT4222_GetClock(ftHandle, ref ft4222_Clock);
                if (ft42Status != FT4222_STATUS.FT4222_OK)
                {
                    Console.WriteLine("GetClock NG: {0}. Press Enter to continue.", ft42Status);
                    return;
                }
                else
                {
                    Console.WriteLine("GetClock:" + ft4222_Clock);
                }
            }



            // Init FT4222 SPI Master
            ft42Status = FT4222_SPIMaster_Init(ftHandle, FT4222_SPIMode.SPI_IO_SINGLE, FT4222_SPIClock.CLK_DIV_16, FT4222_SPICPOL.CLK_IDLE_LOW, FT4222_SPICPHA.CLK_TRAILING, 0x01);
            if (ft42Status != FT4222_STATUS.FT4222_OK)
            {
                Console.WriteLine("Open NG: {0}", ft42Status);
                Console.WriteLine("NG! Press Enter to continue.");
                return;
            }
            else
            {
                Console.WriteLine("Init FT4222 SPI Master OK");
            }

            ft42Status = FT4222_SPI_SetDrivingStrength(ftHandle, SPI_DrivingStrength.DS_12MA, SPI_DrivingStrength.DS_12MA, SPI_DrivingStrength.DS_16MA);
            if (ft42Status != FT4222_STATUS.FT4222_OK)
            {
                Console.WriteLine("SetDrivingStrength NG: {0}", ft42Status);
                Console.WriteLine("NG! Press Enter to continue.");
                return;
            }
        }