aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorChristian Starkjohann <cs+github@obdev.at>2008-05-13 15:15:13 +0000
committerChristian Starkjohann <cs+github@obdev.at>2008-05-13 15:15:13 +0000
commitdfcb2cf64a95e99e5325bf3290697ac7823994bc (patch)
treeefd8ac1bbba5bb0d3c5dbdbe56fb8d8cd652f5cd /examples
parent1cc341a56727a0d1bcc3a2eb3d4f2a312ee740a3 (diff)
downloadv-usb-dfcb2cf64a95e99e5325bf3290697ac7823994bc.tar.gz
v-usb-dfcb2cf64a95e99e5325bf3290697ac7823994bc.tar.bz2
v-usb-dfcb2cf64a95e99e5325bf3290697ac7823994bc.zip
- added (optional) reliability test in custom-class example
Diffstat (limited to 'examples')
-rw-r--r--examples/custom-class/commandline/set-led.c34
-rw-r--r--examples/custom-class/firmware/main.c11
-rw-r--r--examples/custom-class/firmware/requests.h4
3 files changed, 46 insertions, 3 deletions
diff --git a/examples/custom-class/commandline/set-led.c b/examples/custom-class/commandline/set-led.c
index 07ba094..807adac 100644
--- a/examples/custom-class/commandline/set-led.c
+++ b/examples/custom-class/commandline/set-led.c
@@ -33,6 +33,9 @@ static void usage(char *name)
fprintf(stderr, " %s on ....... turn on LED\n", name);
fprintf(stderr, " %s off ...... turn off LED\n", name);
fprintf(stderr, " %s status ... ask current status of LED\n", name);
+#if HAVE_TEST
+ fprintf(stderr, " %s test ..... run driver reliability test\n", name);
+#endif
}
int main(int argc, char **argv)
@@ -40,7 +43,7 @@ int main(int argc, char **argv)
usb_dev_handle *handle = NULL;
const unsigned char rawVid[2] = {USB_CFG_VENDOR_ID}, rawPid[2] = {USB_CFG_DEVICE_ID};
char vendor[] = {USB_CFG_VENDOR_NAME, 0}, product[] = {USB_CFG_DEVICE_NAME, 0};
-char buffer[1];
+char buffer[4];
int cnt, vid, pid, isOn;
usb_init();
@@ -75,6 +78,35 @@ int cnt, vid, pid, isOn;
if(cnt < 0){
fprintf(stderr, "USB error: %s\n", usb_strerror());
}
+#if HAVE_TEST
+ }else if(strcasecmp(argv[1], "test") == 0){
+ int i;
+ srandomdev();
+ for(i = 0; i < 5000; i++){
+ int value = random() & 0xffff, index = random() & 0xffff;
+ int rxValue, rxIndex;
+ if((i+1) % 100 == 0){
+ fprintf(stderr, "\r%04d", i+1);
+ fflush(stderr);
+ }
+ cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_ECHO, value, index, buffer, sizeof(buffer), 5000);
+ if(cnt < 0){
+ fprintf(stderr, "\nUSB error in iteration %d: %s\n", i, usb_strerror());
+ break;
+ }else if(cnt != 4){
+ fprintf(stderr, "\nerror in iteration %d: %d bytes received instead of 4\n", i, cnt);
+ break;
+ }
+ rxValue = ((int)buffer[0] & 0xff) | (((int)buffer[1] & 0xff) << 8);
+ rxIndex = ((int)buffer[2] & 0xff) | (((int)buffer[3] & 0xff) << 8);
+ if(rxValue != value || rxIndex != index){
+ fprintf(stderr, "\ndata error in iteration %d:\n", i);
+ fprintf(stderr, "rxValue = 0x%04x value = 0x%04x\n", rxValue, value);
+ fprintf(stderr, "rxIndex = 0x%04x index = 0x%04x\n", rxIndex, index);
+ }
+ }
+ fprintf(stderr, "\nTest completed.\n");
+#endif
}else{
usage(argv[0]);
exit(1);
diff --git a/examples/custom-class/firmware/main.c b/examples/custom-class/firmware/main.c
index 940f62e..e12ef00 100644
--- a/examples/custom-class/firmware/main.c
+++ b/examples/custom-class/firmware/main.c
@@ -37,15 +37,22 @@ different port or bit, change the macros below:
usbMsgLen_t usbFunctionSetup(uchar data[8])
{
usbRequest_t *rq = (void *)data;
+static uchar dataBuffer[4]; /* buffer must stay valid when usbFunctionSetup returns */
- if(rq->bRequest == CUSTOM_RQ_SET_STATUS){
+ if(rq->bRequest == CUSTOM_RQ_ECHO){ /* echo -- used for reliability tests */
+ dataBuffer[0] = rq->wValue.bytes[0];
+ dataBuffer[1] = rq->wValue.bytes[1];
+ dataBuffer[2] = rq->wIndex.bytes[0];
+ dataBuffer[3] = rq->wIndex.bytes[1];
+ usbMsgPtr = dataBuffer; /* tell the driver which data to return */
+ return 4;
+ }else if(rq->bRequest == CUSTOM_RQ_SET_STATUS){
if(rq->wValue.bytes[0] & 1){ /* set LED */
LED_PORT_OUTPUT |= _BV(LED_BIT);
}else{ /* clear LED */
LED_PORT_OUTPUT &= ~_BV(LED_BIT);
}
}else if(rq->bRequest == CUSTOM_RQ_GET_STATUS){
- static uchar dataBuffer[1]; /* buffer must stay valid when usbFunctionSetup returns */
dataBuffer[0] = ((LED_PORT_OUTPUT & _BV(LED_BIT)) != 0);
usbMsgPtr = dataBuffer; /* tell the driver which data to return */
return 1; /* tell the driver to send 1 byte */
diff --git a/examples/custom-class/firmware/requests.h b/examples/custom-class/firmware/requests.h
index b6a3c2b..2e7fac3 100644
--- a/examples/custom-class/firmware/requests.h
+++ b/examples/custom-class/firmware/requests.h
@@ -16,6 +16,10 @@
#ifndef __REQUESTS_H_INCLUDED__
#define __REQUESTS_H_INCLUDED__
+#define CUSTOM_RQ_ECHO 0
+/* Request that the device sends back wValue and wIndex. This is used with
+ * random data to test the reliability of the communication.
+ */
#define CUSTOM_RQ_SET_STATUS 1
/* Set the LED status. Control-OUT.
* The requested status is passed in the "wValue" field of the control