From 2459eabd66169d18f3f223c942cc73152ce6de68 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Wed, 20 Mar 2019 12:21:36 -0400 Subject: [PATCH 1/2] flush flash filesystem once a second --- ports/atmel-samd/background.c | 2 ++ ports/nrf/background.c | 2 ++ ports/nrf/tick.c | 14 +++++++++----- py/circuitpy_mpconfig.h | 1 + supervisor/filesystem.h | 4 ++++ supervisor/shared/autoreload.c | 5 +++-- supervisor/shared/filesystem.c | 24 ++++++++++++++++++++++++ 7 files changed, 45 insertions(+), 7 deletions(-) diff --git a/ports/atmel-samd/background.c b/ports/atmel-samd/background.c index 519d0eeec758d..a8c4e3891813f 100644 --- a/ports/atmel-samd/background.c +++ b/ports/atmel-samd/background.c @@ -27,6 +27,7 @@ #include "audio_dma.h" #include "tick.h" +#include "supervisor/filesystem.h" #include "supervisor/usb.h" #include "py/runtime.h" @@ -53,6 +54,7 @@ void run_background_tasks(void) { #if CIRCUITPY_NETWORK network_module_background(); #endif + filesystem_background(); usb_background(); assert_heap_ok(); diff --git a/ports/nrf/background.c b/ports/nrf/background.c index 69c76fd066ecd..ea6e846b31fff 100644 --- a/ports/nrf/background.c +++ b/ports/nrf/background.c @@ -25,6 +25,7 @@ */ #include "py/runtime.h" +#include "supervisor/filesystem.h" #include "supervisor/usb.h" #include "supervisor/shared/stack.h" @@ -33,6 +34,7 @@ #endif void run_background_tasks(void) { + filesystem_background(); usb_background(); #ifdef CIRCUITPY_DISPLAYIO diff --git a/ports/nrf/tick.c b/ports/nrf/tick.c index cdd329f71da3d..6d8fd13e0a891 100644 --- a/ports/nrf/tick.c +++ b/ports/nrf/tick.c @@ -27,6 +27,7 @@ #include "tick.h" #include "supervisor/shared/autoreload.h" +#include "supervisor/filesystem.h" #include "shared-module/gamepad/__init__.h" #include "shared-bindings/microcontroller/Processor.h" #include "nrf.h" @@ -39,14 +40,17 @@ void SysTick_Handler(void) { // (every millisecond). ticks_ms += 1; - #ifdef CIRCUITPY_AUTORELOAD_DELAY_MS - autoreload_tick(); - #endif - #ifdef CIRCUITPY_GAMEPAD_TICKS +#if CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS > 0 + filesystem_tick(); +#endif +#ifdef CIRCUITPY_AUTORELOAD_DELAY_MS + autoreload_tick(); +#endif +#ifdef CIRCUITPY_GAMEPAD_TICKS if (!(ticks_ms & CIRCUITPY_GAMEPAD_TICKS)) { gamepad_tick(); } - #endif +#endif } void tick_init() { diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index 00f3c7b3f400b..df5bfbd8cd32b 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -592,6 +592,7 @@ void run_background_tasks(void); #define MICROPY_VM_HOOK_RETURN run_background_tasks(); #define CIRCUITPY_AUTORELOAD_DELAY_MS 500 +#define CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS 1000 #define CIRCUITPY_BOOT_OUTPUT_FILE "/boot_out.txt" #endif // __INCLUDED_MPCONFIG_CIRCUITPY_H diff --git a/supervisor/filesystem.h b/supervisor/filesystem.h index 76f235f47d88f..c7c951a5e6e65 100644 --- a/supervisor/filesystem.h +++ b/supervisor/filesystem.h @@ -31,6 +31,10 @@ #include "extmod/vfs_fat.h" +extern volatile bool filesystem_flush_requested; + +void filesystem_background(void); +void filesystem_tick(void); void filesystem_init(bool create_allowed, bool force_create); void filesystem_flush(void); bool filesystem_present(void); diff --git a/supervisor/shared/autoreload.c b/supervisor/shared/autoreload.c index 2a7fd1e9d8b25..14b21902cdeef 100644 --- a/supervisor/shared/autoreload.c +++ b/supervisor/shared/autoreload.c @@ -29,9 +29,10 @@ #include "py/mphal.h" #include "py/reload.h" -volatile uint32_t autoreload_delay_ms = 0; -bool autoreload_enabled = false; +static volatile uint32_t autoreload_delay_ms = 0; +static bool autoreload_enabled = false; static bool autoreload_suspended = false; + volatile bool reload_requested = false; inline void autoreload_tick() { diff --git a/supervisor/shared/filesystem.c b/supervisor/shared/filesystem.c index 0ef978ef21a46..dc061fa4185f2 100644 --- a/supervisor/shared/filesystem.c +++ b/supervisor/shared/filesystem.c @@ -37,6 +37,30 @@ static mp_vfs_mount_t _mp_vfs; static fs_user_mount_t _internal_vfs; +static volatile uint32_t filesystem_flush_interval_ms = CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS; +volatile bool filesystem_flush_requested = false; + +void filesystem_background(void) { + if (filesystem_flush_requested) { + filesystem_flush(); + filesystem_flush_requested = false; + } +} + +inline void filesystem_tick(void) { + if (filesystem_flush_interval_ms == 0) { + // 0 means not turned on. + return; + } + if (filesystem_flush_interval_ms == 1) { + filesystem_flush_requested = true; + filesystem_flush_interval_ms = CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS; + } else { + filesystem_flush_interval_ms--; + } +} + + static void make_empty_file(FATFS *fatfs, const char *path) { FIL fp; f_open(fatfs, &fp, path, FA_WRITE | FA_CREATE_ALWAYS); From 2229f17911bae7f6db386aad96ac3fa077a46862 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Mon, 25 Mar 2019 20:42:08 -0400 Subject: [PATCH 2/2] reset flush timer on call to filesystem_flush() --- supervisor/shared/filesystem.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/supervisor/shared/filesystem.c b/supervisor/shared/filesystem.c index dc061fa4185f2..68c6f474990ae 100644 --- a/supervisor/shared/filesystem.c +++ b/supervisor/shared/filesystem.c @@ -115,6 +115,8 @@ void filesystem_init(bool create_allowed, bool force_create) { } void filesystem_flush(void) { + // Reset interval before next flush. + filesystem_flush_interval_ms = CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS; supervisor_flash_flush(); }