/// <summary> /// Equivalent to the CyUSBEndPoint.XferData(...) but with pointer pinning to ensure that the garbage collector does not move buffers /// </summary> /// <param name="buf">The buffer to transfer data into or out</param> /// <param name="len">Length of data to transfer. Can be overwritten with actual transfer length</param> /// <param name="endpoint">Endpoint to perform the transfer operation on</param> /// <returns>Bool indicating success of the transfer operation</returns> static public unsafe bool XferData(ref byte[] buf, ref int len, ref CyUSBEndPoint endpoint) { byte[] ov = new byte[endpoint.OverlapSignalAllocSize]; fixed(byte *numPtr = ov) { ((OVERLAPPED *)numPtr)->hEvent = PInvoke.CreateEvent(0U, 0U, 0U, 0U); byte[] singleXfer = new byte[38 + (endpoint.XferMode == XMODE.DIRECT ? 0 : len)]; // These pinned pointers ensure that the buffers don't move in memory var cmd_buff_handle = GCHandle.Alloc(singleXfer, GCHandleType.Pinned); var data_handle = GCHandle.Alloc(buf, GCHandleType.Pinned); //Perform async transfer endpoint.BeginDataXfer(ref singleXfer, ref buf, ref len, ref ov); bool flag1 = WaitForIO(((OVERLAPPED *)numPtr)->hEvent, ref endpoint); bool flag2 = endpoint.FinishDataXfer(ref singleXfer, ref buf, ref len, ref ov); PInvoke.CloseHandle(((OVERLAPPED *)numPtr)->hEvent); //release memory cmd_buff_handle.Free(); data_handle.Free(); //return operation flags return(flag1 && flag2); } }
private void GetEndpointsOfNode(TreeNode devTree) { cboINEndpoint.Items.Clear(); cboOutEndPoint.Items.Clear(); foreach (TreeNode node in devTree.Nodes) { if (node.Nodes.Count > 0) { GetEndpointsOfNode(node); } else { CyUSBEndPoint ept = node.Tag as CyUSBEndPoint; if (ept == null) { //return; } else if (node.Text.Contains("Bulk in")) { CyUSBInterface ifc = node.Parent.Tag as CyUSBInterface; string s = string.Format("ALT-{0}, {1} Byte {2}", ifc.bAlternateSetting, ept.MaxPktSize, node.Text); cboINEndpoint.Items.Add(s); } else if (node.Text.Contains("Bulk out")) { CyUSBInterface ifc = node.Parent.Tag as CyUSBInterface; string s = string.Format("ALT-{0}, {1} Byte {2}", ifc.bAlternateSetting, ept.MaxPktSize, node.Text); cboOutEndPoint.Items.Add(s); } } } }
public static bool Initialize() { bool retVal = true; m_USBDevices = new USBDeviceList(CyConst.DEVICES_CYUSB); m_USBDevices.DeviceAttached += new EventHandler(usbDevices_DeviceAttached); m_USBDevices.DeviceRemoved += new EventHandler(usbDevices_DeviceRemoved); // Get the first device having VendorID == 0x04B4 and ProductID == 0x8613 for (int i = 0; i < m_USBDevices.Count; i++) { m_CyUSBDevice = m_USBDevices[i] as CyFX3Device; if ((m_CyUSBDevice != null) && (m_CyUSBDevice.ProductID == 0x4720)) { //Fr.BootProgrammerDeviceFound(); break; } } //m_CyUSBDevice = m_USBDevices[0x04B4, 0x00F0] as CyUSBDevice; if (m_CyUSBDevice != null) { Status = m_CyUSBDevice.FriendlyName + " connected."; m_EndPoint = m_CyUSBDevice.EndPoints[0]; } else { m_EndPoint = null; } return(retVal); }
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); } }
/*Summary * This is the event handler for device removal. This method resets the device count and searches for the device with * VID-PID 04b4-1003 */ void usbDevices_DeviceRemoved(object sender, EventArgs e) { bRunning = false; if (tListen != null && tListen.IsAlive == true) { tListen.Abort(); tListen.Join(); tListen = null; } MyDevice = null; EndPoint = null; SetDevice(false); if (StartBtn.Text.Equals("Start") == false) { { DevicesComboBox.Enabled = true; EndPointsComboBox.Enabled = true; PpxBox.Enabled = true; QueueBox.Enabled = true; StartBtn.Text = "Start"; bRunning = false; t2 = DateTime.Now; elapsed = t2 - t1; xferRate = (long)(XferBytes / elapsed.TotalMilliseconds); xferRate = xferRate / (int)100 * (int)100; StartBtn.BackColor = Color.Aquamarine; } } }
private void InitEndInPoints(CyUSBDevice usbDevice) { int endPointCount = (int)usbDevice.EndPointCount; int index1 = 0; int index2 = 0; for (int index3 = 1; index3 < endPointCount; ++index3) { if (((int)usbDevice.EndPoints[index3].Address & 128) == 128) { index1 = index3; } else { index2 = index3; } } if (endPointCount == 3) { this.InEndpt = usbDevice.USBCfgs[0].Interfaces[0].EndPoints[index1]; this.OutEndpt = usbDevice.USBCfgs[0].Interfaces[0].EndPoints[index2]; } else { this.InEndpt = (CyUSBEndPoint)null; this.OutEndpt = (CyUSBEndPoint)null; } }
static void usbDevices_DeviceRemoved(object sender, EventArgs e) { USBEventArgs usbEvent = e as USBEventArgs; Status = usbEvent.FriendlyName + " removed."; m_EndPoint = null; // MessageBox.Show("Removed"); }
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); }
private static bool WaitForIO(System.IntPtr ovlapEvent, ref CyUSBEndPoint endpoint) { switch (PInvoke.WaitForSingleObject(ovlapEvent, endpoint.TimeOut)) { case 0: return(true); case 258: endpoint.Abort(); PInvoke.WaitForSingleObject(ovlapEvent, uint.MaxValue); break; } return(false); }
/*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); }
static void usbDevices_DeviceAttached(object sender, EventArgs e) { try { USBEventArgs usbEvent = e as USBEventArgs; Status = usbEvent.Device.FriendlyName + " connected."; m_CyUSBDevice = usbEvent.Device as CyUSBDevice; if (m_CyUSBDevice.EndPointCount >= 1) { m_EndPoint = m_CyUSBDevice.EndPoints[0]; } } catch (Exception exc) { //MessageBox.Show(exc.Message, "Excepion Occurred After disconnecting"); } // MessageBox.Show("Connected"); }
} // end private void mUSBDevList_Removed(Object sender, EventArgs e) /// <summary> /// function for all detected thing /// </summary> private void USBDetected() { mCyUSBDev = mUSBDevList[0x04b4, 0x1004] as CyUSBDevice; if (mCyUSBDev == null) { MessageBox.Show("No Such Device!"); } else { //mCyUSBInEP = mCyUSBDev.BulkInEndPt; mCyUSBOutEP = mCyUSBDev.BulkOutEndPt; foreach (CyUSBEndPoint ept in mCyUSBDev.EndPoints) { if (ept.bIn && (ept.Attributes == 2)) { mCyUSBInEP = ept as CyBulkEndPoint; } } } } //private void USBDetected()
// Endpoint private void GetEndpointsOfNode(TreeNode devTree) { // 박스 클리어 ConnectCyUsbInEndpointComboBox.Items.Clear(); ConnectCyUsbOutEndpointComboBox.Items.Clear(); // in Endpoint, out Endpoint 박스에 텍스트 추가 foreach (TreeNode node in devTree.Nodes) { // 노드 맨 하위에 위치한 Endpoint 찾기 if (node.Nodes.Count > 0) { GetEndpointsOfNode(node); } else { CyUSBEndPoint ept = node.Tag as CyUSBEndPoint; if (ept == null) { // Tag가 없으면 pass } // 벌크 인 엔드포인트 정보 찾기 else if (node.Text.Contains("Bulk in")) { // Alterate Setting ?? CyUSBInterface ifc = node.Parent.Tag as CyUSBInterface; string s = string.Format("ALT-{0}, {1} Byte {2}", ifc.bAlternateSetting, ept.MaxPktSize, node.Text); ConnectCyUsbInEndpointComboBox.Items.Add(s); } // 벌크 아웃 엔드포인트 정보 찾기 else if (node.Text.Contains("Bulk out")) { CyUSBInterface ifc = node.Parent.Tag as CyUSBInterface; string s = string.Format("ALT-{0}, {1} Byte {2}", ifc.bAlternateSetting, ept.MaxPktSize, node.Text); ConnectCyUsbOutEndpointComboBox.Items.Add(s); } } } }
/*Summary * Recursive routine populates EndPointsComboBox with strings * representing all the endpoints in the device. */ private void GetEndpointsOfNode(TreeNode devTree) { foreach (TreeNode node in devTree.Nodes) { if (node.Nodes.Count > 0) { GetEndpointsOfNode(node); } else { CyUSBEndPoint ept = node.Tag as CyUSBEndPoint; if (ept == null) { //return; } else if (!node.Text.Contains("Control")) { CyUSBInterface ifc = node.Parent.Tag as CyUSBInterface; string s = string.Format("ALT-{0}, {1} Byte, {2}", ifc.bAlternateSetting, ept.MaxPktSize, node.Text); EndPointsComboBox.Items.Add(s); } } } }
/*Summary * This is the event handler for device removal. This method resets the device count and searches for the device with * VID-PID 04b4-1003 */ void usbDevices_DeviceRemoved(object sender, EventArgs e) { MyDevice = null; EndPoint = null; SetDevice(); }
private void DeviceComboBox_SelectedIndexChanged(object sender, EventArgs e) { MyDevice = null; EndPoint = null; SetDevice(true); }