betaflight-configurator/workers/hex_parser.js

120 lines
5.2 KiB
JavaScript
Raw Normal View History

2013-11-13 08:13:24 +00:00
// input = string
2013-11-15 15:17:25 +00:00
// result = if hex file is valid, result is an object
// if hex file wasn't valid (crc check failed on any of the lines), result will be false
2013-11-13 08:13:24 +00:00
function read_hex_file(data) {
data = data.split("\n");
// check if there is an empty line in the end of hex file, if there is, remove it
if (data[data.length - 1] == "") {
data.pop();
}
var hexfile_valid = true; // if any of the crc checks failed, this variable flips to false
2013-11-15 15:17:25 +00:00
var result = {
data: [],
end_of_file: false,
2013-11-16 18:14:34 +00:00
extended_linear_address: [],
2013-11-15 15:17:25 +00:00
start_linear_address: 0,
bytes: 0
};
2013-11-17 08:20:04 +00:00
var next_address_pos = 0;
2013-11-16 20:10:56 +00:00
2013-11-13 08:13:24 +00:00
for (var i = 0; i < data.length; i++) {
2013-11-16 19:32:32 +00:00
var byte_count = parseInt(data[i].substr(1, 2), 16); // each byte is represnted by two chars
2013-11-13 08:13:24 +00:00
var address = data[i].substr(3, 4);
var record_type = parseInt(data[i].substr(7, 2), 16); // also converting from hex to decimal
2013-11-16 19:32:32 +00:00
var content = data[i].substr(9, byte_count * 2);
var checksum = parseInt(data[i].substr(9 + byte_count * 2, 2), 16); // also converting from hex to decimal (this is a 2's complement value)
2013-11-13 08:13:24 +00:00
2013-11-15 15:17:25 +00:00
switch (record_type) {
case 0x00: // data record
2013-11-16 20:10:56 +00:00
// fix "holes" if there are any
2013-11-17 08:20:04 +00:00
if (parseInt(address, 16) != next_address_pos) {
var difference = parseInt(address, 16) - (next_address_pos);
2013-11-16 20:10:56 +00:00
// fill in the difference
2013-11-17 08:20:04 +00:00
for (var x = 0; x < difference; x++) {
2013-11-16 20:10:56 +00:00
result.data.push(0xFF);
result.bytes++;
}
2013-11-17 08:20:04 +00:00
console.log('HEX_PARSER - Address hole detected, expected: ' + next_address_pos + ', received: ' + parseInt(address, 16) + ', filling: ' + difference + ' bytes');
2013-11-16 20:10:56 +00:00
}
2013-11-17 08:20:04 +00:00
// update for next comparison
next_address_pos = parseInt(address, 16) + byte_count;
2013-11-16 20:10:56 +00:00
// process data
2013-11-16 19:33:58 +00:00
var crc = byte_count + parseInt(address.substr(0, 2), 16) + parseInt(address.substr(2, 2), 16) + record_type;
for (var needle = 0; needle < byte_count * 2; needle += 2) {
var num = parseInt(content.substr(needle, 2), 16); // get one byte in hex and convert it to decimal
result.data.push(num);
2013-11-13 10:48:45 +00:00
2013-11-16 19:33:58 +00:00
crc += num;
result.bytes++;
}
// change crc to 2's complement (same as checksum)
crc = ~crc + 1;
crc &= 0xFF;
// verify
if (crc != checksum) {
hexfile_valid = false;
2013-11-15 15:17:25 +00:00
2013-11-16 19:33:58 +00:00
// break out of the for loop as crc is wrong anyway, we dont need to process any more data
i = data.length;
2013-11-13 10:48:45 +00:00
}
2013-11-15 15:17:25 +00:00
break;
case 0x01: // end of file record
result.end_of_file = true;
break;
case 0x02: // extended segment address record
// not implemented
2013-11-16 18:14:34 +00:00
console.log('extended segment address record found - NOT IMPLEMENTED !!!');
2013-11-15 15:17:25 +00:00
break;
case 0x03: // start segment address record
// not implemented
2013-11-16 18:14:34 +00:00
console.log('start segment address record found - NOT IMPLEMENTED !!!');
2013-11-15 15:17:25 +00:00
break;
2013-11-16 20:10:56 +00:00
case 0x04: // extended linear address record
var extended_linear_address = (parseInt(content.substr(0, 2), 16) << 24) | parseInt(content.substr(2, 2), 16) << 16;
result.extended_linear_address.push(extended_linear_address);
2013-11-17 08:20:04 +00:00
if (next_address_pos != 0) { // dont execute the first time
extended_linear_address -= 0x08000000;
var difference = extended_linear_address - next_address_pos;
2013-11-16 20:10:56 +00:00
2013-11-17 08:20:04 +00:00
// fill in the difference
for (var x = 0; x < difference; x++) {
2013-11-16 20:10:56 +00:00
result.data.push(0xFF);
result.bytes++;
}
2013-11-17 08:20:04 +00:00
if (difference > 0) console.log('HEX_PARSER - Address hole detected (changing linear address), expected: ' + next_address_pos + ', received: ' + extended_linear_address + ', filling: ' + difference + ' bytes');
2013-11-16 20:10:56 +00:00
// reset some variables
2013-11-17 08:20:04 +00:00
next_address_pos = 0;
2013-11-16 20:10:56 +00:00
}
2013-11-15 15:17:25 +00:00
break;
case 0x05: // start linear address record
result.start_linear_address = (parseInt(content.substr(0, 2), 16) << 24) | (parseInt(content.substr(2, 2), 16) << 16) | (parseInt(content.substr(4, 2), 16) << 8) | parseInt(content.substr(6, 2), 16);
break;
2013-11-13 08:13:24 +00:00
}
}
if (result.end_of_file && hexfile_valid) {
postMessage(result);
2013-11-13 08:13:24 +00:00
} else {
postMessage(false);
2013-11-13 08:13:24 +00:00
}
}
onmessage = function(event) {
read_hex_file(event.data);
// terminate worker
close();
2013-11-13 08:13:24 +00:00
}