SdInfo.ino 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * This program attempts to initialize an SD card and analyze its structure.
  3. */
  4. #include "SdFat.h"
  5. #include "sdios.h"
  6. /*
  7. Set DISABLE_CS_PIN to disable a second SPI device.
  8. For example, with the Ethernet shield, set DISABLE_CS_PIN
  9. to 10 to disable the Ethernet controller.
  10. */
  11. const int8_t DISABLE_CS_PIN = -1;
  12. /*
  13. Change the value of SD_CS_PIN if you are using SPI
  14. and your hardware does not use the default value, SS.
  15. Common values are:
  16. Arduino Ethernet shield: pin 4
  17. Sparkfun SD shield: pin 8
  18. Adafruit SD shields and modules: pin 10
  19. */
  20. // SDCARD_SS_PIN is defined for the built-in SD on some boards.
  21. #ifndef SDCARD_SS_PIN
  22. const uint8_t SD_CS_PIN = SS;
  23. #else // SDCARD_SS_PIN
  24. const uint8_t SD_CS_PIN = SDCARD_SS_PIN;
  25. #endif // SDCARD_SS_PIN
  26. // Try to select the best SD card configuration.
  27. #if HAS_SDIO_CLASS
  28. #define SD_CONFIG SdioConfig(FIFO_SDIO)
  29. #elif ENABLE_DEDICATED_SPI
  30. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SD_SCK_MHZ(16))
  31. #else // HAS_SDIO_CLASS
  32. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI, SD_SCK_MHZ(16))
  33. #endif // HAS_SDIO_CLASS
  34. //------------------------------------------------------------------------------
  35. SdFs sd;
  36. cid_t cid;
  37. csd_t csd;
  38. scr_t scr;
  39. uint8_t cmd6Data[64];
  40. uint32_t eraseSize;
  41. uint32_t ocr;
  42. static ArduinoOutStream cout(Serial);
  43. //------------------------------------------------------------------------------
  44. void cidDmp() {
  45. cout << F("\nManufacturer ID: ");
  46. cout << uppercase << showbase << hex << int(cid.mid) << dec << endl;
  47. cout << F("OEM ID: ") << cid.oid[0] << cid.oid[1] << endl;
  48. cout << F("Product: ");
  49. for (uint8_t i = 0; i < 5; i++) {
  50. cout << cid.pnm[i];
  51. }
  52. cout << F("\nRevision: ") << cid.prvN() << '.' << cid.prvM() << endl;
  53. cout << F("Serial number: ") << hex << cid.psn() << dec << endl;
  54. cout << F("Manufacturing date: ");
  55. cout << cid.mdtMonth() << '/' << cid.mdtYear() << endl;
  56. cout << endl;
  57. }
  58. //------------------------------------------------------------------------------
  59. void clearSerialInput() {
  60. uint32_t m = micros();
  61. do {
  62. if (Serial.read() >= 0) {
  63. m = micros();
  64. }
  65. } while (micros() - m < 10000);
  66. }
  67. //------------------------------------------------------------------------------
  68. void csdDmp() {
  69. eraseSize = csd.eraseSize();
  70. cout << F("cardSize: ") << 0.000512 * csd.capacity();
  71. cout << F(" MB (MB = 1,000,000 bytes)\n");
  72. cout << F("flashEraseSize: ") << int(eraseSize) << F(" blocks\n");
  73. cout << F("eraseSingleBlock: ");
  74. if (csd.eraseSingleBlock()) {
  75. cout << F("true\n");
  76. } else {
  77. cout << F("false\n");
  78. }
  79. cout << F("dataAfterErase: ");
  80. if (scr.dataAfterErase()) {
  81. cout << F("ones\n");
  82. } else {
  83. cout << F("zeros\n");
  84. }
  85. }
  86. //------------------------------------------------------------------------------
  87. void errorPrint() {
  88. if (sd.sdErrorCode()) {
  89. cout << F("SD errorCode: ") << hex << showbase;
  90. printSdErrorSymbol(&Serial, sd.sdErrorCode());
  91. cout << F(" = ") << int(sd.sdErrorCode()) << endl;
  92. cout << F("SD errorData = ") << int(sd.sdErrorData()) << dec << endl;
  93. }
  94. }
  95. //------------------------------------------------------------------------------
  96. bool mbrDmp() {
  97. MbrSector_t mbr;
  98. bool valid = true;
  99. if (!sd.card()->readSector(0, (uint8_t *)&mbr)) {
  100. cout << F("\nread MBR failed.\n");
  101. errorPrint();
  102. return false;
  103. }
  104. cout << F("\nSD Partition Table\n");
  105. cout << F("part,boot,bgnCHS[3],type,endCHS[3],start,length\n");
  106. for (uint8_t ip = 1; ip < 5; ip++) {
  107. MbrPart_t *pt = &mbr.part[ip - 1];
  108. if ((pt->boot != 0 && pt->boot != 0X80) ||
  109. getLe32(pt->relativeSectors) > csd.capacity()) {
  110. valid = false;
  111. }
  112. cout << int(ip) << ',' << uppercase << showbase << hex;
  113. cout << int(pt->boot) << ',';
  114. for (int i = 0; i < 3; i++) {
  115. cout << int(pt->beginCHS[i]) << ',';
  116. }
  117. cout << int(pt->type) << ',';
  118. for (int i = 0; i < 3; i++) {
  119. cout << int(pt->endCHS[i]) << ',';
  120. }
  121. cout << dec << getLe32(pt->relativeSectors) << ',';
  122. cout << getLe32(pt->totalSectors) << endl;
  123. }
  124. if (!valid) {
  125. cout << F("\nMBR not valid, assuming Super Floppy format.\n");
  126. }
  127. return true;
  128. }
  129. //------------------------------------------------------------------------------
  130. void dmpVol() {
  131. cout << F("\nScanning FAT, please wait.\n");
  132. int32_t freeClusterCount = sd.freeClusterCount();
  133. if (sd.fatType() <= 32) {
  134. cout << F("\nVolume is FAT") << int(sd.fatType()) << endl;
  135. } else {
  136. cout << F("\nVolume is exFAT\n");
  137. }
  138. cout << F("sectorsPerCluster: ") << sd.sectorsPerCluster() << endl;
  139. cout << F("fatStartSector: ") << sd.fatStartSector() << endl;
  140. cout << F("dataStartSector: ") << sd.dataStartSector() << endl;
  141. cout << F("clusterCount: ") << sd.clusterCount() << endl;
  142. cout << F("freeClusterCount: ");
  143. if (freeClusterCount >= 0) {
  144. cout << freeClusterCount << endl;
  145. } else {
  146. cout << F("failed\n");
  147. errorPrint();
  148. }
  149. }
  150. //------------------------------------------------------------------------------
  151. void printCardType() {
  152. cout << F("\nCard type: ");
  153. switch (sd.card()->type()) {
  154. case SD_CARD_TYPE_SD1:
  155. cout << F("SD1\n");
  156. break;
  157. case SD_CARD_TYPE_SD2:
  158. cout << F("SD2\n");
  159. break;
  160. case SD_CARD_TYPE_SDHC:
  161. if (csd.capacity() < 70000000) {
  162. cout << F("SDHC\n");
  163. } else {
  164. cout << F("SDXC\n");
  165. }
  166. break;
  167. default:
  168. cout << F("Unknown\n");
  169. }
  170. }
  171. //------------------------------------------------------------------------------
  172. void printConfig(SdSpiConfig config) {
  173. if (DISABLE_CS_PIN < 0) {
  174. cout << F(
  175. "\nAssuming the SD is the only SPI device.\n"
  176. "Edit DISABLE_CS_PIN to disable an SPI device.\n");
  177. } else {
  178. cout << F("\nDisabling SPI device on pin ");
  179. cout << int(DISABLE_CS_PIN) << endl;
  180. pinMode(DISABLE_CS_PIN, OUTPUT);
  181. digitalWrite(DISABLE_CS_PIN, HIGH);
  182. }
  183. cout << F("\nAssuming the SD chip select pin is: ") << int(config.csPin);
  184. cout << F("\nEdit SD_CS_PIN to change the SD chip select pin.\n");
  185. }
  186. //------------------------------------------------------------------------------
  187. void printConfig(SdioConfig config) {
  188. (void)config;
  189. cout << F("Assuming an SDIO interface.\n");
  190. }
  191. //-----------------------------------------------------------------------------
  192. void setup() {
  193. Serial.begin(9600);
  194. // Wait for USB Serial
  195. while (!Serial) {
  196. yield();
  197. }
  198. cout << F("SdFat version: ") << SD_FAT_VERSION_STR << endl;
  199. printConfig(SD_CONFIG);
  200. }
  201. //------------------------------------------------------------------------------
  202. void loop() {
  203. // Read any existing Serial data.
  204. clearSerialInput();
  205. // F stores strings in flash to save RAM
  206. cout << F("\ntype any character to start\n");
  207. while (!Serial.available()) {
  208. yield();
  209. }
  210. uint32_t t = millis();
  211. if (!sd.cardBegin(SD_CONFIG)) {
  212. cout << F(
  213. "\nSD initialization failed.\n"
  214. "Do not reformat the card!\n"
  215. "Is the card correctly inserted?\n"
  216. "Is there a wiring/soldering problem?\n");
  217. if (isSpi(SD_CONFIG)) {
  218. cout << F(
  219. "Is SD_CS_PIN set to the correct value?\n"
  220. "Does another SPI device need to be disabled?\n");
  221. }
  222. errorPrint();
  223. return;
  224. }
  225. t = millis() - t;
  226. cout << F("init time: ") << dec << t << " ms" << endl;
  227. if (!sd.card()->readCID(&cid) || !sd.card()->readCSD(&csd) ||
  228. !sd.card()->readOCR(&ocr) || !sd.card()->readSCR(&scr)) {
  229. cout << F("readInfo failed\n");
  230. errorPrint();
  231. return;
  232. }
  233. printCardType();
  234. cout << F("sdSpecVer: ") << 0.01 * scr.sdSpecVer() << endl;
  235. cout << F("HighSpeedMode: ");
  236. if (scr.sdSpecVer() && sd.card()->cardCMD6(0X00FFFFFF, cmd6Data) &&
  237. (2 & cmd6Data[13])) {
  238. cout << F("true\n");
  239. } else {
  240. cout << F("false\n");
  241. }
  242. cidDmp();
  243. csdDmp();
  244. cout << F("\nOCR: ") << uppercase << showbase;
  245. cout << hex << ocr << dec << endl;
  246. if (!mbrDmp()) {
  247. return;
  248. }
  249. if (!sd.volumeBegin()) {
  250. cout << F("\nvolumeBegin failed. Is the card formatted?\n");
  251. errorPrint();
  252. return;
  253. }
  254. dmpVol();
  255. }