No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

166 líneas
5.0KB

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