Merge pull request #1249 from jflyper/bfcdev-use-wtransfersize-for-writing-and-reading

Use wTransferSize from DFU functional descriptor for writing/reading
10.5.x-maintenance
Michael Keller 2018-12-12 23:33:36 +13:00 committed by GitHub
commit 9d9dd13cc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 42 additions and 5 deletions

View File

@ -64,6 +64,7 @@ var STM32DFU_protocol = function () {
this.chipInfo = null; // information about chip's memory
this.flash_layout = { 'start_address': 0, 'total_size': 0, 'sectors': []};
this.transferSize = 2048; // Default USB DFU transfer size for F3,F4 and F7
};
STM32DFU_protocol.prototype.connect = function (device, hex, options, callback) {
@ -287,6 +288,38 @@ STM32DFU_protocol.prototype.getInterfaceDescriptor = function (_interface, callb
});
}
STM32DFU_protocol.prototype.getFunctionalDescriptor = function (_interface, callback) {
var self = this;
chrome.usb.controlTransfer(this.handle, {
'direction': 'in',
'recipient': 'interface',
'requestType': 'standard',
'request': 6,
'value': 0x2100,
'index': 0,
'length': 255
}, function (result) {
if(self.checkChromeError()) {
console.log('USB transfer failed! ' + result.resultCode);
callback({}, result.resultCode);
return;
}
var buf = new Uint8Array(result.data);
var descriptor = {
'bLength': buf[0],
'bDescriptorType': buf[1],
'bmAttributes': buf[2],
'wDetachTimeOut': (buf[4] << 8)|buf[3],
'wTransferSize': (buf[6] << 8)|buf[5],
'bcdDFUVersion': buf[7]
};
callback(descriptor, result.resultCode);
});
}
STM32DFU_protocol.prototype.getChipInfo = function (_interface, callback) {
var self = this;
@ -512,7 +545,7 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) {
console.log('Failed to detect internal flash');
self.upload_procedure(99);
}
self.chipInfo = chipInfo;
self.flash_layout = chipInfo.internal_flash;
@ -526,8 +559,12 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) {
(self.available_flash_size / 1024.0).toFixed(1)]));
self.upload_procedure(99);
} else {
self.clearStatus(function () {
self.upload_procedure(1);
self.getFunctionalDescriptor(0, function (descriptor, resultCode) {
self.transferSize = resultCode ? 2048 : descriptor.wTransferSize;
console.log('Using transfer size: ' + self.transferSize);
self.clearStatus(function () {
self.upload_procedure(1);
});
});
}
}
@ -764,7 +801,7 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) {
var write = function () {
if (bytes_flashed < self.hex.data[flashing_block].bytes) {
var bytes_to_write = ((bytes_flashed + 2048) <= self.hex.data[flashing_block].bytes) ? 2048 : (self.hex.data[flashing_block].bytes - bytes_flashed);
var bytes_to_write = ((bytes_flashed + self.transferSize) <= self.hex.data[flashing_block].bytes) ? self.transferSize : (self.hex.data[flashing_block].bytes - bytes_flashed);
var data_to_flash = self.hex.data[flashing_block].data.slice(bytes_flashed, bytes_flashed + bytes_to_write);
@ -848,7 +885,7 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) {
var read = function () {
if (bytes_verified < self.hex.data[reading_block].bytes) {
var bytes_to_read = ((bytes_verified + 2048) <= self.hex.data[reading_block].bytes) ? 2048 : (self.hex.data[reading_block].bytes - bytes_verified);
var bytes_to_read = ((bytes_verified + self.transferSize) <= self.hex.data[reading_block].bytes) ? self.transferSize : (self.hex.data[reading_block].bytes - bytes_verified);
self.controlTransfer('in', self.request.UPLOAD, wBlockNum++, 0, bytes_to_read, 0, function (data, code) {
for (var i = 0; i < data.length; i++) {