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.1KB

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