Skip to content

Commit ee7081d

Browse files
append-script: Add padding to Intel Hex record for old DAPLink compatibility
1 parent 4d5fc04 commit ee7081d

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/__tests__/appended-script.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('Inject Python code into Intel Hex string', () => {
1717
':10E000004D50360066726F6D206D6963726F626984\n' +
1818
':10E010007420696D706F7274202A0A646973706C61\n' +
1919
':10E0200061792E7363726F6C6C282748656C6C6F16\n' +
20-
':0AE030002C20576F726C6421272921';
20+
':10E030002C20576F726C642127290000000000001B';
2121

2222
const output: string = appendScriptToIntelHex(simpleIntelHex, pyCode);
2323

@@ -34,7 +34,7 @@ describe('Inject Python code into Intel Hex string', () => {
3434
':10E000004D50360066726F6D206D6963726F626984\n' +
3535
':10E010007420696D706F7274202A0A646973706C61\n' +
3636
':10E0200061792E7363726F6C6C282748656C6C6F16\n' +
37-
':0AE030002C20576F726C6421272921\n';
37+
':10E030002C20576F726C642127290000000000001B\n';
3838
const uicr: string =
3939
':020000041000EA\n' +
4040
':1010C0007CB0EE17FFFFFFFF0A0000000000E30006\n' +

src/appended-script.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const enum UserCodeBlock {
2828
* paste the Python Code
2929
*/
3030
const HEX_INSERTION_POINT = ':::::::::::::::::::::::::::::::::::::::::::\n';
31+
const HEX_RECORD_DATA_LEN = 16;
3132

3233
/**
3334
* Removes the old insertion line the input Intel Hex string contains it.
@@ -76,10 +77,11 @@ function getScriptFromIntelHex(intelHex: string): string {
7677
* @returns Byte array with the full User Code Block.
7778
*/
7879
function createUserCodeBlock(dataBytes: Uint8Array): Uint8Array {
80+
let blockLength = dataBytes.length + UserCodeBlock.HeaderLength;
81+
// Old DAPLink versions need padding on the last record to fill the line
82+
blockLength += HEX_RECORD_DATA_LEN - (blockLength % HEX_RECORD_DATA_LEN);
83+
const blockBytes: Uint8Array = new Uint8Array(blockLength).fill(0x00);
7984
// The user script block has to start with "MP" marker + script length
80-
const blockBytes: Uint8Array = new Uint8Array(
81-
dataBytes.length + UserCodeBlock.HeaderLength
82-
);
8385
blockBytes[0] = UserCodeBlock.HeaderStartByte0;
8486
blockBytes[1] = UserCodeBlock.HeaderStartByte1;
8587
blockBytes[2] = dataBytes.length & 0xff;

0 commit comments

Comments
 (0)