How to expand the size of spiflash?

Showroom for MicroPython related hardware projects.
Target audience: Users wanting to show off their project!
Post Reply
turoksama
Posts: 12
Joined: Sun Mar 31, 2019 8:49 am

How to expand the size of spiflash?

Post by turoksama » Tue Apr 23, 2019 11:09 am

Hi there,
I built a custom stm32f411 board recently, embeded with a 16MB spiflash(Do not use internal flash for storage). When loaded the firmware, it's max size is 2MB, not 16MB, and something wrong with file read or write. I've look up over all codes about spiflash in Micropython but with no luck.
What else should I do?

spiflash paras associate in mpconfigboard.h:

// use external SPI flash for storage: GPIOB
#define MICROPY_HW_SPIFLASH_SIZE_BYTES (16 * 1024 * 1024)
#define MICROPY_HW_SPIFLASH_BLOCK_SIZE (4096)


#define MICROPY_HW_SPIFLASH_CS (pyb_pin_FS_CS)
#define MICROPY_HW_SPIFLASH_SCK (pyb_pin_FS_CLK)
#define MICROPY_HW_SPIFLASH_IO0 (pyb_pin_FS_D0)
#define MICROPY_HW_SPIFLASH_IO1 (pyb_pin_FS_D1)
#define MICROPY_HW_SPIFLASH_IO2 (pyb_pin_FS_D2)
#define MICROPY_HW_SPIFLASH_IO3 (pyb_pin_FS_D3)
#define MICROPY_HW_SOFTQSPI_NIBBLE_READ(self) ((GPIOB->IDR >> 4) & 0xf)

// block device config for first SPI flash
extern const struct _mp_spiflash_config_t spiflash_config;
extern struct _spi_bdev_t spi_bdev;

#define MICROPY_HW_BDEV_IOCTL(op, arg) ( \
(op) == BDEV_IOCTL_NUM_BLOCKS ? (MICROPY_HW_SPIFLASH_SIZE_BYTES / MICROPY_HW_SPIFLASH_BLOCK_SIZE) : \
(op) == BDEV_IOCTL_INIT ? spi_bdev_ioctl(&spi_bdev, (op), (uint32_t)&spiflash_config) : \
spi_bdev_ioctl(&spi_bdev, (op), (arg)) \
)
#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))
Last edited by turoksama on Tue Apr 23, 2019 12:05 pm, edited 1 time in total.

Online
User avatar
Roberthh
Posts: 1416
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: How to expand the size of spiflash?

Post by Roberthh » Tue Apr 23, 2019 11:40 am

Which board are you using, and do you want to extend the file system?

turoksama
Posts: 12
Joined: Sun Mar 31, 2019 8:49 am

Re: How to expand the size of spiflash?

Post by turoksama » Tue Apr 23, 2019 11:46 am

Hi,
A custom board designed by myself, STM32F411RET6 CPU.
Mostly looks like PYBV10, but embeded an spiflash of 16MB capacity(use softqspi).
Due to the size of firmware(add some custom libs), I have to use that spiflash for USB storage instead of internal flash.

Online
User avatar
Roberthh
Posts: 1416
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: How to expand the size of spiflash?

Post by Roberthh » Tue Apr 23, 2019 11:53 am

If it is an ESP32, the magic is in modules/flashbdev.py. You get a larger file system, if you change it to the one below. Please note that only the last line is changed.

Code: Select all

import esp

class FlashBdev:

    SEC_SIZE = 4096
    START_SEC = esp.flash_user_start() // SEC_SIZE

    def __init__(self, blocks):
        self.blocks = blocks

    def readblocks(self, n, buf):
        #print("readblocks(%s, %x(%d))" % (n, id(buf), len(buf)))
        esp.flash_read((n + self.START_SEC) * self.SEC_SIZE, buf)

    def writeblocks(self, n, buf):
        #print("writeblocks(%s, %x(%d))" % (n, id(buf), len(buf)))
        #assert len(buf) <= self.SEC_SIZE, len(buf)
        esp.flash_erase(n + self.START_SEC)
        esp.flash_write((n + self.START_SEC) * self.SEC_SIZE, buf)

    def ioctl(self, op, arg):
        #print("ioctl(%d, %r)" % (op, arg))
        if op == 4:  # BP_IOCTL_SEC_COUNT
            return self.blocks
        if op == 5:  # BP_IOCTL_SEC_SIZE
            return self.SEC_SIZE

size = esp.flash_size()
if size < 1024*1024:
    # flash too small for a filesystem
    bdev = None
