Browse Source

stm32/storage: Make extended-block-device more configurable.

A board can now define the following to fully customise the extended block
device interface provided by the storage sub-system:
- MICROPY_HW_BDEV_BLOCKSIZE_EXT
- MICROPY_HW_BDEV_READBLOCKS_EXT
- MICROPY_HW_BDEV_WRITEBLOCKS_EXT
- MICROPY_HW_BDEV_ERASEBLOCKS_EXT

Signed-off-by: Damien George <damien@micropython.org>
pull/7710/head
Damien George 3 years ago
parent
commit
6936f410ab
  1. 1
      ports/stm32/flashbdev.c
  2. 22
      ports/stm32/spibdev.c
  3. 54
      ports/stm32/storage.c
  4. 4
      ports/stm32/storage.h

1
ports/stm32/flashbdev.c

@ -157,6 +157,7 @@ int32_t flash_bdev_ioctl(uint32_t op, uint32_t arg) {
return 0;
case BDEV_IOCTL_NUM_BLOCKS:
// Units are FLASH_BLOCK_SIZE
return FLASH_MEM_SEG1_NUM_BLOCKS + FLASH_MEM_SEG2_NUM_BLOCKS;
case BDEV_IOCTL_IRQ_HANDLER:

22
ports/stm32/spibdev.c

@ -59,13 +59,6 @@ int32_t spi_bdev_ioctl(spi_bdev_t *bdev, uint32_t op, uint32_t arg) {
}
#endif
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;
}
@ -108,4 +101,19 @@ int spi_bdev_writeblocks_raw(spi_bdev_t *bdev, const uint8_t *src, uint32_t bloc
return ret;
}
int spi_bdev_eraseblocks_raw(spi_bdev_t *bdev, uint32_t block_num, uint32_t num_bytes) {
int ret = 0;
while (num_bytes >= MP_SPIFLASH_ERASE_BLOCK_SIZE) {
uint32_t basepri = raise_irq_pri(IRQ_PRI_FLASH); // prevent cache flushing and USB access
ret = mp_spiflash_erase_block(&bdev->spiflash, block_num * MP_SPIFLASH_ERASE_BLOCK_SIZE);
restore_irq_pri(basepri);
if (ret) {
break;
}
block_num += 1;
num_bytes -= MP_SPIFLASH_ERASE_BLOCK_SIZE;
}
return ret;
}
#endif

54
ports/stm32/storage.c

