You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 line
4.9KB

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