summaryrefslogtreecommitdiff
path: root/tools/usb_benchmark.c
blob: 6d4b27ccbf53b1e7b1a408e88bf0d7d48f8c7d04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
	LPCUSB, an USB device driver for LPC microcontrollers	
	Copyright (C) 2006 Bertrik Sikken (bertrik@sikken.nl)

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

/*
	Simple benchmarking application.
	
	It talks with the 'custom' device application on the LPC214x through
	libusb.

        2007-11-01: Some minor modifications by <bjorn@haxx.se>

*/

#include <stdio.h>
#include <unistd.h>
#include <sys/timeb.h>

#include "usb.h"

// types
#ifndef MIN
#define MIN(a,b)	((a)<(b)?(a):(b))
#endif
typedef unsigned int U32;
typedef unsigned char U8;

#define MAX_TIME 3000

static unsigned char abData[16384];

// USB device specific definitions
#define VENDOR_ID	0x0781
#define PRODUCT_ID	0x7450

#define	BM_REQUEST_TYPE		(2<<5)
#define BULK_IN_EP			0x82
#define BULK_OUT_EP			0x05

// this structure should match with the expectations of the 'custom' device!
typedef struct {
	U32		dwAddress;
	U32		dwLength;
} TMemoryCmd;


static struct usb_device * find_device(int iVendor, int iProduct)
{
	struct usb_bus	*usb_bus;
	struct usb_device *dev;	
	
	for (usb_bus = usb_get_busses(); usb_bus; usb_bus = usb_bus->next) {
		for (dev = usb_bus->devices; dev; dev = dev->next) {
			if ((dev->descriptor.idVendor == iVendor) && 
				(dev->descriptor.idProduct == iProduct)) {
				return dev;
			}
		}
	}
	return NULL;
}


static struct timeb start;

static void starttimer(void)
{
	ftime(&start);
}

static int stoptimer(void)
{
	struct timeb now;
	
	ftime(&now);
	return 1000 * (now.time - start.time) + now.millitm - start.millitm;
}


int main(void)
{
    const int blocksize[] = { 128, 512 };
	struct usb_device *dev;	
	struct usb_dev_handle *hdl;
	int i, j;
	U32 dwBlockSize, dwChunk, dwBytes;
	TMemoryCmd MemCmd;
	int iTimer;

	usb_init();
	usb_find_busses();
	usb_find_devices();
	
        for (i=0; i<sizeof abData/4; i++)
            ((unsigned int*)abData)[i] = i;

	dev = find_device(VENDOR_ID, PRODUCT_ID);
	if (dev == NULL) {
		fprintf(stderr, "device not found\n");
		return -1;
	}
	
	hdl = usb_open(dev);
	
	i = usb_set_configuration(hdl, 1);
	if (i < 0) {
		fprintf(stderr, "usb_set_configuration failed\n");
	}
	
	i = usb_claim_interface(hdl, 0);
	if (i < 0) {
		fprintf(stderr, "usb_claim_interface failed %d\n", i);
		return -1;
	}


	// read some data
	for (j = 0; j < 1; j++) {
                dwBlockSize = blocksize[j];
		fprintf(stderr, "Testing blocksize %5d\n", dwBlockSize);

#if 1
		fprintf(stderr, "* write:");
		// send a vendor request for a write
		MemCmd.dwAddress = 0;
		MemCmd.dwLength = 20 * 1024;
		i = usb_control_msg(hdl, BM_REQUEST_TYPE, 0x02, 20, 1024, NULL, 0, 1000);
		if (i < 0) {
			fprintf(stderr, "usb_control_msg failed %d\n", i);
                        break;
		}
		dwBytes = 0;
		starttimer();
		while (MemCmd.dwLength > 0) {
			dwChunk = MIN(dwBlockSize, MemCmd.dwLength);
			i = usb_bulk_write(hdl, 0x01, (char *)abData, dwChunk, 2000);
			if (i < 1) {
				fprintf(stderr, "usb_bulk_write failed %d\n", i);
				break;
			}
			MemCmd.dwLength -= dwChunk;
			dwBytes += dwBlockSize;
			if (stoptimer() > MAX_TIME) {
				break;
			}
                        ((unsigned int*)abData)[0]++;
                }
                if (i<0)
                    break;
		iTimer = stoptimer();
                if (iTimer)
                    fprintf(stderr, " %7d bytes in %d ms = %d kB/s\n", dwBytes, iTimer, dwBytes / iTimer);
		// stdout
		printf("%d,%d,%d\n", dwBlockSize, dwBytes, iTimer);
#endif
#if 1
		fprintf(stderr, "* read :");
		// send a vendor request for a read
		MemCmd.dwAddress = 0;
		MemCmd.dwLength = 20 * 1024;
		i = usb_control_msg(hdl, BM_REQUEST_TYPE, 0x01, 20, 1024, NULL, 0, 1000);
		if (i < 0) {
			fprintf(stderr, "usb_control_msg failed %d\n", i);
                        break;
		}
		dwBytes = 0;
		starttimer();
		while (MemCmd.dwLength > 0) {
			dwChunk = MIN(dwBlockSize, MemCmd.dwLength);
			i = usb_bulk_read(hdl, 0x82, (char *)abData, dwChunk, 2000);
			if (i < 1) {
				fprintf(stderr, "usb_bulk_read failed %d\n", i);
				break;
			}
			MemCmd.dwLength -= dwChunk;
			dwBytes += dwBlockSize;
			if (stoptimer() > MAX_TIME) {
				break;
			}
		}
                if (i<0)
                    break;
		iTimer = stoptimer();
                if (iTimer)
                    fprintf(stderr, " %7d bytes in %d ms = %d kB/s\n", dwBytes, iTimer, dwBytes / iTimer);
		// stdout
		printf("%d,%d,%d\n", dwBlockSize, dwBytes, iTimer);
#endif
	}


	usb_release_interface(hdl, 0);
	usb_close(hdl);

	return 0;
}