GIGA crashes on SDRAM array access

I'm trying to create an array in SDRAM following the "cheat sheet" example, but the GIGA crashes (red LED flashes) the instant I try to write anything to the created array. The code is below, and follows the cheat sheet malloc procedure (I renamed the arrray). Everything I've tried instantly crashes, although I can reprogram OK when this happens if I time it right rebooting. Anyone see what I'm doing wrong? This test example just attempts to read an ADC channel into the SDRAM array, where up to 250,000 points are needed.

/*
 Test program for accessing and using the SDRAM on GIGA R1 (from cheat sheet notes).
*/

#include "SDRAM.h"           // To access 8 MB of SDRAM external to the processor 
#define DCInPin A0

int usdelay = 2;

// Allocate 5 MB of SDRAM (none of the combinations below work)
uint8_t* bigArray = (uint8_t*)SDRAM.malloc(5 * 1024 * 1024);   // Allocate 5 MB of SDRAM (cheat sheet uses uint8_t)
//uint16_t* bigArray = (uint16_t*)SDRAM.malloc(5 * 1024 * 1024);   // Allocate 5 MB of SDRAM (unit_16)
//float* bigArray = (float)SDRAM.malloc(5 * 1024 * 1024);   // Allocate 5 MB of SDRAM (try float)

// Assign number of points per raw data array (250,000). Three additional arrays of this same size are needed
// for the processing arrays, so a total of 250,000 * 4 bytes/float * 4 arrays = 4 MB.  Allocate 5MB to be safe
// in the above malloc statement. uint16_t behaves the same was as using unit_8 or float (all crash GIGA) when
// bigArray[] is accessed).

unsigned long NPTS = 250000;    // 250,000 points to collect for raw data

/*********************************************************************************/
void setup()                    // One-time initialization code
{
  SDRAM.begin();       // Needed? Program crashes whether this is present, or not (cheat sheet doesn't show it)
  delay(1000);         // 1s delay for SDRAM init

// Set the ADC resolution
  analogReadResolution(16);     // Set ADC resolution to 16 bits

// Initialize serial communications at 115200 baud
  Serial.begin(115200);
  delay(500);
  Serial.println("Serial port initialized");
}                 // End of setup()

/******************************************************************************/
void loop()    // Grab one scan to test if SDRAM array being filled
{
  unsigned long i;
  delay(8000);        //  8s delay for GIGA crash and reprogram attempts
  Serial.println("Starting spectrum collection...");
  for(i=0; i<NPTS; i++)        // Digitize preamp over scan range and NPTS
    {
      bigArray[i] = analogRead(DCInPin);      // Get laser power (DC) value
      delayMicroseconds(usdelay);             // Adjust usdelay as needed
    }                  // End loop over ADC reads
  Serial.println("Scan Collected Successfully");
  SDRAM.free(bigArray);
}                      // End of Loop

Hi @rmay_pci Declare your bigArray as a nullptr and then do bigArray = (uint8_t*)SDRAM.malloc(5 * 1024 * 1024); after you've done the SDRAM.begin();

Actually, your crash may be being caused by the SDRAM.free(bigArray); at the end of your loop as 2nd time round the mems is not realloced. Also analogRead will be returning int (32-bit) into 8-bit array member

p.s. you may find the sdram example in the IDE better to follow than the cheatsheet

Thanks steve9. I didn't have the SDRAM.free line in initially (still got the crashes) so added that in case it was needed. But the crash happens the instant bigArray[] is first written to (or the attempt at this). I didn't realize there was an example in the IDE, so I'll look for that. I'd like to use floats and need 4 MB total for those at 4 bytes each, so I tried uint16_t as well as float in case that was the issue, but I forgot to reduce the SDRAM size in the call and thought maybe the malloc statement only needed a total size in bytes, regardless of the variable type being used. I'll see what I can find in the IDE for a better example to start from. Thanks again for the tips.

Yes, that is true. You allocate the space you want in bytes, then you can use it as you like, but bigArray[i] is always a byte. You could use a structure to store your data or use highByte/lowByte to store a 16-bit ADC value using bigArray[i] and bigArray[i+1]

I tried the code below and it works, so thanks again for the help. The IDE example shows that you can declare multiple arrays in the space, which will make life easier for my main program as I had rewritten it to manipulate the indices of bigArray[] to access my data in 4 different sequential blocks of this one big array. Making 4 smaller arrays with different names makes things much more readable and easier to follow. In case anyone else runs into this, the example below does the trick (kudos to steve9).

/*
 Test program for accessing and using the SDRAM on GIGA R1 (from IDE example, and steve9).
*/

#include "SDRAM.h"           // To access 8 MB of SDRAM external to the processor 
#define DCInPin A0

int usdelay = 2;

// Assign number of points per raw data array (250,000).
unsigned long NPTS = 250000;    // 250,000 points to collect for raw data

// Declare main data array
uint16_t* bigArray = nullptr;

/*********************************************************************************/
void setup()                    // One-time initialization code
{
  SDRAM.begin();       // Needed? Program crashes whether this is present, or not (cheat sheet doesn't show it)
  delay(1000);         // 1s delay for SDRAM init
  bigArray = (uint16_t*)SDRAM.malloc(2 * 1024 * 1024);

// Set the ADC resolution
  analogReadResolution(16);     // Set ADC resolution to 16 bits

// Initialize serial communications at 115200 baud
  Serial.begin(115200);
  delay(500);
  Serial.println("Serial port initialized");
}                 // End of setup()

/******************************************************************************/
void loop()    // Grab one scan to test if SDRAM array being filled
{
  unsigned long i;
  delay(5000);        //  5s delay between test cycles
  Serial.println("Starting spectrum collection...");
  for(i=0; i<NPTS; i++)        // Digitize preamp over scan range and NPTS
    {
      bigArray[i] = analogRead(DCInPin);      // Get 16-bit data
      delayMicroseconds(usdelay);             // Adjust usdelay as needed
    }                  // End loop over ADC reads
  Serial.println("Scan Collected Successfully");
  // Print a few value for diagnostics
  for(i=0; i<NPTS; i+=20000)            // Digitize preamp over scan range and NPTS
    {
      Serial.println(bigArray[i]);      // Print to serial port
    }                  // End loop over ADC reads
  Serial.println("Waiting 5s to try again");
}                      // End of Loop

Glad you're not crashing, and happy to help.