1010#include "py/gc.h"
1111
1212#include "lib/fatfs/ff.h"
13+ #include "lib/fatfs/diskio.h"
1314#include "lib/utils/pyexec.h"
1415#include "extmod/fsusermount.h"
1516
@@ -160,6 +161,12 @@ static char *stack_top;
160161static char heap [16384 ];
161162
162163void reset_mp () {
164+ // Sync the file systems in case any used RAM from the GC to cache. As soon
165+ // as we re-init the GC all bets are off on the cache.
166+ disk_ioctl (0 , CTRL_SYNC , NULL );
167+ disk_ioctl (1 , CTRL_SYNC , NULL );
168+ disk_ioctl (2 , CTRL_SYNC , NULL );
169+
163170 #if MICROPY_ENABLE_GC
164171 gc_init (heap , heap + sizeof (heap ));
165172 #endif
@@ -185,9 +192,6 @@ int main(int argc, char **argv) {
185192 samd21_init ();
186193 #endif
187194
188- // Initialise the local flash filesystem.
189- // Create it if needed, mount in on /flash, and set it as current dir.
190- init_flash_fs ();
191195
192196 int stack_dummy ;
193197 // Store the location of stack_dummy as an approximation for the top of the
@@ -196,6 +200,11 @@ int main(int argc, char **argv) {
196200 stack_top = (char * )& stack_dummy ;
197201 reset_mp ();
198202
203+ // Initialise the local flash filesystem after the gc in case we need to
204+ // grab memory from it. Create it if needed, mount in on /flash, and set it
205+ // as current dir.
206+ init_flash_fs ();
207+
199208 // Start USB after getting everything going.
200209 #ifdef USB_REPL
201210 udc_start ();
0 commit comments