o add first working draft of usb version
o having bad sync problems only can use 4 bytes buffer, supper slow
This commit is contained in:
@@ -22,7 +22,7 @@ EXE_SUFFIX =
|
||||
#USBLIBS = -L/usr/local/lib -lusb
|
||||
#EXE_SUFFIX = .exe
|
||||
|
||||
NAME = set-led
|
||||
NAME = snesuploader
|
||||
|
||||
OBJECTS = opendevice.o $(NAME).o
|
||||
|
||||
|
||||
@@ -1,135 +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)
|
||||
* This Revision: $Id: set-led.c 692 2008-11-07 15:07:40Z cs $
|
||||
*/
|
||||
|
||||
/*
|
||||
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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <usb.h> /* this is libusb */
|
||||
#include "opendevice.h" /* common code moved to separate module */
|
||||
|
||||
#include "../requests.h" /* custom request numbers */
|
||||
#include "../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;
|
||||
}
|
||||
162
poc/avr_usbload/commandline/snesuploader.c
Normal file
162
poc/avr_usbload/commandline/snesuploader.c
Normal file
@@ -0,0 +1,162 @@
|
||||
/* 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)
|
||||
* This Revision: $Id: set-led.c 692 2008-11-07 15:07:40Z cs $
|
||||
*/
|
||||
|
||||
/*
|
||||
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 BUFFER_SIZE 8
|
||||
#define BUFFER_CRC (1024 * 64)
|
||||
#define BANK_SIZE (1<<15)
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <usb.h> /* this is libusb */
|
||||
#include "opendevice.h" /* common code moved to separate module */
|
||||
|
||||
#include "../requests.h" /* custom request numbers */
|
||||
#include "../usbconfig.h" /* device's VID/PID and names */
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
return crc;
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void usage(char *name)
|
||||
{
|
||||
fprintf(stderr, "usage:\n");
|
||||
fprintf(stderr, " %s upload filename.. upload\n", name);
|
||||
}
|
||||
|
||||
|
||||
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};
|
||||
int cnt, vid, pid;
|
||||
int cnt_crc = 0;
|
||||
unsigned char *read_buffer;
|
||||
unsigned char *crc_buffer;
|
||||
unsigned char setup_buffer[8];
|
||||
unsigned int addr = 0;
|
||||
unsigned int bank_addr;
|
||||
unsigned int bank_num;
|
||||
uint16_t crc = 0;
|
||||
FILE *fp ;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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(BUFFER_SIZE);
|
||||
crc_buffer = (unsigned char*)malloc(BUFFER_CRC);
|
||||
memset(crc_buffer,0,BUFFER_CRC);
|
||||
addr = 0x000000;
|
||||
bank_addr = addr& 0xffff;
|
||||
bank_num = (addr>>16) & 0xff;
|
||||
|
||||
while((cnt = fread(read_buffer, BUFFER_SIZE, 1, fp)) > 0){
|
||||
bank_addr = addr& 0xffff;
|
||||
bank_num = (addr>>16) & 0xff;
|
||||
|
||||
memcpy(crc_buffer + cnt_crc,read_buffer,BUFFER_SIZE);
|
||||
cnt_crc += BUFFER_SIZE;
|
||||
if (cnt_crc == BANK_SIZE){
|
||||
crc = do_crc(crc_buffer,BANK_SIZE);
|
||||
printf("Addr: 0x%06x Bank: 0x%02x Rom Addr: 0x%04x Addr: 0x%06x Cnt: 0x%04x\n",addr,bank_num, bank_addr, addr,crc);
|
||||
memset(crc_buffer,0,BUFFER_CRC);
|
||||
cnt_crc =0;
|
||||
}
|
||||
usb_control_msg(handle,
|
||||
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT,
|
||||
CUSTOM_RQ_UPLOAD, bank_num, bank_addr,
|
||||
(char*) read_buffer, BUFFER_SIZE,
|
||||
5000);
|
||||
addr += BUFFER_SIZE;
|
||||
//printf("Addr: 0x%06x Bank: 0x%02x Rom Addr: 0x%04x Addr: 0x%06x Cnt: 0x%04x cnt: %i\n",addr,bank_num, bank_addr, addr,crc,cnt_crc);
|
||||
}
|
||||
cnt = usb_control_msg(handle,
|
||||
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT,
|
||||
CUSTOM_RQ_CRC_CHECK, bank_num + 1, 0,
|
||||
(char*) setup_buffer, sizeof(setup_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{
|
||||
usage(argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
usb_close(handle);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user