示例#1
0
        /* Summary
            The function sets the device, as the one having VID=04b4 and PID=1004
            This will detect only the devices with the above VID,PID combinations
        */
        public void setDevice()
        {
            try
            {
                loopDevice = usbDevices[0x04b4, 0x8613] as CyUSBDevice;

                StartBtn.Enabled = (loopDevice != null);

                if (loopDevice != null)
                    Text = loopDevice.FriendlyName;
                else
                    Text = "BVCOMVC - no device";

                outEndpoint = loopDevice.EndPointOf(0x02) as CyBulkEndPoint;
                inEndpoint = loopDevice.EndPointOf(0x86) as CyBulkEndPoint;

                outEndpoint.TimeOut = 1000;
                inEndpoint.TimeOut = 1000;
            }
            catch
            {
                Text = "BVCOMVC - no device";
            }
        }
    /*
     * 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;
    }
    /*
     * 3.5   TE_USB_FX2_GetData()
     * 
     * 3.5.1   Declaration
     * public static bool TE_USB_FX2_GetData(ref CyUSBDevice TE_USB_FX2_USBDevice, ref byte[] DataRead, ref int DataReadLength, 
     * int PipeNo, uint Timeout, int BufferSize)
     * 
     * 3.5.2   Function Call
     * Your application program shall call this function like this:
     * TE_USB_FX2.TE_USB_FX2.TE_USB_FX2_GetData(ref TE_USB_FX2_USBDevice, ref DataRead, ref DataReadLength, PI_EP6, Timeout, 
     * BufferSize);
     * 
     * 3.5.3   Description
     * This function takes an already initialized USB Device (previously selected by TE_USB_FX2_Open()) and reads data from 
     * USB FX2 microcontroller endpoint EP6 (0x86) (endpoints EP4(0x84) or EP2(0x82) are also theoretically possible). 
     * Data comes from the FPGA.
     * Currently (April 2012), only endpoint 0x86 is actually implemented in Trenz Electronic USB FPGA modules, so that endpoints 
     * EP2 and EP4 cannot be read or , more precisely, they are not even connected to the FPGA. That is why attempting to read them 
     * causes a function failure after Timeout expires.
     * 3.5.4   Expected Data Throughput
     * The maximum data throughput expected (with a DataReadLength= 120*10^6) is 37 Mbyte/s (PacketSize = BufferSize = 131,072), 
     * but in fact this value is variable between 31-36 Mbyte/s (the mean value seems 33.5 Mbyte/s); so if you measure this range 
     * of values, the data reception can be considered as normal.
     * The data throughput is variable in two ways:
     * 1. depends on the used host computer;
     * 2. varies with every function call.
     * 3.5.5   DataRead Size Shall Not Be Too Large
     * TE_USB_FX2_GetData() 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 (e.g. 120 millions of byte); transfer the 
     * same data with many packets instead (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.5.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++)
     * {
     *   TE_USB_FX2_GetData(ref TE_USB_FX2_USBDevice, ref buffer, ref packetlen, PI_EP6, TIMEOUT_MS,BUFFER_SIZE)
     *   Buffer.BlockCopy(buffer, 0, data, total_cnt, packetlen);
     *   total_cnt += packetlen; 
     * }
     * 3.5.5.2   Expanded version (code)
     * PACKETLENGTH=100000;
     * packets=1200;
     * byte[] data = new byte[packetlen*packets];
     * byte[] buffer = new byte[packetlen];
     * //starts test: the FPGA start to write data in the buffer EP6 of FX2 chip
     * SendFPGAcommand(ref TE_USB_FX2_USBDevice, MB_Commands.FX22MB_REG0_START_TX, TIMEOUT_MS);
     * test_cnt = 0;
     * total_cnt = 0;
     * for (int i = 0; i < packets; i++)
     * {
     *   //buffer = &data[total_cnt];
     *   packetlen = PACKETLENGTH;
     *   //fixed (byte* buffer = &data[total_cnt]
     *   bResultXfer = TE_USB_FX2.TE_USB_FX2.TE_USB_FX2_GetData(ref   TE_USB_FX2_USBDevice, ref buffer, ref packetlen, PI_EP6, 
     *   TIMEOUT_MS,BUFFER_SIZE);
     *   Buffer.BlockCopy(buffer, 0, data, total_cnt, packetlen); 
     *   if (bResultXfer == false)
     *   {
     *     //cout << "ERROR" << endl;
     *     Console.WriteLine("Error Get Data");
     *     SendFPGAcommand(ref TE_USB_FX2_USBDevice, MB_Commands.FX22MB_REG0_STOP, TIMEOUT_MS);
     *     return;
     *   }
     *   total_cnt += packetlen
     *}
     *  //stop test: the FPGA start to write data in the buffer EP6 of //FX2 chip
     *SendFPGAcommand(ref TE_USB_FX2_USBDevice,
     *MB_Commands.FX22MB_REG0_STOP, TIMEOUT_MS);
     *3.5.6   DataRead Size Shall Not Be Too Small
     *There are two reasons why DataRead size shall not be too small.
     *The first reason is described in section 1.1.4 PacketSize. PacketSize has also a strong influence on DataThroughput. 
     *If PacketSize is too small (e.g. 512 byte), you can have very low DataThroughput (2.2 Mbyte/s) even if you use a large driver 
     *buffer (driver buffer size = 131,072 bytes). See section 6 TE_USB_FX2_CyUSB.dll:  Data Transfer Throughput Optimization.
     *The second reason is that probably the FPGA imposes your minimum packet size. In a properly used read test mode 
     *(using FX22MB_REG0_START_TX and therefore attaching the FPGA), TE_USB_FX2_GetData() is unable to read less than 1024 byte. 
     *In a improperly used read test mode (not using FX22MB_REG0_START_TX and therefore detaching the FPGA), TE_USB_FX2_GetData() 
     *is able to read a packet size down to 64 byte. The same CyUSB method XferData() used (under the hood) in 
     *TE_USB_FX2_SendCommand() is able to read a packet size of 64 byte. These facts prove that the minimum packet size is imposed 
     *by FPGA. To be safe, we recommend to use this function with a size multiple of 1 kbyte. 
     *
     *3.5.7   Parameters
     *1. ref CyUSBDevice TE_USB-FX2_USBDevice
     *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[] DataRead
     *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 rather than by value. This 
     *parameter points to the byte array that, after the function returns, will contain the data read from the buffer EP6 of the 
     *USB FX2 microcontroller. The data contained in EP6 generated by the FPGA. If no data is contained in EP6, the byte array is 
     *left unchanged. 
     *3. ref int DataReadLength
     *This parameter is the length (in bytes) of the previous byte array; it is the length of the packet read from the USB FX2 
     *microcontroller endpoint EP6 (0x86). It is typically PacketLength. This parameter is passed by reference (ref).
     *4. int PipeNumber
     *This parameter is the value that identifies the endpoint used for data transfer. It is called PipeNumber because it identifies 
     *the buffer (pipe) used by the USB FX2 microcontroller.
     *5. uint Timeout
     *It is the integer time value 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 data/command transmission/reception.. Otherwise the transmission/reception will fail. 
     *See 1.1.2 Timeout Setting.
     *6. int BufferSize
     *It is the dimension (in bytes) of the driver buffer (SW) used in data reception of a single endpoint (EP6 0x86 in this case)
     *single endpoint (EP6 0x86 in this case); the total buffer size is the sum of BufferSize of every endpoint used. BufferSize has 
     *a strong influence on DataThroughput. If the BufferSize is too small,  DataThroughput can be 1/3-1/2 of the maximum value 
     *(from a maximum value of 36 Mbyte/s for read transactions to an actual value of 18 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.5.8   Return Value
     *1. bool : logical type
     *This function returns true if it is able to receive the data from buffer EP6 within Timeout milliseconds. 
     *This function returns false otherwise.
     * 
     */

    /// <summary>
    /// ////////
    /// </summary>
    /// <param name="TE03xxUSBdevice"></param>
    /// <param name="DataRead"></param>
    /// <param name="DataReadLength"></param>
    /// <param name="PipeNo"></param>
    /// <param name="Timeout"></param>
    ///  /// <returns></returns>

    //public static bool TE_USB_FX2_GetData(ref CyUSBDevice TE_USB_FX2_USBDevice,ref CyBulkEndPoint inEndpointPipeNo, ref byte[] DataRead, ref int DataReadLength, int PipeNo, uint Timeout)
    public static bool TE_USB_FX2_GetData(ref CyUSBDevice TE_USB_FX2_USBDevice, ref byte[] DataRead, ref int DataReadLength, int PipeNo, uint Timeout, int BufferSize)
    {
      bool bResultDataRead = false;
      byte PipeNoHex = 0x00;

      //Shortest and more portable way to select the Address using the PipeNumber

      CyBulkEndPoint inEndpointPipeNo = null;
      //Shortest and more portable way
      if (PipeNo == 2) PipeNoHex = 0x82;
      else PipeNoHex = 0x00;
      if (PipeNo == 4) PipeNoHex = 0x84;
      else PipeNoHex = 0x00;
      if (PipeNo == 6) PipeNoHex = 0x86;
      else PipeNoHex = 0x00;

      //Fundamental Note: currently (March 2012) only 0x86 EndPoint is actually implemented in TE-USB FPGA modules

      if ((TE_USB_FX2_USBDevice != null) && (PipeNoHex == 0x86))  //(TE_USB_FX2_USBDevice != null) &&
      {
        inEndpointPipeNo = TE_USB_FX2_USBDevice.EndPointOf(PipeNoHex) as CyBulkEndPoint;
        inEndpointPipeNo.TimeOut = Timeout;

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

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

        //outEndpointPipeNo.XferMode = XMODE.DIRECT;

        inEndpointPipeNo.XferSize = BufferSize; // 131072;

        //calls the XferData function for bulk transfer(IN) in the cyusb.dll
        bResultDataRead = inEndpointPipeNo.XferData(ref DataRead, ref DataReadLength);
        //uint inUSBstatus1 = inEndpointPipeNo.UsbdStatus;
        //Console.WriteLine("UsbdStatus {0:X8} ", inUSBstatus1);

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

        if (bResultDataRead == true) return true;
        else return false;
      }
      else return false;

    }
    /*
    // 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;
    }
示例#5
0
        /* Summary
            Executes the script loaded
        */
        private void play_button_Click(object sender, EventArgs e)
        {
            FxDev = FxDevDeviceSelected();

            if (FxDev == null)
            {
                return;
            }

            if (playscriptfile.Length == 0)
            {
                MessageBox.Show("Load a script before playing it.", "Load script");
                return;
            }

            StatLabel.Text = "Playing Script " + FOpenDialog.FileName + " in Outputbox";
            Refresh();

            FileStream stream = new FileStream(playscriptfile, FileMode.Open, FileAccess.Read);

            if (stream.Length > 0)
            {
                try
                {
                    Xaction.ReadFromStream(stream);

                    if (FxDev.Config != Xaction.ConfigNum)
                        FxDev.Config = Xaction.ConfigNum;

                    if (FxDev.AltIntfc != Xaction.AltIntfc)
                        FxDev.AltIntfc = Xaction.AltIntfc;

                    stream.Close();

                    stream = new FileStream(playscriptfile, FileMode.Open, FileAccess.Read);

                    long totalFileSize = stream.Length;
                    long file_bytes_read = 0;

                    do
                    {
                        Xaction.ReadFromStream(stream);
                        file_bytes_read += 32;

                        if (Xaction.Tag == 0xFF)
                        {
                            Thread.Sleep(100);
                        }
                        else
                        {
                            byte[] buffer = new byte[Xaction.DataLen];
                            int len = (int)Xaction.DataLen;

                            curEndpt = FxDev.EndPointOf(Xaction.EndPtAddr);

                            if (curEndpt != null)
                            {
                                if (curEndpt.Attributes == 0)
                                {
                                    /* Control transfer */
                                    CyControlEndPoint ctlEpt = curEndpt as CyControlEndPoint;

                                    byte tmp = Xaction.bReqType;

                                    ctlEpt.Target = (byte)(tmp & TTransaction.ReqType_TGT_MASK);
                                    ctlEpt.ReqType = (byte)(tmp & TTransaction.ReqType_TYPE_MASK);
                                    ctlEpt.Direction = (byte)(tmp & TTransaction.ReqType_DIR_MASK);
                                    ctlEpt.ReqCode = Xaction.CtlReqCode;
                                    ctlEpt.Value = Xaction.wValue;
                                    ctlEpt.Index = Xaction.wIndex;

                                    if (Xaction.Tag == 0)
                                    {
                                        Xaction.ReadToBuffer(stream, ref buffer, ref len);
                                        file_bytes_read += len;
                                    }

                                    if (Xaction.Tag == 1)
                                    {
                                        /* Read from device saving to file */

                                        string tmpSFilter = FSave.Filter;
                                        string tmpSTitle = FSave.Title;
                                        string file;

                                        FSave.Title = "Save Data as:";
                                        FSave.Filter = "All Files(*.*) | *.*";

                                        if (FSave.ShowDialog() == DialogResult.OK)
                                        {
                                            file = FSave.FileName;
                                            Refresh();
                                        }
                                        else
                                        {
                                            FSave.Filter = tmpSFilter;
                                            FSave.Title = tmpSTitle;
                                            return;
                                        }

                                        FSave.FileName = "";
                                        FSave.Filter = tmpSFilter;
                                        FSave.Title = tmpSTitle;

                                        PerformCtlFileTransfer(file, ref buffer, ref len);
                                    }
                                    else
                                    {
                                        PerformCtlTransfer(ref buffer, ref len);
                                    }
                                }
                                else
                                {
                                    /* Non Ep0 transfer */
                                    if (Xaction.Tag == 0)
                                    {
                                        Xaction.ReadToBuffer(stream, ref buffer, ref len);
                                        file_bytes_read += len;
                                    }

                                    if (Xaction.Tag == 1)
                                    {
                                        /* Read from device saving to file */
                                        string tmpSFilter = FSave.Filter;
                                        string tmpSTitle = FSave.Title;
                                        string file;

                                        FSave.Title = "Save Data as:";
                                        FSave.Filter = "All files(*.*) | *.*";

                                        if (FSave.ShowDialog() == DialogResult.OK)
                                        {
                                            file = FSave.FileName;
                                            Refresh();
                                        }
                                        else
                                        {
                                            FSave.Filter = tmpSFilter;
                                            FSave.Title = tmpSTitle;
                                            return;
                                        }

                                        FSave.FileName = "";
                                        FSave.Filter = tmpSFilter;
                                        FSave.Title = tmpSTitle;

                                        PerformNonEP0FileXfer(file, ref buffer, ref len);
                                    }
                                    else
                                    {
                                        PerformNonEP0Xfer(ref buffer, ref len);
                                    }
                                }
                            }
                        }
                    } while ((totalFileSize - file_bytes_read) >= 32);
                }
                catch (Exception esc)
                {
                    MessageBox.Show(esc.Message, "Invalid file data");
                }
            }
            else
                MessageBox.Show("Script Loaded is empty", "Invalid file");

            stream.Close();
        }
示例#6
0
        /* Summary
            The function sets the device, as the one having VID=04b4 and PID=1004
            This will detect only the devices with the above VID,PID combinations
        */
        public void setDevice()
        {
            loopDevice = usbDevices[0x04b4, 0x0010] as CyUSBDevice;

            btnSendConfiguration.Enabled = (loopDevice != null);

            sendDeviceStateToFromHeader();

            // Set the IN and OUT endpoints per the selected radio buttons.
            if (loopDevice != null)
            {

                outEndpoint = loopDevice.EndPointOf(0x02) as CyBulkEndPoint;//0x00 is out + EP2 = 0x02
                inEndpoint = loopDevice.EndPointOf(0x81) as CyBulkEndPoint; //0x80 is in + EP1 = 0x81

                outEndpoint.TimeOut = 1000;
                inEndpoint.TimeOut = 1000;
            }
        }