Skip to content

A display fix and file system flush tweak #1743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ports/atmel-samd/supervisor/internal_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ uint32_t supervisor_flash_get_block_count(void) {
void supervisor_flash_flush(void) {
}

void supervisor_flash_release_cache(void) {
}

void flash_flush(void) {
supervisor_flash_flush();
}
Expand Down
3 changes: 3 additions & 0 deletions ports/nrf/supervisor/internal_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ void supervisor_flash_flush(void) {
_flash_page_addr = NO_CACHE;
}

void supervisor_flash_release_cache(void) {
}

mp_uint_t supervisor_flash_read_blocks(uint8_t *dest, uint32_t block, uint32_t num_blocks) {
// Must write out anything in cache before trying to read.
supervisor_flash_flush();
Expand Down
2 changes: 1 addition & 1 deletion supervisor/flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
void supervisor_flash_init(void);
uint32_t supervisor_flash_get_block_size(void);
uint32_t supervisor_flash_get_block_count(void);
void supervisor_flash_flush(void);

// these return 0 on success, non-zero on error
mp_uint_t supervisor_flash_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks);
Expand All @@ -49,5 +48,6 @@ mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t block_num,
struct _fs_user_mount_t;
void supervisor_flash_init_vfs(struct _fs_user_mount_t *vfs);
void supervisor_flash_flush(void);
void supervisor_flash_release_cache(void);

#endif // MICROPY_INCLUDED_SUPERVISOR_FLASH_H
1 change: 1 addition & 0 deletions supervisor/shared/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ void supervisor_start_terminal(uint16_t width_px, uint16_t height_px) {
void supervisor_stop_terminal(void) {
if (tilegrid_tiles != NULL) {
free_memory(tilegrid_tiles);
tilegrid_tiles = NULL;
supervisor_terminal_text_grid.inline_tiles = false;
supervisor_terminal_text_grid.tiles = NULL;
}
Expand Down
38 changes: 25 additions & 13 deletions supervisor/shared/external_flash/external_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ uint32_t supervisor_flash_get_block_count(void) {
// Flush the cache that was written to the scratch portion of flash. Only used
// when ram is tight.
static bool flush_scratch_flash(void) {
if (current_sector == NO_SECTOR_LOADED) {
return true;
}
// First, copy out any blocks that we haven't touched from the sector we've
// cached.
bool copy_to_scratch_ok = true;
Expand Down Expand Up @@ -360,9 +363,25 @@ static bool allocate_ram_cache(void) {
return success;
}

static void release_ram_cache(void) {
if (supervisor_cache != NULL) {
free_memory(supervisor_cache);
supervisor_cache = NULL;
} else {
m_free(MP_STATE_VM(flash_ram_cache));
}
MP_STATE_VM(flash_ram_cache) = NULL;
}

// Flush the cached sector from ram onto the flash. We'll free the cache unless
// keep_cache is true.
static bool flush_ram_cache(bool keep_cache) {
if (current_sector == NO_SECTOR_LOADED) {
if (!keep_cache) {
release_ram_cache();
}
return true;
}
// First, copy out any blocks that we haven't touched from the sector
// we've cached. If we don't do this we'll erase the data during the sector
// erase below.
Expand Down Expand Up @@ -403,22 +422,13 @@ static bool flush_ram_cache(bool keep_cache) {
}
// We're done with the cache for now so give it back.
if (!keep_cache) {
if (supervisor_cache != NULL) {
free_memory(supervisor_cache);
supervisor_cache = NULL;
} else {
m_free(MP_STATE_VM(flash_ram_cache));
}
MP_STATE_VM(flash_ram_cache) = NULL;
release_ram_cache();
}
return true;
}

// Delegates to the correct flash flush method depending on the existing cache.
static void spi_flash_flush_keep_cache(bool keep_cache) {
if (current_sector == NO_SECTOR_LOADED) {
return;
}
#ifdef MICROPY_HW_LED_MSC
port_pin_set_output_level(MICROPY_HW_LED_MSC, true);
#endif
Expand All @@ -436,9 +446,11 @@ static void spi_flash_flush_keep_cache(bool keep_cache) {
#endif
}

// External flash function used. If called externally we assume we won't need
// the cache after.
void supervisor_flash_flush(void) {
spi_flash_flush_keep_cache(true);
}

void supervisor_flash_release_cache(void) {
spi_flash_flush_keep_cache(false);
}

Expand Down Expand Up @@ -502,7 +514,7 @@ bool external_flash_write_block(const uint8_t *data, uint32_t block) {
return write_flash(address, data, FILESYSTEM_BLOCK_SIZE);
}
if (current_sector != NO_SECTOR_LOADED) {
spi_flash_flush_keep_cache(true);
supervisor_flash_flush();
}
if (MP_STATE_VM(flash_ram_cache) == NULL && !allocate_ram_cache()) {
erase_sector(flash_device->total_size - SPI_FLASH_ERASE_SIZE);
Expand Down
6 changes: 5 additions & 1 deletion supervisor/shared/filesystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ volatile bool filesystem_flush_requested = false;

void filesystem_background(void) {
if (filesystem_flush_requested) {
filesystem_flush();
filesystem_flush_interval_ms = CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS;
// Flush but keep caches
supervisor_flash_flush();
filesystem_flush_requested = false;
}
}
Expand Down Expand Up @@ -118,6 +120,8 @@ void filesystem_flush(void) {
// Reset interval before next flush.
filesystem_flush_interval_ms = CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS;
supervisor_flash_flush();
// Don't keep caches because this is called when starting or stopping the VM.
supervisor_flash_release_cache();
}

void filesystem_set_internal_writable_by_usb(bool writable) {
Expand Down