@ -234,24 +234,27 @@ int storage_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_bl
#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)))
#define MICROPY_HW_BDEV_BLOCKSIZE_EXT (MP_SPIFLASH_ERASE_BLOCK_SIZE)
#define MICROPY_HW_BDEV_READBLOCKS_EXT(dest, bl, off, len) \
(spi_bdev_readblocks_raw(MICROPY_HW_BDEV_SPIFLASH_EXTENDED, (dest), (bl), (off), (len)))
#define MICROPY_HW_BDEV_WRITEBLOCKS_EXT(src, bl, off, len) \
(spi_bdev_writeblocks_raw(MICROPY_HW_BDEV_SPIFLASH_EXTENDED, (src), (bl), (off), (len)))
#define MICROPY_HW_BDEV_ERASEBLOCKS_EXT(bl, len) \
(spi_bdev_eraseblocks_raw(MICROPY_HW_BDEV_SPIFLASH_EXTENDED, (bl), (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_BLOCKSIZE_EXT (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)
#ifndef MICROPY_HW_BDEV_BLOCKSIZE_EXT
#define MICROPY_HW_BDEV_BLOCKSIZE_EXT (FLASH_BLOCK_SIZE)
#endif
#if defined(MICROPY_HW_BDEV_READBLOCKS_EXT)
// Size of blocks is PYB_FLASH_NATIVE_BLOCK_SIZE
// Size of blocks is MICROPY_HW_BDEV_BLOCKSIZE_EXT
int storage_readblocks_ext(uint8_t *dest, uint32_t block, uint32_t offset, uint32_t len) {
return MICROPY_HW_BDEV_READBLOCKS_EXT(dest, block, offset, len);
}
@ -261,9 +264,7 @@ 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
@ -299,23 +300,21 @@ STATIC mp_obj_t pyb_flash_make_new(const mp_obj_type_t *type, size_t n_args, siz
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)) {
} else if (!(0 <= start && start < bl_len && start % MICROPY_HW_BDEV_BLOCKSIZE_EXT == 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)) {
} else if (!(0 < len && start + len <= bl_len && len % MICROPY_HW_BDEV_BLOCKSIZE_EXT == 0)) {
mp_raise_ValueError(NULL);
}
@ -340,10 +339,10 @@ STATIC mp_obj_t pyb_flash_readblocks(size_t n_args, const mp_obj_t *args) {
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]);
if ((block_num * PYB_FLASH_NATIVE_BLOCK_SIZE) >= self->len) {
if ((block_num * MICROPY_HW_BDEV_BLOCKSIZE_EXT) >= self->len) {
ret = -MP_EFAULT; // Bad address
} else {
block_num += self->start / PYB_FLASH_NATIVE_BLOCK_SIZE;
block_num += self->start / MICROPY_HW_BDEV_BLOCKSIZE_EXT;
ret = MICROPY_HW_BDEV_READBLOCKS_EXT(bufinfo.buf, block_num, offset, bufinfo.len);
}
}
@ -367,10 +366,10 @@ STATIC mp_obj_t pyb_flash_writeblocks(size_t n_args, const mp_obj_t *args) {
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]);
if ((block_num * PYB_FLASH_NATIVE_BLOCK_SIZE) >= self->len) {
if ((block_num * MICROPY_HW_BDEV_BLOCKSIZE_EXT) >= self->len) {
ret = -MP_EFAULT; // Bad address
} else {
block_num += self->start / PYB_FLASH_NATIVE_BLOCK_SIZE;
block_num += self->start / MICROPY_HW_BDEV_BLOCKSIZE_EXT;
ret = MICROPY_HW_BDEV_WRITEBLOCKS_EXT(bufinfo.buf, block_num, offset, bufinfo.len);
}
}
@ -390,11 +389,9 @@ STATIC mp_obj_t pyb_flash_ioctl(mp_obj_t self_in, mp_obj_t cmd_in, mp_obj_t arg_
// 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
// Switch to use native block size of the underlying storage.
self->use_native_block_size = true;
#endif
}
}
return MP_OBJ_NEW_SMALL_INT(ret);
@ -411,10 +408,8 @@ STATIC mp_obj_t pyb_flash_ioctl(mp_obj_t self_in, mp_obj_t cmd_in, mp_obj_t arg_
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
n = self->len / MICROPY_HW_BDEV_BLOCKSIZE_EXT;
} else {
n = self->len / FLASH_BLOCK_SIZE;
}
@ -423,20 +418,19 @@ STATIC mp_obj_t pyb_flash_ioctl(mp_obj_t self_in, mp_obj_t cmd_in, mp_obj_t arg_
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;
n = MICROPY_HW_BDEV_BLOCKSIZE_EXT;
}
#endif
return MP_OBJ_NEW_SMALL_INT(n);
}
case MP_BLOCKDEV_IOCTL_BLOCK_ERASE: {
int ret = 0;
#if defined(SPIFLASH)
#if defined(MICROPY_HW_BDEV_ERASEBLOCKS_EXT)
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);
mp_int_t block_num = self->start / MICROPY_HW_BDEV_BLOCKSIZE_EXT + mp_obj_get_int(arg_in);
ret = MICROPY_HW_BDEV_ERASEBLOCKS_EXT(block_num, MICROPY_HW_BDEV_BLOCKSIZE_EXT);
}
#endif
return MP_OBJ_NEW_SMALL_INT(ret);

4
ports/stm32/storage.h

@ -35,8 +35,7 @@
enum {
BDEV_IOCTL_INIT = 1,
BDEV_IOCTL_SYNC = 3,
BDEV_IOCTL_NUM_BLOCKS = 4,
BDEV_IOCTL_BLOCK_ERASE = 6,
BDEV_IOCTL_NUM_BLOCKS = 4, // units are FLASH_BLOCK_SIZE
BDEV_IOCTL_IRQ_HANDLER = 7,
};
@ -70,6 +69,7 @@ int spi_bdev_writeblocks(spi_bdev_t *bdev, const uint8_t *src, uint32_t block_nu
// 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);
int spi_bdev_eraseblocks_raw(spi_bdev_t *bdev, uint32_t block_num, uint32_t num_bytes);
extern const struct _mp_obj_type_t pyb_flash_type;
extern const struct _pyb_flash_obj_t pyb_flash_obj;

Loading…
Cancel
Save