예제 #1
0
        private void GetEndpointsOfNode(TreeNode devTree)
        {
            foreach (TreeNode node in devTree.Nodes)
            {
                if (node.Nodes.Count > 0)
                {
                    GetEndpointsOfNode(node);
                }
                else
                {
                    CyControlEndPoint ctrlEp = node.Tag as CyControlEndPoint;
                    CyBulkEndPoint    bulkEp = node.Tag as CyBulkEndPoint;

                    if (ctrlEp != null)
                    {
                        controlEndPoint = ctrlEp;
                    }

                    if (bulkEp != null && bulkEp.bIn)
                    {
                        bulkInEndPoint = bulkEp;
                        CyUSBInterface ifc = node.Parent.Tag as CyUSBInterface;
                        if (ifc != null)
                        {
                            cyDevice.AltIntfc = ifc.bAlternateSetting;
                        }
                    }
                }
            }
        }
예제 #2
0
        public override bool Recover()
        {
            try
            {
                if (loopDevice != null)
                {
                    loopDevice.Dispose();
                    loopDevice = null;
                }

                if (usbDevices != null)
                {
                    usbDevices.DeviceRemoved  -= usbDevices_DeviceRemoved;
                    usbDevices.DeviceAttached -= usbDevices_DeviceAttached;
                    usbDevices.Dispose();
                    usbDevices = null;
                }
            }
            catch (System.Exception ex)
            {
            }


            inEndpoint  = null;
            outEndpoint = null;

            usbDevices = new USBDeviceList(CyConst.DEVICES_CYUSB);
            usbDevices.DeviceRemoved  += new EventHandler(usbDevices_DeviceRemoved);
            usbDevices.DeviceAttached += new EventHandler(usbDevices_DeviceAttached);
            return(intDevice());
        }
예제 #3
0
 void usbDevices_DeviceRemoved(object sender, EventArgs e)
 {
     MyDevice   = null;
     inEndpoint = null;
     SetDevice();
     openPort();
 }
예제 #4
0
        public bool intDevice()
        {
            // 可能抛出“无法访问已释放的对象”的异常
            try { loopDevice = usbDevices[0x04b4, 0x3031] as CyUSBDevice; }
            catch { loopDevice = null; }
            //PID 0x1004 is used for cardio
            //if (loopDevice == null) loopDevice = usbDevices[0x04b4, 0x1004] as CyUSBDevice;

            if (loopDevice != null)
            {
                try
                {
                    usbName             = loopDevice.FriendlyName;
                    outEndpoint         = loopDevice.EndPointOf(0x02) as CyBulkEndPoint;
                    inEndpoint          = loopDevice.EndPointOf(0x86) as CyBulkEndPoint;
                    outEndpoint.TimeOut = 500;
                    inEndpoint.TimeOut  = 1000;
                    _isUSBAvailable     = true;
                    InitializeParams();
                    PreReadAsysnchonous();
                }
                catch (Exception ex)
                {
                    return(false);
                }
                return(true);
            }
            else
            {
                _isUSBAvailable = false;
                return(false);
            }
        }
예제 #5
0
        private void SetDevice()
        {
            USBDevice dev = usbDevices[46084, 4099];

            if (dev != null)
            {
                cyDevice        = (CyUSBDevice)dev;
                controlEndPoint = null;
                bulkInEndPoint  = null;

                GetEndpointsOfNode(cyDevice.Tree);
                if (controlEndPoint != null)
                {
                    isReady = true;

                    name     = dev.FriendlyName;
                    version  = GetFirmwareVer();
                    revision = GetRevision();

                    DeviceReadyEventArgs args = new DeviceReadyEventArgs();
                    args.DeviceName = name;
                    args.VidPid     = new[] { dev.VendorID, dev.ProductID };
                    args.Version    = version;
                    args.Revision   = revision;

                    DeviceReady?.Invoke(this, args);
                }
            }
            else
            {
                isReady = false;
                DeviceNotReady?.Invoke(this, null);
            }
        }
예제 #6
0
        public Fx2Device(CyUSBDevice usbDevice, MonoUsbProfile usbProfile, EDeviceType deviceType = EDeviceType.Unknown)
        {
            if (usbDevice != null)
            {
                USBDevice = usbDevice;
            }
            if (usbProfile != null)
            {
                USBProfile = usbProfile;
            }
            DeviceType = deviceType;

            if (deviceType == EDeviceType.Unknown)
            {
                Console.WriteLine($"+ {this}");
            }

            if (deviceType == EDeviceType.DAC_SA || deviceType == EDeviceType.ADC_SA)
            {
                if (usbDevice != null)
                {
                    CyBulkEndPoint outEndpoint = usbDevice.EndPointOf(0x01) as CyBulkEndPoint;
                    CyBulkEndPoint inEndpoint  = usbDevice.EndPointOf(0x81) as CyBulkEndPoint;
                    avalonPacket = new AvalonPacket(outEndpoint, inEndpoint);
                }
                else
                {
                    avalonPacket = new MonoAvalonPacket(MonoDeviceHandle);
                }
            }
        }
예제 #7
0
        public bool ReadFWCommand(ref byte[] inBuffer)
        {
            bool retValue = false;

            try
            {
                int buffCnt = 512;
                m_USBDevices = new USBDeviceList(CyConst.DEVICES_CYUSB);
                FxDev        = m_USBDevices[0] as CyUSBDevice;
                bulkEpt      = FxDev.BulkOutEndPt;
                retValue     = bulkEpt.XferData(ref inBuffer, ref buffCnt, false);

                Thread.Sleep(50);

                bulkEpt = FxDev.BulkInEndPt;
                if (bulkEpt != null)
                {
                    inBuffer = new byte[512];
                    retValue = bulkEpt.XferData(ref inBuffer, ref buffCnt, false);
                }
            }
            catch (Exception ex)
            {
                retValue = false;
            }
            return(retValue);
        }
