Linux下DNW源码及安装
阅读原文时间:2023年07月11日阅读:1

平台:

Ubuntu12.04 + 64bit

tiny4412ADK

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

dnw.c

/* dnw2 linux main file. This depends on libusb.
* *
* * Author: Fox hulifox008@163.com
* * License: GPL
* *
* */
#include
#include
#include
#include
#include
#include

#define TINY4412_SECBULK_IDVENDOR 0x04e8
#define TINY4412_SECBULK_IDPRODUCT 0x1234

#define TINY4412_RAM_BASE 0xc0000000

// TINY4412
#define RAM_BASE TINY4412_RAM_BASE
#define VENDOR_ID TINY4412_SECBULK_IDVENDOR
#define PRODUCT_ID TINY4412_SECBULK_IDPRODUCT

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)  
    {  
        if( VENDOR\_ID==dev->descriptor.idVendor  
        &&  PRODUCT\_ID==dev->descriptor.idProduct)  
        {  
            printf("Target usb device found!\\n");  
            struct usb\_dev\_handle \*hdev = usb\_open(dev);  
            if(!hdev)  
            {  
                perror("Cannot open device");  
            }  
            else  
            {  
                if(!=usb\_claim\_interface(hdev, ))  
                {  
                    perror("Cannot claim interface");  
                    usb\_close(hdev);  
                    hdev = NULL;  
                }  
            }  
            return hdev;  
        }  
    }  
}

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

return NULL;  

}

void usage()
{
printf("Usage: dnw2 \n\n");
}

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(-==fd)  
{  
    perror("Cannot open file");  
    return NULL;  
}  
if(-==fstat(fd, &fs))  
{  
    perror("Cannot get file size");  
    goto error;  
}  
write\_buf = (unsigned char\*)malloc(fs.st\_size+);  
if(NULL==write\_buf)  
{  
    perror("malloc failed");  
    goto error;  
}

if(fs.st\_size != read(fd, write\_buf+, fs.st\_size))  
{  
    perror("Reading file failed");  
    goto error;  
}  
    unsigned short sum = ;  
    int i;  
    for(i=; i<fs.st\_size+; i++)  
    {  
            sum += write\_buf\[i\];  
    }  
printf("Filename : %s\\n", filename);  
printf("Filesize : %d bytes\\n", (int)fs.st\_size);  
printf ("Sum is %x\\n",sum);

\*((u\_int32\_t\*)write\_buf) = RAM\_BASE;        //download address  
\*((u\_int32\_t\*)write\_buf+) = fs.st\_size + ;    //download size;  
write\_buf \[fs.st\_size + \] = sum & 0xff;  
write\_buf \[fs.st\_size + \] = sum >> ;  
\*len = fs.st\_size + ;  
return write\_buf;

error:
if(fd!=-) close(fd);
if(NULL!=write_buf) free(write_buf);
fs.st_size = ;
return NULL;

}

int main(int argc, char *argv[])
{
if(!=argc)
{
usage();
return ;
}

struct usb\_dev\_handle \*hdev = open\_port();  
if(!hdev)  
{  
    return ;  
}

unsigned int len = ;  
unsigned char\* write\_buf = prepare\_write\_buf(argv\[\], &len);  
if(NULL==write\_buf) return ;

unsigned int remain = len;  
unsigned int towrite;  
printf("Writing data ...\\n");  
while(remain)  
{  
    towrite = remain> ?  : remain;  
    if(towrite != usb\_bulk\_write(hdev, 0x02, write\_buf+(len-remain), towrite, ))  
    {  
        perror("usb\_bulk\_write failed");  
        break;  
    }  
    remain-=towrite;  
    printf("\\r %d \\t %d bytes     ", (len-remain)\*/len, len-remain);  
    fflush(stdout);  
}  
if(==remain) printf("Done!\\n");  
return ;  

}

Makefile

#******************************************************************************
#*
#* Copyright (C) - Broadcom Corporation
#*
#* Licensed under the Apache License, Version 2.0 (the "License");
#* you may not use this file except in compliance with the License.
#* You may obtain a copy of the License at
#*
#* http://www.apache.org/licenses/LICENSE-2.0
#*
#* Unless required by applicable law or agreed to in writing, software
#* distributed under the License is distributed on an "AS IS" BASIS,
#* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#* See the License for the specific language governing permissions and
#* limitations under the License.
#*
#******************************************************************************

LDLIBS = -lusb
CFLAGS=-g

dnw: dnw.o

2. 把它保存为文件dnw.c,编译“gcc dnw2.c -o dnw -lusb”,编译完得到的dnw就是usb下载的PC端了。下载时用“dnw ”下载文件到板上。也可生成的链接文件“sudo ln -s ./dnw /usr/sbin/dnw”,这样在编译完要下载文件就可以直接下载了。