Quite some time ago, I published some blogs about the ESP32 PSRAM. I think it is about time to dig a litte deeper into the topic and look at how we can use the unadressable 4 MBytes.

PSRAM and C++ STL

One thing that took me by surprise when I was testing with the actual ESP32 3.2.0 core, is that you just need to switch on the PSRAM in Arduino, and the C++ STL classes will automatically use the PSRAM. So if you need a bigger array: all you will need to do is to use a std::vector!

#include <vector>

// create vector of int with 1000 entries with value 0 
std::vector<int> my_vector(1000, 0);

// create empty vector where you can add entries with push_back() 
std::vector<int> my_vector1;

The std::vector class is very powerful, and there are no reasons to use the old style C arrays any more and it is quite impressive that we can use all the STL container classes w/o any additional code!

HIMEM

The ESP32 can only address 4 MBytes of PSRAM directly. If you have 8MB, the addtional 4 MBytes can only be addressed with a combersome HIMEM paging API.

To make the HIMEM very easy to use, I implemented a library that provides the following classes

  • VectorHIMEM – a paged vector implementation
  • FileHIMEM – An API that let’s you access the HIMEM like a file
  • RingBufferStreamHIMEM – An Arduino Stream based RingBuffer
  • TypedRingBufferHIMEM – A RingBuffer for your custom types

To keep things consistent, I am also providing the corresponding classes for PSRAM.

Should you have the need for additinal container types, it should be easy to implement them on top of the VectorHIMEM class.

Performance Comparison

To minimize the number of allocations, you can call reserve() and indicate the number of entries. Here are the measured timings to insert 100000 int values into a Vector:

  • PSRAM reserve 34 ms
  • PSRAM 94 ms
  • HIMEM reserve 150 ms
  • HIMEM 539 ms

Further information can be found in my esp32-psram library!

 

Categories: Arduino

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *