DFU: Add local erase, 2 kB page size hardcoded

Not possible to read chip ID using DFU or page size info from USB descriptor (not yet supported by Chrome)
10.3.x-maintenance
Michael Corcoran 2015-10-08 23:17:46 +13:00
parent d89e9682c2
commit 1e386f508b
3 changed files with 105 additions and 32 deletions

View File

@ -107,7 +107,7 @@ STM32_protocol.prototype.connect = function (port, baud, hex, options, callback)
PortHandler.check_usb_devices(function(dfu_available) {
if(dfu_available) {
GUI.connect_lock = false;
STM32DFU.connect(usbDevices.STM32DFU, hex);
STM32DFU.connect(usbDevices.STM32DFU, hex, options);
} else {
serial.connect(port, {bitrate: self.baud, parityBit: 'even', stopBits: 'one'}, function (openInfo) {
if (openInfo) {

View File

@ -61,13 +61,26 @@ var STM32DFU_protocol = function () {
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.
};
// Assume 2 kB page size (STM32F303)
// Cannot read chip ID using DFU protocol and Chrome doesn't provide the interface
// description string with flash page size information (at least on Linux anyway)
this.page_size = 2048;
};
STM32DFU_protocol.prototype.connect = function (device, hex, callback) {
STM32DFU_protocol.prototype.connect = function (device, hex, options, callback) {
var self = this;
self.hex = hex;
self.callback = callback;
self.options = {
erase_chip: false
};
if (options.erase_chip) {
self.options.erase_chip = true;
}
// reset and set some variables before we start
self.upload_time_start = new Date().getTime();
self.verify_hex = [];
@ -150,6 +163,12 @@ STM32DFU_protocol.prototype.controlTransfer = function (direction, request, valu
'index': _interface,
'length': length
}, function (result) {
if (chrome.runtime.lastError) {
if(chrome.runtime.lastError.message)
console.log(chrome.runtime.lastError.message);
else
console.log(chrome.runtime.lastError);
}
if (result.resultCode) console.log(result.resultCode);
var buf = new Uint8Array(result.data);
@ -217,7 +236,7 @@ STM32DFU_protocol.prototype.loadAddress = function (address, callback) {
if (data[4] == self.state.dfuDNLOAD_IDLE) {
callback(data);
} else {
console.log('Failed to execure address load');
console.log('Failed to execute address load');
self.upload_procedure(99);
}
});
@ -256,32 +275,85 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) {
});
break;
case 2:
// full chip erase
console.log('Executing global chip erase');
$('span.progressLabel').text('Erasing ...');
// erase
if (self.options.erase_chip) {
// full chip erase
console.log('Executing global chip erase');
$('span.progressLabel').text('Erasing ...');
self.controlTransfer('out', self.request.DNLOAD, 0, 0, 0, [0x41], function () {
self.controlTransfer('in', self.request.GETSTATUS, 0, 0, 6, 0, function (data) {
if (data[4] == self.state.dfuDNBUSY) { // completely normal
var delay = data[1] | (data[2] << 8) | (data[3] << 16);
self.controlTransfer('out', self.request.DNLOAD, 0, 0, 0, [0x41], function () {
self.controlTransfer('in', self.request.GETSTATUS, 0, 0, 6, 0, function (data) {
if (data[4] == self.state.dfuDNBUSY) { // completely normal
var delay = data[1] | (data[2] << 8) | (data[3] << 16);
setTimeout(function () {
self.controlTransfer('in', self.request.GETSTATUS, 0, 0, 6, 0, function (data) {
if (data[4] == self.state.dfuDNLOAD_IDLE) {
self.upload_procedure(4);
} else {
console.log('Failed to execute global chip erase');
self.upload_procedure(99);
}
});
}, delay);
} else {
console.log('Failed to initiate global chip erase');
self.upload_procedure(99);
}
setTimeout(function () {
self.controlTransfer('in', self.request.GETSTATUS, 0, 0, 6, 0, function (data) {
if (data[4] == self.state.dfuDNLOAD_IDLE) {
self.upload_procedure(4);
} else {
console.log('Failed to execute global chip erase');
self.upload_procedure(99);
}
});
}, delay);
} else {
console.log('Failed to initiate global chip erase');
self.upload_procedure(99);
}
});
});
});
} else {
// local erase
var max_address = self.hex.data[self.hex.data.length - 1].address + self.hex.data[self.hex.data.length - 1].bytes - 0x8000000,
erase_pages_n = Math.ceil(max_address / self.page_size),
page = 0;
$('span.progressLabel').text('Erasing ...');
console.log('Executing local chip erase');
console.log('Erasing. page: 0x00 - 0x' + erase_pages_n.toString(16));
var erase_page = function() {
var page_addr = page * self.page_size + 0x8000000;
var cmd = [0x41, page_addr & 0xff, (page_addr >> 8) & 0xff, (page_addr >> 16) & 0xff, (page_addr >> 24) & 0xff];
self.controlTransfer('out', self.request.DNLOAD, 0, 0, 0, cmd, function () {
self.controlTransfer('in', self.request.GETSTATUS, 0, 0, 6, 0, function (data) {
if (data[4] == self.state.dfuDNBUSY) { // completely normal
var delay = data[1] | (data[2] << 8) | (data[3] << 16);
setTimeout(function () {
self.controlTransfer('in', self.request.GETSTATUS, 0, 0, 6, 0, function (data) {
if (data[4] == self.state.dfuDNLOAD_IDLE) {
// update progress bar
self.progress_bar_e.val((page + 1) / erase_pages_n * 100);
page++;
if(page == erase_pages_n) {
console.log("Erase: complete");
self.upload_procedure(4);
}
else
erase_page();
} else {
console.log('Failed to erase page 0x' + self.current_page.toString(16));
self.upload_procedure(99);
}
});
}, delay);
} else {
console.log('Failed to initiate page erase, page 0x' + self.current_page.toString(16));
self.upload_procedure(99);
}
});
});
};
// start
erase_page();
}
break;
case 4:
// upload
// we dont need to clear the state as we are already using DFU_DNLOAD

View File

@ -307,10 +307,15 @@ TABS.firmware_flasher.initialize = function (callback) {
if (!$(this).hasClass('locked')) {
if (!GUI.connect_lock) { // button disabled while flashing is in progress
if (parsed_hex != false) {
var options = {};
if ($('input.erase_chip').is(':checked')) {
options.erase_chip = true;
}
if (String($('div#port-picker #port').val()) != 'DFU') {
if (String($('div#port-picker #port').val()) != '0') {
var options = {},
port = String($('div#port-picker #port').val()),
var port = String($('div#port-picker #port').val()),
baud;
switch (GUI.operating_system) {
@ -332,10 +337,6 @@ TABS.firmware_flasher.initialize = function (callback) {
options.reboot_baud = parseInt($('div#port-picker #baud').val());
}
if ($('input.erase_chip').is(':checked')) {
options.erase_chip = true;
}
if ($('input.flash_manual_baud').is(':checked')) {
baud = parseInt($('#flash_manual_baud_rate').val());
}
@ -347,7 +348,7 @@ TABS.firmware_flasher.initialize = function (callback) {
GUI.log('<span style="color: red">Please select valid serial port</span>');
}
} else {
STM32DFU.connect(usbDevices.STM32DFU, parsed_hex);
STM32DFU.connect(usbDevices.STM32DFU, parsed_hex, options);
}
} else {
$('span.progressLabel').text(chrome.i18n.getMessage('firmwareFlasherFirmwareNotLoaded'));