Hello
when i use below program to write and read data from USB mass storage device(flash memory) i am getting following error. I've succeeded with usb_bulk_write() but can't utilize
usb_bulk_read(). It returns an error.
Set configuration
usb_bulk_write: 12 bytes written
33 5b 2 1 0 5 1 3 7 f 7f 1f
read failed: libusb0-dll:err [_usb_reap_async] reaping request failed, win error
: A device attached to the system is not functioning.
usb_bulk_read: -5 bytes read:
what is the problem in this code?
#include "lusb0_usb.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define VENDOR_ID 0x13FE
#define PRODUCT_ID 0x3600
#define CONFIGURATION 0x01
#define TIMEOUT 5000
struct usb_device *current_device;
usb_dev_handle *current_handle;
struct usb_device *locate_usb_dev(void)
{
struct usb_bus *bus_search;
struct usb_device *device_search;
struct usb_device *device_found;
device_found = NULL;
bus_search = usb_busses;
while (bus_search != NULL)
{
device_search = bus_search->devices;
while (device_search != NULL)
{
if ((device_search->descriptor.idVendor ==VENDOR_ID)
&& (device_search->descriptor.idProduct ==PRODUCT_ID)
&& (device_found == NULL))
{
device_found = device_search;
}
device_search = device_search->next;
}
bus_search = bus_search->next;
}
if(device_found == NULL)
{
printf("USB Device not found!\n");
exit(1);
}
return device_found;
}
void f_claim(void)
{
if (usb_claim_interface(current_handle, 0) < 0)
{
fprintf(stderr, "Could not claim interface 0: %s\n", usb_strerror());
return;
}
usb_set_altinterface(current_handle, 0);
}
int f_read(void)
{
int size = 512;
char *data = (char *) malloc(size*sizeof(char));
int r = usb_bulk_read(current_handle,0x81,data, size,TIMEOUT);
if (r < 0){
printf("read failed: %s\n", usb_strerror());
}
printf("usb_bulk_read: %d bytes read: ", r);
for (int i = 0; i < r; ++i) {
printf("%3x ", data[i]);
}
printf("\n");
free(data);
return r;
}
int f_write(void)
{
int size = 12;
char *data = (char *) malloc(size*sizeof(char));
data[0] = 0x33;
data[1] = 0x5B;
data[2] = 0x02;
data[3] = 0x01;
data[4] = 0x00;
data[5] = 0x05;
data[6] = 0x01;
data[7] = 0x03;
data[8] = 0x07;
data[9] = 0x0F;
data[10] = 0x7F;
data[11] = 0x1F;
int w=usb_bulk_write(current_handle,0x02,data,size,TIMEOUT);
if (w < 0){
printf("usb_bulk_write failed: %s\n", usb_strerror());
return -1;
}
printf("usb_bulk_write: %d bytes written\n", w);
for (int i = 0; i < size; ++i) {
printf("%3x ", data[i]);
}
printf("\n");
free(data);
return w;
}
void f_release(void)
{
usb_release_interface(current_handle, 0);
}
int f_init(void)
{
usb_init();
usb_find_busses();
usb_find_devices();
current_device = locate_usb_dev();
if (current_device == NULL)
{
fprintf(stderr, "Cannot find FLASH Device on any Bus\n");
return -1;
}
current_handle = usb_open(current_device);
printf("Set configuration\n");
if (usb_set_configuration(current_handle, CONFIGURATION) < 0) {
printf("error on usb_set_configuration: %s\n", usb_strerror());
system("PAUSE");
return -1;
}
return 0;
}
void f_exit(void)
{
usb_close(current_handle);
}
int main(void)
{
f_init();
f_claim();
f_write();
f_release();
f_claim();
f_read();
f_release();
f_exit();
}