How to properly increase FLASH_FS on pyboardv11?

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.
SmartTech
Posts: 4
Joined: Tue Sep 22, 2020 4:18 pm

Re: How to properly increase FLASH_FS on pyboardv11?

Post by SmartTech » Thu Sep 24, 2020 11:45 am

Is there any way to increase pyboard PBV11 flash drive?

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

Re: How to properly increase FLASH_FS on pyboardv11?

Post by jimmo » Fri Sep 25, 2020 12:54 am

SmartTech wrote:
Thu Sep 24, 2020 11:45 am
Is there any way to increase pyboard PBV11 flash drive?
The chip has a fixed amount of flash that needs to be used for firmware and filesystem. You can't increase this internal storage, however you could add an external spiflash chip and put the filesystem on there instead.

However, the MicroPython firmware assumes that it should reserve 112kiB for the filesystem, allowing 896kiB for the MicroPython firmware (including any frozen modules).

The PYBV11 firmware only needs about ~400k, so **in theory** there's a fair bit that can be reallocated to the filesystem. However, it's more complicated than that...

On the F405 you have the following sectors

0: 16kiB
1: 16kiB
2: 16kiB
3: 16kiB

4: 64kiB

5: 128kiB
6: 128kiB
7: 128kiB

8: 128kiB
9: 128kiB
10: 128kiB
11: 128kiB

The default layout is to put the bootloader in sector 0, the filesystem in sectors 1,2,3,4 (112kiB), and the firmware in the remaining sectors (5,6,7,8,9,10,11).

But the issue is that (on STM32F4) you can only erase on a whole-sector boundary, but our filesystems (FAT, LFS) work on much smaller block sizes. So that means you need a in-RAM cache that is as big as the largest sector so that you can do read-update-write to the whole sector. Unfortunately, we only have 64kiB of RAM available for a cache (in CCMRAM) so that means we're limited to just using the sectors that are 64kiB or smaller (i.e. 0-4) for the filesystem.

If you need more space for Python code, I suggest freezing the Python code instead (which will make it part of the firmware). For other resources (such as images or data files), then you can also freeze them into the firmware too -- see https://github.com/micropython/micropyt ... -562918657

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

Re: How to properly increase FLASH_FS on pyboardv11?

Post by jimmo » Fri Sep 25, 2020 1:11 am

Note: If you were prepared to waste half of the 128kiB sectors, you could treat them like 64kiB sectors. (i.e. edit the layout defined at the top of stm32/flash.c). So MicroPython would think they were 64kiB sectors and ignore the top half.

A simpler example is of doing this with just sector 11 is in the top of stm32/flashbdev.c (line 51). If you enable that, then modify the linker script to make FLASH_TEXT 128kiB smaller, then you'll get an extra 64kiB of filesystem.

SmartTech
Posts: 4
Joined: Tue Sep 22, 2020 4:18 pm

Re: How to properly increase FLASH_FS on pyboardv11?

Post by SmartTech » Thu Oct 15, 2020 8:59 am

Sorry for the late reply, but your suggestion was really simple and quick to do. I should have looked few line below! I would still like to know why I can't add other section as it was suggested in the beginning of this issue. Their is still half of the memory not used, but 64KB more is enough for my need for now.

Thanks for your help jimmo!

Post Reply