USB file transfer for dnw2 in Ubuntu

Source: Internet
Author: User
After twists and turns, we finally see the steps below for the program to be downloaded to the target board: first install the libusb-dev library. I did it in Ubuntu. Then: After sudoapt-getinstalllibusb-dev is installed, a download tool will be compiled and provided by a cool online user. The Code is as follows:/* dnw2linuxmainfile. Thisdependsonlibusb. ** Author: Fo

After several twists and turns, I finally saw that the program was downloaded to the target board. The specific steps are as follows:

), First install the libusb-dev library. I did it in Ubuntu.

Then: sudo apt-get install libusb-dev

Compile a download tool after installation, and a cool man provides one online. The Code is as follows:

/* Dnw2 linux main file. This depends on libusb.
*
* Author: Fox
* License: GPL
*
*/
# Include
# Include
# Include
# Include
# Include
# Include

# Define qq2440_secbulk_idvender 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-> next)
{
Struct usb_device * dev;
For (dev = bus-> devices; dev = dev-> next)
{
Printf ("idVendor: 0x % x \ t, ipProduct: 0x % x \ n", dev-> descriptor. idVendor, dev-> descriptor. idProduct );
If (QQ2440_SECBULK_IDVENDOR = dev-> descriptor. idVendor
& QQ2440_SECBULK_IDPRODUCT = 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 (0! = Usb_claim_interface (hdev, 0 ))
{
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 ");
}

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 \ n", filename );
Printf ("Filesize: % d bytes \ n", fs. st_size );

* (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... \ n ");
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 ("\ r % d % \ t % d bytes", (len-remain) * 100/len, len-remain );
Fflush (stdout );
}
If (0 = remain) printf ("Done! \ N ");
Return 0;
}

2), save it as a file such as: dnw2.c
Then compile: gcc dnw2.c-o dnw2-lusb


3) Move the dnw2 executable file to/usr/bin.

Sudo mv./dnw2/usr/bin

4)

Sudo dnw2 path/your_filename

Simply generate the link file sudo ln-s./dnw2/usr/dnw2
In this way, we can directly download the file to be downloaded after each compilation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.