forked from TinyCircuits/TinyCircuits-Thumby-Code-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload-uf2.js
23 lines (20 loc) · 752 Bytes
/
load-uf2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { decodeBlock } from './uf2/dist/esm/uf2.js';
import { RP2040 } from './rp2040js/dist/esm/rp2040.js';
export async function loadUF2(filename, rp2040) {
console.log("UF2 loading");
// Get the uf2 and set its con
const res = await fetch(filename);
const buffer = await res.arrayBuffer();
const fileData = new Uint8Array(buffer);
let fileIndex = 0;
var lastFlashAddress = 0;
while (fileIndex < fileData.length) {
const dataBlock = fileData.slice(fileIndex, fileIndex + 512);
const block = decodeBlock(dataBlock);
const { flashAddress, payload } = block;
rp2040.flash.set(payload, flashAddress - 0x10000000);
lastFlashAddress = flashAddress;
fileIndex = fileIndex + 512;
}
console.log("UF2 loaded");
}