diff --git a/avr/usbload/main.c b/avr/usbload/main.c index 2257f5d..0db4444 100644 --- a/avr/usbload/main.c +++ b/avr/usbload/main.c @@ -73,7 +73,7 @@ uint8_t tx_buffer[32]; uint8_t data_buffer[4]; uint32_t addr; uint16_t crc = 0; - +uint8_t loader_enabled = 1; @@ -130,9 +130,9 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) req_bank++; timer_start(); - } else { - sram_bulk_write_start(req_addr); } + sram_bulk_write_start(req_addr); + ret_len = USB_MAX_TRANS; /* @@ -239,6 +239,11 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) tx_buffer[1] = (crc >> 8) & 0xff; ret_len = 2; req_state = REQ_STATUS_IDLE; + + + } else if (rq->bRequest == USB_SET_LAODER) { + loader_enabled = rq->wValue.word; + ret_len = 0; } usbMsgPtr = data_buffer; @@ -371,7 +376,14 @@ int main(void) shared_memory_init(); irq_init(); - boot_startup_rom(500); + + if(loader_enabled) + boot_startup_rom(500); + else + { + avr_bus_active(); + send_reset(); + } } diff --git a/avr/usbload/requests.h b/avr/usbload/requests.h index fe954ec..9604dc1 100644 --- a/avr/usbload/requests.h +++ b/avr/usbload/requests.h @@ -38,5 +38,6 @@ #define USB_MODE_SNES 10 #define USB_MODE_AVR 11 #define USB_AVR_RESET 12 +#define USB_SET_LAODER 13 #endif /* __REQUESTS_H_INCLUDED__ */ diff --git a/avr/usbload/sram.c b/avr/usbload/sram.c index 5bd48ad..435ae40 100644 --- a/avr/usbload/sram.c +++ b/avr/usbload/sram.c @@ -228,8 +228,6 @@ void sram_bulk_write_start(uint32_t addr) sreg_set(addr); - AVR_WR_PORT &= ~(1 << AVR_WR_PIN); - } inline void sram_bulk_write_next(void) @@ -237,11 +235,11 @@ inline void sram_bulk_write_next(void) addr_current++; AVR_WR_PORT |= (1 << AVR_WR_PIN); counter_up(); - AVR_WR_PORT &= ~(1 << AVR_WR_PIN); } inline void sram_bulk_write( uint8_t data) { + AVR_WR_PORT &= ~(1 << AVR_WR_PIN); AVR_DATA_PORT = data; } diff --git a/tools/qdinc/opendevice.c b/tools/qdinc/opendevice.c new file mode 100644 index 0000000..7706310 --- /dev/null +++ b/tools/qdinc/opendevice.c @@ -0,0 +1,286 @@ +/* + * Name: opendevice.c Project: V-USB host-side library 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) This Revision: $Id: + * opendevice.c 740 2009-04-13 18:23:31Z cs $ + */ + +/* + * General Description: The functions in this module can be used to find and + * open a device based on libusb or libusb-win32. + */ + + +#include +#include "opendevice.h" + +/* + * ------------------------------------------------------------------------- + */ + +#define MATCH_SUCCESS 1 +#define MATCH_FAILED 0 +#define MATCH_ABORT -1 + +/* + * private interface: match text and p, return MATCH_SUCCESS, MATCH_FAILED, or + * MATCH_ABORT. + */ +static int _shellStyleMatch(char *text, char *p) +{ + int last, + matched, + reverse; + + for (; *p; text++, p++) { + if (*text == 0 && *p != '*') + return MATCH_ABORT; + switch (*p) { + case '\\': + /* + * Literal match with following character. + */ + p++; + /* + * FALLTHROUGH + */ + default: + if (*text != *p) + return MATCH_FAILED; + continue; + case '?': + /* + * Match anything. + */ + continue; + case '*': + while (*++p == '*') + /* + * Consecutive stars act just like one. + */ + continue; + if (*p == 0) + /* + * Trailing star matches everything. + */ + return MATCH_SUCCESS; + while (*text) + if ((matched = _shellStyleMatch(text++, p)) != MATCH_FAILED) + return matched; + return MATCH_ABORT; + case '[': + reverse = p[1] == '^'; + if (reverse) /* Inverted character class. */ + p++; + matched = MATCH_FAILED; + if (p[1] == ']' || p[1] == '-') + if (*++p == *text) + matched = MATCH_SUCCESS; + for (last = *p; *++p && *p != ']'; last = *p) + if (*p == '-' && p[1] != ']' ? *text <= *++p + && *text >= last : *text == *p) + matched = MATCH_SUCCESS; + if (matched == reverse) + return MATCH_FAILED; + continue; + } + } + return *text == 0; +} + +/* + * public interface for shell style matching: returns 0 if fails, 1 if matches + */ +static int shellStyleMatch(char *text, char *pattern) +{ + if (pattern == NULL) /* NULL pattern is synonymous to "*" */ + return 1; + return _shellStyleMatch(text, pattern) == MATCH_SUCCESS; +} + +/* + * ------------------------------------------------------------------------- + */ + +int usbGetStringAscii(usb_dev_handle * dev, int index, char *buf, int buflen) +{ + char buffer[256]; + int rval, + i; + + if ((rval = usb_get_string_simple(dev, index, buf, buflen)) >= 0) /* use + * libusb + * version + * if + * it + * works + */ + return rval; + if ((rval = + usb_control_msg(dev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, + (USB_DT_STRING << 8) + index, 0x0409, buffer, + sizeof(buffer), 5000)) < 0) + return rval; + if (buffer[1] != USB_DT_STRING) { + *buf = 0; + return 0; + } + if ((unsigned char) buffer[0] < rval) + rval = (unsigned char) buffer[0]; + rval /= 2; + /* + * lossy conversion to ISO Latin1: + */ + for (i = 1; i < rval; i++) { + if (i > buflen) /* destination buffer overflow */ + break; + buf[i - 1] = buffer[2 * i]; + if (buffer[2 * i + 1] != 0) /* outside of ISO Latin1 range */ + buf[i - 1] = '?'; + } + buf[i - 1] = 0; + return i - 1; +} + +/* + * ------------------------------------------------------------------------- + */ + +int usbOpenDevice(usb_dev_handle ** device, int vendorID, + char *vendorNamePattern, int productID, + char *productNamePattern, char *serialNamePattern, + FILE * printMatchingDevicesFp, FILE * warningsFp) +{ + struct usb_bus *bus; + struct usb_device *dev; + usb_dev_handle *handle = NULL; + int errorCode = USBOPEN_ERR_NOTFOUND; + + usb_find_busses(); + usb_find_devices(); + for (bus = usb_get_busses(); bus; bus = bus->next) { + for (dev = bus->devices; dev; dev = dev->next) { /* iterate over + * all devices + * on all + * busses */ + if ((vendorID == 0 || dev->descriptor.idVendor == vendorID) + && (productID == 0 || dev->descriptor.idProduct == productID)) { + char vendor[256], + product[256], + serial[256]; + int len; + handle = usb_open(dev); /* we need to open the device in order + * to query strings */ + if (!handle) { + errorCode = USBOPEN_ERR_ACCESS; + if (warningsFp != NULL) + fprintf(warningsFp, + "Warning: cannot open VID=0x%04x PID=0x%04x: %s\n", + dev->descriptor.idVendor, + dev->descriptor.idProduct, usb_strerror()); + continue; + } + /* + * now check whether the names match: + */ + len = vendor[0] = 0; + if (dev->descriptor.iManufacturer > 0) { + len = + usbGetStringAscii(handle, dev->descriptor.iManufacturer, + vendor, sizeof(vendor)); + } + if (len < 0) { + errorCode = USBOPEN_ERR_ACCESS; + if (warningsFp != NULL) + fprintf(warningsFp, + "Warning: cannot query manufacturer for VID=0x%04x PID=0x%04x: %s\n", + dev->descriptor.idVendor, + dev->descriptor.idProduct, usb_strerror()); + } else { + errorCode = USBOPEN_ERR_NOTFOUND; + /* + * printf("seen device from vendor ->%s<-\n", vendor); + */ + if (shellStyleMatch(vendor, vendorNamePattern)) { + len = product[0] = 0; + if (dev->descriptor.iProduct > 0) { + len = + usbGetStringAscii(handle, + dev->descriptor.iProduct, + product, sizeof(product)); + } + if (len < 0) { + errorCode = USBOPEN_ERR_ACCESS; + if (warningsFp != NULL) + fprintf(warningsFp, + "Warning: cannot query product for VID=0x%04x PID=0x%04x: %s\n", + dev->descriptor.idVendor, + dev->descriptor.idProduct, + usb_strerror()); + } else { + errorCode = USBOPEN_ERR_NOTFOUND; + /* + * printf("seen product ->%s<-\n", product); + */ + if (shellStyleMatch(product, productNamePattern)) { + len = serial[0] = 0; + if (dev->descriptor.iSerialNumber > 0) { + len = + usbGetStringAscii(handle, + dev->descriptor. + iSerialNumber, serial, + sizeof(serial)); + } + if (len < 0) { + errorCode = USBOPEN_ERR_ACCESS; + if (warningsFp != NULL) + fprintf(warningsFp, + "Warning: cannot query serial for VID=0x%04x PID=0x%04x: %s\n", + dev->descriptor.idVendor, + dev->descriptor.idProduct, + usb_strerror()); + } + if (shellStyleMatch(serial, serialNamePattern)) { + if (printMatchingDevicesFp != NULL) { + if (serial[0] == 0) { + fprintf(printMatchingDevicesFp, + "VID=0x%04x PID=0x%04x vendor=\"%s\" product=\"%s\"\n", + dev->descriptor.idVendor, + dev->descriptor.idProduct, + vendor, product); + } else { + fprintf(printMatchingDevicesFp, + "VID=0x%04x PID=0x%04x vendor=\"%s\" product=\"%s\" serial=\"%s\"\n", + dev->descriptor.idVendor, + dev->descriptor.idProduct, + vendor, product, serial); + } + } else { + break; + } + } + } + } + } + } + usb_close(handle); + handle = NULL; + } + } + if (handle) /* we have found a deice */ + break; + } + if (handle != NULL) { + errorCode = 0; + *device = handle; + } + if (printMatchingDevicesFp != NULL) /* never return an error for listing + * only */ + errorCode = 0; + return errorCode; +} + +/* + * ------------------------------------------------------------------------- + */ diff --git a/tools/qdinc/opendevice.h b/tools/qdinc/opendevice.h new file mode 100644 index 0000000..0e04f73 --- /dev/null +++ b/tools/qdinc/opendevice.h @@ -0,0 +1,77 @@ +/* Name: opendevice.h + * Project: V-USB host-side library + * 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) + * This Revision: $Id: opendevice.h 740 2009-04-13 18:23:31Z cs $ + */ + +/* +General Description: +This module offers additional functionality for host side drivers based on +libusb or libusb-win32. It includes a function to find and open a device +based on numeric IDs and textual description. It also includes a function to +obtain textual descriptions from a device. + +To use this functionality, simply copy opendevice.c and opendevice.h into your +project and add them to your Makefile. You may modify and redistribute these +files according to the GNU General Public License (GPL) version 2 or 3. +*/ + +#ifndef __OPENDEVICE_H_INCLUDED__ +#define __OPENDEVICE_H_INCLUDED__ + +#include /* this is libusb, see http://libusb.sourceforge.net/ */ +#include + +int usbGetStringAscii(usb_dev_handle *dev, int index, char *buf, int buflen); +/* This function gets a string descriptor from the device. 'index' is the + * string descriptor index. The string is returned in ISO Latin 1 encoding in + * 'buf' and it is terminated with a 0-character. The buffer size must be + * passed in 'buflen' to prevent buffer overflows. A libusb device handle + * must be given in 'dev'. + * Returns: The length of the string (excluding the terminating 0) or + * a negative number in case of an error. If there was an error, use + * usb_strerror() to obtain the error message. + */ + +int usbOpenDevice(usb_dev_handle **device, int vendorID, char *vendorNamePattern, int productID, char *productNamePattern, char *serialNamePattern, FILE *printMatchingDevicesFp, FILE *warningsFp); +/* This function iterates over all devices on all USB busses and searches for + * a device. Matching is done first by means of Vendor- and Product-ID (passed + * in 'vendorID' and 'productID'. An ID of 0 matches any numeric ID (wildcard). + * When a device matches by its IDs, matching by names is performed. Name + * matching can be done on textual vendor name ('vendorNamePattern'), product + * name ('productNamePattern') and serial number ('serialNamePattern'). A + * device matches only if all non-null pattern match. If you don't care about + * a string, pass NULL for the pattern. Patterns are Unix shell style pattern: + * '*' stands for 0 or more characters, '?' for one single character, a list + * of characters in square brackets for a single character from the list + * (dashes are allowed to specify a range) and if the lis of characters begins + * with a caret ('^'), it matches one character which is NOT in the list. + * Other parameters to the function: If 'warningsFp' is not NULL, warning + * messages are printed to this file descriptor with fprintf(). If + * 'printMatchingDevicesFp' is not NULL, no device is opened but matching + * devices are printed to the given file descriptor with fprintf(). + * If a device is opened, the resulting USB handle is stored in '*device'. A + * pointer to a "usb_dev_handle *" type variable must be passed here. + * Returns: 0 on success, an error code (see defines below) on failure. + */ + +/* usbOpenDevice() error codes: */ +#define USBOPEN_SUCCESS 0 /* no error */ +#define USBOPEN_ERR_ACCESS 1 /* not enough permissions to open device */ +#define USBOPEN_ERR_IO 2 /* I/O error */ +#define USBOPEN_ERR_NOTFOUND 3 /* device not found */ + + +/* Obdev's free USB IDs, see USBID-License.txt for details */ + +#define USB_VID_OBDEV_SHARED 5824 /* obdev's shared vendor ID */ +#define USB_PID_OBDEV_SHARED_CUSTOM 1500 /* shared PID for custom class devices */ +#define USB_PID_OBDEV_SHARED_HID 1503 /* shared PID for HIDs except mice & keyboards */ +#define USB_PID_OBDEV_SHARED_CDCACM 1505 /* shared PID for CDC Modem devices */ +#define USB_PID_OBDEV_SHARED_MIDI 1508 /* shared PID for MIDI class devices */ + +#endif /* __OPENDEVICE_H_INCLUDED__ */ diff --git a/tools/qdinc/qdinc.c b/tools/qdinc/qdinc.c new file mode 100644 index 0000000..ac7a408 --- /dev/null +++ b/tools/qdinc/qdinc.c @@ -0,0 +1,165 @@ +#include +#include + +#include + +#include "opendevice.h" + +#include "qdinc.h" + + +int main(int argc, char * argv[]) +{ + if(argc != 4 && argc != 3) + { + fprintf(stderr, "usage: qdinc [hl] [reference]\n"); + exit(1); + } + + int hirom = (argv[1][0] == 'h' || argv[1][0] == 'H'); + + + unsigned int pos = 0; + int ref_complete, upd_complete = 0; + + FILE * upd = fopen(argv[2], "rb"); + + if(upd == 0) + { + fprintf(stderr, "couldn't open file %s\n", argv[3]); + } + + + FILE * ref = 0; + if(argc == 4) + ref = fopen(argv[3], "rb"); + + ref_complete = (ref == 0); + + 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}; + int cnt, vid, pid; + + usb_init(); + vid = rawVid[1] * 256 + rawVid[0]; + pid = rawPid[1] * 256 + rawPid[0]; + 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); + } + +#ifndef QDINC_OLD_FIRMWARE + cnt = usb_control_msg(handle, + USB_TYPE_VENDOR | USB_RECIP_DEVICE | + USB_ENDPOINT_OUT, USB_SET_LAODER, 0, 0, NULL, + 0, 5000); +#endif + cnt = usb_control_msg(handle, + USB_TYPE_VENDOR | USB_RECIP_DEVICE | + USB_ENDPOINT_OUT, USB_MODE_AVR, 0, 0, NULL, + 0, 5000); + +#ifdef QDINC_OLD_FIRMWARE + /* wait for the loader to depack */ + usleep(500000); +#endif + + cnt = usb_control_msg(handle, + USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, + USB_BULK_UPLOAD_INIT, (hirom ? 16 : 15) , (hirom ? 32 : 64) /* << this is wrong, but only used for progress display */, NULL, 0, 5000); + + unsigned char diffblock[256]; + unsigned int blocklen = 0; + unsigned int reallen = 0; + unsigned int position = 0; + unsigned int last_position = (unsigned int)(-1); + + unsigned char lastchar; + unsigned int transmitted = 0; + + while(!upd_complete) + { + unsigned char updchar = fgetc(upd); + unsigned char refchar; + if(feof(upd)) + upd_complete = 1; + if(!ref_complete) + { + refchar = fgetc(ref); + if(feof(ref)) + ref_complete= 1; + } + + int match = (ref_complete || updchar != refchar); +#ifdef QDINC_OLD_FIRMWARE + if(position < 64 * 1024) + match = 1; +#endif + if(upd_complete) + match = 0; + if(match) + { +#ifdef QDINC_OLD_FIRMWARE + if(blocklen == 0 && (position & (hirom ? 0xFFFF : 0x7FFF)) == 0 && position != 0 && position != last_position) + { + diffblock[0] = lastchar; + blocklen++; + } +#endif + diffblock[blocklen] = updchar; + blocklen++; + reallen = blocklen; + } + else if(blocklen > 0) + { + diffblock[blocklen] = updchar; + blocklen++; + } + + position++; + if((reallen > 0 && upd_complete) || ((!upd_complete) && (( (blocklen - reallen) >= QDINC_RANGE && !match) || blocklen == QDINC_SIZE)) ) + { + int consecutive = (last_position == position - blocklen); + unsigned int begin = position - blocklen; + + printf("updating %sblock from 0x%06x to 0x%06x of size 0x%03x\n", (consecutive ? "consecutive " : ""), begin, begin + reallen - 1, reallen); + + + cnt = usb_control_msg(handle, + USB_TYPE_VENDOR | USB_RECIP_DEVICE | + USB_ENDPOINT_OUT, consecutive ? USB_BULK_UPLOAD_NEXT : USB_BULK_UPLOAD_ADDR, begin >> 16, + begin & 0xFFFF, (char *) diffblock, + reallen, 5000); + + last_position = position - blocklen + reallen; + transmitted += reallen; + blocklen = 0; + reallen = 0; + } + + lastchar = updchar; + } + position--; + cnt = usb_control_msg(handle, + USB_TYPE_VENDOR | USB_RECIP_DEVICE | + USB_ENDPOINT_OUT, USB_BULK_UPLOAD_END, 0, 0, NULL, + 0, 5000); + +#ifndef QDINC_OLD_FIRMWARE + cnt = usb_control_msg(handle, + USB_TYPE_VENDOR | USB_RECIP_DEVICE | + USB_ENDPOINT_OUT, USB_SET_LAODER, 1, 1, NULL, + 0, 5000); +#endif + + printf("Transmitted %i of %i bytes (%3.2f)\n", transmitted, position, transmitted * 100.0 / position); + cnt = usb_control_msg(handle, + USB_TYPE_VENDOR | USB_RECIP_DEVICE | + USB_ENDPOINT_OUT, USB_MODE_SNES, 0, 0, NULL, + 0, 5000); + return 0; + +} \ No newline at end of file diff --git a/tools/qdinc/qdinc.h b/tools/qdinc/qdinc.h new file mode 100644 index 0000000..1284536 --- /dev/null +++ b/tools/qdinc/qdinc.h @@ -0,0 +1,62 @@ +#ifndef __QDINC_H__ +#define __QDINC_H__ + +#define QDINC_RANGE 32 +#define QDINC_SIZE 254 + +// makes this compatible with a bug in older quickdev firmware revisions +//#define QDINC_OLD_FIRMWARE + +/* quickdev protocol defines */ + +#define USB_UPLOAD_INIT 0 +#define USB_UPLOAD_ADDR 1 + +#define USB_DOWNLOAD_INIT 2 +#define USB_DOWNLOAD_ADDR 3 + +#define USB_CRC 4 +#define USB_CRC_ADDR 5 + +#define USB_BULK_UPLOAD_INIT 6 +#define USB_BULK_UPLOAD_ADDR 7 +#define USB_BULK_UPLOAD_NEXT 8 +#define USB_BULK_UPLOAD_END 9 +#define USB_MODE_SNES 10 +#define USB_MODE_AVR 11 +#define USB_AVR_RESET 12 +#define USB_SET_LAODER 13 + +/* -------------------------- Device Description --------------------------- */ + +#define USB_CFG_VENDOR_ID 0xc0, 0x16 +/* USB vendor ID for the device, low byte first. If you have registered your + * own Vendor ID, define it here. Otherwise you use one of obdev's free shared + * VID/PID pairs. Be sure to read USBID-License.txt for rules! + */ +#define USB_CFG_DEVICE_ID 0xdd, 0x05 +/* This is the ID of the product, low byte first. It is interpreted in the + * scope of the vendor ID. If you have registered your own VID with usb.org + * or if you have licensed a PID from somebody else, define it here. Otherwise + * you use obdev's free shared VID/PID pair. Be sure to read the rules in + * USBID-License.txt! + */ +#define USB_CFG_DEVICE_VERSION 0x00, 0x01 +/* Version number of the device: Minor number first, then major number. + */ +#define USB_CFG_VENDOR_NAME 'o', 'p', 't', 'i', 'x', 'x', '.', 'o', 'r', 'g' +#define USB_CFG_VENDOR_NAME_LEN 10 +/* These two values define the vendor name returned by the USB device. The name + * must be given as a list of characters under single quotes. The characters + * are interpreted as Unicode (UTF-16) entities. + * If you don't want a vendor name string, undefine these macros. + * ALWAYS define a vendor name containing your Internet domain name if you use + * obdev's free shared VID/PID pair. See the file USBID-License.txt for + * details. + */ +#define USB_CFG_DEVICE_NAME 'Q', 'U', 'I', 'C', 'K', 'D', 'E', 'V', '1', '6' +#define USB_CFG_DEVICE_NAME_LEN 10 + +/* end of quickdev protocol defines */ + +#endif