SPI Flash for PyBoard

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
bradstew
Posts: 41
Joined: Thu Nov 29, 2018 9:29 pm

SPI Flash for PyBoard

Post by bradstew » Mon Feb 08, 2021 10:08 pm

Hello,

I have designed several boards using the STM32F405 and run a slightly modified version of uPython 1.3.
I have added 2M and 16M external serial flash and have libraries to access them. But I can only do this using OS libraries within the Python environment.

Is there a plan, or a way to use external Flash with uPython 1.3 on an STM32F405/7 as main flash so that it enumerates as a disk drive? Right now the size in the internal flash is relatively small by comparison. To me, this is the only weakness of the PyBoard. Even so, it's the best implementation of MicroPython I've seen.

I started some development on a STM32F407 Discovery board using ST's CubeIDE. This board as a 2MB serial flash. The demo software includes USB file support so when you connect it to USB, it enumerates as a 2MB flash card. Internal flash is not used for file storage (similar as some Circuit Python boards).

Thanks,

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

Re: SPI Flash for PyBoard

Post by jimmo » Mon Feb 08, 2021 11:35 pm

bradstew wrote:
Mon Feb 08, 2021 10:08 pm
Is there a plan, or a way to use external Flash with uPython 1.3 on an STM32F405/7 as main flash so that it enumerates as a disk drive?
Yes, this can be done in your mpconfigboard.h

Have a look at, for example, the PYBD_SF2 board. The way this works is that you have:
- Internal flash (main firmware)
- External spiflash 1 (sw qspi, filesystem, exposed over USB)
- External spiflash 2 (memory mapped using hw qspi, additional firmware)

The key details:
- stm32/storage.c provides the pyb.Flash class (which is also used for USB MSC) on top of a block device
- stm32/flash.c provides a driver for the internal flash
- stm32/flashbdev.c provides a block device using the internal flash driver
-stm32/spibdev.c provides a block device driver for external spiflash

So PYBD_SF2 sets MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE to disable flashbdev.c, but instead provides the MICROPY_HW_BDEV_ macros using the spibdev implementation (otherwise these macros would default to using flashbdev.c's implementation).

bradstew
Posts: 41
Joined: Thu Nov 29, 2018 9:29 pm

Re: SPI Flash for PyBoard

Post by bradstew » Tue Feb 09, 2021 12:45 am

Thanks! That sounds pretty easy.

Then internal flash is only used for kernel and frozen code/applications as a .dfu object. Is this correct?

If so, then what is purpose of stm32/flash.c for internal flash? Is internal flash still available as a flash file device?

Also, I assume s/w qspi is internal 4-bit but serializes to single data line (since STM32F405/7 does not have h/w qspi).

Brad

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

Re: SPI Flash for PyBoard

Post by jimmo » Tue Feb 09, 2021 2:09 am

bradstew wrote:
Tue Feb 09, 2021 12:45 am
Then internal flash is only used for kernel and frozen code/applications as a .dfu object. Is this correct?
Yep, exactly.
bradstew wrote:
Tue Feb 09, 2021 12:45 am
If so, then what is purpose of stm32/flash.c for internal flash? Is internal flash still available as a flash file device?
It will be unused if you're not using flashbdev.c (the linker will GC the unused symbols during linking).
bradstew wrote:
Tue Feb 09, 2021 12:45 am
Also, I assume s/w qspi is internal 4-bit but serializes to single data line (since STM32F405/7 does not have h/w qspi).
Yep, I was describing how it works on the PYBD, but yeah take a look at (say) STM32L476DISC/mpconfigboard.h for a L4 board that is doing (I think) exactly what you want.

bradstew
Posts: 41
Joined: Thu Nov 29, 2018 9:29 pm

Re: SPI Flash for PyBoard

Post by bradstew » Tue Feb 09, 2021 5:58 pm

Thanks Mate!

bradstew
Posts: 41
Joined: Thu Nov 29, 2018 9:29 pm

Re: SPI Flash for PyBoard

Post by bradstew » Wed Feb 10, 2021 11:45 pm

So I had some success! I have a full 16MB with V1.13.

Next, I got a fresh clone of the repo for V1.14 and it compiles the non-external-flash without any problems.
Capture.JPG
Capture.JPG (18.42 KiB) Viewed 4550 times
However, when I enable the external flash, it fails. This doesn't happen with V1.13.

Problem seems to be with bdev.c (which I borrowed from /boards/STM32L476.
Capture1.JPG
Capture1.JPG (61.95 KiB) Viewed 4550 times
I've also attached the configuration file and and bdev.c (Note that changing line #8 in mpconfigboard.h had no effect)

Any ideas?
Attachments
Desktop.zip
(921 Bytes) Downloaded 151 times

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

Re: SPI Flash for PyBoard

Post by jimmo » Thu Feb 11, 2021 5:28 am

bradstew wrote:
Wed Feb 10, 2021 11:45 pm
However, when I enable the external flash, it fails. This doesn't happen with V1.13.
The cache was made optional in v1.14. See https://github.com/micropython/micropython/pull/6704

FWIW -- the steps to figure this out:
- See that .cache is set in other boards (like the one you copied), so it must be valid.
- Look at the definition of the mp_spiflash_config_t struct
- See that .cache is guarded by MICROPY_HW_SPIFLASH_ENABLE_CACHE
- This is enabled on the working board.
- Do a git blame on that line (added Dec 2020).

bradstew
Posts: 41
Joined: Thu Nov 29, 2018 9:29 pm

Re: SPI Flash for PyBoard

Post by bradstew » Fri Feb 12, 2021 2:08 am

So I added #define MICROPY_HW_SPIFLASH_ENABLE_CACHE (1) to mpconfigboard.h.
Mostly compiled until it got to the end:
Capture.JPG
Capture.JPG (39.36 KiB) Viewed 4520 times
It's all a bit odd. I can successfully build STM32L476DISC.

bradstew
Posts: 41
Joined: Thu Nov 29, 2018 9:29 pm

Re: SPI Flash for PyBoard

Post by bradstew » Sat Feb 13, 2021 12:57 am

No matter what I try, I just can't get my board to compile with V1.14. So for now, I'll just keep using V1.13.

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

Re: SPI Flash for PyBoard

Post by jimmo » Mon Feb 15, 2021 3:16 am

bradstew wrote:
Fri Feb 12, 2021 2:08 am
Mostly compiled until it got to the end:

(missing reference to storage_readblocks_ext)
This means that:
- You've compiled with support for LFS
- You haven't enabled MICROPY_HW_BDEV_SPIFLASH_EXTENDED

The reason STM32L476DISC works is that it does not enable LFS.

Take a look at either PYBD_SF2 or F769DISC for an example.

Post Reply