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.

SdInfo.ino 7.2KB

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