public bool ReConnect() { MyUsb = MyUsbList[0x04B4, 0x1004] as CyUSBDevice; if (MyUsb != null) { MyUsb.ReConnect(); MyUsb = MyUsbList[0x04B4, 0x00F1] as CyUSBDevice; } else { MyUsb = MyUsbList[0x04B4, 0x00F1] as CyUSBDevice; } if (MyUsb != null) { MyOutPoint = MyUsb.EndPointOf(0x02); MyInPoint = MyUsb.EndPointOf(0x86); MyControlPoint = MyUsb.ControlEndPt; Reset(); return(true); } else { return(false); } }
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); } } }
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); } }
/* * // 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); } }
public DDSUSBChip(CyUSBDevice CyUSBDevice) { cyUSBDevice = CyUSBDevice; // Switch to interface 1 which provides several Endpoints cyUSBDevice.AltIntfc = 1; // Define Endpoints EP1OUT = cyUSBDevice.EndPointOf(0x01); EP1IN = cyUSBDevice.EndPointOf(0x81); EP2OUT = cyUSBDevice.EndPointOf(0x02); }
/* 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; } } }
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)); }
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); }
/// <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>"; } }
/* * 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); } }
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; } }
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 } }
/* 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; } } }
//寻找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); }
/*Summary * This is a system event handler, when the selected index changes(end point selection). */ private void EndPointsComboBox_SelectedIndexChanged(object sender, EventArgs e) { // Get the Alt setting string sAlt = EndPointsComboBox.Text.Substring(4, 1); byte a = Convert.ToByte(sAlt); MyDevice.AltIntfc = a; // Get the endpoint int aX = EndPointsComboBox.Text.LastIndexOf("0x"); string sAddr = EndPointsComboBox.Text.Substring(aX, 4); byte addr = (byte)Util.HexToInt(sAddr); EndPoint = MyDevice.EndPointOf(addr); // Ensure valid PPX for this endpoint PpxBox_SelectedIndexChanged(sender, null); }
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; } }
private void InitializeScanner() { if (usbdevices == null || usbdevices.Count == 0) { return; } usbdevice = usbdevices[M280DEF.USB_VID, M280DEF.USB_PID] as CyUSBDevice; if (usbdevice == null) { devicebusy = false; bulkendpoint = null; controlendpoint = null; return; } if (bulkendpoint == null) { bulkendpoint = usbdevice.EndPointOf(0x86) as CyBulkEndPoint; if (bulkendpoint == null) { return; } bulkendpoint.TimeOut = M280DEF.TRANSFER_TIMEOUT; controlendpoint = usbdevice.ControlEndPt; if (controlendpoint == null) { return; } controlendpoint.TimeOut = M280DEF.TRANSFER_TIMEOUT; InitializeImageInfo(); SendCommand(M280DEF.CMD_INIT_CAMERA, 0, 0); scannerconnected = true; deviceready = true; devicebusy = false; checkstatus.Start(); } }
/* * 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); } }