记录一下解决EZ6410的板子在Linux下用USB来传输文件问题的过程~
前两天ARM11的开发板到了,需要先验证一下各项功能。以往用FS2410时用的是minicom+usb2ram,功能比windows下的dnw更强,因为minicom支持彩色显示,不会像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的源代码
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: Fox <hulifox008@163.com>
* Ace Strong <acestrong@gmail.com>
* License: GPL
*
*/
#include <stdio.h>
#include <usb.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define QQ2440_VENDOR_ID 0x5345
#define QQ2440_PRODUCT_ID 0x1234
#define FS2410_VENDOR_ID 0x5345
#define FS2410_PRODUCT_ID 0x1234
#define EZ6410_VENDOR_ID 0x04e8
#define EZ6410_PRODUCT_ID 0x1234
#define EZ6410_RAM_BASE 0x50200000
#define FS2410_RAM_BASE 0x30200000
/*
// FS2410
#define RAM_BASE FS2410_RAM_BASE
#define VENDOR_ID FS2410_VENDOR_ID
#define PRODUCT_ID FS2410_PRODUCT_ID
*/
// EZ6410
#define RAM_BASE EZ6410_RAM_BASE
#define VENDOR_ID EZ6410_VENDOR_ID
#define PRODUCT_ID EZ6410_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!\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("Usage: dnw2 <file>\n\n");
}
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\n", filename);
printf("Filesize : %d bytes\n", 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 \n");
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("\r%d%\t %d bytes ", (len-remain)*100/len, len-remain);
fflush(stdout);
}
if(0==remain) printf("Done!\n");
return 0;
}
posted on 2015-05-03 23:49
聂文龙 阅读(1032)
评论(0) 编辑 收藏 引用