您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

228 行
6.8KB

  1. /*
  2. * This program attempts to initialize an SD card and analyze its structure.
  3. */
  4. #include <SPI.h>
  5. #include "SdFat.h"
  6. /*
  7. * SD chip select pin. Common values are:
  8. *
  9. * Arduino Ethernet shield, pin 4.
  10. * SparkFun SD shield, pin 8.
  11. * Adafruit SD shields and modules, pin 10.
  12. * Default SD chip select is the SPI SS pin.
  13. */
  14. const uint8_t SD_CHIP_SELECT = SS;
  15. /*
  16. * Set DISABLE_CHIP_SELECT to disable a second SPI device.
  17. * For example, with the Ethernet shield, set DISABLE_CHIP_SELECT
  18. * to 10 to disable the Ethernet controller.
  19. */
  20. const int8_t DISABLE_CHIP_SELECT = -1;
  21. SdFat sd;
  22. // serial output steam
  23. ArduinoOutStream cout(Serial);
  24. // global for card size
  25. uint32_t cardSize;
  26. // global for card erase size
  27. uint32_t eraseSize;
  28. //------------------------------------------------------------------------------
  29. // store error strings in flash
  30. #define sdErrorMsg(msg) sd.errorPrint(F(msg));
  31. //------------------------------------------------------------------------------
  32. uint8_t cidDmp() {
  33. cid_t cid;
  34. if (!sd.card()->readCID(&cid)) {
  35. sdErrorMsg("readCID failed");
  36. return false;
  37. }
  38. cout << F("\nManufacturer ID: ");
  39. cout << hex << int(cid.mid) << dec << endl;
  40. cout << F("OEM ID: ") << cid.oid[0] << cid.oid[1] << endl;
  41. cout << F("Product: ");
  42. for (uint8_t i = 0; i < 5; i++) {
  43. cout << cid.pnm[i];
  44. }
  45. cout << F("\nVersion: ");
  46. cout << int(cid.prv_n) << '.' << int(cid.prv_m) << endl;
  47. cout << F("Serial number: ") << hex << cid.psn << dec << endl;
  48. cout << F("Manufacturing date: ");
  49. cout << int(cid.mdt_month) << '/';
  50. cout << (2000 + cid.mdt_year_low + 10 * cid.mdt_year_high) << endl;
  51. cout << endl;
  52. return true;
  53. }
  54. //------------------------------------------------------------------------------
  55. uint8_t csdDmp() {
  56. csd_t csd;
  57. uint8_t eraseSingleBlock;
  58. if (!sd.card()->readCSD(&csd)) {
  59. sdErrorMsg("readCSD failed");
  60. return false;
  61. }
  62. if (csd.v1.csd_ver == 0) {
  63. eraseSingleBlock = csd.v1.erase_blk_en;
  64. eraseSize = (csd.v1.sector_size_high << 1) | csd.v1.sector_size_low;
  65. } else if (csd.v2.csd_ver == 1) {
  66. eraseSingleBlock = csd.v2.erase_blk_en;
  67. eraseSize = (csd.v2.sector_size_high << 1) | csd.v2.sector_size_low;
  68. } else {
  69. cout << F("csd version error\n");
  70. return false;
  71. }
  72. eraseSize++;
  73. cout << F("cardSize: ") << 0.000512*cardSize;
  74. cout << F(" MB (MB = 1,000,000 bytes)\n");
  75. cout << F("flashEraseSize: ") << int(eraseSize) << F(" blocks\n");
  76. cout << F("eraseSingleBlock: ");
  77. if (eraseSingleBlock) {
  78. cout << F("true\n");
  79. } else {
  80. cout << F("false\n");
  81. }
  82. return true;
  83. }
  84. //------------------------------------------------------------------------------
  85. // print partition table
  86. uint8_t partDmp() {
  87. mbr_t mbr;
  88. if (!sd.card()->readBlock(0, (uint8_t*)&mbr)) {
  89. sdErrorMsg("read MBR failed");
  90. return false;
  91. }
  92. for (uint8_t ip = 1; ip < 5; ip++) {
  93. part_t *pt = &mbr.part[ip - 1];
  94. if ((pt->boot & 0X7F) != 0 || pt->firstSector > cardSize) {
  95. cout << F("\nNo MBR. Assuming Super Floppy format.\n");
  96. return true;
  97. }
  98. }
  99. cout << F("\nSD Partition Table\n");
  100. cout << F("part,boot,type,start,length\n");
  101. for (uint8_t ip = 1; ip < 5; ip++) {
  102. part_t *pt = &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 << F("\nVolume is FAT") << int(sd.vol()->fatType()) << endl;
  111. cout << F("blocksPerCluster: ") << int(sd.vol()->blocksPerCluster()) << endl;
  112. cout << F("clusterCount: ") << sd.vol()->clusterCount() << endl;
  113. cout << F("freeClusters: ");
  114. uint32_t volFree = sd.vol()->freeClusterCount();
  115. cout << volFree << endl;
  116. float fs = 0.000512*volFree*sd.vol()->blocksPerCluster();
  117. cout << F("freeSpace: ") << fs << F(" MB (MB = 1,000,000 bytes)\n");
  118. cout << F("fatStartBlock: ") << sd.vol()->fatStartBlock() << endl;
  119. cout << F("fatCount: ") << int(sd.vol()->fatCount()) << endl;
  120. cout << F("blocksPerFat: ") << sd.vol()->blocksPerFat() << endl;
  121. cout << F("rootDirStart: ") << sd.vol()->rootDirStart() << endl;
  122. cout << F("dataStartBlock: ") << sd.vol()->dataStartBlock() << endl;
  123. if (sd.vol()->dataStartBlock() % eraseSize) {
  124. cout << F("Data area is not aligned on flash erase boundaries!\n");
  125. cout << F("Download and use formatter from www.sdcard.org!\n");
  126. }
  127. }
  128. //------------------------------------------------------------------------------
  129. void setup() {
  130. Serial.begin(9600);
  131. // Wait for USB Serial
  132. while (!Serial) {
  133. SysCall::yield();
  134. }
  135. // use uppercase in hex and use 0X base prefix
  136. cout << uppercase << showbase << endl;
  137. // F stores strings in flash to save RAM
  138. cout << F("SdFat version: ") << SD_FAT_VERSION << endl;
  139. if (DISABLE_CHIP_SELECT < 0) {
  140. cout << F(
  141. "\nAssuming the SD is the only SPI device.\n"
  142. "Edit DISABLE_CHIP_SELECT to disable another device.\n");
  143. } else {
  144. cout << F("\nDisabling SPI device on pin ");
  145. cout << int(DISABLE_CHIP_SELECT) << endl;
  146. pinMode(DISABLE_CHIP_SELECT, OUTPUT);
  147. digitalWrite(DISABLE_CHIP_SELECT, HIGH);
  148. }
  149. cout << F("\nAssuming the SD chip select pin is: ") <<int(SD_CHIP_SELECT);
  150. cout << F("\nEdit SD_CHIP_SELECT to change the SD chip select pin.\n");
  151. }
  152. //------------------------------------------------------------------------------
  153. void loop() {
  154. // Read any existing Serial data.
  155. do {
  156. delay(10);
  157. } while (Serial.available() && Serial.read() >= 0);
  158. // F stores strings in flash to save RAM
  159. cout << F("\ntype any character to start\n");
  160. while (!Serial.available()) {
  161. SysCall::yield();
  162. }
  163. uint32_t t = millis();
  164. // Initialize at the highest speed supported by the board that is
  165. // not over 50 MHz. Try a lower speed if SPI errors occur.
  166. if (!sd.cardBegin(SD_CHIP_SELECT, SD_SCK_MHZ(50))) {
  167. sdErrorMsg("\ncardBegin failed");
  168. return;
  169. }
  170. t = millis() - t;
  171. cardSize = sd.card()->cardSize();
  172. if (cardSize == 0) {
  173. sdErrorMsg("cardSize failed");
  174. return;
  175. }
  176. cout << F("\ninit time: ") << t << " ms" << endl;
  177. cout << F("\nCard type: ");
  178. switch (sd.card()->type()) {
  179. case SD_CARD_TYPE_SD1:
  180. cout << F("SD1\n");
  181. break;
  182. case SD_CARD_TYPE_SD2:
  183. cout << F("SD2\n");
  184. break;
  185. case SD_CARD_TYPE_SDHC:
  186. if (cardSize < 70000000) {
  187. cout << F("SDHC\n");
  188. } else {
  189. cout << F("SDXC\n");
  190. }
  191. break;
  192. default:
  193. cout << F("Unknown\n");
  194. }
  195. if (!cidDmp()) {
  196. return;
  197. }
  198. if (!csdDmp()) {
  199. return;
  200. }
  201. uint32_t ocr;
  202. if (!sd.card()->readOCR(&ocr)) {
  203. sdErrorMsg("\nreadOCR failed");
  204. return;
  205. }
  206. cout << F("OCR: ") << hex << ocr << dec << endl;
  207. if (!partDmp()) {
  208. return;
  209. }
  210. if (!sd.fsBegin()) {
  211. sdErrorMsg("\nFile System initialization failed.\n");
  212. return;
  213. }
  214. volDmp();
  215. }