Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

197 Zeilen
6.0KB

  1. /*
  2. * This sketch attempts to initialize an SD card and analyze its structure.
  3. */
  4. #include <SdFat.h>
  5. /*
  6. * SD chip select pin. Common values are:
  7. *
  8. * Arduino Ethernet shield, pin 4.
  9. * SparkFun SD shield, pin 8.
  10. * Adafruit SD shields and modules, pin 10.
  11. * Default SD chip select is the SPI SS pin.
  12. */
  13. const uint8_t SdChipSelect = SS;
  14. Sd2Card card;
  15. SdVolume vol;
  16. // serial output steam
  17. ArduinoOutStream cout(Serial);
  18. // global for card size
  19. uint32_t cardSize;
  20. // global for card erase size
  21. uint32_t eraseSize;
  22. //------------------------------------------------------------------------------
  23. // store error strings in flash
  24. #define sdErrorMsg(msg) sdErrorMsg_P(PSTR(msg));
  25. void sdErrorMsg_P(const char* str) {
  26. cout << pgm(str) << endl;
  27. if (card.errorCode()) {
  28. cout << pstr("SD errorCode: ");
  29. cout << hex << int(card.errorCode()) << endl;
  30. cout << pstr("SD errorData: ");
  31. cout << int(card.errorData()) << dec << endl;
  32. }
  33. }
  34. //------------------------------------------------------------------------------
  35. uint8_t cidDmp() {
  36. cid_t cid;
  37. if (!card.readCID(&cid)) {
  38. sdErrorMsg("readCID failed");
  39. return false;
  40. }
  41. cout << pstr("\nManufacturer ID: ");
  42. cout << hex << int(cid.mid) << dec << endl;
  43. cout << pstr("OEM ID: ") << cid.oid[0] << cid.oid[1] << endl;
  44. cout << pstr("Product: ");
  45. for (uint8_t i = 0; i < 5; i++) {
  46. cout << cid.pnm[i];
  47. }
  48. cout << pstr("\nVersion: ");
  49. cout << int(cid.prv_n) << '.' << int(cid.prv_m) << endl;
  50. cout << pstr("Serial number: ") << hex << cid.psn << dec << endl;
  51. cout << pstr("Manufacturing date: ");
  52. cout << int(cid.mdt_month) << '/';
  53. cout << (2000 + cid.mdt_year_low + 10 * cid.mdt_year_high) << endl;
  54. cout << endl;
  55. return true;
  56. }
  57. //------------------------------------------------------------------------------
  58. uint8_t csdDmp() {
  59. csd_t csd;
  60. uint8_t eraseSingleBlock;
  61. if (!card.readCSD(&csd)) {
  62. sdErrorMsg("readCSD failed");
  63. return false;
  64. }
  65. if (csd.v1.csd_ver == 0) {
  66. eraseSingleBlock = csd.v1.erase_blk_en;
  67. eraseSize = (csd.v1.sector_size_high << 1) | csd.v1.sector_size_low;
  68. } else if (csd.v2.csd_ver == 1) {
  69. eraseSingleBlock = csd.v2.erase_blk_en;
  70. eraseSize = (csd.v2.sector_size_high << 1) | csd.v2.sector_size_low;
  71. } else {
  72. cout << pstr("csd version error\n");
  73. return false;
  74. }
  75. eraseSize++;
  76. cout << pstr("cardSize: ") << 0.000512*cardSize;
  77. cout << pstr(" MB (MB = 1,000,000 bytes)\n");
  78. cout << pstr("flashEraseSize: ") << int(eraseSize) << pstr(" blocks\n");
  79. cout << pstr("eraseSingleBlock: ");
  80. if (eraseSingleBlock) {
  81. cout << pstr("true\n");
  82. } else {
  83. cout << pstr("false\n");
  84. }
  85. return true;
  86. }
  87. //------------------------------------------------------------------------------
  88. // print partition table
  89. uint8_t partDmp() {
  90. cache_t *p = vol.cacheClear();
  91. if (!p) {
  92. sdErrorMsg("cacheClear failed");
  93. return false;
  94. }
  95. if (!card.readBlock(0, p->data)) {
  96. sdErrorMsg("read MBR failed");
  97. return false;
  98. }
  99. cout << pstr("\nSD Partition Table\n");
  100. cout << pstr("part,boot,type,start,length\n");
  101. for (uint8_t ip = 1; ip < 5; ip++) {
  102. part_t *pt = &p->mbr.part[ip - 1];
  103. cout << int(ip) << ',' << hex << int(pt->boot) << ',' << int(pt->type);
  104. cout << dec << ',' << pt->firstSector <<',' << pt->totalSectors << endl;
  105. }
  106. return true;
  107. }
  108. //------------------------------------------------------------------------------
  109. void volDmp() {
  110. cout << pstr("\nVolume is FAT") << int(vol.fatType()) << endl;
  111. cout << pstr("blocksPerCluster: ") << int(vol.blocksPerCluster()) << endl;
  112. cout << pstr("clusterCount: ") << vol.clusterCount() << endl;
  113. uint32_t volFree = vol.freeClusterCount();
  114. cout << pstr("freeClusters: ") << volFree << endl;
  115. float fs = 0.000512*volFree*vol.blocksPerCluster();
  116. cout << pstr("freeSpace: ") << fs << pstr(" MB (MB = 1,000,000 bytes)\n");
  117. cout << pstr("fatStartBlock: ") << vol.fatStartBlock() << endl;
  118. cout << pstr("fatCount: ") << int(vol.fatCount()) << endl;
  119. cout << pstr("blocksPerFat: ") << vol.blocksPerFat() << endl;
  120. cout << pstr("rootDirStart: ") << vol.rootDirStart() << endl;
  121. cout << pstr("dataStartBlock: ") << vol.dataStartBlock() << endl;
  122. if (vol.dataStartBlock() % eraseSize) {
  123. cout << pstr("Data area is not aligned on flash erase boundaries!\n");
  124. cout << pstr("Download and use formatter from www.sdcard.org/consumer!\n");
  125. }
  126. }
  127. //------------------------------------------------------------------------------
  128. void setup() {
  129. Serial.begin(9600);
  130. while(!Serial) {} // wait for Leonardo
  131. // use uppercase in hex and use 0X base prefix
  132. cout << uppercase << showbase << endl;
  133. // pstr stores strings in flash to save RAM
  134. cout << pstr("SdFat version: ") << SD_FAT_VERSION << endl;
  135. }
  136. //------------------------------------------------------------------------------
  137. void loop() {
  138. // read any existing Serial data
  139. while (Serial.read() >= 0) {}
  140. // pstr stores strings in flash to save RAM
  141. cout << pstr("\ntype any character to start\n");
  142. while (Serial.read() <= 0) {}
  143. delay(400); // catch Due reset problem
  144. uint32_t t = millis();
  145. // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  146. // breadboards. use SPI_FULL_SPEED for better performance.
  147. if (!card.init(SPI_HALF_SPEED, SdChipSelect)) {
  148. sdErrorMsg("\ncard.init failed");
  149. return;
  150. }
  151. t = millis() - t;
  152. cardSize = card.cardSize();
  153. if (cardSize == 0) {
  154. sdErrorMsg("cardSize failed");
  155. return;
  156. }
  157. cout << pstr("\ninit time: ") << t << " ms" << endl;
  158. cout << pstr("\nCard type: ");
  159. switch (card.type()) {
  160. case SD_CARD_TYPE_SD1:
  161. cout << pstr("SD1\n");
  162. break;
  163. case SD_CARD_TYPE_SD2:
  164. cout << pstr("SD2\n");
  165. break;
  166. case SD_CARD_TYPE_SDHC:
  167. if (cardSize < 70000000) {
  168. cout << pstr("SDHC\n");
  169. } else {
  170. cout << pstr("SDXC\n");
  171. }
  172. break;
  173. default:
  174. cout << pstr("Unknown\n");
  175. }
  176. if (!cidDmp()) return;
  177. if (!csdDmp()) return;
  178. if (!partDmp()) return;
  179. if (!vol.init(&card)) {
  180. sdErrorMsg("\nvol.init failed");
  181. return;
  182. }
  183. volDmp();
  184. }