o add missing files
o add usbload
This commit is contained in:
@@ -1,71 +1,86 @@
|
||||
/* 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 $
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
* General Description: The functions in this module can be used to find and
|
||||
* open a device based on libusb or libusb-win32.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#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)
|
||||
/*
|
||||
* 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;
|
||||
int last,
|
||||
matched,
|
||||
reverse;
|
||||
|
||||
for(; *p; text++, p++){
|
||||
if(*text == 0 && *p != '*')
|
||||
for (; *p; text++, p++) {
|
||||
if (*text == 0 && *p != '*')
|
||||
return MATCH_ABORT;
|
||||
switch(*p){
|
||||
switch (*p) {
|
||||
case '\\':
|
||||
/* Literal match with following character. */
|
||||
/*
|
||||
* Literal match with following character.
|
||||
*/
|
||||
p++;
|
||||
/* FALLTHROUGH */
|
||||
/*
|
||||
* FALLTHROUGH
|
||||
*/
|
||||
default:
|
||||
if(*text != *p)
|
||||
if (*text != *p)
|
||||
return MATCH_FAILED;
|
||||
continue;
|
||||
case '?':
|
||||
/* Match anything. */
|
||||
/*
|
||||
* Match anything.
|
||||
*/
|
||||
continue;
|
||||
case '*':
|
||||
while(*++p == '*')
|
||||
/* Consecutive stars act just like one. */
|
||||
while (*++p == '*')
|
||||
/*
|
||||
* Consecutive stars act just like one.
|
||||
*/
|
||||
continue;
|
||||
if(*p == 0)
|
||||
/* Trailing star matches everything. */
|
||||
if (*p == 0)
|
||||
/*
|
||||
* Trailing star matches everything.
|
||||
*/
|
||||
return MATCH_SUCCESS;
|
||||
while(*text)
|
||||
if((matched = _shellStyleMatch(text++, p)) != MATCH_FAILED)
|
||||
while (*text)
|
||||
if ((matched = _shellStyleMatch(text++, p)) != MATCH_FAILED)
|
||||
return matched;
|
||||
return MATCH_ABORT;
|
||||
case '[':
|
||||
reverse = p[1] == '^';
|
||||
if(reverse) /* Inverted character class. */
|
||||
if (reverse) /* Inverted character class. */
|
||||
p++;
|
||||
matched = MATCH_FAILED;
|
||||
if(p[1] == ']' || p[1] == '-')
|
||||
if(*++p == *text)
|
||||
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)
|
||||
for (last = *p; *++p && *p != ']'; last = *p)
|
||||
if (*p == '-' && p[1] != ']' ? *text <= *++p
|
||||
&& *text >= last : *text == *p)
|
||||
matched = MATCH_SUCCESS;
|
||||
if(matched == reverse)
|
||||
if (matched == reverse)
|
||||
return MATCH_FAILED;
|
||||
continue;
|
||||
}
|
||||
@@ -73,110 +88,174 @@ int last, matched, reverse;
|
||||
return *text == 0;
|
||||
}
|
||||
|
||||
/* public interface for shell style matching: returns 0 if fails, 1 if matches */
|
||||
/*
|
||||
* 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 "*" */
|
||||
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)
|
||||
int usbGetStringAscii(usb_dev_handle * dev, int index, char *buf, int buflen)
|
||||
{
|
||||
char buffer[256];
|
||||
int rval, i;
|
||||
char buffer[256];
|
||||
int rval,
|
||||
i;
|
||||
|
||||
if((rval = usb_get_string_simple(dev, index, buf, buflen)) >= 0) /* use libusb version if it works */
|
||||
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)
|
||||
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){
|
||||
if (buffer[1] != USB_DT_STRING) {
|
||||
*buf = 0;
|
||||
return 0;
|
||||
}
|
||||
if((unsigned char)buffer[0] < rval)
|
||||
rval = (unsigned char)buffer[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 */
|
||||
/*
|
||||
* 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] = buffer[2 * i];
|
||||
if (buffer[2 * i + 1] != 0) /* outside of ISO Latin1 range */
|
||||
buf[i - 1] = '?';
|
||||
}
|
||||
buf[i-1] = 0;
|
||||
return 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)
|
||||
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;
|
||||
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){
|
||||
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());
|
||||
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: */
|
||||
/*
|
||||
* 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 (dev->descriptor.iManufacturer > 0) {
|
||||
len =
|
||||
usbGetStringAscii(handle, dev->descriptor.iManufacturer,
|
||||
vendor, sizeof(vendor));
|
||||
}
|
||||
if(len < 0){
|
||||
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{
|
||||
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)){
|
||||
/*
|
||||
* 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 (dev->descriptor.iProduct > 0) {
|
||||
len =
|
||||
usbGetStringAscii(handle,
|
||||
dev->descriptor.iProduct,
|
||||
product, sizeof(product));
|
||||
}
|
||||
if(len < 0){
|
||||
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{
|
||||
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)){
|
||||
/*
|
||||
* 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 (dev->descriptor.iSerialNumber > 0) {
|
||||
len =
|
||||
usbGetStringAscii(handle,
|
||||
dev->descriptor.
|
||||
iSerialNumber, serial,
|
||||
sizeof(serial));
|
||||
}
|
||||
if(len < 0){
|
||||
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 (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);
|
||||
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{
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -188,16 +267,19 @@ int errorCode = USBOPEN_ERR_NOTFOUND;
|
||||
handle = NULL;
|
||||
}
|
||||
}
|
||||
if(handle) /* we have found a deice */
|
||||
if (handle) /* we have found a deice */
|
||||
break;
|
||||
}
|
||||
if(handle != NULL){
|
||||
if (handle != NULL) {
|
||||
errorCode = 0;
|
||||
*device = handle;
|
||||
}
|
||||
if(printMatchingDevicesFp != NULL) /* never return an error for listing only */
|
||||
if (printMatchingDevicesFp != NULL) /* never return an error for listing
|
||||
* only */
|
||||
errorCode = 0;
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
/*
|
||||
* -------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
@@ -7,14 +7,12 @@
|
||||
*/
|
||||
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
* 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.
|
||||
*/
|
||||
|
||||
|
||||
#define READ_BUFFER_SIZE 1024
|
||||
@@ -33,108 +31,95 @@ respectively.
|
||||
#include "../usbconfig.h" /* device's VID/PID and names */
|
||||
|
||||
|
||||
void
|
||||
dump_packet (uint32_t addr, uint32_t len, uint8_t * packet)
|
||||
void dump_packet(uint32_t addr, uint32_t len, uint8_t * packet)
|
||||
{
|
||||
uint16_t i, j;
|
||||
uint16_t i,
|
||||
j;
|
||||
uint16_t sum = 0;
|
||||
uint8_t clear = 0;
|
||||
|
||||
for (i = 0; i < len; i += 16)
|
||||
{
|
||||
for (i = 0; i < len; i += 16) {
|
||||
|
||||
sum = 0;
|
||||
for (j = 0; j < 16; j++)
|
||||
{
|
||||
sum += packet[i + j];
|
||||
}
|
||||
if (!sum)
|
||||
{
|
||||
clear = 1;
|
||||
continue;
|
||||
}
|
||||
if (clear)
|
||||
{
|
||||
printf ("*\n");
|
||||
clear = 0;
|
||||
}
|
||||
printf ("%08x:", addr + i);
|
||||
for (j = 0; j < 16; j++)
|
||||
{
|
||||
printf (" %02x", packet[i + j]);
|
||||
}
|
||||
printf (" |");
|
||||
for (j = 0; j < 16; j++)
|
||||
{
|
||||
if (packet[i + j] >= 33 && packet[i + j] <= 126)
|
||||
printf ("%c", packet[i + j]);
|
||||
else
|
||||
printf (".");
|
||||
}
|
||||
printf ("|\n");
|
||||
}
|
||||
sum = 0;
|
||||
for (j = 0; j < 16; j++) {
|
||||
sum += packet[i + j];
|
||||
}
|
||||
if (!sum) {
|
||||
clear = 1;
|
||||
continue;
|
||||
}
|
||||
if (clear) {
|
||||
printf("*\n");
|
||||
clear = 0;
|
||||
}
|
||||
printf("%08x:", addr + i);
|
||||
for (j = 0; j < 16; j++) {
|
||||
printf(" %02x", packet[i + j]);
|
||||
}
|
||||
printf(" |");
|
||||
for (j = 0; j < 16; j++) {
|
||||
if (packet[i + j] >= 33 && packet[i + j] <= 126)
|
||||
printf("%c", packet[i + j]);
|
||||
else
|
||||
printf(".");
|
||||
}
|
||||
printf("|\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
uint16_t
|
||||
crc_xmodem_update (uint16_t crc, uint8_t data)
|
||||
uint16_t crc_xmodem_update(uint16_t crc, uint8_t data)
|
||||
{
|
||||
int i;
|
||||
crc = crc ^ ((uint16_t) data << 8);
|
||||
for (i = 0; i < 8; i++)
|
||||
{
|
||||
if (crc & 0x8000)
|
||||
crc = (crc << 1) ^ 0x1021;
|
||||
else
|
||||
crc <<= 1;
|
||||
}
|
||||
for (i = 0; i < 8; i++) {
|
||||
if (crc & 0x8000)
|
||||
crc = (crc << 1) ^ 0x1021;
|
||||
else
|
||||
crc <<= 1;
|
||||
}
|
||||
|
||||
return crc;
|
||||
}
|
||||
|
||||
uint16_t
|
||||
do_crc (uint8_t * data, uint16_t size)
|
||||
uint16_t do_crc(uint8_t * data, uint16_t size)
|
||||
{
|
||||
uint16_t crc = 0;
|
||||
uint16_t i;
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
crc = crc_xmodem_update (crc, data[i]);
|
||||
}
|
||||
for (i = 0; i < size; i++) {
|
||||
crc = crc_xmodem_update(crc, data[i]);
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
|
||||
uint16_t
|
||||
do_crc_update (uint16_t crc, uint8_t * data, uint16_t size)
|
||||
uint16_t do_crc_update(uint16_t crc, uint8_t * data, uint16_t size)
|
||||
{
|
||||
uint16_t i;
|
||||
for (i = 0; i < size; i++)
|
||||
crc = crc_xmodem_update (crc, data[i]);
|
||||
crc = crc_xmodem_update(crc, data[i]);
|
||||
return crc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void
|
||||
usage (char *name)
|
||||
static void usage(char *name)
|
||||
{
|
||||
fprintf (stderr, "usage:\n");
|
||||
fprintf (stderr, " %s upload filename.. upload\n", name);
|
||||
fprintf(stderr, "usage:\n");
|
||||
fprintf(stderr, " %s upload filename.. upload\n", name);
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
usb_dev_handle *handle = NULL;
|
||||
const unsigned char rawVid[2] = { USB_CFG_VENDOR_ID }, rawPid[2] =
|
||||
{
|
||||
const unsigned char rawVid[2] = { USB_CFG_VENDOR_ID }, rawPid[2] = {
|
||||
USB_CFG_DEVICE_ID};
|
||||
char vendor[] = { USB_CFG_VENDOR_NAME, 0 }, product[] =
|
||||
{
|
||||
char vendor[] = { USB_CFG_VENDOR_NAME, 0 }, product[] = {
|
||||
USB_CFG_DEVICE_NAME, 0};
|
||||
int cnt, vid, pid;
|
||||
int cnt,
|
||||
vid,
|
||||
pid;
|
||||
int cnt_crc = 0;
|
||||
uint8_t *read_buffer;
|
||||
uint8_t *crc_buffer;
|
||||
@@ -146,126 +131,114 @@ main (int argc, char **argv)
|
||||
uint8_t bank = 0;
|
||||
FILE *fp;
|
||||
|
||||
usb_init ();
|
||||
if (argc < 2)
|
||||
{ /* we need at least one argument */
|
||||
usage (argv[0]);
|
||||
exit (1);
|
||||
}
|
||||
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);
|
||||
}
|
||||
printf ("Open USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid);
|
||||
if (strcasecmp (argv[1], "upload") == 0)
|
||||
{
|
||||
if (argc < 3)
|
||||
{ /* we need at least one argument */
|
||||
usage (argv[0]);
|
||||
exit (1);
|
||||
}
|
||||
fp = fopen (argv[2], "r");
|
||||
if (fp == NULL)
|
||||
{
|
||||
fprintf (stderr, "Cannot open file %s ", argv[2]);
|
||||
exit (1);
|
||||
}
|
||||
read_buffer = (unsigned char *) malloc (READ_BUFFER_SIZE);
|
||||
crc_buffer = (unsigned char *) malloc (BUFFER_CRC);
|
||||
memset (crc_buffer, 0, BUFFER_CRC);
|
||||
addr = 0x000000;
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
printf("Open USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid,
|
||||
pid);
|
||||
if (strcasecmp(argv[1], "upload") == 0) {
|
||||
if (argc < 3) { /* we need at least one argument */
|
||||
usage(argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
fp = fopen(argv[2], "r");
|
||||
if (fp == NULL) {
|
||||
fprintf(stderr, "Cannot open file %s ", argv[2]);
|
||||
exit(1);
|
||||
}
|
||||
read_buffer = (unsigned char *) malloc(READ_BUFFER_SIZE);
|
||||
crc_buffer = (unsigned char *) malloc(BUFFER_CRC);
|
||||
memset(crc_buffer, 0, BUFFER_CRC);
|
||||
addr = 0x000000;
|
||||
|
||||
usb_control_msg (handle,
|
||||
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, USB_UPLOAD_INIT, 0, 0, NULL, 0, 5000);
|
||||
usb_control_msg(handle,
|
||||
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT,
|
||||
USB_UPLOAD_INIT, 0, 0, NULL, 0, 5000);
|
||||
|
||||
|
||||
while ((cnt = fread (read_buffer, READ_BUFFER_SIZE, 1, fp)) > 0)
|
||||
{
|
||||
for (step = 0; step < READ_BUFFER_SIZE; step += SEND_BUFFER_SIZE)
|
||||
{
|
||||
addr_lo = addr & 0xffff;
|
||||
addr_hi = (addr >> 16) & 0xff;
|
||||
usb_control_msg (handle,
|
||||
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT,
|
||||
USB_UPLOAD_ADDR, addr_hi, addr_lo,
|
||||
(char *) read_buffer + step, SEND_BUFFER_SIZE, 5000);
|
||||
while ((cnt = fread(read_buffer, READ_BUFFER_SIZE, 1, fp)) > 0) {
|
||||
for (step = 0; step < READ_BUFFER_SIZE; step += SEND_BUFFER_SIZE) {
|
||||
addr_lo = addr & 0xffff;
|
||||
addr_hi = (addr >> 16) & 0xff;
|
||||
usb_control_msg(handle,
|
||||
USB_TYPE_VENDOR | USB_RECIP_DEVICE |
|
||||
USB_ENDPOINT_OUT, USB_UPLOAD_ADDR, addr_hi,
|
||||
addr_lo, (char *) read_buffer + step,
|
||||
SEND_BUFFER_SIZE, 5000);
|
||||
#if 0
|
||||
dump_packet (addr, SEND_BUFFER_SIZE, read_buffer + step);
|
||||
dump_packet(addr, SEND_BUFFER_SIZE, read_buffer + step);
|
||||
#endif
|
||||
addr += SEND_BUFFER_SIZE;
|
||||
}
|
||||
|
||||
memcpy (crc_buffer + cnt_crc, read_buffer, READ_BUFFER_SIZE);
|
||||
cnt_crc += READ_BUFFER_SIZE;
|
||||
if (cnt_crc >= BANK_SIZE)
|
||||
{
|
||||
crc = do_crc (crc_buffer, BANK_SIZE);
|
||||
printf ("Addr: 0x%06x Bank: 0x%02x HiAddr: 0x%02x LoAddr: 0x%04x Crc: 0x%04x\n", addr, bank,
|
||||
addr_hi, addr_lo, crc);
|
||||
memset (crc_buffer, 0, BUFFER_CRC);
|
||||
bank++;
|
||||
cnt_crc = 0;
|
||||
}
|
||||
addr += SEND_BUFFER_SIZE;
|
||||
}
|
||||
cnt = usb_control_msg (handle,
|
||||
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT,
|
||||
USB_CRC, addr_hi, addr_lo, NULL, 0, 5000);
|
||||
|
||||
|
||||
if (cnt < 1)
|
||||
{
|
||||
if (cnt < 0)
|
||||
{
|
||||
fprintf (stderr, "USB error: %s\n", usb_strerror ());
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf (stderr, "only %d bytes received.\n", cnt);
|
||||
}
|
||||
memcpy(crc_buffer + cnt_crc, read_buffer, READ_BUFFER_SIZE);
|
||||
cnt_crc += READ_BUFFER_SIZE;
|
||||
if (cnt_crc >= BANK_SIZE) {
|
||||
crc = do_crc(crc_buffer, BANK_SIZE);
|
||||
printf
|
||||
("Addr: 0x%06x Bank: 0x%02x HiAddr: 0x%02x LoAddr: 0x%04x Crc: 0x%04x\n",
|
||||
addr, bank, addr_hi, addr_lo, crc);
|
||||
memset(crc_buffer, 0, BUFFER_CRC);
|
||||
bank++;
|
||||
cnt_crc = 0;
|
||||
}
|
||||
}
|
||||
else if (strcasecmp (argv[1], "crc") == 0)
|
||||
{
|
||||
/*
|
||||
if(argc < 2){
|
||||
usage(argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
*/
|
||||
addr = 0x000000;
|
||||
addr_lo = addr & 0xffff;
|
||||
addr_hi = (addr >> 16) & 0xff;
|
||||
printf ("Request CRC for Addr: 0x%06x\n", addr);
|
||||
}
|
||||
cnt = usb_control_msg(handle,
|
||||
USB_TYPE_VENDOR | USB_RECIP_DEVICE |
|
||||
USB_ENDPOINT_OUT, USB_CRC, addr_hi, addr_lo, NULL,
|
||||
0, 5000);
|
||||
|
||||
cnt = usb_control_msg (handle,
|
||||
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT,
|
||||
USB_CRC_ADDR, addr_hi, addr_lo, NULL, (1 << 15) / 4, 5000);
|
||||
|
||||
if (cnt < 1)
|
||||
{
|
||||
if (cnt < 0)
|
||||
{
|
||||
fprintf (stderr, "USB error: %s\n", usb_strerror ());
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf (stderr, "only %d bytes received.\n", cnt);
|
||||
}
|
||||
if (cnt < 1) {
|
||||
if (cnt < 0) {
|
||||
fprintf(stderr, "USB error: %s\n", usb_strerror());
|
||||
} else {
|
||||
fprintf(stderr, "only %d bytes received.\n", cnt);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
usage (argv[0]);
|
||||
exit (1);
|
||||
}
|
||||
usb_close (handle);
|
||||
}
|
||||
} else if (strcasecmp(argv[1], "crc") == 0) {
|
||||
/*
|
||||
* if(argc < 2){ usage(argv[0]); exit(1); }
|
||||
*/
|
||||
addr = 0x000000;
|
||||
addr_lo = addr & 0xffff;
|
||||
addr_hi = (addr >> 16) & 0xff;
|
||||
printf("Request CRC for Addr: 0x%06x\n", addr);
|
||||
|
||||
cnt = usb_control_msg(handle,
|
||||
USB_TYPE_VENDOR | USB_RECIP_DEVICE |
|
||||
USB_ENDPOINT_OUT, USB_CRC_ADDR, addr_hi, addr_lo,
|
||||
NULL, (1 << 15) / 4, 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 {
|
||||
usage(argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
usb_close(handle);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "crc.h"
|
||||
#include "uart.h"
|
||||
|
||||
extern FILE uart_stdout;
|
||||
|
||||
uint16_t crc_xmodem_update (uint16_t crc, uint8_t data)
|
||||
uint16_t crc_xmodem_update(uint16_t crc, uint8_t data)
|
||||
{
|
||||
int i;
|
||||
crc = crc ^ ((uint16_t)data << 8);
|
||||
for (i=0; i<8; i++)
|
||||
{
|
||||
crc = crc ^ ((uint16_t) data << 8);
|
||||
for (i = 0; i < 8; i++) {
|
||||
if (crc & 0x8000)
|
||||
crc = (crc << 1) ^ 0x1021;
|
||||
else
|
||||
@@ -21,23 +20,22 @@ uint16_t crc_xmodem_update (uint16_t crc, uint8_t data)
|
||||
return crc;
|
||||
}
|
||||
|
||||
uint16_t do_crc(uint8_t * data,uint16_t size)
|
||||
uint16_t do_crc(uint8_t * data, uint16_t size)
|
||||
{
|
||||
uint16_t crc =0;
|
||||
uint16_t i;
|
||||
for (i=0; i<size; i++){
|
||||
crc = crc_xmodem_update(crc,data[i]);
|
||||
//printf("%x : %x\n",crc,data[i]);
|
||||
}
|
||||
return crc;
|
||||
uint16_t crc = 0;
|
||||
uint16_t i;
|
||||
for (i = 0; i < size; i++) {
|
||||
crc = crc_xmodem_update(crc, data[i]);
|
||||
// printf("%x : %x\n",crc,data[i]);
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
|
||||
uint16_t do_crc_update(uint16_t crc,uint8_t * data,uint16_t size)
|
||||
uint16_t do_crc_update(uint16_t crc, uint8_t * data, uint16_t size)
|
||||
{
|
||||
uint16_t i;
|
||||
for (i=0; i<size; i++)
|
||||
crc = crc_xmodem_update(crc,data[i]);
|
||||
return crc;
|
||||
uint16_t i;
|
||||
for (i = 0; i < size; i++)
|
||||
crc = crc_xmodem_update(crc, data[i]);
|
||||
return crc;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "debug.h"
|
||||
#include "uart.h"
|
||||
@@ -7,36 +7,38 @@
|
||||
|
||||
extern FILE uart_stdout;
|
||||
|
||||
void dump_packet(uint32_t addr,uint32_t len,uint8_t *packet){
|
||||
uint16_t i,j;
|
||||
uint16_t sum = 0;
|
||||
uint8_t clear=0;
|
||||
|
||||
for (i=0;i<len;i+=16) {
|
||||
|
||||
sum = 0;
|
||||
for (j=0;j<16;j++) {
|
||||
sum +=packet[i+j];
|
||||
}
|
||||
if (!sum){
|
||||
clear=1;
|
||||
continue;
|
||||
}
|
||||
if (clear){
|
||||
printf("*\n");
|
||||
clear = 0;
|
||||
}
|
||||
printf("%08lx:", addr + i);
|
||||
for (j=0;j<16;j++) {
|
||||
printf(" %02x", packet[i+j]);
|
||||
}
|
||||
printf(" |");
|
||||
for (j=0;j<16;j++) {
|
||||
if (packet[i+j]>=33 && packet[i+j]<=126 )
|
||||
printf("%c", packet[i+j]);
|
||||
else
|
||||
printf(".");
|
||||
}
|
||||
printf("|\n");
|
||||
}
|
||||
void dump_packet(uint32_t addr, uint32_t len, uint8_t * packet)
|
||||
{
|
||||
uint16_t i,
|
||||
j;
|
||||
uint16_t sum = 0;
|
||||
uint8_t clear = 0;
|
||||
|
||||
for (i = 0; i < len; i += 16) {
|
||||
|
||||
sum = 0;
|
||||
for (j = 0; j < 16; j++) {
|
||||
sum += packet[i + j];
|
||||
}
|
||||
if (!sum) {
|
||||
clear = 1;
|
||||
continue;
|
||||
}
|
||||
if (clear) {
|
||||
printf("*\n");
|
||||
clear = 0;
|
||||
}
|
||||
printf("%08lx:", addr + i);
|
||||
for (j = 0; j < 16; j++) {
|
||||
printf(" %02x", packet[i + j]);
|
||||
}
|
||||
printf(" |");
|
||||
for (j = 0; j < 16; j++) {
|
||||
if (packet[i + j] >= 33 && packet[i + j] <= 126)
|
||||
printf("%c", packet[i + j]);
|
||||
else
|
||||
printf(".");
|
||||
}
|
||||
printf("|\n");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
#include <avr/io.h>
|
||||
#include <avr/wdt.h>
|
||||
#include <avr/interrupt.h> /* for sei() */
|
||||
#include <util/delay.h> /* for _delay_ms() */
|
||||
#include <avr/interrupt.h> /* for sei() */
|
||||
#include <util/delay.h> /* for _delay_ms() */
|
||||
#include <stdlib.h>
|
||||
#include <avr/pgmspace.h> /* required by usbdrv.h */
|
||||
#include <avr/pgmspace.h> /* required by usbdrv.h */
|
||||
|
||||
#include "usbdrv.h"
|
||||
#include "oddebug.h" /* This is also an example for using debug macros */
|
||||
#include "requests.h" /* The custom request numbers we use */
|
||||
#include "oddebug.h" /* This is also an example for using debug
|
||||
* macros */
|
||||
#include "requests.h" /* The custom request numbers we use */
|
||||
#include "uart.h"
|
||||
#include "sram.h"
|
||||
#include "debug.h"
|
||||
@@ -21,137 +22,149 @@
|
||||
|
||||
extern FILE uart_stdout;
|
||||
|
||||
uint8_t read_buffer[BUFFER_SIZE];
|
||||
uint32_t req_addr = 0;
|
||||
uint32_t req_size;
|
||||
uint8_t req_bank;
|
||||
uint32_t req_bank_size;
|
||||
uint8_t req_state = REQ_IDLE;
|
||||
uint8_t rx_remaining = 0;
|
||||
uint8_t tx_remaining = 0;
|
||||
uint16_t sync_errors = 0;
|
||||
uint8_t tx_buffer[32];
|
||||
uint8_t data_buffer[4];
|
||||
uint32_t addr;
|
||||
uint8_t read_buffer[BUFFER_SIZE];
|
||||
uint32_t req_addr = 0;
|
||||
uint32_t req_size;
|
||||
uint8_t req_bank;
|
||||
uint32_t req_bank_size;
|
||||
uint8_t req_state = REQ_IDLE;
|
||||
uint8_t rx_remaining = 0;
|
||||
uint8_t tx_remaining = 0;
|
||||
uint16_t sync_errors = 0;
|
||||
uint8_t tx_buffer[32];
|
||||
uint8_t data_buffer[4];
|
||||
uint32_t addr;
|
||||
|
||||
void crc_check_memory(uint32_t top_addr){
|
||||
uint16_t crc = 0;
|
||||
void crc_check_memory(uint32_t top_addr)
|
||||
{
|
||||
uint16_t crc = 0;
|
||||
uint32_t addr;
|
||||
req_bank = 0;
|
||||
for (addr=0x000000; addr < top_addr; addr+=BUFFER_SIZE) {
|
||||
sram_read_buffer(addr,read_buffer,BUFFER_SIZE);
|
||||
crc = do_crc_update(crc,read_buffer,BUFFER_SIZE);
|
||||
if (addr && addr%32768 == 0){
|
||||
printf("crc_check_memory: req_bank: 0x%x Addr: 0x%lx CRC: %x\n",req_bank,addr,crc);
|
||||
req_bank = 0;
|
||||
for (addr = 0x000000; addr < top_addr; addr += BUFFER_SIZE) {
|
||||
sram_read_buffer(addr, read_buffer, BUFFER_SIZE);
|
||||
crc = do_crc_update(crc, read_buffer, BUFFER_SIZE);
|
||||
if (addr && addr % 32768 == 0) {
|
||||
printf("crc_check_memory: req_bank: 0x%x Addr: 0x%lx CRC: %x\n",
|
||||
req_bank, addr, crc);
|
||||
req_bank++;
|
||||
crc = 0;
|
||||
}
|
||||
crc = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void crc_check_memory_range(uint32_t start_addr,uint32_t size){
|
||||
uint16_t crc = 0;
|
||||
void crc_check_memory_range(uint32_t start_addr, uint32_t size)
|
||||
{
|
||||
uint16_t crc = 0;
|
||||
uint32_t addr;
|
||||
req_bank = 0;
|
||||
for (addr=start_addr; addr < start_addr + size; addr+=BUFFER_SIZE) {
|
||||
sram_read_buffer(addr,read_buffer,BUFFER_SIZE);
|
||||
crc = do_crc_update(crc,read_buffer,BUFFER_SIZE);
|
||||
}
|
||||
req_bank = 0;
|
||||
for (addr = start_addr; addr < start_addr + size; addr += BUFFER_SIZE) {
|
||||
sram_read_buffer(addr, read_buffer, BUFFER_SIZE);
|
||||
crc = do_crc_update(crc, read_buffer, BUFFER_SIZE);
|
||||
}
|
||||
tx_buffer[0] = crc & 0xff;
|
||||
tx_buffer[1] = (crc >> 8) & 0xff;
|
||||
printf("crc_check_memory_range: Addr: 0x%lx CRC: %x\n",addr,crc);
|
||||
printf("crc_check_memory_range: Addr: 0x%lx CRC: %x\n", addr, crc);
|
||||
}
|
||||
|
||||
|
||||
usbMsgLen_t usbFunctionSetup(uchar data[8]){
|
||||
|
||||
usbRequest_t *rq = (void *)data;
|
||||
usbMsgLen_t usbFunctionSetup(uchar data[8])
|
||||
{
|
||||
|
||||
usbRequest_t *rq = (void *) data;
|
||||
uint8_t ret_len = 0;
|
||||
if(rq->bRequest == USB_UPLOAD_INIT){
|
||||
req_bank =0;
|
||||
rx_remaining=0;
|
||||
req_bank_size= 1 << rq->wValue.word;
|
||||
sync_errors=0;
|
||||
printf("USB_UPLOAD_INIT: bank size %li\n",req_bank_size);
|
||||
}else if(rq->bRequest == USB_UPLOAD_ADDR){ /* echo -- used for reliability tests */
|
||||
if (rq->bRequest == USB_UPLOAD_INIT) {
|
||||
req_bank = 0;
|
||||
rx_remaining = 0;
|
||||
req_bank_size = 1 << rq->wValue.word;
|
||||
sync_errors = 0;
|
||||
printf("USB_UPLOAD_INIT: bank size %li\n", req_bank_size);
|
||||
} else if (rq->bRequest == USB_UPLOAD_ADDR) { /* echo -- used for
|
||||
* reliability tests */
|
||||
req_state = REQ_UPLOAD;
|
||||
req_addr = rq->wValue.word;
|
||||
req_addr = req_addr << 16;
|
||||
req_addr = req_addr | rq->wIndex.word;
|
||||
if (rx_remaining){
|
||||
if (rx_remaining) {
|
||||
sync_errors++;
|
||||
printf("USB_UPLOAD_ADDR: Out of sync Addr=0x%lx remain=%i packet=%i sync_error=%i\n",req_addr,rx_remaining,rq->wLength.word,sync_errors );
|
||||
ret_len=0;
|
||||
}
|
||||
rx_remaining = rq->wLength.word;
|
||||
printf
|
||||
("USB_UPLOAD_ADDR: Out of sync Addr=0x%lx remain=%i packet=%i sync_error=%i\n",
|
||||
req_addr, rx_remaining, rq->wLength.word, sync_errors);
|
||||
ret_len = 0;
|
||||
}
|
||||
rx_remaining = rq->wLength.word;
|
||||
ret_len = 0xff;
|
||||
if (req_addr && req_addr % req_bank_size== 0){
|
||||
printf("USB_UPLOAD_ADDR: req_bank: 0x%x Addr: 0x%08lx \n",req_bank,req_addr);
|
||||
if (req_addr && req_addr % req_bank_size == 0) {
|
||||
printf("USB_UPLOAD_ADDR: req_bank: 0x%x Addr: 0x%08lx \n",
|
||||
req_bank, req_addr);
|
||||
req_bank++;
|
||||
}
|
||||
ret_len=0xff;
|
||||
}else if(rq->bRequest == USB_DOWNLOAD_INIT){
|
||||
}
|
||||
ret_len = 0xff;
|
||||
} else if (rq->bRequest == USB_DOWNLOAD_INIT) {
|
||||
printf("USB_DOWNLOAD_INIT\n");
|
||||
}else if(rq->bRequest == USB_DOWNLOAD_ADDR){
|
||||
} else if (rq->bRequest == USB_DOWNLOAD_ADDR) {
|
||||
printf("USB_DOWNLOAD_ADDR\n");
|
||||
}else if(rq->bRequest == USB_CRC){
|
||||
} else if (rq->bRequest == USB_CRC) {
|
||||
req_addr = rq->wValue.word;
|
||||
req_addr = req_addr << 16;
|
||||
req_addr = req_addr | rq->wIndex.word;
|
||||
printf("USB_CRC: Addr 0x%lx \n", req_addr);
|
||||
printf("USB_CRC: Addr 0x%lx \n", req_addr);
|
||||
cli();
|
||||
crc_check_memory(req_addr);
|
||||
sei();
|
||||
}else if(rq->bRequest == USB_CRC_ADDR){
|
||||
} else if (rq->bRequest == USB_CRC_ADDR) {
|
||||
req_addr = rq->wValue.word;
|
||||
req_addr = req_addr << 16;
|
||||
req_addr = req_addr | rq->wIndex.word;
|
||||
printf("USB_CRC_ADDR: Addr: 0x%lx Size: %i\n", req_addr,rq->wLength.word);
|
||||
printf("USB_CRC_ADDR: Addr: 0x%lx Size: %i\n", req_addr,
|
||||
rq->wLength.word);
|
||||
req_size = rq->wLength.word;
|
||||
req_size = req_size << 2;
|
||||
tx_remaining = 2;
|
||||
printf("USB_CRC_ADDR: Addr: 0x%lx Size: %li\n", req_addr,req_size);
|
||||
printf("USB_CRC_ADDR: Addr: 0x%lx Size: %li\n", req_addr, req_size);
|
||||
cli();
|
||||
//crc_check_memory_range(req_addr,req_size);
|
||||
// crc_check_memory_range(req_addr,req_size);
|
||||
sei();
|
||||
ret_len=2;
|
||||
ret_len = 2;
|
||||
}
|
||||
|
||||
|
||||
usbMsgPtr = data_buffer;
|
||||
return ret_len; /* default for not implemented requests: return no data back to host */
|
||||
return ret_len; /* default for not implemented requests: return
|
||||
* no data back to host */
|
||||
}
|
||||
|
||||
|
||||
uint8_t usbFunctionWrite(uint8_t *data, uint8_t len)
|
||||
uint8_t usbFunctionWrite(uint8_t * data, uint8_t len)
|
||||
{
|
||||
if (len > rx_remaining){
|
||||
printf("usbFunctionWrite more data than expected remain: %i len: %i\n",rx_remaining,len);
|
||||
len = rx_remaining;
|
||||
}
|
||||
if (req_state==REQ_UPLOAD){
|
||||
|
||||
if (len > rx_remaining) {
|
||||
printf("usbFunctionWrite more data than expected remain: %i len: %i\n",
|
||||
rx_remaining, len);
|
||||
len = rx_remaining;
|
||||
}
|
||||
if (req_state == REQ_UPLOAD) {
|
||||
|
||||
rx_remaining -= len;
|
||||
#if 1
|
||||
printf("usbFunctionWrite addr: 0x%08lx len: %i rx_remaining=%i\n",req_addr,len,rx_remaining);
|
||||
#endif
|
||||
#if 1
|
||||
printf("usbFunctionWrite addr: 0x%08lx len: %i rx_remaining=%i\n",
|
||||
req_addr, len, rx_remaining);
|
||||
#endif
|
||||
cli();
|
||||
sram_copy(req_addr,data,len);
|
||||
sram_copy(req_addr, data, len);
|
||||
sei();
|
||||
req_addr +=len;
|
||||
req_addr += len;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
uint8_t usbFunctionRead(uint8_t *data, uint8_t len)
|
||||
uint8_t usbFunctionRead(uint8_t * data, uint8_t len)
|
||||
{
|
||||
uint8_t i;
|
||||
if(len > tx_remaining)
|
||||
if (len > tx_remaining)
|
||||
len = tx_remaining;
|
||||
tx_remaining -= len;
|
||||
#if 1
|
||||
printf("usbFunctionRead len=%i tx_remaining=%i \n",len,tx_remaining);
|
||||
#endif
|
||||
#if 1
|
||||
printf("usbFunctionRead len=%i tx_remaining=%i \n", len, tx_remaining);
|
||||
#endif
|
||||
for (i = 0; i < len; i++) {
|
||||
*data = tx_buffer[len];
|
||||
data++;
|
||||
@@ -159,7 +172,9 @@ uint8_t usbFunctionRead(uint8_t *data, uint8_t len)
|
||||
return len;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
/*
|
||||
* -------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
int main(void)
|
||||
{
|
||||
@@ -168,27 +183,30 @@ int main(void)
|
||||
uart_init();
|
||||
stdout = &uart_stdout;
|
||||
sram_init();
|
||||
printf("SRAM Init\n");
|
||||
spi_init();
|
||||
printf("SPI Init\n");
|
||||
printf("SRAM Init\n");
|
||||
spi_init();
|
||||
printf("SPI Init\n");
|
||||
usbInit();
|
||||
printf("USB Init\n");
|
||||
usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */
|
||||
printf("USB disconnect\n");
|
||||
printf("USB Init\n");
|
||||
usbDeviceDisconnect(); /* enforce re-enumeration, do this while
|
||||
* interrupts are disabled! */
|
||||
printf("USB disconnect\n");
|
||||
i = 10;
|
||||
while(--i){ /* fake USB disconnect for > 250 ms */
|
||||
while (--i) { /* fake USB disconnect for > 250 ms */
|
||||
wdt_reset();
|
||||
_delay_ms(1);
|
||||
}
|
||||
usbDeviceConnect();
|
||||
printf("USB connect\n");
|
||||
printf("USB connect\n");
|
||||
sei();
|
||||
printf("USB poll\n");
|
||||
for(;;){ /* main event loop */
|
||||
printf("USB poll\n");
|
||||
for (;;) { /* main event loop */
|
||||
wdt_reset();
|
||||
usbPoll();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------- */
|
||||
/*
|
||||
* -------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdint.h>
|
||||
#include <avr/io.h>
|
||||
|
||||
#include "sram.h"
|
||||
@@ -8,150 +8,165 @@
|
||||
|
||||
void spi_init(void)
|
||||
{
|
||||
/* Set MOSI and SCK output, all others input */
|
||||
SPI_DIR |= ((1<<S_MOSI) | (1<<S_SCK) | (1<<S_LATCH));
|
||||
SPI_DIR &= ~(1<<S_MISO);
|
||||
SPI_PORT |= (1<<S_MISO);
|
||||
/* Enable SPI, Master*/
|
||||
SPCR = ((1<<SPE) | (1<<MSTR));
|
||||
/*
|
||||
* Set MOSI and SCK output, all others input
|
||||
*/
|
||||
SPI_DIR |= ((1 << S_MOSI) | (1 << S_SCK) | (1 << S_LATCH));
|
||||
SPI_DIR &= ~(1 << S_MISO);
|
||||
SPI_PORT |= (1 << S_MISO);
|
||||
/*
|
||||
* Enable SPI, Master
|
||||
*/
|
||||
SPCR = ((1 << SPE) | (1 << MSTR));
|
||||
}
|
||||
|
||||
void spi_master_transmit(unsigned char cData)
|
||||
{
|
||||
/* Start transmission */
|
||||
SPDR = cData;
|
||||
/*
|
||||
* Start transmission
|
||||
*/
|
||||
SPDR = cData;
|
||||
|
||||
/* Wait for transmission complete */
|
||||
while(!(SPSR & (1<<SPIF)));
|
||||
/*
|
||||
* Wait for transmission complete
|
||||
*/
|
||||
while (!(SPSR & (1 << SPIF)));
|
||||
}
|
||||
|
||||
|
||||
void sram_set_addr(uint32_t addr)
|
||||
{
|
||||
spi_master_transmit((uint8_t)(addr>>16));
|
||||
spi_master_transmit((uint8_t)(addr>>8));
|
||||
spi_master_transmit((uint8_t)(addr>>0));
|
||||
spi_master_transmit((uint8_t) (addr >> 16));
|
||||
spi_master_transmit((uint8_t) (addr >> 8));
|
||||
spi_master_transmit((uint8_t) (addr >> 0));
|
||||
|
||||
LATCH_PORT |= (1<<S_LATCH);
|
||||
LATCH_PORT &= ~(1<<S_LATCH);
|
||||
LATCH_PORT |= (1 << S_LATCH);
|
||||
LATCH_PORT &= ~(1 << S_LATCH);
|
||||
}
|
||||
|
||||
uint8_t sram_read(uint32_t addr)
|
||||
{
|
||||
uint8_t byte;
|
||||
uint8_t byte;
|
||||
|
||||
RAM_DIR = 0x00;
|
||||
RAM_PORT = 0xff;
|
||||
RAM_DIR = 0x00;
|
||||
RAM_PORT = 0xff;
|
||||
|
||||
CTRL_PORT |= (1<<R_RD);
|
||||
CTRL_PORT |= (1<<R_WR);
|
||||
CTRL_PORT |= (1 << R_RD);
|
||||
CTRL_PORT |= (1 << R_WR);
|
||||
|
||||
spi_master_transmit((uint8_t)(addr>>16));
|
||||
spi_master_transmit((uint8_t)(addr>>8));
|
||||
spi_master_transmit((uint8_t)(addr>>0));
|
||||
spi_master_transmit((uint8_t) (addr >> 16));
|
||||
spi_master_transmit((uint8_t) (addr >> 8));
|
||||
spi_master_transmit((uint8_t) (addr >> 0));
|
||||
|
||||
LATCH_PORT |= (1<<S_LATCH);
|
||||
LATCH_PORT &= ~(1<<S_LATCH);
|
||||
CTRL_PORT &= ~(1<<R_RD);
|
||||
|
||||
asm volatile ("nop");
|
||||
asm volatile ("nop");
|
||||
asm volatile ("nop");
|
||||
asm volatile ("nop");
|
||||
asm volatile ("nop");
|
||||
asm volatile ("nop");
|
||||
asm volatile ("nop");
|
||||
asm volatile ("nop");
|
||||
LATCH_PORT |= (1 << S_LATCH);
|
||||
LATCH_PORT &= ~(1 << S_LATCH);
|
||||
CTRL_PORT &= ~(1 << R_RD);
|
||||
|
||||
byte = RAM_REG;
|
||||
CTRL_PORT |= (1<<R_RD);
|
||||
RAM_DIR =0x00;
|
||||
RAM_PORT =0x00;
|
||||
return byte;
|
||||
asm volatile ("nop");
|
||||
asm volatile ("nop");
|
||||
asm volatile ("nop");
|
||||
asm volatile ("nop");
|
||||
asm volatile ("nop");
|
||||
asm volatile ("nop");
|
||||
asm volatile ("nop");
|
||||
asm volatile ("nop");
|
||||
|
||||
byte = RAM_REG;
|
||||
CTRL_PORT |= (1 << R_RD);
|
||||
RAM_DIR = 0x00;
|
||||
RAM_PORT = 0x00;
|
||||
return byte;
|
||||
}
|
||||
|
||||
void sram_write(uint32_t addr, uint8_t data)
|
||||
{
|
||||
RAM_DIR = 0xff;
|
||||
RAM_DIR = 0xff;
|
||||
|
||||
CTRL_PORT |= (1<<R_RD);
|
||||
CTRL_PORT |= (1<<R_WR);
|
||||
CTRL_PORT |= (1 << R_RD);
|
||||
CTRL_PORT |= (1 << R_WR);
|
||||
|
||||
spi_master_transmit((uint8_t)(addr>>16));
|
||||
spi_master_transmit((uint8_t)(addr>>8));
|
||||
spi_master_transmit((uint8_t)(addr>>0));
|
||||
spi_master_transmit((uint8_t) (addr >> 16));
|
||||
spi_master_transmit((uint8_t) (addr >> 8));
|
||||
spi_master_transmit((uint8_t) (addr >> 0));
|
||||
|
||||
LATCH_PORT |= (1<<S_LATCH);
|
||||
LATCH_PORT &= ~(1<<S_LATCH);
|
||||
|
||||
LATCH_PORT |= (1 << S_LATCH);
|
||||
LATCH_PORT &= ~(1 << S_LATCH);
|
||||
|
||||
CTRL_PORT &= ~(1<<R_WR);
|
||||
|
||||
RAM_PORT = data;
|
||||
CTRL_PORT |= (1<<R_WR);
|
||||
CTRL_PORT &= ~(1 << R_WR);
|
||||
|
||||
RAM_DIR = 0x00;
|
||||
RAM_PORT = 0x00;
|
||||
RAM_PORT = data;
|
||||
CTRL_PORT |= (1 << R_WR);
|
||||
|
||||
RAM_DIR = 0x00;
|
||||
RAM_PORT = 0x00;
|
||||
}
|
||||
|
||||
void sram_init(void){
|
||||
|
||||
RAM_DIR = 0x00;
|
||||
RAM_PORT = 0x00;
|
||||
void sram_init(void)
|
||||
{
|
||||
|
||||
CTRL_DIR |= ((1<<R_WR) | (1<<R_RD));
|
||||
CTRL_PORT |= (1<<R_RD);
|
||||
CTRL_PORT |= (1<<R_WR);
|
||||
RAM_DIR = 0x00;
|
||||
RAM_PORT = 0x00;
|
||||
|
||||
LED_PORT |= (1<<D_LED0);
|
||||
CTRL_DIR |= ((1 << R_WR) | (1 << R_RD));
|
||||
CTRL_PORT |= (1 << R_RD);
|
||||
CTRL_PORT |= (1 << R_WR);
|
||||
|
||||
LED_PORT |= (1 << D_LED0);
|
||||
}
|
||||
|
||||
void sram_snes_mode01(void){
|
||||
CTRL_PORT |= (1<<R_WR);
|
||||
CTRL_PORT &= ~(1<<R_RD);
|
||||
void sram_snes_mode01(void)
|
||||
{
|
||||
CTRL_PORT |= (1 << R_WR);
|
||||
CTRL_PORT &= ~(1 << R_RD);
|
||||
}
|
||||
|
||||
void sram_snes_mode02(void){
|
||||
CTRL_DIR |= (1<<R_WR);
|
||||
CTRL_PORT |= (1<<R_WR);
|
||||
//CTRL_PORT &= ~(1<<R_RD);
|
||||
CTRL_DIR &= ~(1<<R_RD);
|
||||
CTRL_PORT &= ~(1<<R_RD);
|
||||
|
||||
void sram_snes_mode02(void)
|
||||
{
|
||||
CTRL_DIR |= (1 << R_WR);
|
||||
CTRL_PORT |= (1 << R_WR);
|
||||
// CTRL_PORT &= ~(1<<R_RD);
|
||||
CTRL_DIR &= ~(1 << R_RD);
|
||||
CTRL_PORT &= ~(1 << R_RD);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void sram_clear(uint32_t addr, uint32_t len){
|
||||
void sram_clear(uint32_t addr, uint32_t len)
|
||||
{
|
||||
|
||||
uint32_t i;
|
||||
for (i=addr; i<(addr + len);i++ ){
|
||||
if (0==i%0xfff)
|
||||
printf("sram_clear %lx\n\r",i);
|
||||
sram_write(i, 0x00);
|
||||
}
|
||||
uint32_t i;
|
||||
for (i = addr; i < (addr + len); i++) {
|
||||
if (0 == i % 0xfff)
|
||||
printf("sram_clear %lx\n\r", i);
|
||||
sram_write(i, 0x00);
|
||||
}
|
||||
}
|
||||
|
||||
void sram_copy(uint32_t addr,uint8_t *src, uint32_t len){
|
||||
void sram_copy(uint32_t addr, uint8_t * src, uint32_t len)
|
||||
{
|
||||
|
||||
uint32_t i;
|
||||
uint8_t *ptr = src;
|
||||
for (i=addr; i<(addr + len);i++ )
|
||||
sram_write(i, *ptr++);
|
||||
uint32_t i;
|
||||
uint8_t *ptr = src;
|
||||
for (i = addr; i < (addr + len); i++)
|
||||
sram_write(i, *ptr++);
|
||||
}
|
||||
|
||||
void sram_read_buffer(uint32_t addr,uint8_t *dst, uint32_t len){
|
||||
void sram_read_buffer(uint32_t addr, uint8_t * dst, uint32_t len)
|
||||
{
|
||||
|
||||
uint32_t i;
|
||||
uint8_t *ptr = dst;
|
||||
for (i=addr; i<(addr + len);i++ ){
|
||||
*ptr = sram_read(i);
|
||||
ptr++;
|
||||
}
|
||||
uint32_t i;
|
||||
uint8_t *ptr = dst;
|
||||
for (i = addr; i < (addr + len); i++) {
|
||||
*ptr = sram_read(i);
|
||||
ptr++;
|
||||
}
|
||||
}
|
||||
uint8_t sram_check(uint8_t *buffer, uint32_t len){
|
||||
uint8_t sram_check(uint8_t * buffer, uint32_t len)
|
||||
{
|
||||
uint16_t cnt;
|
||||
for (cnt=0; cnt<len; cnt++)
|
||||
if (buffer[cnt])
|
||||
return 1;
|
||||
return 0;
|
||||
for (cnt = 0; cnt < len; cnt++)
|
||||
if (buffer[cnt])
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -5,13 +5,11 @@
|
||||
#include "uart.h"
|
||||
#include "fifo.h"
|
||||
|
||||
volatile struct
|
||||
{
|
||||
uint8_t tmr_int: 1;
|
||||
uint8_t adc_int: 1;
|
||||
uint8_t rx_int: 1;
|
||||
}
|
||||
intflags;
|
||||
volatile struct {
|
||||
uint8_t tmr_int:1;
|
||||
uint8_t adc_int:1;
|
||||
uint8_t rx_int:1;
|
||||
} intflags;
|
||||
|
||||
/*
|
||||
* * Last character read from the UART.
|
||||
@@ -24,9 +22,10 @@ FILE uart_stdout = FDEV_SETUP_STREAM(uart_stream, NULL, _FDEV_SETUP_WRITE);
|
||||
|
||||
void uart_init(void)
|
||||
{
|
||||
UCSRA = _BV(U2X); /* improves baud rate error @ F_CPU = 1 MHz */
|
||||
UCSRB = _BV(TXEN)|_BV(RXEN)|_BV(RXCIE); /* tx/rx enable, rx complete intr */
|
||||
UBRRL = (F_CPU / (8 * 115200UL)) - 1; /* 9600 Bd */
|
||||
UCSRA = _BV(U2X); /* improves baud rate error @ F_CPU = 1 MHz */
|
||||
UCSRB = _BV(TXEN) | _BV(RXEN) | _BV(RXCIE); /* tx/rx enable, rx complete
|
||||
* intr */
|
||||
UBRRL = (F_CPU / (8 * 115200UL)) - 1; /* 9600 Bd */
|
||||
|
||||
}
|
||||
|
||||
@@ -35,14 +34,14 @@ ISR(USART_RXC_vect)
|
||||
{
|
||||
uint8_t c;
|
||||
c = UDR;
|
||||
if (bit_is_clear(UCSRA, FE)){
|
||||
if (bit_is_clear(UCSRA, FE)) {
|
||||
rxbuff = c;
|
||||
intflags.rx_int = 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void uart_putc(uint8_t c)
|
||||
void uart_putc(uint8_t c)
|
||||
{
|
||||
loop_until_bit_is_set(UCSRA, UDRE);
|
||||
UDR = c;
|
||||
@@ -68,7 +67,7 @@ void uart_puts_P(PGM_P s)
|
||||
}
|
||||
}
|
||||
|
||||
static int uart_stream(char c, FILE *stream)
|
||||
static int uart_stream(char c, FILE * stream)
|
||||
{
|
||||
if (c == '\n')
|
||||
uart_putc('\r');
|
||||
@@ -76,4 +75,3 @@ static int uart_stream(char c, FILE *stream)
|
||||
UDR = c;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user