How to load matrix into additional memory 8MB PSRAM on Teensy 4.1

Igor_K

Member
Hi everyone,.
Please help me place the ads of the matrix byteMatrix and the values the matrix byteMatrix in additional memory 8MB PSRAM chip. Memory PSRAM is already on Teensy 4.1 and tested. When compiling, only load RAM1. Below is my code.
Thanks in advance for the community's support.
Code:
#include <SD.h>
#include <SPI.h>
#include <malloc.h>
#include "LittleFS.h"
extern "C" uint8_t external_psram_size;

const int chipSelect = BUILTIN_SDCARD;  //Teensy 4.1
const int rows = 1000;   // 200 max
const long cols = 36000;  // 17000 max

File file;
byte byteMatrix[rows][(cols + 7) / 8]; // объявление байтовой матрицы

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  
  file = SD.open("mat36000.txt", FILE_READ);  // m80-200
  if (!file) {
    Serial.println("No open file.");
    return;
  }

  //  read data from file and convert to byte array
  for (int i = 0; i < rows; i++) {
    for (long j = 0; j < cols; j++) {
      int value = file.parseInt();
      if (value == 1) {
        byteMatrix[i][j / 8] |= (1 << (7 - (j % 8)));
      }
    }
  }
  file.close();

  // displaying element values
  for (int i = 0; i < rows; i++) {
    for (long j = 0; j < cols; j++) {
      byte bitValue = (byteMatrix[i][j / 8] >> (7 - j % 8)) & 1;
      Serial.print(bitValue);
      Serial.print(" ");
    }
    Serial.println();
  }
}

void loop() {
  // code
}
 
One note - you seem to be assuming that byteMartix contains all 0's at startup.
This is not a valid assumption and could result in unexpected behaviour. I've seen things like this cause temperature related software bugs. At room temperature the memory was all 0 and so the code worked. Cool things down and it could start up with values in the memory and the code crashed.
You should use something like memset(byteMatrix,0,rows*((cols + 7) / 8)); to clear it first.
 
Back
Top