Git Product home page Git Product logo

Comments (3)

wnienhaus avatar wnienhaus commented on August 28, 2024

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:

  1. 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.

  2. 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 of sys.modules to del everything you no longer need (and all other reference to those things you have). After all this, call gc.collect() for the garbage collector to clean up everything no longer needed from RAM.

  3. 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.

kjm1102 avatar kjm1102 commented on August 28, 2024

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.

RSC-Games avatar RSC-Games commented on August 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.