himem.h

idahowalker
Posts: 166
Joined: Wed Aug 01, 2018 12:06 pm

himem.h

Postby idahowalker » Sat Oct 19, 2019 11:53 pm

First, I got some code here to access the 1st 4MB of PSRAM of a WROVER model that you may find useful at some point.

Code: Select all

#include "esp_system.h" //This inclusion configures the peripherals in the ESP system.
#include "esp32-hal-psram.h"
// #include "himem.h"
////
struct stuTime
{
  int iSeconds = 0;
  int iMinutes = 0;
  int iHours = 0;
};
////
void setup()
{
  //  log_d("Total heap: %d", ESP.getHeapSize());
  //  log_d("Free heap: %d", ESP.getFreeHeap());
  log_d("Total PSRAM: %d", ESP.getPsramSize());
  log_d("Free PSRAM: %d", ESP.getFreePsram());
  ////
  int *ptr, *ptr1;
  int n, n1, i, sum = 0;

  // Get the number of elements for the array
  n = 10;
  n1 = 20;
  log_d("Number of elements ptr: %d", n);
  log_d("Number of elements ptr1: %d", n1);
  // log_d("Number of elements ptr1: %d\n", n1);
  // Dynamically allocate memory using malloc()
  // ptr = (int*)ps_malloc(n * sizeof(int)); //works
  ptr = (int*)ps_calloc( n, sizeof(int) ); // works
  log_d("Free PSRAM: %d", ESP.getFreePsram());
  //
  ptr1 = (int*)ps_calloc( n1, sizeof(int) ); // works
  log_d("Free PSRAM: %d", ESP.getFreePsram());
  // Check if the memory has been successfully
  // allocated in ps_ram
  if (ptr == NULL) {
    log_d(" ptr memory not allocated.\n");
    exit(0);
  }
  if (ptr1 == NULL) 
  {
    log_d("ptr1 memory not allocated.\n");
    exit(0);
  }
  else 
  {
    // Memory has been successfully allocated
    log_d("ps_ram memory successfully allocated using ps_calloc.");
    // put elements into ps_ram array
    for (i = 0; i < n; ++i)
    {
      ptr[i] = i + 1;
    }
        for (i = 0; i < n1; ++i)
    {
      ptr1[i] = i + 2;
    }
    // Print the elements of the array
    log_d("The elements of the ptr array are: ");
    for (i = 0; i < n; ++i) {
      log_d("%d, ", ptr[i]);
    }
        log_d("The elements of the ptr1 array are: ");
    for (i = 0; i < n1; ++i) {
      log_d("%d, ", ptr1[i]);
    }
  }
  free(ptr);
  free(ptr1);
  // put a structure into psram
  struct stuTime *ptrTime;
  // log_i("Free PSRAM before structure: %d", ESP.getFreePsram());
  ptrTime = (struct stuTime *)ps_malloc(sizeof(struct stuTime));
  // log_i("Free PSRAM after structure: %d", ESP.getFreePsram());
  ptrTime->iSeconds = 10;
  ptrTime->iMinutes = 60;
  ptrTime->iHours = 100;
  log_i("Seconds: %d Minutes: %d Hours: %d", ptrTime->iSeconds, ptrTime->iMinutes, ptrTime->iHours );
  free(ptrTime);
} // setup()
////
void loop() {}
I want to access the other 4 MB of PSRAM on my WROVER-B rev1. I understand about paging of memory. Anyways I am trying to include "himem.h" so I can work on figuring out how to do paging in the Arduino IDE. What is the proper "path" to enter so that I can use; I have tried #include "esp32/himem.h", #include <himem.h>, and a few others with no luck. What is the syntax to include "himem.h" in the Arduino IDE?

ESP_Dazz
Posts: 308
Joined: Fri Jun 02, 2017 6:50 am

Re: himem.h

Postby ESP_Dazz » Mon Oct 21, 2019 9:25 am

