Portenta - Usage of SDRAM.h library

According to the datasheet, the Arduino Portenta has 8MB of SDRAM.

I did not found any official documentation how to access these.

I found a post on stackexchange, where the usage of the SDRAM.h library is recommended.

With the following sketch I'm trying to test this. But I don't get the results back that I'm writing to the sdram

#include <SDRAM.h>

#define file_size 512

SDRAMClass ram;
byte * file; 

void setup() {
  SerialUSB.begin (9600);
  while (!SerialUSB) ;

  ram.begin();
  file = (uint8_t *)ram.malloc(file_size * sizeof(byte));
  
  for(uint32_t i = 0; i < file_size;i++){
    *(file+i)=0x12;
  }
}

void loop() {
  for(uint32_t i = 0; i < file_size;i++){
    
    if(*(file+i)!=0x12){
          SerialUSB.print(F("ERROR AT: "));
          SerialUSB.println(i);
    }
  }
  for(uint32_t i = 0; i < file_size;i+=32){
    for(int j = 0; j < 32 ;j++){
          SerialUSB.print(*(file+i+j),HEX);
    }
    SerialUSB.println("");
  }
  delay(100);
  SerialUSB.println("HALLO");
  
}

Outputs:

ERROR AT: 52
ERROR AT: 53
ERROR AT: 148
ERROR AT: 149
ERROR AT: 180
ERROR AT: 181
ERROR AT: 212
ERROR AT: 213
ERROR AT: 252
ERROR AT: 253
ERROR AT: 276
ERROR AT: 277
ERROR AT: 340
ERROR AT: 341
ERROR AT: 404
ERROR AT: 405
ERROR AT: 436
ERROR AT: 437
ERROR AT: 468
ERROR AT: 469
1212121212121212121212121212121212121212121212121212121212121212
1212121212121212121212121212121212121212FAFB12121212121212121212
1212121212121212121212121212121212121212121212121212121212121212
1212121212121212121212121212121212121212121212121212121212121212
1212121212121212121212121212121212121212FAFB12121212121212121212
1212121212121212121212121212121212121212FFFF12121212121212121212
1212121212121212121212121212121212121212FAFB12121212121212121212
12121212121212121212121212121212121212121212121212121212FAFB1212
1212121212121212121212121212121212121212FFFF12121212121212121212
1212121212121212121212121212121212121212121212121212121212121212
1212121212121212121212121212121212121212FAFB12121212121212121212
1212121212121212121212121212121212121212121212121212121212121212
1212121212121212121212121212121212121212FBFF12121212121212121212
1212121212121212121212121212121212121212FAFB12121212121212121212
1212121212121212121212121212121212121212FAFB12121212121212121212
1212121212121212121212121212121212121212121212121212121212121212

With different loop Iterations, the number of errors varies.

So what am I missing out?

I tried different portentas.

Thank you

I have also been looking into this issue without any success yet. If I run your program, I get the mostly the same results, however my errors are usually at the same locations (52, 53, 80, 81, 144 and 272 are common locations) and usually the value in SDRAM only differs from 0x12 by 1 flipped byte.

When saving larger types (e. g. 64 bit integers) to SDRAM like in a bigger project of mine, the errors are so large that the SDRAM is essentially useless. I will try to find a fix for this issue as soon as possible (and then post that here), but it is not my top priority at this moment.

However, one thing for your code: instead of creating an instance of SDRAMClass ram and then calling ram.begin() and ram.malloc(...) on it, you can use the built-in object SDRAM, leave out the instantiation of SDRAMClass and call begin() and malloc(...) directly on SDRAM.
This should not change any functionality of the code, since SDRAMClass does not have any instance variables.

I am interested in using the SDRAM on the Portenta to store large frameBuffers and arrays. Any other example code would be appreiciated.

So far I have success with


#include <SDRAM.h>

SDRAMClass mySDRAM;

uint8_t *sdram_frame_buffer;



// in the setup

  
  mySDRAM.begin(SDRAM_START_ADDRESS);   // for camera 320x320

  sdram_frame_buffer = (uint8_t *)mySDRAM.malloc(320 * 320 * sizeof(uint8_t));
 

// in the main loop
   int myCamResult =  myCam.grab(sdram_frame_buffer); // myCamResult should be zero 



I have more advanced code that better aligns the SDRAM buffer.

#include <SDRAM.h>


SDRAMClass mySDRAM;

uint8_t *ei_camera_frame_buffer; // 32-byte aligned
static uint8_t *ei_camera_frame_mem;

  
  mySDRAM.begin(SDRAM_START_ADDRESS);   // for camera 320x320



    ei_camera_frame_mem = (uint8_t *) SDRAM.malloc(320 * 320 + 32 /*alignment*/);
    ei_camera_frame_buffer = (uint8_t *)ALIGN_PTR((uintptr_t)ei_camera_frame_mem, 32);



// main loop

   int myCamResult =  myCam.grab(ei_camera_frame_buffer); // myCamResult should be zero 



The above work great but they are using only pointers

What I am now trying to do is replace a large array in SDRAM and then use it like a regular array in my program. Any suggestions?

const unsigned char model_tflite[] = { ...}
unsigned int model_tflite_len = 2640; // length of above array to put into SDRAM

  modelSetup(model_tflite);  // How the array is used in setup
  

Any suggestions how to do the above with the SDRAM pointer?

I just tried a forth Arduino Portenta, which I bought a few months later. With this portenta, the issue is not present. So I'd guess its a batch of somewhat faulty hardware...