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 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: # include & lt; stdi
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: sudo apt-get install libusb-dev
Compile a download tool after installation, and a cool man provides one online. The Code is as follows:
# 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