diff options
Diffstat (limited to 'pc/usbtest.c')
-rw-r--r-- | pc/usbtest.c | 88 |
1 files changed, 69 insertions, 19 deletions
diff --git a/pc/usbtest.c b/pc/usbtest.c index 148fc16..137180f 100644 --- a/pc/usbtest.c +++ b/pc/usbtest.c @@ -114,12 +114,52 @@ static usb_dev_handle * usbOpenDevice(int vendor, char *vendorName, return NULL; } + +void usage(const char* argv0) +{ + printf("Usage: %s read\n" + " %s write <PAGE> <BYTE1> <BYTE2> ... <BYTE32>\n" + " where <PAGE> is 0,1,2,3\n" + " and <BYTEn> is in hex, without 0x.\n" + " example: write 2 13 37 DE AD be ef ...\n\n", argv0, argv0); + exit(0); +} + int main(int argc, char **argv) { usb_dev_handle *handle = NULL; int nBytes = 0; char buffer[256]; + int mode; + #define MODE_READ 0 + #define MODE_WRITE 1 + + int write_page = -1; // which page should be written to? contains 0,1,2,3 + + if (argc <= 1) + usage(argv[0]); + + if (strcmp(argv[1], "read")==0 && argc == 2) + mode=MODE_READ; + else if (strcmp(argv[1], "write")==0 && argc == 2+1+32) + { + mode=MODE_WRITE; + write_page = atoi(argv[2]) - 1; + + if (write_page > 3 || write_page < 0) + { + printf("ERROR: invalid page\n\n"); + exit(1); + } + + for (int i=0; i<32; i++) + buffer[i] = strtol(argv[3+i],NULL,16); + } + else + usage(argv[0]); + + handle = usbOpenDevice(0x16C0, "windfisch.org", 0x05DC, "DS1992 Dumper"); if(handle == NULL) @@ -129,30 +169,40 @@ int main(int argc, char **argv) } printf("got device\n"); - - // read from device - nBytes = usb_control_msg(handle, - USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, - 0x21, 0, 0, (char *)buffer, sizeof(buffer), 1000); - printf("Got %d bytes: %s = \n", nBytes, buffer); - for (int i=0; i<nBytes; i++) - printf("%.2X ",buffer[i] & 0xFF); - printf("\n"); - // write to device - nBytes = usb_control_msg(handle, - USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, - 0x42, 0, 0, "Tschuess", strlen("Tschuess"), 1000); - printf("Wrote %d bytes\n", nBytes); + if (mode == MODE_READ) + { + // read from device + nBytes = usb_control_msg(handle, + USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + 0x21, 0, 0, (char *)buffer, sizeof(buffer), 1000); + printf("Got %d bytes: %s = \n", nBytes, buffer); + for (int i=0; i<nBytes; i++) + printf("%.2X ",buffer[i] & 0xFF); + printf("\n"); + } + else if (mode == MODE_WRITE) + { + buffer[32] = write_page; + // write to device + nBytes = usb_control_msg(handle, + USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, + 0x42, 0, 0, buffer, 32+1 /*length*/, 1000); + printf("Wrote %d bytes\n", nBytes); - // read from device - nBytes = usb_control_msg(handle, - USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, - 0x21, 0, 0, (char *)buffer, sizeof(buffer), 1000); - printf("Got %d bytes: %s;\n", nBytes, buffer); + + // read from device + nBytes = usb_control_msg(handle, + USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + 0x21, 0, 0, (char *)buffer, sizeof(buffer), 1000); + printf("Got %d bytes: %s = \n", nBytes, buffer); + for (int i=0; i<nBytes; i++) + printf("%.2X ",buffer[i] & 0xFF); + printf("\n"); + } /* |