MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / downloadToMemory

Method downloadToMemory

tools/xsbug/esptool.js:501–545  ·  view source on GitHub ↗
(data, offset, entry)

Source from the content-addressed store, hash-verified

499 });
500 }
501 async downloadToMemory(data, offset, entry) {
502 const byteLength = data.byteLength;
503 const ESP_RAM_BLOCK = 6 * 1024;
504 let blocks = Math.ceil(byteLength / ESP_RAM_BLOCK);
505
506 let payload = new DataView(new ArrayBuffer(4 * 4));
507 payload.setUint32(0, byteLength, true); // little-endian
508 payload.setUint32(4, blocks, true); // little-endian
509 payload.setUint32(8, ESP_RAM_BLOCK, true); // little-endian
510 payload.setUint32(12, offset, true); // little-endian
511 await this.command(Bootloader.MEM_BEGIN, payload.buffer);
512 let reply = this.parseReply(await this.getReply(), Bootloader.MEM_BEGIN);
513
514 let block = 0;
515 while (blocks--) {
516 const fragment = data.slice(block * ESP_RAM_BLOCK, (block + 1) * ESP_RAM_BLOCK);
517 const checksum = this.espChecksum(fragment);
518 payload = new DataView(new ArrayBuffer((4 * 4) + fragment.length));
519 payload.setUint32(0, fragment.length, true);// little-endian
520 payload.setUint32(4, block, true); // little-endian
521 payload.setUint32(8, 0, true); // little-endian
522 payload.setUint32(12, 0, true); // little-endian
523 payload = new Uint8Array(payload.buffer);
524 payload.set(fragment, 16);
525 await this.command(Bootloader.MEM_DATA, payload, checksum);
526 reply = this.parseReply(await this.getReply(Math.floor(50 + (fragment.length/(this.#baud / 10) * 1000))), Bootloader.MEM_DATA);
527 block += 1;
528 }
529
530 if (entry) {
531 payload = new DataView(new ArrayBuffer(2 * 4));
532 payload.setUint32(0, 0, true); // little-endian
533 payload.setUint32(4, entry, true); // little-endian
534
535 await this.command(Bootloader.MEM_END, new Uint8Array(payload.buffer));
536 reply = await this.getReply(50, true);
537 if (!this.parseReply(reply, Bootloader.MEM_END))
538 throw new Error("stub download failed");
539 let s = "";
540 for (let i = 0; i < reply.length; i++)
541 s += String.fromCharCode(reply[i]);
542 if (s.indexOf("OHAI") < 0)
543 throw new Error("Stub loader didn't reply");
544 }
545 }
546 async deployStub(stub) {
547 await this.downloadToMemory(stub.text, stub.text_start);
548 await this.downloadToMemory(stub.data, stub.data_start, stub.entry);

Callers 1

deployStubMethod · 0.95

Calls 6

commandMethod · 0.95
parseReplyMethod · 0.95
getReplyMethod · 0.95
espChecksumMethod · 0.95
sliceMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected