DNW for Linux(Tiny 6410)


最近搞了块友善的6410开发板耍,买来后,悲剧的是,送的光碟本本居然打不开。搞的一个多月没玩什么。

终于,将资料以各种方法弄出来了,刚好也有时间研究研究。

一直喜欢linux,所以,各种嵌入式开发的工具必须搞齐(minicom,kermit,dnw)。

上面这些都是放狗才知道,要不然真不晓得咋办。

好了,开始正题了。这里给有需要的朋友介绍下dnw(如果您不知道dnw是干嘛的话,请百度)。假如是在win下,dnw现成的,啥也不需要搞。

linux下就必须自己编译了。

  1. /* dnw2 linux main file. This depends on libusb.  
  2. *  
  3. * You should use lsusb to find out the actual vender ID & product ID of board.  
  4. *  
  5. * Author:     Fox <hulifox008@163.com>  
  6. *             Ace Strong <acestrong@gmail.com>  
  7. * License:    GPL  
  8. *  
  9. */  
  10. #include <stdio.h>   
  11. #include <usb.h>   
  12. #include <errno.h>   
  13. #include <sys/stat.h>   
  14. #include <fcntl.h>   
  15. #include <unistd.h>   
  16. #define QQ2440_VENDOR_ID        0x5345   
  17. #define QQ2440_PRODUCT_ID       0x1234   
  18. #define FS2410_VENDOR_ID        0x5345   
  19. #define FS2410_PRODUCT_ID       0x1234   
  20. #define EZ6410_VENDOR_ID        0x5345   
  21. #define EZ6410_PRODUCT_ID       0x1234   
  22. #define EZ6410_RAM_BASE         0x50200000   
  23. #define FS2410_RAM_BASE         0x30200000   
  24. /*  
  25. // FS2410  
  26. #define RAM_BASE            FS2410_RAM_BASE  
  27. #define VENDOR_ID           FS2410_VENDOR_ID  
  28. #define PRODUCT_ID          FS2410_PRODUCT_ID  
  29. */  
  30. // EZ6410   
  31. #define RAM_BASE            EZ6410_RAM_BASE   
  32. #define VENDOR_ID           EZ6410_VENDOR_ID   
  33. #define PRODUCT_ID          EZ6410_PRODUCT_ID   
  34. struct usb_dev_handle * open_port()   
  35. {   
  36.     struct usb_bus *busses, *bus;   
  37.     usb_init();   
  38.     usb_find_busses();   
  39.     usb_find_devices();   
  40.     busses = usb_get_busses();   
  41.     for(bus=busses;bus;bus=bus->next)   
  42.     {   
  43.         struct usb_device *dev;   
  44.         for(dev=bus->devices;dev;dev=dev->next)   
  45.         {   
  46.             if( VENDOR_ID==dev->descriptor.idVendor   
  47.             &&  PRODUCT_ID==dev->descriptor.idProduct)   
  48.             {   
  49.                 printf("Target usb device found!\n");   
  50.                 struct usb_dev_handle *hdev = usb_open(dev);   
  51.                 if(!hdev)   
  52.                 {   
  53.                     perror("Cannot open device");      
  54.                 }   
  55.                 else  
  56.                 {   
  57.                     if(0!=usb_claim_interface(hdev, 0))   
  58.                     {   
  59.                         perror("Cannot claim interface");   
  60.                         usb_close(hdev);   
  61.                         hdev = NULL;   
  62.                     }   
  63.                 }   
  64.                 return hdev;   
  65.             }   
  66.         }   
  67.     }   
  68.       
  69.     printf("Target usb device not found!\n");   
  70.     return NULL;   
  71. }   
  72. void usage()   
  73. {   
  74.     printf("Usage: dnw2 <file>\n\n");   
  75. }   
  76. static u_int16_t ace_csum(const unsigned char *data, u_int32_t len)   
  77. {   
  78.         u_int16_t csum = 0;   
  79.         int j;   
  80.         for (j = 0; j < len; j ++) {   
  81.                 csum += data[j];   
  82.         }   
  83.         return csum;   
  84. }   
  85. unsigned char* prepare_write_buf(char *filename, unsigned int *len)   
  86. {   
  87.     unsigned char *write_buf = NULL;   
  88.     struct stat fs;   
  89.     int fd = open(filename, O_RDONLY);   
  90.     if(-1==fd)   
  91.     {   
  92.         perror("Cannot open file");   
  93.         return NULL;   
  94.     }   
  95.     if(-1==fstat(fd, &fs))   
  96.     {   
  97.         perror("Cannot get file size");   
  98.         goto error;   
  99.     }   
  100.     write_buf = (unsigned char*)malloc(fs.st_size+10);   
  101.     if(NULL==write_buf)   
  102.     {   
  103.         perror("malloc failed");   
  104.         goto error;   
  105.     }   
  106.     if(fs.st_size != read(fd, write_buf+8, fs.st_size))   
  107.     {   
  108.         perror("Reading file failed");   
  109.         goto error;   
  110.     }   
  111.     printf("Filename : %s\n", filename);   
  112.     printf("Filesize : %lu bytes\n", fs.st_size);   
  113.     *((u_int32_t*)write_buf) = RAM_BASE;        //download address   
  114.     *((u_int32_t*)write_buf+1) = fs.st_size + 10;    //download size;   
  115.     *len = fs.st_size + 10;   
  116.        
  117.     // calculate checksum value   
  118.     u_int16_t csum = ace_csum(write_buf+8, fs.st_size);   
  119.     *(write_buf+fs.st_size+8) = csum & 0xff;   
  120.     *(write_buf+fs.st_size+9) = (csum >> 8) & 0xff;   
  121.     return write_buf;   
  122. error:   
  123.     if(fd!=-1) close(fd);   
  124.     if(NULL!=write_buf) free(write_buf);   
  125.     fs.st_size = 0;   
  126.     return NULL;   
  127.       
  128. }   
  129. int main(int argc, char *argv[])   
  130. {   
  131.     if(2!=argc)   
  132.     {   
  133.         usage();   
  134.         return 1;   
  135.     }   
  136.     struct usb_dev_handle *hdev = open_port();   
  137.     if(!hdev)   
  138.     {   
  139.         return 1;   
  140.     }   
  141.     unsigned int len = 0;   
  142.     unsigned char* write_buf = prepare_write_buf(argv[1], &len);   
  143.     if(NULL==write_buf) return 1;   
  144.     unsigned int remain = len;   
  145.     unsigned int towrite;   
  146.     printf("Writing data ...\n");   
  147.     while(remain)   
  148.     {   
  149.         towrite = remain>512 ? 512 : remain;   
  150.         if(towrite != usb_bulk_write(hdev, 0x02, write_buf+(len-remain), towrite, 3000))   
  151.         {   
  152.             perror("usb_bulk_write failed");   
  153.             break;   
  154.         }   
  155.         remain-=towrite;   
  156.         printf("\r%d%%\t %d bytes     ", (len-remain)*100/len, len-remain);   
  157.         fflush(stdout);   
  158.     }   
  159.     if(0==remain) printf("Done!\n");   
  160.     return 0;   
  161. }  
  • 1
  • 2
  • 下一页

相关内容