Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2150830
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: LINUX

2013-06-05 15:15:18

以前在tq2440上有linux下的dnw,不用装驱动一个ELF文件就可以执行下载.
把以前的代码拿过来修改了一下,发现还是不错的,呵呵,这下烧的时候不用插拔SD卡了! (附件中有编译好的dnw)
dusb.c
  1. /* dnw2 linux main file. This depends on libusb.
  2.  *
  3.  * Author: Fox <hulifox008@163.com>
  4.  * License: GPL
  5.  * dusb.c
  6.  */

  7. #include <stdio.h>
  8. #include <usb.h>
  9. #include <errno.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. #include <unistd.h>
  13. #if 1
  14. #define QQ2440_SECBULK_IDVENDOR        0x04e8
  15. #define QQ2440_SECBULK_IDPRODUCT    0x1234
  16. #else
  17. #define QQ2440_SECBULK_IDVENDOR        0x1234
  18. #define QQ2440_SECBULK_IDPRODUCT    0x04e8
  19. #endif
  20. struct usb_dev_handle * open_port() {
  21.     struct usb_bus *busses, *bus;

  22.     usb_init();
  23.     usb_find_busses();
  24.     usb_find_devices();

  25.     busses = usb_get_busses();
  26.     for (bus = busses; bus; bus = bus->next) {
  27.         struct usb_device *dev;
  28.         for (dev = bus->devices; dev; dev = dev->next) {
  29.             printf("idVendor:0x%xt,ipProduct:0x%xn",
  30.                     dev->descriptor.idVendor, dev->descriptor.idProduct);
  31.             if (QQ2440_SECBULK_IDVENDOR == dev->descriptor.idVendor
  32.                     && QQ2440_SECBULK_IDPRODUCT == dev->descriptor.idProduct) {
  33.                 printf("Target usb device found!n");
  34.                 struct usb_dev_handle *hdev = usb_open(dev);
  35.                 if (!hdev) {
  36.                     perror("Cannot open device");
  37.                 } else {
  38.                     if (0 != usb_claim_interface(hdev, 0)) {
  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() {
  52.     printf("Usage: dnw2 nn");
  53. }

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

  57.     int fd = open(filename, O_RDONLY);
  58.     if (-1 == fd) {
  59.         perror("Cannot open file");
  60.         return NULL;
  61.     }
  62.     if (-1 == fstat(fd, &fs)) {
  63.         perror("Cannot get file size");
  64.         goto error;
  65.     }
  66.     write_buf = (unsigned char*) malloc(fs.st_size + 10);
  67.     if (NULL == write_buf) {
  68.         perror("malloc failed");
  69.         goto error;
  70.     }
  71.     if (fs.st_size != read(fd, write_buf+8, fs.st_size)) {
  72.         perror("Reading file failed");
  73.         goto error;
  74.     }
  75.     printf("Filename : %sn", filename);
  76.     printf("Filesize : 0x%x bytesn", (int)fs.st_size);

  77.     *((u_int32_t*) write_buf) = 0x30000000;              //download address:这个值写啥无所谓
  78.     *((u_int32_t*) write_buf + 1) = fs.st_size + 10;     //download size;

  79.     //USB format : addr(4)+size(4)+data(n)+cs(2)
  80.      // 主要是添加了下面这部分内容: 生成checksum值,并写到write_buf中
  81.     //如果重新编译的u-boot去掉了 #ifdef USB_CHECKSUM_EN,那么下面这个可以不加
  82.     int i;
  83.     unsigned short checkSum = 0;

  84.     unsigned char* cs_start = write_buf+8;
  85.     unsigned char* cs_end = write_buf+8+fs.st_size;
  86.     while(cs_start < cs_end) {
  87.         checkSum += *cs_start++;
  88.     }
  89.     printf("checkSum=0x%xn", checkSum);

  90.     *(unsigned short*)(write_buf+fs.st_size+8) = checkSum;

  91.     *len = fs.st_size + 10;
  92.     return write_buf;

  93.     error: if (fd != -1)
  94.         close(fd);
  95.     if (NULL != write_buf)
  96.         free(write_buf);
  97.     fs.st_size = 0;
  98.     return NULL;

  99. }

  100. int main(int argc, char *argv[]) {
  101.     if (2 != argc) {
  102.         usage();
  103.         return 1;
  104.     }

  105.     struct usb_dev_handle *hdev = open_port();
  106.     if (!hdev) {
  107.         return 1;
  108.     }

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

  113.     unsigned int remain = len;
  114.     unsigned int towrite;
  115.     printf("Writing data ...n");
  116.     while (remain) {
  117.         towrite = remain > 512 ? 512 : remain;
  118.         //端点与TQ2440不一样是个二
  119.         //if (towrite != usb_bulk_write(hdev, 0x03, write_buf + (len - remain), towrite, 3000)) {
  120.         if (towrite != usb_bulk_write(hdev, 0x02, write_buf + (len - remain), towrite, 3000)) {
  121.             perror("usb_bulk_write failed");
  122.             break;
  123.         }
  124.         remain -= towrite;
  125.         printf("r%dt %d bytes ", (len-remain)*100/len, len-remain);
  126.         fflush(stdout);
  127.     }
  128.     if (0 == remain)
  129.         printf("Done!n");
  130.     return 0;
  131. }

Makefile:
  1. dusb:dusb.c
  2.     gcc -g -o $@ $< -lusb
  3. clean:
  4.     rm -rf ./*.o dusb
如果提示找不到头文件,
fatal error: usb.h: No such file or directory
这可能是libusb库没有安装

sudo apt-get install libusb-dev

附件是编译好的dnw与源码
dusb_ok6410.rar
阅读(1932) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~