usb_application_benchmark.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include "usbapp/usb_application_benchmark.h"
  2. #include "usb/usb_bridge.h"
  3. #include "usb/usb_config.h"
  4. #include "usbd_usbtmc.h"
  5. #include "crc32.h"
  6. static uint8_t selectedTest = 0;
  7. static unsigned long crc = 0;
  8. static int8_t fBenchmarkInit();
  9. static int8_t fBenchmarkDeInit();
  10. static void fBenchmarkReset();
  11. static bool fBenchmarkSetup( const tUSBSetupPacket_t * pSetup, bool bFirstStage, bool success );
  12. static size_t fBenchmarkControlRx( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * rx, size_t idx, size_t bytesRemaining );
  13. static size_t fBenchmarkControlTx( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * tx, size_t idx, size_t bytesRemaining );
  14. uint8_t BENCHMARK_datain_beginsend( uint8_t epnum );
  15. uint8_t BENCHMARK_datain_sendprepared( uint8_t epnum );
  16. const sUSBAppEntry_Control_t usbapplication_BENCHMARK_control = {
  17. .fUsbInit = fBenchmarkInit,
  18. .fUsbDeInit = fBenchmarkDeInit,
  19. .fUsbSetup = fBenchmarkSetup,
  20. .fUsbCtlEpRx = fBenchmarkControlRx,
  21. .fUsbCtlEpTx = fBenchmarkControlTx,
  22. .fResetEvent = fBenchmarkReset,
  23. };
  24. static bool fBenchmark_DataInitHandler( uint8_t bEpLogAddress );
  25. static bool fBenchmark_DataRxHandler( uint8_t bEpLogAddress, sUSBTransfer_t * rx );
  26. static bool fBenchmark_DataTxHandler( uint8_t bEpLogAddress, sUSBTransfer_t * tx );
  27. static bool fBenchmark_DataErrHandler( uint8_t bEpLogAddress, uint32_t error );
  28. static void fBenchmark_DataDeInitHandler( uint8_t bEpLogAddress );
  29. const sUSBAppEntry_Data_t usbapplication_BENCHMARK_dataapp = {
  30. .fDataInitHandler = fBenchmark_DataInitHandler,
  31. .fDataRxHandler = fBenchmark_DataRxHandler,
  32. .fDataTxHandler = fBenchmark_DataTxHandler,
  33. .fDataErrHandler = fBenchmark_DataErrHandler,
  34. .fDataDeInitHandler = fBenchmark_DataDeInitHandler,
  35. };
  36. static size_t g_TxByte = 0;
  37. static size_t g_RxByte = 0;
  38. static size_t g_RxBytesReceived = 0;
  39. static uint8_t g_BenchmarkBuffer[ 64 ];
  40. static int8_t fBenchmarkInit()
  41. {
  42. // if( !usb_bridge_register_dataapp( USBTMC_BENCHMARK_EP, &usbapplication_BENCHMARK_dataapp ) )
  43. // return 1;
  44. return 0;
  45. }
  46. static int8_t fBenchmarkDeInit()
  47. {
  48. // usb_bridge_unregister_dataapp( USBTMC_BENCHMARK_EP );
  49. return 0;
  50. }
  51. static void fBenchmarkReset()
  52. {
  53. }
  54. static bool fBenchmarkSetup( const tUSBSetupPacket_t * pSetup, bool bFirstStage, bool success )
  55. {
  56. switch( pSetup->bRequest )
  57. {
  58. // Benchmark select test:
  59. // bRequest = 0x0E
  60. // wValue = 0(None), 1(Read), 2(Write), 3(Loop)
  61. case 0x0E:
  62. {
  63. switch( pSetup->wValue )
  64. {
  65. case 0: // None
  66. case 1: // Read
  67. case 2: // Write
  68. case 3: // Loop
  69. selectedTest = pSetup->wValue;
  70. g_TxByte = 0; g_RxByte = 0;
  71. memset( g_BenchmarkBuffer, 0, sizeof(g_BenchmarkBuffer) );
  72. g_RxBytesReceived = 0;
  73. crc = ICRC32;
  74. sUSBTransfer_t * tx = USBD_USBTMC_GetDataTxTransferHandle();
  75. sUSBTransfer_t * rx = USBD_USBTMC_GetDataRxTransferHandle();
  76. usb_reset_transfer( tx );
  77. usb_reset_transfer( rx );
  78. return true; //bibb
  79. }
  80. }
  81. break;
  82. // Benchmark get selected test:
  83. // bRequest = 0x0F
  84. case 0x0F:
  85. {
  86. return true; //bibb
  87. }
  88. }
  89. return false;
  90. }
  91. static size_t fBenchmarkControlRx( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * rx, size_t idx, size_t bytesRemaining )
  92. {
  93. return 0;
  94. }
  95. static size_t fBenchmarkControlTx( const tUSBSetupPacket_t * pSetup, sUSBTransfer_t * tx, size_t idx, size_t bytesRemaining )
  96. {
  97. return usb_write_transfer( tx, &selectedTest, 1 ); // Select test accept or return selected test
  98. }
  99. static bool fBenchmark_DataInitHandler( uint8_t bEpLogAddress )
  100. {
  101. return true;
  102. }
  103. static bool fBenchmark_DataRxHandler( uint8_t bEpLogAddress, sUSBTransfer_t * rx )
  104. {
  105. size_t n = usb_count_transfer(rx);
  106. g_RxBytesReceived += n;
  107. crc = CRC32( crc, usb_transfer_raw_read(rx), n );
  108. sUSBTransfer_t * tx = USBD_USBTMC_GetDataTxTransferHandle();
  109. bool first = ( usb_count_transfer(tx) == 0 );
  110. if( n < usb_copy_transfer( tx, rx, n, false ) )
  111. {
  112. // tx buffer full
  113. asm("bkpt #0");
  114. }
  115. usb_reset_transfer( rx );
  116. if( first )
  117. BENCHMARK_datain_sendprepared( bEpLogAddress );
  118. return true;
  119. while( n > 0 )
  120. {
  121. size_t pos = g_RxByte % sizeof(g_BenchmarkBuffer);
  122. size_t free = sizeof(g_BenchmarkBuffer) - pos;
  123. size_t bytes = usb_read_transfer( rx, &g_BenchmarkBuffer[ pos ], free, false );
  124. g_RxByte += bytes;
  125. n -= bytes;
  126. }
  127. usb_bridge_datain_beginsend( bEpLogAddress );
  128. return true;
  129. }
  130. static bool fBenchmark_DataTxHandler( uint8_t bEpLogAddress, sUSBTransfer_t * tx )
  131. {
  132. // for( ;; )
  133. // {
  134. // size_t TxByte = g_TxByte++;
  135. // if( 0 == usb_write_transfer( tx, &g_BenchmarkBuffer[TxByte % sizeof(g_BenchmarkBuffer)], 1 ) )
  136. // {
  137. // break;
  138. // }
  139. // }
  140. return true;
  141. }
  142. static bool fBenchmark_DataErrHandler( uint8_t bEpLogAddress, uint32_t error )
  143. {
  144. return true;
  145. }
  146. static void fBenchmark_DataDeInitHandler( uint8_t bEpLogAddress )
  147. {
  148. }
  149. // BENCHMARK_datain_beginsend()
  150. // Begin the transmission using already prepared TX-transfer.
  151. // Due to the hardware does not support NAK-sent-interrupt, it is
  152. // required to initiate the first packet sending to start TX-flow (DataIN).
  153. // This function issues the first packet sending using virtual DataIN event.
  154. //
  155. uint8_t BENCHMARK_datain_beginsend( uint8_t epnum )
  156. {
  157. // issue virtual DataIN event:
  158. return USBD_USBTMC_DataIn_BeginSend( epnum, false );
  159. }
  160. // BENCHMARK_datain_sendprepared()
  161. // Begin the transmission using DataIN handler virtual call.
  162. // Due to the hardware does not support NAK-sent-interrupt, it is
  163. // required to initiate the first packet sending to start TX-flow (DataIN).
  164. // This function issues the first packet sending using virtual DataIN event.
  165. //
  166. uint8_t BENCHMARK_datain_sendprepared( uint8_t epnum )
  167. {
  168. // issue virtual DataIN event:
  169. return USBD_USBTMC_DataIn_BeginSend( epnum, true );
  170. }