?? cbootloader.cpp
字號:
/* cbootloader.cpp - part of flashtool for AVRUSBBoot, an USB bootloader for Atmel AVR controllers Thomas Fischl <tfischl@gmx.de> Creation Date..: 2006-03-18 Last change....: 2006-06-25 Parts are taken from the PowerSwitch project by Objective Development Software GmbH*/
//#include <windows.h>
#include "usb.h"
#include "cbootloader.h"
//#include <string.h>
extern void UpdataMessageStr(char* mystring);
extern void UpdataMessageVal(unsigned int i);static int usbGetStringAscii(usb_dev_handle *dev, int index, int langid, char *buf, int buflen){char buffer[256];int rval, i; if((rval = usb_control_msg(dev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, langid, buffer, sizeof(buffer), 1000)) < 0) return rval; if(buffer[1] != USB_DT_STRING) 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;}/* This project uses the free shared default VID/PID. If you want to see an * example device lookup where an individually reserved PID is used, see our * RemoteSensor reference implementation. */static usb_dev_handle *findDevice(void){struct usb_bus *bus;struct usb_device *dev;usb_dev_handle *handle = 0; usb_find_busses(); usb_find_devices(); for(bus=usb_get_busses(); bus; bus=bus->next){ for(dev=bus->devices; dev; dev=dev->next){ if(dev->descriptor.idVendor == USBDEV_SHARED_VENDOR && dev->descriptor.idProduct == USBDEV_SHARED_PRODUCT){ char string[256]; int len; handle = usb_open(dev); /* we need to open the device in order to query strings */ if(!handle){ //fprintf(stderr, "Warning: cannot open USB device: %s\n", usb_strerror());
UpdataMessageStr("Warning: cannot open USB device:");
UpdataMessageStr(usb_strerror());
UpdataMessageStr("\r\n");
continue; } len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, 0x0409, string, sizeof(string)); if(len < 0){ //fprintf(stderr, "warning: cannot query manufacturer for device: %s\n", usb_strerror());
UpdataMessageStr("warning: cannot query manufacturer for device: ");
UpdataMessageStr(usb_strerror());
UpdataMessageStr("\r\n"); goto skipDevice; } if(strcmp(string, "www.fischl.de") != 0) goto skipDevice; len = usbGetStringAscii(handle, dev->descriptor.iProduct, 0x0409, string, sizeof(string)); if(len < 0){ //fprintf(stderr, "warning: cannot query product for device: %s\n", usb_strerror());
UpdataMessageStr("warning: cannot query product for device: ");
UpdataMessageStr(usb_strerror());
UpdataMessageStr("\r\n"); goto skipDevice; } //fprintf(stderr, "seen product ->%s<-\n", string);
//UpdataMessageStr(usb_strerror());
//UpdataMessageStr("seen product ->");
//UpdataMessageStr(string);
//UpdataMessageStr("<-");
//UpdataMessageStr("\r\n"); if(strcmp(string, "AVRUSBBoot") == 0) break;skipDevice: usb_close(handle); handle = NULL; } } if(handle) break; } if(!handle)
{ // fprintf(stderr, "Could not find USB device www.fischl.de/AVRUSBBoot\n");
UpdataMessageStr("Could not find USB device www.fischl.de/AVRUSBBoot");
UpdataMessageStr("\r\n");
} return handle;}CBootloader::CBootloader() {
char *vname = "";
char *devname = "";
device=NULL; if(usbOpenDevice(&device, USBDEV_SHARED_VENDOR, vname, USBDEV_SHARED_PRODUCT, devname, 1)){ //fprintf(stderr, "Could not find USB device \"AVRUSBBoot\" with vid=0x%x pid=0x%x\n", USBDEV_SHARED_VENDOR, USBDEV_SHARED_PRODUCT); //exit(1);
UpdataMessageStr("Could not find USB device with VID=0x");
UpdataMessageVal(USBDEV_SHARED_VENDOR);
UpdataMessageStr(" PID=0x");
UpdataMessageVal(USBDEV_SHARED_PRODUCT);
UpdataMessageStr("\r\n"); }}CBootloader::~CBootloader() {
usbCloseDevice(device);}unsigned int CBootloader::getPagesize() {
char buffer[132]; int ErrorCode;
int len=132;
unsigned int pagesize;
if(device==NULL)//ssda
return 0; ErrorCode=usbGetReport(device, USB_HID_REPORT_TYPE_FEATURE, 1, (char *)buffer, &len); if (ErrorCode!=0) { //fprintf(stderr, "Error: wrong response size in getPageSize: %d !\n", nBytes);
//exit(1);
UpdataMessageStr("Error: wrong response size in getPageSize:");
UpdataMessageVal(ErrorCode);
UpdataMessageStr("\r\n"); return 0; }
pagesize=(unsigned int)((unsigned char)(buffer[1] << 8) | (unsigned char)buffer[2]); return pagesize;}void CBootloader::startApplication() { char buffer[132]; int len=132;
int ErrorCode; buffer[0]=1;
buffer[1]=1; ErrorCode=usbSetReport(device, USB_HID_REPORT_TYPE_FEATURE,(char *)buffer, len); if (ErrorCode != 0) {
UpdataMessageStr("Error: wrong response size in startApplication:");
UpdataMessageVal(ErrorCode);
UpdataMessageStr("\r\n");
exit(1); }}void CBootloader::writePage(CPage* page)
{ //unsigned int nBytes;
char buffer[132];
int len=132;
int ErrorCode;
buffer[0]=1;
buffer[1]=2;
buffer[2]=page->getPageaddress()&0x00ff;
buffer[3]=(page->getPageaddress()&0xff00)>>8;
memcpy(buffer+4,page->getData(),(unsigned int)page->getPagesize());
ErrorCode=usbSetReport(device, USB_HID_REPORT_TYPE_FEATURE,(char *)buffer, len); //nBytes = usb_control_msg(usbhandle, // USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, // 2, page->getPageaddress(), 0, // (char*) page->getData(), page->getPagesize(), // 5000); if (ErrorCode!=0)
{
UpdataMessageStr("Error: wrong byte count in writePage:");
UpdataMessageVal(ErrorCode);
UpdataMessageStr("\r\n");
exit(1); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -