Skip to content

Commit 2047f12

Browse files
WebUSB: iHex flashing whenever it fallsback to full flash.
1 parent e158bcd commit 2047f12

5 files changed

Lines changed: 21 additions & 17 deletions

File tree

js/fs.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ var microbitFsWrapper = function() {
4545
* initial main.py
4646
*/
4747
fsWrapper.setupFilesystem = function() {
48-
var deferred1 = $.get('firmware.hex', function(fileStr) {
48+
var deferred1 = $.get('micropython/microbit-micropython-v1.hex', function(fileStr) {
4949
fs1 = new microbitFs.MicropythonFsHex(fileStr, {
5050
'maxFsSize': commonFsSize,
5151
});
5252
}).fail(function() {
5353
console.error('Could not load the MicroPython hex 1 file.');
5454
});
55-
var deferred2 = $.get('firmware2.hex', function(fileStr) {
55+
var deferred2 = $.get('micropython/microbit-micropython-v2.hex', function(fileStr) {
5656
fs2 = new microbitFs.MicropythonFsHex(fileStr, {
5757
'maxFsSize': commonFsSize,
5858
});
@@ -89,7 +89,10 @@ var microbitFsWrapper = function() {
8989
}
9090
};
9191

92-
// TODO: doc this
92+
/**
93+
* @param {string} boardId String with the Board ID for the generation.
94+
* @returns ArrayBuffer with the Intel Hex data for the given Board ID.
95+
*/
9396
fsWrapper.getIntelHexForBoardId = function(boardId) {
9497
if (boardId == '9900' || boardId == '9901') {
9598
var hexStr = fs1.getIntelHex();
@@ -98,6 +101,7 @@ var microbitFsWrapper = function() {
98101
} else {
99102
throw Error('Could not recognise the Board ID ' + boardId);
100103
}
104+
// iHex is ASCII so we can do a 1-to-1 conversion from chars to bytes
101105
var hexBuffer = new Uint8Array(hexStr.length);
102106
for (var i=0, strLen=hexStr.length; i < strLen; i++) {
103107
hexBuffer[i] = hexStr.charCodeAt(i);

js/partial-flashing.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ let PartialFlashing = {
551551
// Write a single page of data to micro:bit ROM by writing it to micro:bit RAM and copying to ROM.
552552
// Drawn from https://github.com/microsoft/pxt-microbit/blob/dec5b8ce72d5c2b4b0b20aafefce7474a6f0c7b2/editor/extension.tsx#L385
553553
partialFlashPageAsync: function(dapwrapper, page, nextPage, i) {
554+
// TODO: This short-circuits UICR, do we need to update this?
554555
if (page.targetAddr >= 0x10000000)
555556
return Promise.resolve();
556557

@@ -598,7 +599,7 @@ let PartialFlashing = {
598599
// Flash the micro:bit's ROM with the provided image by only copying over the pages that differ.
599600
// Falls back to a full flash if partial flashing fails.
600601
// Drawn from https://github.com/microsoft/pxt-microbit/blob/dec5b8ce72d5c2b4b0b20aafefce7474a6f0c7b2/editor/extension.tsx#L335
601-
partialFlashAsync: async function(dapwrapper, image, updateProgress) {
602+
partialFlashAsync: async function(dapwrapper, flashBytes, hexBuffer, updateProgress) {
602603
let checksums;
603604
return this.getFlashChecksumsAsync(dapwrapper)
604605
.then(buf => {
@@ -607,15 +608,15 @@ let PartialFlashing = {
607608
return dapwrapper.writeBlockAsync(this.loadAddr, this.flashPageBIN);
608609
})
609610
.then(async () => {
610-
let aligned = PartialFlashingUtils.pageAlignBlocks(image, 0);
611+
let aligned = PartialFlashingUtils.pageAlignBlocks(flashBytes, 0);
611612
const totalPages = aligned.length;
612613
PartialFlashingUtils.log("Total pages: " + totalPages);
613614
aligned = PartialFlashingUtils.onlyChanged(aligned, checksums);
614615
PartialFlashingUtils.log("Changed pages: " + aligned.length);
615616

616617
if (aligned.length > (totalPages / 2)) {
617618
try {
618-
await this.fullFlashAsync(dapwrapper, image, updateProgress);
619+
await this.fullFlashAsync(dapwrapper, hexBuffer, updateProgress);
619620
} catch(err) {
620621
PartialFlashingUtils.log(err);
621622
PartialFlashingUtils.log("Full flash failed, attempting partial flash.");
@@ -628,7 +629,7 @@ let PartialFlashing = {
628629
} catch(err) {
629630
PartialFlashingUtils.log(err);
630631
PartialFlashingUtils.log("Partial flash failed, attempting full flash.");
631-
await this.fullFlashAsync(dapwrapper, image, updateProgress);
632+
await this.fullFlashAsync(dapwrapper, hexBuffer, updateProgress);
632633
}
633634
}
634635

@@ -695,7 +696,7 @@ let PartialFlashing = {
695696

696697
// Flash the micro:bit's ROM with the provided image, resetting the micro:bit first.
697698
// Drawn from https://github.com/microsoft/pxt-microbit/blob/dec5b8ce72d5c2b4b0b20aafefce7474a6f0c7b2/editor/extension.tsx#L439
698-
flashAsync: async function(dapwrapper, image, updateProgress) {
699+
flashAsync: async function(dapwrapper, flashBytes, hexBuffer, updateProgress) {
699700
try {
700701
let p = Promise.resolve()
701702
.then(() => {
@@ -727,11 +728,11 @@ let PartialFlashing = {
727728
"event-type": "info",
728729
"message": "flash-failed" + "/" + "attempting-full-flash"
729730
}}));
730-
return this.fullFlashAsync(dapwrapper, image, updateProgress);
731+
return this.fullFlashAsync(dapwrapper, hexBuffer, updateProgress);
731732
} else {
732733
// Start flashing
733734
PartialFlashingUtils.log("Begin Flashing");
734-
return this.partialFlashAsync(dapwrapper, image, updateProgress);
735+
return this.partialFlashAsync(dapwrapper, flashBytes, hexBuffer, updateProgress);
735736
}
736737
})
737738
.finally(() => {

python-main.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,17 +1477,16 @@ function web_editor(config) {
14771477
// throw new Error('One ore more of the modules used in this script are not available in this version of MicroPython.');
14781478
//}
14791479

1480-
// TODO: If the UICR is fixed in the future we can go back to only send the flash region without the whole hex data
1481-
var flashData = usePartialFlashing
1482-
? FS.getBytesForBoardId(window.dapwrapper.boardId)
1483-
: FS.getIntelHexForBoardId(window.dapwrapper.boardId);
1484-
1480+
// Collect data to flash, partial flashing can use just the flash bytes,
1481+
// but full flashing needs the entire Intel Hex to include the UICR data
1482+
var flashBytes = FS.getBytesForBoardId(window.dapwrapper.boardId);
1483+
var hexBuffer = FS.getIntelHexForBoardId(window.dapwrapper.boardId);
14851484
// Begin flashing
14861485
$('#webusb-flashing-loader').hide();
14871486
$('#webusb-flashing-progress').val(0).css('display', 'inline-block');
14881487
return usePartialFlashing
1489-
? PartialFlashing.flashAsync(window.dapwrapper, flashData, updateProgress)
1490-
: PartialFlashing.fullFlashAsync(window.dapwrapper, flashData, updateProgress);
1488+
? PartialFlashing.flashAsync(window.dapwrapper, flashBytes, hexBuffer, updateProgress)
1489+
: PartialFlashing.fullFlashAsync(window.dapwrapper, hexBuffer, updateProgress);
14911490
})
14921491
.then(function() {
14931492
// Show tick

0 commit comments

Comments
 (0)