wchisp-web 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +339 -1
- package/README.md +29 -2
- package/dist/index.d.ts +5 -0
- package/dist/index.js +254 -44
- package/dist/index.js.map +1 -1
- package/dist/index.umd.min.js +1 -1
- package/dist/index.umd.min.js.map +1 -1
- package/package.json +2 -1
package/dist/index.js
CHANGED
|
@@ -87,7 +87,7 @@ function commandReadConfig(mask = 31) {
|
|
|
87
87
|
]);
|
|
88
88
|
}
|
|
89
89
|
function commandWriteConfig(mask, data) {
|
|
90
|
-
const len =
|
|
90
|
+
const len = 2 + data.length;
|
|
91
91
|
return concatBytes(Uint8Array.from([
|
|
92
92
|
CMD.WRITE_CONFIG,
|
|
93
93
|
...le16(len),
|
|
@@ -311,7 +311,12 @@ var WebUsbTransport = class WebUsbTransport {
|
|
|
311
311
|
}
|
|
312
312
|
async open() {
|
|
313
313
|
if (!this.device.opened) await this.device.open();
|
|
314
|
-
if (!this.device.configuration || this.device.configuration.configurationValue !== this.options.configurationValue)
|
|
314
|
+
if (!this.device.configuration || this.device.configuration.configurationValue !== this.options.configurationValue) try {
|
|
315
|
+
await this.device.selectConfiguration(this.options.configurationValue);
|
|
316
|
+
} catch (error) {
|
|
317
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
318
|
+
throw Object.assign(/* @__PURE__ */ new Error(`Failed to select USB configuration ${this.options.configurationValue}; is the bootloader running? ${detail}`), { cause: error });
|
|
319
|
+
}
|
|
315
320
|
this.validateEndpoints();
|
|
316
321
|
await this.device.claimInterface(this.options.interfaceNumber);
|
|
317
322
|
}
|
|
@@ -461,9 +466,10 @@ var ChipDatabase = class ChipDatabase {
|
|
|
461
466
|
return new ChipDatabase(normalizeFamilies(obj.families ?? obj.chips ?? []));
|
|
462
467
|
}
|
|
463
468
|
find(chipId, deviceType) {
|
|
464
|
-
for (const family of this.families) {
|
|
465
|
-
if ((family.deviceType & 255) !== (deviceType & 255)) continue;
|
|
469
|
+
for (const matchFamily of [true, false]) for (const family of this.families) {
|
|
470
|
+
if (matchFamily && (family.deviceType & 255) !== (deviceType & 255)) continue;
|
|
466
471
|
for (const variant of family.variants) {
|
|
472
|
+
if (!matchFamily && ((variant.deviceType ?? family.deviceType) & 255) !== (deviceType & 255)) continue;
|
|
467
473
|
const ids = variant.altChipIds === "*" ? [] : variant.altChipIds ?? [];
|
|
468
474
|
if (variant.altChipIds === "*" || variant.chipId === chipId || ids.includes(chipId)) return mergeFamilyChip(family, variant);
|
|
469
475
|
}
|
|
@@ -577,6 +583,105 @@ const basicConfig = [
|
|
|
577
583
|
explanation: { "0xFFFFFFFF": "Unprotected" }
|
|
578
584
|
}
|
|
579
585
|
];
|
|
586
|
+
function bleConfig(userReset) {
|
|
587
|
+
return [
|
|
588
|
+
{
|
|
589
|
+
offset: 0,
|
|
590
|
+
name: "RESERVED",
|
|
591
|
+
reset: 4294967295
|
|
592
|
+
},
|
|
593
|
+
{
|
|
594
|
+
offset: 4,
|
|
595
|
+
name: "WPROTECT",
|
|
596
|
+
reset: 4294967295
|
|
597
|
+
},
|
|
598
|
+
{
|
|
599
|
+
offset: 8,
|
|
600
|
+
name: "USER_CFG",
|
|
601
|
+
reset: userReset,
|
|
602
|
+
enableDebug: 1342115805,
|
|
603
|
+
fields: [
|
|
604
|
+
{
|
|
605
|
+
name: "CFG_RESET_EN",
|
|
606
|
+
bitRange: [3, 3]
|
|
607
|
+
},
|
|
608
|
+
{
|
|
609
|
+
name: "CFG_DEBUG_EN",
|
|
610
|
+
bitRange: [4, 4]
|
|
611
|
+
},
|
|
612
|
+
{
|
|
613
|
+
name: "CFG_BOOT_EN",
|
|
614
|
+
bitRange: [6, 6]
|
|
615
|
+
},
|
|
616
|
+
{
|
|
617
|
+
name: "CFG_ROM_READ",
|
|
618
|
+
bitRange: [7, 7]
|
|
619
|
+
}
|
|
620
|
+
]
|
|
621
|
+
}
|
|
622
|
+
];
|
|
623
|
+
}
|
|
624
|
+
const ch58Config = bleConfig(3574595407);
|
|
625
|
+
const ch59Config = bleConfig(1342115661);
|
|
626
|
+
const ch32h417Config = [
|
|
627
|
+
{
|
|
628
|
+
offset: 0,
|
|
629
|
+
name: "RDPR_USER",
|
|
630
|
+
reset: 16734885,
|
|
631
|
+
fields: [
|
|
632
|
+
{
|
|
633
|
+
name: "RDPR",
|
|
634
|
+
bitRange: [7, 0],
|
|
635
|
+
explanation: {
|
|
636
|
+
"0xA5": "Unprotected",
|
|
637
|
+
"_": "Protected"
|
|
638
|
+
}
|
|
639
|
+
},
|
|
640
|
+
{
|
|
641
|
+
name: "IWDG_SW",
|
|
642
|
+
bitRange: [16, 16]
|
|
643
|
+
},
|
|
644
|
+
{
|
|
645
|
+
name: "USBFSDLEN",
|
|
646
|
+
bitRange: [22, 22]
|
|
647
|
+
},
|
|
648
|
+
{
|
|
649
|
+
name: "USARTDLEN",
|
|
650
|
+
bitRange: [23, 23]
|
|
651
|
+
}
|
|
652
|
+
]
|
|
653
|
+
},
|
|
654
|
+
{
|
|
655
|
+
offset: 4,
|
|
656
|
+
name: "DATA",
|
|
657
|
+
reset: 4278255360,
|
|
658
|
+
fields: [{
|
|
659
|
+
name: "DATA0",
|
|
660
|
+
bitRange: [7, 0]
|
|
661
|
+
}, {
|
|
662
|
+
name: "DATA1",
|
|
663
|
+
bitRange: [23, 16]
|
|
664
|
+
}]
|
|
665
|
+
},
|
|
666
|
+
{
|
|
667
|
+
offset: 8,
|
|
668
|
+
name: "WRP01",
|
|
669
|
+
reset: 4294967295,
|
|
670
|
+
explanation: {
|
|
671
|
+
"0xFFFFFFFF": "Unprotected",
|
|
672
|
+
"_": "Some 4K sections are protected"
|
|
673
|
+
}
|
|
674
|
+
},
|
|
675
|
+
{
|
|
676
|
+
offset: 12,
|
|
677
|
+
name: "WRP23",
|
|
678
|
+
reset: 4294967295,
|
|
679
|
+
explanation: {
|
|
680
|
+
"0xFFFFFFFF": "Unprotected",
|
|
681
|
+
"_": "Some 4K sections are protected"
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
];
|
|
580
685
|
const BUILTIN_CHIPS = normalizeFamilies([
|
|
581
686
|
{
|
|
582
687
|
name: "CH32V20x",
|
|
@@ -650,38 +755,104 @@ const BUILTIN_CHIPS = normalizeFamilies([
|
|
|
650
755
|
configRegisters: basicConfig
|
|
651
756
|
},
|
|
652
757
|
{
|
|
653
|
-
name: "CH57x
|
|
758
|
+
name: "CH57x",
|
|
654
759
|
mcuType: 8,
|
|
655
760
|
deviceType: 24,
|
|
656
761
|
supportUsb: true,
|
|
657
762
|
supportSerial: true,
|
|
658
|
-
variants: [
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
flashSize: 448 * 1024
|
|
670
|
-
},
|
|
671
|
-
{
|
|
672
|
-
name: "CH582",
|
|
673
|
-
chipId: 130,
|
|
674
|
-
deviceType: 24,
|
|
675
|
-
flashSize: 448 * 1024
|
|
676
|
-
},
|
|
677
|
-
{
|
|
678
|
-
name: "CH592",
|
|
679
|
-
chipId: 146,
|
|
680
|
-
deviceType: 24,
|
|
681
|
-
flashSize: 448 * 1024
|
|
682
|
-
}
|
|
683
|
-
],
|
|
763
|
+
variants: [{
|
|
764
|
+
name: "CH573",
|
|
765
|
+
chipId: 115,
|
|
766
|
+
deviceType: 24,
|
|
767
|
+
flashSize: 448 * 1024
|
|
768
|
+
}, {
|
|
769
|
+
name: "CH579",
|
|
770
|
+
chipId: 121,
|
|
771
|
+
deviceType: 24,
|
|
772
|
+
flashSize: 448 * 1024
|
|
773
|
+
}],
|
|
684
774
|
configRegisters: basicConfig
|
|
775
|
+
},
|
|
776
|
+
{
|
|
777
|
+
name: "CH58x",
|
|
778
|
+
mcuType: 8,
|
|
779
|
+
deviceType: 24,
|
|
780
|
+
supportUsb: true,
|
|
781
|
+
supportSerial: true,
|
|
782
|
+
variants: [{
|
|
783
|
+
name: "CH582",
|
|
784
|
+
chipId: 130,
|
|
785
|
+
deviceType: 24,
|
|
786
|
+
flashSize: 448 * 1024
|
|
787
|
+
}],
|
|
788
|
+
configRegisters: basicConfig
|
|
789
|
+
},
|
|
790
|
+
{
|
|
791
|
+
name: "CH59x",
|
|
792
|
+
mcuType: 8,
|
|
793
|
+
deviceType: 24,
|
|
794
|
+
supportUsb: true,
|
|
795
|
+
supportSerial: true,
|
|
796
|
+
variants: [{
|
|
797
|
+
name: "CH592",
|
|
798
|
+
chipId: 146,
|
|
799
|
+
deviceType: 24,
|
|
800
|
+
flashSize: 448 * 1024
|
|
801
|
+
}],
|
|
802
|
+
configRegisters: basicConfig
|
|
803
|
+
},
|
|
804
|
+
{
|
|
805
|
+
name: "CH58x",
|
|
806
|
+
mcuType: 6,
|
|
807
|
+
deviceType: 22,
|
|
808
|
+
supportUsb: true,
|
|
809
|
+
supportSerial: true,
|
|
810
|
+
variants: [{
|
|
811
|
+
name: "CH582",
|
|
812
|
+
chipId: 130,
|
|
813
|
+
deviceType: 22,
|
|
814
|
+
flashSize: 448 * 1024,
|
|
815
|
+
eepromSize: 32 * 1024,
|
|
816
|
+
eepromStartAddr: 448 * 1024
|
|
817
|
+
}, {
|
|
818
|
+
name: "CH584",
|
|
819
|
+
chipId: 132,
|
|
820
|
+
deviceType: 22,
|
|
821
|
+
flashSize: 448 * 1024,
|
|
822
|
+
eepromSize: 32 * 1024,
|
|
823
|
+
eepromStartAddr: 448 * 1024
|
|
824
|
+
}],
|
|
825
|
+
configRegisters: ch58Config
|
|
826
|
+
},
|
|
827
|
+
{
|
|
828
|
+
name: "CH59x",
|
|
829
|
+
mcuType: 18,
|
|
830
|
+
deviceType: 34,
|
|
831
|
+
supportUsb: true,
|
|
832
|
+
supportSerial: true,
|
|
833
|
+
variants: [{
|
|
834
|
+
name: "CH592",
|
|
835
|
+
chipId: 146,
|
|
836
|
+
deviceType: 34,
|
|
837
|
+
flashSize: 448 * 1024,
|
|
838
|
+
eepromSize: 32 * 1024
|
|
839
|
+
}],
|
|
840
|
+
configRegisters: ch59Config
|
|
841
|
+
},
|
|
842
|
+
{
|
|
843
|
+
name: "CH32H417",
|
|
844
|
+
mcuType: 24,
|
|
845
|
+
deviceType: 40,
|
|
846
|
+
supportUsb: true,
|
|
847
|
+
supportSerial: true,
|
|
848
|
+
supportNet: false,
|
|
849
|
+
variants: [{
|
|
850
|
+
name: "CH32H417QEU6",
|
|
851
|
+
chipId: 112,
|
|
852
|
+
deviceType: 40,
|
|
853
|
+
flashSize: 960 * 1024
|
|
854
|
+
}],
|
|
855
|
+
configRegisters: ch32h417Config
|
|
685
856
|
}
|
|
686
857
|
]);
|
|
687
858
|
|
|
@@ -973,10 +1144,7 @@ var WchIspFlasher = class {
|
|
|
973
1144
|
async flash(input, options = {}) {
|
|
974
1145
|
const info = await this.ensureConnected(options.progress);
|
|
975
1146
|
if (options.unprotect) await this.unprotect(false, options.progress);
|
|
976
|
-
const
|
|
977
|
-
const baseAddress = options.baseAddress ?? image.baseAddress;
|
|
978
|
-
if (baseAddress !== 0) throw new Error(`Only images starting at address 0 are directly flashable; got base address 0x${baseAddress.toString(16)}`);
|
|
979
|
-
const data = options.pad === false ? image.data : padToSector(image.data);
|
|
1147
|
+
const data = await this.firmwareData(input, options);
|
|
980
1148
|
if (options.erase !== false) await this.eraseCode(data.length, "bytes", options.progress);
|
|
981
1149
|
await this.beginIspKey();
|
|
982
1150
|
await this.writeCode(data, options.progress);
|
|
@@ -987,13 +1155,22 @@ var WchIspFlasher = class {
|
|
|
987
1155
|
if (options.reset !== false) await this.reset(1, options.progress);
|
|
988
1156
|
return info;
|
|
989
1157
|
}
|
|
1158
|
+
async verifyFirmware(input, options = {}) {
|
|
1159
|
+
const info = await this.ensureConnected(options.progress);
|
|
1160
|
+
const data = await this.firmwareData(input, options);
|
|
1161
|
+
await this.beginIspKey();
|
|
1162
|
+
await this.verifyCode(data, options.progress);
|
|
1163
|
+
return info;
|
|
1164
|
+
}
|
|
990
1165
|
async writeCode(data, progress) {
|
|
991
1166
|
const key = await this.xorKey();
|
|
992
1167
|
let address = 0;
|
|
993
1168
|
for (let off = 0; off < data.length; off += 56) {
|
|
994
1169
|
const chunk = data.slice(off, off + 56);
|
|
995
1170
|
const xored = xorWithKey(chunk, key);
|
|
996
|
-
|
|
1171
|
+
const resp = await transfer(this.transport, commandProgram(address, xored), 300);
|
|
1172
|
+
assertOk(resp, `program 0x${address.toString(16)}`);
|
|
1173
|
+
assertProgramStatus(resp, address);
|
|
997
1174
|
address += chunk.length;
|
|
998
1175
|
progress?.({
|
|
999
1176
|
phase: "write",
|
|
@@ -1002,7 +1179,9 @@ var WchIspFlasher = class {
|
|
|
1002
1179
|
address
|
|
1003
1180
|
});
|
|
1004
1181
|
}
|
|
1005
|
-
|
|
1182
|
+
const finalResp = await transfer(this.transport, commandProgram(address, /* @__PURE__ */ new Uint8Array()), 300);
|
|
1183
|
+
assertOk(finalResp, `program final 0x${address.toString(16)}`);
|
|
1184
|
+
assertProgramStatus(finalResp, address);
|
|
1006
1185
|
}
|
|
1007
1186
|
async verifyCode(data, progress) {
|
|
1008
1187
|
const key = await this.xorKey();
|
|
@@ -1078,20 +1257,39 @@ var WchIspFlasher = class {
|
|
|
1078
1257
|
address
|
|
1079
1258
|
});
|
|
1080
1259
|
}
|
|
1081
|
-
|
|
1260
|
+
const finalResp = await transfer(this.transport, commandProgram(address, /* @__PURE__ */ new Uint8Array()), 300);
|
|
1261
|
+
assertOk(finalResp, `data_program final 0x${address.toString(16)}`);
|
|
1262
|
+
assertProgramStatus(finalResp, address);
|
|
1082
1263
|
}
|
|
1083
1264
|
async resetConfig(registers) {
|
|
1084
1265
|
const info = await this.ensureConnected();
|
|
1085
1266
|
const regs = registers ?? info.chip?.configRegisters ?? [];
|
|
1267
|
+
await this.writeRegisterDefaults(regs, false);
|
|
1268
|
+
}
|
|
1269
|
+
async enableDebug() {
|
|
1270
|
+
const regs = (await this.ensureConnected()).chip?.configRegisters ?? [];
|
|
1271
|
+
if (!regs.some((reg) => reg.enableDebug !== void 0)) throw new Error("Chip does not advertise an enable-debug configuration");
|
|
1272
|
+
await this.writeRegisterDefaults(regs, true);
|
|
1273
|
+
const readBack = await this.readConfig(7);
|
|
1274
|
+
for (const reg of regs) {
|
|
1275
|
+
if (reg.enableDebug === void 0) continue;
|
|
1276
|
+
if (readBack.length < 2 + reg.offset + 4) throw new Error(`read_config response too short to verify register ${reg.name}`);
|
|
1277
|
+
if (readLe32(readBack, 2 + reg.offset) !== reg.enableDebug >>> 0) throw new Error(`Failed to verify debug configuration in register ${reg.name}`);
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1280
|
+
async writeRegisterDefaults(registers, enableDebug) {
|
|
1086
1281
|
const rawResp = await transfer(this.transport, commandReadConfig(7));
|
|
1087
1282
|
assertOk(rawResp, "read_config");
|
|
1283
|
+
if (rawResp.payload.length < 2) throw new Error("read_config response too short for configuration registers");
|
|
1088
1284
|
const raw = rawResp.payload.slice(2);
|
|
1089
|
-
for (const reg of
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
raw
|
|
1093
|
-
raw[reg.offset
|
|
1094
|
-
raw[reg.offset +
|
|
1285
|
+
for (const reg of registers) {
|
|
1286
|
+
const value = enableDebug ? reg.enableDebug ?? reg.reset : reg.reset;
|
|
1287
|
+
if (value === void 0) continue;
|
|
1288
|
+
if (!Number.isInteger(reg.offset) || reg.offset < 0 || raw.length < reg.offset + 4) throw new Error(`read_config response too short for register ${reg.name} at offset ${reg.offset}`);
|
|
1289
|
+
raw[reg.offset] = value & 255;
|
|
1290
|
+
raw[reg.offset + 1] = value >>> 8 & 255;
|
|
1291
|
+
raw[reg.offset + 2] = value >>> 16 & 255;
|
|
1292
|
+
raw[reg.offset + 3] = value >>> 24 & 255;
|
|
1095
1293
|
}
|
|
1096
1294
|
await this.writeConfig(7, raw);
|
|
1097
1295
|
}
|
|
@@ -1114,12 +1312,24 @@ var WchIspFlasher = class {
|
|
|
1114
1312
|
async ensureConnected(progress) {
|
|
1115
1313
|
return this.info ?? this.connect(progress);
|
|
1116
1314
|
}
|
|
1315
|
+
async firmwareData(input, options) {
|
|
1316
|
+
const image = await this.normalizeFirmware(input);
|
|
1317
|
+
if (image.data.length === 0) throw new Error("Firmware image is empty");
|
|
1318
|
+
const baseAddress = options.baseAddress ?? image.baseAddress;
|
|
1319
|
+
if (baseAddress !== 0) throw new Error(`Only images starting at address 0 are directly flashable; got base address 0x${baseAddress.toString(16)}`);
|
|
1320
|
+
return options.pad === false ? image.data : padToSector(image.data);
|
|
1321
|
+
}
|
|
1117
1322
|
async normalizeFirmware(input) {
|
|
1118
1323
|
if (typeof File !== "undefined" && input instanceof File) return readFirmwareFile(input);
|
|
1119
1324
|
if (typeof input === "object" && "data" in input && "segments" in input) return input;
|
|
1120
1325
|
return parseFirmware(input);
|
|
1121
1326
|
}
|
|
1122
1327
|
};
|
|
1328
|
+
function assertProgramStatus(response, address) {
|
|
1329
|
+
const status = response.payload[0];
|
|
1330
|
+
if (status === void 0) throw new Error(`program 0x${address.toString(16)} returned no status`);
|
|
1331
|
+
if (status !== 0) throw new Error(`program 0x${address.toString(16)} rejected by BootROM (status 0x${hexByte(status)})`);
|
|
1332
|
+
}
|
|
1123
1333
|
|
|
1124
1334
|
//#endregion
|
|
1125
1335
|
export { BUILTIN_CHIPS, CFG_MASK_ALL, CFG_MASK_BTVER, CFG_MASK_RDPR_USER_DATA_WPR, CFG_MASK_UID, CMD, CODE_CHUNK_SIZE, ChipDatabase, EEPROM_READ_CHUNK_SIZE, MAX_PACKET_SIZE, SECTOR_SIZE, WchIspFlasher, WebSerialTransport, WebUsbTransport, assertOk, checksum8, chipDisplayName, commandDataErase, commandDataProgram, commandDataRead, commandErase, commandIdentify, commandIspEnd, commandIspKey, commandProgram, commandReadConfig, commandSetBaud, commandVerify, commandWriteConfig, concatBytes, delay, explainConfig, frameSerialRequest, hex, hexByte, ispKeyChecksum, le16, le32, looksLikeElf, makeXorKey, minEraseSectorNumber, normalizeFamilies, padToSector, parseElf, parseFirmware, parseIntelHex, parseResponse, parseSerialResponse, readFirmwareFile, readLe16, readLe32, transfer, xorWithKey };
|