2 LPCUSB, an USB device driver for LPC microcontrollers
3 Copyright (C) 2006 Bertrik Sikken (bertrik@sikken.nl)
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 Simple benchmarking application.
23 It talks with the 'custom' device application on the LPC214x through
26 2007-11-01: Some minor modifications by <bjorn@haxx.se>
32 #include <sys/timeb.h>
38 #define MIN(a,b) ((a)<(b)?(a):(b))
40 typedef unsigned int U32
;
41 typedef unsigned char U8
;
45 static unsigned char abData
[16384];
47 // USB device specific definitions
48 #define VENDOR_ID 0x0781
49 #define PRODUCT_ID 0x7450
51 #define BM_REQUEST_TYPE (2<<5)
52 #define BULK_IN_EP 0x82
53 #define BULK_OUT_EP 0x05
55 // this structure should match with the expectations of the 'custom' device!
62 static struct usb_device
* find_device(int iVendor
, int iProduct
)
64 struct usb_bus
*usb_bus
;
65 struct usb_device
*dev
;
67 for (usb_bus
= usb_get_busses(); usb_bus
; usb_bus
= usb_bus
->next
) {
68 for (dev
= usb_bus
->devices
; dev
; dev
= dev
->next
) {
69 if ((dev
->descriptor
.idVendor
== iVendor
) &&
70 (dev
->descriptor
.idProduct
== iProduct
)) {
79 static struct timeb start
;
81 static void starttimer(void)
86 static int stoptimer(void)
91 return 1000 * (now
.time
- start
.time
) + now
.millitm
- start
.millitm
;
97 const int blocksize
[] = { 128, 512 };
98 struct usb_device
*dev
;
99 struct usb_dev_handle
*hdl
;
101 U32 dwBlockSize
, dwChunk
, dwBytes
;
109 for (i
=0; i
<sizeof abData
/4; i
++)
110 ((unsigned int*)abData
)[i
] = i
;
112 dev
= find_device(VENDOR_ID
, PRODUCT_ID
);
114 fprintf(stderr
, "device not found\n");
120 i
= usb_set_configuration(hdl
, 1);
122 fprintf(stderr
, "usb_set_configuration failed\n");
125 i
= usb_claim_interface(hdl
, 0);
127 fprintf(stderr
, "usb_claim_interface failed %d\n", i
);
133 for (j
= 0; j
< 1; j
++) {
134 dwBlockSize
= blocksize
[j
];
135 fprintf(stderr
, "Testing blocksize %5d\n", dwBlockSize
);
138 fprintf(stderr
, "* write:");
139 // send a vendor request for a write
140 MemCmd
.dwAddress
= 0;
141 MemCmd
.dwLength
= 20 * 1024;
142 i
= usb_control_msg(hdl
, BM_REQUEST_TYPE
, 0x02, 20, 1024, NULL
, 0, 1000);
144 fprintf(stderr
, "usb_control_msg failed %d\n", i
);
149 while (MemCmd
.dwLength
> 0) {
150 dwChunk
= MIN(dwBlockSize
, MemCmd
.dwLength
);
151 i
= usb_bulk_write(hdl
, 0x01, (char *)abData
, dwChunk
, 2000);
153 fprintf(stderr
, "usb_bulk_write failed %d\n", i
);
156 MemCmd
.dwLength
-= dwChunk
;
157 dwBytes
+= dwBlockSize
;
158 if (stoptimer() > MAX_TIME
) {
161 ((unsigned int*)abData
)[0]++;
165 iTimer
= stoptimer();
167 fprintf(stderr
, " %7d bytes in %d ms = %d kB/s\n", dwBytes
, iTimer
, dwBytes
/ iTimer
);
169 printf("%d,%d,%d\n", dwBlockSize
, dwBytes
, iTimer
);
172 fprintf(stderr
, "* read :");
173 // send a vendor request for a read
174 MemCmd
.dwAddress
= 0;
175 MemCmd
.dwLength
= 20 * 1024;
176 i
= usb_control_msg(hdl
, BM_REQUEST_TYPE
, 0x01, 20, 1024, NULL
, 0, 1000);
178 fprintf(stderr
, "usb_control_msg failed %d\n", i
);
183 while (MemCmd
.dwLength
> 0) {
184 dwChunk
= MIN(dwBlockSize
, MemCmd
.dwLength
);
185 i
= usb_bulk_read(hdl
, 0x82, (char *)abData
, dwChunk
, 2000);
187 fprintf(stderr
, "usb_bulk_read failed %d\n", i
);
190 MemCmd
.dwLength
-= dwChunk
;
191 dwBytes
+= dwBlockSize
;
192 if (stoptimer() > MAX_TIME
) {
198 iTimer
= stoptimer();
200 fprintf(stderr
, " %7d bytes in %d ms = %d kB/s\n", dwBytes
, iTimer
, dwBytes
/ iTimer
);
202 printf("%d,%d,%d\n", dwBlockSize
, dwBytes
, iTimer
);
207 usb_release_interface(hdl
, 0);