@@ -161,6 +161,95 @@ static const char fresh_readme_txt[] =
161161"Please visit http://micropython.org/help/ for further help.\r\n"
162162;
163163
164+ // we don't make this function static because it needs a lot of stack and we
165+ // want it to be executed without using stack within main() function
166+ void init_flash_fs (uint reset_mode ) {
167+ // try to mount the flash
168+ FRESULT res = f_mount (& fatfs0 , "/flash" , 1 );
169+
170+ if (reset_mode == 3 || res == FR_NO_FILESYSTEM ) {
171+ // no filesystem, or asked to reset it, so create a fresh one
172+
173+ // LED on to indicate creation of LFS
174+ led_state (PYB_LED_R2 , 1 );
175+ uint32_t start_tick = HAL_GetTick ();
176+
177+ res = f_mkfs ("/flash" , 0 , 0 );
178+ if (res == FR_OK ) {
179+ // success creating fresh LFS
180+ } else {
181+ __fatal_error ("could not create LFS" );
182+ }
183+
184+ // set label
185+ f_setlabel ("/flash/pybflash" );
186+
187+ // create empty main.py
188+ FIL fp ;
189+ f_open (& fp , "/flash/main.py" , FA_WRITE | FA_CREATE_ALWAYS );
190+ UINT n ;
191+ f_write (& fp , fresh_main_py , sizeof (fresh_main_py ) - 1 /* don't count null terminator */ , & n );
192+ // TODO check we could write n bytes
193+ f_close (& fp );
194+
195+ // create .inf driver file
196+ f_open (& fp , "/flash/pybcdc.inf" , FA_WRITE | FA_CREATE_ALWAYS );
197+ f_write (& fp , fresh_pybcdc_inf , sizeof (fresh_pybcdc_inf ) - 1 /* don't count null terminator */ , & n );
198+ f_close (& fp );
199+
200+ // create readme file
201+ f_open (& fp , "/flash/README.txt" , FA_WRITE | FA_CREATE_ALWAYS );
202+ f_write (& fp , fresh_readme_txt , sizeof (fresh_readme_txt ) - 1 /* don't count null terminator */ , & n );
203+ f_close (& fp );
204+
205+ // keep LED on for at least 200ms
206+ sys_tick_wait_at_least (start_tick , 200 );
207+ led_state (PYB_LED_R2 , 0 );
208+ } else if (res == FR_OK ) {
209+ // mount sucessful
210+ } else {
211+ __fatal_error ("could not access LFS" );
212+ }
213+
214+ // The current directory is used as the boot up directory.
215+ // It is set to the internal flash filesystem by default.
216+ f_chdrive ("/flash" );
217+
218+ // Make sure we have a /flash/boot.py. Create it if needed.
219+ FILINFO fno ;
220+ #if _USE_LFN
221+ fno .lfname = NULL ;
222+ fno .lfsize = 0 ;
223+ #endif
224+ res = f_stat ("/flash/boot.py" , & fno );
225+ if (res == FR_OK ) {
226+ if (fno .fattrib & AM_DIR ) {
227+ // exists as a directory
228+ // TODO handle this case
229+ // see http://elm-chan.org/fsw/ff/img/app2.c for a "rm -rf" implementation
230+ } else {
231+ // exists as a file, good!
232+ }
233+ } else {
234+ // doesn't exist, create fresh file
235+
236+ // LED on to indicate creation of boot.py
237+ led_state (PYB_LED_R2 , 1 );
238+ uint32_t start_tick = HAL_GetTick ();
239+
240+ FIL fp ;
241+ f_open (& fp , "/flash/boot.py" , FA_WRITE | FA_CREATE_ALWAYS );
242+ UINT n ;
243+ f_write (& fp , fresh_boot_py , sizeof (fresh_boot_py ) - 1 /* don't count null terminator */ , & n );
244+ // TODO check we could write n bytes
245+ f_close (& fp );
246+
247+ // keep LED on for at least 200ms
248+ sys_tick_wait_at_least (start_tick , 200 );
249+ led_state (PYB_LED_R2 , 0 );
250+ }
251+ }
252+
164253int main (void ) {
165254 // TODO disable JTAG
166255
@@ -323,94 +412,8 @@ int main(void) {
323412 pyb_usb_init0 ();
324413
325414 // Initialise the local flash filesystem.
326- // Create it if needed, and mount in on /flash.
327- {
328- // try to mount the flash
329- FRESULT res = f_mount (& fatfs0 , "/flash" , 1 );
330- if (reset_mode == 3 || res == FR_NO_FILESYSTEM ) {
331- // no filesystem, or asked to reset it, so create a fresh one
332-
333- // LED on to indicate creation of LFS
334- led_state (PYB_LED_R2 , 1 );
335- uint32_t start_tick = HAL_GetTick ();
336-
337- res = f_mkfs ("/flash" , 0 , 0 );
338- if (res == FR_OK ) {
339- // success creating fresh LFS
340- } else {
341- __fatal_error ("could not create LFS" );
342- }
343-
344- // set label
345- f_setlabel ("/flash/pybflash" );
346-
347- // create empty main.py
348- FIL fp ;
349- f_open (& fp , "/flash/main.py" , FA_WRITE | FA_CREATE_ALWAYS );
350- UINT n ;
351- f_write (& fp , fresh_main_py , sizeof (fresh_main_py ) - 1 /* don't count null terminator */ , & n );
352- // TODO check we could write n bytes
353- f_close (& fp );
354-
355- // create .inf driver file
356- f_open (& fp , "/flash/pybcdc.inf" , FA_WRITE | FA_CREATE_ALWAYS );
357- f_write (& fp , fresh_pybcdc_inf , sizeof (fresh_pybcdc_inf ) - 1 /* don't count null terminator */ , & n );
358- f_close (& fp );
359-
360- // create readme file
361- f_open (& fp , "/flash/README.txt" , FA_WRITE | FA_CREATE_ALWAYS );
362- f_write (& fp , fresh_readme_txt , sizeof (fresh_readme_txt ) - 1 /* don't count null terminator */ , & n );
363- f_close (& fp );
364-
365- // keep LED on for at least 200ms
366- sys_tick_wait_at_least (start_tick , 200 );
367- led_state (PYB_LED_R2 , 0 );
368- } else if (res == FR_OK ) {
369- // mount sucessful
370- } else {
371- __fatal_error ("could not access LFS" );
372- }
373- }
374-
375- // The current directory is used as the boot up directory.
376- // It is set to the internal flash filesystem by default.
377- f_chdrive ("/flash" );
378-
379- // Make sure we have a /flash/boot.py. Create it if needed.
380- {
381- FILINFO fno ;
382- #if _USE_LFN
383- fno .lfname = NULL ;
384- fno .lfsize = 0 ;
385- #endif
386- FRESULT res = f_stat ("/flash/boot.py" , & fno );
387- if (res == FR_OK ) {
388- if (fno .fattrib & AM_DIR ) {
389- // exists as a directory
390- // TODO handle this case
391- // see http://elm-chan.org/fsw/ff/img/app2.c for a "rm -rf" implementation
392- } else {
393- // exists as a file, good!
394- }
395- } else {
396- // doesn't exist, create fresh file
397-
398- // LED on to indicate creation of boot.py
399- led_state (PYB_LED_R2 , 1 );
400- uint32_t start_tick = HAL_GetTick ();
401-
402- FIL fp ;
403- f_open (& fp , "/flash/boot.py" , FA_WRITE | FA_CREATE_ALWAYS );
404- UINT n ;
405- f_write (& fp , fresh_boot_py , sizeof (fresh_boot_py ) - 1 /* don't count null terminator */ , & n );
406- // TODO check we could write n bytes
407- f_close (& fp );
408-
409- // keep LED on for at least 200ms
410- sys_tick_wait_at_least (start_tick , 200 );
411- led_state (PYB_LED_R2 , 0 );
412- }
413- }
415+ // Create it if needed, mount in on /flash, and set it as current dir.
416+ init_flash_fs (reset_mode );
414417
415418#if defined(USE_DEVICE_MODE )
416419 usb_storage_medium_t usb_medium = USB_STORAGE_MEDIUM_FLASH ;
0 commit comments