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;
}




你可能感兴趣的:(linux,struct,File,null,download,interface)