Skip to content

Commit 6936f41

Browse files
committed
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 <[email protected]>
1 parent a66bd7a commit 6936f41

File tree

4 files changed

+42
-39
lines changed

4 files changed

+42
-39
lines changed

ports/stm32/flashbdev.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ int32_t flash_bdev_ioctl(uint32_t op, uint32_t arg) {
157157
return 0;
158158

159159
case BDEV_IOCTL_NUM_BLOCKS:
160+
// Units are FLASH_BLOCK_SIZE
160161
return FLASH_MEM_SEG1_NUM_BLOCKS + FLASH_MEM_SEG2_NUM_BLOCKS;
161162

162163
case BDEV_IOCTL_IRQ_HANDLER:

ports/stm32/spibdev.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,6 @@ int32_t spi_bdev_ioctl(spi_bdev_t *bdev, uint32_t op, uint32_t arg) {
5959
}
6060
#endif
6161
return 0;
62-
63-
case BDEV_IOCTL_BLOCK_ERASE: {
64-
uint32_t basepri = raise_irq_pri(IRQ_PRI_FLASH); // prevent cache flushing and USB access
65-
mp_spiflash_erase_block(&bdev->spiflash, arg * MP_SPIFLASH_ERASE_BLOCK_SIZE);
66-
restore_irq_pri(basepri);
67-
return 0;
68-
}
6962
}
7063
return -MP_EINVAL;
7164
}
@@ -108,4 +101,19 @@ int spi_bdev_writeblocks_raw(spi_bdev_t *bdev, const uint8_t *src, uint32_t bloc
108101
return ret;
109102
}
110103

104+
int spi_bdev_eraseblocks_raw(spi_bdev_t *bdev, uint32_t block_num, uint32_t num_bytes) {
105+
int ret = 0;
106+
while (num_bytes >= MP_SPIFLASH_ERASE_BLOCK_SIZE) {
107+
uint32_t basepri = raise_irq_pri(IRQ_PRI_FLASH); // prevent cache flushing and USB access
108+
ret = mp_spiflash_erase_block(&bdev->spiflash, block_num * MP_SPIFLASH_ERASE_BLOCK_SIZE);
109+
restore_irq_pri(basepri);
110+
if (ret) {
111+
break;
112+
}
113+
block_num += 1;
114+
num_bytes -= MP_SPIFLASH_ERASE_BLOCK_SIZE;
115+
}
116+
return ret;
117+
}
118+
111119
#endif

ports/stm32/storage.c

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -234,24 +234,27 @@ int storage_write_blocks(const uint8_t *src, uint32_t block_num, uint32_t num_bl
234234

235235
#ifdef MICROPY_HW_BDEV_SPIFLASH_EXTENDED
236236
// Board defined an external SPI flash for use with extended block protocol
237-
#define SPIFLASH (MICROPY_HW_BDEV_SPIFLASH_EXTENDED)
238-
#define PYB_FLASH_NATIVE_BLOCK_SIZE (MP_SPIFLASH_ERASE_BLOCK_SIZE)
239-
#define MICROPY_HW_BDEV_READBLOCKS_EXT(dest, bl, off, len) (spi_bdev_readblocks_raw(SPIFLASH, (dest), (bl), (off), (len)))
240-
#define MICROPY_HW_BDEV_WRITEBLOCKS_EXT(dest, bl, off, len) (spi_bdev_writeblocks_raw(SPIFLASH, (dest), (bl), (off), (len)))
237+
#define MICROPY_HW_BDEV_BLOCKSIZE_EXT (MP_SPIFLASH_ERASE_BLOCK_SIZE)
238+
#define MICROPY_HW_BDEV_READBLOCKS_EXT(dest, bl, off, len) \
239+
(spi_bdev_readblocks_raw(MICROPY_HW_BDEV_SPIFLASH_EXTENDED, (dest), (bl), (off), (len)))
240+
#define MICROPY_HW_BDEV_WRITEBLOCKS_EXT(src, bl, off, len) \
241+
(spi_bdev_writeblocks_raw(MICROPY_HW_BDEV_SPIFLASH_EXTENDED, (src), (bl), (off), (len)))
242+
#define MICROPY_HW_BDEV_ERASEBLOCKS_EXT(bl, len) \
243+
(spi_bdev_eraseblocks_raw(MICROPY_HW_BDEV_SPIFLASH_EXTENDED, (bl), (len)))
241244

242245
#elif (MICROPY_VFS_LFS1 || MICROPY_VFS_LFS2) && MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE
243246
// Board uses littlefs and internal flash, so enable extended block protocol on internal flash
244-
#define PYB_FLASH_NATIVE_BLOCK_SIZE (FLASH_BLOCK_SIZE)
247+
#define MICROPY_HW_BDEV_BLOCKSIZE_EXT (FLASH_BLOCK_SIZE)
245248
#define MICROPY_HW_BDEV_READBLOCKS_EXT(dest, bl, off, len) (flash_bdev_readblocks_ext((dest), (bl), (off), (len)))
246249
#define MICROPY_HW_BDEV_WRITEBLOCKS_EXT(dest, bl, off, len) (flash_bdev_writeblocks_ext((dest), (bl), (off), (len)))
247250
#endif
248251

249-
#ifndef PYB_FLASH_NATIVE_BLOCK_SIZE
250-
#define PYB_FLASH_NATIVE_BLOCK_SIZE (FLASH_BLOCK_SIZE)
252+
#ifndef MICROPY_HW_BDEV_BLOCKSIZE_EXT
253+
#define MICROPY_HW_BDEV_BLOCKSIZE_EXT (FLASH_BLOCK_SIZE)
251254
#endif
252255

253256
#if defined(MICROPY_HW_BDEV_READBLOCKS_EXT)
254-
// Size of blocks is PYB_FLASH_NATIVE_BLOCK_SIZE
257+
// Size of blocks is MICROPY_HW_BDEV_BLOCKSIZE_EXT
255258
int storage_readblocks_ext(uint8_t *dest, uint32_t block, uint32_t offset, uint32_t len) {
256259
return MICROPY_HW_BDEV_READBLOCKS_EXT(dest, block, offset, len);
257260
}
@@ -261,9 +264,7 @@ typedef struct _pyb_flash_obj_t {
261264
mp_obj_base_t base;
262265
uint32_t start; // in bytes
263266
uint32_t len; // in bytes
264-
#if defined(SPIFLASH)
265267
bool use_native_block_size;
266-
#endif
267268
} pyb_flash_obj_t;
268269

269270
// 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
299300

300301
pyb_flash_obj_t *self = m_new_obj(pyb_flash_obj_t);
301302
self->base.type = &pyb_flash_type;
302-
#if defined(SPIFLASH)
303303
self->use_native_block_size = false;
304-
#endif
305304

306305
uint32_t bl_len = (storage_get_block_count() - FLASH_PART1_START_BLOCK) * FLASH_BLOCK_SIZE;
307306

308307
mp_int_t start = args[ARG_start].u_int;
309308
if (start == -1) {
310309
start = 0;
311-
} else if (!(0 <= start && start < bl_len && start % PYB_FLASH_NATIVE_BLOCK_SIZE == 0)) {
310+
} else if (!(0 <= start && start < bl_len && start % MICROPY_HW_BDEV_BLOCKSIZE_EXT == 0)) {
312311
mp_raise_ValueError(NULL);
313312
}
314313

315314
mp_int_t len = args[ARG_len].u_int;
316315
if (len == -1) {
317316
len = bl_len - start;
318-
} else if (!(0 < len && start + len <= bl_len && len % PYB_FLASH_NATIVE_BLOCK_SIZE == 0)) {
317+
} else if (!(0 < len && start + len <= bl_len && len % MICROPY_HW_BDEV_BLOCKSIZE_EXT == 0)) {
319318
mp_raise_ValueError(NULL);
320319
}
321320

@@ -340,10 +339,10 @@ STATIC mp_obj_t pyb_flash_readblocks(size_t n_args, const mp_obj_t *args) {
340339
else if (self != &pyb_flash_obj) {
341340
// Extended block read on a sub-section of the flash storage
342341
uint32_t offset = mp_obj_get_int(args[3]);
343-
if ((block_num * PYB_FLASH_NATIVE_BLOCK_SIZE) >= self->len) {
342+
if ((block_num * MICROPY_HW_BDEV_BLOCKSIZE_EXT) >= self->len) {
344343
ret = -MP_EFAULT; // Bad address
345344
} else {
346-
block_num += self->start / PYB_FLASH_NATIVE_BLOCK_SIZE;
345+
block_num += self->start / MICROPY_HW_BDEV_BLOCKSIZE_EXT;
347346
ret = MICROPY_HW_BDEV_READBLOCKS_EXT(bufinfo.buf, block_num, offset, bufinfo.len);
348347
}
349348
}
@@ -367,10 +366,10 @@ STATIC mp_obj_t pyb_flash_writeblocks(size_t n_args, const mp_obj_t *args) {
367366
else if (self != &pyb_flash_obj) {
368367
// Extended block write on a sub-section of the flash storage
369368
uint32_t offset = mp_obj_get_int(args[3]);
370-
if ((block_num * PYB_FLASH_NATIVE_BLOCK_SIZE) >= self->len) {
369+
if ((block_num * MICROPY_HW_BDEV_BLOCKSIZE_EXT) >= self->len) {
371370
ret = -MP_EFAULT; // Bad address
372371
} else {
373-
block_num += self->start / PYB_FLASH_NATIVE_BLOCK_SIZE;
372+
block_num += self->start / MICROPY_HW_BDEV_BLOCKSIZE_EXT;
374373
ret = MICROPY_HW_BDEV_WRITEBLOCKS_EXT(bufinfo.buf, block_num, offset, bufinfo.len);
375374
}
376375
}
@@ -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_
390389
// Will be using extended block protocol
391390
if (self == &pyb_flash_obj) {
392391
ret = -1;
393-
#if defined(SPIFLASH)
394392
} else {
395-
// Switch to use native block size of SPI flash
393+
// Switch to use native block size of the underlying storage.
396394
self->use_native_block_size = true;
397-
#endif
398395
}
399396
}
400397
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_
411408
if (self == &pyb_flash_obj) {
412409
// Get true size
413410
n = storage_get_block_count();
414-
#if defined(SPIFLASH)
415411
} else if (self->use_native_block_size) {
416-
n = self->len / PYB_FLASH_NATIVE_BLOCK_SIZE;
417-
#endif
412+
n = self->len / MICROPY_HW_BDEV_BLOCKSIZE_EXT;
418413
} else {
419414
n = self->len / FLASH_BLOCK_SIZE;
420415
}
@@ -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_
423418

424419
case MP_BLOCKDEV_IOCTL_BLOCK_SIZE: {
425420
mp_int_t n = FLASH_BLOCK_SIZE;
426-
#if defined(SPIFLASH)
427421
if (self->use_native_block_size) {
428-
n = PYB_FLASH_NATIVE_BLOCK_SIZE;
422+
n = MICROPY_HW_BDEV_BLOCKSIZE_EXT;
429423
}
430-
#endif
431424
return MP_OBJ_NEW_SMALL_INT(n);
432425
}
433426

434427
case MP_BLOCKDEV_IOCTL_BLOCK_ERASE: {
435428
int ret = 0;
436-
#if defined(SPIFLASH)
429+
#if defined(MICROPY_HW_BDEV_ERASEBLOCKS_EXT)
437430
if (self->use_native_block_size) {
438-
mp_int_t block_num = self->start / PYB_FLASH_NATIVE_BLOCK_SIZE + mp_obj_get_int(arg_in);
439-
ret = spi_bdev_ioctl(SPIFLASH, BDEV_IOCTL_BLOCK_ERASE, block_num);
431+
mp_int_t block_num = self->start / MICROPY_HW_BDEV_BLOCKSIZE_EXT + mp_obj_get_int(arg_in);
432+
433+
ret = MICROPY_HW_BDEV_ERASEBLOCKS_EXT(block_num, MICROPY_HW_BDEV_BLOCKSIZE_EXT);
440434
}
441435
#endif
442436
return MP_OBJ_NEW_SMALL_INT(ret);

ports/stm32/storage.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
enum {
3636
BDEV_IOCTL_INIT = 1,
3737
BDEV_IOCTL_SYNC = 3,
38-
BDEV_IOCTL_NUM_BLOCKS = 4,
39-
BDEV_IOCTL_BLOCK_ERASE = 6,
38+
BDEV_IOCTL_NUM_BLOCKS = 4, // units are FLASH_BLOCK_SIZE
4039
BDEV_IOCTL_IRQ_HANDLER = 7,
4140
};
4241

@@ -70,6 +69,7 @@ int spi_bdev_writeblocks(spi_bdev_t *bdev, const uint8_t *src, uint32_t block_nu
7069
// These raw functions bypass the cache and go directly to SPI flash
7170
int spi_bdev_readblocks_raw(spi_bdev_t *bdev, uint8_t *dest, uint32_t block_num, uint32_t block_offset, uint32_t num_bytes);
7271
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);
72+
int spi_bdev_eraseblocks_raw(spi_bdev_t *bdev, uint32_t block_num, uint32_t num_bytes);
7373

7474
extern const struct _mp_obj_type_t pyb_flash_type;
7575
extern const struct _pyb_flash_obj_t pyb_flash_obj;

0 commit comments

Comments
 (0)