8000 rp2: Allow exclusive access to storage. · micropython/micropython@3589583 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3589583

Browse files
committed
rp2: Allow exclusive access to storage.
1 parent 0d6d74e commit 3589583

File tree

3 files changed

+67
-18
lines changed

3 files changed

+67
-18
lines changed

ports/rp2/main.c

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,15 @@
4949
#include "hardware/rtc.h"
5050
#include "hardware/structs/rosc.h"
5151

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+
5258
extern uint8_t __StackTop, __StackBottom;
5359
static char gc_heap[192 * 1024];
60+
static volatile bool mp_initialized = false;
5461

5562
// Embed version info in the binary in machine readable form
5663
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,
6168
BINARY_INFO_ID_MP_FROZEN, "frozen modules",
6269
BI_NAMED_GROUP_SEPARATE_COMMAS | BI_NAMED_GROUP_SORT_ALPHA));
6370

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+
64115
int main(int argc, char **argv) {
65116
#if MICROPY_HW_ENABLE_UART_REPL
66117
bi_decl(bi_program_feature("UART REPL"))
@@ -101,6 +152,7 @@ int main(int argc, char **argv) {
101152
// Initialise MicroPython runtime.
102153
mp_init();
103154
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib));
155+
mp_initialized = true;
104156

105157
// Initialise sub-systems.
106158
readline_init0();
@@ -115,10 +167,11 @@ int main(int argc, char **argv) {
115167
mod_network_init();
116168
#endif
117169

118-
// Execute _boot.py to set up the filesystem.
119170
#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());
121173
#else
174+
// Execute _boot.py to set up the filesystem.
122175
pyexec_frozen_module("_boot.py");
123176
#endif
124177

@@ -147,6 +200,7 @@ int main(int argc, char **argv) {
147200
}
148201

149202
soft_reset_exit:
203+
mp_initialized = false;
150204
mp_printf(MP_PYTHON_PRINTER, "MPY: soft reboot\n");
151205
#if MICROPY_PY_NETWORK
152206
mod_network_deinit();

ports/rp2/modules/_boot_fat.py

Lines changed: 0 additions & 16 deletions
This file was deleted.

ports/rp2/msc_disk.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "hardware/flash.h"
2929
#include "hardware/sync.h"
3030
#include "pico/stdlib.h"
31+
#include "py/mphal.h"
3132

3233
// This implementation does Not support Flash sector caching.
3334
#if MICROPY_FATFS_MAX_SS != FLASH_SECTOR_SIZE
@@ -41,6 +42,15 @@
4142

4243
static bool ejected = false;
4344

45+
bool tud_msc_device_ejected() {
46+
return ejected;
47+
}
48+
49+
MP_WEAK int tud_msc_device_ejected_cb(bool ejected) {
50+
(void)ejected;
51+
return 0;
52+
}
53+
4454
// Invoked when received SCSI_CMD_INQUIRY
4555
// Application fill vendor id, product id and revision with string up to 8, 16, 4 characters respectively
4656
void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]) {
@@ -82,6 +92,7 @@ bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, boo
8292
// unload disk storage
8393
ejected = true;
8494
}
95+
tud_msc_device_ejected_cb(ejected);
8596
}
8697
return true;
8798
}

0 commit comments

Comments
 (0)
0