@@ -2456,7 +2456,6 @@ MspHelper.prototype.dataflashRead = function(address, blockSize, onDataCallback)
2456
2456
1 // allow compression
2457
2457
] ;
2458
2458
2459
- // Ensure MSP object exists
2460
2459
const mspObj = this . msp || ( typeof MSP !== 'undefined' ? MSP : null ) ;
2461
2460
if ( ! mspObj ) {
2462
2461
console . error ( 'MSP object not found, cannot read dataflash.' ) ;
@@ -2465,29 +2464,35 @@ MspHelper.prototype.dataflashRead = function(address, blockSize, onDataCallback)
2465
2464
}
2466
2465
2467
2466
mspObj . send_message ( MSPCodes . MSP_DATAFLASH_READ , outData , false , function ( response ) {
2468
- const headerSize = 7 ;
2469
- const chunkAddress = response . data . readU32 ( ) ;
2470
- const dataSize = response . data . readU16 ( ) ;
2471
- const dataCompressionType = response . data . readU8 ( ) ;
2472
-
2473
- let payloadView ;
2474
- if ( dataCompressionType === 0 ) {
2475
- payloadView = new DataView ( response . data . buffer , response . data . byteOffset + headerSize , dataSize ) ;
2476
- } else if ( dataCompressionType === 1 ) {
2477
- const compressedCharCount = response . data . readU16 ( ) ;
2478
- const compressedArray = new Uint8Array ( response . data . buffer , response . data . byteOffset + headerSize + 2 , dataSize - 2 ) ;
2479
- const decompressedArray = huffmanDecodeBuf ( compressedArray , compressedCharCount , defaultHuffmanTree , defaultHuffmanLenIndex ) ;
2480
- payloadView = new DataView ( decompressedArray . buffer ) ;
2467
+ let payloadView = null ;
2468
+
2469
+ if ( response && response . data ) {
2470
+ const headerSize = 7 ;
2471
+ const chunkAddress = response . data . readU32 ( ) ;
2472
+ const dataSize = response . data . readU16 ( ) ;
2473
+ const dataCompressionType = response . data . readU8 ( ) ;
2474
+
2475
+ if ( chunkAddress === address ) {
2476
+ if ( dataCompressionType === 0 ) {
2477
+ payloadView = new DataView ( response . data . buffer , response . data . byteOffset + headerSize , dataSize ) ;
2478
+ } else if ( dataCompressionType === 1 ) {
2479
+ const compressedCharCount = response . data . readU16 ( ) ;
2480
+ const compressedArray = new Uint8Array ( response . data . buffer , response . data . byteOffset + headerSize + 2 , dataSize - 2 ) ;
2481
+ const decompressedArray = huffmanDecodeBuf ( compressedArray , compressedCharCount , defaultHuffmanTree , defaultHuffmanLenIndex ) ;
2482
+ payloadView = new DataView ( decompressedArray . buffer ) ;
2483
+ }
2484
+ } else {
2485
+ console . log ( `Expected address ${ address } but received ${ chunkAddress } ` ) ;
2486
+ }
2481
2487
}
2482
2488
2483
- // Always deliver data, even if CRC fails
2489
+ // Deliver payloadView if defined, otherwise pass null
2484
2490
onDataCallback ( address , payloadView ) ;
2485
- if ( response . crcError ) {
2486
- console . log ( `CRC error for block at ${ address } ignored; data delivered` ) ;
2487
- } else if ( chunkAddress !== address ) {
2488
- console . log ( `Expected address ${ address } but received ${ chunkAddress } ` ) ;
2489
- } else {
2490
- console . log ( `Block at ${ address } received (${ dataSize } bytes)` ) ;
2491
+
2492
+ if ( ! response || response . crcError ) {
2493
+ console . log ( `CRC error or missing data at address ${ address } - delivering whatever we got` ) ;
2494
+ } else if ( payloadView ) {
2495
+ console . log ( `Block at ${ address } received (${ payloadView . byteLength } bytes)` ) ;
2491
2496
}
2492
2497
} , true ) ;
2493
2498
} ;
0 commit comments