昨天ARM11的开发板到了,需要先验证一下各项功能。以往用FS2410时用的是minicom+usb2ram,功能比windows下的dnw更强,因为minicom支持彩 {MOD}显示,不会像dnw一样出现很多乱码,这一点上超级终端做的也不错,可惜不能支持USB传输,加上我们用的是Linux嵌入式系统,平时开发都在Ubuntu下进行,当然选择minicom+usb2ram了~
不过这次出问题了,usb2ram不支持EZ6410板子的USB传输,这让我很郁闷!查看usb2ram的源代码,发现里面主要有四个地方和具体的板子相关,分别是vendorID,productID,ram_base和EndPoint地址。问题随之变为如何查看EZ6410开发板上这些参数的具体值了。ram_base很简单,使用手册里就有,0x50200000。其他三个上网搜了一下,发现用lsusb可以列出来前两个,分别为0x04e8和0x1234,至于EndPoint地址本来为0x03,我试着改为0x02就可以了~
编译通过后,可以配合minicom使用了,不过速度还是偏慢,尤其是和Windows下的DNW对比,慢得和蜗牛一样!于是我又上网搜Linux下和DNW相似的USB传输程序,还真让我发现了一个,叫做dnw2。
很简单,就一个c文件,打开看源代码,是支持QQ2440的,结构比usb2ram更加清晰简单,要改的就三个地方,vendorID,productID和ram_base,不需要EndPoint了,更确切的说是已经固定为0x02了~我把三个宏定义设置好,编译通过后试了一下,可以支持,并且速度明显比usb2ram快了很多,和Windows下的DNW没有直接对比,还不好说,不过估计也差不多了~
有了dnw2,估计以后就不需要usb2ram了,毕竟速度上差了很多,没有人会喜欢慢的东西嘛~
ps,dnw2.c的源代码放在了http://fayaa.com/code/view/11533,欢迎大家下载使用~
ps,知道为什么这个叫做dnw2了~原来以前有个dnw for linux,是写了一个usb传输的驱动,然后dnw程序将数据传入驱动进行USB的传输。比dnw2要麻烦一些,需要编译一个驱动,还要加载到内核空间,不象dnw2直接一个文件搞定!传输速度上两者应该差不多,都是以512字节的数据块进行传输,不过理论上dnw要把数据从用户空间拷入内核空间,这会影响一些速度,不过差别应该很小,几乎感觉不出来吧~
再插一句,usb2ram速度慢的原因应该就是因为数据块的设置小了,改大一些应该能改善速度~而且貌似dnw原来也不是以512字节的数据块传输的(32字节?),改了以后速度才上来的~不知道最佳的数据块大小是多少,改为1024的话能否进一步的提高速度呢?以后传输根文件系统的时候再来试试看吧~
C语言: Linux下通过USB传输文件到RAM中用的dnw2
/* dnw2 linux main file. This depends on libusb.
*
* You should use lsusb to find out the actual vender ID & product ID of board.
*
* Author: Sea.Chen <31312532@qq.com>
*
* License: GPL
*
*/
#include
#include
#include
#include
#include
#include
#define OK6410_VENDOR_ID 0x04e8
#define OK6410_PRODUCT_ID 0x1234
#define OK6410_RAM_BASE 0x50200000
/*
// FS2410
#define RAM_BASE FS2410_RAM_BASE
#define VENDOR_ID FS2410_VENDOR_ID
#define PRODUCT_ID FS2410_PRODUCT_ID
*/
// OK6410
#define RAM_BASE OK6410_RAM_BASE
#define VENDOR_ID OK6410_VENDOR_ID
#define PRODUCT_ID OK6410_PRODUCT_ID
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)
{
if( VENDOR_ID==dev->descriptor.idVendor
&& PRODUCT_ID==dev->descriptor.idProduct)
{
printf("Target usb device found!
");
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!
");
return NULL;
}
void usage()
{
printf("Usage: dnw2
");
}
unsigned char* prepare_write_buf(char *filename, unsigned int *len)
{
unsigned char *write_buf = NULL;
struct stat fs;
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
", filename);
printf("Filesize : %d bytes
", fs.st_size);
*((u_int32_t*)write_buf) = RAM_BASE; //download address
*((u_int32_t*)write_buf+1) = fs.st_size + 10; //download size;
*len = fs.st_size + 10;
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[])
{
if(2!=argc)
{
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(argv[1], &len);
if(NULL==write_buf) return 1;
unsigned int remain = len;
unsigned int towrite;
printf("Writing data ...
");
while(remain)
{
towrite = remain>512 ? 512 : remain;
if(towrite != usb_bulk_write(hdev, 0x02, write_buf+(len-remain), towrite, 3000))
{
perror("usb_bulk_write failed");
break;
}
remain-=towrite;
printf("
%d% %d bytes ", (len-remain)*100/len, len-remain);
fflush(stdout);
}
if(0==remain) printf("Done!
");
return 0;
}
/* dnw2 linux main file. This depends on libusb.
*
* Author: Fox
* License: GPL
*
*/
#include
#include
#include
#include
#include
#include
#define QQ2440_SECBULK_IDVENDOR 0x04e8
#define QQ2440_SECBULK_IDPRODUCT 0x1234
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)
{
if( QQ2440_SECBULK_IDVENDOR==dev->descriptor.idVendor
&& QQ2440_SECBULK_IDPRODUCT==dev->descriptor.idProduct)
{
printf("Target usb device found!
");
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!
");
return NULL;
}
void usage()
{
printf("Usage: dnw2
");
}
unsigned char* prepare_write_buf(char *filename, unsigned int *len)
{
unsigned char *write_buf = NULL;
struct stat fs;
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
", filename);
printf("Filesize : %d bytes
", fs.st_size);
*((u_int32_t*)write_buf) = 0x30000000; //download address
*((u_int32_t*)write_buf+1) = fs.st_size + 10; //download size;
*len = fs.st_size + 10;
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[])
{
if(2!=argc)
{
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(argv[1], &len);
if(NULL==write_buf) return 1;
unsigned int remain = len;
unsigned int towrite;
printf("Writing data ...
");
while(remain)
{
towrite = remain>1024 ? 1024 : remain;
if(towrite != usb_bulk_write(hdev, 0x02, write_buf+(len-remain), towrite, 3000))
{
perror("usb_bulk_write failed");
break;
}
remain-=towrite;
printf("
%d% %d bytes ", (len-remain)*100/len, len-remain);
fflush(stdout);
}
if(0==remain) printf("Done!
");
return 0;
}