SetLatency() публичный Метод

Sets the value of the latency timer. Default value is 16ms.
public SetLatency ( byte Latency ) : FT_STATUS
Latency byte 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.
Результат FT_STATUS
        //-------------------------------------------------------------------------------------------------
        public FTDI.FT_STATUS InitializeFTDI()
        {
            // Create new instance of the FTDI device class
             SPI_Device= new FTDI();
            uint ftdiDeviceCount = 0;

            int i;

            Initialize_SPI_Constants();

            // Determine the number of FTDI devices connected to the machine
            ftStatus = SPI_Device.GetNumberOfDevices(ref ftdiDeviceCount);

            // Check status
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                return (ftStatus);
            }

            // If no devices available, return
            if (ftdiDeviceCount == 0)
            {
                ftStatus = FTDI.FT_STATUS.FT_DEVICE_NOT_FOUND;
                return (ftStatus);
            }

            // Allocate storage for device info list
            FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount];

            // Populate our device list
            ftStatus = SPI_Device.GetDeviceList(ftdiDeviceList);

            if (ftStatus == FTDI.FT_STATUS.FT_OK)
            {
                for (i = 0; i < ftdiDeviceCount; i++)
                {
                    //MessageBox.Show("Device Index: " + i.ToString());
                    //MessageBox.Show("Flags: " + String.Format("{0:x}", ftdiDeviceList[i].Flags));
                    //MessageBox.Show("Type: " + ftdiDeviceList[i].Type.ToString());
                    //MessageBox.Show("ID: " + String.Format("{0:x}", ftdiDeviceList[i].ID));
                    //MessageBox.Show("Location ID: " + String.Format("{0:x}", ftdiDeviceList[i].LocId));
                    //MessageBox.Show("Serial Number: " + ftdiDeviceList[i].SerialNumber.ToString());
                    //MessageBox.Show("Description: " + ftdiDeviceList[i].Description.ToString());
                    //MessageBox.Show("");
                }
            }


            // Open first device in our list by serial number
            ftStatus = SPI_Device.OpenBySerialNumber(ftdiDeviceList[0].SerialNumber);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                return (ftStatus);
            }
            // Set latency timer
            ftStatus = SPI_Device.SetLatency(2);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                return (ftStatus);
            }

            // Reset the controller
            ftStatus = SPI_Device.SetBitMode(0, 0);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                return (ftStatus);
            }

            // Set synchronous bit bang mode
            ftStatus = SPI_Device.SetBitMode(FT232Routputs, 4);  // Set device to mode 4 and sets outputs
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                return (ftStatus);
            }

            // Set baud rate/bit clock settings
            ftStatus = SPI_Device.SetBaudRate(3000000);
            if (ftStatus != FTDI.FT_STATUS.FT_OK)
            {
                return (ftStatus);
            }

            presetShiftRegisterOutputs();

            return (ftStatus);
        }
