@@ -272,6 +272,9 @@ uint32_t supervisor_flash_get_block_count(void) {
272
272
// Flush the cache that was written to the scratch portion of flash. Only used
273
273
// when ram is tight.
274
274
static bool flush_scratch_flash (void ) {
275
+ if (current_sector == NO_SECTOR_LOADED ) {
276
+ return true;
277
+ }
275
278
// First, copy out any blocks that we haven't touched from the sector we've
276
279
// cached.
277
280
bool copy_to_scratch_ok = true;
@@ -360,9 +363,25 @@ static bool allocate_ram_cache(void) {
360
363
return success ;
361
364
}
362
365
366
+ static void release_ram_cache (void ) {
367
+ if (supervisor_cache != NULL ) {
368
+ free_memory (supervisor_cache );
369
+ supervisor_cache = NULL ;
370
+ } else {
371
+ m_free (MP_STATE_VM (flash_ram_cache ));
372
+ }
373
+ MP_STATE_VM (flash_ram_cache ) = NULL ;
374
+ }
375
+
363
376
// Flush the cached sector from ram onto the flash. We'll free the cache unless
364
377
// keep_cache is true.
365
378
static bool flush_ram_cache (bool keep_cache ) {
379
+ if (current_sector == NO_SECTOR_LOADED ) {
380
+ if (!keep_cache ) {
381
+ release_ram_cache ();
382
+ }
383
+ return true;
384
+ }
366
385
// First, copy out any blocks that we haven't touched from the sector
367
386
// we've cached. If we don't do this we'll erase the data during the sector
368
387
// erase below.
@@ -403,22 +422,13 @@ static bool flush_ram_cache(bool keep_cache) {
403
422
}
404
423
// We're done with the cache for now so give it back.
405
424
if (!keep_cache ) {
406
- if (supervisor_cache != NULL ) {
407
- free_memory (supervisor_cache );
408
- supervisor_cache = NULL ;
409
- } else {
410
- m_free (MP_STATE_VM (flash_ram_cache ));
411
- }
412
- MP_STATE_VM (flash_ram_cache ) = NULL ;
425
+ release_ram_cache ();
413
426
}
414
427
return true;
415
428
}
416
429
417
430
// Delegates to the correct flash flush method depending on the existing cache.
418
431
static void spi_flash_flush_keep_cache (bool keep_cache ) {
419
- if (current_sector == NO_SECTOR_LOADED ) {
420
- return ;
421
- }
422
432
#ifdef MICROPY_HW_LED_MSC
423
433
port_pin_set_output_level (MICROPY_HW_LED_MSC , true);
424
434
#endif
@@ -436,9 +446,11 @@ static void spi_flash_flush_keep_cache(bool keep_cache) {
436
446
#endif
437
447
}
438
448
439
- // External flash function used. If called externally we assume we won't need
440
- // the cache after.
441
449
void supervisor_flash_flush (void ) {
450
+ spi_flash_flush_keep_cache (true);
451
+ }
452
+
453
+ void supervisor_flash_release_cache (void ) {
442
454
spi_flash_flush_keep_cache (false);
443
455
}
444
456
@@ -502,7 +514,7 @@ bool external_flash_write_block(const uint8_t *data, uint32_t block) {
502
514
return write_flash (address , data , FILESYSTEM_BLOCK_SIZE );
503
515
}
504
516
if (current_sector != NO_SECTOR_LOADED ) {
505
- spi_flash_flush_keep_cache (true );
517
+ supervisor_flash_flush ( );
506
518
}
507
519
if (MP_STATE_VM (flash_ram_cache ) == NULL && !allocate_ram_cache ()) {
508
520
erase_sector (flash_device -> total_size - SPI_FLASH_ERASE_SIZE );
0 commit comments