Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2150602
  • 博文数量: 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

2012-03-01 16:14:44

TQ2440使用0---linux下用dnw进行烧写
dnw.c
  1. /* dnw2 linux main file. This depends on libusb.
  2.  *
  3.  * Author: Fox <hulifox008@163.com>
  4.  * License: GPL
  5.  *
  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. #define QQ2440_SECBULK_IDVENDOR        0x5345
  14. #define QQ2440_SECBULK_IDPRODUCT    0x1234

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

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

  44.     return NULL;
  45. }

  46. void usage() {
  47.     printf("Usage: dnw2 \n\n");
  48. }

  49. unsigned char* prepare_write_buf(char *filename, unsigned int *len) {
  50.     unsigned char *write_buf = NULL;
  51.     struct stat fs;

  52.     int fd = open(filename, O_RDONLY);
  53.     if (-1 == fd) {
  54.         perror("Cannot open file");
  55.         return NULL;
  56.     }
  57.     if (-1 == fstat(fd, &fs)) {
  58.         perror("Cannot get file size");
  59.         goto error;
  60.     }
  61.     write_buf = (unsigned char*) malloc(fs.st_size + 10);
  62.     if (NULL == write_buf) {
  63.         perror("malloc failed");
  64.         goto error;
  65.     }

  66.     if (fs.st_size != read(fd, write_buf + 8, fs.st_size)) {
  67.         perror("Reading file failed");
  68.         goto error;
  69.     }

  70.     printf("Filename : %s\n", filename);
  71.     printf("Filesize : %d bytes\n", (int)fs.st_size);

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

  74.     *len = fs.st_size + 10;
  75.     return write_buf;

  76. error: if (fd != -1)
  77.            close(fd);
  78.        if (NULL != write_buf)
  79.            free(write_buf);
  80.        fs.st_size = 0;
  81.        return NULL;

  82. }

  83. int main(int argc, char *argv[]) {
  84.     if (2 != argc) {
  85.         usage();
  86.         return 1;
  87.     }

  88.     struct usb_dev_handle *hdev = open_port();
  89.     if (!hdev) {
  90.         return 1;
  91.     }

  92.     unsigned int len = 0;
  93.     unsigned char* write_buf = prepare_write_buf(argv[1], &len);
  94.     if (NULL == write_buf)
  95.         return 1;

  96.     unsigned int remain = len;
  97.     unsigned int towrite;
  98.     printf("Writing data ...\n");
  99.     while (remain) {
  100.         towrite = remain > 512 ? 512 : remain;
  101.         if (towrite != usb_bulk_write(hdev, 0x03, write_buf + (len - remain),
  102.                     towrite, 3000)) {
  103.             perror("usb_bulk_write failed");
  104.             break;
  105.         }
  106.         remain -= towrite;
  107.         printf("\r%d\t %d bytes ", (len-remain)*100/len, len-remain);
  108.         fflush(stdout);
  109.     }
  110.     if (0 == remain)
  111.         printf("Done!\n");
  112.     return 0;
  113. }
[转]
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
阅读(1541) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~