You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
7.0KB

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