// SPI flash chip, PSRAM chip and ESP32 SPI master (user-command mode) export class FlashChip { constructor(data, size) { this.d = data; this.size = size; this.sr = 0; this.sr2 = 0; this.wel = false; this.dirty = false; const ids = { 2: [0xEF, 0x30, 0x15], 4: [0xC8, 0x40, 0x16], 8: [0xC8, 0x40, 0x17], 16: [0x9D, 0x60, 0x18] }; this.id = ids[size >> 20] || ids[4]; } // returns Uint8Array of rx bytes (rxLen), cmd/addr already parsed transaction(cmd, addr, tx, rxLen) { const d = this.d, rx = new Uint8Array(rxLen); switch (cmd) { case 0x03: case 0x0B: case 0x3B: case 0x6B: case 0xBB: case 0xEB: case 0x13: case 0x0C: case 0x3C: case 0x6C: case 0xBC: case 0xEC: for (let i = 0; i < rxLen; i++) rx[i] = d[(addr + i) % this.size]; break; case 0x9F: for (let i = 0; i < rxLen; i++) rx[i] = this.id[i] || 0; break; case 0xAB: for (let i = 0; i < rxLen; i++) rx[i] = 0x16; break; case 0x90: for (let i = 0; i < rxLen; i++) rx[i] = [this.id[0], this.id[2]][i] || 0; break; case 0x05: for (let i = 0; i < rxLen; i++) rx[i] = (this.sr & ~2) | (this.wel ? 2 : 0); break; case 0x35: for (let i = 0; i < rxLen; i++) rx[i] = this.sr2; break; case 0x15: for (let i = 0; i < rxLen; i++) rx[i] = 0; break; case 0x01: if (this.wel) { if (tx.length > 0) this.sr = tx[0]; if (tx.length > 1) this.sr2 = tx[1]; this.wel = false; } break; case 0x31: if (this.wel) { this.sr2 = tx[0] || 0; this.wel = false; } break; case 0x06: this.wel = true; break; case 0x50: this.wel = true; break; case 0x04: this.wel = false; break; case 0x02: case 0x32: case 0x38: { if (!this.wel) break; let page = addr & ~0xFF, off = addr & 0xFF; for (let i = 0; i < tx.length; i++) { d[(page + off) % this.size] &= tx[i]; off = (off + 1) & 0xFF; } this.wel = false; this.dirty = true; break; } case 0x20: if (this.wel) { const a = (addr & ~0xFFF) % this.size; d.fill(0xFF, a, a + 0x1000); this.wel = false; this.dirty = true; } break; case 0x52: if (this.wel) { const a = (addr & ~0x7FFF) % this.size; d.fill(0xFF, a, a + 0x8000); this.wel = false; this.dirty = true; } break; case 0xD8: if (this.wel) { const a = (addr & ~0xFFFF) % this.size; d.fill(0xFF, a, a + 0x10000); this.wel = false; this.dirty = true; } break; case 0xC7: case 0x60: if (this.wel) { d.fill(0xFF); this.wel = false; this.dirty = true; } break; default: break; } return rx; } } export class PsramChip { constructor(data, mb) { this.d = data; this.mb = mb; this.size = mb * 1048576; } transaction(cmd, addr, tx, rxLen, hasAddr) { const rx = new Uint8Array(rxLen); if (cmd === 0x9F) { const resp = [0xff, 0x5d, ({ 2: 0, 4: 0x21, 8: 0x40 })[this.mb] || 0x21, 0xaa, 0xbb, 0xcc, 0xdd, 0xee]; for (let i = 0; i < rxLen; i++) rx[i] = resp[i] || 0; } else if (cmd === 0x03 || cmd === 0x0B || cmd === 0xEB) { for (let i = 0; i < rxLen; i++) rx[i] = this.d[(addr + i) % this.size]; } else if (cmd === 0x02 || cmd === 0x38) { for (let i = 0; i < tx.length; i++) this.d[(addr + i) % this.size] = tx[i]; } return rx; } } // ESP32 SPI controller (SPI1/2/3) - user mode and built-in flash commands. export class SpiController { constructor(soc, n) { this.soc = soc; this.n = n; this.reset(); this.devices = [null, null, null]; } reset() { this.r = new Int32Array(0x100); this.r[0x34 >> 2] = 0x6; // PIN this.r[0x20 >> 2] = (23 << 26) | 7; // USER1 this.r[0x24 >> 2] = (4 << 28); // USER2 this.dmaOutLink = 0; this.dmaInLink = 0; this.intRaw = 0; this.intEna = 0; } read(off) { const r = this.r; if (off === 0x00) return 0; // commands complete instantly if (off === 0x38) return (1 << 4) | (1 << 9); // SLAVE: TRANS_DONE if (off === 0xF8) return 0; return r[(off >> 2) & 0xFF]; } write(off, v) { const r = this.r; if (off === 0x00) { this.command(v); return; } r[(off >> 2) & 0xFF] = v; } devFor() { const pin = this.r[0x34 >> 2]; // CS enabled when its disable bit is 0 if (this.n === 1) { if (!(pin & 1)) return this.soc.flashChip; if (!(pin & 2)) return this.soc.psramChip; return null; } for (let i = 0; i < 3; i++) if (!(pin & (1 << i)) && this.devices[i]) return this.devices[i]; return null; } command(cmd) { const r = this.r; const dev = this.devFor(); const W = (i) => r[(0x80 >> 2) + i]; const bytesOf = (bits) => (bits + 1 + 7) >> 3; const user1 = r[0x20 >> 2], user = r[0x1c >> 2], user2 = r[0x24 >> 2]; const addrBytesBuiltin = bytesOf((user1 >>> 26) & 63); const getTx = (n) => { const o = new Uint8Array(n); for (let i = 0; i < n; i++) o[i] = (r[(0x80 >> 2) + (i >> 2)] >>> ((i & 3) * 8)) & 0xFF; return o; }; const putRx = (rx) => { for (let i = 0; i < rx.length; i++) { const w = (0x80 >> 2) + (i >> 2); const sh = (i & 3) * 8; r[w] = (r[w] & ~(0xFF << sh)) | (rx[i] << sh); } }; const run = (c, addr, tx, rxLen, putTo) => { if (!dev) return; const rx = dev.transaction(c, addr >>> 0, tx, rxLen, true); if (putTo === 'w') putRx(rx); else if (putTo === 'status') r[0x10 >> 2] = rx[0] | (rx[1] << 8); else if (putTo === 'w0') putRx(rx); }; const A = r[0x04 >> 2]; switch (cmd >>> 0) { case 0x80000000: run(0x03, A & 0xFFFFFF, null, bytesOf(r[0x2c >> 2]), 'w'); return; // READ case 0x40000000: run(0x06, 0, null, 0); return; // WREN case 0x20000000: run(0x04, 0, null, 0); return; // WRDI case 0x10000000: run(0x9F, 0, null, 3, 'w0'); return; // RDID case 0x08000000: { // RDSR if (dev) { const rx = dev.transaction(0x05, 0, null, 1); r[0x10 >> 2] = rx[0]; if (this.soc.flashChip && dev === this.soc.flashChip) { /* also bit for status2 */ } } return; } case 0x04000000: run(0x01, 0, [r[0x10 >> 2] & 0xFF, (r[0x10 >> 2] >> 8) & 0xFF], 0); return; // WRSR case 0x02000000: { const len = A >>> 24; run(0x02, A & 0xFFFFFF, getTx(len), 0); return; } // PP case 0x01000000: run(0x20, A & 0xFFFFFF, null, 0); return; // SE case 0x00800000: run(0xD8, A & 0xFFFFFF, null, 0); return; // BE case 0x00400000: run(0xC7, 0, null, 0); return; // CE case 0x00200000: run(0xB9, 0, null, 0); return; // DP case 0x00100000: run(0xAB, 0, null, 3, 'w0'); return; // RES case 0x00040000: break; // USR default: return; } // USER command if (!dev && this.n !== 1) { this.userNoDev(); } if (!dev) return; let c = 0, hasCmd = false; if ((user >>> 31) & 1) { hasCmd = true; c = user2 & 0xFFFF; const cb = ((user2 >>> 28) & 15) + 1; if (cb > 8) c = (c >> 8) & 0xFF; else c &= 0xFF; } let addr = 0; if ((user >>> 30) & 1) { const ab = bytesOf((user1 >>> 26) & 63); addr = (A >>> (32 - ab * 8)) >>> 0; if (ab === 4) addr = A >>> 8; if (ab > 3) addr = (A >>> 8) >>> 0; } let tx = null, rxLen = 0, putTo = null; if ((user >>> 27) & 1) tx = getTx(bytesOf(r[0x28 >> 2])); if ((user >>> 28) & 1) { rxLen = bytesOf(r[0x2c >> 2]); putTo = 'w'; } if (!hasCmd) c = -1; const rx = dev.transaction(c, addr, tx || new Uint8Array(0), rxLen, (user >>> 30) & 1); if (rxLen) putRx(rx); } userNoDev() {} }