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...