circuitpython: STM32F405 runs out of memory prematurely compared to SAMD51 (5.0.0-beta)

The attached file illustrates an interesting memory issue that’s significantly different between Feather STM32F405 (192k RAM) and Adafruit SAMD51 M4 devices with 192k RAM. The code is simple, and mainly has hundreds of lines just incrementing a dummy variable. The file has just over 1700 lines of code, one line makes the difference between having enough memory or not.

Found this trying my main application (much more complex, with many more imported modules and libraries), with ~1900 lines of code (~79k code.py), which runs on 192k M4, but I had to cut roughly in half (lines of code / file size) to run on STM32F405.

The attached file is only 19k, so seems to be more related to lines (or memory usage of interpretation) rather than code complexity or size of code file.

If it isn’t repeatable, just add or subtract a few more dummy lines. I tested this across power cycles. (testing this with beta.1 because beta.2 has some other issue with STM32F405 (on macOS) where I can’t test this… separate issue has been filed)

For comparison, Feather M4 doesn’t get a memory error with this code until something over 3000 dummy += 1 lines.

Seems to be trying to allocate a lot of memory for a simple addition statement.

Is it possible to show the line number / trace in memory allocation errors?

Github truncates the large file if I try to include it here, so the code is in the comment below, sorry for the length.

Output, first running properly, then re-saving after uncommenting the last dummy += 1 line:

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
-------------------------------------------------
Checking CPU...
Feather STM32F405 Express with STM32F405RG
CircuitPython 5.0.0-beta.1 on 2019-12-10
-------------------------------------------------
Loop...
656.619
..............................................soft reboot

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
MemoryError: memory allocation failed, allocating 15477 bytes



Press any key to enter the REPL. Use CTRL-D to reload.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 23 (11 by maintainers)

Most upvoted comments

ok updated guide/product plz check

@anecdata I’m sorry I didn’t get on here sooner, I could have saved you some time on this. The memory of the F405 is not contiguous - 128KB exist at address 0x2000_0000 - 0x2001_FFFF, but the remaining 64KB is Core Coupled Memory (CCMRAM) at address 0x1000_0000. Currently we do not use this extra RAM at all, pending flash improvements where it will be used to buffer the internal filesystem to increase it by 64KB and improve read/write speeds (Micropython uses it for the same purpose). You can see a diagram of this memory in the attached image from page 71 of the F405 datasheet: Screen Shot 2019-12-23 at 1 22 40 PM

So for the foreseeable future, you should treat the F405 as having only 128KB of accessible memory, same as micropython. However, it might be worth discussing an option to compile without an internal filesystem for boards that don’t need it, so we can explore direct access to the CCRAM for user code.

@dhalbert Would you like me to keep this issue open for the fragmentation issue? I’m not sure what that could be.