1
0
Fork 0
mirror of https://github.com/betaflight/betaflight-configurator.git synced 2025-07-16 04:45:20 +03:00

Rebase of stm32.js

This commit is contained in:
Mark Haslinghuis 2021-08-19 01:21:39 +02:00
parent bfa9a38fef
commit 682539f11b
No known key found for this signature in database
GPG key ID: 198B0F616296A584
2 changed files with 148 additions and 142 deletions

View file

@ -7,7 +7,7 @@
*/ */
'use strict'; 'use strict';
var STM32_protocol = function () { const STM32_protocol = function () {
this.baud = null; this.baud = null;
this.options = {}; this.options = {};
this.callback = null; this.callback = null;
@ -16,7 +16,7 @@ var STM32_protocol = function () {
this.receive_buffer = []; this.receive_buffer = [];
this.bytes_to_read = 0; this.bytesToRead = 0;
this.read_callback = null; this.read_callback = null;
this.upload_time_start = 0; this.upload_time_start = 0;
@ -53,7 +53,7 @@ var STM32_protocol = function () {
// no input parameters // no input parameters
STM32_protocol.prototype.connect = function (port, baud, hex, options, callback) { STM32_protocol.prototype.connect = function (port, baud, hex, options, callback) {
var self = this; const self = this;
self.hex = hex; self.hex = hex;
self.port = port; self.port = port;
self.baud = baud; self.baud = baud;
@ -122,7 +122,7 @@ STM32_protocol.prototype.connect = function (port, baud, hex, options, callback)
} }
}; };
var legacyRebootAndFlash = function() { const legacyRebootAndFlash = function() {
serial.connect(self.port, {bitrate: self.options.reboot_baud}, function (openInfo) { serial.connect(self.port, {bitrate: self.options.reboot_baud}, function (openInfo) {
if (!openInfo) { if (!openInfo) {
GUI.connect_lock = false; GUI.connect_lock = false;
@ -133,8 +133,8 @@ STM32_protocol.prototype.connect = function (port, baud, hex, options, callback)
console.log('Using legacy reboot method'); console.log('Using legacy reboot method');
console.log('Sending ascii "R" to reboot'); console.log('Sending ascii "R" to reboot');
var bufferOut = new ArrayBuffer(1); const bufferOut = new ArrayBuffer(1);
var bufferView = new Uint8Array(bufferOut); const bufferView = new Uint8Array(bufferOut);
bufferView[0] = 0x52; bufferView[0] = 0x52;
@ -144,7 +144,7 @@ STM32_protocol.prototype.connect = function (port, baud, hex, options, callback)
}); });
}; };
var onConnectHandler = function () { const onConnectHandler = function () {
GUI.log(i18n.getMessage('apiVersionReceived', [FC.CONFIG.apiVersion])); GUI.log(i18n.getMessage('apiVersionReceived', [FC.CONFIG.apiVersion]));
@ -201,14 +201,14 @@ STM32_protocol.prototype.connect = function (port, baud, hex, options, callback)
} }
}; };
var onTimeoutHandler = function() { const onTimeoutHandler = function() {
GUI.connect_lock = false; GUI.connect_lock = false;
console.log('Looking for capabilities via MSP failed'); console.log('Looking for capabilities via MSP failed');
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32RebootingToBootloaderFailed'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.INVALID); TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32RebootingToBootloaderFailed'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.INVALID);
}; };
var onFailureHandler = function() { const onFailureHandler = function() {
GUI.connect_lock = false; GUI.connect_lock = false;
TABS.firmware_flasher.refresh(); TABS.firmware_flasher.refresh();
}; };
@ -222,7 +222,7 @@ STM32_protocol.prototype.connect = function (port, baud, hex, options, callback)
// initialize certain variables and start timers that oversee the communication // initialize certain variables and start timers that oversee the communication
STM32_protocol.prototype.initialize = function () { STM32_protocol.prototype.initialize = function () {
var self = this; const self = this;
// reset and set some variables before we start // reset and set some variables before we start
self.receive_buffer = []; self.receive_buffer = [];
@ -265,60 +265,60 @@ STM32_protocol.prototype.initialize = function () {
// this method should be executed every 1 ms via interval timer // this method should be executed every 1 ms via interval timer
STM32_protocol.prototype.read = function (readInfo) { STM32_protocol.prototype.read = function (readInfo) {
// routine that fills the buffer // routine that fills the buffer
var data = new Uint8Array(readInfo.data); const data = new Uint8Array(readInfo.data);
for (var i = 0; i < data.length; i++) { for (const instance of data) {
this.receive_buffer.push(data[i]); this.receive_buffer.push(instance);
} }
// routine that fetches data from buffer if statement is true // routine that fetches data from buffer if statement is true
if (this.receive_buffer.length >= this.bytes_to_read && this.bytes_to_read != 0) { if (this.receive_buffer.length >= this.bytesToRead && this.bytesToRead != 0) {
var data = this.receive_buffer.slice(0, this.bytes_to_read); // bytes requested const fetched = this.receive_buffer.slice(0, this.bytesToRead); // bytes requested
this.receive_buffer.splice(0, this.bytes_to_read); // remove read bytes this.receive_buffer.splice(0, this.bytesToRead); // remove read bytes
this.bytes_to_read = 0; // reset trigger this.bytesToRead = 0; // reset trigger
this.read_callback(data); this.read_callback(fetched);
} }
}; };
// we should always try to consume all "proper" available data while using retrieve // we should always try to consume all "proper" available data while using retrieve
STM32_protocol.prototype.retrieve = function (n_bytes, callback) { STM32_protocol.prototype.retrieve = function (nBytes, callback) {
if (this.receive_buffer.length >= n_bytes) { if (this.receive_buffer.length >= nBytes) {
// data that we need are there, process immediately // data that we need are there, process immediately
var data = this.receive_buffer.slice(0, n_bytes); const data = this.receive_buffer.slice(0, nBytes);
this.receive_buffer.splice(0, n_bytes); // remove read bytes this.receive_buffer.splice(0, nBytes); // remove read bytes
callback(data); callback(data);
} else { } else {
// still waiting for data, add callback // still waiting for data, add callback
this.bytes_to_read = n_bytes; this.bytesToRead = nBytes;
this.read_callback = callback; this.read_callback = callback;
} }
}; };
// bytes_to_send = array of bytes that will be send over serial // bytes_to_send = array of bytes that will be send over serial
// bytes_to_read = received bytes necessary to trigger read_callback // bytesToRead = received bytes necessary to trigger read_callback
// callback = function that will be executed after received bytes = bytes_to_read // callback = function that will be executed after received bytes = bytesToRead
STM32_protocol.prototype.send = function (bytes_to_send, bytes_to_read, callback) { STM32_protocol.prototype.send = function (bytes_to_send, bytesToRead, callback) {
// flip flag // flip flag
this.upload_process_alive = true; this.upload_process_alive = true;
var bufferOut = new ArrayBuffer(bytes_to_send.length); const bufferOut = new ArrayBuffer(bytes_to_send.length);
var bufferView = new Uint8Array(bufferOut); const bufferView = new Uint8Array(bufferOut);
// set bytes_to_send values inside bufferView (alternative to for loop) // set bytes_to_send values inside bufferView (alternative to for loop)
bufferView.set(bytes_to_send); bufferView.set(bytes_to_send);
// update references // update references
this.bytes_to_read = bytes_to_read; this.bytesToRead = bytesToRead;
this.read_callback = callback; this.read_callback = callback;
// empty receive buffer before next command is out // empty receive buffer before next command is out
this.receive_buffer = []; this.receive_buffer = [];
// send over the actual data // send over the actual data
serial.send(bufferOut, function (writeInfo) {}); serial.send(bufferOut);
}; };
// val = single byte to be verified // val = single byte to be verified
@ -326,8 +326,8 @@ STM32_protocol.prototype.send = function (bytes_to_send, bytes_to_read, callback
// result = true/false // result = true/false
STM32_protocol.prototype.verify_response = function (val, data) { STM32_protocol.prototype.verify_response = function (val, data) {
if (val != data[0]) { if (val !== data[0]) {
var message = 'STM32 Communication failed, wrong response, expected: ' + val + ' (0x' + val.toString(16) + ') received: ' + data[0] + ' (0x' + data[0].toString(16) + ')'; const message = `STM32 Communication failed, wrong response, expected: ${val} (0x${val.toString(16)}) received: ${data[0]} (0x${data[0].toString(16)})`;
console.error(message); console.error(message);
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32WrongResponse',[val, val.toString(16), data[0], data[0].toString(16)]), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.INVALID); TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32WrongResponse',[val, val.toString(16), data[0], data[0].toString(16)]), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.INVALID);
@ -408,45 +408,44 @@ STM32_protocol.prototype.verify_chip_signature = function (signature) {
return true; return true;
} else { } else {
console.log('Supplied hex is bigger then flash available on the chip, HEX: ' + this.hex.bytes_total + ' bytes, limit = ' + this.available_flash_size + ' bytes'); console.log('Supplied hex is bigger then flash available on the chip, HEX: ' + this.hex.bytes_total + ' bytes, limit = ' + this.available_flash_size + ' bytes');
return false; return false;
} }
} }
console.log('Chip NOT recognized: ' + signature); console.log(`Chip NOT recognized: ${signature}`);
return false; return false;
}; };
// first_array = usually hex_to_flash array // firstArray = usually hex_to_flash array
// second_array = usually verify_hex array // secondArray = usually verify_hex array
// result = true/false // result = true/false
STM32_protocol.prototype.verify_flash = function (first_array, second_array) { STM32_protocol.prototype.verify_flash = function (firstArray, secondArray) {
for (var i = 0; i < first_array.length; i++) { for (let i = 0; i < firstArray.length; i++) {
if (first_array[i] != second_array[i]) { if (firstArray[i] !== secondArray[i]) {
console.log('Verification failed on byte: ' + i + ' expected: 0x' + first_array[i].toString(16) + ' received: 0x' + second_array[i].toString(16)); console.log(`Verification failed on byte: ${i} expected: 0x${firstArray[i].toString(16)} received: 0x${secondArray[i].toString(16)}`);
return false; return false;
} }
} }
console.log('Verification successful, matching: ' + first_array.length + ' bytes'); console.log(`Verification successful, matching: ${firstArray.length} bytes`);
return true; return true;
}; };
// step = value depending on current state of upload_procedure // step = value depending on current state of upload_procedure
STM32_protocol.prototype.upload_procedure = function (step) { STM32_protocol.prototype.upload_procedure = function (step) {
var self = this; const self = this;
switch (step) { switch (step) {
case 1: case 1:
// initialize serial interface on the MCU side, auto baud rate settings // initialize serial interface on the MCU side, auto baud rate settings
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32ContactingBootloader'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL); TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32ContactingBootloader'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL);
var send_counter = 0; let sendCounter = 0;
GUI.interval_add('stm32_initialize_mcu', function () { // 200 ms interval (just in case mcu was already initialized), we need to break the 2 bytes command requirement GUI.interval_add('stm32_initialize_mcu', function () { // 200 ms interval (just in case mcu was already initialized), we need to break the 2 bytes command requirement
self.send([0x7F], 1, function (reply) { self.send([0x7F], 1, function (reply) {
if (reply[0] == 0x7F || reply[0] == self.status.ACK || reply[0] == self.status.NACK) { if (reply[0] === 0x7F || reply[0] === self.status.ACK || reply[0] === self.status.NACK) {
GUI.interval_remove('stm32_initialize_mcu'); GUI.interval_remove('stm32_initialize_mcu');
console.log('STM32 - Serial interface initialized on the MCU side'); console.log('STM32 - Serial interface initialized on the MCU side');
@ -462,7 +461,7 @@ STM32_protocol.prototype.upload_procedure = function (step) {
} }
}); });
if (send_counter++ > 3) { if (sendCounter++ > 3) {
// stop retrying, its too late to get any response from MCU // stop retrying, its too late to get any response from MCU
console.log('STM32 - no response from bootloader, disconnecting'); console.log('STM32 - no response from bootloader, disconnecting');
@ -475,29 +474,31 @@ STM32_protocol.prototype.upload_procedure = function (step) {
self.upload_procedure(99); self.upload_procedure(99);
} }
}, 250, true); }, 250, true);
break; break;
case 2: case 2:
// get version of the bootloader and supported commands // get version of the bootloader and supported commands
self.send([self.command.get, 0xFF], 2, function (data) { // 0x00 ^ 0xFF self.send([self.command.get, 0xFF], 2, function (data) { // 0x00 ^ 0xFF
if (self.verify_response(self.status.ACK, data)) { if (self.verify_response(self.status.ACK, data)) {
self.retrieve(data[1] + 1 + 1, function (data) { // data[1] = number of bytes that will follow [ 1 except current and ACKs] self.retrieve(data[1] + 1 + 1, function (data) { // data[1] = number of bytes that will follow [ 1 except current and ACKs]
console.log('STM32 - Bootloader version: ' + (parseInt(data[0].toString(16)) / 10).toFixed(1)); // convert dec to hex, hex to dec and add floating point console.log(`STM32 - Bootloader version: ${(parseInt(data[0].toString(16)) / 10).toFixed(1)}`); // convert dec to hex, hex to dec and add floating point
self.useExtendedErase = (data[7] == self.command.extended_erase); self.useExtendedErase = (data[7] === self.command.extended_erase);
// proceed to next step // proceed to next step
self.upload_procedure(3); self.upload_procedure(3);
}); });
} }
}); });
break; break;
case 3: case 3:
// get ID (device signature) // get ID (device signature)
self.send([self.command.get_ID, 0xFD], 2, function (data) { // 0x01 ^ 0xFF self.send([self.command.get_ID, 0xFD], 2, function (data) { // 0x01 ^ 0xFF
if (self.verify_response(self.status.ACK, data)) { if (self.verify_response(self.status.ACK, data)) {
self.retrieve(data[1] + 1 + 1, function (data) { // data[1] = number of bytes that will follow [ 1 (N = 1 for STM32), except for current byte and ACKs] self.retrieve(data[1] + 1 + 1, function (data) { // data[1] = number of bytes that will follow [ 1 (N = 1 for STM32), except for current byte and ACKs]
var signature = (data[0] << 8) | data[1]; const signature = (data[0] << 8) | data[1];
console.log('STM32 - Signature: 0x' + signature.toString(16)); // signature in hex representation console.log(`STM32 - Signature: 0x${signature.toString(16)}`); // signature in hex representation
if (self.verify_chip_signature(signature)) { if (self.verify_chip_signature(signature)) {
// proceed to next step // proceed to next step
@ -509,6 +510,7 @@ STM32_protocol.prototype.upload_procedure = function (step) {
}); });
} }
}); });
break; break;
case 4: case 4:
// erase memory // erase memory
@ -516,7 +518,7 @@ STM32_protocol.prototype.upload_procedure = function (step) {
if (self.useExtendedErase) { if (self.useExtendedErase) {
if (self.options.erase_chip) { if (self.options.erase_chip) {
var message = 'Executing global chip erase (via extended erase)'; const message = 'Executing global chip erase (via extended erase)';
console.log(message); console.log(message);
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32GlobalEraseExtended'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL); TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32GlobalEraseExtended'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL);
@ -532,7 +534,7 @@ STM32_protocol.prototype.upload_procedure = function (step) {
}); });
} else { } else {
var message = 'Executing local erase (via extended erase)'; const message = 'Executing local erase (via extended erase)';
console.log(message); console.log(message);
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32LocalEraseExtended'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL); TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32LocalEraseExtended'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL);
@ -541,35 +543,35 @@ STM32_protocol.prototype.upload_procedure = function (step) {
// For reference: https://code.google.com/p/stm32flash/source/browse/stm32.c#723 // For reference: https://code.google.com/p/stm32flash/source/browse/stm32.c#723
var max_address = self.hex.data[self.hex.data.length - 1].address + self.hex.data[self.hex.data.length - 1].bytes - 0x8000000, const maxAddress = 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), const erasePagesN = Math.ceil(maxAddress / self.page_size);
buff = [], const buff = [];
checksum = 0; let checksum = 0;
var pg_byte; let pgByte;
pg_byte = (erase_pages_n - 1) >> 8; pgByte = (erasePagesN - 1) >> 8;
buff.push(pg_byte); buff.push(pgByte);
checksum ^= pg_byte; checksum ^= pgByte;
pg_byte = (erase_pages_n - 1) & 0xFF; pgByte = (erasePagesN - 1) & 0xFF;
buff.push(pg_byte); buff.push(pgByte);
checksum ^= pg_byte; checksum ^= pgByte;
for (var i = 0; i < erase_pages_n; i++) { for (let i = 0; i < erasePagesN; i++) {
pg_byte = i >> 8; pgByte = i >> 8;
buff.push(pg_byte); buff.push(pgByte);
checksum ^= pg_byte; checksum ^= pgByte;
pg_byte = i & 0xFF; pgByte = i & 0xFF;
buff.push(pg_byte); buff.push(pgByte);
checksum ^= pg_byte; checksum ^= pgByte;
} }
buff.push(checksum); buff.push(checksum);
console.log('Erasing. pages: 0x00 - 0x' + erase_pages_n.toString(16) + ', checksum: 0x' + checksum.toString(16)); console.log(`Erasing. pages: 0x00 - 0x${erasePagesN.toString(16)}, checksum: 0x${checksum.toString(16)}`);
self.send(buff, 1, function (reply) { self.send(buff, 1, function (_reply) {
if (self.verify_response(self.status.ACK, reply)) { if (self.verify_response(self.status.ACK, _reply)) {
console.log('Erasing: done'); console.log('Erasing: done');
// proceed to next step // proceed to next step
self.upload_procedure(5); self.upload_procedure(5);
@ -584,7 +586,7 @@ STM32_protocol.prototype.upload_procedure = function (step) {
} }
if (self.options.erase_chip) { if (self.options.erase_chip) {
var message = 'Executing global chip erase' ; const message = 'Executing global chip erase' ;
console.log(message); console.log(message);
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32GlobalErase'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL); TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32GlobalErase'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL);
@ -600,23 +602,23 @@ STM32_protocol.prototype.upload_procedure = function (step) {
} }
}); });
} else { } else {
var message = 'Executing local erase'; const message = 'Executing local erase';
console.log(message); console.log(message);
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32LocalErase'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL); TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32LocalErase'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL);
self.send([self.command.erase, 0xBC], 1, function (reply) { // 0x43 ^ 0xFF self.send([self.command.erase, 0xBC], 1, function (reply) { // 0x43 ^ 0xFF
if (self.verify_response(self.status.ACK, reply)) { if (self.verify_response(self.status.ACK, reply)) {
// the bootloader receives one byte that contains N, the number of pages to be erased 1 // the bootloader receives one byte that contains N, the number of pages to be erased 1
var max_address = self.hex.data[self.hex.data.length - 1].address + self.hex.data[self.hex.data.length - 1].bytes - 0x8000000, const maxAddress = 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), const erasePagesN = Math.ceil(maxAddress / self.page_size);
buff = [], const buff = [];
checksum = erase_pages_n - 1; let checksum = erasePagesN - 1;
buff.push(erase_pages_n - 1); buff.push(erasePagesN - 1);
for (var i = 0; i < erase_pages_n; i++) { for (let ii = 0; ii < erasePagesN; ii++) {
buff.push(i); buff.push(ii);
checksum ^= i; checksum ^= ii;
} }
buff.push(checksum); buff.push(checksum);
@ -631,49 +633,50 @@ STM32_protocol.prototype.upload_procedure = function (step) {
} }
}); });
} }
break; break;
case 5: case 5:
// upload // upload
console.log('Writing data ...'); console.log('Writing data ...');
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32Flashing'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL); TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32Flashing'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL);
var blocks = self.hex.data.length - 1, let blocks = self.hex.data.length - 1,
flashing_block = 0, flashing_block = 0,
address = self.hex.data[flashing_block].address, address = self.hex.data[flashing_block].address,
bytes_flashed = 0, bytes_flashed = 0,
bytes_flashed_total = 0; // used for progress bar bytes_flashed_total = 0; // used for progress bar
var write = function () { const write = function () {
if (bytes_flashed < self.hex.data[flashing_block].bytes) { if (bytes_flashed < self.hex.data[flashing_block].bytes) {
var bytes_to_write = ((bytes_flashed + 256) <= self.hex.data[flashing_block].bytes) ? 256 : (self.hex.data[flashing_block].bytes - bytes_flashed); const bytesToWrite = ((bytes_flashed + 256) <= self.hex.data[flashing_block].bytes) ? 256 : (self.hex.data[flashing_block].bytes - bytes_flashed);
// console.log('STM32 - Writing to: 0x' + address.toString(16) + ', ' + bytes_to_write + ' bytes'); // console.log('STM32 - Writing to: 0x' + address.toString(16) + ', ' + bytesToWrite + ' bytes');
self.send([self.command.write_memory, 0xCE], 1, function (reply) { // 0x31 ^ 0xFF self.send([self.command.write_memory, 0xCE], 1, function (reply) { // 0x31 ^ 0xFF
if (self.verify_response(self.status.ACK, reply)) { if (self.verify_response(self.status.ACK, reply)) {
// address needs to be transmitted as 32 bit integer, we need to bit shift each byte out and then calculate address checksum // address needs to be transmitted as 32 bit integer, we need to bit shift each byte out and then calculate address checksum
var address_arr = [(address >> 24), (address >> 16), (address >> 8), address]; const addressArray = [(address >> 24), (address >> 16), (address >> 8), address];
var address_checksum = address_arr[0] ^ address_arr[1] ^ address_arr[2] ^ address_arr[3]; const addressChecksum = addressArray[0] ^ addressArray[1] ^ addressArray[2] ^ addressArray[3];
// write start address + checksum
self.send([addressArray[0], addressArray[1], addressArray[2], addressArray[3], addressChecksum], 1, function (_reply) {
if (self.verify_response(self.status.ACK, _reply)) {
const arrayOut = Array.from(bytesToWrite + 2); // 2 byte overhead [N, ...., checksum]
arrayOut[0] = bytesToWrite - 1; // number of bytes to be written (to write 128 bytes, N must be 127, to write 256 bytes, N must be 255)
self.send([address_arr[0], address_arr[1], address_arr[2], address_arr[3], address_checksum], 1, function (reply) { // write start address + checksum let checksum = arrayOut[0];
if (self.verify_response(self.status.ACK, reply)) { for (let ii = 0; ii < bytesToWrite; ii++) {
var array_out = new Array(bytes_to_write + 2); // 2 byte overhead [N, ...., checksum] arrayOut[ii + 1] = self.hex.data[flashing_block].data[bytes_flashed]; // + 1 because of the first byte offset
array_out[0] = bytes_to_write - 1; // number of bytes to be written (to write 128 bytes, N must be 127, to write 256 bytes, N must be 255)
var checksum = array_out[0];
for (var i = 0; i < bytes_to_write; i++) {
array_out[i + 1] = self.hex.data[flashing_block].data[bytes_flashed]; // + 1 because of the first byte offset
checksum ^= self.hex.data[flashing_block].data[bytes_flashed]; checksum ^= self.hex.data[flashing_block].data[bytes_flashed];
bytes_flashed++; bytes_flashed++;
} }
array_out[array_out.length - 1] = checksum; // checksum (last byte in the array_out array) arrayOut[arrayOut.length - 1] = checksum; // checksum (last byte in the arrayOut array)
address += bytes_to_write; address += bytesToWrite;
bytes_flashed_total += bytes_to_write; bytes_flashed_total += bytesToWrite;
self.send(array_out, 1, function (reply) { self.send(arrayOut, 1, function (response) {
if (self.verify_response(self.status.ACK, reply)) { if (self.verify_response(self.status.ACK, response)) {
// flash another page // flash another page
write(); write();
} }
@ -706,48 +709,49 @@ STM32_protocol.prototype.upload_procedure = function (step) {
// start writing // start writing
write(); write();
break; break;
case 6: case 6:
// verify // verify
console.log('Verifying data ...'); console.log('Verifying data ...');
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32Verifying'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL); TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32Verifying'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL);
var blocks = self.hex.data.length - 1, blocks = self.hex.data.length - 1;
reading_block = 0, let readingBlock = 0;
address = self.hex.data[reading_block].address, address = self.hex.data[readingBlock].address;
bytes_verified = 0, let bytesVerified = 0;
bytes_verified_total = 0; // used for progress bar let bytesVerifiedTotal = 0; // used for progress bar
// initialize arrays // initialize arrays
for (var i = 0; i <= blocks; i++) { for (let i = 0; i <= blocks; i++) {
self.verify_hex.push([]); self.verify_hex.push([]);
} }
var reading = function () { const reading = function () {
if (bytes_verified < self.hex.data[reading_block].bytes) { if (bytesVerified < self.hex.data[readingBlock].bytes) {
var bytes_to_read = ((bytes_verified + 256) <= self.hex.data[reading_block].bytes) ? 256 : (self.hex.data[reading_block].bytes - bytes_verified); const bytesToRead = ((bytesVerified + 256) <= self.hex.data[readingBlock].bytes) ? 256 : (self.hex.data[readingBlock].bytes - bytesVerified);
// console.log('STM32 - Reading from: 0x' + address.toString(16) + ', ' + bytes_to_read + ' bytes'); // console.log('STM32 - Reading from: 0x' + address.toString(16) + ', ' + bytesToRead + ' bytes');
self.send([self.command.read_memory, 0xEE], 1, function (reply) { // 0x11 ^ 0xFF self.send([self.command.read_memory, 0xEE], 1, function (reply) { // 0x11 ^ 0xFF
if (self.verify_response(self.status.ACK, reply)) { if (self.verify_response(self.status.ACK, reply)) {
var address_arr = [(address >> 24), (address >> 16), (address >> 8), address]; const addressArray = [(address >> 24), (address >> 16), (address >> 8), address];
var address_checksum = address_arr[0] ^ address_arr[1] ^ address_arr[2] ^ address_arr[3]; const addressChecksum = addressArray[0] ^ addressArray[1] ^ addressArray[2] ^ addressArray[3];
self.send([address_arr[0], address_arr[1], address_arr[2], address_arr[3], address_checksum], 1, function (reply) { // read start address + checksum self.send([addressArray[0], addressArray[1], addressArray[2], addressArray[3], addressChecksum], 1, function (_reply) { // read start address + checksum
if (self.verify_response(self.status.ACK, reply)) { if (self.verify_response(self.status.ACK, _reply)) {
var bytes_to_read_n = bytes_to_read - 1; const bytesToReadN = bytesToRead - 1;
// bytes to be read + checksum XOR(complement of bytesToReadN)
self.send([bytes_to_read_n, (~bytes_to_read_n) & 0xFF], 1, function (reply) { // bytes to be read + checksum XOR(complement of bytes_to_read_n) self.send([bytesToReadN, (~bytesToReadN) & 0xFF], 1, function (response) {
if (self.verify_response(self.status.ACK, reply)) { if (self.verify_response(self.status.ACK, response)) {
self.retrieve(bytes_to_read, function (data) { self.retrieve(bytesToRead, function (data) {
for (var i = 0; i < data.length; i++) { for (const instance of data) {
self.verify_hex[reading_block].push(data[i]); self.verify_hex[readingBlock].push(instance);
} }
address += bytes_to_read; address += bytesToRead;
bytes_verified += bytes_to_read; bytesVerified += bytesToRead;
bytes_verified_total += bytes_to_read; bytesVerifiedTotal += bytesToRead;
// verify another page // verify another page
reading(); reading();
@ -756,25 +760,25 @@ STM32_protocol.prototype.upload_procedure = function (step) {
}); });
// update progress bar // update progress bar
TABS.firmware_flasher.flashProgress(Math.round((self.hex.bytes_total + bytes_verified_total) / (self.hex.bytes_total * 2) * 100)); TABS.firmware_flasher.flashProgress(Math.round((self.hex.bytes_total + bytesVerifiedTotal) / (self.hex.bytes_total * 2) * 100));
} }
}); });
} }
}); });
} else { } else {
// move to another block // move to another block
if (reading_block < blocks) { if (readingBlock < blocks) {
reading_block++; readingBlock++;
address = self.hex.data[reading_block].address; address = self.hex.data[readingBlock].address;
bytes_verified = 0; bytesVerified = 0;
reading(); reading();
} else { } else {
// all blocks read, verify // all blocks read, verify
var verify = true; let verify = true;
for (var i = 0; i <= blocks; i++) { for (let i = 0; i <= blocks; i++) {
verify = self.verify_flash(self.hex.data[i].data, self.verify_hex[i]); verify = self.verify_flash(self.hex.data[i].data, self.verify_hex[i]);
if (!verify) break; if (!verify) break;
@ -801,6 +805,7 @@ STM32_protocol.prototype.upload_procedure = function (step) {
// start reading // start reading
reading(); reading();
break; break;
case 7: case 7:
// go // go
@ -809,18 +814,19 @@ STM32_protocol.prototype.upload_procedure = function (step) {
self.send([self.command.go, 0xDE], 1, function (reply) { // 0x21 ^ 0xFF self.send([self.command.go, 0xDE], 1, function (reply) { // 0x21 ^ 0xFF
if (self.verify_response(self.status.ACK, reply)) { if (self.verify_response(self.status.ACK, reply)) {
var gt_address = 0x8000000, const gtAddress = 0x8000000;
address = [(gt_address >> 24), (gt_address >> 16), (gt_address >> 8), gt_address], address = [(gtAddress >> 24), (gtAddress >> 16), (gtAddress >> 8), gtAddress];
address_checksum = address[0] ^ address[1] ^ address[2] ^ address[3]; const addressChecksum = address[0] ^ address[1] ^ address[2] ^ address[3];
self.send([address[0], address[1], address[2], address[3], address_checksum], 1, function (reply) { self.send([address[0], address[1], address[2], address[3], addressChecksum], 1, function (response) {
if (self.verify_response(self.status.ACK, reply)) { if (self.verify_response(self.status.ACK, response)) {
// disconnect // disconnect
self.upload_procedure(99); self.upload_procedure(99);
} }
}); });
} }
}); });
break; break;
case 99: case 99:
// disconnect // disconnect
@ -847,9 +853,9 @@ STM32_protocol.prototype.cleanup = function () {
$('select[name="release"]').prop('disabled', false); $('select[name="release"]').prop('disabled', false);
// handle timing // handle timing
var timeSpent = new Date().getTime() - self.upload_time_start; const timeSpent = new Date().getTime() - self.upload_time_start;
console.log('Script finished after: ' + (timeSpent / 1000) + ' seconds'); console.log(`Script finished after: ${(timeSpent / 1000)} seconds`);
if (self.callback) { if (self.callback) {
self.callback(); self.callback();
@ -857,4 +863,4 @@ STM32_protocol.prototype.cleanup = function () {
}; };
// initialize object // initialize object
var STM32 = new STM32_protocol(); const STM32 = new STM32_protocol();

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
var ReleaseChecker = function (releaseName, releaseUrl) { const ReleaseChecker = function (releaseName, releaseUrl) {
var self = this; const self = this;
self._releaseName = releaseName; self._releaseName = releaseName;
self._releaseDataTag = `${self._releaseName}ReleaseData`; self._releaseDataTag = `${self._releaseName}ReleaseData`;