Skip to content

Flash Memory

MX25L12833F

The Renesas-Arduino core includes a built-in FATFileSystem library to control the QSPI flash memory. This example code demonstrates how to initialize the flash memory, a file is written, and data is read from the file. Users can also find other example sketches in the File > Examples > Storage drop-down menu.

QSPI_Flash_FileSystem_Test.ino
/**
  QSPI Flash File System test
  Name: QSPI_Flash_FileSystem_Test.ino
  Purpose: This sketch test the onboard QSPI Flash memory
  file system by writing and reading a file.

  @author Arduino Team
  @version 1.0 20/06/23
*/

// Include necessary libraries and drivers
#include "QSPIFlashBlockDevice.h"
#include "FATFileSystem.h"

// Define constants for file system and test file name
#define QSPI_FS_NAME "qspi"
#define TEST_FILE "test.txt"

// Instantiate flash block device and file system
QSPIFlashBlockDevice block_device(PIN_QSPI_CLK, PIN_QSPI_SS, PIN_QSPI_D0, PIN_QSPI_D1, PIN_QSPI_D2, PIN_QSPI_D3); 
FATFileSystem fs(QSPI_FS_NAME);

// Define full path to the test file
std::string file_test_name = std::string("/") + std::string(QSPI_FS_NAME) + std::string("/") + std::string(TEST_FILE); 

void setup() {
  // Initialize serial communication and wait a connection
  Serial.begin(9600);
  while(!Serial);

  // Print test start message
  Serial.println();
  Serial.println("- SIMPLE QSPI FLASH TEST");
  Serial.println();

  // Try to mount the QSPI Flash file system
  // If mounting fails, try to format the file system
  int err =  fs.mount(&block_device);
  if (err) {
    Serial.println("- No filesystem found, formatting... ");
    err = fs.reformat(&block_device);
  }
  if (err) {
    // If formatting fails, print error and halt
    Serial.println("- Error formatting QSPI Flash ");
    while(1);
  }

  // Try to open a test file for writing
  // If file opened successfully, write some text to the file
  FILE* fp = fopen(file_test_name.c_str(), "w");
  if(fp != NULL) {
    Serial.println("- Opened file for writing!");
    char text[] = "Hello from QSPI Flash!\n";
    fwrite(text, sizeof(char), strlen(text), fp);
    // Always close the file after writing to save changes
    fclose(fp);
  }
  else {
    // If file opening fails, print an error message
    Serial.print("- Failed to open file for writing: ");
    Serial.println(file_test_name.c_str());
  }

  // Try to open the test file for reading
  // If file opened successfully, read and print its content
  fp = fopen(file_test_name.c_str(), "r");
  if(fp != NULL) {
    Serial.println("- Opened file for reading!");
    char ch;
    while(fread(&ch, sizeof(char), 1, fp) == 1) {
      Serial.print(ch);
    }
    // Always close the file after reading
    fclose(fp);
  }
  else {
    // If file opening fails, print an error message
    Serial.print("Failed to open file for reading: ");
    Serial.println(file_test_name.c_str());
  }
}

void loop() {}
  • Initialize the onboard Flash memory: After setting up the Serial port, the sketch tries to mount the QSPI Flash file system. If the file system is not found, the code formats the memory to set up a new file system. If there's an error during this process, the code will halt and print an error message to the IDE's Serial Monitor.

    Code
    // Try to mount the QSPI Flash file system
    // If mounting fails, try to format the file system
    int err =  fs.mount(&block_device);
    if (err) {
      Serial.println("- No filesystem found, formatting... ");
      err = fs.reformat(&block_device);
    }
    if (err) {
      // If formatting fails, print error and halt
      Serial.println("- Error formatting QSPI Flash ");
      while(1);
    
  • Write to a file: Next, the sketch attempts to open a file named test.txt in write mode. If this process is successful, it writes the string Hello from QSPI Flash! to the file and then closes it. If it cannot open the file, it will print an error message to the IDE's Serial Monitor.

    Code
    // Try to open a test file for writing
    // If file opened successfully, write some text to the file
    FILE* fp = fopen(file_test_name.c_str(), "w");
    if(fp != NULL) {
      Serial.println("- Opened file for writing!");
      char text[] = "Hello from QSPI Flash!\n";
      fwrite(text, sizeof(char), strlen(text), fp);
      // Always close the file after writing to save changes
      fclose(fp);
    }
    else {
      // If file opening fails, print an error message
      Serial.print("- Failed to open file for writing: ");
      Serial.println(file_test_name.c_str());
    }
    
  • Read from a file: After writing to the file, the sketch attempts to open the same file, but this time in 'read' mode. If successful, it will read the content of the file one character at a time and print each character to the IDE's Serial Monitor. After reading, it closes the file. If it cannot open the file for reading, it will print an error message to the IDE's Serial Monitor.

    Code
    // Try to open the test file for reading
    // If file opened successfully, read and print its content
    fp = fopen(file_test_name.c_str(), "r");
    if(fp != NULL) {
      Serial.println("- Opened file for reading!");
      char ch;
      while(fread(&ch, sizeof(char), 1, fp) == 1) {
        Serial.print(ch);
      }
      // Always close the file after reading
      fclose(fp);
    }
    else {
      // If file opening fails, print an error message
      Serial.print("Failed to open file for reading: ");
      Serial.println(file_test_name.c_str());
    }