Ubuntu 12.04 system environment uses USB to download the program to the tq2440 Development Board

Source: Internet
Author: User

This program is used with minicom .....

Note that this method is successfully tested on the tq2440 Development Board. For other development boards, find the corresponding code on the King Road !!!

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: sudoapt-Get install libusb-Dev

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


# Include <stdio. h>
# Include <USB. h>
# Include <errno. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
# Include <unistd. h>

# Defineqq2440_secbulk_idvendor 0x5345
# Define qq2440_secbulk_idproduct0x1234


Structusb_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)
{
Structusb_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 ("targetusb device found! /N ");
Struct usb_dev_handle * hdev = usb_open (Dev );
If (! Hdev)
{
Perror ("cannot opendevice ");
}
Else
{
If (0! = Usb_claim_interface (hdev, 0 ))
{
Perror ("cannot claiminterface ");
Usb_close (hdev );
Hdev = NULL;
}
}
Returnhdev;
}
}
}

Printf ("targetusb device not found! /N ");

Returnnull;
}

Voidusage ()
{
Printf ("Usage: dnw2 <File>/n ");
}

Unsignedchar * prepare_write_buf (char * filename, unsigned int * Len)
{
Unsignedchar * write_buf = NULL;
Struct stat FS;

Intfd = open (filename, o_rdonly );
If (-1 = FD)
{
Perror ("cannotopen file ");
Return NULL;
}
If (-1 = fstat (FD, & FS ))
{
Perror ("cannot get file size ");
Gotoerror;
}
Write_buf = (unsignedchar *) malloc (FS. st_size + 10 );
If (null = write_buf)
{
Perror ("mallocfailed ");
Goto error;
}

If (FS. st_size! = Read (FD, write_buf + 8, FS. st_size ))
{
Perror ("readingfile 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;

}

Intmain (INT argc, char * argv [])
{
If (2! = Argc)
{
Usage ();
Return1;
}

Structusb_dev_handle * hdev = open_port ();
If (! Hdev)
{
Return 1;
}

Unsignedint Len = 0;
Unsigned char * write_buf = prepare_write_buf (argv [1], & Len );
If (null = write_buf) return 1;

Unsignedint remain = Len;
Unsigned int towrite;
Printf ("writingdata.../N ");
While (remain)
{
Towrite = remain> 512? 512: remain;
If (towrite! = Usb_bulk_write (hdev, 0x03, write_buf + (LEN-remain), towrite, 3000 ))
{
Perror ("usb_bulk_writefailed ");
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.

Sudomv./dnw2/usr/bin

4)

Sudodnw2 path/your_filename

Note that this method is successfully tested on the tq2440 Development Board. For other development boards, find the corresponding code on the King Road !!!

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: sudoapt-Get install libusb-Dev

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


# Include <stdio. h>
# Include <USB. h>
# Include <errno. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
# Include <unistd. h>

# Defineqq2440_secbulk_idvendor 0x5345
# Define qq2440_secbulk_idproduct0x1234


Structusb_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)
{
Structusb_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 ("targetusb device found! /N ");
Struct usb_dev_handle * hdev = usb_open (Dev );
If (! Hdev)
{
Perror ("cannot opendevice ");
}
Else
{
If (0! = Usb_claim_interface (hdev, 0 ))
{
Perror ("cannot claiminterface ");
Usb_close (hdev );
Hdev = NULL;
}
}
Returnhdev;
}
}
}

Printf ("targetusb device not found! /N ");

Returnnull;
}

Voidusage ()
{
Printf ("Usage: dnw2 <File>/n ");
}

Unsignedchar * prepare_write_buf (char * filename, unsigned int * Len)
{
Unsignedchar * write_buf = NULL;
Struct stat FS;

Intfd = open (filename, o_rdonly );
If (-1 = FD)
{
Perror ("cannotopen file ");
Return NULL;
}
If (-1 = fstat (FD, & FS ))
{
Perror ("cannot get file size ");
Gotoerror;
}
Write_buf = (unsignedchar *) malloc (FS. st_size + 10 );
If (null = write_buf)
{
Perror ("mallocfailed ");
Goto error;
}

If (FS. st_size! = Read (FD, write_buf + 8, FS. st_size ))
{
Perror ("readingfile 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;

}

Intmain (INT argc, char * argv [])
{
If (2! = Argc)
{
Usage ();
Return1;
}

Structusb_dev_handle * hdev = open_port ();
If (! Hdev)
{
Return 1;
}

Unsignedint Len = 0;
Unsigned char * write_buf = prepare_write_buf (argv [1], & Len );
If (null = write_buf) return 1;

Unsignedint remain = Len;
Unsigned int towrite;
Printf ("writingdata.../N ");
While (remain)
{
Towrite = remain> 512? 512: remain;
If (towrite! = Usb_bulk_write (hdev, 0x03, write_buf + (LEN-remain), towrite, 3000 ))
{
Perror ("usb_bulk_writefailed ");
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.

Sudomv./dnw2/usr/bin

4)

Sudodnw2 path/your_filename

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.