【问题标题】:Device firmware update and libusbx API in Windows CEWindows CE 中的设备固件更新和 libusbx API
【发布时间】:2013-12-05 04:35:23
【问题描述】:

我正在尝试从 Windows CE 环境更新 Atmel 设备上的固件。这是我的源代码的sn-p:

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);

由于控制转移,我不断收到-9。我该如何解决这个问题?

【问题讨论】:

    标签: c usb windows-ce libusb firmware


    【解决方案1】:

    您在 libusb_control_transfer 函数中使用了 NULL 句柄值。您需要初始化 libusb 并打开设备才能与它对话:

    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);
    

    【讨论】:

    • 你能告诉我函数 is_my_device 应该是什么
    • 您可能希望对您的 VID 和 PID 进行一些匹配,以确保您知道您在说什么。更多内容取决于您的实施。
    • 我试过 r=libusb_get_device_descriptor(dev, &desc);将 desc vid 和 pid 与已知的 vid 和 pid 进行比较。我还尝试通过 devh = libusb_open_device_with_vid_pid(NULL, 0x05ba, 0x000a); 获取句柄我仍然得到-9。我是否将正确的参数传递给 libusb_control_transfer?第四个和第五个应该是什么值?
    • 当您调用libusb_open_device_with_vid_pid() 时,您是否得到返回的有效句柄,函数是否成功?如果您没有有效的设备句柄,则不会有任何效果。如果您指的是libusb_control_transfer() 的第四个和第五个参数,它们是 wValue 和 wIndex 值。您需要查阅您设备的协议以确保这些协议有效,否则您的设备会 NAK。
    • 是的,我确实得到了有效的句柄
    猜你喜欢
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 2013-11-12
    • 2010-10-11
    相关资源
    最近更新 更多