(timeout = 20_000, doFlash = function(){})
| 389 | return result; |
| 390 | } |
| 391 | async beginProgramming(timeout = 20_000, doFlash = function(){}) { |
| 392 | const stop = timeout + Date.now(); |
| 393 | let baud = this.#baudDownload ?? 921600; |
| 394 | let reset = this.#reset; |
| 395 | |
| 396 | for (let pass = 0; pass < 2; pass++) { |
| 397 | await this.buildSerial(this.#baud); |
| 398 | |
| 399 | const syncPayload = new Uint8Array(4 + 32); |
| 400 | syncPayload[0] = 0x07; |
| 401 | syncPayload[1] = 0x07; |
| 402 | syncPayload[2] = 0x12; |
| 403 | syncPayload[3] = 0x20; |
| 404 | for (let i = 0; i < 32; i++) |
| 405 | syncPayload[i + 4] = 0x55; |
| 406 | let synced = false; |
| 407 | |
| 408 | syncLoop: |
| 409 | for (let retry = 0; retry < 5; retry++) { |
| 410 | if (reset) { |
| 411 | await doFlash(true); |
| 412 | this.#serial.set({DTR: false, RTS: true}); |
| 413 | |
| 414 | await delay(100); |
| 415 | |
| 416 | this.#serial.set({DTR: true, RTS: false}); |
| 417 | |
| 418 | await doFlash(false); |
| 419 | |
| 420 | await delay(50); |
| 421 | |
| 422 | this.#serial.set({DTR: false}); |
| 423 | } |
| 424 | |
| 425 | for (let i = 0; i < 4; i++) { |
| 426 | if (Date.now() > stop) |
| 427 | break syncLoop; |
| 428 | this.#serial.purge(); |
| 429 | this.command(Bootloader.SYNC, syncPayload.buffer); |
| 430 | const received = await this.getReply(); |
| 431 | if (received && this.parseReply(received, Bootloader.SYNC)) { |
| 432 | synced = true; |
| 433 | break syncLoop; |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | if (!synced) |
| 438 | throw new Error("Unable to enter programming mode"); |
| 439 | |
| 440 | await this.deployStub(this.#loader); |
| 441 | console.log("change to baud rate of " + baud); |
| 442 | await this.changeBaud(baud, this.#baud); |
| 443 | |
| 444 | if (("esp32" !== this.#mcu) || (undefined !== this.#baudDownload)) |
| 445 | break; |
| 446 | |
| 447 | try { |
| 448 | const DR_REG_SYSCON_BASE = 0x3ff66000; |
no test coverage detected