dnw for linux(pc)

網上下載的開源dnw程序 ,在pc linux下用gcc編譯即可。

$gcc dnw2.c -o dnw -lusb

注意事項:

中: 編譯如果提示沒有usb.h文件,說明你的pc linux系統沒有安裝usb開發包,apt-get install libusb-dev

英:if not exist usb.h, please install libusb-dev, i.e  apt-get install libusb-dev


dnw2.c的源碼如下,這裏需要說明:

1.  如果你的開發板連接pc後提示找不到設備,很可能是因爲

QQ2440_SECBULK_IDVENDOR 
QQ2440_SECBULK_IDPRODUCT

這兩個宏定義跟固件內的定久不一致。 一可以修改開發板的usb download相關定義。二可以修改dnw2.c中的定義。

2.  在虛擬機下執行,發現USB寫速度太慢。 我的優化辦法是加大寫緩存,也就是把宏BLOCK_SIZE的值調大。

速度提升非常明顯


/*Linux main file. This depends on libusb.
*
* Author: Fox <[email protected]>
* License: GPL
*
*/
#include <stdio.h>
#include <usb.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h> 
#include <string.h>
#define QQ2440_SECBULK_IDVENDOR 0x5345
#define QQ2440_SECBULK_IDPRODUCT 0x1234


#define DOWNLOAD_ADDR_DEFAULT 0x32000000
struct usb_dev_handle * open_port()
{
struct usb_bus *busses, *bus;


usb_init();
usb_find_busses();
usb_find_devices();


busses = usb_get_busses();
for(bus=busses;bus;bus=bus->next)
{
struct usb_device *dev;
for(dev=bus->devices;dev;dev=dev->next)
{
printf("idVendor:0x%x\t,ipProduct:0x%x\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
if( QQ2440_SECBULK_IDVENDOR==dev->descriptor.idVendor
&& QQ2440_SECBULK_IDPRODUCT==dev->descriptor.idProduct)
{
printf("Target usb device found!\n");
struct usb_dev_handle *hdev = usb_open(dev);
if(!hdev)
{
perror("Cannot open device"); 
}
else
{
if(0!=usb_claim_interface(hdev, 0))
{
perror("Cannot claim interface");
usb_close(hdev);
hdev = NULL;
}
}
return hdev;
}
}
}


printf("Target usb device not found!\n");


return NULL;
}


void usage()
{
printf("Usage1: dnw <file>, Default Download Addr =0x%x\n", DOWNLOAD_ADDR_DEFAULT);
printf("Usage2: dnw <addr> <file>\n\n");
}


int myatoi( char *str )
{
   int data = 0;
   int off = 0;
   int len = 0;
   int isnegative = 0;
   int multi = 10;
   int i = 0;
  
  if( str == NULL || (len=strlen(str))==0)
  {
return data;
  }
  
  if( len == 0 )
  {
return 0;
  }

  if( str[off] == '-' )
  {
     isnegative = 1;
off++;
len--;
  }
  
  if( len == 0 )
  {
return 0;
  }
  
  //16進制
  if( len > 2 && str[off] == '0' && (str[off+1] == 'x' || str[off+1] == 'X') )
  {
multi = 16;
off += 2;
  }
  //2進制
  else  if( len > 2 && str[off] == '0' && (str[off+1] == 'b' || str[off+1] == 'B') )
  {
multi = 2;
off += 2;
  }
  //8進制
  else if( str[off] == '0' )
  {
multi = 8;
off += 1;
  } 
  //10進制
  else
  {
multi = 10;
  }
  
  if( multi == 16 )
  {
for( i = 0; ; i++ )
 {
if( str[i+off] >= '0' && str[i+off] <= '9' )
{
data = (data<<4) + (str[i+off]-'0');
}
else if( str[i+off] >= 'a' && str[i+off] <= 'z' )
{
data = (data<<4) + (str[i+off]-'a');
}
else if( str[i+off] >= 'A' && str[i+off] <= 'Z' )
{
data = (data<<4) + (str[i+off]-'A');
}
else 
{
break;
}  
 }  
  }
  else if( multi == 10 )
  {
for( i = 0; ; i++ )
 {
if( str[i+off] >= '0' && str[i+off] <= '9' )
{
data = (data*10) + (str[i+off]-'0');
}  
else 
{
break;
}  
 }  
  }
  else if( multi == 8 )
  {
for( i = 0; ; i++ )
 {
if( str[i+off] >= '0' && str[i+off] <= '7' )
{
data = (data<<3) + (str[i+off]-'0');
}  
else 
{
break;
}  
 }  
  }
  else if (multi == 2 )
  {
for( i = 0; ; i++ )
 {
if( str[i+off] == '0' || str[i+off] == '1' )
{
data = (data<<1) + (str[i+off]-'0');
}  
else 
{
break;
}  
 }  
  }
  else
  {
     return 0;
  }
  
  if( isnegative )
  {
     data *= -1;
  }
  
  return data; 
  
}




unsigned char* prepare_write_buf(unsigned int addr, char *filename, unsigned int *len)
{
unsigned char *write_buf = NULL;
struct stat fs;
unsigned int i;
unsigned short checksum = 0;
unsigned char *pChar = NULL;


int fd = open(filename, O_RDONLY);
if(-1==fd)
{
perror("Cannot open file");
return NULL;
}
if(-1==fstat(fd, &fs))
{
perror("Cannot get file size");
goto error;
}
write_buf = (unsigned char*)malloc(fs.st_size+10);
if(NULL==write_buf)
{
perror("malloc failed");
goto error;
}


if(fs.st_size != read(fd, write_buf+8, fs.st_size))
{
perror("Reading file failed");
goto error;
}


printf("Filename : %s\n", filename);
printf("Filesize : %d bytes\n", (int)fs.st_size);


*((u_int32_t*)write_buf) = addr; //download address
*((u_int32_t*)write_buf+1) = fs.st_size + 10; //download size;


printf("StartAddr:0x%x\n",*((u_int32_t*)write_buf));
printf("Size:0x%x\n",*((u_int32_t*)write_buf+1));



pChar = write_buf+8;
for( i = 0; i < fs.st_size; i++ )
{
checksum += *pChar++; 
}

printf("CheckSum:0x%x\n",checksum);


pChar = write_buf + 8 + fs.st_size;
pChar[0]= (unsigned char)(checksum&0xff);
pChar[1]= (unsigned char)((checksum>>8)&0xff);


*len = fs.st_size + 10;


close(fd);

return write_buf;


error:
if( fd != -1 )
{
close(fd);
}

if( NULL != write_buf ) 
{
free(write_buf);
}
fs.st_size = 0;
return NULL;
}


int main(int argc, char *argv[])
{
unsigned int downloadaddr = DOWNLOAD_ADDR_DEFAULT;
char *filename = NULL;

if( 2 == argc )
{
filename = argv[1];
}
else if( 3 == argc )
{
downloadaddr = myatoi(argv[1]);
filename = argv[2];
}
else
{
usage();
return -1;
}


if( downloadaddr == 0 || filename == NULL )
{
printf("Bad Param!!!\n");
//usage();
//return -1;
}


struct usb_dev_handle *hdev = open_port();
if(!hdev)
{
return -1;
}


unsigned int len = 0;
unsigned char* write_buf = prepare_write_buf(downloadaddr, filename, &len);
if (NULL==write_buf )
{
return 1;
}


unsigned int remain = len;
unsigned int towrite;
printf("Writing data ...\n");
while(remain)
{
#define BLOCK_SIZE 81920
towrite = remain > BLOCK_SIZE ? BLOCK_SIZE : remain;
if(towrite != usb_bulk_write(hdev, 0x02, write_buf+(len-remain), towrite, 30000))
{
perror("usb_bulk_write failed");
break;
}
remain -= towrite;
printf("\r%d%%\t %dK Bytes ", ((len-remain)*10)/(len/10), (len-remain)>>12);
fflush(stdout);
}

if( 0== remain )

printf("Done!\n");
}

if( NULL != write_buf ) 
{
free(write_buf);
}
return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章