Пример #2
0
        static void Main(string[] args)
        {
            string deviceSerialNumber = "33VRWQARA";
            FTDI.FT_STATUS status = new FTDI.FT_STATUS();
            FTDI device = new FTDI();
            UInt32 numberOfDevices = 0;
            int sleepTime = 100;

            status = device.GetNumberOfDevices(ref numberOfDevices);
            FTDI.FT_DEVICE_INFO_NODE[] devicelist = new FTDI.FT_DEVICE_INFO_NODE[numberOfDevices];
            status = device.GetDeviceList(devicelist);
            Thread.Sleep(sleepTime);
            if (status == FTDI.FT_STATUS.FT_OK)
                Console.WriteLine("We have {0} devices", numberOfDevices);
            else
                Console.WriteLine("Failed to get number of devices");

            status = device.OpenBySerialNumber(deviceSerialNumber);
            Thread.Sleep(sleepTime);
            if (status == FTDI.FT_STATUS.FT_OK)
                Console.WriteLine("Device {0} is opened", deviceSerialNumber);
            else
                Console.WriteLine("Failed to open {0} device", deviceSerialNumber);

            status = device.SetBitMode(0xff, FTDI.FT_BIT_MODES.FT_BIT_MODE_RESET);
            Thread.Sleep(sleepTime);
            if (status == FTDI.FT_STATUS.FT_OK)
                Console.WriteLine("BitMode is resetted");
            else
                Console.WriteLine("Failed to reset BitMode");

            status = device.SetBitMode(0xff, FTDI.FT_BIT_MODES.FT_BIT_MODE_SYNC_FIFO);
            Thread.Sleep(sleepTime);
            if (status == FTDI.FT_STATUS.FT_OK)
                Console.WriteLine("BitMode is {0}", FTDI.FT_BIT_MODES.FT_BIT_MODE_SYNC_FIFO);
            else
                Console.WriteLine("Failed to set BitMode");

            byte latency = 2;
            device.SetLatency(latency);
            Thread.Sleep(sleepTime);
            if (status == FTDI.FT_STATUS.FT_OK)
                Console.WriteLine("Latency timer value is {0}", latency);
            else
                Console.WriteLine("Failed to set latency");

            uint inTransferSize = 0x10000;
            device.InTransferSize(inTransferSize);
            Thread.Sleep(sleepTime);
            if (status == FTDI.FT_STATUS.FT_OK)
                Console.WriteLine("inTransferSize value is {0}", inTransferSize);
            else
                Console.WriteLine("Failed to set inTransferSize");

            device.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x00, 0x00);
            Thread.Sleep(sleepTime);
            if (status == FTDI.FT_STATUS.FT_OK)
                Console.WriteLine("FlowControl is {0}", FTDI.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS);
            else
                Console.WriteLine("Failed to set FlowControl");

            device.Purge(FTDI.FT_PURGE.FT_PURGE_RX);

            using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                using (BinaryReader br = new BinaryReader(fs))
                {
                    int chunkLendth = 0x10000;
                    byte[] chunk;
                    uint numBytesWritten = 0;
                    //uint TxQueue = 0;

                    while (((chunk = br.ReadBytes(chunkLendth)).Length > 0) && (Console.KeyAvailable == false))
                    {
                        //status = device.GetTxBytesWaiting(ref TxQueue);
                        //if (status != FTDI.FT_STATUS.FT_OK)
                        //    Console.WriteLine("status!=ok");
                        //while (TxQueue != 0)
                        //    status = device.GetTxBytesWaiting(ref TxQueue);

                        status = device.Write(chunk, chunk.Length, ref numBytesWritten);
                        if (numBytesWritten != chunk.Length)
                            Console.WriteLine("Error writting to the device,\r\nchunk.Length={0}\r\nnumBytesWritten={1}",
                                chunk.Length, numBytesWritten);
                    }
                }
            }
            Console.WriteLine("Key is pressed, end of file writting");
        }
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="iSoundChipType"></param>
        /// <param name="clock"></param>
        /// <returns></returns>
        public static VsifClient TryToConnectVSIF(VsifSoundModuleType soundModule, PortId comPort, bool shareOnly)
        {
            lock (lockObject)
            {
                foreach (var c in vsifClients)
                {
                    if (c.SoundModuleType == soundModule)
                    {
                        if (c.DataWriter.PortName.Equals("COM" + (int)(comPort + 1)))
                        {
                            c.ReferencedCount++;
                            return(c);
                        }
                        if (c.DataWriter.PortName.Equals("FTDI_COM" + (int)comPort))
                        {
                            c.ReferencedCount++;
                            return(c);
                        }
                    }
                }
                if (shareOnly)
                {
                    return(null);
                }

                try
                {
                    switch (soundModule)
                    {
                    case VsifSoundModuleType.Generic_UART:
                    {
                        SerialPort sp = null;
                        sp = new SerialPort("COM" + ((int)comPort + 1));
                        sp.WriteTimeout = 100;
                        sp.ReadTimeout  = 100;
                        sp.BaudRate     = 115200;
                        sp.BaudRate     = 1200 * (int)Settings.Default.BitBangWaitAY8910;
                        sp.StopBits     = StopBits.One;
                        sp.DataBits     = 8;
                        sp.Handshake    = Handshake.None;
                        sp.Open();
                        var client = new VsifClient(soundModule, new PortWriterGenericUart(sp));
                        client.Disposed += Client_Disposed;
                        vsifClients.Add(client);
                        return(client);
                    }

                    case VsifSoundModuleType.SMS:
                    {
                        SerialPort sp = null;
                        sp = new SerialPort("COM" + ((int)comPort + 1));
                        sp.WriteTimeout = 100;
                        sp.ReadTimeout  = 100;
                        sp.BaudRate     = 115200;
                        sp.StopBits     = StopBits.Two;
                        //sp.BaudRate = 57600;
                        //sp.StopBits = StopBits.One;
                        sp.Parity    = Parity.None;
                        sp.DataBits  = 8;
                        sp.Handshake = Handshake.None;
                        sp.Open();
                        var client = new VsifClient(soundModule, new PortWriterGenesis(sp));
                        client.Disposed += Client_Disposed;
                        vsifClients.Add(client);
                        return(client);
                    }

                    case VsifSoundModuleType.Genesis:
                    {
                        SerialPort sp = null;
                        sp = new SerialPort("COM" + ((int)comPort + 1));
                        sp.WriteTimeout = 500;
                        sp.ReadTimeout  = 500;
                        //sp.BaudRate = 230400;
                        sp.BaudRate = 163840;
                        sp.StopBits = StopBits.One;
                        sp.Parity   = Parity.None;
                        sp.DataBits = 8;
                        sp.Open();
                        var client = new VsifClient(soundModule, new PortWriterGenesis(sp));
                        client.Disposed += Client_Disposed;
                        vsifClients.Add(client);
                        return(client);
                    }

                    case VsifSoundModuleType.Genesis_Low:
                    {
                        SerialPort sp = null;
                        sp = new SerialPort("COM" + ((int)comPort + 1));
                        sp.WriteTimeout = 500;
                        sp.ReadTimeout  = 500;
                        sp.BaudRate     = 115200;
                        sp.StopBits     = StopBits.One;
                        sp.Parity       = Parity.None;
                        sp.DataBits     = 8;
                        sp.Open();
                        var client = new VsifClient(soundModule, new PortWriterGenesis(sp));
                        client.Disposed += Client_Disposed;
                        vsifClients.Add(client);
                        return(client);
                    }

                    case VsifSoundModuleType.Genesis_FTDI:
                    {
                        var ftdi = new FTD2XX_NET.FTDI();
                        var stat = ftdi.OpenByIndex((uint)comPort);
                        if (stat == FTDI.FT_STATUS.FT_OK)
                        {
                            ftdi.SetBitMode(0x00, FTDI.FT_BIT_MODES.FT_BIT_MODE_RESET);
                            ftdi.SetBitMode(0xff, FTDI.FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG);
                            ftdi.SetBaudRate(FTDI_BAUDRATE_GEN * FTDI_BAUDRATE_GEN_MUL);
                            ftdi.SetTimeouts(500, 500);
                            ftdi.SetLatency(0);
                            byte ps = 0;
                            ftdi.GetPinStates(ref ps);
                            if ((ps & 0x40) == 0)
                            {
                                uint dummy = 0;
                                ftdi.Write(new byte[] { 0x40 }, 1, ref dummy);
                            }

                            var client = new VsifClient(soundModule, new PortWriterGenesis(ftdi, comPort));

                            //ftdi.Write(new byte[] { (byte)(((0x07 << 1) & 0xe) | 0) }, 1, ref dummy);
                            //ftdi.Write(new byte[] { (byte)(((0x38 >> 2) & 0xe) | 1) }, 1, ref dummy);
                            //ftdi.Write(new byte[] { (byte)(((0xC0 >> 5) & 0xe) | 0) }, 1, ref dummy);
                            //ftdi.Write(new byte[] { 1 }, 1, ref dummy);

                            client.Disposed += Client_Disposed;
                            vsifClients.Add(client);
                            return(client);
                        }
                    }
                    break;

                    case VsifSoundModuleType.MSX_FTDI:
                    {
                        var ftdi = new FTD2XX_NET.FTDI();
                        var stat = ftdi.OpenByIndex((uint)comPort);
                        if (stat == FTDI.FT_STATUS.FT_OK)
                        {
                            ftdi.SetBitMode(0x00, FTDI.FT_BIT_MODES.FT_BIT_MODE_RESET);
                            ftdi.SetBitMode(0xff, FTDI.FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG);
                            ftdi.SetBaudRate(FTDI_BAUDRATE_MSX * FTDI_BAUDRATE_MSX_MUL);
                            ftdi.SetTimeouts(500, 500);
                            ftdi.SetLatency(0);
                            //{
                            //    uint dummy = 0x00;
                            //    ftdi.Write(new byte[] { (byte)dummy }, 1, ref dummy);
                            //}

                            var client = new VsifClient(soundModule, new PortWriterMsx(ftdi, comPort));

                            client.Disposed += Client_Disposed;
                            vsifClients.Add(client);
                            return(client);
                        }
                    }
                    break;

                    case VsifSoundModuleType.NES_FTDI_INDIRECT:
                    case VsifSoundModuleType.NES_FTDI_DIRECT:
                    {
                        var ftdi = new FTD2XX_NET.FTDI();
                        var stat = ftdi.OpenByIndex((uint)comPort);
                        if (stat == FTDI.FT_STATUS.FT_OK)
                        {
                            ftdi.SetBitMode(0x00, FTDI.FT_BIT_MODES.FT_BIT_MODE_RESET);
                            ftdi.SetBitMode(0xff, FTDI.FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG);
                            ftdi.SetBaudRate(FTDI_BAUDRATE_NES * FTDI_BAUDRATE_NES_MUL);
                            ftdi.SetTimeouts(500, 500);
                            ftdi.SetLatency(0);
                            byte ps = 0;
                            ftdi.GetPinStates(ref ps);
                            if ((ps & 0x10) == 0x10)
                            {
                                uint dummy = 0;
                                ftdi.Write(new byte[] { 0x00 }, 1, ref dummy);
                            }

                            VsifClient client = null;
                            switch (soundModule)
                            {
                            case VsifSoundModuleType.NES_FTDI_DIRECT:
                                client = new VsifClient(soundModule, new PortWriterNesDirect(ftdi, comPort));
                                break;

                            case VsifSoundModuleType.NES_FTDI_INDIRECT:
                                client = new VsifClient(soundModule, new PortWriterNesIndirect(ftdi, comPort));
                                break;
                            }

                            client.Disposed += Client_Disposed;
                            vsifClients.Add(client);
                            return(client);
                        }
                    }
                    break;
                    }

                    //sp.Write(new byte[] { (byte)'M', (byte)'a', (byte)'M', (byte)'i' }, 0, 4);
                    //sp.BaseStream.WriteByte((byte)soundModule);
                    //Thread.Sleep(100);
                    //var ret = sp.BaseStream.ReadByte();
                    //if (ret == 0x0F)   //OK
                }
                catch (Exception ex)
                {
                    if (ex.GetType() == typeof(Exception))
                    {
                        throw;
                    }
                    else if (ex.GetType() == typeof(SystemException))
                    {
                        throw;
                    }
                }
                return(null);
            }
        }
Пример #4
0
        static void Main(string[] args)
        {
            string deviceSerialNumber = "33VRWQARA";
            FTDI.FT_STATUS status = new FTDI.FT_STATUS();
            FTDI device = new FTDI();
            UInt32 numberOfDevices = 0;
            int sleepTime = 100;

            status = device.GetNumberOfDevices(ref numberOfDevices);
            FTDI.FT_DEVICE_INFO_NODE[] devicelist = new FTDI.FT_DEVICE_INFO_NODE[numberOfDevices];
            status = device.GetDeviceList(devicelist);
            Thread.Sleep(sleepTime);
            if (status == FTDI.FT_STATUS.FT_OK)
                Console.WriteLine("We have {0} devices", numberOfDevices);
            else
                Console.WriteLine("Failed to get number of devices");

            status = device.OpenBySerialNumber(deviceSerialNumber);
            Thread.Sleep(sleepTime);
            if(status == FTDI.FT_STATUS.FT_OK)
                Console.WriteLine("Device {0} is opened", deviceSerialNumber);
            else
                Console.WriteLine("Failed to open {0} device", deviceSerialNumber);

            status = device.SetBitMode(0xff, FTDI.FT_BIT_MODES.FT_BIT_MODE_RESET);
            Thread.Sleep(sleepTime);
            if (status == FTDI.FT_STATUS.FT_OK)
                Console.WriteLine("BitMode is resetted");
            else
                Console.WriteLine("Failed to reset BitMode");

            status = device.SetBitMode(0xff, FTDI.FT_BIT_MODES.FT_BIT_MODE_SYNC_FIFO);
            Thread.Sleep(sleepTime);
            if (status == FTDI.FT_STATUS.FT_OK)
                Console.WriteLine("BitMode is {0}", FTDI.FT_BIT_MODES.FT_BIT_MODE_SYNC_FIFO);
            else
                Console.WriteLine("Failed to set BitMode");

            byte latency = 2;
            device.SetLatency(latency);
            Thread.Sleep(sleepTime);
            if (status == FTDI.FT_STATUS.FT_OK)
                Console.WriteLine("Latency timer value is {0}", latency);
            else
                Console.WriteLine("Failed to set latency");

            uint inTransferSize = 0x10000;
            device.InTransferSize(inTransferSize);
            Thread.Sleep(sleepTime);
            if (status == FTDI.FT_STATUS.FT_OK)
                Console.WriteLine("inTransferSize value is {0}", inTransferSize);
            else
                Console.WriteLine("Failed to set inTransferSize");

            device.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS, 0x00, 0x00);
            Thread.Sleep(sleepTime);
            if (status == FTDI.FT_STATUS.FT_OK)
                Console.WriteLine("FlowControl is {0}", FTDI.FT_FLOW_CONTROL.FT_FLOW_RTS_CTS);
            else
                Console.WriteLine("Failed to set FlowControl");

            device.Purge(FTDI.FT_PURGE.FT_PURGE_RX);

            uint numBytes = 0;
            //int cycles = 0;
            using(FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (BinaryWriter bw = new BinaryWriter(fs))
                {
                    while (Console.KeyAvailable == false)
                    {
                        device.GetRxBytesAvailable(ref numBytes);
                        if (numBytes >= 1)
                        {
                            //cycles++;
                            byte[] bBuf = new byte[numBytes];
                            device.Read(bBuf, numBytes, ref numBytes);
                            bw.Write(bBuf);

                            //if (cycles == 1)
                            //{
                            //    cycles = 0;
                            //    Console.WriteLine("{0}", bBuf.Length);
                            //}
                        }
                        if (numBytes >= 0x10000)
                        {
                            Console.WriteLine("Buffer overload!");
                        }
                    }

                    //Console.WriteLine("Press 'p' to erase control bytes, 'q' to quite");
                    //ConsoleKeyInfo cki = Console.ReadKey(false);
                    //if (cki.Key == ConsoleKey.Q)
                    //    Environment.Exit(-1);
                    //if (cki.Key == ConsoleKey.P)
                    //{

                    //}
                }
            }
            Console.WriteLine("Key is pressed, end of file writting");
        }