From c23307d11666127bfd495dba8839d0e54e23f0aa Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Sun, 24 Jan 2016 18:33:50 +0100 Subject: . --- avr/main.c | 22 ++++------ pc/a.out | Bin 12744 -> 12856 bytes pc/main.c | 134 ----------------------------------------------------------- pc/usbtest.c | 88 ++++++++++++++++++++++++++++++--------- 4 files changed, 77 insertions(+), 167 deletions(-) delete mode 100644 pc/main.c diff --git a/avr/main.c b/avr/main.c index 635c44d..9810e49 100644 --- a/avr/main.c +++ b/avr/main.c @@ -43,6 +43,9 @@ #define FUNC_START_BOOTLOADER 30 #define FUNC_GET_TYPE 0xFE +volatile int got_data = 0; // set to 1, if we get data to write from the PC + + void jump_to_bootloader(void) { cli(); @@ -67,6 +70,7 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) len = 0; break; case FUNC_READ: + got_data = 1; return USB_NO_MSG; case FUNC_WRITE: @@ -149,7 +153,7 @@ int main(void) wdt_reset(); // keep the watchdog happy usbPoll(); - if (++c % 3000 == 0) + if (++c % (got_data ? 1500 : 3000) == 0) { PORTC^=LED_BLUE; PORTC |= LED_RED | LED_GREEN; @@ -157,27 +161,17 @@ int main(void) if (c % 12000 == 0) { uint8_t result = w1_reset(); - if (result == 0) + if (result == 0) // device detected { PORTC &= ~LED_GREEN; ds1992_read(0, replyBuffer, 128); } - else if (result == 1) + else if (result == 1) // no presence pulse == no device detected PORTC &= ~LED_RED; - else + else // short circuit detected PORTC &= ~(LED_RED | LED_GREEN); } - /*if ( (c / 3000) % 40 == 0) - { - PORTC &= ~LED_RED; - usb_disconnect(); - } - else if ((c / 3000) % 40 == 20) - { - usb_reconnect(); - PORTC |= LED_RED; - }*/ } jump_to_bootloader(); diff --git a/pc/a.out b/pc/a.out index fbba741..c271ddd 100755 Binary files a/pc/a.out and b/pc/a.out differ diff --git a/pc/main.c b/pc/main.c deleted file mode 100644 index fe0d75d..0000000 --- a/pc/main.c +++ /dev/null @@ -1,134 +0,0 @@ -/* Name: set-led.c - * Project: custom-class, a basic USB example - * Author: Christian Starkjohann - * Creation Date: 2008-04-10 - * Tabsize: 4 - * Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH - * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt) - */ - -/* -General Description: -This is the host-side driver for the custom-class example device. It searches -the USB for the LEDControl device and sends the requests understood by this -device. -This program must be linked with libusb on Unix and libusb-win32 on Windows. -See http://libusb.sourceforge.net/ or http://libusb-win32.sourceforge.net/ -respectively. -*/ - -#include -#include -#include -#include /* this is libusb */ -#include "opendevice.h" /* common code moved to separate module */ - -#include "../firmware/requests.h" /* custom request numbers */ -#include "../firmware/usbconfig.h" /* device's VID/PID and names */ - -static void usage(char *name) -{ - fprintf(stderr, "usage:\n"); - 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 ENABLE_TEST - fprintf(stderr, " %s test ..... run driver reliability test\n", name); -#endif /* ENABLE_TEST */ -} - -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[4]; -int cnt, vid, pid, isOn; - - usb_init(); - if(argc < 2){ /* we need at least one argument */ - usage(argv[0]); - exit(1); - } - /* compute VID/PID from usbconfig.h so that there is a central source of information */ - vid = rawVid[1] * 256 + rawVid[0]; - pid = rawPid[1] * 256 + rawPid[0]; - /* The following function is in opendevice.c: */ - if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) != 0){ - fprintf(stderr, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid); - exit(1); - } - /* Since we use only control endpoint 0, we don't need to choose a - * configuration and interface. Reading device descriptor and setting a - * configuration and interface is done through endpoint 0 after all. - * However, newer versions of Linux require that we claim an interface - * even for endpoint 0. Enable the following code if your operating system - * needs it: */ -#if 0 - int retries = 1, usbConfiguration = 1, usbInterface = 0; - if(usb_set_configuration(handle, usbConfiguration) && showWarnings){ - fprintf(stderr, "Warning: could not set configuration: %s\n", usb_strerror()); - } - /* now try to claim the interface and detach the kernel HID driver on - * Linux and other operating systems which support the call. */ - while((len = usb_claim_interface(handle, usbInterface)) != 0 && retries-- > 0){ -#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP - if(usb_detach_kernel_driver_np(handle, 0) < 0 && showWarnings){ - fprintf(stderr, "Warning: could not detach kernel driver: %s\n", usb_strerror()); - } -#endif - } -#endif - - if(strcasecmp(argv[1], "status") == 0){ - cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_STATUS, 0, 0, buffer, sizeof(buffer), 5000); - if(cnt < 1){ - if(cnt < 0){ - fprintf(stderr, "USB error: %s\n", usb_strerror()); - }else{ - fprintf(stderr, "only %d bytes received.\n", cnt); - } - }else{ - printf("LED is %s\n", buffer[0] ? "on" : "off"); - } - }else if((isOn = (strcasecmp(argv[1], "on") == 0)) || strcasecmp(argv[1], "off") == 0){ - cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_SET_STATUS, isOn, 0, buffer, 0, 5000); - if(cnt < 0){ - fprintf(stderr, "USB error: %s\n", usb_strerror()); - } -#if ENABLE_TEST - }else if(strcasecmp(argv[1], "test") == 0){ - int i; - srandomdev(); - for(i = 0; i < 50000; i++){ - int value = random() & 0xffff, index = random() & 0xffff; - int rxValue, rxIndex; - if((i+1) % 100 == 0){ - fprintf(stderr, "\r%05d", 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 /* ENABLE_TEST */ - }else{ - usage(argv[0]); - exit(1); - } - usb_close(handle); - return 0; -} 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 ... \n" + " where is 0,1,2,3\n" + " and 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