Skip to content

Commit 5cfb37e

Browse files
Add microbitBoardId type and update DeviceVersion enum.
1 parent 5aa7708 commit 5cfb37e

File tree

9 files changed

+56
-27
lines changed

9 files changed

+56
-27
lines changed

docs/quick-guide.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,19 @@ Using multiple MicroPython Intel Hex files to generate a Universal Hex:
6161
```js
6262
// Create a new FileSystem instance passing the MicroPython Intel Hex string
6363
var micropythonFs = new microbitFs.MicropythonFsHex([
64-
{ hex: uPy1HexFile, boardId: 0x9900 },
65-
{ hex: uPy2HexFile, boardId: 0x9903 },
64+
{ hex: uPy1HexFile, boardId: microbitFs.microbitBoardId.V1 },
65+
{ hex: uPy2HexFile, boardId: microbitFs.microbitBoardId.V2 },
6666
]);;
6767

6868
// Import files from a different MicroPython Intel hex file with filesystem
6969
var addedFilenames = micropythonFs.importFilesFromIntelHex(UploadedHexWithUserFiles);
7070
addedFilenames = micropythonFs.importFilesFromIntelHex(UploadedHexWithUserFiles, {overwrite: false, formatFirst: false});
7171

7272
// Generate a new Intel hex string or Uint8Array with MicroPython and the files
73-
var uPy1IntelHexStrWithFs = micropythonFs.getIntelHex(0x9900);
74-
var uPy1IntelHexBytesWithFs = micropythonFs.getIntelHexBytes(0x9900);
75-
var uPy2IntelHexStrWithFs = micropythonFs.getIntelHex(0x9903);
76-
var uPy2IntelHexBytesWithFs = micropythonFs.getIntelHexBytes(0x9903);
73+
var uPy1IntelHexStrWithFs = micropythonFs.getIntelHex(microbitFs.microbitBoardId.V1);
74+
var uPy1IntelHexBytesWithFs = micropythonFs.getIntelHexBytes(microbitFs.microbitBoardId.V1);
75+
var uPy2IntelHexStrWithFs = micropythonFs.getIntelHex(microbitFs.microbitBoardId.V2);
76+
var uPy2IntelHexBytesWithFs = micropythonFs.getIntelHexBytes(microbitFs.microbitBoardId.V2);
7777

7878
// Generate a new Universal hex string with all MicroPython+files data
7979
var universalHexStrWithFs = micropythonFs.getUniversalHex();

src/__tests__/flash-regions.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ describe('Read MicroPython UICR data.', () => {
3838
expect(result.fsStartAddress).toEqual(expectedFsStartAddress);
3939
expect(result.fsEndAddress).toEqual(expectedFsEndAddress);
4040
expect(result.uPyVersion).toEqual(expectedUpyVersion);
41-
expect(result.deviceVersion).toEqual(2);
41+
expect(result.deviceVersion).toEqual('V2');
4242
});
4343
});

src/__tests__/micropython-fs-hex.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import MemoryMap from 'nrf-intel-hex';
88
import * as microbitUh from '@microbit/microbit-universal-hex';
99

1010
import * as fsBuilder from '../micropython-fs-builder';
11-
import { MicropythonFsHex } from '../micropython-fs-hex';
11+
import { MicropythonFsHex, microbitBoardId } from '../micropython-fs-hex';
1212

1313
// MicroPython hex file for testing
1414
const uPy1HexFile = fs.readFileSync('./src/__tests__/upy-v1.0.1.hex', 'utf8');
@@ -36,7 +36,7 @@ describe('Test the class constructor', () => {
3636
const microbitFs = new MicropythonFsHex([
3737
{
3838
hex: uPy1HexFile,
39-
boardId: 0x9903,
39+
boardId: microbitBoardId.V2,
4040
},
4141
{
4242
hex: '',
@@ -64,7 +64,7 @@ describe('Test the class constructor', () => {
6464
const microbitFs = new MicropythonFsHex([
6565
{
6666
hex: uPy1HexFile,
67-
boardId: 0x9903,
67+
boardId: microbitBoardId.V2,
6868
},
6969
{
7070
hex: 'notahex',
@@ -128,8 +128,8 @@ describe('Test general read write operations', () => {
128128
testInstance(new MicropythonFsHex(uPy1HexFile));
129129
testInstance(
130130
new MicropythonFsHex([
131-
{ hex: uPy1HexFile, boardId: 0x9900 },
132-
{ hex: uPy2HexFile, boardId: 0x9903 },
131+
{ hex: uPy1HexFile, boardId: microbitBoardId.V1 },
132+
{ hex: uPy2HexFile, boardId: microbitBoardId.V2 },
133133
])
134134
);
135135
});

src/__tests__/uicr.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ describe('Read MicroPython UICR data.', () => {
3232
const expectedUPyVersion =
3333
'micro:bit v1.0.1+b0bf4a9 on 2018-12-13; ' +
3434
'MicroPython v1.9.2-34-gd64154c73 on 2017-09-01';
35-
const expectedDeviceVersion = 1;
35+
const expectedDeviceVersion = 'V1';
3636

3737
const result = getIntelHexUicrData(uPy1HexFile);
3838

@@ -72,7 +72,7 @@ describe('Read MicroPython UICR data.', () => {
7272
const expectedUPyVersion =
7373
'micro:bit v2.0.99+3e09245 on 2020-11-02; ' +
7474
'MicroPython 3e09245 on 2020-11-02';
75-
const expectedDeviceVersion = 2;
75+
const expectedDeviceVersion = 'V2';
7676

7777
const result = getIntelHexUicrData(uPy2HexFile);
7878

src/device-mem-info.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,37 @@
1+
/**
2+
* This interface represents the data about the device target contained within a
3+
* MicroPython hex file.
4+
*
5+
* This data is stored in different formats depending on the MicroPython port,
6+
* V1 uses the UICR location, and V2 uses a table stored in flash.
7+
*/
18
export interface DeviceMemInfo {
9+
/** Size of a single flash page, in bytes. */
210
flashPageSize: number;
11+
/** Full flash size in bytes. */
312
flashSize: number;
13+
/** Start address for the flash memory. */
414
flashStartAddress: number;
15+
/** End address for the flash memory. */
516
flashEndAddress: number;
17+
/** Start address in flash where the MicroPython runtime starts. */
618
runtimeStartAddress: number;
19+
/** End address in flash where the MicroPython runtime starts. */
720
runtimeEndAddress: number;
21+
/** Start address in flash where the MicroPython filesystem starts. */
822
fsStartAddress: number;
23+
/** End address in flash where the MicroPython filesystem starts. */
924
fsEndAddress: number;
25+
/** MicroPython version string. */
1026
uPyVersion: string;
11-
deviceVersion: number;
27+
/** Device targeted by this hex file. */
28+
deviceVersion: DeviceVersion;
1229
}
1330

31+
/**
32+
* Represents the micro:bit Version the hex file targets.
33+
*/
1434
export const enum DeviceVersion {
15-
one = 1,
16-
two = 2,
35+
V1 = 'V1',
36+
V2 = 'V2',
1737
}

src/flash-regions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ function getHexMapFlashRegionsData(iHexMap: MemoryMap): DeviceMemInfo {
329329
fsStartAddress,
330330
fsEndAddress,
331331
uPyVersion,
332-
deviceVersion: DeviceVersion.two,
332+
deviceVersion: DeviceVersion.V2,
333333
};
334334
}
335335

src/micropython-fs-builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ function getEndAddress(intelHexMap: MemoryMap): number {
173173
let endAddress = deviceMem.fsEndAddress;
174174
// TODO: Maybe we should move this inside the UICR module to calculate
175175
// the real fs area in that step
176-
if (deviceMem.deviceVersion === DeviceVersion.one) {
176+
if (deviceMem.deviceVersion === DeviceVersion.V1) {
177177
if (isAppendedScriptPresent(intelHexMap)) {
178178
endAddress = AppendedBlock.StartAdd;
179179
}

src/micropython-fs-hex.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,21 @@ import {
1818
import { SimpleFile } from './simple-file';
1919
import { areUint8ArraysEqual } from './common';
2020

21+
/**
22+
* The Board ID is used to identify the different targets from a Universal Hex.
23+
* In this case the target represents a micro:bit version.
24+
* For micro:bit V1 (v1.3, v1.3B and v1.5) the `boardId` is `0x9900`, and for
25+
* V2 `0x9903`.
26+
* This is being re-exported from the @microbit/microbit-universal-hex package.
27+
*/
28+
export import microbitBoardId = microbitUh.microbitBoardId;
29+
2130
/**
2231
* Extends the interface from microbit-fs-building to include the board ID that
2332
* corresponds to each of the cached objects.
2433
*/
2534
interface MpFsBuilderCacheWithId extends MpFsBuilderCache {
26-
boardId: number;
35+
boardId: number | microbitBoardId;
2736
}
2837

2938
/**
@@ -33,7 +42,7 @@ export interface IntelHexWithId {
3342
/** Intel Hex string */
3443
hex: string;
3544
/** Board ID to identify the Intel Hex and encode inside the Universal Hex */
36-
boardId: number;
45+
boardId: number | microbitBoardId;
3746
}
3847

3948
/**
@@ -456,7 +465,7 @@ export class MicropythonFsHex implements FsInterface {
456465
*
457466
* @returns A new string with MicroPython and the filesystem included.
458467
*/
459-
getIntelHex(boardId?: number): string {
468+
getIntelHex(boardId?: number | microbitBoardId): string {
460469
if (this.getStorageRemaining() < 0) {
461470
throw new Error('There is no storage space left.');
462471
}
@@ -499,7 +508,7 @@ export class MicropythonFsHex implements FsInterface {
499508
*
500509
* @returns A Uint8Array with MicroPython and the filesystem included.
501510
*/
502-
getIntelHexBytes(boardId?: number): Uint8Array {
511+
getIntelHexBytes(boardId?: number | microbitBoardId): Uint8Array {
503512
if (this.getStorageRemaining() < 0) {
504513
throw new Error('There is no storage space left.');
505514
}

src/uicr.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ import { DeviceVersion } from './hex-mem-info';
1515

1616
const DEVICE_INFO = [
1717
{
18-
deviceVersion: DeviceVersion.one,
18+
deviceVersion: DeviceVersion.V1,
1919
magicHeader: 0x17eeb07c,
2020
flashSize: 256 * 1024,
2121
fsEnd: 256 * 1024,
2222
},
2323
{
24-
deviceVersion: DeviceVersion.two,
24+
deviceVersion: DeviceVersion.V2,
2525
magicHeader: 0x47eeb07c,
2626
flashSize: 512 * 1024,
2727
fsEnd: 0x73000,
@@ -99,7 +99,7 @@ function getMagicValue(intelHexMap: MemoryMap): number {
9999
* @param intelHexMap - Memory map of the Intel Hex data.
100100
* @returns The micro:bit board version.
101101
*/
102-
function getDeviceVersion(intelHexMap: MemoryMap): number {
102+
function getDeviceVersion(intelHexMap: MemoryMap): DeviceVersion {
103103
const readMagicHeader = getMagicValue(intelHexMap);
104104
for (const device of DEVICE_INFO) {
105105
if (device.magicHeader === readMagicHeader) {
@@ -214,7 +214,7 @@ function getHexMapUicrData(intelHexMap: MemoryMap): MicropythonUicrData {
214214
const runtimeEndAddress: number = pagesUsed * flashPageSize;
215215
const versionAddress: number = getVersionLocation(uicrMap);
216216
const uPyVersion: string = hexMapUtil.getString(intelHexMap, versionAddress);
217-
const deviceVersion: number = getDeviceVersion(uicrMap);
217+
const deviceVersion: DeviceVersion = getDeviceVersion(uicrMap);
218218
const fsEndAddress: number = getFsEndAddress(uicrMap);
219219

220220
return {

0 commit comments

Comments
 (0)