@@ -490,45 +490,45 @@ onboard_logging.initialize = function (callback) {
490490 show_saving_dialog ( ) ;
491491
492492function onChunkRead ( chunkAddress , chunkDataView , bytesCompressed ) {
493- if ( chunkDataView !== null ) {
494- // Did we receive any data?
495- if ( chunkDataView . byteLength > 0 ) {
496- nextAddress += chunkDataView . byteLength ;
497- if ( isNaN ( bytesCompressed ) || isNaN ( totalBytesCompressed ) ) {
498- totalBytesCompressed = null ;
493+ if ( chunkDataView !== null && chunkDataView . byteLength > 0 ) {
494+ // Write whatever we received, even if the block had CRC issues
495+ nextAddress += chunkDataView . byteLength ;
496+
497+ if ( isNaN ( bytesCompressed ) || isNaN ( totalBytesCompressed ) ) {
498+ totalBytesCompressed = null ;
499+ } else {
500+ totalBytesCompressed += bytesCompressed ;
501+ }
502+
503+ $ ( ".dataflash-saving progress" ) . attr ( "value" , ( nextAddress / maxBytes ) * 100 ) ;
504+
505+ const blob = new Blob ( [ chunkDataView ] ) ;
506+ FileSystem . writeChunck ( openedFile , blob ) . then ( ( ) => {
507+ if ( saveCancelled || nextAddress >= maxBytes ) {
508+ mark_saving_dialog_done ( startTime , nextAddress , totalBytesCompressed ) ;
509+ FileSystem . closeFile ( openedFile ) ;
499510 } else {
500- totalBytesCompressed += bytesCompressed ;
511+ mspHelper . dataflashRead ( nextAddress , self . blockSize , onChunkRead ) ;
501512 }
513+ } ) ;
514+
515+ } else if ( chunkDataView !== null && chunkDataView . byteLength === 0 ) {
516+ // Zero-byte block → end-of-file
517+ mark_saving_dialog_done ( startTime , nextAddress , totalBytesCompressed ) ;
518+ FileSystem . closeFile ( openedFile ) ;
502519
503- $ ( ".dataflash-saving progress" ) . attr ( "value" , ( nextAddress / maxBytes ) * 100 ) ;
520+ } else {
521+ // CRC or address mismatch
522+ console . warn ( `Skipping block at address ${ nextAddress } due to CRC or mismatch` ) ;
504523
524+ // If some data arrived (chunkDataView not null), write it anyway
525+ if ( chunkDataView && chunkDataView . byteLength > 0 ) {
505526 const blob = new Blob ( [ chunkDataView ] ) ;
506- FileSystem . writeChunck ( openedFile , blob ) . then ( ( ) => {
507- if ( saveCancelled || nextAddress >= maxBytes ) {
508- if ( saveCancelled ) {
509- dismiss_saving_dialog ( ) ;
510- } else {
511- mark_saving_dialog_done ( startTime , nextAddress , totalBytesCompressed ) ;
512- }
513- FileSystem . closeFile ( openedFile ) ;
514- } else {
515- if ( ! self . writeError ) {
516- mspHelper . dataflashRead ( nextAddress , self . blockSize , onChunkRead ) ;
517- } else {
518- dismiss_saving_dialog ( ) ;
519- FileSystem . closeFile ( openedFile ) ;
520- }
521- }
522- } ) ;
527+ FileSystem . writeChunck ( openedFile , blob ) ;
528+ nextAddress += chunkDataView . byteLength ;
523529 } else {
524- // A zero-byte block indicates end-of-file, so we're done
525- mark_saving_dialog_done ( startTime , nextAddress , totalBytesCompressed ) ;
526- FileSystem . closeFile ( openedFile ) ;
530+ nextAddress += self . blockSize ;
527531 }
528- } else {
529- // There was an error with the received block (address didn't match the one we asked for), skip it
530- console . warn ( `Skipping block at address ${ nextAddress } due to CRC or mismatch` ) ;
531- nextAddress += self . blockSize ;
532532
533533 if ( nextAddress >= maxBytes ) {
534534 mark_saving_dialog_done ( startTime , nextAddress , totalBytesCompressed ) ;
0 commit comments