Skip to content

Commit f88d611

Browse files
committed
mimxrt: Allow local write only after a soft reset.
Since remount after MSC eject does not update the file system's data in memory, keep file the system readonly until after a soft reset, which has to be done deliberately. That's neither comfortable nor elegant, but safe. Disabled MSC as default option. Disabled MSC for MIMXRT1050_EVK deliberately. Enabled MSC for MIMXRT1020_EVK, Teensy 4.1 and OLIMEX_RT1010 for testing.
1 parent ba82ce3 commit f88d611

File tree

9 files changed

+31
-42
lines changed

9 files changed

+31
-42
lines changed

ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#define MICROPY_HW_BOARD_NAME "i.MX RT1020 EVK"
22
#define MICROPY_HW_MCU_NAME "MIMXRT1021DAG5A"
33

4+
#define MICROPY_HW_USB_MSC (1)
5+
46
// i.MX RT1020 EVK has 1 board LED
57
// Todo: think about replacing the define with searching in the generated pins?
68
#define MICROPY_HW_LED1_PIN (pin_GPIO_AD_B0_05)

ports/mimxrt/boards/OLIMEX_RT1010/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#define MICROPY_HW_USB_PID 0x0046
66
#define MICROPY_PY_UOS_DUPTERM_BUILTIN_STREAM (0)
77

8+
#define MICROPY_HW_USB_MSC (1)
9+
810
// Olimex RT1010-Py has 1 board LED
911
#define MICROPY_HW_LED1_PIN (pin_GPIO_11)
1012
#define MICROPY_HW_LED_ON(pin) (mp_hal_pin_high(pin))

ports/mimxrt/boards/TEENSY41/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#define MICROPY_HW_BOARD_NAME "Teensy 4.1"
22
#define MICROPY_HW_MCU_NAME "MIMXRT1062DVJ6A"
33

4+
#define MICROPY_HW_USB_MSC (1)
5+
46
// Teensy 4.1 has 1 board LED
57
#define MICROPY_HW_LED1_PIN (pin_GPIO_B0_03)
68
#define MICROPY_HW_LED_ON(pin) (mp_hal_pin_high(pin))

ports/mimxrt/flash.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ extern uint8_t __flash_start;
4545

4646
extern flexspi_nor_config_t qspiflash_config;
4747

48+
enum {
49+
MOUNTED = 0,
50+
EJECTED,
51+
TRANSIT
52+
};
53+
4854
// --------------------------------------------------------------------+
4955
// Global Function Declarations
5056
// --------------------------------------------------------------------+

ports/mimxrt/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
extern uint8_t _sstack, _estack, _gc_heap_start, _gc_heap_end;
5050

5151
void board_init(void);
52+
void update_msc_state(void);
5253

