• ubuntu12.04 dnw2 fl2440 配置


    1、安装libusb-dev

    sudo apt-get install libusb-dev

    2、dnw2编译配置

    源码如下,将其保存为dnw2.c

    编译命令 gcc dnw2.c -o dnw2 -lusb。

    不同的硬件平台,修改以下三项:

    QQ2440_SECBULK_IDVENDOR,QQ2440_SECBULK_IDPRODUCT,*((u_int32_t*)write_buf) = 0x30800000; //download address
    /* 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	,ipProduct:0x%x
    ",dev->descriptor.idVendor,dev->descriptor.idProduct);
                            if( QQ2440_SECBULK_IDVENDOR==dev->descriptor.idVendor
                            &&  QQ2440_SECBULK_IDPRODUCT==dev->descriptor.idProduct)
                            {
                                    printf("Target usb device found!
    ");
                                    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!
    ");
    
            return NULL;
    }
    
    void usage()
    {
            printf("Usage: dnw2 <file>
    
    ");
    }
    
    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
    ", filename);
            printf("Filesize : %d bytes
    ", fs.st_size);
    
            *((u_int32_t*)write_buf) = 0x30800000;                //download address
            //*((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 ...
    ");
            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("
    %d%	 %d bytes     ", (len-remain)*100/len, len-remain);
                    fflush(stdout);
            }
            if(0==remain) printf("Done!
    ");
            return 0;
    }
    

      

  • 相关阅读:
    断开Oracle连接用户
    【转】Windows英文操作系统下ArcGIS 显示和保存中文数据
    断开Oracle连接用户
    如何判断标准输入或输出是否经过了重定向,即是否在命令行上使用了“”?
    PHP MYSQL网站注入扫描
    在PHPLIB中的MYSQL类中加INSERT,UPDATE,DELETE等函数
    为Linux安置红旗紫光输出法
    Linux下Resin JSP MySQL的摆设和设置配备摆设2
    红旗Linux桌面4.1文本安顿过程图解(一)
    MYSQL数据迁移tips,ORA00907: missing right parenthesis
  • 原文地址:https://www.cnblogs.com/larkin-cn/p/4376290.html
Copyright © 2020-2023  润新知