/* USB DFU uses: control transfers for communicating recipient is interface request type is class */ var STM32DFU_protocol = function() { this.hex; // ref this.verify_hex; this.handle = null; // connection handle this.request = { DETACH: 0x00, // OUT, Requests the device to leave DFU mode and enter the application. DNLOAD: 0x01, // OUT, Requests data transfer from Host to the device in order to load them into device internal Flash. Includes also erase commands UPLOAD: 0x02, // IN, Requests data transfer from device to Host in order to load content of device internal Flash into a Host file. GETSTATUS: 0x03, // IN, Requests device to send status report to the Host (including status resulting from the last request execution and the state the device will enter immediately after this request). CLRSTATUS: 0x04, // OUT, Requests device to clear error status and move to next step GETSTATE: 0x05, // IN, Requests the device to send only the state it will enter immediately after this request. ABORT: 0x06 // OUT, Requests device to exit the current state/operation and enter idle state immediately. }; this.status = { OK: 0x00, // No error condition is present. errTARGET: 0x01, // File is not targeted for use by this device. errFILE: 0x02, // File is for this device but fails some vendor-specific verification test errWRITE: 0x03, // Device is unable to write memory. errERASE: 0x04, // Memory erase function failed. errCHECK_ERASED: 0x05, // Memory erase check failed. errPROG: 0x06, // Program memory function failed. errVERIFY: 0x07, // Programmed memory failed verification. errADDRESS: 0x08, // Cannot program memory due to received address that is out of range. errNOTDONE: 0x09, // Received DFU_DNLOAD with wLength = 0, but device does not think it has all of the data yet. errFIRMWARE: 0x0A, // Device's firmware is corrupt. It cannot return to run-time (non-DFU) operations. errVENDOR: 0x0B, // iString indicates a vendor-specific error. errUSBR: 0x0C, // Device detected unexpected USB reset signaling. errPOR: 0x0D, // Device detected unexpected power on reset. errUNKNOWN: 0x0E, // Something went wrong, but the device does not know what it was. errSTALLEDPKT: 0x0F // Device stalled an unexpected request. }; this.state = { appIDLE: 0, // Device is running its normal application. appDETACH: 1, // Device is running its normal application, has received the DFU_DETACH request, and is waiting for a USB reset. dfuIDLE: 2, // Device is operating in the DFU mode and is waiting for requests. dfuDNLOAD_SYNC: 3, // Device has received a block and is waiting for the host to solicit the status via DFU_GETSTATUS. dfuDNBUSY: 4, // Device is programming a control-write block into its nonvolatile memories. dfuDNLOAD_IDLE: 5, // Device is processing a download operation. Expecting DFU_DNLOAD requests. dfuMANIFEST_SYNC: 6, // Device has received the final block of firmware from the host and is waiting for receipt of DFU_GETSTATUS to begin the Manifestation phase; or device has completed the Manifestation phase and is waiting for receipt of DFU_GETSTATUS. dfuMANIFEST: 7, // Device is in the Manifestation phase. (Not all devices will be able to respond to DFU_GETSTATUS when in this state.) dfuMANIFEST_WAIT_RESET: 8, // Device has programmed its memories and is waiting for a USB reset or a power on reset. (Devices that must enter this state clear bitManifestationTolerant to 0.) dfuUPLOAD_IDLE: 9, // The device is processing an upload operation. Expecting DFU_UPLOAD requests. dfuERROR: 10 // An error has occurred. Awaiting the DFU_CLRSTATUS request. }; }; STM32DFU_protocol.prototype.connect = function(hex) { var self = this; self.hex = hex; chrome.usb.getDevices(usbDevices.STM32DFU, function(result) { if (result.length) { console.log('USB DFU detected with ID: ' + result[0].device); self.openDevice(result[0]); } else { // TODO: throw some error } }); }; STM32DFU_protocol.prototype.openDevice = function(device) { var self = this; chrome.usb.openDevice(device, function(handle) { self.handle = handle; console.log('Handle ID: ' + handle.handle); self.claimInterface(0); }); }; STM32DFU_protocol.prototype.closeDevice = function(callback) { }; STM32DFU_protocol.prototype.claimInterface = function(interfaceNumber) { chrome.usb.claimInterface(this.handle, interfaceNumber, function claimed() { console.log('Claimed interface: ' + interfaceNumber); }); }; STM32DFU_protocol.prototype.releaseInterface = function(callback) { }; STM32DFU_protocol.prototype.resetDevice = function(callback) { chrome.usb.resetDevice(this.handle, function(result) { console.log('Reset Device: ' + result); if (callback) callback(); }); }; STM32DFU_protocol.prototype.controlTransfer = function(direction, request, value, interface, length, data, callback) { if (direction == 'in') { // data is ignored chrome.usb.controlTransfer(this.handle, { 'direction': 'in', 'recipient': 'interface', 'requestType': 'class', 'request': request, 'value': value, 'index': interface, 'length': length }, callback); } else { // length is ignored if (data) { var arrayBuf = new ArrayBuffer(data.length); var arrayBufView = new Uint8Array(arrayBuf); arrayBufView.set(data); } else { var arrayBuf = new ArrayBuffer(0); } chrome.usb.controlTransfer(this.handle, { 'direction': 'out', 'recipient': 'interface', 'requestType': 'class', 'request': request, 'value': value, 'index': interface, 'data': arrayBuf }, callback); } }; // initialize object var STM32DFU = new STM32DFU_protocol();