選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

QuickStart.ino 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Quick hardware test.
  2. //
  3. #include <SPI.h>
  4. #include <SdFat.h>
  5. //
  6. // Set DISABLE_CHIP_SELECT to disable a second SPI device.
  7. // For example, with the Ethernet shield, set DISABLE_CHIP_SELECT
  8. // to 10 to disable the Ethernet controller.
  9. const int8_t DISABLE_CHIP_SELECT = -1;
  10. //
  11. // Test with reduced SPI speed for breadboards.
  12. // Change spiSpeed to SPI_FULL_SPEED for better performance
  13. // Use SPI_QUARTER_SPEED for even slower SPI bus speed
  14. const uint8_t spiSpeed = SPI_HALF_SPEED;
  15. //------------------------------------------------------------------------------
  16. // File system object.
  17. SdFat sd;
  18. // Serial streams
  19. ArduinoOutStream cout(Serial);
  20. // input buffer for line
  21. char cinBuf[40];
  22. ArduinoInStream cin(Serial, cinBuf, sizeof(cinBuf));
  23. // SD card chip select
  24. int chipSelect;
  25. void cardOrSpeed() {
  26. cout << pstr("Try another SD card or reduce the SPI bus speed.\n");
  27. cout << pstr("Edit spiSpeed in this sketch to change it.\n");
  28. }
  29. void reformatMsg() {
  30. cout << pstr("Try reformatting the card. For best results use\n");
  31. cout << pstr("the SdFormatter sketch in SdFat/examples or download\n");
  32. cout << pstr("and use SDFormatter from www.sdcard.org/downloads.\n");
  33. }
  34. void setup() {
  35. Serial.begin(9600);
  36. while (!Serial) {} // Wait for Leonardo.
  37. cout << pstr("\nSPI pins:\n");
  38. cout << pstr("MISO: ") << int(MISO) << endl;
  39. cout << pstr("MOSI: ") << int(MOSI) << endl;
  40. cout << pstr("SCK: ") << int(SCK) << endl;
  41. cout << pstr("SS: ") << int(SS) << endl;
  42. if (DISABLE_CHIP_SELECT < 0) {
  43. cout << pstr(
  44. "\nBe sure to edit DISABLE_CHIP_SELECT if you have\n"
  45. "a second SPI device. For example, with the Ethernet\n"
  46. "shield, DISABLE_CHIP_SELECT should be set to 10\n"
  47. "to disable the Ethernet controller.\n");
  48. }
  49. cout << pstr(
  50. "\nSD chip select is the key hardware option.\n"
  51. "Common values are:\n"
  52. "Arduino Ethernet shield, pin 4\n"
  53. "Sparkfun SD shield, pin 8\n"
  54. "Adafruit SD shields and modules, pin 10\n");
  55. }
  56. bool firstTry = true;
  57. void loop() {
  58. // read any existing Serial data
  59. while (Serial.read() >= 0) {}
  60. if (!firstTry) cout << pstr("\nRestarting\n");
  61. firstTry = false;
  62. cout << pstr("\nEnter the chip select pin number: ");
  63. while (!Serial.available()) {}
  64. delay(400); // catch Due restart problem
  65. cin.readline();
  66. if (cin >> chipSelect) {
  67. cout << chipSelect << endl;
  68. } else {
  69. cout << pstr("\nInvalid pin number\n");
  70. return;
  71. }
  72. if (DISABLE_CHIP_SELECT < 0) {
  73. cout << pstr(
  74. "\nAssuming the SD is the only SPI device.\n"
  75. "Edit DISABLE_CHIP_SELECT to disable another device.\n");
  76. } else {
  77. cout << pstr("\nDisabling SPI device on pin ");
  78. cout << int(DISABLE_CHIP_SELECT) << endl;
  79. pinMode(DISABLE_CHIP_SELECT, OUTPUT);
  80. digitalWrite(DISABLE_CHIP_SELECT, HIGH);
  81. }
  82. if (!sd.begin(chipSelect, spiSpeed)) {
  83. if (sd.card()->errorCode()) {
  84. cout << pstr(
  85. "\nSD initialization failed.\n"
  86. "Do not reformat the card!\n"
  87. "Is the card correctly inserted?\n"
  88. "Is chipSelect set to the correct value?\n"
  89. "Does another SPI device need to be disabled?\n"
  90. "Is there a wiring/soldering problem?\n");
  91. cout << pstr("\nerrorCode: ") << hex << showbase;
  92. cout << int(sd.card()->errorCode());
  93. cout << pstr(", errorData: ") << int(sd.card()->errorData());
  94. cout << dec << noshowbase << endl;
  95. return;
  96. }
  97. cout << pstr("\nCard successfully initialized.\n");
  98. if (sd.vol()->fatType() == 0) {
  99. cout << pstr("Can't find a valid FAT16/FAT32 partition.\n");
  100. reformatMsg();
  101. return;
  102. }
  103. if (!sd.vwd()->isOpen()) {
  104. cout << pstr("Can't open root directory.\n");
  105. reformatMsg();
  106. return;
  107. }
  108. cout << pstr("Can't determine error type\n");
  109. return;
  110. }
  111. cout << pstr("\nCard successfully initialized.\n");
  112. cout << endl;
  113. uint32_t size = sd.card()->cardSize();
  114. if (size == 0) {
  115. cout << pstr("Can't determine the card size.\n");
  116. cardOrSpeed();
  117. return;
  118. }
  119. uint32_t sizeMB = 0.000512 * size + 0.5;
  120. cout << pstr("Card size: ") << sizeMB;
  121. cout << pstr(" MB (MB = 1,000,000 bytes)\n");
  122. cout << endl;
  123. cout << pstr("Volume is FAT") << int(sd.vol()->fatType());
  124. cout << pstr(", Cluster size (bytes): ") << 512L * sd.vol()->blocksPerCluster();
  125. cout << endl << endl;
  126. cout << pstr("Files found (name date time size):\n");
  127. sd.ls(LS_R | LS_DATE | LS_SIZE);
  128. if ((sizeMB > 1100 && sd.vol()->blocksPerCluster() < 64)
  129. || (sizeMB < 2200 && sd.vol()->fatType() == 32)) {
  130. cout << pstr("\nThis card should be reformatted for best performance.\n");
  131. cout << pstr("Use a cluster size of 32 KB for cards larger than 1 GB.\n");
  132. cout << pstr("Only cards larger than 2 GB should be formatted FAT32.\n");
  133. reformatMsg();
  134. return;
  135. }
  136. // read any existing Serial data
  137. while (Serial.read() >= 0) {}
  138. cout << pstr("\nSuccess! Type any character to restart.\n");
  139. while (Serial.read() < 0) {}
  140. }