No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

SdInfo.ino 7.1KB

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