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.