FS#10053 - Sansa AMS >2GB support by Rafaël Carré. Enables bank switching on
[kugel-rb/myfork.git] / tools / usb_benchmark.c
blob6d4b27ccbf53b1e7b1a408e88bf0d7d48f8c7d04
1 /*
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
24 libusb.
26 2007-11-01: Some minor modifications by <bjorn@haxx.se>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <sys/timeb.h>
34 #include "usb.h"
36 // types
37 #ifndef MIN
38 #define MIN(a,b) ((a)<(b)?(a):(b))
39 #endif
40 typedef unsigned int U32;
41 typedef unsigned char U8;
43 #define MAX_TIME 3000
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!
56 typedef struct {
57 U32 dwAddress;
58 U32 dwLength;
59 } TMemoryCmd;
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)) {
71 return dev;
75 return NULL;
79 static struct timeb start;
81 static void starttimer(void)
83 ftime(&start);
86 static int stoptimer(void)
88 struct timeb now;
90 ftime(&now);
91 return 1000 * (now.time - start.time) + now.millitm - start.millitm;
95 int main(void)
97 const int blocksize[] = { 128, 512 };
98 struct usb_device *dev;
99 struct usb_dev_handle *hdl;
100 int i, j;
101 U32 dwBlockSize, dwChunk, dwBytes;
102 TMemoryCmd MemCmd;
103 int iTimer;
105 usb_init();
106 usb_find_busses();
107 usb_find_devices();
109 for (i=0; i<sizeof abData/4; i++)
110 ((unsigned int*)abData)[i] = i;
112 dev = find_device(VENDOR_ID, PRODUCT_ID);
113 if (dev == NULL) {
114 fprintf(stderr, "device not found\n");
115 return -1;
118 hdl = usb_open(dev);
120 i = usb_set_configuration(hdl, 1);
121 if (i < 0) {
122 fprintf(stderr, "usb_set_configuration failed\n");
125 i = usb_claim_interface(hdl, 0);
126 if (i < 0) {
127 fprintf(stderr, "usb_claim_interface failed %d\n", i);
128 return -1;
132 // read some data
133 for (j = 0; j < 1; j++) {
134 dwBlockSize = blocksize[j];
135 fprintf(stderr, "Testing blocksize %5d\n", dwBlockSize);
137 #if 1
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);
143 if (i < 0) {
144 fprintf(stderr, "usb_control_msg failed %d\n", i);
145 break;
147 dwBytes = 0;
148 starttimer();
149 while (MemCmd.dwLength > 0) {
150 dwChunk = MIN(dwBlockSize, MemCmd.dwLength);
151 i = usb_bulk_write(hdl, 0x01, (char *)abData, dwChunk, 2000);
152 if (i < 1) {
153 fprintf(stderr, "usb_bulk_write failed %d\n", i);
154 break;
156 MemCmd.dwLength -= dwChunk;
157 dwBytes += dwBlockSize;
158 if (stoptimer() > MAX_TIME) {
159 break;
161 ((unsigned int*)abData)[0]++;
163 if (i<0)
164 break;
165 iTimer = stoptimer();
166 if (iTimer)
167 fprintf(stderr, " %7d bytes in %d ms = %d kB/s\n", dwBytes, iTimer, dwBytes / iTimer);
168 // stdout
169 printf("%d,%d,%d\n", dwBlockSize, dwBytes, iTimer);
170 #endif
171 #if 1
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);
177 if (i < 0) {
178 fprintf(stderr, "usb_control_msg failed %d\n", i);
179 break;
181 dwBytes = 0;
182 starttimer();
183 while (MemCmd.dwLength > 0) {
184 dwChunk = MIN(dwBlockSize, MemCmd.dwLength);
185 i = usb_bulk_read(hdl, 0x82, (char *)abData, dwChunk, 2000);
186 if (i < 1) {
187 fprintf(stderr, "usb_bulk_read failed %d\n", i);
188 break;
190 MemCmd.dwLength -= dwChunk;
191 dwBytes += dwBlockSize;
192 if (stoptimer() > MAX_TIME) {
193 break;
196 if (i<0)
197 break;
198 iTimer = stoptimer();
199 if (iTimer)
200 fprintf(stderr, " %7d bytes in %d ms = %d kB/s\n", dwBytes, iTimer, dwBytes / iTimer);
201 // stdout
202 printf("%d,%d,%d\n", dwBlockSize, dwBytes, iTimer);
203 #endif
207 usb_release_interface(hdl, 0);
208 usb_close(hdl);
210 return 0;