From 2407ccdfb8df1bce4d617266973534a490c449f2 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Thu, 18 Jan 2024 13:41:06 +0000 Subject: [PATCH 1/4] extmod/vfs_fat.c: Add method for setting filesystem label. Signed-off-by: Phil Howard --- extmod/vfs_fat.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index ee1169b8c3193..6f3582f550660 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -39,6 +39,7 @@ #include #include "py/runtime.h" #include "py/mperrno.h" +#include "py/objstr.h" #include "lib/oofatfs/ff.h" #include "extmod/vfs_fat.h" #include "shared/timeutils/timeutils.h" @@ -378,6 +379,22 @@ static mp_obj_t fat_vfs_statvfs(mp_obj_t vfs_in, mp_obj_t path_in) { } static MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_statvfs_obj, fat_vfs_statvfs); +static mp_obj_t fat_vfs_label(mp_obj_t vfs_in, mp_obj_t label_in) { + #ifdef MICROPY_FATFS_USE_LABEL + mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in); + const char *label = mp_obj_str_get_str(label_in); + + FRESULT res = f_setlabel(&self->fatfs, label); + + if (FR_OK != res) { + mp_raise_OSError(fresult_to_errno_table[res]); + } + + #endif + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_label_obj, fat_vfs_label); + static mp_obj_t vfs_fat_mount(mp_obj_t self_in, mp_obj_t readonly, mp_obj_t mkfs) { fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in); @@ -426,6 +443,7 @@ static const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_rename), MP_ROM_PTR(&fat_vfs_rename_obj) }, { MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&fat_vfs_stat_obj) }, { MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&fat_vfs_statvfs_obj) }, + { MP_ROM_QSTR(MP_QSTR_label), MP_ROM_PTR(&fat_vfs_label_obj) }, { MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&vfs_fat_mount_obj) }, { MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&fat_vfs_umount_obj) }, }; From acafe3a63b8955929df81af0208b4361ec81d5f7 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Thu, 18 Jan 2024 13:47:20 +0000 Subject: [PATCH 2/4] ports/rp2: Set MSC label. Set the USB mass storage label when the filesystem is created. Signed-off-by: Phil Howard --- ports/rp2/modules/_boot_fat.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ports/rp2/modules/_boot_fat.py b/ports/rp2/modules/_boot_fat.py index 02d1f99f0daf3..b7647f3f2fe64 100644 --- a/ports/rp2/modules/_boot_fat.py +++ b/ports/rp2/modules/_boot_fat.py @@ -8,6 +8,8 @@ vfs.mount(vfs.VfsFat(bdev), "/") except: vfs.VfsFat.mkfs(bdev) - vfs.mount(vfs.VfsFat(bdev), "/") + msc = vfs.VfsFat(bdev) + msc.label("RP2_MSC") + vfs.mount(msc, "/") del vfs, bdev From 360c61c81b714293664a2d4503176611ab983fec Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Fri, 19 Jan 2024 09:45:23 +0000 Subject: [PATCH 3/4] Experimental dual MSC. --- ports/rp2/boards/RPI_PICO/mpconfigboard.h | 9 ++++++++ ports/rp2/modules/_boot_fat.py | 27 ++++++++++++++++------- ports/rp2/msc_disk.c | 22 ++++++++++++++---- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/ports/rp2/boards/RPI_PICO/mpconfigboard.h b/ports/rp2/boards/RPI_PICO/mpconfigboard.h index c39bc4d489bb6..ceecc921dae6c 100644 --- a/ports/rp2/boards/RPI_PICO/mpconfigboard.h +++ b/ports/rp2/boards/RPI_PICO/mpconfigboard.h @@ -1,3 +1,12 @@ // Board and hardware specific configuration #define MICROPY_HW_BOARD_NAME "Raspberry Pi Pico" #define MICROPY_HW_FLASH_STORAGE_BYTES (1408 * 1024) + +#define MICROPY_HW_USB_MSC (1) +#define MICROPY_HW_USB_VID (0x1FFB) +#define MICROPY_HW_USB_PID (0x2043) +#define MICROPY_HW_USB_DESC_STR_MAX (40) +#define MICROPY_HW_USB_MANUFACTURER_STRING "Pimoroni" +#define MICROPY_HW_USB_PRODUCT_FS_STRING MICROPY_HW_BOARD_NAME " MicroPython" + +#define MICROPY_BANNER_MACHINE MICROPY_HW_BOARD_NAME \ No newline at end of file diff --git a/ports/rp2/modules/_boot_fat.py b/ports/rp2/modules/_boot_fat.py index b7647f3f2fe64..7d4eef88a94ec 100644 --- a/ports/rp2/modules/_boot_fat.py +++ b/ports/rp2/modules/_boot_fat.py @@ -1,15 +1,26 @@ import vfs import machine, rp2 +# 1441792 +bdev_root = rp2.Flash(start=0, len=4096 * 100) +bdev_storage = rp2.Flash(start=4096 * 100, len=4096 * 250) -# Try to mount the filesystem, and format the flash if it doesn't exist. -bdev = rp2.Flash() try: - vfs.mount(vfs.VfsFat(bdev), "/") + vfs_root = vfs.VfsFat(bdev_root) + vfs.mount(vfs_root, "/", readonly=True) except: - vfs.VfsFat.mkfs(bdev) - msc = vfs.VfsFat(bdev) - msc.label("RP2_MSC") - vfs.mount(msc, "/") + vfs.VfsFat.mkfs(bdev_root) + vfs_root = vfs.VfsFat(bdev_root) + vfs_root.label("Root") + vfs.mount(vfs_root, "/", readonly=True) -del vfs, bdev +try: + vfs_storage = vfs.VfsFat(bdev_storage) + vfs.mount(vfs_storage, "/storage") +except: + vfs.VfsFat.mkfs(bdev_storage) + vfs_storage = vfs.VfsFat(bdev_storage) + vfs_storage.label("Storage") + vfs.mount(vfs_storage, "/storage") + +del bdev_root, bdev_storage, vfs_storage, vfs_root diff --git a/ports/rp2/msc_disk.c b/ports/rp2/msc_disk.c index 0937eeddf0d06..8d8f7b156eb3d 100644 --- a/ports/rp2/msc_disk.c +++ b/ports/rp2/msc_disk.c @@ -41,6 +41,17 @@ static bool ejected = false; +// Invoked to determine max LUN +uint8_t tud_msc_get_maxlun_cb(void) +{ + return 2; // dual LUN +} + +bool tud_msc_is_writable_cb (uint8_t lun) +{ + return lun == 0; // Only BOOT is writable from host, storage is not +} + // Invoked when received SCSI_CMD_INQUIRY // Application fill vendor id, product id and revision with string up to 8, 16, 4 characters respectively void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]) { @@ -63,7 +74,8 @@ bool tud_msc_test_unit_ready_cb(uint8_t lun) { // Application update block count and block size void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count, uint16_t *block_size) { *block_size = BLOCK_SIZE; - *block_count = BLOCK_COUNT; + //*block_count = BLOCK_COUNT; + *block_count = (lun == 1) ? 250 : 100; } // Invoked when received Start Stop Unit command @@ -86,7 +98,8 @@ bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, boo // Copy disk's data to buffer (up to bufsize) and return number of copied bytes. int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize) { uint32_t count = bufsize / BLOCK_SIZE; - memcpy(buffer, (void *)(FLASH_MMAP_ADDR + lba * BLOCK_SIZE), count * BLOCK_SIZE); + uint32_t lun_offset = lun == 1 ? 100 : 0; + memcpy(buffer, (void *)(FLASH_MMAP_ADDR + (lun_offset + lba) * BLOCK_SIZE), count * BLOCK_SIZE); return count * BLOCK_SIZE; } @@ -94,9 +107,10 @@ int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buff // Process data in buffer to disk's storage and return number of written bytes int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t *buffer, uint32_t bufsize) { uint32_t count = bufsize / BLOCK_SIZE; + uint32_t lun_offset = lun == 1 ? 100 : 0; uint32_t ints = save_and_disable_interrupts(); - flash_range_erase(FLASH_BASE_ADDR + lba * BLOCK_SIZE, count * BLOCK_SIZE); - flash_range_program(FLASH_BASE_ADDR + lba * BLOCK_SIZE, buffer, count * BLOCK_SIZE); + flash_range_erase(FLASH_BASE_ADDR + (lun_offset + lba) * BLOCK_SIZE, count * BLOCK_SIZE); + flash_range_program(FLASH_BASE_ADDR + (lun_offset + lba) * BLOCK_SIZE, buffer, count * BLOCK_SIZE); restore_interrupts(ints); return count * BLOCK_SIZE; } From 493000a086b8bc16783711d3c4ce6428812fad4f Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Fri, 19 Jan 2024 10:47:55 +0000 Subject: [PATCH 4/4] Attempt to use whole flash. --- ports/rp2/modules/_boot_fat.py | 9 ++++++--- ports/rp2/msc_disk.c | 7 ++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/ports/rp2/modules/_boot_fat.py b/ports/rp2/modules/_boot_fat.py index 7d4eef88a94ec..698c023ed9ebc 100644 --- a/ports/rp2/modules/_boot_fat.py +++ b/ports/rp2/modules/_boot_fat.py @@ -1,9 +1,12 @@ import vfs import machine, rp2 -# 1441792 -bdev_root = rp2.Flash(start=0, len=4096 * 100) -bdev_storage = rp2.Flash(start=4096 * 100, len=4096 * 250) +blocks_root = 100 +blocks_total = rp2.Flash().ioctl(4, None) +block_size = rp2.Flash().ioctl(5, None) +blocks_storage = blocks_total - blocks_root +bdev_root = rp2.Flash(start=0, len=block_size * blocks_root) +bdev_storage = rp2.Flash(start=block_size * blocks_root, len=block_size * blocks_storage) try: vfs_root = vfs.VfsFat(bdev_root) diff --git a/ports/rp2/msc_disk.c b/ports/rp2/msc_disk.c index 8d8f7b156eb3d..d86edb44917ed 100644 --- a/ports/rp2/msc_disk.c +++ b/ports/rp2/msc_disk.c @@ -38,6 +38,7 @@ #define BLOCK_COUNT (MICROPY_HW_FLASH_STORAGE_BYTES / BLOCK_SIZE) #define FLASH_BASE_ADDR (PICO_FLASH_SIZE_BYTES - MICROPY_HW_FLASH_STORAGE_BYTES) #define FLASH_MMAP_ADDR (XIP_BASE + FLASH_BASE_ADDR) +#define ROOT_BLOCK_COUNT 100 static bool ejected = false; @@ -75,7 +76,7 @@ bool tud_msc_test_unit_ready_cb(uint8_t lun) { void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count, uint16_t *block_size) { *block_size = BLOCK_SIZE; //*block_count = BLOCK_COUNT; - *block_count = (lun == 1) ? 250 : 100; + *block_count = (lun == 1) ? (BLOCK_COUNT - ROOT_BLOCK_COUNT) : ROOT_BLOCK_COUNT; } // Invoked when received Start Stop Unit command @@ -98,7 +99,7 @@ bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, boo // Copy disk's data to buffer (up to bufsize) and return number of copied bytes. int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize) { uint32_t count = bufsize / BLOCK_SIZE; - uint32_t lun_offset = lun == 1 ? 100 : 0; + uint32_t lun_offset = lun == 1 ? ROOT_BLOCK_COUNT : 0; memcpy(buffer, (void *)(FLASH_MMAP_ADDR + (lun_offset + lba) * BLOCK_SIZE), count * BLOCK_SIZE); return count * BLOCK_SIZE; } @@ -107,7 +108,7 @@ int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buff // Process data in buffer to disk's storage and return number of written bytes int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t *buffer, uint32_t bufsize) { uint32_t count = bufsize / BLOCK_SIZE; - uint32_t lun_offset = lun == 1 ? 100 : 0; + uint32_t lun_offset = lun == 1 ? ROOT_BLOCK_COUNT : 0; uint32_t ints = save_and_disable_interrupts(); flash_range_erase(FLASH_BASE_ADDR + (lun_offset + lba) * BLOCK_SIZE, count * BLOCK_SIZE); flash_range_program(FLASH_BASE_ADDR + (lun_offset + lba) * BLOCK_SIZE, buffer, count * BLOCK_SIZE); 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