본문 바로가기

[ST_MICRO]/STM32G4

[STM32G4 SM EVM] Aruino - SD Card 테스트

SM Type EVM 보드를 테스트 할 수 있는 확장 보드중 NEX-EXP 시리즈 보드는 SD Card 소켓이 실장되어 있다.

 

SD Card CS 핀은 SM Type 핀맵 A열 13번에 할당되어 있어 STM32G431의 PB12에 연결된다.

 

 


SD Card 테스트는 Arduino에서 제공하는 SD 라이브러리를 사용하였다.

 

SD Card 정보를 출력하는 기본 예제를 구동해 보자

#include <SPI.h>
#include <SD.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

// change this to match your SD shield or module;
const int chipSelect = PB12;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("\nInitializing SD card...");

  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card inserted?");
    Serial.println("* is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    Serial.println("Note: press reset button on the board and reopen this Serial Monitor after fixing your issue!");
    while (1);
  } else {
    Serial.println("Wiring is correct and a card is present.");
  }

  // print the type of card
  Serial.println();
  Serial.print("Card type:         ");
  switch (card.type()) {
    case SD_CARD_TYPE_SD1:
      Serial.println("SD1");
      break;
    case SD_CARD_TYPE_SD2:
      Serial.println("SD2");
      break;
    case SD_CARD_TYPE_SDHC:
      Serial.println("SDHC");
      break;
    default:
      Serial.println("Unknown");
  }

  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    while (1);
  }

  Serial.print("Clusters:          ");
  Serial.println(volume.clusterCount());
  Serial.print("Blocks x Cluster:  ");
  Serial.println(volume.blocksPerCluster());

  Serial.print("Total Blocks:      ");
  Serial.println(volume.blocksPerCluster() * volume.clusterCount());
  Serial.println();

  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("Volume type is:    FAT");
  Serial.println(volume.fatType(), DEC);

  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize /= 2;                           // SD card blocks are always 512 bytes (2 blocks are 1 KB)
  Serial.print("Volume size (KB):  ");
  Serial.println(volumesize);
  Serial.print("Volume size (MB):  ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (GB):  ");
  Serial.println((float)volumesize / 1024.0);

  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);

  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);
  root.close();
}

void loop(void) {
}

 

테스트 결과 SD Card의 파일 정보를 Serial 포트로 출력 하는것을 확인 할 수 있다.

Initializing SD card...Wiring is correct and a card is present.

Card type:         SDHC
Clusters:          1942716
Blocks x Cluster:  16
Total Blocks:      31083456

Volume type is:    FAT32
Volume size (Kb):  15541728
Volume size (Mb):  15177
Volume size (Gb):  14.82

Files found on the card (name, date and size in bytes):
20099F~1.JPG  2018-08-24 18:13:12 2659589
    20CEFE~4.JPG  2018-08-25 12:37:32 2685556
    20D086~2.JPG  2018-08-25 15:40:34 2560021
    20F5A6~3.JPG  2018-08-25 21:26:52 2075688
    203E73~2.JPG  2018-08-24 18:31:44 3534098
    200CBD~5.JPG  2018-08-25 21:42:00 2451751
    208B9E~7.JPG  2018-08-25 22:44:06 2757033
반응형