1
1
/**
2
- * Converts standard Intel Hex strings into a Universal Hex with records
3
- * organised in self contained 512-byte blocks.
2
+ * Convert between standard Intel Hex strings and Universal Hex strings.
3
+ *
4
+ * This module provides the main functionality to convert Intel Hex strings
5
+ * (with their respective Board IDs) into the Universal Hex format.
6
+ *
7
+ * It can also separate a Universal Hex string into the individual Intel Hex
8
+ * strings that forms it.
9
+ *
10
+ * The content here assumes familiarity with the
11
+ * [Universal Hex Specification](https://github.com/microbit-foundation/spec-universal-hex)
12
+ * and the rest of
13
+ * [this library documentation](https://microbit-foundation.github.io/microbit-universal-hex/).
14
+ * @packageDocumentation
4
15
*
5
16
* (c) 2020 Micro:bit Educational Foundation and the project contributors.
6
17
* SPDX-License-Identifier: MIT
@@ -10,17 +21,43 @@ import * as ihex from './ihex';
10
21
const V1_BOARD_IDS = [ 0x9900 , 0x9901 ] ;
11
22
const BLOCK_SIZE = 512 ;
12
23
24
+ /**
25
+ * The Board ID is used to identify the different targets from a Universal Hex.
26
+ * In this case the target represents a micro:bit version.
27
+ * For micro:bit V1 (v1.3, v1.3B and v1.5) the `boardId` is `0x9900`, and for
28
+ * V2 `0x9903`.
29
+ */
30
+ enum microbitBoardId {
31
+ V1 = 0x9900 ,
32
+ V2 = 0x9903 ,
33
+ }
34
+
35
+ /**
36
+ * Very simple interface to group an Intel Hex string with a Board ID.
37
+ *
38
+ * The Board ID is used in an Universal Hex file to identify the target, in this
39
+ * case the micro:bit version.
40
+ */
13
41
interface IndividualHex {
42
+ /** The Intel Hex in string format. */
14
43
hex : string ;
15
- boardId : number ;
44
+ /**
45
+ * The Board ID identifies the target for the Intel Hex string.
46
+ * Any number can be used in this field, but to target a micro:bit the
47
+ * micro:bit firmware will process the data
48
+ */
49
+ boardId : number | microbitBoardId ;
16
50
}
17
51
18
52
/**
19
- * Converts an Intel Hex file string into a Universal Hex ready hex string using
20
- * custom records and 512 byte blocks .
53
+ * Converts an Intel Hex string into a Hex string using the 512 byte blocks
54
+ * format and the Universal Hex specific record types .
21
55
*
22
- * More information on the format:
23
- * https://github.com/microbit-foundation/universal-hex
56
+ * The output of this function is not a fully formed Universal Hex, but one part
57
+ * of a Universal Hex, ready to be merged by the calling code.
58
+ *
59
+ * More information on this "block" format:
60
+ * https://github.com/microbit-foundation/spec-universal-hex
24
61
*
25
62
* @throws {Error } When the Board ID is not between 0 and 2^16.
26
63
* @throws {Error } When there is an EoF record not at the end of the file.
@@ -29,7 +66,10 @@ interface IndividualHex {
29
66
* byte blocks and the customer records.
30
67
* @returns New Intel Hex string with the custom format.
31
68
*/
32
- function iHexToCustomFormatBlocks ( iHexStr : string , boardId : number ) : string {
69
+ function iHexToCustomFormatBlocks (
70
+ iHexStr : string ,
71
+ boardId : number | microbitBoardId
72
+ ) : string {
33
73
// Hex files for v1.3 and v1.5 continue using the normal Data Record Type
34
74
const replaceDataRecord = ! V1_BOARD_IDS . includes ( boardId ) ;
35
75
@@ -121,11 +161,14 @@ function iHexToCustomFormatBlocks(iHexStr: string, boardId: number): string {
121
161
}
122
162
123
163
/**
124
- * Converts an Intel Hex file string into a Universal Hex ready hex string using
125
- * custom records and sections aligned with 512-byte boundaries.
164
+ * Converts an Intel Hex string into a Hex string using custom records and
165
+ * aligning the content size to a 512-byte boundary.
166
+ *
167
+ * The output of this function is not a fully formed Universal Hex, but one part
168
+ * of a Universal Hex, ready to be merged by the calling code.
126
169
*
127
- * More information on the format:
128
- * https://github.com/microbit-foundation/universal-hex
170
+ * More information on this "section" format:
171
+ * https://github.com/microbit-foundation/spec- universal-hex
129
172
*
130
173
* @throws {Error } When the Board ID is not between 0 and 2^16.
131
174
* @throws {Error } When there is an EoF record not at the end of the file.
@@ -134,7 +177,10 @@ function iHexToCustomFormatBlocks(iHexStr: string, boardId: number): string {
134
177
* byte blocks and the customer records.
135
178
* @returns New Intel Hex string with the custom format.
136
179
*/
137
- function iHexToCustomFormatSection ( iHexStr : string , boardId : number ) : string {
180
+ function iHexToCustomFormatSection (
181
+ iHexStr : string ,
182
+ boardId : number | microbitBoardId
183
+ ) : string {
138
184
const sectionLines : string [ ] = [ ] ;
139
185
let sectionLen = 0 ;
140
186
let ih = 0 ;
@@ -214,13 +260,19 @@ function iHexToCustomFormatSection(iHexStr: string, boardId: number): string {
214
260
}
215
261
216
262
/**
217
- * Creates a Universal Hex from an collection of Intel Hex strings and their
263
+ * Creates a Universal Hex from a collection of Intel Hex strings and their
218
264
* board IDs.
219
265
*
220
- * @param hexes An array of objects containing an Intel Hex strings and the
221
- * board ID associated with it.
222
- * @param blocks Indicate if the Universal Hex should be blocks instead of
223
- * sections.
266
+ * For the current micro:bit board versions use the values from the
267
+ * `microbitBoardId` enum.
268
+ *
269
+ * @param hexes An array of objects containing an Intel Hex string and the board
270
+ * ID associated with it.
271
+ * @param blocks Indicate if the Universal Hex format should be "blocks"
272
+ * instead of "sections". The current specification recommends using the
273
+ * default "sections" format as is much quicker in micro:bits with DAPLink
274
+ * version 0234.
275
+ * @returns A Universal Hex string.
224
276
*/
225
277
function createUniversalHex ( hexes : IndividualHex [ ] , blocks = false ) : string {
226
278
if ( ! hexes . length ) return '' ;
@@ -257,8 +309,9 @@ function createUniversalHex(hexes: IndividualHex[], blocks = false): string {
257
309
* Very simple test only checking for the opening Extended Linear Address and
258
310
* Block Start records.
259
311
*
260
- * The string is manually checked as this method can be x20 faster than breaking
261
- * the string into records and checking their types with the ihex functions.
312
+ * The string is manually iterated as this method can be x20 faster than
313
+ * breaking the string into records and checking their types with the ihex
314
+ * functions.
262
315
*
263
316
* @param hexStr Hex string to check
264
317
* @return True if the hex is an Universal Hex.
@@ -281,7 +334,7 @@ function isUniversalHex(hexStr: string): boolean {
281
334
}
282
335
283
336
/**
284
- * Separates a Universal Hex into the individual hexes .
337
+ * Separates a Universal Hex into its individual Intel Hexes .
285
338
*
286
339
* @param universalHexStr Universal Hex string with the Universal Hex.
287
340
* @returns An array of object with boardId and hex keys.
@@ -367,6 +420,8 @@ function separateUniversalHex(universalHexStr: string): IndividualHex[] {
367
420
}
368
421
369
422
export {
423
+ microbitBoardId ,
424
+ IndividualHex ,
370
425
iHexToCustomFormatBlocks ,
371
426
iHexToCustomFormatSection ,
372
427
createUniversalHex ,
0 commit comments