From 5e3a79d308b355f52032f7c73af8fc14ab36e84a Mon Sep 17 00:00:00 2001 From: gouthamsk98 Date: Fri, 15 Nov 2024 16:25:34 +0530 Subject: [PATCH] update:intel hex read changes --- src/connection.ts | 2 +- src/flasherV2/loader_handler.ts | 2 +- src/flasherV2/protocol_handler.ts | 10 +++++ src/flasherV2/transport_handler.ts | 71 ++++++++++++++++++++++++------ 4 files changed, 70 insertions(+), 15 deletions(-) diff --git a/src/connection.ts b/src/connection.ts index 287c156..eff27ee 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -93,7 +93,7 @@ function readFileAsText(file: File): Promise { const reader = new FileReader(); reader.onload = () => resolve(reader.result as string); reader.onerror = reject; - reader.readAsText(file); + reader.readAsText(file, "utf-8"); }); } export function readFile(element: HTMLInputElement) { diff --git a/src/flasherV2/loader_handler.ts b/src/flasherV2/loader_handler.ts index 6e81b89..b1aecb0 100644 --- a/src/flasherV2/loader_handler.ts +++ b/src/flasherV2/loader_handler.ts @@ -238,7 +238,7 @@ export class MSPLoaderV2 extends SerialTransport { 0, false ); - const raw = this.intelHexToUint8Array(hex); + const raw = await this.readIHex(hex); let address = 0x00000000; //this.FLASH_START_ADDRESS; const cmd: BSLCommand = { type: "ProgramData", diff --git a/src/flasherV2/protocol_handler.ts b/src/flasherV2/protocol_handler.ts index c6f68a4..645a42c 100644 --- a/src/flasherV2/protocol_handler.ts +++ b/src/flasherV2/protocol_handler.ts @@ -1,3 +1,13 @@ +export interface Section { + offset: number; + value: Uint8Array; +} +export interface IHexRecord { + type: string; + offset: number; + data: Uint8Array; + address: number; +} export enum OLEDPOS { ALIGN_DEFAULT = 0, ALIGN_TOP_LEFT = 1, diff --git a/src/flasherV2/transport_handler.ts b/src/flasherV2/transport_handler.ts index a9dc176..d94d409 100644 --- a/src/flasherV2/transport_handler.ts +++ b/src/flasherV2/transport_handler.ts @@ -1,3 +1,4 @@ +import { Section, IHexRecord } from "./protocol_handler"; export class SerialTransport { baudrate = 9600; buffer_size = 1024 * 1024; //1MB (max can be 16MB) @@ -232,22 +233,66 @@ export class SerialTransport { writer.releaseLock(); } } - intelHexToUint8Array(hexString: string) { - const lines = hexString.trim().split("\n"); - const data: Array = []; - lines.forEach((line) => { - if (line.startsWith(":")) { - const byteCount = parseInt(line.substr(1, 2), 16); - const dataStartIndex = 9; // Data starts after 9 characters (: + 2-byte count + 4-byte address + 2-byte record type) - const dataEndIndex = dataStartIndex + byteCount * 2; + private mergeSections(sections: Section[]): Uint8Array { + sections.sort((a, b) => a.offset - b.offset); // order by start address + + const startAddress = sections[0].offset; + const endAddress = + sections[sections.length - 1].offset + + sections[sections.length - 1].value.length; + + const totalSize = endAddress - startAddress; + + const binary = new Uint8Array(totalSize); + // FIXME: check section overlap? + for (const section of sections) { + const sectStart = section.offset - startAddress; + binary.set(section.value, sectStart); + } + return binary; + } + async readIHex(data: string): Promise { + console.log("read intel hex"); - for (let i = dataStartIndex; i < dataEndIndex; i += 2) { - data.push(parseInt(line.substr(i, 2), 16)); + const records: Section[] = []; + let baseAddress = 0; + + const lines = data.split("\n"); + for (const line of lines) { + if (line.startsWith(":")) { + const record = this.parseIHexRecord(line); + switch (record.type) { + case "00": // Data + const offset = baseAddress + record.offset; + records.push({ offset, value: record.data }); + break; + case "01": // End Of File + break; + case "02": // Extended Segment Address + baseAddress = record.address * 16; + break; + case "03": // Start Segment Address + break; + case "04": // Extended Linear Address + baseAddress = record.address << 16; + break; + case "05": // Start Linear Address + break; } } - }); - - return new Uint8Array(data); + } + return this.mergeSections(records); + } + parseIHexRecord(line: string): IHexRecord { + const length = parseInt(line.substr(1, 2), 16); + const offset = parseInt(line.substr(3, 4), 16); + const type = line.substr(7, 2); + const data = new Uint8Array(length); + for (let i = 0; i < length; i++) { + data[i] = parseInt(line.substr(9 + i * 2, 2), 16); + } + const address = parseInt(line.substr(9, 4), 16); + return { type, offset, data, address }; } async receive(timeout = 0) { if (this.leftOver.length != 0) {