From c169094f615c9337610703399be1cfb02a6ff120 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 13 Nov 2019 17:31:35 +1100 Subject: [PATCH 1/7] stm32/storage: Make pyb.Flash configurable, and support ext block proto. The pyb.Flash() class can now be used to construct objects which reference sections of the flash storage, starting at a certain offset and going for a certain length. Such objects also support the extended block protocol. The signature for the constructor is: pyb.Flash(start=-1, len=-1). --- ports/stm32/flashbdev.c | 40 ++++++++ ports/stm32/spibdev.c | 23 +++++ ports/stm32/storage.c | 196 ++++++++++++++++++++++++++++++++++++---- ports/stm32/storage.h | 11 ++- 4 files changed, 249 insertions(+), 21 deletions(-) diff --git a/ports/stm32/flashbdev.c b/ports/stm32/flashbdev.c index 15bf0d6b0a837..beb28c4925bd0 100644 --- a/ports/stm32/flashbdev.c +++ b/ports/stm32/flashbdev.c @@ -292,4 +292,44 @@ bool flash_bdev_writeblock(const uint8_t *src, uint32_t block) { return true; } +int flash_bdev_readblocks_ext(uint8_t *dest, uint32_t block, uint32_t offset, uint32_t len) { + // Get data from flash memory, possibly via cache + while (len) { + uint32_t l = MIN(len, FLASH_BLOCK_SIZE - offset); + uint32_t flash_addr = convert_block_to_flash_addr(block); + if (flash_addr == -1) { + // bad block number + return -1; + } + uint8_t *src = flash_cache_get_addr_for_read(flash_addr + offset); + memcpy(dest, src, l); + dest += l; + block += 1; + offset = 0; + len -= l; + } + return 0; +} + +int flash_bdev_writeblocks_ext(const uint8_t *src, uint32_t block, uint32_t offset, uint32_t len) { + // Copy to cache + while (len) { + uint32_t l = MIN(len, FLASH_BLOCK_SIZE - offset); + uint32_t flash_addr = convert_block_to_flash_addr(block); + if (flash_addr == -1) { + // bad block number + return -1; + } + uint32_t basepri = raise_irq_pri(IRQ_PRI_FLASH); // prevent cache flushing and USB access + uint8_t *dest = flash_cache_get_addr_for_write(flash_addr + offset); + memcpy(dest, src, l); + restore_irq_pri(basepri); + src += l; + block += 1; + offset = 0; + len -= l; + } + return 0; +} + #endif // MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE diff --git a/ports/stm32/spibdev.c b/ports/stm32/spibdev.c index 9b5a10b400372..97ce885d453e8 100644 --- a/ports/stm32/spibdev.c +++ b/ports/stm32/spibdev.c @@ -55,6 +55,13 @@ int32_t spi_bdev_ioctl(spi_bdev_t *bdev, uint32_t op, uint32_t arg) { restore_irq_pri(basepri); } return 0; + + case BDEV_IOCTL_BLOCK_ERASE: { + uint32_t basepri = raise_irq_pri(IRQ_PRI_FLASH); // prevent cache flushing and USB access + mp_spiflash_erase_block(&bdev->spiflash, arg * MP_SPIFLASH_ERASE_BLOCK_SIZE); + restore_irq_pri(basepri); + return 0; + } } return -MP_EINVAL; } @@ -79,4 +86,20 @@ int spi_bdev_writeblocks(spi_bdev_t *bdev, const uint8_t *src, uint32_t block_nu return ret; } +int spi_bdev_readblocks_raw(spi_bdev_t *bdev, uint8_t *dest, uint32_t block_num, uint32_t block_offset, uint32_t num_bytes) { + uint32_t basepri = raise_irq_pri(IRQ_PRI_FLASH); // prevent cache flushing and USB access + mp_spiflash_read(&bdev->spiflash, block_num * MP_SPIFLASH_ERASE_BLOCK_SIZE + block_offset, num_bytes, dest); + restore_irq_pri(basepri); + + return 0; +} + +int spi_bdev_writeblocks_raw(spi_bdev_t *bdev, const uint8_t *src, uint32_t block_num, uint32_t block_offset, uint32_t num_bytes) { + uint32_t basepri = raise_irq_pri(IRQ_PRI_FLASH); // prevent cache flushing and USB access + int ret = mp_spiflash_write(&bdev->spiflash, block_num * MP_SPIFLASH_ERASE_BLOCK_SIZE + block_offset, num_bytes, src); + restore_irq_pri(basepri); + + return ret; +} + #endif diff --git a/ports/stm32/storage.c b/ports/stm32/storage.c index 905d51f325354..0d3285ba051d7 100644 --- a/ports/stm32/storage.c +++ b/ports/stm32/storage.c @@ -28,6 +28,7 @@ #include #include "py/runtime.h" +#include "py/mperrno.h" #include "extmod/vfs_fat.h" #include "systick.h" @@ -233,41 +234,197 @@ mp_uint_t storage_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t // // Expose the flash as an object with the block protocol. -// there is a singleton Flash object -const mp_obj_base_t pyb_flash_obj = {&pyb_flash_type}; +#ifdef MICROPY_HW_BDEV_SPIFLASH_EXTENDED +// Board defined an external SPI flash for use with extended block protocol +#define SPIFLASH (MICROPY_HW_BDEV_SPIFLASH_EXTENDED) +#define PYB_FLASH_NATIVE_BLOCK_SIZE (MP_SPIFLASH_ERASE_BLOCK_SIZE) +#define MICROPY_HW_BDEV_READBLOCKS_EXT(dest, bl, off, len) (spi_bdev_readblocks_raw(SPIFLASH, (dest), (bl), (off), (len))) +#define MICROPY_HW_BDEV_WRITEBLOCKS_EXT(dest, bl, off, len) (spi_bdev_writeblocks_raw(SPIFLASH, (dest), (bl), (off), (len))) + +#elif (MICROPY_VFS_LFS1 || MICROPY_VFS_LFS2) && MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE +// Board uses littlefs and internal flash, so enable extended block protocol on internal flash +#define PYB_FLASH_NATIVE_BLOCK_SIZE (FLASH_BLOCK_SIZE) +#define MICROPY_HW_BDEV_READBLOCKS_EXT(dest, bl, off, len) (flash_bdev_readblocks_ext((dest), (bl), (off), (len))) +#define MICROPY_HW_BDEV_WRITEBLOCKS_EXT(dest, bl, off, len) (flash_bdev_writeblocks_ext((dest), (bl), (off), (len))) +#endif + +#ifndef PYB_FLASH_NATIVE_BLOCK_SIZE +#define PYB_FLASH_NATIVE_BLOCK_SIZE (FLASH_BLOCK_SIZE) +#endif + +typedef struct _pyb_flash_obj_t { + mp_obj_base_t base; + uint32_t start; // in bytes + uint32_t len; // in bytes + #if defined(SPIFLASH) + bool use_native_block_size; + #endif +} pyb_flash_obj_t; + +// This Flash object represents the entire available flash, with emulated partition table at start +const pyb_flash_obj_t pyb_flash_obj = { + { &pyb_flash_type }, + -(FLASH_PART1_START_BLOCK * FLASH_BLOCK_SIZE), // to offset FLASH_PART1_START_BLOCK + 0, // actual size handled in ioctl, MP_BLOCKDEV_IOCTL_BLOCK_COUNT case +}; + +STATIC void pyb_flash_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + pyb_flash_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (self == &pyb_flash_obj) { + mp_printf(print, "Flash()"); + } else { + mp_printf(print, "Flash(start=%u, len=%u)", self->start, self->len); + } +} + +STATIC mp_obj_t pyb_flash_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + // Parse arguments + enum { ARG_start, ARG_len }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_start, MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_len, MP_ARG_INT, {.u_int = -1} }, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + if (args[ARG_start].u_int == -1 && args[ARG_len].u_int == -1) { + // Default singleton object that accesses entire flash, including virtual partition table + return MP_OBJ_FROM_PTR(&pyb_flash_obj); + } + + pyb_flash_obj_t *self = m_new_obj(pyb_flash_obj_t); + self->base.type = &pyb_flash_type; + #if defined(SPIFLASH) + self->use_native_block_size = false; + #endif + + uint32_t bl_len = (storage_get_block_count() - FLASH_PART1_START_BLOCK) * FLASH_BLOCK_SIZE; + + mp_int_t start = args[ARG_start].u_int; + if (start == -1) { + start = 0; + } else if (!(0 <= start && start < bl_len && start % PYB_FLASH_NATIVE_BLOCK_SIZE == 0)) { + mp_raise_ValueError(NULL); + } + + mp_int_t len = args[ARG_len].u_int; + if (len == -1) { + len = bl_len - start; + } else if (!(0 < len && start + len <= bl_len && len % PYB_FLASH_NATIVE_BLOCK_SIZE == 0)) { + mp_raise_ValueError(NULL); + } -STATIC mp_obj_t pyb_flash_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { - // check arguments - mp_arg_check_num(n_args, n_kw, 0, 0, false); + self->start = start; + self->len = len; - // return singleton object - return MP_OBJ_FROM_PTR(&pyb_flash_obj); + return MP_OBJ_FROM_PTR(self); } -STATIC mp_obj_t pyb_flash_readblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) { +STATIC mp_obj_t pyb_flash_readblocks(size_t n_args, const mp_obj_t *args) { + pyb_flash_obj_t *self = MP_OBJ_TO_PTR(args[0]); + uint32_t block_num = mp_obj_get_int(args[1]); mp_buffer_info_t bufinfo; - mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE); - mp_uint_t ret = storage_read_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FLASH_BLOCK_SIZE); + mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_WRITE); + mp_uint_t ret = -MP_EIO; + if (n_args == 3) { + // Cast self->start to signed in case it's pyb_flash_obj with negative start + block_num += FLASH_PART1_START_BLOCK + (int32_t)self->start / FLASH_BLOCK_SIZE; + ret = storage_read_blocks(bufinfo.buf, block_num, bufinfo.len / FLASH_BLOCK_SIZE); + } + #if defined(MICROPY_HW_BDEV_READBLOCKS_EXT) + else if (self != &pyb_flash_obj) { + // Extended block read on a sub-section of the flash storage + uint32_t offset = mp_obj_get_int(args[3]); + block_num += self->start / PYB_FLASH_NATIVE_BLOCK_SIZE; + ret = MICROPY_HW_BDEV_READBLOCKS_EXT(bufinfo.buf, block_num, offset, bufinfo.len); + } + #endif return MP_OBJ_NEW_SMALL_INT(ret); } -STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_flash_readblocks_obj, pyb_flash_readblocks); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_flash_readblocks_obj, 3, 4, pyb_flash_readblocks); -STATIC mp_obj_t pyb_flash_writeblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) { +STATIC mp_obj_t pyb_flash_writeblocks(size_t n_args, const mp_obj_t *args) { + pyb_flash_obj_t *self = MP_OBJ_TO_PTR(args[0]); + uint32_t block_num = mp_obj_get_int(args[1]); mp_buffer_info_t bufinfo; - mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ); - mp_uint_t ret = storage_write_blocks(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / FLASH_BLOCK_SIZE); + mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ); + mp_uint_t ret = -MP_EIO; + if (n_args == 3) { + // Cast self->start to signed in case it's pyb_flash_obj with negative start + block_num += FLASH_PART1_START_BLOCK + (int32_t)self->start / FLASH_BLOCK_SIZE; + ret = storage_write_blocks(bufinfo.buf, block_num, bufinfo.len / FLASH_BLOCK_SIZE); + } + #if defined(MICROPY_HW_BDEV_WRITEBLOCKS_EXT) + else if (self != &pyb_flash_obj) { + // Extended block write on a sub-section of the flash storage + uint32_t offset = mp_obj_get_int(args[3]); + block_num += self->start / PYB_FLASH_NATIVE_BLOCK_SIZE; + ret = MICROPY_HW_BDEV_WRITEBLOCKS_EXT(bufinfo.buf, block_num, offset, bufinfo.len); + } + #endif return MP_OBJ_NEW_SMALL_INT(ret); } -STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_flash_writeblocks_obj, pyb_flash_writeblocks); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_flash_writeblocks_obj, 3, 4, pyb_flash_writeblocks); -STATIC mp_obj_t pyb_flash_ioctl(mp_obj_t self, mp_obj_t cmd_in, mp_obj_t arg_in) { +STATIC mp_obj_t pyb_flash_ioctl(mp_obj_t self_in, mp_obj_t cmd_in, mp_obj_t arg_in) { + pyb_flash_obj_t *self = self_in; mp_int_t cmd = mp_obj_get_int(cmd_in); switch (cmd) { - case MP_BLOCKDEV_IOCTL_INIT: storage_init(); return MP_OBJ_NEW_SMALL_INT(0); + case MP_BLOCKDEV_IOCTL_INIT: { + mp_int_t ret = 0; + storage_init(); + if (mp_obj_get_int(arg_in) == 1) { + // Will be using extended block protocol + if (self == &pyb_flash_obj) { + ret = -1; + #if defined(SPIFLASH) + } else { + // Switch to use native block size of SPI flash + self->use_native_block_size = true; + #endif + } + } + return MP_OBJ_NEW_SMALL_INT(ret); + } case MP_BLOCKDEV_IOCTL_DEINIT: storage_flush(); return MP_OBJ_NEW_SMALL_INT(0); // TODO properly case MP_BLOCKDEV_IOCTL_SYNC: storage_flush(); return MP_OBJ_NEW_SMALL_INT(0); - case MP_BLOCKDEV_IOCTL_BLOCK_COUNT: return MP_OBJ_NEW_SMALL_INT(storage_get_block_count()); - case MP_BLOCKDEV_IOCTL_BLOCK_SIZE: return MP_OBJ_NEW_SMALL_INT(storage_get_block_size()); + + case MP_BLOCKDEV_IOCTL_BLOCK_COUNT: { + mp_int_t n; + if (self == &pyb_flash_obj) { + // Get true size + n = storage_get_block_count(); + #if defined(SPIFLASH) + } else if (self->use_native_block_size) { + n = self->len / PYB_FLASH_NATIVE_BLOCK_SIZE; + #endif + } else { + n = self->len / FLASH_BLOCK_SIZE; + } + return MP_OBJ_NEW_SMALL_INT(n); + } + + case MP_BLOCKDEV_IOCTL_BLOCK_SIZE: { + mp_int_t n = FLASH_BLOCK_SIZE; + #if defined(SPIFLASH) + if (self->use_native_block_size) { + n = PYB_FLASH_NATIVE_BLOCK_SIZE; + } + #endif + return MP_OBJ_NEW_SMALL_INT(n); + } + + case MP_BLOCKDEV_IOCTL_BLOCK_ERASE: { + int ret = 0; + #if defined(SPIFLASH) + if (self->use_native_block_size) { + mp_int_t block_num = self->start / PYB_FLASH_NATIVE_BLOCK_SIZE + mp_obj_get_int(arg_in); + ret = spi_bdev_ioctl(SPIFLASH, BDEV_IOCTL_BLOCK_ERASE, block_num); + } + #endif + return MP_OBJ_NEW_SMALL_INT(ret); + } + default: return mp_const_none; } } @@ -284,6 +441,7 @@ STATIC MP_DEFINE_CONST_DICT(pyb_flash_locals_dict, pyb_flash_locals_dict_table); const mp_obj_type_t pyb_flash_type = { { &mp_type_type }, .name = MP_QSTR_Flash, + .print = pyb_flash_print, .make_new = pyb_flash_make_new, .locals_dict = (mp_obj_dict_t*)&pyb_flash_locals_dict, }; diff --git a/ports/stm32/storage.h b/ports/stm32/storage.h index 0ba3497a17a3a..3a343b3275022 100644 --- a/ports/stm32/storage.h +++ b/ports/stm32/storage.h @@ -35,7 +35,8 @@ enum { BDEV_IOCTL_INIT = 1, BDEV_IOCTL_SYNC = 3, BDEV_IOCTL_NUM_BLOCKS = 4, - BDEV_IOCTL_IRQ_HANDLER = 6, + BDEV_IOCTL_BLOCK_ERASE = 6, + BDEV_IOCTL_IRQ_HANDLER = 7, }; void storage_init(void); @@ -52,6 +53,8 @@ mp_uint_t storage_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t int32_t flash_bdev_ioctl(uint32_t op, uint32_t arg); bool flash_bdev_readblock(uint8_t *dest, uint32_t block); bool flash_bdev_writeblock(const uint8_t *src, uint32_t block); +int flash_bdev_readblocks_ext(uint8_t *dest, uint32_t block, uint32_t offset, uint32_t len); +int flash_bdev_writeblocks_ext(const uint8_t *src, uint32_t block, uint32_t offset, uint32_t len); typedef struct _spi_bdev_t { mp_spiflash_t spiflash; @@ -62,8 +65,12 @@ int32_t spi_bdev_ioctl(spi_bdev_t *bdev, uint32_t op, uint32_t arg); int spi_bdev_readblocks(spi_bdev_t *bdev, uint8_t *dest, uint32_t block_num, uint32_t num_blocks); int spi_bdev_writeblocks(spi_bdev_t *bdev, const uint8_t *src, uint32_t block_num, uint32_t num_blocks); +// These raw functions bypass the cache and go directly to SPI flash +int spi_bdev_readblocks_raw(spi_bdev_t *bdev, uint8_t *dest, uint32_t block_num, uint32_t block_offset, uint32_t num_bytes); +int spi_bdev_writeblocks_raw(spi_bdev_t *bdev, const uint8_t *src, uint32_t block_num, uint32_t block_offset, uint32_t num_bytes); + extern const struct _mp_obj_type_t pyb_flash_type; -extern const struct _mp_obj_base_t pyb_flash_obj; +extern const struct _pyb_flash_obj_t pyb_flash_obj; struct _fs_user_mount_t; void pyb_flash_init_vfs(struct _fs_user_mount_t *vfs); From 7897f5d9bea22a3a0d7474805af624d0683c6d52 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 13 Nov 2019 22:04:39 +1100 Subject: [PATCH 2/7] stm32/main: Auto detect block device used for main filesystem. --- ports/stm32/main.c | 51 ++++++++++++++++++++++++++++++++++++++++--- ports/stm32/storage.c | 2 -- ports/stm32/storage.h | 1 + 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/ports/stm32/main.c b/ports/stm32/main.c index 707b1fc68ee29..e2b3f3c36115e 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -35,8 +35,13 @@ #include "lib/mp-readline/readline.h" #include "lib/utils/pyexec.h" #include "lib/oofatfs/ff.h" +#include "lib/littlefs/lfs1.h" +#include "lib/littlefs/lfs1_util.h" +#include "lib/littlefs/lfs2.h" +#include "lib/littlefs/lfs2_util.h" #include "extmod/vfs.h" #include "extmod/vfs_fat.h" +#include "extmod/vfs_lfs.h" #if MICROPY_PY_LWIP #include "lwip/init.h" @@ -183,13 +188,53 @@ MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) { factory_reset_create_filesystem(); } - // Try to mount the flash on "/flash" and chdir to it for the boot-up directory. + // Default block device to entire flash storage mp_obj_t bdev = MP_OBJ_FROM_PTR(&pyb_flash_obj); + + #if MICROPY_VFS_LFS1 || MICROPY_VFS_LFS2 + + // Try to detect the block device used for the main filesystem, based on the first block + + uint8_t buf[FLASH_BLOCK_SIZE]; + storage_read_blocks(buf, FLASH_PART1_START_BLOCK, 1); + + mp_int_t len = -1; + + #if MICROPY_VFS_LFS1 + if (memcmp(&buf[40], "littlefs", 8) == 0) { + // LFS1 + lfs1_superblock_t *superblock = (void*)&buf[12]; + uint32_t block_size = lfs1_fromle32(superblock->d.block_size); + uint32_t block_count = lfs1_fromle32(superblock->d.block_count); + len = block_count * block_size; + } + #endif + + #if MICROPY_VFS_LFS2 + if (memcmp(&buf[8], "littlefs", 8) == 0) { + // LFS2 + lfs2_superblock_t *superblock = (void*)&buf[20]; + uint32_t block_size = lfs2_fromle32(superblock->block_size); + uint32_t block_count = lfs2_fromle32(superblock->block_count); + len = block_count * block_size; + } + #endif + + if (len != -1) { + // Detected a littlefs filesystem so create correct block device for it + mp_obj_t args[] = { MP_OBJ_NEW_SMALL_INT(0), MP_OBJ_NEW_SMALL_INT(len) }; + bdev = pyb_flash_type.make_new(&pyb_flash_type, 2, 0, args); + } + + #endif + + // Try to mount the flash on "/flash" and chdir to it for the boot-up directory. mp_obj_t mount_point = MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash); int ret = vfs_mount_and_chdir(bdev, mount_point); - if (ret == -MP_ENODEV && reset_mode != 3) { - // No filesystem (and didn't already create one), try to create a fresh one + if (ret == -MP_ENODEV && bdev == MP_OBJ_FROM_PTR(&pyb_flash_obj) && reset_mode != 3) { + // No filesystem, bdev is still the default (so didn't detect a possibly corrupt littlefs), + // and didn't already create a filesystem, so try to create a fresh one now. ret = factory_reset_create_filesystem(); if (ret == 0) { ret = vfs_mount_and_chdir(bdev, mount_point); diff --git a/ports/stm32/storage.c b/ports/stm32/storage.c index 0d3285ba051d7..06dc8d6859385 100644 --- a/ports/stm32/storage.c +++ b/ports/stm32/storage.c @@ -41,8 +41,6 @@ #define STORAGE_SYSTICK_MASK (0x1ff) // 512ms #define STORAGE_IDLE_TICK(tick) (((tick) & ~(SYSTICK_DISPATCH_NUM_SLOTS - 1) & STORAGE_SYSTICK_MASK) == 0) -#define FLASH_PART1_START_BLOCK (0x100) - #if defined(MICROPY_HW_BDEV2_IOCTL) #define FLASH_PART2_START_BLOCK (FLASH_PART1_START_BLOCK + MICROPY_HW_BDEV2_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0)) #endif diff --git a/ports/stm32/storage.h b/ports/stm32/storage.h index 3a343b3275022..2766ac59b3b1d 100644 --- a/ports/stm32/storage.h +++ b/ports/stm32/storage.h @@ -29,6 +29,7 @@ #include "drivers/memory/spiflash.h" #define FLASH_BLOCK_SIZE (512) +#define FLASH_PART1_START_BLOCK (0x100) // Try to match Python-level VFS block protocol where possible for these constants enum { From 715e4fc25f7b103da48c9bc9513ee76e12a1471a Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 13 Nov 2019 22:04:52 +1100 Subject: [PATCH 3/7] stm32/moduos: Add VfsLfs1 and VfsLfs2 to uos module, if enabled. --- ports/stm32/moduos.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ports/stm32/moduos.c b/ports/stm32/moduos.c index ead2380b33253..8bf58623c93d3 100644 --- a/ports/stm32/moduos.c +++ b/ports/stm32/moduos.c @@ -36,6 +36,7 @@ #include "extmod/misc.h" #include "extmod/vfs.h" #include "extmod/vfs_fat.h" +#include "extmod/vfs_lfs.h" #include "genhdr/mpversion.h" #include "rng.h" #include "usb.h" @@ -174,6 +175,12 @@ STATIC const mp_rom_map_elem_t os_module_globals_table[] = { #if MICROPY_VFS_FAT { MP_ROM_QSTR(MP_QSTR_VfsFat), MP_ROM_PTR(&mp_fat_vfs_type) }, #endif + #if MICROPY_VFS_LFS1 + { MP_ROM_QSTR(MP_QSTR_VfsLfs1), MP_ROM_PTR(&mp_type_vfs_lfs1) }, + #endif + #if MICROPY_VFS_LFS2 + { MP_ROM_QSTR(MP_QSTR_VfsLfs2), MP_ROM_PTR(&mp_type_vfs_lfs2) }, + #endif }; STATIC MP_DEFINE_CONST_DICT(os_module_globals, os_module_globals_table); From 5634a31a9835ead5bdf7bbff5380c878f16db56d Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 14 Nov 2019 16:30:10 +1100 Subject: [PATCH 4/7] extmod/vfs_lfs: Pass flag along to ioctl when init'ing bdev for lfs. To hint to the block device that the extended block protocol will be used. --- extmod/vfs_lfsx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extmod/vfs_lfsx.c b/extmod/vfs_lfsx.c index e5826803b2433..4f5ad5fdf7b46 100644 --- a/extmod/vfs_lfsx.c +++ b/extmod/vfs_lfsx.c @@ -73,7 +73,7 @@ STATIC void MP_VFS_LFSx(init_config)(MP_OBJ_VFS_LFSx *self, mp_obj_t bdev, size_ config->erase = MP_VFS_LFSx(dev_erase); config->sync = MP_VFS_LFSx(dev_sync); - MP_VFS_LFSx(dev_ioctl)(config, MP_BLOCKDEV_IOCTL_INIT, 0, false); // initialise block device + MP_VFS_LFSx(dev_ioctl)(config, MP_BLOCKDEV_IOCTL_INIT, 1, false); // initialise block device int bs = MP_VFS_LFSx(dev_ioctl)(config, MP_BLOCKDEV_IOCTL_BLOCK_SIZE, 0, true); // get block size int bc = MP_VFS_LFSx(dev_ioctl)(config, MP_BLOCKDEV_IOCTL_BLOCK_COUNT, 0, true); // get block count self->blockdev.block_size = bs; From 120368ba1ab444b2f1c17d1eb69bc6f09072ec5d Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 14 Nov 2019 16:36:05 +1100 Subject: [PATCH 5/7] stm32/boards: Enable LFS2 on PYBv1.x and PYBD boards. --- ports/stm32/boards/PYBD_SF2/mpconfigboard.h | 1 + ports/stm32/boards/PYBD_SF2/mpconfigboard.mk | 1 + ports/stm32/boards/PYBV10/mpconfigboard.mk | 3 +++ ports/stm32/boards/PYBV11/mpconfigboard.mk | 3 +++ 4 files changed, 8 insertions(+) diff --git a/ports/stm32/boards/PYBD_SF2/mpconfigboard.h b/ports/stm32/boards/PYBD_SF2/mpconfigboard.h index 8926ec081be78..8e116bd02a59e 100644 --- a/ports/stm32/boards/PYBD_SF2/mpconfigboard.h +++ b/ports/stm32/boards/PYBD_SF2/mpconfigboard.h @@ -84,6 +84,7 @@ extern struct _spi_bdev_t spi_bdev; ) #define MICROPY_HW_BDEV_READBLOCKS(dest, bl, n) spi_bdev_readblocks(&spi_bdev, (dest), (bl), (n)) #define MICROPY_HW_BDEV_WRITEBLOCKS(src, bl, n) spi_bdev_writeblocks(&spi_bdev, (src), (bl), (n)) +#define MICROPY_HW_BDEV_SPIFLASH_EXTENDED (&spi_bdev) // for extended block protocol // SPI flash #2, to be memory mapped #define MICROPY_HW_QSPIFLASH_SIZE_BITS_LOG2 (24) diff --git a/ports/stm32/boards/PYBD_SF2/mpconfigboard.mk b/ports/stm32/boards/PYBD_SF2/mpconfigboard.mk index 834a60b029ee8..9c0121f3137b6 100644 --- a/ports/stm32/boards/PYBD_SF2/mpconfigboard.mk +++ b/ports/stm32/boards/PYBD_SF2/mpconfigboard.mk @@ -16,3 +16,4 @@ MICROPY_PY_LWIP = 1 MICROPY_PY_NETWORK_CYW43 = 1 MICROPY_PY_USSL = 1 MICROPY_SSL_MBEDTLS = 1 +MICROPY_VFS_LFS2 = 1 diff --git a/ports/stm32/boards/PYBV10/mpconfigboard.mk b/ports/stm32/boards/PYBV10/mpconfigboard.mk index a4430cc1df0e4..cb78a7846d933 100644 --- a/ports/stm32/boards/PYBV10/mpconfigboard.mk +++ b/ports/stm32/boards/PYBV10/mpconfigboard.mk @@ -11,3 +11,6 @@ LD_FILES = boards/stm32f405.ld boards/common_ifs.ld TEXT0_ADDR = 0x08000000 TEXT1_ADDR = 0x08020000 endif + +# MicroPython settings +MICROPY_VFS_LFS2 = 1 diff --git a/ports/stm32/boards/PYBV11/mpconfigboard.mk b/ports/stm32/boards/PYBV11/mpconfigboard.mk index a4430cc1df0e4..cb78a7846d933 100644 --- a/ports/stm32/boards/PYBV11/mpconfigboard.mk +++ b/ports/stm32/boards/PYBV11/mpconfigboard.mk @@ -11,3 +11,6 @@ LD_FILES = boards/stm32f405.ld boards/common_ifs.ld TEXT0_ADDR = 0x08000000 TEXT1_ADDR = 0x08020000 endif + +# MicroPython settings +MICROPY_VFS_LFS2 = 1 From d8057c325a63d87b3e37cf4a6cdb04cb4eeba124 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 21 Nov 2019 16:11:59 +1100 Subject: [PATCH 6/7] stm32/storage: Change storage_read/write_blocks to return int type. And return -MP_EIO if calling storage_read_block/storage_write_block fails. This lines up with the return type and value (negative for error) of the calls to MICROPY_HW_BDEV_READBLOCKS (and WRITEBLOCKS, and BDEV2 versions). --- ports/stm32/storage.c | 8 ++++---- ports/stm32/storage.h | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ports/stm32/storage.c b/ports/stm32/storage.c index 06dc8d6859385..aecef8acc6eaa 100644 --- a/ports/stm32/storage.c +++ b/ports/stm32/storage.c @@ -185,7 +185,7 @@ bool storage_write_block(const uint8_t *src, uint32_t block) { } } -mp_uint_t storage_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) { +int storage_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks) { #if defined(MICROPY_HW_BDEV_READBLOCKS) if (FLASH_PART1_START_BLOCK <= block_num && block_num + num_blocks <= FLASH_PART1_START_BLOCK + MICROPY_HW_BDEV_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0)) { return MICROPY_HW_BDEV_READBLOCKS(dest, block_num - FLASH_PART1_START_BLOCK, num_blocks); @@ -200,13 +200,13 @@ mp_uint_t storage_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_bl for (size_t i = 0; i < num_blocks; i++) { if (!storage_read_block(dest + i * FLASH_BLOCK_SIZE, block_num + i)) { - return 1; // error + return -MP_EIO; // error } } return 0; // success } -mp_uint_t storage_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks) { +int storage_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks) { #if defined(MICROPY_HW_BDEV_WRITEBLOCKS) if (FLASH_PART1_START_BLOCK <= block_num && block_num + num_blocks <= FLASH_PART1_START_BLOCK + MICROPY_HW_BDEV_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0)) { return MICROPY_HW_BDEV_WRITEBLOCKS(src, block_num - FLASH_PART1_START_BLOCK, num_blocks); @@ -221,7 +221,7 @@ mp_uint_t storage_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t for (size_t i = 0; i < num_blocks; i++) { if (!storage_write_block(src + i * FLASH_BLOCK_SIZE, block_num + i)) { - return 1; // error + return -MP_EIO; // error } } return 0; // success diff --git a/ports/stm32/storage.h b/ports/stm32/storage.h index 2766ac59b3b1d..490fc4a09b8ac 100644 --- a/ports/stm32/storage.h +++ b/ports/stm32/storage.h @@ -47,9 +47,9 @@ void storage_flush(void); bool storage_read_block(uint8_t *dest, uint32_t block); bool storage_write_block(const uint8_t *src, uint32_t block); -// these return 0 on success, non-zero on error -mp_uint_t storage_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks); -mp_uint_t storage_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks); +// these return 0 on success, negative errno on error +int storage_read_blocks(uint8_t *dest, uint32_t block_num, uint32_t num_blocks); +int storage_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_blocks); int32_t flash_bdev_ioctl(uint32_t op, uint32_t arg); bool flash_bdev_readblock(uint8_t *dest, uint32_t block); From 6b3404f25e3d901d9b1313f2827a760093cddd92 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 22 Nov 2019 15:14:24 +1100 Subject: [PATCH 7/7] extmod/vfs_lfs: Fix bug when passing no args to constructor and mkfs. --- extmod/vfs_lfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extmod/vfs_lfs.c b/extmod/vfs_lfs.c index d776849980942..f34d160f00538 100644 --- a/extmod/vfs_lfs.c +++ b/extmod/vfs_lfs.c @@ -33,7 +33,7 @@ enum { LFS_MAKE_ARG_bdev, LFS_MAKE_ARG_readsize, LFS_MAKE_ARG_progsize, LFS_MAKE_ARG_lookahead }; static const mp_arg_t lfs_make_allowed_args[] = { - { MP_QSTR_, MP_ARG_OBJ }, + { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ }, { MP_QSTR_readsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 32} }, { MP_QSTR_progsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 32} }, { MP_QSTR_lookahead, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 32} },