Comments (3)
The assembler does use quite a bit of memory (it's actually mostly about memory fragmentation, rather than purely allocating it all).
But: the ULP requires "no memory" - at least not any memory that is available to MicroPython. MicroPython does not use the RTC_SLOW_MEM for any of its normal operation.
So as soon as you have loaded a program into the ULP, it will happily run irrespective of MicroPython's memory usage and vice versa.
I would see 3 ways of addressing your issue:
-
The first one is more of a "likely it wont help (enough)... but it's easy to try, so try it anyway": Simply try to call a garbage collect after assembling+loading your ULP. MicroPython should really do this by itself when it runs out of memory, but a forced garbage collect every so often can help reduce memory fragmentation.
-
Alternatively, after the assembly+loading of the ULP program, carefully deallocate everything again you put in memory and then force a garbage collect. You dont need to worry about variables that went out-of-scope (e.g. variables inside a method that ended), but any global variables of modules you imported can be removed. You can do that with
del variablename
. For modules, you need to free up both the local reference, plus it's entry in sys.modules[]. i.e.del modname; del sys.modules['modname']
. Also note that modules might have loaded further modules and so on, so check the contents ofsys.modules
to del everything you no longer need (and all other reference to those things you have). After all this, callgc.collect()
for the garbage collector to clean up everything no longer needed from RAM. -
The above is a bit tedious, so what I do in my projects is to save the ULP binary to a file and on next boot, load that file instead of re-assembling the ULP source code (thus skipping the memory intensive and slow assembly process). It looks something like this:
## PART 1: save assembled output to file
from esp32_ulp import src_to_binary
source = """\
...assembly code
"""
binary = src_to_binary(source)
with open("counter.ulp", "w") as f:
f.write(binary)
## PART 2: load from file
ulp = ULP()
with open("counter.ulp", "r") as f:
binary = f.read()
ulp.load_binary(0, binary)
ulp.set_wakeup_period(0, 50000) #in usec
ulp.run(entry_addr)
Then make sure you only do PART 1 when really needed (e.g. wrap the with open(..,"r") as f)
in a try...except
and in case of exception, do PART 1 and perhaps even force a restart (import machine; machine.reset()
) afterwards to make sure all RAM is cleared). Then every time you change the source, make sure you delete the "counter.ulp" file to trigger assembly again.
For general memory management topics, it may also be useful to post on https://forum.micropython.org. Once the assembling step is done, there is no need for any (MicroPython) memory anymore for running / keeping-running the ULP. So then on memory management becomes a bit broader than this library.
(One more little tip: If you look at my edge counter (that I shared previously), you can also check if the magic token has been set already (i.e. ULP code is already loaded and running), and then even skip PART2 during a wakeup - i.e. do nothing. In my example I test for the magic token if if mem32[ULP_MEM_BASE + 4*4] & ULP_DATA_MASK == 0xcafe
)
from micropython-esp32-ulp.
Limiting download of that 14k file to those cycles after the ulp has setup worked great. I should have worked that out for myself, thnx again.
Re #2 deleting modules. sys.modules shows 9 esp32 modules (& one flashdev I probaly should leave?) after ulp counter setup. Is there a way to see their size? Since each one needs to be done twice I'm hoping to go the low hanging fruit & just delete the big ones.
from micropython-esp32-ulp.
Do you mean this file?
https://github.com/micropython/micropython/blob/master/ports/esp32/modules/flashbdev.py
I would not worry about keeping a reference to this file. That executes at boot to mount your flash VFS at root. However it's
only a couple hundred bytes long.
You could also make a function that contains a whitelist of modules and then it deletes all other modules from sys.modules.
Keep in mind that this would not delete your local references to the modules.
Also, as far as I know you cannot see the size of the module. The closest you can get to seeing how much RAM modules use is to enter micropython.mem_info(True) and see how much of the RAM is fragmented and how many consecutive blocks are marked as B or M. Link: https://www.adafruitdaily.com/2017/03/31/measure-your-memory-usage-with-mem_info/
from micropython-esp32-ulp.
Related Issues (20)
- github + pypi setup > docs?
- "how to release" docs
- Can't get it to run HOT 16
- Thank you so much for great lib, I cannot figure out I2C RD/WR. HOT 8
- LED is not blinking HOT 3
- Still Struggling HOT 37
- Bug: Upper case expressions not supported
- Requesting a WAKE example HOT 12
- Request for Clarification of ULP GPIO example HOT 1
- Questiona about GPIO Output (LED) example HOT 3
- Update Actions runner image HOT 1
- Fix build
- Decide how to follow the latest MicroPython HOT 2
- Support ESP32-S2 HOT 8
- ulp rtc clock calibration HOT 2
- Add License/Copyright to all files HOT 7
- interrupt from RTC GPIO
- Update quickstart to use mip instead of upip HOT 7
- Release 1.3.0
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from micropython-esp32-ulp.