View Single Post
Old 02-23-2021, 12:43 AM   #67
stevenaleach
Enthusiast
stevenaleach can bend spoons with a thought.stevenaleach can bend spoons with a thought.stevenaleach can bend spoons with a thought.stevenaleach can bend spoons with a thought.stevenaleach can bend spoons with a thought.stevenaleach can bend spoons with a thought.stevenaleach can bend spoons with a thought.stevenaleach can bend spoons with a thought.stevenaleach can bend spoons with a thought.stevenaleach can bend spoons with a thought.stevenaleach can bend spoons with a thought.
 
Posts: 46
Karma: 134116
Join Date: Oct 2013
Device: Android Tablet
Quote:
Originally Posted by hobnail View Post
I'm still unclear on how the PSRAM can be used. Can you load code in it and run the code from it?
It's just ram. In Micropython you need do nothing special at all to use it - it's just available memory which will be allocated and used by your code. In Arduino C, you call ps_malloc, but what you get back is just a pointer like any other, it's just ram. As to whether it's executable memory you could put code in, I don't know - but for the most part you would never load code into RAM at all - your code will usually be in flash and thus doesn't occupy any of the available RAM - most boards (including the M5Paper) seem to have 16MB of flash.

So in Micropython:
----------------------------------------------------------------------------------------------------
>>> import gc
>>> gc.mem_free()
4096944
>>> bar = bytearray([0]*1024*750)
>>> len(bar)
768000
>>> gc.mem_free()
252224
----------------------------------------------------------------------------------------------------
So... there's some overhead for a byte array in Python, it seems, because creating the 750k buffer above uses over 3MB of memory... *BUT* it's also more memory than the ESP32 has internally, so that tells us that MicroPython is making use of the PSRAM.

In Arduino C: (https://thingpulse.com/esp32-how-to-use-psram/)
----------------------------------------------------------------------------------------------------
#include <Arduino.h>

void logMemory() {
log_d("Used PSRAM: %d", ESP.getPsramSize() - ESP.getFreePsram());
}

void setup() {
logMemory();
byte* psdRamBuffer = (byte*)ps_malloc(500000);
logMemory();
free(psdRamBuffer);
logMemory();
}

void loop() {}
----------------------------------------------------------------------------------------------------

So there's some C code that allocates and then frees 500,000 bytes of memory in PSRAM with ps_malloc - it returns a memory pointer like any other, you don't need to worry about whether it's in internal ram or PSRAM, it's just ram. Free it with free() when you're done - the same as if you'd allocated it with malloc().

So... in short, in MicroPython, there is absolutely nothing different about PSRAM as opposed to RAM. You don't need to worry about it at all. In Arduino C, you use ps_malloc() instead of malloc().... that's it, it's just ram.

Now in Python, if you're entering code at the prompt, the resulting bytecode will occupy RAM (whether internal, PSRAM, or a combination of both). But when you compile and upload C code, it lives in flash memory and doesn't use RAM.
stevenaleach is offline   Reply With Quote