Chinaunix首页 | 论坛 | 博客
  • 博客访问: 20087
  • 博文数量: 8
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 50
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-23 14:34
文章分类
文章存档

2012年(8)

我的朋友
最近访客

分类:

2012-12-01 22:31:26

原文地址:Linux下DNW源码及安装 作者:txgc_wm

1.首先要安装libusb-dev这个库,执行“sudo apt-get install libusb-dev”,装完之后就编译一个下载工具,网上有个牛人提供了一个。代码如下:

CODE:
  1. /*
  2. * dnw.c linux main file. This depends on libusb.
  3. * Author: Fox <hulifox008@163.com>
  4. * License: GPL
  5. */
  6. #include <stdio.h>
  7. #include <usb.h>
  8. #include <errno.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include <unistd.h>

  12. #define QQ2440_SECBULK_IDVENDOR 0x5345
  13. #define QQ2440_SECBULK_IDPRODUCT 0x1234


  14. struct usb_dev_handle * open_port()
  15. {
  16.     struct usb_bus *busses, *bus;

  17.     usb_init();
  18.     usb_find_busses();
  19.     usb_find_devices();

  20.     busses = usb_get_busses();
  21.     for(bus=busses;bus;bus=bus->next)
  22.     {
  23.         struct usb_device *dev;
  24.         for(dev=bus->devices;dev;dev=dev->next)
  25.         {
  26.             printf("idVendor:0x%x\t,ipProduct:0x%x\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
  27.             if( QQ2440_SECBULK_IDVENDOR==dev->descriptor.idVendor&& QQ2440_SECBULK_IDPRODUCT==dev->descriptor.idProduct)
  28.             {
  29.                 printf("Target usb device found!\n");
  30.                 struct usb_dev_handle *hdev = usb_open(dev);
  31.                 if(!hdev)
  32.                 {
  33.                     perror("Cannot open device");
  34.                 }
  35.                 else
  36.                 {
  37.                     if(0!=usb_claim_interface(hdev, 0))
  38.                     {
  39.                         perror("Cannot claim interface");
  40.                         usb_close(hdev);
  41.                         hdev = NULL;
  42.                     }
  43.                 }
  44.                 return hdev;
  45.             }
  46.         }
  47.     }

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

  49.     return NULL;
  50. }

  51. void usage(void)
  52. {
  53.     printf("Usage: dnw \n\n");
  54. }

  55. unsigned char* prepare_write_buf(char *filename, unsigned int *len)
  56. {
  57.     unsigned char *write_buf = NULL;
  58.     struct stat fs;

  59.     int fd = open(filename, O_RDONLY);
  60.     if(-1==fd)
  61.     {
  62.         perror("Cannot open file");
  63.         return NULL;
  64.     }
  65.     if(-1==fstat(fd, &fs))
  66.     {
  67.         perror("Cannot get file size");
  68.         goto error;
  69.     }
  70.     write_buf = (unsigned char*)malloc(fs.st_size+10);
  71.     if(NULL==write_buf)
  72.     {
  73.         perror("malloc failed");
  74.         goto error;
  75.     }

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

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

  83.     *((u_int32_t*)write_buf) = 0x30000000; //download address
  84.     *((u_int32_t*)write_buf+1) = fs.st_size + 10; //download size;

  85.     *len = fs.st_size + 10;
  86.     return write_buf;

  87. error:
  88.     if(fd!=-1)
  89.         close(fd);
  90.     if(NULL!=write_buf)
  91.         free(write_buf);
  92.     fs.st_size = 0;
  93.     return NULL;
  94. }

  95. int main(int argc, char *argv[])
  96. {
  97.     if(2!=argc)
  98.     {
  99.         usage();
  100.         return 1;
  101.     }

  102.     struct usb_dev_handle *hdev = open_port();
  103.     if(!hdev)
  104.     {
  105.         return 1;
  106.     }

  107.     unsigned int len = 0;
  108.     unsigned char* write_buf = prepare_write_buf(argv[1], &len);
  109.     if(NULL==write_buf)
  110.         return 1;

  111.     unsigned int remain = len;
  112.     unsigned int towrite;
  113.     printf("Writing data ...\n");
  114.     while(remain)
  115.     {
  116.         towrite = remain>512 ? 512 : remain;
  117.         if(towrite != usb_bulk_write(hdev, 0x03, write_buf+(len-remain), towrite, 3000))
  118.         {
  119.             perror("usb_bulk_write failed");
  120.             break;
  121.         }
  122.         remain-=towrite;
  123.         printf("\r%d%\t %d bytes ", (len-remain)*100/len, len-remain);
  124.         fflush(stdout);
  125.     }
  126.     if(0==remain)
  127.         printf("Done!\n");
  128.     return 0;
  129. }

2.把它保存为文件dnw.c,编译“gcc dnw2.c -o dnw -lusb”,编译完得到的dnw就是usb下载的PC端了。下载时用“dnw ”下载文件到板上。也可生成的链接文件“sudo ln -s ./dnw /usr/sbin/dnw”,这样在编译完要下载文件就可以直接下载了。
阅读(922) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~