49
49
#include "hardware/rtc.h"
50
50
#include "hardware/structs/rosc.h"
51
51
52
+ #if MICROPY_VFS_FAT && MICROPY_HW_USB_MSC
53
+ #include "extmod/vfs.h"
54
+ #include "extmod/vfs_fat.h"
55
+ extern bool tud_msc_device_ejected ();
56
+ #endif
57
+
52
58
extern uint8_t __StackTop , __StackBottom ;
53
59
static char gc_heap [192 * 1024 ];
60
+ static volatile bool mp_initialized = false;
54
61
55
62
// Embed version info in the binary in machine readable form
56
63
bi_decl (bi_program_version_string (MICROPY_GIT_TAG ));
@@ -61,6 +68,50 @@ bi_decl(bi_program_feature_group_with_flags(BINARY_INFO_TAG_MICROPYTHON,
61
68
BINARY_INFO_ID_MP_FROZEN , "frozen modules" ,
62
69
BI_NAMED_GROUP_SEPARATE_COMMAS | BI_NAMED_GROUP_SORT_ALPHA ));
63
70
71
+ #if MICROPY_VFS_FAT && MICROPY_HW_USB_MSC
72
+ #define debug_printf (...) mp_printf(&mp_plat_print, "main.c: " __VA_ARGS__)
73
+
74
+ int tud_msc_device_ejected_cb (bool ejected ) {
75
+ if (mp_initialized == false) {
76
+ // Make sure this is not called before MP is initialized.
77
+ return 0 ;
78
+ }
79
+
80
+ mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION ();
81
+ int ret = 0 ;
82
+ const char * mount_point_str = "/flash" ;
83
+ mp_obj_t mount_point = mp_obj_new_str (mount_point_str , strlen (mount_point_str ));
84
+ nlr_buf_t nlr ;
85
+ if (nlr_push (& nlr ) == 0 ) {
86
+ if (ejected ) {
87
+ // Mount filesystem to allow device access.
88
+ mp_obj_t bdev = rp2_flash_type .make_new (& rp2_flash_type , 0 , 0 , NULL );
89
+ mp_obj_t args [] = { bdev , mount_point , MP_OBJ_NEW_QSTR (MP_QSTR_mkfs ), mp_const_true };
90
+ mp_map_t kw_args ;
91
+ mp_map_init_fixed_table (& kw_args , 1 , args + 2 );
92
+ mp_vfs_mount (2 , args , & kw_args );
93
+ mp_vfs_chdir (mount_point );
94
+ } else {
95
+ // Unmount filesystem to allow host access.
96
+ mp_vfs_umount (mount_point );
97
+ }
98
+ nlr_pop ();
99
+ } else {
100
+ ret = -1 ;
101
+ }
102
+ MICROPY_END_ATOMIC_SECTION (atomic_state );
103
+ return ret ;
104
+ }
105
+
106
+ void tud_mount_cb (void ) {
107
+ tud_msc_device_ejected_cb (false);
108
+ }
109
+
110
+ void tud_umount_cb (void ) {
111
+ tud_msc_device_ejected_cb (true);
112
+ }
113
+ #endif
114
+
64
115
int main (int argc , char * * argv ) {
65
116
#if MICROPY_HW_ENABLE_UART_REPL
66
117
bi_decl (bi_program_feature ("UART REPL" ))
@@ -101,6 +152,7 @@ int main(int argc, char **argv) {
101
152
// Initialise MicroPython runtime.
102
153
mp_init ();
103
154
mp_obj_list_append (mp_sys_path , MP_OBJ_NEW_QSTR (MP_QSTR__slash_lib ));
155
+ mp_initialized = true;
104
156
105
157
// Initialise sub-systems.
106
158
readline_init0 ();
@@ -115,10 +167,11 @@ int main(int argc, char **argv) {
115
167
mod_network_init ();
116
168
#endif
117
169
118
- // Execute _boot.py to set up the filesystem.
119
170
#if MICROPY_VFS_FAT && MICROPY_HW_USB_MSC
120
- pyexec_frozen_module ("_boot_fat.py" );
171
+ // Mount flash filesystem for device if not connected to a host.
172
+ tud_msc_device_ejected_cb (!tud_connected () || tud_msc_device_ejected ());
121
173
#else
174
+ // Execute _boot.py to set up the filesystem.
122
175
pyexec_frozen_module ("_boot.py" );
123
176
#endif
124
177
@@ -147,6 +200,7 @@ int main(int argc, char **argv) {
147
200
}
148
201
149
202
soft_reset_exit :
203
+ mp_initialized = false;
150
204
mp_printf (MP_PYTHON_PRINTER , "MPY: soft reboot\n" );
151
205
#if MICROPY_PY_NETWORK
152
206
mod_network_deinit ();
0 commit comments