예제 #8
0
        void usbDevices_DeviceRemoved(object sender, EventArgs e)
        {
            bRunning = false;

            if (!ThreadData.IsAddingCompleted)
            {
                ThreadData.CompleteAdding();
            }

            MyDevice        = null;
            BulkInEndPoint  = null;
            BulkOutEndPoint = null;
            RefreshDeviceLists(false);
            RefreshEndPoint();
            tbxInfo.AppendText("USB Disconnect\n");
            lblUsbName.Content       = "C# USB - no device";
            lblUsbName.Foreground    = Brushes.Red;
            btnStartAcq.IsEnabled    = false;
            btnCmdSend.IsEnabled     = false;
            btnStartAcq.Content      = "Start";
            btnSetUsb.Content        = "Set";
            btnStartAcq.Background   = new SolidColorBrush(Color.FromRgb(27, 129, 62));
            cbxDeviceLists.IsEnabled = true;
            btnSetUsb.IsEnabled      = true;
            cbxInEndPoint.IsEnabled  = true;
            cbxOutEndPoint.IsEnabled = true;
            cbxPpx.IsEnabled         = true;
            cbxXferQueue.IsEnabled   = true;
        }
예제 #9
0
        /*
         * // This one must be corrected, cause trouble
         * public static bool TE_USB_FX2_DisplayDriverInformation(ref CyUSBDevice TE_USB_FX2_USBDevice, ref USBDeviceList USBdevList, int CardNumber)
         * {
         *
         * int CardCounted = 0;   // Trenz Board
         * int DeviceNumber = 0;  // Cypress Board ( number >= TrenzBoard)
         *
         * int CypressDeviceNumber = 0;
         * int TrenzDeviceNumber = 0;
         *
         * UInt16 PID = 0x0000;
         * UInt16 VID = 0x0000;
         *
         * uint DriverVersion1 = 0;
         *
         * string DriverName1 = null;
         *
         * //Creation of a list of USB device that use the CYUSB.SYS driver
         * USBdevList = new USBDeviceList(CyConst.DEVICES_CYUSB);
         * //If exist at least an USB device that use the CYUSB.SYS driver,
         * //I search and count the number of these devices that are of Trenz Electronic
         * if (USBdevList.Count != 0)
         * {
         *  foreach (USBDevice USBdev in USBdevList)
         *  {
         *    PID = USBdev.ProductID;
         *    VID = USBdev.VendorID;
         *    //Number of Cypress Card augmented by one
         *    CypressDeviceNumber++;
         *    if ((((PID == 0x0300) && (VID == 0x0bd0)) == true))  //0x0bd0 . 0x0bd0
         *    {
         *      //Number of Trenz Card (a subcategory of Cypress Card) augmented by one
         *      //CardCount++;
         *      TrenzDeviceNumber++;
         *      Console.WriteLine("PID e VID: {0}, {1}", PID, VID);
         *      // CardNumber=TrenzDeviceNumber-1 by definition.
         *      if ((TrenzDeviceNumber - 1) == CardNumber)
         *      {
         *        //I store the DeviceNumber that identify the Trenz Card (CardNumber) requested
         *        DeviceNumber = CypressDeviceNumber - 1;
         *        Console.WriteLine("DeviceNumber: {0}", DeviceNumber);
         *      }
         *    }
         *  }
         * }
         *
         * //At this point I memorize the Cards Counted and zeroed the variable that I have used in the counting.
         * CardCounted = TrenzDeviceNumber;
         * //Console.WriteLine("CardCounted: {0}", CardCount);
         * TrenzDeviceNumber = 0;
         * //Now I search the Trenz USB Device with the Card Number (CardNo) specified
         * if (((CardNumber >= 0) && (CardNumber < CardCounted)) == true)  //CardCounted
         * {
         *  USBDevice USBdev = USBdevList[DeviceNumber];
         *  PID = USBdev.ProductID;
         *  VID = USBdev.VendorID;
         *  if ((((PID == 0x0300) && (VID == 0x0bd0)) == true))
         *  {
         *    TE_USB_FX2_USBDevice = USBdev as CyUSBDevice;
         *    Console.WriteLine("USBdev {0} ", TE_USB_FX2_USBDevice);
         *
         *    //I cast the abstract USBdev in a concrete CyUSBDevice
         *    TE_USB_FX2_USBDevice = USBdev as CyUSBDevice;
         *    DriverVersion1 = TE_USB_FX2_USBDevice.DriverVersion;
         *    Console.WriteLine("DriverVersion {0} ", DriverVersion1);
         *    DriverName1 = TE_USB_FX2_USBDevice.DriverName;
         *    Console.WriteLine("Original Name of the Driver {0} ", DriverName1);
         *
         *    return true;
         *  }
         *  else
         *  {
         *    TE_USB_FX2_USBDevice = null;
         *    return false;
         *  }
         * }
         * else
         * {
         *  TE_USB_FX2_USBDevice = null;
         *  return false;
         * }
         * }
         */


        /*
         *
         * 3.4   TE_USB_FX2_SendCommand()
         *
         * 3.4.1   Declaration
         * public static bool TE_USB_FX2_SendCommand(ref CyUSBDevice TE_USB_FX2_USBDevice, ref byte[] Command, ref int CmdLength,
         * ref byte[] Reply, ref int ReplyLength, uint Timeout)
         *
         * 3.4.2   Function Call
         * Your application program shall call this function like this:
         * TE_USB_FX2.TE_USB_FX2.TE_USB_FX2_SendCommand (ref TE_USB_FX2_USBDevice, ref Command, ref CmdLength, ref Reply,
         * ref ReplyLength, Timeout);
         *
         * 3.4.3   Description
         * This function takes an already initialized USB device (previously selected by TE_USB_FX2_Open()) and sends a command
         *(API command) to the USB FX2 microcontroller (USB FX2 API command) or to the MicroBlaze embedded processor
         *(MicroBlaze API command) through the USB FX2 microcontroller endpoint EP1 buffer.
         * This function is normally used to send 64 bytes packets to the USB endpoint EP1 (0x01).
         * This function is also able to obtain the response of the USB FX2 microcontroller or MicroBlaze embedded processor through
         * the USB FX2 microcontroller endpoint EP1 (0x81).
         * 3.4.4   Parameters
         * 1. ref CyUSBDevice TE_USB-FX2_USBDevice
         * CyUSBDevice is a type defined in CyUSB.dll. This parameter points to the module selected by TE_USB_FX2_Open().
         * This parameter is passed by reference (ref). See pages 70-93 of CyUSB.NET.pdf (Cypress CyUSB .NET DLL Programmer's Reference)
         * 2. ref byte[] Command
         * This parameter is passed by reference (ref). It is the byte array that contains the commands to send to USB FX2 microcontroller
         *(FX2_Commands) or to the MicroBlaze embedded processor (MB_Commands).
         * The byte array shall be properly initialized using instructions similar to the following ones:
         * Command[0] = (byte)FX2_Commands.I2C_WRITE;
         * Command[1] = (byte)FX2_Commands.MB_I2C_ADDRESS;
         * Command[2] = (byte)FX2_Commands.I2C_BYTES;
         * Command[3] = (byte)0;
         * Command[4] = (byte)0;
         * Command[5] = (byte)0;
         * Command[6] = (byte)Command2MB;
         * 3. ref int CmdLength
         * This parameter (passed by reference (ref)) is the length (in bytes) of the previous byte array; it is the length of the
         * packet to transmit to USB FX2 controller endpoint EP1 (0x01). It is typically initialized to 64 bytes.
         * 4. ref byte[] Reply
         * This parameter (passed by reference (ref)) is the byte array that contains the response to the command sent to the
         * USB FX2 microcontroller (FX2_Commands) or to the MicroBlaze embedded processor (MB_Commands).
         * 5. ref int ReplyLength
         * This parameter (passed by reference (ref)) is the length (in bytes) of the previous byte array; it is the length of
         * the packet to transmit to the USB FX2 microcontroller endpoint EP1 (0x81). It is typically initialized to 64 byes,
         * but normally the meaningful bytes are less. The parameter is a reference, meaning that the method can modify its value.
         * The number of bytes actually received is passed back in ReplyLength.
         * 6. uint Timeout
         * The unsigned integer value is the time in milliseconds assigned to the synchronous method XferData() of data transfer used
         * by CyUSB.dll.
         * Timeout is the time that is allowed to the function for sending/receiving the data packet passed to the function;
         * this timeout shall be large enough to allow the data/command transmission/reception. Otherwise the transmission/reception will fail. See 1.1.2 Timeout Setting.
         * 3.4.5   Return Value
         * 1. bool : logical type
         * This function returns true if it is able to send a command to EP1 and  receive a response within 2*Timeout milliseconds.
         * This function returns false otherwise.
         *
         */

        /// <summary>
        /// /////
        /// </summary>
        /// <param name="TE03xxUSBdevice"></param>
        /// <param name="Command"></param>
        /// <param name="CmdLength"></param>
        /// <param name="Reply"></param>
        /// <param name="ReplyLength"></param>
        /// <param name="Timeout"></param>
        /// <returns></returns>

        public static bool TE_USB_FX2_SendCommand(ref CyUSBDevice TE_USB_FX2_USBDevice, ref byte[] Command, ref int CmdLength,
                                                  ref byte[] Reply, ref int ReplyLength, uint Timeout)
        {
            bool bResultCommand = false;
            bool bResultReply   = false;


            //Concrete class
            CyBulkEndPoint inEndpoint1  = null;
            CyBulkEndPoint outEndpoint1 = null;

            if (TE_USB_FX2_USBDevice != null)
            {
                //CyBulkEndPoint TE03xxUSBDeviceConcrete = TE03xxUSBdevice as CyBulkEndPoint;
                //Select the endpoint of IN number 1 (EP1 INPUT)
                inEndpoint1 = TE_USB_FX2_USBDevice.EndPointOf(0x81) as CyBulkEndPoint;
                //Select the endpoint of OUT number 1 (EP1 OUTPUT)
                outEndpoint1 = TE_USB_FX2_USBDevice.EndPointOf(0x01) as CyBulkEndPoint;

                // Set the timeout
                outEndpoint1.TimeOut = Timeout;
                inEndpoint1.TimeOut  = Timeout;

                //calls the XferData function for bulk transfer(OUT) in the cyusb.dll
                bResultCommand = outEndpoint1.XferData(ref Command, ref CmdLength);
                //Console.WriteLine("bResultCommand  {0} ", bResultCommand);
                //Console.WriteLine("Command[0] {0:X2} ", Command[0]);
                //Console.WriteLine("CmdLength {0} ", CmdLength);

                //uint inUSBstatus1 = inEndpoint1.UsbdStatus;
                //Console.WriteLine("UsbdStatus {0:X8} e ", inUSBstatus1);

                //uint inUSBstatus2 = inEndpoint1.NtStatus;
                //Console.WriteLine("NtStatus {0:X8} e ", inUSBstatus2);

                if (bResultCommand == true)
                {
                    //calls the XferData function for bulk transfer(IN) in the cyusb.dll
                    bResultReply = inEndpoint1.XferData(ref Reply, ref ReplyLength);
                    //Console.WriteLine("bResultReply  {0}  ", bResultReply);
                }
                else
                {
                    return(false);
                }
                if ((bResultCommand && bResultReply) == true)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
예제 #10
0
 private bool CommandSend(byte[] CmdBytes, CyBulkEndPoint usbBulkOutEndPoint, bool IsSuperSpeed, bool IsLittleEndian)
 {
     byte[] CmdBytesToUsb = CommandLengthCheck(CmdBytes, IsSuperSpeed);
     if (IsLittleEndian)
     {
         CmdBytesToUsb = CommandEndianChange(CmdBytesToUsb, IsSuperSpeed);
     }
     return(CommandSend(CmdBytesToUsb, usbBulkOutEndPoint));
 }
예제 #11
0
        /*
         * 3.6   TE_USB_FX2_SetData()
         *
         * 3.6.1   Declaration
         * public static bool TE_USB_FX2_SetData(ref CyUSBDevice TE_USB_FX2_USBDevice, ref byte[] DataWrite, ref int DataWriteLength,
         * int PipeNo, uint Timeout, int BufferSize)
         *
         * 3.6.2   Function Call
         * Your application program shall call this function like this:
         * TE_USB_FX2.TE_USB_FX2.TE_USB_FX2_SetData(ref TE_USB_FX2_USBDevice, ref DataWrite, ref DataWriteLength, PI_EP8, Timeout,
         * BufferSize);
         *
         * 3.6.3   Description
         * This function takes an already initialized USB device (CyUSBDevice is a type defined in CyUSB.dll), selected by
         * TE_USB_FX2_Open(), and writes data to the USB FX2 microcontroller endpoint EP8 (0x08). This data is then passed to the FPGA.
         * If there is not a proper connection (not using FX22MB_REG0_START_RX) between FPGA and USB FX2 microcontroller,
         * the function can experience a strange behavior. For example, a very low throughput (9-10 Mbyte/s even if a 22-24 Mbyte/s are
         * expected) is measured or the function fails returning false. These happen because buffer EP8 (the HW buffer, not the
         * SW buffer of the driver whose size is given by BufferSize parameter) is already full (it is not properly read/emptied by the
         * FPGA) and no longer able to receive further packets.
         * 3.6.4   Data throughput expected
         * The maximum data throughput expected (with a DataWriteLength= 120*10^6) is 24 Mbyte/s (PacketSize = BufferSize =131,072)
         * but in fact this value is variable between 22-24 Mbyte/s (the mean value seems 24 Mbyte/s); so if you measure this range
         * of values, the data reception can be considered normal.
         * The data throughput is variable in two way:
         * 1. depends on which host computer is used (on some host computers this value is even higher: 29 Mbyte/s)
         * 2. vary with every function call
         * 3.6.5   DataWrite size shall not be too large
         * TE_USB_FX2_SetData() seems unable to use too large arrays or, more precisely, this fact seems variable by changing host
         * computer. To be safe, do not try to transfer in a single packet very large data (120 millions of byte); transfer the same
         * data with many packets (1,200 packets * 100,000 byte) and copy the data in a single large data array if necessary (with
         * Buffer.BlockCopy()). Buffer.BlockCopy seems not to hinder throughput too much (max 2 Mbyte/s).
         * 3.6.5.1   Reduced version (pseudo code)
         * PACKETLENGTH=100000;
         * packets=1200;
         * byte[] data = new byte[packetlen*packets];
         * byte[] buffer = new byte[packetlen];
         * for (int i = 0; i < packets; i++)
         * {
         *   Buffer.BlockCopy(data, total_cnt, buffer, 0, packetlen);
         *   TE_USB_FX2_SetData(ref TE_USB_FX2_USBDevice, ref buffer, ref   packetlen, PI_EP8,  TIMEOUT_MS,BUFFER_SIZE);
         *   total_cnt += packetlen;
         * }
         * 3.6.5.2   Expanded version (code)
         * SendFPGAcommand(ref TE_USB_FX2_USBDevice, MB_Commands.FX22MB_REG0_START_RX, TIMEOUT_MS);
         * //ElapsedTime.Start(); //StopWatch start
         * Stopwatch stopWatch = new Stopwatch();
         * stopWatch.Start();
         * for (int i = 0; i < packets; i++)
         * {
         *   packetlen = PACKETLENGTH;
         *   Buffer.BlockCopy(data, total_cnt, buffer, 0, packetlen);
         *   if (TE_USB_FX2.TE_USB_FX2.TE_USB_FX2_SetData(ref  TE_USB_FX2_USBDevice, ref buffer, ref packetlen, PI_EP8, TIMEOUT_MS,
         *   BUFFER_SIZE) == false) errors++;
         *   else total_cnt += packetlen;
         * }
         * //total_cnt += (packetlen * packets);
         * stopWatch.Stop();
         * 3.6.6   DataWrite size shall not be too small
         * The  reason is described in section 1.1.4 PacketSize.
         * PacketSize has also a strong influence on DataThroughput. If PacketSize is too small (512 byte for example) you can achieve
         * very low data throughput (2.2 Mbyte/s) even if you use a large driver buffer (driver buffer size = 131,072 byte).
         * See 6 TE_USB_FX2_CyUSB.dll:  Data Transfer Throughput Optimization.
         *
         * 3.6.7   Parameters
         * 1. ref CyUSBDevice TE_USB-FX2_USBDevice
         * This parameter is passed by reference (ref). It points to the module selected by TE_USB_FX2_Open(). See pages 70-93 of
         * CyUSB.NET.pdf (Cypress CyUSB .NET DLL Programmer's Reference)
         * 2. ref byte[] DataWrite
         * This parameter is passed by reference (ref). C# applications use directly TE_USB_FX2_CyUSB.dll based on CyUSB.dll. To avoid
         * copying back and forth large amount of data between these two DLLs, data is passed by reference and not by value.
         * This parameter points to the byte array that contains the data to be written to buffer EP8 (0x08) of USB FX2 microcontroller.
         * Data contained in EP8 are then read by the FPGA.
         * 3. ref int DataWriteLength
         * This parameter is passed by reference (ref). This parameter is the length (in bytes) of the previous byte array;
         * it is the length of the packet read from FX2 USB endpoint EP6 (0x86). Normally it is PacketLength.
         * 4. int PipeNumber
         * This parameter is the value that identify the endpoint used for the data transfer. It is called PipeNumber because it
         * identifies the buffer (pipe) used by the USB FX2 microcontroller.
         * 5. uint Timeout.
         * The unsigned integer value is the time in milliseconds assigned to the synchronous method XferData() of data transfer used by
         * CyUSB.dll.
         * Timeout is the time that is allowed to the function for sending/receiving the data packet passed to the function; this
         * timeout shall be large enough to allow the data/command transmission/reception. Otherwise the transmission/reception will
         * fail. See 1.1.2 Timeout Setting.
         * 6. int BufferSize
         * The integer value is the dimension (in bytes) of the driver buffer (SW) used in data transmission of a single endpoint
         * (EP8 0x08 in this case); the total buffer size is the sum of all BufferSize of every endpoint used.
         * The BufferSize has a strong influence on DataThroughput. If  BufferSize is too small, DataThroughput can be 1/3-1/2 of
         * the maximum value (from a maximum value of 24 Mbyte/s for write transactions to an actual value of 14 Mbyte/s).
         * If BufferSize has a large value (a roomy buffer), the program shall be able to cope with the non-deterministic behavior of
         * C# without losing packets.
         *
         * 3.6.8   Return Value
         * 1. bool: logical type
         * This function returns true if it is able to write data to buffer EP8 within Timeout milliseconds.
         * This function returns false otherwise.
         *
         */

        public static bool TE_USB_FX2_SetData(ref CyUSBDevice TE_USB_FX2_USBDevice, ref byte[] DataWrite, ref int DataWriteLength, int PipeNo, uint Timeout, int BufferSize)
        //public static bool TE_USB_FX2_SetData(ref CyBulkEndPoint outEndpointPipeNo, ref byte[] DataWrite, ref int DataWriteLength)
        {
            bool bResultDataRead = false;
            byte PipeNoHex       = 0x00;

            CyBulkEndPoint outEndpointPipeNo = null;

            //Shortest and more portable way to select the Address using the PipeNumber
            if (PipeNo == 8)
            {
                PipeNoHex = 0x08;
            }
            else
            {
                PipeNoHex = 0x00;
            }

            if ((TE_USB_FX2_USBDevice != null) && (PipeNoHex == 0x08))
            {
                outEndpointPipeNo         = TE_USB_FX2_USBDevice.EndPointOf(PipeNoHex) as CyBulkEndPoint;
                outEndpointPipeNo.TimeOut = Timeout;

                //int MaxPacketSize= outEndpointPipeNo.MaxPktSize;
                //Console.WriteLine("MaxPacketSize {0} ", MaxPacketSize);

                //int XferSize1 = outEndpointPipeNo.XferSize;
                //Console.WriteLine("XferSize {0} ", XferSize1);

                //outEndpointPipeNo.XferMode = XMODE.DIRECT;

                outEndpointPipeNo.XferSize = BufferSize;// 131072;

                //calls the XferData function for bulk transfer(IN) in the cyusb.dll
                bResultDataRead = outEndpointPipeNo.XferData(ref DataWrite, ref DataWriteLength);
                //Console.WriteLine("bResultDataRead {0} ", bResultDataRead);

                //uint inUSBstatus1 = outEndpointPipeNo.UsbdStatus;
                //Console.WriteLine("UsbdStatus {0:X8} e ", inUSBstatus1);

                //uint inUSBstatus2 = outEndpointPipeNo.NtStatus;
                //Console.WriteLine("NtStatus {0:X8} e ", inUSBstatus2);

                if (bResultDataRead == true)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
예제 #12
0
        private void SetDevice()
        {
            MyDevice = usbDevices[M280DEF.USB_VID, M280DEF.USB_PID] as CyUSBDevice;

            if (MyDevice != null)
            {
                // USB High Speed Check

                /*
                 * if (MyDevice.bHighSpeed == false)
                 * {
                 *  MessageBox.Show("M280 supports USB2.0 only.");
                 *  MyDevice = null;
                 *  inEndpoint = null;
                 *  ep0 = null;
                 * }
                 * else
                 */
                if (inEndpoint == null)
                {
                    // Set the IN and OUT endpoints
                    inEndpoint = MyDevice.EndPointOf(0x86) as CyBulkEndPoint;
                    ep0        = MyDevice.ControlEndPt;
                    // Set time out
                    inEndpoint.TimeOut = M280DEF.TRANSFER_TIMEOUT;
                    ep0.TimeOut        = M280DEF.TRANSFER_TIMEOUT;
                    // Get Image Info
                    GetImageInfo();
                    // Initialize Camera module
                    SendCommand(M280DEF.CMD_INIT_CAMERA, 0, 0);
                    // Turn on USB connected LED
                    //ST: Comment as we move the code to service
                    //labUSBConImg.Image = global::M280_SampleApp.Properties.Resources.green3;
                    // Start Check status
                    //ST: Comment as we move the code to service
                    //this.timCheckStatus.Start();
                }
            }
            else
            {
                inEndpoint = null;
                ep0        = null;

                // Turn off USB connected LED
                //ST: Comment as we move the code to service
                //labUSBConImg.Image = global::M280_SampleApp.Properties.Resources.gray3;
                //labSysReadyImg.Image = global::M280_SampleApp.Properties.Resources.gray3;
                //labReadyImg.Image = global::M280_SampleApp.Properties.Resources.gray3;
                //labBusyImg.Image = global::M280_SampleApp.Properties.Resources.gray3;
                //labButtonImg.Image = global::M280_SampleApp.Properties.Resources.gray3;

                //// Stop checking status thread
                //this.timCheckStatus.Stop();
                this.bInitM280 = false;
                this.bSysBusy  = false;
            }
        }
예제 #13
0
        public void GetDevice()
        {
            if (DeviceAttached == true)
            {
                myDevice    = usbDevices[0x04b4, 0x00F1] as CyUSBDevice;           // check for device with VID/PID of 0x04B4/0x1002
                myFX3Device = usbDevices[0x04B4, 0x00F3] as CyFX3Device;


                if ((myFX3Device != null) && (myDevice == null))                                           // If myDevice exists
                {
                    button1.Enabled = true;
                    button2.Enabled = false;
                    button3.Enabled = false;

                    StatLabel1.Text = "NEXT STEP: Use Download Firmware button to load image into FX3";
                    rtConsole.AppendText("EZ-USB FX3 Bootloader Device connected\n");
                }
                else if ((myDevice != null) && (myFX3Device == null))              // If myDevice exists
                {
                    BulkOutEndPt    = myDevice.EndPointOf(0x02) as CyBulkEndPoint; //Assign EP2 as BulkOutEP and EP6 as BulkInEP
                    button3.Enabled = true;
                    button1.Enabled = false;

                    button2.Enabled = false;
                    rtConsole.AppendText("FX3 - Xilinx Slave Serial Programmer detected\n");
                    StatLabel1.Text = "NEXT STEP: Use Select Bitstream button to select the .bin file for FPGA ";
                }
                else if ((firmwaredownloaded) && (myDevice != null))
                {
                    button1.Enabled = false;
                    rtConsole.AppendText("NO FX3 - Xilinx Slave Serial Programmer detected\n");
                    StatLabel1.Text = "NEXT STEP: Reset the FX3 board and Download the right image into FX3";
                }
                else
                {
                    button1.Enabled = false;
                    button2.Enabled = false;
                    button3.Enabled = false;



                    rtConsole.AppendText("No EZ-USB  FX3 device is connected\n");
                    StatLabel1.Text = "NEXT STEP: Connect EZ-USB FX3 board to PC using an USB cable";
                    DeviceAttached  = false;
                }
            }

            if (DeviceAttached == false)
            {
                DisableApp();                                                          // there is either no device attached or the attached device is not a FX2
            }
            else
            {
                EnableApp();                                                           // the attached device is a FX2LP with our VID/PID
            }
        }
예제 #14
0
        private bool CommandSend(byte[] CmdBytes, CyBulkEndPoint usbBulkOutEndPoint)
        {
            int CmdLength = CmdBytes.Length;

            if (CmdLength == 0)
            {
                return(false);
            }
            return(usbBulkOutEndPoint.XferData(ref CmdBytes, ref CmdLength));
        }
예제 #15
0
        /* Summary
         *  set the endpoints 2/4 as OUT and 4/8 as IN
         */
        private void ConstructEndpoints()
        {
            if (loopDevice != null && cboOutEndPoint.Items.Count > 0 && cboINEndpoint.Items.Count > 0)
            {
                string sAltOut         = cboOutEndPoint.Text.Substring(4, 1);
                byte   outAltInferface = Convert.ToByte(sAltOut);

                string sAltIn         = cboINEndpoint.Text.Substring(4, 1);
                byte   inAltInferface = Convert.ToByte(sAltIn);

                if (outAltInferface != inAltInferface)
                {
                    Text             = "Output Endpoint and Input Endpoint should present in the same ALT interface";
                    StartBtn.Enabled = false;
                    return;
                }

                // Get the endpoint
                int    aX     = cboINEndpoint.Text.LastIndexOf("0x");
                string sAddr  = cboINEndpoint.Text.Substring(aX, 4);
                byte   addrIn = (byte)Util.HexToInt(sAddr);

                aX    = cboOutEndPoint.Text.LastIndexOf("0x");
                sAddr = cboOutEndPoint.Text.Substring(aX, 4);
                byte addrOut = (byte)Util.HexToInt(sAddr);

                outEndpoint = loopDevice.EndPointOf(addrOut) as CyBulkEndPoint;
                inEndpoint  = loopDevice.EndPointOf(addrIn) as CyBulkEndPoint;

                if ((outEndpoint != null) && (inEndpoint != null))
                {
                    //make sure that the device configuration doesn't contain the other than bulk endpoint
                    if ((outEndpoint.Attributes & 0x03 /*0,1 bit for type of transfer*/) != 0x02 /*Bulk endpoint*/)
                    {
                        Text             = "Device Configuration mismatch";
                        StartBtn.Enabled = false;
                        return;
                    }
                    if ((inEndpoint.Attributes & 0x03) != 0x02)
                    {
                        Text             = "Device Configuration mismatch";
                        StartBtn.Enabled = false;
                        return;
                    }
                    outEndpoint.TimeOut = 1000;
                    inEndpoint.TimeOut  = 1000;
                }
                else
                {
                    Text             = "Device Configuration mismatch";
                    StartBtn.Enabled = false;
                    return;
                }
            }
        }
예제 #16
0
 public UsbDataProducer(CyBulkEndPoint usbEndPoint)
 {
     if (usbEndPoint.bIn)
     {
         _bulkInEndPoint = usbEndPoint;
     }
     else
     {
         _bulkInEndPoint = null;
     }
 }
예제 #17
0
        protected override IAvalonPacket CreateAvalonPacket()
        {
            CyUSBDevice sampleDevice = usbDevices[0x04b4, 0x1004] as CyUSBDevice;

            if (sampleDevice == null)
            {
                if (Listener != null)
                {
                    Listener.Stop();
                }
                if (connectedDevice != null)
                {
                    //Console.WriteLine("DeviceRemoved");
                    connectedDevice = null;
                }

                CyFX2Device noEepromDevice = usbDevices[0x04b4, 0x8613] as CyFX2Device;
                if (noEepromDevice == null)
                {
                    return(null);
                }
                string path = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), firmware);
                noEepromDevice.LoadExternalRam(path);

                return(null);
            }

            if (connectedDevice != null)
            {
                return(null);
            }

            //Console.WriteLine("DeviceAttached");
            CyBulkEndPoint outEndpoint = sampleDevice.EndPointOf(0x01) as CyBulkEndPoint;

            if (outEndpoint == null)
            {
                return(null);
            }
            CyBulkEndPoint inEndpoint = sampleDevice.EndPointOf(0x81) as CyBulkEndPoint;

            if (inEndpoint == null)
            {
                return(null);
            }
            endpoint2 = sampleDevice.EndPointOf(0x82) as CyBulkEndPoint;
            if (endpoint2 == null)
            {
                return(null);
            }
            connectedDevice = sampleDevice;
            return(new CsrpAvalonPacket(outEndpoint, inEndpoint));
        }
예제 #18
0
        public bool Close()
        {
            if (device != null)
            {
                device.Dispose();
            }
            device    = null;
            bepOut    = null;
            bepIn82   = null;
            isConnect = false;

            return(true);
        }
예제 #19
0
 public bool connect()
 {
     this.findDevices();
     if (this.device != null)
     {
         this.BulkInEndPt  = this.device.EndPointOf((byte)129) as CyBulkEndPoint;
         this.BulkOutEndPt = this.device.EndPointOf((byte)1) as CyBulkEndPoint;
         this.lastMessage  = this.getDeviceName() + " connected.";
         return(true);
     }
     this.lastMessage = "Please Connect Cornerstone Device";
     return(false);
 }
예제 #20
0
 private bool SetDevice()
 {
     MyDevice = usbDevices[cbxDeviceLists.SelectedIndex] as CyUSBDevice;
     if (MyDevice != null && MyDevice.BulkInEndPt != null && MyDevice.BulkOutEndPt != null)
     {
         BulkInEndPoint        = MyDevice.EndPointOf(UsbInEndPointNum) as CyBulkEndPoint;
         BulkOutEndPoint       = MyDevice.EndPointOf(UsbOutEndPointNum) as CyBulkEndPoint;
         lblUsbName.Content    = MyDevice.FriendlyName + " Connected";
         lblUsbName.Foreground = Brushes.Green;
         IsUsbFX3 = rdbFx3.IsChecked == true;
         return(true);
     }
     return(false);
 }
예제 #21
0
 private void btnSetUsb_Click(object sender, RoutedEventArgs e)
 {
     if (btnSetUsb.Content.Equals("Set"))
     {
         if (cbxDeviceLists.SelectedIndex == -1)
         {
             MessageBox.Show("Please select the USB device", "USB ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
             return;
         }
         if (SetDevice())
         {
             cbxDeviceLists.IsEnabled = false;
             cbxInEndPoint.IsEnabled  = false;
             cbxOutEndPoint.IsEnabled = false;
             cbxPpx.IsEnabled         = false;
             cbxXferQueue.IsEnabled   = false;
             tbxInfo.AppendText("USB Connect\n");
             btnSetUsb.Content     = "Reset";
             btnStartAcq.IsEnabled = true;
             btnCmdSend.IsEnabled  = true;
             rdbFx2.IsEnabled      = false;
             rdbFx3.IsEnabled      = false;
         }
         else
         {
             MessageBox.Show("No Bulk EndPoint. Please program the Device", "Device ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
         }
     }
     else
     {
         MyDevice                 = null;
         BulkInEndPoint           = null;
         BulkOutEndPoint          = null;
         cbxDeviceLists.IsEnabled = true;
         cbxInEndPoint.IsEnabled  = true;
         cbxOutEndPoint.IsEnabled = true;
         cbxPpx.IsEnabled         = true;
         cbxXferQueue.IsEnabled   = true;
         tbxInfo.AppendText("USB Disconnect\n");
         lblUsbName.Content    = "C# USB - no device";
         lblUsbName.Foreground = Brushes.Red;
         btnStartAcq.IsEnabled = false;
         btnCmdSend.IsEnabled  = false;
         btnSetUsb.Content     = "Set";
         rdbFx2.IsEnabled      = true;
         rdbFx3.IsEnabled      = true;
     }
 }
 public M280(string commPort = "")
 {
     readerdata = string.Empty;
     commport   = commPort;
     dlfile     = new DLFILE();
     dispatcher = Dispatcher.CurrentDispatcher;
     dispatcher.Thread.Priority = ThreadPriority.Highest;
     captureutility             = new CaptureUtility();
     Initialized           = null;
     Captured              = null;
     DeviceAttached        = null;
     DeviceDetached        = null;
     ReceiveData           = null;
     handleException       = Thread_Exception;
     imageview             = Image_Captured;
     checkstatus           = new System.Timers.Timer();
     checkstatus.AutoReset = false;
     checkstatus.Elapsed  += CheckState_Tick;
     checkstatus.Interval  = M280DEF.StatusGetInterval;
     capture           = new System.Timers.Timer();
     capture.AutoReset = false;
     capture.Elapsed  += Capture_Tick;
     capture.Interval  = 500;
     bulkendpoint      = null;
     controlendpoint   = null;
     deviceready       = false;
     release           = true;
     running           = false;
     scannerconnected  = false;
     devicebusy        = true;
     deskew            = true;
     m280status        = new byte[M280DEF.STATUS_SIZE];
     pixelformat       = PixelFormat.Format16bppRgb565;
     totalpixelsize    = 0;
     InitializePixelSize();
     buffernum     = totalpixelsize / (512 * M280DEF.Packet_Xfer);
     buffersize    = 0;
     queuesize     = 0;
     successes     = 0;
     failures      = 0;
     transferbytes = 0;
     disposed      = false;
     GetUsbDevices();
     InitializeScanner();
     InitializeReader();
     onInitialized();
 }
예제 #23
0
        /// <summary>
        /// Updates the usb device.  Registered as a callback against the device
        /// driver, which will notify us (usually) when a new USB peripheral is
        /// attached to the driver via Windows
        /// </summary>
        public void UpdateUSBDevice()
        {
            theUSBDevice = usbDevices[VENDOR_ID, PRODUCT_ID] as CyUSBDevice;

            if (theUSBDevice == null)
            {
                PRODUCT_ID   = 0x00F0;
                theUSBDevice = usbDevices[VENDOR_ID, PRODUCT_ID] as CyUSBDevice;
            }

            if (theUSBDevice != null)
            {
                USBDeviceNameLabel.Text = theUSBDevice.FriendlyName;

                outEndpoint = theUSBDevice.EndPointOf(0x01) as CyBulkEndPoint;
                inEndpoint  = theUSBDevice.EndPointOf(0x81) as CyBulkEndPoint;

                if ((outEndpoint != null) && (inEndpoint != null))
                {
                    //make sure that the device configuration doesn't contain the other than bulk endpoint
                    if ((outEndpoint.Attributes & 0x03) != 0x02)
                    {
                        USBOutEndpointLabel.Text = "<incorrect endpoint type detected - expected a bulk endpoint>";
                    }
                    else
                    {
                        USBOutEndpointLabel.Text = "Bulk out endpoint 0x01";
                    }

                    if ((inEndpoint.Attributes & 0x03) != 0x02)
                    {
                        USBInEndpointLabel.Text = "<incorrect endpoint type detected - expected a bulk endpoint>";
                    }
                    else
                    {
                        USBInEndpointLabel.Text = "Bulk in endpoint 0x81";
                    }

                    outEndpoint.TimeOut = 1000;
                    inEndpoint.TimeOut  = 1000;
                }
            }
            else
            {
                USBDeviceNameLabel.Text = USBOutEndpointLabel.Text = USBInEndpointLabel.Text = "<no device detected>";
            }
        }
예제 #24
0
        void usbDevices_DeviceRemoved(object sender, EventArgs e)
        {
            if (IsRunning)
            {
                StopReceiving();
            }
            isReady = false;

            name     = null;
            version  = null;
            revision = null;

            cyDevice        = null;
            controlEndPoint = null;
            bulkInEndPoint  = null;

            SetDevice();
        }
 private void GetUsbDevices()
 {
     try
     {
         usbdevices = new USBDeviceList(CyConst.DEVICES_CYUSB);
     }
     catch
     {
         usbdevices = null;
     }
     if (usbdevices == null || usbdevices.Count == 0)
     {
         devicebusy      = false;
         bulkendpoint    = null;
         controlendpoint = null;
         return;
     }
     usbdevices.DeviceAttached += usbdevices_DeviceAttached;
     usbdevices.DeviceRemoved  += usbdevices_DeviceRemoved;
 }
예제 #26
0
        public bool WriteFWCommand(byte[] buffer)
        {
            bool retValue = false;

            try
            {
                //int iBufSize = buffer.Length;
                int iBufSize = 512;//Keep Constant for any no. of bytes sent
                m_USBDevices = new USBDeviceList(CyConst.DEVICES_CYUSB);
                FxDev        = m_USBDevices[0] as CyUSBDevice;
                bulkEpt      = FxDev.BulkOutEndPt;

                retValue = bulkEpt.XferData(ref buffer, ref iBufSize, false);
            }
            catch (Exception ex)
            {
                retValue = false;
            }
            return(retValue);
        }
예제 #27
0
 /* Summary
  *  set the endpoints 2/4 as OUT and 4/8 as IN
  */
 private void EptPair1Btn_Click(object sender, EventArgs e)
 {
     if (loopDevice != null)
     {
         if (EptPair1Btn.Checked)
         {
             outEndpoint = loopDevice.EndPointOf(0x01) as CyBulkEndPoint;
             inEndpoint  = loopDevice.EndPointOf(0x81) as CyBulkEndPoint;
         }
         else
         {
             outEndpoint = loopDevice.EndPointOf(0x04) as CyBulkEndPoint;
             inEndpoint  = loopDevice.EndPointOf(0x88) as CyBulkEndPoint;
         }
         if ((outEndpoint != null) && (inEndpoint != null))
         {
             //make sure that the device configuration doesn't contain the other than bulk endpoint
             if ((outEndpoint.Attributes & 0x03 /*0,1 bit for type of transfer*/) != 0x02 /*Bulk endpoint*/)
             {
                 Text             = "Device Configuration mismatch";
                 StartBtn.Enabled = false;
                 return;
             }
             if ((inEndpoint.Attributes & 0x03) != 0x02)
             {
                 Text             = "Device Configuration mismatch";
                 StartBtn.Enabled = false;
                 return;
             }
             outEndpoint.TimeOut = 1000;
             inEndpoint.TimeOut  = 1000;
         }
         else
         {
             Text             = "Device Configuration mismatch";
             StartBtn.Enabled = false;
             return;
         }
     }
 }
예제 #28
0
        //寻找usb设备,并连接

        /* 寻找匹配的USB设备,
         * 失败返回false,
         * 成功则返回true,同时配置myDevice、bulkInEndPt、bulkOutEndPt、usbStatus
         * bool变量可以传递给python,可在Python中调用
         */
        public bool check_USB()
        {
            usbDevices = new USBDeviceList(CyConst.DEVICES_CYUSB);
            bool result = true;

            myDevice = usbDevices[VID, PID] as CyUSBDevice;
            if (myDevice != null)
            {
                usbStatus            = true;
                bulkOutEndPt         = myDevice.EndPointOf(0x08) as CyBulkEndPoint; // EP8
                bulkInEndPt          = myDevice.EndPointOf(0x82) as CyBulkEndPoint; //EP2
                bulkInEndPt.XferSize = bulkInEndPt.MaxPktSize * 8;                  // transfer size means the max limits of data in USB driver
                bulkInEndPt.TimeOut  = 100;
            }
            else
            {
                usbStatus    = false;
                bulkOutEndPt = null;
                bulkInEndPt  = null;
                result       = false;
            }
            return(result);
        }
예제 #29
0
        void SetDevice()
        {
            MyDevice = usbDevices[M280DEF.USB_VID, M280DEF.USB_PID] as CyUSBDevice;

            if (MyDevice != null)
            {
                if (inEndpoint == null)
                {
                    inEndpoint         = MyDevice.EndPointOf(0x86) as CyBulkEndPoint;
                    ep0                = MyDevice.ControlEndPt;
                    inEndpoint.TimeOut = 500;

                    SendCommand(M280DEF.CMD_INIT_CAMERA, 0, 0);
                }
            }
            else
            {
                inEndpoint     = null;
                ep0            = null;
                this.bInitM280 = false;
                this.bSysBusy  = false;
            }
        }
예제 #30
0
        public bool Open()
        {
            device = FindCyDevice(VID, PID);
            if (null == device)
            {
                return(false);
            }

            foreach (CyUSBEndPoint ept in device.EndPoints)
            {
                if (!ept.bIn && (ept.Attributes == 2) && ept.Address == 0x02)
                {
                    bepOut = ept as CyBulkEndPoint;
                }

                if (ept.bIn && (ept.Attributes == 2) && ept.Address == 0x82)
                {
                    bepIn82 = ept as CyBulkEndPoint;
                }
            }

            //he Attributes member indicates the type of endpoint per the following list.
            //0: Control
            //1: Isochronous
            //2: Bulk
            //3: Interrupt
            isConnect = true;
            Task.Run(() => Bep82USBRecv());
            //Thread t83Recv = new Thread(Bep83USBRecv);
            //t83Recv.Priority = ThreadPriority.Highest;
            //t83Recv.IsBackground = true;
            //t83Recv.Start();

            Task.Run(() => AnalyseBepIn83Packet());
            //Task.Run(() => AnalyseBepIn82Packet());
            return(true);
        }