Arduino入門

2. デバイス&サンプル

5. 拡張モジュール&サンプル

アクセサリー

6. アプリケーション

CoreP4X microSD カード

CoreP4X の microSD カードに関する API とサンプルプログラムです。

サンプルプログラム

コンパイル要件

  • M5Stack ボードマネージャーバージョン >= 3.3.10-a
  • ボード選択 = M5CoreP4X
  • M5Unified ライブラリバージョン >= 0.3.1
  • M5GFX ライブラリバージョン >= 0.3.1

CoreP4X の microSD は 1-bit SPI を使用します:SCK G10、MOSI G7、MISO G8、CS G50。カードスロットの電源は 3V3_L3B から供給され、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);
}

FAT32 でフォーマットした microSD カードを用意し、ルートディレクトリに 480 x 480 の PNG 画像を 2 枚配置します:

書き込み後、プログラムは microSD をマウントして /WriteTest.txt を作成し、その後 1 秒ごとに PNG 画像を切り替えて表示します。

API

CoreP4X の microSD は Arduino 標準の SPI、SD ライブラリと M5GFX の drawPngFile を使用します。その他の API については以下を参照してください:

Page Tools
PDF
On This Page