5354
int main(void) {
5455
board_init();
@@ -123,6 +124,9 @@ int main(void) {
123124
#if MICROPY_PY_NETWORK
124125
mod_network_deinit();
125126
#endif
127+
#if CFG_TUD_MSC
128+
update_msc_state();
129+
#endif
126130
machine_pwm_deinit_all();
127131
gc_sweep_all();
128132
mp_deinit();

ports/mimxrt/mimxrt_flash.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ STATIC mimxrt_flash_obj_t mimxrt_flash_obj = {
4545
.base = { &mimxrt_flash_type }
4646
};
4747

48-
extern bool tud_msc_ejected;
48+
extern uint8_t tud_msc_state;
4949

5050
STATIC mp_obj_t mimxrt_flash_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
5151
// Check args.
@@ -145,7 +145,7 @@ STATIC mp_obj_t mimxrt_flash_ioctl(mp_obj_t self_in, mp_obj_t cmd_in, mp_obj_t a
145145
}
146146
case MP_BLOCKDEV_IOCTL_STATUS:
147147
#if MICROPY_HW_USB_MSC
148-
return MP_OBJ_NEW_SMALL_INT(!tud_msc_ejected);
148+
return MP_OBJ_NEW_SMALL_INT(tud_msc_state != EJECTED);
149149
#else
150150
return MP_OBJ_NEW_SMALL_INT(false);
151151
#endif

ports/mimxrt/mpconfigport.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,12 @@ uint32_t trng_random_u32(void);
191191
// by default enable MSC support, unless disabled at some boards
192192
// Boards with hyperflash must disbale MSC support.
193193
#ifndef MICROPY_HW_USB_MSC
194-
#define MICROPY_HW_USB_MSC (1)
194+
#define MICROPY_HW_USB_MSC (0)
195195
#endif
196196

197197
#if MICROPY_HW_USB_MSC
198198
#define MICROPY_FATFS_USE_LABEL (1)
199199
#define MICROPY_FATFS_MULTI_PARTITION (1)
200-
#define MICROPY_HW_USB_MSC_EXCLUSIVE_ACCESS (1)
201200
#endif
202201

203202
#if MICROPY_VFS_FAT && MICROPY_HW_USB_MSC

ports/mimxrt/msc_disk.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,13 @@
3434
#define BLOCK_COUNT (MICROPY_HW_FLASH_STORAGE_BYTES / BLOCK_SIZE)
3535
#define FLASH_BASE_ADDR (MICROPY_HW_FLASH_STORAGE_BASE)
3636

37-
bool tud_msc_ejected = true;
38-
extern void tud_msc_remount(void);
37+
uint8_t tud_msc_state = EJECTED;
38+
39+
void update_msc_state(void) {
40+
if (tud_msc_state == TRANSIT) {
41+
tud_msc_state = EJECTED;
42+
}
43+
}
3944

4045
// Invoked when received SCSI_CMD_INQUIRY
4146
// Application fill vendor id, product id and revision with string up to 8, 16, 4 characters respectively
@@ -47,13 +52,13 @@ void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16
4752
strncpy((char *)vendor_id, vid, 8);
4853
strncpy((char *)product_id, pid, 16);
4954
strncpy((char *)product_rev, rev, 4);
50-
tud_msc_ejected = false;
55+
tud_msc_state = MOUNTED;
5156
}
5257

5358
// Invoked when received Test Unit Ready command.
5459
// return true allowing host to read/write this LUN e.g SD card inserted
5560
bool tud_msc_test_unit_ready_cb(uint8_t lun) {
56-
if (tud_msc_ejected) {
61+
if (tud_msc_state != MOUNTED) {
5762
tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3a, 0x00);
5863
return false;
5964
}
@@ -65,7 +70,6 @@ bool tud_msc_test_unit_ready_cb(uint8_t lun) {
6570
void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count, uint16_t *block_size) {
6671
*block_size = BLOCK_SIZE;
6772
*block_count = BLOCK_COUNT;
68-
tud_msc_ejected = false;
6973
}
7074

7175
// Invoked when received Start Stop Unit command
@@ -75,13 +79,10 @@ bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, boo
7579
if (load_eject) {
7680
if (start) {
7781
// load disk storage
78-
tud_msc_ejected = false;
82+
tud_msc_state = MOUNTED;
7983
} else {
8084
// unload disk storage
81-
tud_msc_ejected = true;
82-
#if MICROPY_HW_USB_MSC_EXCLUSIVE_ACCESS
83-
tud_msc_remount();
84-
#endif
85+
tud_msc_state = TRANSIT;
8586
}
8687
}
8788
return true;
@@ -125,4 +126,5 @@ int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void *buffer, u
125126
}
126127
return resplen;
127128
}
129+
128130
#endif

shared/runtime/tinyusb_helpers.c

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -52,31 +52,3 @@ void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
5252
}
5353

5454
#endif
55-
56-
#if MICROPY_HW_USB_MSC_EXCLUSIVE_ACCESS
57-
#include "tusb.h"
58-
#include "extmod/vfs.h"
59-
#include "extmod/vfs_fat.h"
60-
static mp_sched_node_t mp_remount_sched_node;
61-
62-
STATIC void tud_msc_remount_task(mp_sched_node_t *node) {
63-
mp_vfs_mount_t *vfs = NULL;
64-
for (vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) {
65-
if (vfs->len == 1) {
66-
const char *path_str = "/";
67-
mp_obj_t path = mp_obj_new_str(path_str, strlen(path_str));
68-
nlr_buf_t nlr;
69-
if (nlr_push(&nlr) == 0) {
70-
mp_vfs_umount(vfs->obj);
71-
mp_vfs_mount_and_chdir_protected(vfs->obj, path);
72-
nlr_pop();
73-
}
74-
break;
75-
}
76-
}
77-
}
78-
79-
void tud_msc_remount(void) {
80-
mp_sched_schedule_node(&mp_remount_sched_node, tud_msc_remount_task);
81-
}
82-
#endif

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy