private static void Main() { bool success; UsbK usb; WINUSB_PIPE_INFORMATION pipeInfoRead; USB_INTERFACE_DESCRIPTOR interfaceDescriptorRead; WINUSB_PIPE_INFORMATION pipeInfoWrite; USB_INTERFACE_DESCRIPTOR interfaceDescriptorWrite; // Find the IN endpoint and configure the device. Test.PipeId = Test.PipeId | AllKConstants.USB_ENDPOINT_DIRECTION_MASK; if (!Test.ConfigureDevice(out pipeInfoRead, out usb, out interfaceDescriptorRead)) { return; } // Find the OUT endpoint. Test.PipeId = Test.PipeId & ~AllKConstants.USB_ENDPOINT_DIRECTION_MASK; if (!Test.FindPipeAndInterface(usb, out interfaceDescriptorWrite, out pipeInfoWrite)) { return; } Debug.Assert( interfaceDescriptorRead.bInterfaceNumber == interfaceDescriptorWrite.bInterfaceNumber, "This example requires the IN and OUT endpoints have the same bInterfaceNumber."); Debug.Assert( pipeInfoRead.MaximumPacketSize > 0 && pipeInfoRead.MaximumPacketSize == pipeInfoWrite.MaximumPacketSize, "This example requires the IN and OUT endpoints have the same MaximumPacketSize."); // We will keep the buffer >= 1024. // To satisfy StmK it must also be an interval of MaximumPacketSize if (Test.TransferBufferSize == -1) { Test.TransferBufferSize = pipeInfoWrite.MaximumPacketSize * ((1024 + pipeInfoWrite.MaximumPacketSize - 1) / pipeInfoWrite.MaximumPacketSize); } #if BMFW // TODO FOR USER: Remove this block if not using benchmark firmware. // This configures devices running benchmark firmware for streaming DeviceToHost transfers. Console.WriteLine("Configuring for benchmark device.."); BM_TEST_TYPE testType = BM_TEST_TYPE.LOOP; success = Benchmark.Configure(usb, BM_COMMAND.SET_TEST, interfaceDescriptorRead.bInterfaceNumber, ref testType); if (!success) { Console.WriteLine("Bench_Configure failed."); } #endif if (!Test.ShowTestReady()) { goto Done; } UsbStream stmRead = new UsbStream(usb, pipeInfoRead.PipeId, Test.TransferBufferSize, Test.MaxPendingTransfers, Test.MaxPendingIO, true, 1); UsbStream stmWrite = new UsbStream(usb, pipeInfoWrite.PipeId, Test.TransferBufferSize, Test.MaxPendingTransfers, Test.MaxPendingIO, true, 3000); StreamReader stmReader = new StreamReader(stmRead); StreamWriter stmWriter = new StreamWriter(stmWrite); stmRead.Start(); stmWrite.Start(); Char[] chTemp = new char[Test.TransferBufferSize]; while (stmReader.Read(chTemp, 0, chTemp.Length) > 0) { Console.WriteLine("Flushing packets.."); } string sWrite = String.Empty; string sPartialRead = String.Empty; string sRead; const string lineFormat = "Tx#{0:000000} This string is being looped through endpoints {1:X2}h and {2:X2}h"; int lineLength = String.Format(lineFormat, 0, 0, 0).Length; // This is a counter/timer used only for statistics gathering. Thread.Sleep(0); Test.Dcs.Start(); for (int iTransfers = 0; iTransfers < Test.MaxTransfersTotal; iTransfers++) { sWrite = String.Format(lineFormat, iTransfers, pipeInfoWrite.PipeId, pipeInfoRead.PipeId); stmWriter.WriteLine(sWrite); Console.WriteLine(sWrite); if ((iTransfers & 8) > 0) { stmWriter.Flush(); } if (iTransfers <= 16 || (String.IsNullOrEmpty((sRead = stmReader.ReadLine())))) { continue; } if (!FormatBrokenStrings(lineLength, "Rx", ref sPartialRead, ref sRead)) { continue; } Console.WriteLine(sRead); } stmWriter.Flush(); while (!(String.IsNullOrEmpty((sRead = stmReader.ReadLine())))) { if (!FormatBrokenStrings(lineLength, "Rx", ref sPartialRead, ref sRead)) { continue; } Console.WriteLine(sRead); } Test.Dcs.Stop(); TimeSpan ts = new TimeSpan(Test.Dcs.Ticks); Console.WriteLine("Elapsed Time:\n\t{0} mins\n\t{1} secs\n\t{2} msecs", Math.Floor(ts.TotalMinutes), ts.Seconds, ts.Milliseconds); stmWriter.Dispose(); stmReader.Dispose(); Done: usb.Free(); }
private static void Main() { bool success; WINUSB_PIPE_INFORMATION pipeInfo; UsbK usb; USB_INTERFACE_DESCRIPTOR interfaceDescriptor; // Find and configure the device. if (!Test.ConfigureDevice(out pipeInfo, out usb, out interfaceDescriptor)) { return; } if (Test.TransferBufferSize == -1) { Test.TransferBufferSize = pipeInfo.MaximumPacketSize * 64; } #if BMFW // TODO FOR USER: Remove this block if not using benchmark firmware. // This configures devices running benchmark firmware for streaming DeviceToHost transfers. Console.WriteLine("Configuring for benchmark device.."); BM_TEST_TYPE testType = ((Test.PipeId & 0x80) > 0) ? BM_TEST_TYPE.READ : BM_TEST_TYPE.WRITE; success = Benchmark.Configure(usb, BM_COMMAND.SET_TEST, interfaceDescriptor.bInterfaceNumber, ref testType); if (!success) { Console.WriteLine("Bench_Configure failed."); } #endif if (!Test.ShowTestReady()) { goto Done; } // In most cases, you should to set the pipe timeout policy before using synchronous I/O. // By default, sync transfers wait infinitely for a transfer to complete. // Set the pipe timeout policy to 3000ms int[] pipeTimeoutMS = new[] { 3000 }; usb.SetPipePolicy( (byte)Test.PipeId, (int)PipePolicyType.PIPE_TRANSFER_TIMEOUT, Marshal.SizeOf(typeof(int)), pipeTimeoutMS); int totalTransfers = 0; success = true; byte[] tempBuffer = new byte[Test.TransferBufferSize]; while (success && totalTransfers++ < Test.MaxTransfersTotal) { int transferred; if ((Test.PipeId & 0x80) > 0) { success = usb.ReadPipe((byte)Test.PipeId, tempBuffer, tempBuffer.Length, out transferred, IntPtr.Zero); } else { success = usb.WritePipe((byte)Test.PipeId, tempBuffer, tempBuffer.Length, out transferred, IntPtr.Zero); } Console.WriteLine("#{0:0000} Transferred {1} bytes.", totalTransfers, transferred); } if (!success) { Console.WriteLine("An error occured transferring data. ErrorCode: {0:X8}h", Marshal.GetLastWin32Error()); } Done: usb.Free(); }
private static void Main() { bool success; WINUSB_PIPE_INFORMATION pipeInfo; UsbK usb; USB_INTERFACE_DESCRIPTOR interfaceDescriptor; // Find and configure the device. if (!Test.ConfigureDevice(out pipeInfo, out usb, out interfaceDescriptor)) { return; } if (Test.TransferBufferSize == -1) { Test.TransferBufferSize = pipeInfo.MaximumPacketSize * 64; } #if BMFW // TODO FOR USER: Remove this block if not using benchmark firmware. // This configures devices running benchmark firmware for streaming DeviceToHost transfers. Console.WriteLine("Configuring for benchmark device.."); BM_TEST_TYPE testType = ((Test.PipeId & 0x80) > 0) ? BM_TEST_TYPE.READ : BM_TEST_TYPE.WRITE; success = Benchmark.Configure(usb, BM_COMMAND.SET_TEST, interfaceDescriptor.bInterfaceNumber, ref testType); if (!success) { Console.WriteLine("Bench_Configure failed."); } #endif if (!Test.ShowTestReady()) { goto Done; } KSTM_CALLBACK callback = new KSTM_CALLBACK(); StmK stm = new StmK( usb.Handle, pipeInfo.PipeId, Test.TransferBufferSize, Test.MaxPendingTransfers, Test.MaxPendingIO, ref callback, KSTM_FLAG.USE_TIMEOUT | (KSTM_FLAG)3000); byte[] tempBuffer = new byte[Test.TransferBufferSize]; Thread.Sleep(0); // This is just a counter/timer for statistics gathering. Test.Dcs.Start(); success = stm.Start(); long totalTransferCount = 0; while (success) { int transferred; if ((pipeInfo.PipeId & 0x80) == 0x80) { success = stm.Read(tempBuffer, 0, tempBuffer.Length, out transferred); if (!success) { break; } } else { success = stm.Write(tempBuffer, 0, tempBuffer.Length, out transferred); if (!success) { break; } } string dataPrefix = String.Format(" Data Prefix: [{0:X2} {1:X2} {2:X2} {3:X2} {4:X2} {5:X2} {6:X2} {7:X2}] ", tempBuffer[0], tempBuffer[1], tempBuffer[2], tempBuffer[3], tempBuffer[4], tempBuffer[5], tempBuffer[6], tempBuffer[7]); Console.WriteLine( totalTransferCount > Test.MaxTransfersTotal ? "#{0}: [Stream Stopped] {1} transferred. {2}" : "#{0}: {1} transferred. {2}", totalTransferCount.ToString("0000"), transferred, dataPrefix); totalTransferCount++; if (totalTransferCount == Test.MaxTransfersTotal) { success = stm.Stop(3000); } } Console.WriteLine("Done. TotalTransfers:{0} ErrorCode:{1:X8}h", totalTransferCount, Marshal.GetLastWin32Error()); stm.Free(); Done: usb.Free(); }
private static void Main() { bool success; WINUSB_PIPE_INFORMATION pipeInfo; UsbK usb; USB_INTERFACE_DESCRIPTOR interfaceDescriptor; // Find and configure the device. if (!Test.ConfigureDevice(out pipeInfo, out usb, out interfaceDescriptor)) { return; } if (Test.TransferBufferSize == -1) { Test.TransferBufferSize = pipeInfo.MaximumPacketSize * 64; } #if BMFW // TODO FOR USER: Remove this block if not using benchmark firmware. // This configures devices running benchmark firmware for streaming DeviceToHost transfers. Console.WriteLine("Configuring for benchmark device.."); BM_TEST_TYPE testType = ((Test.PipeId & 0x80) > 0) ? BM_TEST_TYPE.READ : BM_TEST_TYPE.WRITE; success = Benchmark.Configure(usb, BM_COMMAND.SET_TEST, interfaceDescriptor.bInterfaceNumber, ref testType); if (!success) { Console.WriteLine("Bench_Configure failed."); } #endif if (!Test.ShowTestReady()) { goto Done; } /* In most cases, you should clear the pipe timeout policy before using asynchronous I/O. (INFINITE) * This decreases overhead in the driver and generally we managed the timeout ourselves from user code. * * Set the PIPE_TRANSFER_TIMEOUT policy to INFINITE: */ int[] pipeTimeoutMS = new[] { 0 }; usb.SetPipePolicy((byte)Test.PipeId, (int)PipePolicyType.PIPE_TRANSFER_TIMEOUT, Marshal.SizeOf(typeof(int)), pipeTimeoutMS); /* In some cases, you may want to set the RAW_IO pipe policy. This will impose more restrictions on the * allowable transfer buffer sizes but will improve performance. (See documentation for restrictions) * * Set the RAW_IO policy to TRUE: */ int[] useRawIO = new[] { 1 }; usb.SetPipePolicy((byte)Test.PipeId, (int)PipePolicyType.RAW_IO, Marshal.SizeOf(typeof(int)), useRawIO); int totalSubmittedTransfers = 0; int totalCompletedTransfers = 0; /* We need a different buffer for each MaxPendingIO slot because they will all be submitted to the driver * (outstanding) at the same time. */ byte[][] transferBuffers = new byte[Test.MaxPendingIO][]; OvlK ovlPool = new OvlK(usb.Handle, Test.MaxPendingIO, KOVL_POOL_FLAG.NONE); List <GCHandle> transferBufferGCList = new List <GCHandle>(Test.MaxPendingIO); success = true; // Start transferring data synchronously; one transfer at a time until the test limit (MaxTransfersTotal) is hit. while (success && totalCompletedTransfers < Test.MaxTransfersTotal) { KOVL_HANDLE ovlHandle; int transferred; while (success && totalSubmittedTransfers < Test.MaxTransfersTotal) { // Get the next KOVL_HANDLE if (!ovlPool.Acquire(out ovlHandle)) { // This example expects a failure of NoMoreItems when the pool has no more handles to acquire. This is // our indication that it is time to begin waiting for a completion. Debug.Assert(Marshal.GetLastWin32Error() == ErrorCodes.NoMoreItems); break; } // Get the next transfer buffer int transferBufferIndex = totalSubmittedTransfers % Test.MaxPendingIO; if (transferBuffers[transferBufferIndex] == null) { /* This index has not been allocated yet. We need a different buffer for each MaxPendingIO * slot because they will all be submitted to the driver (outstanding) at the same time. */ Console.WriteLine("Allocating transfer buffer at index:{0}", transferBufferIndex); transferBuffers[transferBufferIndex] = new byte[Test.TransferBufferSize]; // Transfer buffers should be pinned so the garbage collector cannot move the memory. transferBufferGCList.Add(GCHandle.Alloc(transferBuffers[transferBufferIndex], GCHandleType.Pinned)); } byte[] transferBuffer = transferBuffers[transferBufferIndex]; int not_used_for_async; if ((Test.PipeId & AllKConstants.USB_ENDPOINT_DIRECTION_MASK) > 0) { success = usb.ReadPipe((byte)Test.PipeId, transferBuffer, transferBuffer.Length, out not_used_for_async, ovlHandle); } else { FillMyBufferForWrite(transferBuffer, out transferred); success = usb.WritePipe((byte)Test.PipeId, transferBuffer, transferred, out not_used_for_async, ovlHandle); } if (Marshal.GetLastWin32Error() == ErrorCodes.IoPending) { success = true; totalSubmittedTransfers++; Console.WriteLine("Pending #{0:0000} {1} bytes.", totalSubmittedTransfers, transferBuffer.Length); } else { Console.WriteLine("Pending #{0:0000} failed. ErrorCode={1:X8}h", totalSubmittedTransfers, Marshal.GetLastWin32Error()); } } if (!success) { break; } // Wait for the oldest transfer to complete and release the ovlHandle to the pool so it can be re-acquired. success = ovlPool.WaitOldest(out ovlHandle, 1000, KOVL_WAIT_FLAG.RELEASE_ALWAYS, out transferred); totalCompletedTransfers++; if (success) { if ((Test.PipeId & AllKConstants.USB_ENDPOINT_DIRECTION_MASK) > 0) { ProcessMyBufferFromRead(transferBuffers[(totalCompletedTransfers - 1) % Test.MaxPendingIO], transferred); } Console.WriteLine("Complete #{0:0000} {1} bytes.", totalCompletedTransfers, transferred); } else { Console.WriteLine("Complete #{0:0000} Wait failed. ErrorCode={1:X8}h", totalCompletedTransfers, Marshal.GetLastWin32Error()); } } if (!success) { Console.WriteLine("An error occured transferring data. ErrorCode: {0:X8}h", Marshal.GetLastWin32Error()); } ovlPool.Free(); // Free the GC handles allocated for the transfer buffers foreach (GCHandle gcHandle in transferBufferGCList) { gcHandle.Free(); } Done: usb.Free(); }
private static void Main(string[] args) { bool success; WINUSB_PIPE_INFORMATION pipeInfo; UsbK usb; USB_INTERFACE_DESCRIPTOR interfaceDescriptor; // Find and configure the device. if (!Test.ConfigureDevice(out pipeInfo, out usb, out interfaceDescriptor)) { return; } #if BMFW // TODO FOR USER: Remove this block if not using benchmark firmware. // This configures devices running benchmark firmware for streaming DeviceToHost transfers. Console.WriteLine("Configuring for benchmark device.."); BM_TEST_TYPE testType = BM_TEST_TYPE.READ; success = Benchmark.Configure(usb, BM_COMMAND.SET_TEST, interfaceDescriptor.bInterfaceNumber, ref testType); if (!success) { Console.WriteLine("Bench_Configure failed."); } #endif // Create the ISO transfer queue. This class manages the pending and outstanding transfer lists. ReadIsoTransferQueue readXfers = new ReadIsoTransferQueue(usb, ref pipeInfo, Test.MaxOutstandingTransfers, Test.IsoPacketsPerTransfer); if (!Test.ShowTestReady()) { goto Done; } // Always issue a reset pipe prior to re/starting an ISO stream. usb.ResetPipe(pipeInfo.PipeId); // This example will not manage the start frame manually, but this is // how I might caculate the starting point. usb.GetCurrentFrameNumber(out readXfers.FrameNumber); unchecked { // Add some start latency readXfers.FrameNumber += 32; // Start FrameNumber at an interval of 8. readXfers.FrameNumber -= ((readXfers.FrameNumber) % 8); } // This is a counter/timer used only for statistics gathering. Thread.Sleep(0); Test.Dcs.Start(); // Transfer processing loop do { IsoTransferItem isoTransferItem; int transferred; int errorCode; // While buffers exist in the completed list, submit them. while (readXfers.Completed.Count > 0 && readXfers.TotalSubmittedCount < Test.MaxTransfersTotal) { errorCode = readXfers.SubmitNextRead(); if (errorCode != 0) { Console.WriteLine("IsoReadPipe failed. ErrorCode: {0:X8}h", errorCode); goto Done; } } if (readXfers.Outstanding.Count == 0) { // The MAX_TRANSFERS_TOTAL test limit hit. Console.WriteLine("Done!"); goto Done; } // Wait for the oldest transfer to complete. errorCode = readXfers.WaitRead(out isoTransferItem, 1000, out transferred); if (errorCode != 0) { Console.WriteLine("OvlPool.Wait failed. ErrorCode: {0:X8}h", errorCode); goto Done; } // Report iso status. IsoXferReport(readXfers, isoTransferItem, transferred); if (readXfers.CompletedCount == 1) { Test.Dcs.Start(); } } while (true); Done: readXfers.Destroy(); Test.Free(); usb.Free(); }