Device firmware update and libusbx API in Windows CE

281 views Asked by At

I am trying to update the firmware on an Atmel device from a Windows CE environment. Here is snippet of my source code:

uint8_t buf[127];
struct libusb_device_handle *handle=NULL;
fp = fopen("\\Nandflash\\a.hex", "r+");
size_t re = fread(buf, 4, 1, fp);
cstatus = libusb_control_transfer(
            handle, 
            LIBUSB_ENDPOINT_OUT|
              LIBUSB_REQUEST_TYPE_VENDOR|
              LIBUSB_RECIPIENT_INTERFACE,
            0xA0, //Upload
            0x01, //Address of the device
            0,buf, sizeof(buf), 100);

I am keep getting -9 as result of the control transfer. How can I fix this problem?

1

There are 1 answers

12
Preston On

You are using a NULL handle value in your libusb_control_transfer function. You will need to initialize libusb and open the device to be able to talk to it:

uint8_t buf[127];
struct libusb_device_handle *handle=NULL;
struct libusb_context *context;
libusb_device **list;
libusb_device *found = NULL;

// Init libusb
libusb_init(&context);

// Open device your device
ssize_t cnt = libusb_get_device_list(NULL, &list);
for (i = 0; i < cnt; i++) {
    libusb_device *device = list[i];
    if (is_my_device(device)) {
        found = device;
        break;
    }
}

// If your device is found, open it and perform transfer, then close
if (found) {
    err = libusb_open(found, &handle);
    if (!err) {
        fp=fopen("\\Nandflash\\a.hex", "r+"); 
        size_t re=fread(buf, 4, 1, fp);
        cstatus=libusb_control_transfer(handle, LIBUSB_ENDPOINT_OUT|LIBUSB_REQUEST_TYPE_VENDOR|LIBUSB_RECIPIENT_INTERFACE,
           0xA0,//upload
           0x01, //address of device
           0,buf, sizeof(buf), 100);
        libusb_close(handle);
    }
}

// Cleanup
libusb_free_device_list(list, 1);
libusb_exit(context);