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.

SdFormatter.ino 7.3KB

hace 5 años
hace 5 años
hace 5 años
hace 5 años
hace 5 años
hace 5 años
hace 5 años
hace 5 años
hace 5 años
hace 5 años
hace 5 años
hace 5 años
hace 5 años
hace 5 años
hace 5 años
hace 5 años
hace 5 años
hace 5 años
hace 5 años
hace 5 años
hace 5 años
hace 5 años
hace 5 años
hace 5 años
hace 5 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * This program will format SD/SDHC/SDXC cards.
  3. * Warning all data will be deleted!
  4. *
  5. * This program attempts to match the format
  6. * generated by SDFormatter available here:
  7. *
  8. * http://www.sdcard.org/consumers/formatter/
  9. *
  10. * For very small cards this program uses FAT16
  11. * and the above SDFormatter uses FAT12.
  12. */
  13. #include "SdFat.h"
  14. #include "sdios.h"
  15. /*
  16. Set DISABLE_CS_PIN to disable a second SPI device.
  17. For example, with the Ethernet shield, set DISABLE_CS_PIN
  18. to 10 to disable the Ethernet controller.
  19. */
  20. const int8_t DISABLE_CS_PIN = -1;
  21. /*
  22. Change the value of SD_CS_PIN if you are using SPI
  23. and your hardware does not use the default value, SS.
  24. Common values are:
  25. Arduino Ethernet shield: pin 4
  26. Sparkfun SD shield: pin 8
  27. Adafruit SD shields and modules: pin 10
  28. */
  29. // SDCARD_SS_PIN is defined for the built-in SD on some boards.
  30. #ifndef SDCARD_SS_PIN
  31. const uint8_t SD_CS_PIN = SS;
  32. #else // SDCARD_SS_PIN
  33. // Assume built-in SD is used.
  34. const uint8_t SD_CS_PIN = SDCARD_SS_PIN;
  35. #endif // SDCARD_SS_PIN
  36. // Try to select the best SD card configuration.
  37. #if HAS_SDIO_CLASS
  38. #define SD_CONFIG SdioConfig(FIFO_SDIO)
  39. #elif ENABLE_DEDICATED_SPI
  40. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI)
  41. #else // HAS_SDIO_CLASS
  42. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI)
  43. #endif // HAS_SDIO_CLASS
  44. //==============================================================================
  45. // Serial output stream
  46. ArduinoOutStream cout(Serial);
  47. //------------------------------------------------------------------------------
  48. uint32_t cardSectorCount = 0;
  49. uint8_t sectorBuffer[512];
  50. //------------------------------------------------------------------------------
  51. // SdCardFactory constructs and initializes the appropriate card.
  52. SdCardFactory cardFactory;
  53. // Pointer to generic SD card.
  54. SdCard* m_card = nullptr;
  55. //------------------------------------------------------------------------------
  56. #define sdError(msg) {cout << F("error: ") << F(msg) << endl; sdErrorHalt();}
  57. //------------------------------------------------------------------------------
  58. void sdErrorHalt() {
  59. if (!m_card) {
  60. cout << F("Invalid SD_CONFIG") << endl;
  61. } else if (m_card->errorCode()) {
  62. if (m_card->errorCode() == SD_CARD_ERROR_CMD0) {
  63. cout << F("No card, wrong chip select pin, or wiring error?") << endl;
  64. }
  65. cout << F("SD errorCode: ") << hex << showbase;
  66. printSdErrorSymbol(&Serial, m_card->errorCode());
  67. cout << F(" = ") << int(m_card->errorCode()) << endl;
  68. cout << F("SD errorData = ") << int(m_card->errorData()) << endl;
  69. }
  70. SysCall::halt();
  71. }
  72. //------------------------------------------------------------------------------
  73. // flash erase all data
  74. uint32_t const ERASE_SIZE = 262144L;
  75. void eraseCard() {
  76. cout << endl << F("Erasing\n");
  77. uint32_t firstBlock = 0;
  78. uint32_t lastBlock;
  79. uint16_t n = 0;
  80. do {
  81. lastBlock = firstBlock + ERASE_SIZE - 1;
  82. if (lastBlock >= cardSectorCount) {
  83. lastBlock = cardSectorCount - 1;
  84. }
  85. if (!m_card->erase(firstBlock, lastBlock)) {
  86. sdError("erase failed");
  87. }
  88. cout << '.';
  89. if ((n++)%64 == 63) {
  90. cout << endl;
  91. }
  92. firstBlock += ERASE_SIZE;
  93. } while (firstBlock < cardSectorCount);
  94. cout << endl;
  95. if (!m_card->readSector(0, sectorBuffer)) {
  96. sdError("readBlock");
  97. }
  98. cout << hex << showbase << setfill('0') << internal;
  99. cout << F("All data set to ") << setw(4) << int(sectorBuffer[0]) << endl;
  100. cout << dec << noshowbase << setfill(' ') << right;
  101. cout << F("Erase done\n");
  102. }
  103. //------------------------------------------------------------------------------
  104. void formatCard() {
  105. ExFatFormatter exFatFormatter;
  106. FatFormatter fatFormatter;
  107. // Format exFAT if larger than 32GB.
  108. bool rtn = cardSectorCount > 67108864 ?
  109. exFatFormatter.format(m_card, sectorBuffer, &Serial) :
  110. fatFormatter.format(m_card, sectorBuffer, &Serial);
  111. if (!rtn) {
  112. sdErrorHalt();
  113. }
  114. cout << F("Run the SdInfo example for format details.") << endl;
  115. }
  116. //------------------------------------------------------------------------------
  117. void printConfig(SdSpiConfig config) {
  118. if (DISABLE_CS_PIN < 0) {
  119. cout << F(
  120. "\nAssuming the SD is the only SPI device.\n"
  121. "Edit DISABLE_CS_PIN to disable an SPI device.\n");
  122. } else {
  123. cout << F("\nDisabling SPI device on pin ");
  124. cout << int(DISABLE_CS_PIN) << endl;
  125. pinMode(DISABLE_CS_PIN, OUTPUT);
  126. digitalWrite(DISABLE_CS_PIN, HIGH);
  127. }
  128. cout << F("\nAssuming the SD chip select pin is: ") << int(config.csPin);
  129. cout << F("\nEdit SD_CS_PIN to change the SD chip select pin.\n");
  130. }
  131. //------------------------------------------------------------------------------
  132. void printConfig(SdioConfig config) {
  133. (void)config;
  134. cout << F("Assuming an SDIO interface.\n");
  135. }
  136. //------------------------------------------------------------------------------
  137. void setup() {
  138. char c;
  139. Serial.begin(9600);
  140. // Wait for USB Serial
  141. while (!Serial) {
  142. SysCall::yield();
  143. }
  144. printConfig(SD_CONFIG);
  145. cout << F("\nType any character to start\n");
  146. while (!Serial.available()) {
  147. SysCall::yield();
  148. }
  149. // Discard any extra characters.
  150. do {
  151. delay(10);
  152. } while (Serial.available() && Serial.read() >= 0);
  153. cout << F(
  154. "\n"
  155. "This program can erase and/or format SD/SDHC/SDXC cards.\n"
  156. "\n"
  157. "Erase uses the card's fast flash erase command.\n"
  158. "Flash erase sets all data to 0X00 for most cards\n"
  159. "and 0XFF for a few vendor's cards.\n"
  160. "\n"
  161. "Cards up to 2 GiB (GiB = 2^30 bytes) will be formated FAT16.\n"
  162. "Cards larger than 2 GiB and up to 32 GiB will be formatted\n"
  163. "FAT32. Cards larger than 32 GiB will be formatted exFAT.\n"
  164. "\n"
  165. "Warning, all data on the card will be erased.\n"
  166. "Enter 'Y' to continue: ");
  167. while (!Serial.available()) {
  168. SysCall::yield();
  169. }
  170. c = Serial.read();
  171. cout << c << endl;
  172. if (c != 'Y') {
  173. cout << F("Quiting, you did not enter 'Y'.\n");
  174. return;
  175. }
  176. // Read any existing Serial data.
  177. do {
  178. delay(10);
  179. } while (Serial.available() && Serial.read() >= 0);
  180. // Select and initialize proper card driver.
  181. m_card = cardFactory.newCard(SD_CONFIG);
  182. if (!m_card || m_card->errorCode()) {
  183. sdError("card init failed.");
  184. return;
  185. }
  186. cardSectorCount = m_card->sectorCount();
  187. if (!cardSectorCount) {
  188. sdError("Get sector count failed.");
  189. return;
  190. }
  191. cout << F("\nCard size: ") << cardSectorCount*5.12e-7;
  192. cout << F(" GB (GB = 1E9 bytes)\n");
  193. cout << F("Card size: ") << cardSectorCount/2097152.0;
  194. cout << F(" GiB (GiB = 2^30 bytes)\n");
  195. cout << F("Card will be formated ");
  196. if (cardSectorCount > 67108864) {
  197. cout << F("exFAT\n");
  198. } else if (cardSectorCount > 4194304) {
  199. cout << F("FAT32\n");
  200. } else {
  201. cout << F("FAT16\n");
  202. }
  203. cout << F(
  204. "\n"
  205. "Options are:\n"
  206. "E - erase the card and skip formatting.\n"
  207. "F - erase and then format the card. (recommended)\n"
  208. "Q - quick format the card without erase.\n"
  209. "\n"
  210. "Enter option: ");
  211. while (!Serial.available()) {
  212. SysCall::yield();
  213. }
  214. c = Serial.read();
  215. cout << c << endl;
  216. if (!strchr("EFQ", c)) {
  217. cout << F("Quiting, invalid option entered.") << endl;
  218. return;
  219. }
  220. if (c == 'E' || c == 'F') {
  221. eraseCard();
  222. }
  223. if (c == 'F' || c == 'Q') {
  224. formatCard();
  225. }
  226. }
  227. void loop() {
  228. }