custom_client.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. LPCUSB, an USB device driver for LPC microcontrollers
  3. Copyright (C) 2006 Bertrik Sikken (bertrik@sikken.nl)
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. */
  16. /*
  17. Simple benchmarking application.
  18. It talks with the 'custom' device application on the LPC214x through
  19. libusb.
  20. */
  21. #include <stdio.h>
  22. #include <unistd.h>
  23. #include <sys/timeb.h>
  24. #include "usb.h"
  25. // types
  26. #ifndef MIN
  27. #define MIN(a,b) ((a)<(b)?(a):(b))
  28. #endif
  29. typedef unsigned int U32;
  30. typedef unsigned char U8;
  31. #define MAX_TIME 3000
  32. static unsigned char abData[16384];
  33. // USB device specific definitions
  34. #define VENDOR_ID 0xFFFF
  35. #define PRODUCT_ID 0x0004
  36. #define BM_REQUEST_TYPE (2<<5)
  37. #define BULK_IN_EP 0x82
  38. #define BULK_OUT_EP 0x05
  39. // this structure should match with the expectations of the 'custom' device!
  40. typedef struct {
  41. U32 dwAddress;
  42. U32 dwLength;
  43. } TMemoryCmd;
  44. static struct usb_device * find_device(int iVendor, int iProduct)
  45. {
  46. struct usb_bus *usb_bus;
  47. struct usb_device *dev;
  48. for (usb_bus = usb_get_busses(); usb_bus; usb_bus = usb_bus->next) {
  49. for (dev = usb_bus->devices; dev; dev = dev->next) {
  50. if ((dev->descriptor.idVendor == iVendor) &&
  51. (dev->descriptor.idProduct == iProduct)) {
  52. return dev;
  53. }
  54. }
  55. }
  56. return NULL;
  57. }
  58. static struct timeb start;
  59. static void starttimer(void)
  60. {
  61. ftime(&start);
  62. }
  63. static int stoptimer(void)
  64. {
  65. struct timeb now;
  66. ftime(&now);
  67. return 1000 * (now.time - start.time) + now.millitm - start.millitm;
  68. }
  69. int main(int argc, char *argv[])
  70. {
  71. struct usb_device *dev;
  72. struct usb_dev_handle *hdl;
  73. int i, j;
  74. U32 dwBlockSize, dwChunk, dwBytes;
  75. TMemoryCmd MemCmd;
  76. int iTimer;
  77. usb_init();
  78. usb_find_busses();
  79. usb_find_devices();
  80. dev = find_device(VENDOR_ID, PRODUCT_ID);
  81. if (dev == NULL) {
  82. fprintf(stderr, "device not found\n");
  83. return -1;
  84. }
  85. hdl = usb_open(dev);
  86. i = usb_set_configuration(hdl, 1);
  87. if (i < 0) {
  88. fprintf(stderr, "usb_set_configuration failed\n");
  89. }
  90. i = usb_claim_interface(hdl, 0);
  91. if (i < 0) {
  92. fprintf(stderr, "usb_claim_interface failed %d\n", i);
  93. return -1;
  94. }
  95. // read some data
  96. for (j = 6; j < 15; j++) {
  97. dwBlockSize = (1 << j);
  98. fprintf(stderr, "Testing blocksize %5d\n", dwBlockSize);
  99. fprintf(stderr, "* read :");
  100. // send a vendor request for a read
  101. MemCmd.dwAddress = 0;
  102. MemCmd.dwLength = 1024 * 1024;
  103. i = usb_control_msg(hdl, BM_REQUEST_TYPE, 0x01, 0, 0, (char *)&MemCmd, sizeof(MemCmd), 1000);
  104. if (i < 0) {
  105. fprintf(stderr, "usb_control_msg failed %d\n", i);
  106. }
  107. dwBytes = 0;
  108. starttimer();
  109. while (MemCmd.dwLength > 0) {
  110. dwChunk = MIN(dwBlockSize, MemCmd.dwLength);
  111. i = usb_bulk_read(hdl, 0x82, (char *)abData, dwBlockSize, 2000);
  112. if (i < 0) {
  113. fprintf(stderr, "usb_bulk_read failed %d\n", i);
  114. break;
  115. }
  116. MemCmd.dwLength -= dwChunk;
  117. dwBytes += dwBlockSize;
  118. if (stoptimer() > MAX_TIME) {
  119. break;
  120. }
  121. }
  122. iTimer = stoptimer();
  123. fprintf(stderr, " %7d bytes in %d ms = %d kB/s\n", dwBytes, iTimer, dwBytes / iTimer);
  124. // stdout
  125. printf("%d,%d,%d,", dwBlockSize, dwBytes, iTimer);
  126. fprintf(stderr, "* write:");
  127. // send a vendor request for a write
  128. MemCmd.dwAddress = 0;
  129. MemCmd.dwLength = 1024 * 1024;
  130. i = usb_control_msg(hdl, BM_REQUEST_TYPE, 0x02, 0, 0, (char *)&MemCmd, sizeof(MemCmd), 1000);
  131. if (i < 0) {
  132. fprintf(stderr, "usb_control_msg failed %d\n", i);
  133. }
  134. dwBytes = 0;
  135. starttimer();
  136. while (MemCmd.dwLength > 0) {
  137. dwChunk = MIN(dwBlockSize, MemCmd.dwLength);
  138. i = usb_bulk_write(hdl, 0x05, (char *)abData, dwBlockSize, 2000);
  139. if (i < 0) {
  140. fprintf(stderr, "usb_bulk_read failed %d\n", i);
  141. break;
  142. }
  143. MemCmd.dwLength -= dwChunk;
  144. dwBytes += dwBlockSize;
  145. if (stoptimer() > MAX_TIME) {
  146. break;
  147. }
  148. }
  149. fprintf(stderr, " %7d bytes in %d ms = %d kB/s\n", dwBytes, iTimer, dwBytes / iTimer);
  150. // stdout
  151. printf("%d,%d,%d\n", dwBlockSize, dwBytes, iTimer);
  152. }
  153. usb_release_interface(hdl, 0);
  154. usb_close(hdl);
  155. return 0;
  156. }