8000 Merge pull request #1672 from dhalbert/regular-fs-flush · cpforbes/circuitpython@2c93ce5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2c93ce5

Browse files
authored
Merge pull request adafruit#1672 from dhalbert/regular-fs-flush
flush flash filesystem once a second
2 parents 952c9c5 + 2229f17 commit 2c93ce5

File tree

7 files changed

+47
-7
lines changed

7 files changed

+47
-7
lines changed

ports/atmel-samd/background.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "audio_dma.h"
2929
#include "tick.h"
30+
#include "supervisor/filesystem.h"
3031
#include "supervisor/usb.h"
3132

3233
#include "py/runtime.h"
@@ -53,6 +54,7 @@ void run_background_tasks(void) {
5354
#if CIRCUITPY_NETWORK
5455
network_module_background();
5556
#endif
57+
filesystem_background();
5658
usb_background();
5759
assert_heap_ok();
5860

ports/nrf/background.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include "py/runtime.h"
28+
#include "supervisor/filesystem.h"
2829
#include "supervisor/usb.h"
2930
#include "supervisor/shared/stack.h"
3031

@@ -33,6 +34,7 @@
3334
#endif
3435

3536
void run_background_tasks(void) {
37+
filesystem_background();
3638
usb_background();
3739

3840
#ifdef CIRCUITPY_DISPLAYIO

ports/nrf/tick.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "tick.h"
2828

2929
#include "supervisor/shared/autoreload.h"
30+
#include "supervisor/filesystem.h"
3031
#include "shared-module/gamepad/__init__.h"
3132
#include "shared-bindings/microcontroller/Processor.h"
3233
#include "nrf.h"
@@ -39,14 +40,17 @@ void SysTick_Handler(void) {
3940
// (every millisecond).
4041
ticks_ms += 1;
4142

42-
#ifdef CIRCUITPY_AUTORELOAD_DELAY_MS
43-
autoreload_tick();
44-
#endif
45-
#ifdef CIRCUITPY_GAMEPAD_TICKS
43+
#if CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS > 0
44+
filesystem_tick();
45+
#endif
46+
#ifdef CIRCUITPY_AUTORELOAD_DELAY_MS
47+
autoreload_tick();
48+
#endif
49+
#ifdef CIRCUITPY_GAMEPAD_TICKS
4650
if (!(ticks_ms & CIRCUITPY_GAMEPAD_TICKS)) {
4751
gamepad_tick();
4852
}
49-
#endif
53+
#endif
5054
}
5155

5256
void tick_init() {

py/circuitpy_mpconfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
8000
@@ -597,6 +597,7 @@ void run_background_tasks(void);
597597
#define MICROPY_VM_HOOK_RETURN run_background_tasks();
598598

599599
#define CIRCUITPY_AUTORELOAD_DELAY_MS 500
600+
#define CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS 1000
600601
#define CIRCUITPY_BOOT_OUTPUT_FILE "/boot_out.txt"
601602

602603
#endif // __INCLUDED_MPCONFIG_CIRCUITPY_H

supervisor/filesystem.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131

3232
#include "extmod/vfs_fat.h"
3333

34+
extern volatile bool filesystem_flush_requested;
35+
36+
void filesystem_background(void);
37+
void filesystem_tick(void);
3438
void filesystem_init(bool create_allowed, bool force_create);
3539
void filesystem_flush(void);
3640
bool filesystem_present(void);

supervisor/shared/autoreload.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@
2929
#include "py/mphal.h"
3030
#include "py/reload.h"
3131

32-
volatile uint32_t autoreload_delay_ms = 0;
33-
bool autoreload_enabled = false;
32+
static volatile uint32_t autoreload_delay_ms = 0;
33+
static bool autoreload_enabled = false;
3434
static bool autoreload_suspended = false;
35+
3536
volatile bool reload_requested = false;
3637

3738
inline void autoreload_tick() {

supervisor/shared/filesystem.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,30 @@
3737
static mp_vfs_mount_t _mp_vfs;
3838
static fs_user_mount_t _internal_vfs;
3939

40+
static volatile uint32_t filesystem_flush_interval_ms = CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS;
41+
volatile bool filesystem_flush_requested = false;
42+
43+
void filesystem_background(void) {
44+
if (filesystem_flush_requested) {
45+
filesystem_flush();
46+
filesystem_flush_requested = false;
47+
}
48+
}
49+
50+
inline void filesystem_tick(void) {
51+
if (filesystem_flush_interval_ms == 0) {
52+
// 0 means not turned on.
53+
return;
54+
}
55+
if (filesystem_flush_interval_ms == 1) {
56+
filesystem_flush_requested = true;
57+
filesystem_flush_interval_ms = CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS;
58+
} else {
59+
filesystem_flush_interval_ms--;
60+
}
61+
}
62+
63+
4064
static void make_empty_file(FATFS *fatfs, const char *path) {
4165
FIL fp;
4266
f_open(fatfs, &fp, path, FA_WRITE | FA_CREATE_ALWAYS);
@@ -91,6 +115,8 @@ void filesystem_init(bool create_allowed, bool force_create) {
91115
}
92116

93117
void filesystem_flush(void) {
118+
// Reset interval before next flush.
119+
filesystem_flush_interval_ms = CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS;
94120
supervisor_flash_flush();
95121
}
96122

0 commit comments

Comments
 (0)
0