LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer
User Name
Password
Linux - Embedded & Single-board computer This forum is for the discussion of Linux on both embedded devices and single-board computers (such as the Raspberry Pi, BeagleBoard and PandaBoard). Discussions involving Arduino, plug computers and other micro-controller like devices are also welcome.

Notices


Reply
  Search this Thread
Old 09-21-2010, 12:09 AM   #1
915086731
Member
 
Registered: Apr 2010
Posts: 144
Blog Entries: 6

Rep: Reputation: 2
Question usb_bulk_write failed: Resource temporarily unavailable


I am learning embed linux now. The develop board I use is mini2440, which contains s3c2440 board and arm920t cpu inside. The common develop platform is windows OS , but I love operating on linux. Minicom can contract to serial port of board and dnw2 can download binary program to board.
When I download program by using dnw2 on linux , I often get the following message:
Code:
85%      524288 bytes      usb_bulk_write failed: Resource temporarily unavailable
But often dnw2 can download small binary successfully. Error comes with big binary!!
The source code of dnw2 is :
dnw2.c
Code:
/* dnw2 linux main file. This depends on libusb.
 *
 * Author: 	Fox <hulifox008@163.com>
 * 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		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=bus->next)
	{
	 	struct usb_device *dev;
		for(dev=bus->devices;dev;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\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;
}
Is there anything wrong with dnw2.c or the libusb it called.

Last edited by 915086731; 09-21-2010 at 12:11 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C - Resource temporarily unavailable golmschenk Programming 1 03-07-2010 03:28 PM
Resource temporarily unavailable manoj.linux Linux - Enterprise 3 11-06-2009 08:21 AM
SIOCGMIIPHY on 'eth1' failed: Resource temporarily unavailable surendra.adhikari Linux - Newbie 1 10-13-2008 08:11 AM
RHEL 4.0 AS mii-tool - SIOCGMIIPHY on eth1 failed: Resource temporarily unavailable Qwerty9119 Red Hat 2 07-04-2007 10:18 AM
cfork failed: Resource temporarily unavailable x2000koh Linux - Networking 0 05-11-2003 10:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer

All times are GMT -5. The time now is 03:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration