Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

178 linhas
5.3KB

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