Transfer files to the Development Board using USB in Linux

Source: Internet
Author: User

The arm11 Development Board arrived yesterday. You need to verify the functions first. In the past, fs2410 was used with minicom + usb2ram, which is more powerful than dnw in windows. Because minicom supports color display, there will be no garbled characters like dnw, at this point, the Super Terminal is doing a good job. Unfortunately, it cannot support USB transmission. In addition, we use a Linux Embedded System, which is usually developed under Ubuntu. Of course, we chose minicom + usb2ram ~

 

 
However, this time the problem occurs. USB 2ram does not support USB transmission on the ez6410 Board, which makes me very depressed! View the source code of usb2ram and find that there are four main areas related to the specific Board, namely the vendorid, productid, ram_base and Endpoint addresses. The problem becomes how to view the specific values of these parameters on the ez6410 Development Board. Ram_base is very simple. There is 0x50200000 in the user manual. The other three searched the internet and found that lsusb can be used to list the first two, 0x04e8 and 0x1234, respectively. As for the endpoint address, it was originally 0x03, I tried to change it to 0x02 ~

 

 
After compilation, it can be used with minicom, but the speed is still relatively slow, especially in comparison with the dnw in Windows. The speed is as slow as the snail bait! So I searched for a USB transfer program similar to dnw in Linux on the Internet and found a file named dnw2.

 

 
It's easy to open a c file and check the source code. It supports qq2440. The structure is clearer and simpler than usb2ram. You need to modify it in three places: vendorid, productid, and ram_base, no endpoint is required. More specifically, it is fixed to 0x02 ~ I have set up the three macro definitions. After compilation, I tried it. It can be supported, and the speed is much faster than that of usb2ram. It is hard to say that it is not directly compared with dnw in windows, but it is estimated that it is almost the same ~

 

 
With dnw2, it is estimated that usb2ram will not be needed in the future. After all, the speed is much worse. No one will like slow things ~
 

 
PS, the source code of dnw2.c is stored at http://fayaa.com/code/view/11533,welcome to download and use it ~
 
PS. You know why this is dnw2 ~ Previously there was a dnw for Linux, which was written with a USB transmission driver, and then the dnw program passed the data into the driver for USB transmission. It is a little more troublesome than dnw2. You need to compile a driver and load it into the kernel space. It is not like dnw2 to get it done directly by a file! The transmission speed should be similar to that of the two, both of which are based on 512-byte data blocks. However, theoretically, dnw needs to merge data from the user space into the kernel space, which will affect some speed, but the difference should be very small, I can hardly feel it ~

 
The reason for the slow speed of USB 2 Ram is that the data block settings are small, and the speed should be improved when the data block settings are large ~ In addition, it seems that dnw was not transferred in 512 bytes (32 bytes ?), The speed will only come up after the change ~ I don't know what the optimal data block size is. Can I further increase the speed if I change it to 1024? Try again later when transferring the root file system ~

 
 
 
 
C language: transfer files to dnw2 in Ram through USB in Linux
/* Dnw2 Linux main file. This depends on libusb.
*
* You shoshould use lsusb to find out the actual vender ID & Product ID of board.
*
* Author: sea. Chen <31312532@qq.com>
*
* License: GPL
*
*/
 
# Include <stdio. h>
# Include <USB. h>
# Include <errno. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
# Include <unistd. h>
 
# Define ok6410_vendor_id 0x04e8
# Define ok6410_product_id 0x1234
 
# Define ok6410_ram_base 0x50200000
 
/*
// Fs2410
# Define ram_base fs2410_ram_base
# Define vendor_id fs2410_vendor_id
# Define product_id fs2410_product_id
*/
// Ok6410
# Define ram_base ok6410_ram_base
# Define vendor_id ok6410_vendor_id
# Define product_id ok6410_product_id
 
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)
{
If (vendor_id = Dev-> descriptor. idvendor
& Amp; 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 (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 <File> \ 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) = ram_base; // 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, 0x02, 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;
}
 
 
 
 
 
 
/* Dnw2 Linux main file. This depends on libusb.
*
* Author: Fox * 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 0x04e8
# 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)
{
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 <File> \ 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> 1024? 1024: remain;
If (towrite! = Usb_bulk_write (hdev, 0x02, 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;
}
 
 
 
 

Related Article

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.