Depending on what version of ESP-IDF your Arduino IDE is being compiling with, it could be #include "esp_himem.h" (for release/v3.2) or #include "esp32/himem.h" (for release/v4.0), or not supported at all (for versions earlier than release/v3.2).

See the "esp_himem.h" header for more details.

idahowalker
Posts: 166
Joined: Wed Aug 01, 2018 12:06 pm

Re: himem.h

Postby idahowalker » Tue Oct 22, 2019 2:36 pm

Thanks to ESP_Dazz, I was able to make a bit of progress.

Code: Select all

extern "C" {
#include <esp_spiram.h>
#include <esp_himem.h>
}
in the declare section allows this

Code: Select all

log_i("spiram size %u\n", esp_spiram_get_size());
in my setup to print
[ESP32_wroverRAMcheck.ino:32] setup(): spiram size 8388608
. Now onto figuring out how to do the paging thing.

ESP_Dazz
Posts: 308
Joined: Fri Jun 02, 2017 6:50 am

Re: himem.h

Postby ESP_Dazz » Tue Oct 22, 2019 2:57 pm

I suggest you take a look at the Himem Example if you haven't already.

idahowalker
Posts: 166
Joined: Wed Aug 01, 2018 12:06 pm

Re: himem.h

Postby idahowalker » Tue Oct 22, 2019 5:47 pm

ESP_Dazz wrote:
Tue Oct 22, 2019 2:57 pm
I suggest you take a look at the Himem Example if you haven't already.
I have taken a look at the example. I found

Code: Select all

size_t memcnt=esp_himem_get_phys_size();
    size_t memfree=esp_himem_get_free_size();
do not run under the Arduino IDE or I am missing some other thing.
[hr]
I get this error [ESP32_wroverRAMcheck.ino:149] setup(): ESP_ERR_INVALID_SIZE with this entry

Code: Select all

esp_himem_handle_t mh; //Handle for the address space we're using
  esp_himem_handle_t mh; //Handle for the address space we're using
  esp_himem_rangehandle_t rh; //Handle for the actual RAM.
  esp_err_t intError = esp_himem_alloc( 65535, &mh);
  log_i( "%s", esp_err_to_name( intError) );

idahowalker
Posts: 166
Joined: Wed Aug 01, 2018 12:06 pm

Re: himem.h

Postby idahowalker » Wed Oct 23, 2019 4:33 pm

Make sure that SPI RAM bank switching is enabled. (Compiling the example with default values will automatically enable this.) It can be found under Component config > ESP32-specific > Support for external, SPI-connected RAM > SPI RAM config.
How is the above done using the Arduino IDE?

idahowalker
Posts: 166
Joined: Wed Aug 01, 2018 12:06 pm

Re: himem.h

Postby idahowalker » Wed Oct 23, 2019 7:05 pm

I get this error:

Code: Select all

(117) spiram: SPI RAM not initialized
E (117) esp_himem: Cannot allocate memory for meta info. Not initializing!
[D][esp32-hal-psram.c:47] psramInit(): PSRAM enabled
[I][ESP32_wroverRAMcheck.ino:37] setup(): Total PSRAM: 3932108
[I][ESP32_wroverRAMcheck.ino:38] setup(): Free PSRAM: 3932108
[I][ESP32_wroverRAMcheck.ino:39] setup(): spiram size 8388608
[I][ESP32_wroverRAMcheck.ino:51] setup(): Number of elements ptr: 10
[I][ESP32_wroverRAMcheck.ino:52] setup(): Number of elements ptr1: 20
[I][ESP32_wroverRAMcheck.ino:53] setup(): Number of elements ptr1: 20

[I][ESP32_wroverRAMcheck.ino:57] setup(): Free PSRAM: 3932052
At first the SPI and himem give an error, then they give out the himem and SPI ram info. i figure I'd back up the the beginning and see if I can work on the two top error messages. Ideas, pointers, answers are all welcome.

Who is online

Users browsing this forum: No registered users and 76 guests