diff --git a/ports/stm32/mpconfigboard_common.h b/ports/stm32/mpconfigboard_common.h index 67a6ee990e..15513fba9c 100644 --- a/ports/stm32/mpconfigboard_common.h +++ b/ports/stm32/mpconfigboard_common.h @@ -548,11 +548,9 @@ // - MICROPY_HW_BDEV_SPIFLASH - pointer to a spi_bdev_t // - MICROPY_HW_BDEV_SPIFLASH_CONFIG - pointer to an mp_spiflash_config_t // - MICROPY_HW_BDEV_SPIFLASH_SIZE_BYTES - size in bytes of the SPI flash -#define MICROPY_HW_BDEV_IOCTL(op, arg) ( \ - (op) == BDEV_IOCTL_NUM_BLOCKS ? (MICROPY_HW_BDEV_SPIFLASH_SIZE_BYTES / FLASH_BLOCK_SIZE) : \ - (op) == BDEV_IOCTL_INIT ? spi_bdev_ioctl(MICROPY_HW_BDEV_SPIFLASH, (op), (uint32_t)MICROPY_HW_BDEV_SPIFLASH_CONFIG) : \ - spi_bdev_ioctl(MICROPY_HW_BDEV_SPIFLASH, (op), (arg)) \ - ) +// The board can specify the SPI flash chip(s) being used as comma separated list in: +// - MICROPY_HW_SPIFLASH_DEVICES +#define MICROPY_HW_BDEV_IOCTL(op, arg) (spi_bdev_ioctl(MICROPY_HW_BDEV_SPIFLASH, (op), (arg))) #define MICROPY_HW_BDEV_READBLOCKS(dest, bl, n) spi_bdev_readblocks(MICROPY_HW_BDEV_SPIFLASH, (dest), (bl), (n)) #define MICROPY_HW_BDEV_WRITEBLOCKS(src, bl, n) spi_bdev_writeblocks(MICROPY_HW_BDEV_SPIFLASH, (src), (bl), (n)) #endif diff --git a/ports/stm32/spibdev.c b/ports/stm32/spibdev.c index fecd4a9915..5366b41cb7 100644 --- a/ports/stm32/spibdev.c +++ b/ports/stm32/spibdev.c @@ -29,16 +29,33 @@ #include "irq.h" #include "led.h" #include "storage.h" +#include "drivers/memory/external_flash_device.h" #if MICROPY_HW_ENABLE_STORAGE int32_t spi_bdev_ioctl(spi_bdev_t *bdev, uint32_t op, uint32_t arg) { switch (op) { case BDEV_IOCTL_INIT: + #ifdef MICROPY_HW_BDEV_SPIFLASH_CONFIG + if (!arg) { + arg = (uint32_t)(MICROPY_HW_BDEV_SPIFLASH_CONFIG); + } + #endif bdev->spiflash.config = (const mp_spiflash_config_t *)arg; - mp_spiflash_init(&bdev->spiflash); + int ret = mp_spiflash_init(&bdev->spiflash); bdev->flash_tick_counter_last_write = 0; - return 0; + return ret; + + case BDEV_IOCTL_NUM_BLOCKS: + #if MICROPY_HW_BDEV_SPIFLASH_SIZE_BYTES + return MICROPY_HW_BDEV_SPIFLASH_SIZE_BYTES / FLASH_BLOCK_SIZE; + #else + if (bdev->spiflash.device != NULL) { + external_flash_device *flash = (external_flash_device *)bdev->spiflash.device; + return flash->total_size / FLASH_BLOCK_SIZE; + } + return -1; + #endif case BDEV_IOCTL_IRQ_HANDLER: { int ret = 0; diff --git a/ports/stm32/storage.c b/ports/stm32/storage.c index 97286ba865..2e6d185b77 100644 --- a/ports/stm32/storage.c +++ b/ports/stm32/storage.c @@ -48,11 +48,11 @@ static void storage_systick_callback(uint32_t ticks_ms); void storage_init(void) { if (!storage_is_initialised) { - storage_is_initialised = true; - systick_enable_dispatch(SYSTICK_DISPATCH_STORAGE, storage_systick_callback); - MICROPY_HW_BDEV_IOCTL(BDEV_IOCTL_INIT, 0); + if (MICROPY_HW_BDEV_IOCTL(BDEV_IOCTL_INIT, 0) == 0) { + storage_is_initialised = true; + } #if defined(MICROPY_HW_BDEV2_IOCTL) MICROPY_HW_BDEV2_IOCTL(BDEV_IOCTL_INIT, 0);