Skip to content

Commit a418ade

Browse files
appended: Move common.cleanseOldHexFormat() here and add tests.
1 parent aeed894 commit a418ade

File tree

4 files changed

+69
-59
lines changed

4 files changed

+69
-59
lines changed

src/__tests__/micropython-appended.spec.ts

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@ const simpleIntelHex: string =
1515
':1000000000400020ED530100295401002B54010051\n' +
1616
':00000001FF\n';
1717

18+
const pyCode = 'from microbit import *\n' + "display.scroll('Hello, World!')";
19+
const pyCodeHex =
20+
':020000040003F7\n' +
21+
':10E000004D50360066726F6D206D6963726F626984\n' +
22+
':10E010007420696D706F7274202A0A646973706C61\n' +
23+
':10E0200061792E7363726F6C6C282748656C6C6F16\n' +
24+
':10E030002C20576F726C642127290000000000001B';
25+
26+
const marker = ':::::::::::::::::::::::::::::::::::::::::::';
27+
1828
describe('Inject Python code into Intel Hex string', () => {
1929
it('Inject Python code into an Intel Hex string', () => {
20-
const pyCode =
21-
'from microbit import *\n' + "display.scroll('Hello, World!')";
22-
const pyCodeHex =
23-
':020000040003F7\n' +
24-
':10E000004D50360066726F6D206D6963726F626984\n' +
25-
':10E010007420696D706F7274202A0A646973706C61\n' +
26-
':10E0200061792E7363726F6C6C282748656C6C6F16\n' +
27-
':10E030002C20576F726C642127290000000000001B';
28-
2930
const output: string = addIntelHexAppendedScript(simpleIntelHex, pyCode);
3031

3132
const fullHex: string[] = simpleIntelHex.split('\n');
@@ -34,14 +35,6 @@ describe('Inject Python code into Intel Hex string', () => {
3435
});
3536

3637
it('Inject Python with present UICR and Start Linear Address record', () => {
37-
const pyCode: string =
38-
'from microbit import *\n' + "display.scroll('Hello, World!')";
39-
const pyCodeHex: string =
40-
':020000040003F7\n' +
41-
':10E000004D50360066726F6D206D6963726F626984\n' +
42-
':10E010007420696D706F7274202A0A646973706C61\n' +
43-
':10E0200061792E7363726F6C6C282748656C6C6F16\n' +
44-
':10E030002C20576F726C642127290000000000001B\n';
4538
const uicr: string =
4639
':020000041000EA\n' +
4740
':1010C0007CB0EE17FFFFFFFF0A0000000000E30006\n' +
@@ -57,10 +50,30 @@ describe('Inject Python code into Intel Hex string', () => {
5750

5851
const expectedHex: string[] = simpleIntelHex.split('\n');
5952
// Note that the 05 record is removed by nrf-intel-hex library!
60-
expectedHex.splice(2, 0, pyCodeHex + uicr);
53+
expectedHex.splice(2, 0, pyCodeHex + '\n' + uicr);
6154
expect(output).toEqual(expectedHex.join('\n'));
6255
});
6356

57+
it('Inject Python in a hex with a marker', () => {
58+
const fullHexWithMarker: string[] = simpleIntelHex.split('\n');
59+
fullHexWithMarker.splice(2, 0, marker);
60+
const fullHexWithout: string[] = simpleIntelHex.split('\n');
61+
const expectedHex: string[] = simpleIntelHex.split('\n');
62+
expectedHex.splice(2, 0, pyCodeHex);
63+
64+
const outputWithMarker: string = addIntelHexAppendedScript(
65+
fullHexWithMarker.join('\n'),
66+
pyCode
67+
);
68+
const outputWithout: string = addIntelHexAppendedScript(
69+
fullHexWithout.join('\n'),
70+
pyCode
71+
);
72+
73+
expect(outputWithMarker).toEqual(expectedHex.join('\n'));
74+
expect(outputWithMarker).toEqual(outputWithout);
75+
});
76+
6477
it('Fail to inject Python code too large for flash', () => {
6578
const failCase = () => {
6679
const fakeCode: string = new Array(8 * 1024 + 2).join('a');
@@ -75,9 +88,15 @@ describe('Inject Python code into Intel Hex string', () => {
7588

7689
describe('Extract Python code from Intel Hex string', () => {
7790
it('Extract Python code', () => {
78-
const pyCode: string =
79-
'from microbit import *\n' + "display.scroll('Hello, World!')";
80-
const intelHex: string =
91+
const intelHex1: string =
92+
':020000040000FA\n' +
93+
':1000000000400020ED530100295401002B54010051\n' +
94+
':020000040003F7\n' +
95+
pyCodeHex +
96+
'\n' +
97+
':00000001FF\n';
98+
// pyCodeHex contains zeros to fill the record, this example doesn't
99+
const intelHex2: string =
81100
':020000040000FA\n' +
82101
':1000000000400020ED530100295401002B54010051\n' +
83102
':020000040003F7\n' +
@@ -87,14 +106,14 @@ describe('Extract Python code from Intel Hex string', () => {
87106
':0AE030002C20576F726C6421272921\n' +
88107
':00000001FF\n';
89108

90-
const result: string = getIntelHexAppendedScript(intelHex);
109+
const result1: string = getIntelHexAppendedScript(intelHex1);
110+
const result2: string = getIntelHexAppendedScript(intelHex2);
91111

92-
expect(result).toEqual(pyCode);
112+
expect(result1).toEqual(pyCode);
113+
expect(result2).toEqual(pyCode);
93114
});
94115

95116
it('Extract Python code with present UICR and Start Linear Address record)', () => {
96-
const pyCode: string =
97-
'from microbit import *\n' + "display.scroll('Hello, World!')";
98117
const intelHex: string =
99118
':020000040000FA\n' +
100119
':1000000000400020ED530100295401002B54010051\n' +

src/common.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,6 @@
66
*/
77
import { TextDecoderLite, TextEncoderLite } from 'text-encoder-lite';
88

9-
/**
10-
* Marker placed inside the MicroPython hex string to indicate where to
11-
* inject the user Python Code.
12-
*/
13-
const HEX_INSERTION_POINT = ':::::::::::::::::::::::::::::::::::::::::::\n';
14-
15-
/**
16-
* Removes the old insertion line the input Intel Hex string contains it.
17-
*
18-
* @param intelHex - String with the intel hex lines.
19-
* @returns The Intel Hex string without insertion line.
20-
*/
21-
export function cleanseOldHexFormat(intelHex: string): string {
22-
return intelHex.replace(HEX_INSERTION_POINT, '');
23-
}
24-
259
/**
2610
* Converts a string into a byte array of characters.
2711
* @param str - String to convert to bytes.

src/micropython-appended.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
import MemoryMap from 'nrf-intel-hex';
88

9-
import { bytesToStr, cleanseOldHexFormat, strToBytes } from './common';
9+
import { bytesToStr, strToBytes } from './common';
1010

1111
/** User script located at specific flash address. */
1212
enum AppendedBlock {
@@ -31,6 +31,22 @@ const HEADER_START_BYTE_1 = 80; // 'P'
3131
/** How many bytes per Intel Hex record line. */
3232
const HEX_RECORD_DATA_LEN = 16;
3333

34+
/**
35+
* Marker placed inside the MicroPython hex string to indicate where to
36+
* inject the user Python Code.
37+
*/
38+
const HEX_INSERTION_POINT = ':::::::::::::::::::::::::::::::::::::::::::\n';
39+
40+
/**
41+
* Removes the old insertion line the input Intel Hex string contains it.
42+
*
43+
* @param intelHex - String with the intel hex lines.
44+
* @returns The Intel Hex string without insertion line.
45+
*/
46+
export function cleanseOldHexFormat(intelHex: string): string {
47+
return intelHex.replace(HEX_INSERTION_POINT, '');
48+
}
49+
3450
/**
3551
* Parses through an Intel Hex string to find the Python code at the
3652
* allocated address and extracts it.

src/micropython-fs-builder.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,7 @@
3333
*/
3434
import MemoryMap from 'nrf-intel-hex';
3535

36-
import {
37-
bytesToStr,
38-
cleanseOldHexFormat,
39-
concatUint8Array,
40-
strToBytes,
41-
} from './common';
36+
import { bytesToStr, concatUint8Array, strToBytes } from './common';
4237
import { AppendedBlock, isAppendedScriptPresent } from './micropython-appended';
4338
import { getHexMapUicrData } from './uicr';
4439

@@ -368,8 +363,7 @@ function addIntelHexFile(
368363
data: Uint8Array
369364
): string {
370365
// filename and data checked in addMemMapFile
371-
const intelHexClean = cleanseOldHexFormat(intelHex);
372-
let intelHexMap: MemoryMap = MemoryMap.fromHex(intelHexClean);
366+
let intelHexMap: MemoryMap = MemoryMap.fromHex(intelHex);
373367
intelHexMap = addMemMapFile(intelHexMap, filename, data);
374368
return intelHexMap.asHexString() + '\n';
375369
}
@@ -393,8 +387,7 @@ function addIntelHexFiles(
393387
files: { [filename: string]: Uint8Array },
394388
returnBytes: boolean = false
395389
): string | Uint8Array {
396-
const intelHexClean = cleanseOldHexFormat(intelHex);
397-
let intelHexMap: MemoryMap = MemoryMap.fromHex(intelHexClean);
390+
let intelHexMap: MemoryMap = MemoryMap.fromHex(intelHex);
398391
Object.keys(files).forEach((filename) => {
399392
intelHexMap = addMemMapFile(intelHexMap, filename, files[filename]);
400393
});
@@ -418,17 +411,16 @@ function addIntelHexFiles(
418411
function getIntelHexFiles(
419412
intelHex: string
420413
): { [filename: string]: Uint8Array } {
421-
const intelHexClean = cleanseOldHexFormat(intelHex);
422-
const hexMap: MemoryMap = MemoryMap.fromHex(intelHexClean);
414+
const hexMap: MemoryMap = MemoryMap.fromHex(intelHex);
423415
const startAddress: number = getStartAddress(hexMap);
424416
const endAddress: number = getLastPageAddress(hexMap);
425417

426418
// TODO: endAddress as the getLastPageAddress works now because this
427419
// library uses the last page as the "persistent" page, so the filesystem does
428420
// end there. In reality, the persistent page could be the first or the last
429421
// page, so we should get the end address as the magnetometer page and then
430-
// check if the persistent marker is present in the first of last page and take that
431-
// into account in the memory range calculation.
422+
// check if the persistent marker is present in the first of last page and
423+
// take that into account in the memory range calculation.
432424
// Note that the persistent marker is only present at the top of the page
433425

434426
// Iterate through the filesystem to collect used chunks and file starts
@@ -518,11 +510,10 @@ function getIntelHexFiles(
518510
* @returns Size of the filesystem in bytes.
519511
*/
520512
function getIntelHexFsSize(intelHex: string): number {
521-
const intelHexClean = cleanseOldHexFormat(intelHex);
522-
const intelHexMap: MemoryMap = MemoryMap.fromHex(intelHexClean);
513+
const intelHexMap: MemoryMap = MemoryMap.fromHex(intelHex);
523514
const startAddress: number = getStartAddress(intelHexMap);
524515
const endAddress = getEndAddress(intelHexMap);
525-
// Remember that one page is used as persistent page
516+
// One extra page is used as persistent page
526517
return endAddress - startAddress - FLASH_PAGE_SIZE;
527518
}
528519

0 commit comments

Comments
 (0)