else:
    # for now we use a fixed size for the filesystem
    bdev = FlashBdev(size // FlashBdev.SEC_SIZE - FlashBdev.START_SEC)

turoksama
Posts: 12
Joined: Sun Mar 31, 2019 8:49 am

Re: How to expand the size of spiflash?

Post by turoksama » Tue Apr 23, 2019 11:55 am

Well, it is stm32 platform.
So, why the capacity setting is not affective? Are there any tricks hide somewhere?

chrismas9
Posts: 98
Joined: Wed Jun 25, 2014 10:07 am

Re: How to expand the size of spiflash?

Post by chrismas9 » Thu Apr 25, 2019 4:50 am

The F411 does not have a hardware QSPI controller. You will need to use 1 bit SPI mode with hard or soft SPI. Have a look at STM32L476DISC port for example.

turoksama
Posts: 12
Joined: Sun Mar 31, 2019 8:49 am

Re: How to expand the size of spiflash?

Post by turoksama » Thu Apr 25, 2019 12:54 pm

Thanks for your help, i will make it a go.

User avatar
Meekdai
Posts: 33
Joined: Mon Jan 29, 2018 12:45 pm

Re: How to expand the size of spiflash?

Post by Meekdai » Fri Apr 26, 2019 12:50 am

I use the W25Q128 configuration on PYBV10 as follows:

Code: Select all

// use external SPI flash for storage
#define MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE (0)
#define MICROPY_HW_SPIFLASH_SIZE_BITS (128 * 1024 * 1024)
#define MICROPY_HW_SPIFLASH_CS      (pin_A2)
#define MICROPY_HW_SPIFLASH_SCK     (pin_A5)
#define MICROPY_HW_SPIFLASH_MOSI    (pin_A7)
#define MICROPY_HW_SPIFLASH_MISO    (pin_A6)
Or you can refer to this https://github.com/micropython/micropython/issues/4586

User avatar
jimmo
Posts: 245
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: How to expand the size of spiflash?

Post by jimmo » Sun Apr 28, 2019 2:44 am

@turoksama it looks like you're using the spiflash #defines, but setting qspiflash pins. e.g. MICROPY_HW_SPIFLASH_IO0 isn't actually used by anything (unless you've also written more code not posted here to configure the soft qpsi?)

Have a look at ports/stm32/boards/STM32F769DISC/mpconfigboard.h to see an example that configures qspiflash (this example uses the hardware qspi on the F7, but there's a soft qspi implementation available). There's an example using softqspi in this PR: https://github.com/micropython/micropyt ... d17b9d1869 have a look at mpconfigport.h and bdev.c for (e.g.) PYBD_SF2

Alternatively, just to get it working with regular SPI, perhaps try setting the _MOSI and _MISO pins (as per @Meekdai suggestion above).

turoksama
Posts: 12
Joined: Sun Mar 31, 2019 8:49 am

Re: How to expand the size of spiflash?

Post by turoksama » Sun Apr 28, 2019 8:32 am

jimmo wrote:
Sun Apr 28, 2019 2:44 am
@turoksama it looks like you're using the spiflash #defines, but setting qspiflash pins. e.g. MICROPY_HW_SPIFLASH_IO0 isn't actually used by anything (unless you've also written more code not posted here to configure the soft qpsi?)

Have a look at ports/stm32/boards/STM32F769DISC/mpconfigboard.h to see an example that configures qspiflash (this example uses the hardware qspi on the F7, but there's a soft qspi implementation available). There's an example using softqspi in this PR: https://github.com/micropython/micropyt ... d17b9d1869 have a look at mpconfigport.h and bdev.c for (e.g.) PYBD_SF2

Alternatively, just to get it working with regular SPI, perhaps try setting the _MOSI and _MISO pins (as per @Meekdai suggestion above).
Hello,
I modified:
```
#define MICROPY_HW_BDEV_IOCTL(op, arg) ( \
(op) == BDEV_IOCTL_NUM_BLOCKS ? (MICROPY_HW_SPIFLASH_SIZE_BYTES / MICROPY_HW_SPIFLASH_BLOCK_SIZE) : \
(op) == BDEV_IOCTL_INIT ? spi_bdev_ioctl(&spi_bdev, (op), (uint32_t)&spiflash_config) : \
spi_bdev_ioctl(&spi_bdev, (op), (arg)) \
)
#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))
```

back to

```
#define MICROPY_HW_BDEV_IOCTL(op, arg) ( \
(op) == BDEV_IOCTL_NUM_BLOCKS ? (MICROPY_HW_SPIFLASH_SIZE_BYTES / FLASH_BLOCK_SIZE) : \
(op) == BDEV_IOCTL_INIT ? spi_bdev_ioctl(&spi_bdev, (op), (uint32_t)&spiflash_config) : \
spi_bdev_ioctl(&spi_bdev, (op), (arg)) \
)
#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))
```
And add some codes in spidev.c, I dont know if it's necessary with spidev.c .

Post Reply