int dfu_makeidle(usb_dev_handle *dev, uint16_t iface) { int i; dfu_status status; for(i = 0; i < 3; i++) { if(dfu_getstatus(dev, iface, &status) < 0) { dfu_clrstatus(dev, iface); continue; } i--; switch(status.bState) { case STATE_DFU_IDLE: return 0; case STATE_DFU_DOWNLOAD_SYNC: case STATE_DFU_DOWNLOAD_IDLE: case STATE_DFU_MANIFEST_SYNC: case STATE_DFU_UPLOAD_IDLE: case STATE_DFU_DOWNLOAD_BUSY: case STATE_DFU_MANIFEST: dfu_abort(dev, iface); continue; case STATE_DFU_ERROR: dfu_clrstatus(dev, iface); continue; case STATE_APP_IDLE: dfu_detach(dev, iface, DFU_DETACH_TIMEOUT); continue; case STATE_APP_DETACH: case STATE_DFU_MANIFEST_WAIT_RESET: usb_reset(dev); return -1; default: return -1; } } return -1; }
int stm32_mem_manifest(usb_dev_handle *dev, uint16_t iface) { dfu_status status; int i; if((i = dfu_dnload(dev, iface, 0, NULL, 0)) < 0) return i; while(1) { if((i = dfu_getstatus(dev, iface, &status)) < 0) return 0; #ifdef WIN32 Sleep(status.bwPollTimeout); #else usleep(status.bwPollTimeout * 1000); #endif switch(status.bState) { case STATE_DFU_MANIFEST: return 0; default: return -1; } } }
static int stm32_download(usb_dev_handle *dev, uint16_t iface, uint16_t wBlockNum, void *data, int size) { dfu_status status; int i; if((i = dfu_dnload(dev, iface, wBlockNum, data, size)) < 0) return i; while(1) { if((i = dfu_getstatus(dev, iface, &status)) < 0) return i; switch(status.bState) { case STATE_DFU_DOWNLOAD_BUSY: #ifdef WIN32 Sleep(status.bwPollTimeout); #else usleep(status.bwPollTimeout * 1000); #endif break; case STATE_DFU_DOWNLOAD_IDLE: return 0; default: return -1; } } }