Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

181 line
5.4KB

  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. #ifdef SDCARD_SS_PIN
  63. cout << F("SDCARD_SS_PIN: ") << int(SDCARD_SS_PIN) << endl;
  64. #endif // SDCARD_SS_PIN
  65. if (DISABLE_CHIP_SELECT < 0) {
  66. cout << F(
  67. "\nBe sure to edit DISABLE_CHIP_SELECT if you have\n"
  68. "a second SPI device. For example, with the Ethernet\n"
  69. "shield, DISABLE_CHIP_SELECT should be set to 10\n"
  70. "to disable the Ethernet controller.\n");
  71. }
  72. cout << F(
  73. "\nSD chip select is the key hardware option.\n"
  74. "Common values are:\n"
  75. "Arduino Ethernet shield, pin 4\n"
  76. "Sparkfun SD shield, pin 8\n"
  77. "Adafruit SD shields and modules, pin 10\n");
  78. }
  79. bool firstTry = true;
  80. void loop() {
  81. // Read any existing Serial data.
  82. do {
  83. delay(10);
  84. } while (Serial.available() && Serial.read() >= 0);
  85. if (!firstTry) {
  86. cout << F("\nRestarting\n");
  87. }
  88. firstTry = false;
  89. cout << F("\nEnter the chip select pin number: ");
  90. while (!Serial.available()) {
  91. SysCall::yield();
  92. }
  93. cin.readline();
  94. if (cin >> chipSelect) {
  95. cout << chipSelect << endl;
  96. } else {
  97. cout << F("\nInvalid pin number\n");
  98. return;
  99. }
  100. if (DISABLE_CHIP_SELECT < 0) {
  101. cout << F(
  102. "\nAssuming the SD is the only SPI device.\n"
  103. "Edit DISABLE_CHIP_SELECT to disable another device.\n");
  104. } else {
  105. cout << F("\nDisabling SPI device on pin ");
  106. cout << int(DISABLE_CHIP_SELECT) << endl;
  107. pinMode(DISABLE_CHIP_SELECT, OUTPUT);
  108. digitalWrite(DISABLE_CHIP_SELECT, HIGH);
  109. }
  110. if (!sd.begin(chipSelect, SPI_SPEED)) {
  111. if (sd.card()->errorCode()) {
  112. cout << F(
  113. "\nSD initialization failed.\n"
  114. "Do not reformat the card!\n"
  115. "Is the card correctly inserted?\n"
  116. "Is chipSelect set to the correct value?\n"
  117. "Does another SPI device need to be disabled?\n"
  118. "Is there a wiring/soldering problem?\n");
  119. cout << F("\nerrorCode: ") << hex << showbase;
  120. cout << int(sd.card()->errorCode());
  121. cout << F(", errorData: ") << int(sd.card()->errorData());
  122. cout << dec << noshowbase << endl;
  123. return;
  124. }
  125. cout << F("\nCard successfully initialized.\n");
  126. if (sd.vol()->fatType() == 0) {
  127. cout << F("Can't find a valid FAT16/FAT32 partition.\n");
  128. reformatMsg();
  129. return;
  130. }
  131. cout << F("Can't determine error type\n");
  132. return;
  133. }
  134. cout << F("\nCard successfully initialized.\n");
  135. cout << endl;
  136. uint32_t size = sd.card()->sectorCount();
  137. if (size == 0) {
  138. cout << F("Can't determine the card size.\n");
  139. cardOrSpeed();
  140. return;
  141. }
  142. uint32_t sizeMB = 0.000512 * size + 0.5;
  143. cout << F("Card size: ") << sizeMB;
  144. cout << F(" MB (MB = 1,000,000 bytes)\n");
  145. cout << endl;
  146. cout << F("Volume is FAT") << int(sd.vol()->fatType());
  147. cout << F(", Cluster size (bytes): ") << sd.vol()->bytesPerCluster();
  148. cout << endl << endl;
  149. cout << F("Files found (date time size name):\n");
  150. sd.ls(LS_R | LS_DATE | LS_SIZE);
  151. if ((sizeMB > 1100 && sd.vol()->sectorsPerCluster() < 64)
  152. || (sizeMB < 2200 && sd.vol()->fatType() == 32)) {
  153. cout << F("\nThis card should be reformatted for best performance.\n");
  154. cout << F("Use a cluster size of 32 KB for cards larger than 1 GB.\n");
  155. cout << F("Only cards larger than 2 GB should be formatted FAT32.\n");
  156. reformatMsg();
  157. return;
  158. }
  159. // Read any extra Serial data.
  160. do {
  161. delay(10);
  162. } while (Serial.available() && Serial.read() >= 0);
  163. cout << F("\nSuccess! Type any character to restart.\n");
  164. while (!Serial.available()) {
  165. SysCall::yield();
  166. }
  167. }