Linux下DNW源码及安装

内核编译和嵌入式产品的设计与开发
回复
头像
田春龙
帖子: 46
注册时间: 2009-08-12 0:39

Linux下DNW源码及安装

#1

帖子 田春龙 » 2010-04-06 9:16

转载自:http://www.91linux.com/html/linux_pub/u ... 19048.html

环境:ubuntu9.10

1),首先要安装libusb-dev这个库。我是在ubuntu下做的。
那么就:sudo apt-get install libusb-dev
装完之后就编译一个下载工具,网上有个牛人提供了一个。代码如下:


CODE:
/* dnw2 linux main file. This depends on libusb.
*
* Author: Fox <hulifox008@163.com>
* License: GPL
*
*/
#include <stdio.h>
#include <usb.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define QQ2440_SECBULK_IDVENDOR 0x5345
#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)
{
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("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) = 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 ...\n");
while(remain)
{
towrite = remain>512 ? 512 : remain;
if(towrite != usb_bulk_write(hdev, 0x03, 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;
}

2),把它保存为文件如:dnw2.c
接着编译: gcc dnw2.c -o dnw2 -lusb
编译完得到的dnw2就是usb下载的PC端了。
下载时用:dnw2 <filename>下载你的文件到板上。速度还不错哦。
干脆再生成的链接文件sudo ln -s ./dnw2 /usr/sbin/dnw2
这样在我们每编译完要下载的文件就可以直接下载了。
头像
shenhao0129
帖子: 192
注册时间: 2007-11-23 12:31

Re: Linux下DNW源码及安装

#2

帖子 shenhao0129 » 2010-05-03 12:23

恩,这个东西不错,我喜欢,mark一下,等下去试试
liueg
帖子: 18
注册时间: 2008-12-08 10:50

Re: Linux下DNW源码及安装

#3

帖子 liueg » 2010-09-14 16:13

谢谢楼主,不知道我的天嵌的板子可以完成不,回去实践一下
天使ッ翼
帖子: 679
注册时间: 2010-06-19 11:44

Re: Linux下DNW源码及安装

#4

帖子 天使ッ翼 » 2010-09-15 10:59

mark
jaspertsin
帖子: 3
注册时间: 2009-04-26 21:03

Re: Linux下DNW源码及安装

#5

帖子 jaspertsin » 2010-10-04 17:16

mark
头像
zhousm
帖子: 122
注册时间: 2010-03-12 13:20
系统: ubuntu 14.04 LTS
来自: 湘潭

Re: Linux下DNW源码及安装

#6

帖子 zhousm » 2010-10-14 22:05

行吗,我的是friendly arm 的mini2440的板,可以用吗?
ubuntu 14.04 LTS
头像
tonghuix
帖子: 355
注册时间: 2010-10-08 0:55

Re: Linux下DNW源码及安装

#7

帖子 tonghuix » 2010-11-22 22:09

楼上的,可以用,完全正常!!
/***
* @tonghuix 佟辉 Tong Hui
* @brief 启智开源 编码自由
* @brief Open Source Open Mind, Coding for free!
* @website: https://tonghuix.io
* @weibo http://weibo.com/234826309
* @Twitter http://twitter.com/tonghuix
* @G+ http://gplus.to/tonghuix
*/
yyxl
帖子: 29
注册时间: 2007-12-17 15:14

Re: Linux下DNW源码及安装

#8

帖子 yyxl » 2010-12-04 9:46

恩,这个东西不错,我喜欢,mark一下,等下去试试
使者0123
帖子: 1
注册时间: 2011-12-27 12:33

Re: Linux下DNW源码及安装

#9

帖子 使者0123 » 2011-12-27 12:36

这么好的东西,收藏了 ,开源精神,值得敬佩!!
hayvane
帖子: 5
注册时间: 2011-12-22 13:42

Re: Linux下DNW源码及安装

#10

帖子 hayvane » 2011-12-27 21:25

我编译DNW 出现错误!dnw.c: 在函数‘prepare_write_buf’中:
dnw.c:96:2: 警告: 格式 ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘__off_t’ [-Wformat]
dnw.c: 在函数‘main’中:
dnw.c:142:3: 警告: 格式字符串中出现无效的类型转换字符 0x9 [-Wformat]
/tmp/ccamvDEs.o: In function `open_port':
dnw.c:(.text+0x7): undefined reference to `usb_init'
dnw.c:(.text+0xc): undefined reference to `usb_find_busses'
dnw.c:(.text+0x11): undefined reference to `usb_find_devices'
dnw.c:(.text+0x16): undefined reference to `usb_get_busses'
dnw.c:(.text+0x9b): undefined reference to `usb_open'
dnw.c:(.text+0xc5): undefined reference to `usb_claim_interface'
dnw.c:(.text+0xe0): undefined reference to `usb_close'
/tmp/ccamvDEs.o: In function `main':
dnw.c:(.text+0x34e): undefined reference to `usb_bulk_write'
collect2: ld 返回 1
咋回事
zzxxer
帖子: 21
注册时间: 2011-02-28 17:41

Re: Linux下DNW源码及安装

#11

帖子 zzxxer » 2012-03-26 14:11

好帖!
ahf814035850
帖子: 2
注册时间: 2009-05-18 23:58

Re: Linux下DNW源码及安装

#12

帖子 ahf814035850 » 2012-05-04 19:43

mark
jizai888
帖子: 39
注册时间: 2012-08-06 15:41
来自: 内蒙古 包头市 昆都仑区

Re: Linux下DNW源码及安装

#13

帖子 jizai888 » 2012-08-10 14:58

编译出现这个错误 为什么 /usr/include/libio.h:334:3: 错误: 未知的类型名‘size_t’
linux新手
jizai888
帖子: 39
注册时间: 2012-08-06 15:41
来自: 内蒙古 包头市 昆都仑区

Re: Linux下DNW源码及安装

#14

帖子 jizai888 » 2012-08-10 15:01

难道我的环境 12.04不行?
linux新手
shiv_zhu
帖子: 4
注册时间: 2012-09-27 9:13
系统: Ubuntu 10.10

Re: Linux下DNW源码及安装

#15

帖子 shiv_zhu » 2012-09-27 9:33

jizai888 写了:编译出现这个错误 为什么 /usr/include/libio.h:334:3: 错误: 未知的类型名‘size_t’
size_t还有一个是包含在sys/types.h文件里面 试一下加一行#include<sys/types.h>
回复