#include <SPI.h>
#include <LittleFS.h>
#include "index.h" // Include the index content (content stored in external PSRAM)
// External PSRAM setup
extern "C" uint8_t external_psram_size; // Access to external PSRAM size
bool memory_ok = false;
uint32_t *memory_begin, *memory_end;
void setup() {
// Wait for serial to initialize
while (!Serial);
pinMode(13, OUTPUT); // Initialize LED for status feedback
uint8_t size = external_psram_size; // Get the size of external PSRAM
Serial.printf("EXTMEM Memory initialized, %d Mbyte\n", size);
if (size == 0) {
Serial.println("No external memory found.");
return; // Exit if no external PSRAM is available
}
// Set up external PSRAM memory range
memory_begin = (uint32_t *)(0x70000000); // Base address of external PSRAM
memory_end = (uint32_t *)(0x70000000 + size * 1048576); // End address
// Check if PSRAM is accessible
if ((uintptr_t)memory_begin >= (uintptr_t)0x70000000 && (uintptr_t)memory_end <= (uintptr_t)(0x70000000 + (size * 1048576))) {
Serial.println("External PSRAM initialized.");
memory_ok = true;
} else {
Serial.println("Invalid PSRAM range!");
return;
}
// Store the content of MAIN_page in external PSRAM
storeIndexContent();
}
void storeIndexContent() {
if (!memory_ok) {
Serial.println("External memory not initialized.");
return;
}
// Ensure that MAIN_page from "index.h" is available
if (!MAIN_page) {
Serial.println("Error: MAIN_page is null!");
return;
}
// Allocate memory in PSRAM for storing the MAIN_page content
char *psramIndexContent = (char*)malloc(strlen(MAIN_page) + 1); // Allocate memory for the index content
if (psramIndexContent == NULL) {
Serial.println("PSRAM memory allocation failed.");
return;
}
// Copy the content of `MAIN_page` from `index.h` to PSRAM
strcpy(psramIndexContent, MAIN_page);
// Copy content into external PSRAM
memcpy(memory_begin, psramIndexContent, strlen(psramIndexContent) + 1);
Serial.println("index.h content stored in external PSRAM.");
// Optionally free allocated memory
free(psramIndexContent);
}
void loop() {
// If external memory is initialized, print the index content stored in PSRAM
if (memory_ok) {
// Display content stored in external PSRAM
Serial.println("Index content from PSRAM:");
Serial.println((char*)memory_begin); // Print content from PSRAM
// Delay to avoid overwhelming the Serial Monitor
delay(1000);
}
digitalWrite(13, HIGH); // Blink onboard LED
delay(100);
digitalWrite(13, LOW);
delay(100);
}