English
English
简体中文
日本語

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

CoreP4X microSD Card

APIs and example programs related to the microSD card of CoreP4X.

Example Program

Compilation Requirements

  • M5Stack Board Manager version >= 3.3.10-a
  • Board selection = M5CoreP4X
  • M5Unified library version >= 0.3.1
  • M5GFX library version >= 0.3.1

CoreP4X microSD uses 1-bit SPI: SCK G10, MOSI G7, MISO G8, CS G50. The card slot is powered from 3V3_L3B, controlled by M5IOE1 IO12.

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#include <SPI.h>
#include <SD.h>
#include <M5Unified.h>
#include <utility/M5IOE1_Class.hpp>

#define SD_SPI_CS_PIN   50
#define SD_SPI_SCK_PIN  10
#define SD_SPI_MISO_PIN 8
#define SD_SPI_MOSI_PIN 7

static constexpr auto IOE_L3B_EN = m5::M5IOE1_Class::gpio12;

static bool sdReady = false;

void setup()
{
  Serial.begin(115200);
  delay(500);
  Serial.println("CoreP4X SD boot");

  auto cfg = M5.config();
  cfg.serial_baudrate = 115200;
  M5.begin(cfg);

  M5.Display.setRotation(0);
  M5.Display.setBrightness(160);
  M5.Display.fillScreen(TFT_BLACK);

  Serial.println("CoreP4X microSD init");
  Serial.printf("SD pins: SCK=%d MOSI=%d MISO=%d CS=%d\n",
                SD_SPI_SCK_PIN, SD_SPI_MOSI_PIN, SD_SPI_MISO_PIN, SD_SPI_CS_PIN);

  auto& ioe = M5.getIOExpander(0);
  const bool powerOk =
      ioe.setHighImpedance(IOE_L3B_EN, false) &&
      ioe.setDirection(IOE_L3B_EN, true) &&
      ioe.digitalWrite(IOE_L3B_EN, true);
  Serial.printf("L3B power enable: %s\n", powerOk ? "OK" : "FAILED");
  delay(300);

  SPI.begin(SD_SPI_SCK_PIN, SD_SPI_MISO_PIN, SD_SPI_MOSI_PIN, SD_SPI_CS_PIN);
  sdReady = SD.begin(SD_SPI_CS_PIN, SPI, 4000000);
  Serial.printf("SD mount: %s\n", sdReady ? "OK" : "FAILED");

  M5.Display.setTextDatum(middle_center);
  if (!sdReady) {
    M5.Display.setTextColor(TFT_RED, TFT_BLACK);
    M5.Display.setFont(&fonts::FreeSansBold18pt7b);
    M5.Display.drawString("SD card not detected", 240, 210);
    M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
    M5.Display.setFont(&fonts::FreeSans12pt7b);
    M5.Display.drawString("See Serial Monitor", 240, 268);
    return;
  }

  M5.Display.setTextColor(TFT_GREEN, TFT_BLACK);
  M5.Display.setFont(&fonts::FreeSansBold18pt7b);
  M5.Display.drawString("SD card detected", 240, 210);

  File file = SD.open("/WriteTest.txt", FILE_WRITE, true);
  if (file) {
    file.print("Hello, world!\nSD card write success!\n");
    file.close();
    Serial.println("Write test: OK");
  } else {
    Serial.println("Write test: FAILED");
  }
}

void loop()
{
  M5.update();

  if (!sdReady) {
    delay(200);
    return;
  }

  Serial.println("Draw picture 01");
  M5.Display.fillScreen(TFT_BLACK);
  M5.Display.drawPngFile(SD, "/CoreP4X_test_picture01.png");
  delay(1000);

  Serial.println("Draw picture 02");
  M5.Display.fillScreen(TFT_BLACK);
  M5.Display.drawPngFile(SD, "/CoreP4X_test_picture02.png");
  delay(1000);
}

Prepare a FAT32-formatted microSD card and place two 480 x 480 PNG images in the root directory:

After uploading, the program mounts the microSD card, creates /WriteTest.txt, and then switches the displayed PNG image every second.

API

The microSD section of CoreP4X uses the Arduino standard SPI and SD libraries and the drawPngFile of M5GFX. For more APIs, refer to:

Page Tools
PDF
On This Page