Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

156 lines
4.8KB

  1. // Quick hardware test.
  2. //
  3. #include <SdFat.h>
  4. //
  5. // Set DISABLE_CHIP_SELECT to disable a second SPI device.
  6. // For example, with the Ethernet shield, set DISABLE_CHIP_SELECT
  7. // to 10 to disable the Ethernet controller.
  8. const int8_t DISABLE_CHIP_SELECT = -1;
  9. //
  10. // Test with reduced SPI speed for breadboards.
  11. // Change spiSpeed to SPI_FULL_SPEED for better performance
  12. // Use SPI_QUARTER_SPEED for even slower SPI bus speed
  13. const uint8_t spiSpeed = SPI_HALF_SPEED;
  14. //------------------------------------------------------------------------------
  15. // Normally the SdFat class is used in applications in place
  16. // of Sd2Card, SdVolume, and SdFile for root.
  17. // These internal classes are used here to diagnose problems.
  18. Sd2Card card;
  19. SdVolume volume;
  20. SdFile root;
  21. // Serial streams
  22. ArduinoOutStream cout(Serial);
  23. // input buffer for line
  24. char cinBuf[40];
  25. ArduinoInStream cin(Serial, cinBuf, sizeof(cinBuf));
  26. // SD card chip select
  27. int chipSelect;
  28. void cardOrSpeed() {
  29. cout << pstr("Try another SD card or reduce the SPI bus speed.\n");
  30. cout << pstr("Edit spiSpeed in this sketch to change it.\n");
  31. }
  32. void reformatMsg() {
  33. cout << pstr("Try reformatting the card. For best results use\n");
  34. cout << pstr("the SdFormatter sketch in SdFat/examples or download\n");
  35. cout << pstr("and use SDFormatter from www.sdcard.org/consumer.\n");
  36. }
  37. void setup() {
  38. Serial.begin(9600);
  39. while (!Serial) {} // Wait for Leonardo.
  40. delay(1000); // Delay for Due.
  41. cout << pstr("\nSPI pins:\n");
  42. cout << pstr("MOSI: ") << int(MOSI) << endl;
  43. cout << pstr("MISO: ") << int(MISO) << endl;
  44. cout << pstr("SCK: ") << int(SCK) << endl;
  45. if (DISABLE_CHIP_SELECT < 0) {
  46. cout << pstr(
  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 << pstr(
  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. while (Serial.read() >= 0) {}
  63. if (!firstTry) cout << pstr("\nRestarting\n");
  64. firstTry = false;
  65. cout << pstr("\nEnter the chip select pin number: ");
  66. cin.readline();
  67. if (cin >> chipSelect) {
  68. cout << chipSelect << endl;
  69. } else {
  70. cout << pstr("\nInvalid pin number\n");
  71. return;
  72. }
  73. if (DISABLE_CHIP_SELECT < 0) {
  74. cout << pstr(
  75. "\nAssuming the SD is the only SPI device.\n"
  76. "Edit DISABLE_CHIP_SELECT to disable another device.\n");
  77. } else {
  78. cout << pstr("\nDisabling SPI device on pin ");
  79. cout << int(DISABLE_CHIP_SELECT) << endl;
  80. pinMode(DISABLE_CHIP_SELECT, OUTPUT);
  81. digitalWrite(DISABLE_CHIP_SELECT, HIGH);
  82. }
  83. if (!card.init(spiSpeed, chipSelect)) {
  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("errorCode: ") << hex << showbase << int(card.errorCode());
  92. cout << pstr(", errorData: ") << int(card.errorData());
  93. cout << dec << noshowbase << endl;
  94. return;
  95. }
  96. cout << pstr("\nCard successfully initialized.\n");
  97. cout << endl;
  98. uint32_t size = card.cardSize();
  99. if (size == 0) {
  100. cout << pstr("Can't determine the card size.\n");
  101. cardOrSpeed();
  102. return;
  103. }
  104. uint32_t sizeMB = 0.000512 * size + 0.5;
  105. cout << pstr("Card size: ") << sizeMB;
  106. cout << pstr(" MB (MB = 1,000,000 bytes)\n");
  107. cout << endl;
  108. if (!volume.init(&card)) {
  109. if (card.errorCode()) {
  110. cout << pstr("Can't read the card.\n");
  111. cardOrSpeed();
  112. } else {
  113. cout << pstr("Can't find a valid FAT16/FAT32 partition.\n");
  114. reformatMsg();
  115. }
  116. return;
  117. }
  118. cout << pstr("Volume is FAT") << int(volume.fatType());
  119. cout << pstr(", Cluster size (bytes): ") << 512L * volume.blocksPerCluster();
  120. cout << endl << endl;
  121. root.close();
  122. if (!root.openRoot(&volume)) {
  123. cout << pstr("Can't open root directory.\n");
  124. reformatMsg();
  125. return;
  126. }
  127. cout << pstr("Files found (name date time size):\n");
  128. root.ls(LS_R | LS_DATE | LS_SIZE);
  129. if ((sizeMB > 1100 && volume.blocksPerCluster() < 64)
  130. || (sizeMB < 2200 && volume.fatType() == 32)) {
  131. cout << pstr("\nThis card should be reformatted for best performance.\n");
  132. cout << pstr("Use a cluster size of 32 KB for cards larger than 1 GB.\n");
  133. cout << pstr("Only cards larger than 2 GB should be formatted FAT32.\n");
  134. reformatMsg();
  135. return;
  136. }
  137. // read any existing Serial data
  138. while (Serial.read() >= 0) {}
  139. cout << pstr("\nSuccess! Type any character to restart.\n");
  140. while (Serial.read() < 0) {}
  141. }