Browse Source

Cleanup, apply standards to SD lib, cardreader

Scott Lahteine 6 years ago
parent
commit
2390f6d3ab
15 changed files with 1439 additions and 1752 deletions
  1. 124
    138
      Marlin/Sd2Card.cpp
  2. 85
    132
      Marlin/Sd2Card.h
  3. 305
    405
      Marlin/SdBaseFile.cpp
  4. 229
    220
      Marlin/SdBaseFile.h
  5. 0
    18
      Marlin/SdFatConfig.h
  6. 384
    424
      Marlin/SdFatStructs.h
  7. 23
    23
      Marlin/SdFatUtil.cpp
  8. 3
    8
      Marlin/SdFatUtil.h
  9. 26
    28
      Marlin/SdFile.cpp
  10. 9
    12
      Marlin/SdFile.h
  11. 27
    49
      Marlin/SdInfo.h
  12. 75
    111
      Marlin/SdVolume.cpp
  13. 105
    121
      Marlin/SdVolume.h
  14. 23
    42
      Marlin/cardreader.cpp
  15. 21
    21
      Marlin/cardreader.h

+ 124
- 138
Marlin/Sd2Card.cpp View File

@@ -26,19 +26,19 @@
26 26
  *
27 27
  * This file is part of the Arduino Sd2Card Library
28 28
  */
29
-#include "Marlin.h"
29
+#include "MarlinConfig.h"
30 30
 
31 31
 #if ENABLED(SDSUPPORT)
32
+
32 33
 #include "Sd2Card.h"
33 34
 
34 35
 #if ENABLED(USE_WATCHDOG)
35 36
   #include "watchdog.h"
36 37
 #endif
37 38
 
38
-//------------------------------------------------------------------------------
39 39
 #if DISABLED(SOFTWARE_SPI)
40 40
   // functions for hardware SPI
41
-  //------------------------------------------------------------------------------
41
+
42 42
   // make sure SPCR rate is in expected bits
43 43
   #if (SPR0 != 0 || SPR1 != 1)
44 44
     #error "unexpected SPCR bits"
@@ -52,14 +52,14 @@
52 52
     SPCR = _BV(SPE) | _BV(MSTR) | (spiRate >> 1);
53 53
     SPSR = spiRate & 1 || spiRate == 6 ? 0 : _BV(SPI2X);
54 54
   }
55
-  //------------------------------------------------------------------------------
55
+
56 56
   /** SPI receive a byte */
57 57
   static uint8_t spiRec() {
58 58
     SPDR = 0xFF;
59 59
     while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
60 60
     return SPDR;
61 61
   }
62
-  //------------------------------------------------------------------------------
62
+
63 63
   /** SPI read data - only one call so force inline */
64 64
   static inline __attribute__((always_inline))
65 65
   void spiRead(uint8_t* buf, uint16_t nbyte) {
@@ -73,13 +73,13 @@
73 73
     while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
74 74
     buf[nbyte] = SPDR;
75 75
   }
76
-  //------------------------------------------------------------------------------
76
+
77 77
   /** SPI send a byte */
78 78
   static void spiSend(uint8_t b) {
79 79
     SPDR = b;
80 80
     while (!TEST(SPSR, SPIF)) { /* Intentionally left empty */ }
81 81
   }
82
-  //------------------------------------------------------------------------------
82
+
83 83
   /** SPI send block - only one call so force inline */
84 84
   static inline __attribute__((always_inline))
85 85
   void spiSendBlock(uint8_t token, const uint8_t* buf) {
@@ -95,9 +95,10 @@
95 95
        //------------------------------------------------------------------------------
96 96
 #else  // SOFTWARE_SPI
97 97
        //------------------------------------------------------------------------------
98
+
98 99
   /** nop to tune soft SPI timing */
99 100
   #define nop asm volatile ("nop\n\t")
100
-  //------------------------------------------------------------------------------
101
+
101 102
   /** Soft SPI receive byte */
102 103
   static uint8_t spiRec() {
103 104
     uint8_t data = 0;
@@ -123,13 +124,13 @@
123 124
     sei();
124 125
     return data;
125 126
   }
126
-  //------------------------------------------------------------------------------
127
+
127 128
   /** Soft SPI read data */
128 129
   static void spiRead(uint8_t* buf, uint16_t nbyte) {
129 130
     for (uint16_t i = 0; i < nbyte; i++)
130 131
       buf[i] = spiRec();
131 132
   }
132
-  //------------------------------------------------------------------------------
133
+
133 134
   /** Soft SPI send byte */
134 135
   static void spiSend(uint8_t data) {
135 136
     // no interrupts during byte send - about 8 us
@@ -153,7 +154,7 @@
153 154
     // enable interrupts
154 155
     sei();
155 156
   }
156
-  //------------------------------------------------------------------------------
157
+
157 158
   /** Soft SPI send block */
158 159
   void spiSendBlock(uint8_t token, const uint8_t* buf) {
159 160
     spiSend(token);
@@ -161,7 +162,7 @@
161 162
       spiSend(buf[i]);
162 163
   }
163 164
 #endif  // SOFTWARE_SPI
164
-//------------------------------------------------------------------------------
165
+
165 166
 // send command and return error code.  Return zero for OK
166 167
 uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) {
167 168
   // select card
@@ -189,7 +190,7 @@ uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) {
189 190
   for (uint8_t i = 0; ((status_ = spiRec()) & 0x80) && i != 0xFF; i++) { /* Intentionally left empty */ }
190 191
   return status_;
191 192
 }
192
-//------------------------------------------------------------------------------
193
+
193 194
 /**
194 195
  * Determine the size of an SD flash memory card.
195 196
  *
@@ -217,19 +218,20 @@ uint32_t Sd2Card::cardSize() {
217 218
     return 0;
218 219
   }
219 220
 }
220
-//------------------------------------------------------------------------------
221
+
221 222
 void Sd2Card::chipSelectHigh() {
222 223
   digitalWrite(chipSelectPin_, HIGH);
223 224
 }
224
-//------------------------------------------------------------------------------
225
+
225 226
 void Sd2Card::chipSelectLow() {
226 227
   #if DISABLED(SOFTWARE_SPI)
227 228
     spiInit(spiRate_);
228 229
   #endif  // SOFTWARE_SPI
229 230
   digitalWrite(chipSelectPin_, LOW);
230 231
 }
231
-//------------------------------------------------------------------------------
232
-/** Erase a range of blocks.
232
+
233
+/**
234
+ * Erase a range of blocks.
233 235
  *
234 236
  * \param[in] firstBlock The address of the first block in the range.
235 237
  * \param[in] lastBlock The address of the last block in the range.
@@ -239,8 +241,7 @@ void Sd2Card::chipSelectLow() {
239 241
  * either 0 or 1, depends on the card vendor.  The card must support
240 242
  * single block erase.
241 243
  *
242
- * \return The value one, true, is returned for success and
243
- * the value zero, false, is returned for failure.
244
+ * \return true for success, false for failure.
244 245
  */
245 246
 bool Sd2Card::erase(uint32_t firstBlock, uint32_t lastBlock) {
246 247
   csd_t csd;
@@ -275,26 +276,26 @@ bool Sd2Card::erase(uint32_t firstBlock, uint32_t lastBlock) {
275 276
   chipSelectHigh();
276 277
   return false;
277 278
 }
278
-//------------------------------------------------------------------------------
279
-/** Determine if card supports single block erase.
279
+
280
+/**
281
+ * Determine if card supports single block erase.
280 282
  *
281
- * \return The value one, true, is returned if single block erase is supported.
282
- * The value zero, false, is returned if single block erase is not supported.
283
+ * \return true if single block erase is supported.
284
+ *         false if single block erase is not supported.
283 285
  */
284 286
 bool Sd2Card::eraseSingleBlockEnable() {
285 287
   csd_t csd;
286 288
   return readCSD(&csd) ? csd.v1.erase_blk_en : false;
287 289
 }
288
-//------------------------------------------------------------------------------
290
+
289 291
 /**
290 292
  * Initialize an SD flash memory card.
291 293
  *
292 294
  * \param[in] sckRateID SPI clock rate selector. See setSckRate().
293 295
  * \param[in] chipSelectPin SD chip select pin number.
294 296
  *
295
- * \return The value one, true, is returned for success and
296
- * the value zero, false, is returned for failure.  The reason for failure
297
- * can be determined by calling errorCode() and errorData().
297
+ * \return true for success, false for failure.
298
+ * The reason for failure can be determined by calling errorCode() and errorData().
298 299
  */
299 300
 bool Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
300 301
   errorCode_ = type_ = 0;
@@ -384,14 +385,13 @@ bool Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
384 385
   chipSelectHigh();
385 386
   return false;
386 387
 }
387
-//------------------------------------------------------------------------------
388
+
388 389
 /**
389 390
  * Read a 512 byte block from an SD card.
390 391
  *
391 392
  * \param[in] blockNumber Logical block to be read.
392 393
  * \param[out] dst Pointer to the location that will receive the data.
393
- * \return The value one, true, is returned for success and
394
- * the value zero, false, is returned for failure.
394
+ * \return true for success, false for failure.
395 395
  */
396 396
 bool Sd2Card::readBlock(uint32_t blockNumber, uint8_t* dst) {
397 397
   // use address if not SDHC card
@@ -399,19 +399,18 @@ bool Sd2Card::readBlock(uint32_t blockNumber, uint8_t* dst) {
399 399
 
400 400
   #if ENABLED(SD_CHECK_AND_RETRY)
401 401
     uint8_t retryCnt = 3;
402
-    do {
403
-      if (!cardCommand(CMD17, blockNumber)) {
404
-        if (readData(dst, 512)) return true;
405
-      }
406
-      else
402
+    for(;;) {
403
+      if (cardCommand(CMD17, blockNumber))
407 404
         error(SD_CARD_ERROR_CMD17);
405
+      else if (readData(dst, 512))
406
+        return true;
408 407
 
409 408
       if (!--retryCnt) break;
410 409
 
411 410
       chipSelectHigh();
412 411
       cardCommand(CMD12, 0); // Try sending a stop command, ignore the result.
413 412
       errorCode_ = 0;
414
-    } while (true);
413
+    }
415 414
   #else
416 415
     if (cardCommand(CMD17, blockNumber))
417 416
       error(SD_CARD_ERROR_CMD17);
@@ -422,13 +421,13 @@ bool Sd2Card::readBlock(uint32_t blockNumber, uint8_t* dst) {
422 421
   chipSelectHigh();
423 422
   return false;
424 423
 }
425
-//------------------------------------------------------------------------------
426
-/** Read one data block in a multiple block read sequence
424
+
425
+/**
426
+ * Read one data block in a multiple block read sequence
427 427
  *
428 428
  * \param[in] dst Pointer to the location for the data to be read.
429 429
  *
430
- * \return The value one, true, is returned for success and
431
- * the value zero, false, is returned for failure.
430
+ * \return true for success, false for failure.
432 431
  */
433 432
 bool Sd2Card::readData(uint8_t* dst) {
434 433
   chipSelectLow();
@@ -436,50 +435,49 @@ bool Sd2Card::readData(uint8_t* dst) {
436 435
 }
437 436
 
438 437
 #if ENABLED(SD_CHECK_AND_RETRY)
439
-static const uint16_t crctab[] PROGMEM = {
440
-  0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
441
-  0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
442
-  0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
443
-  0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
444
-  0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
445
-  0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
446
-  0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
447
-  0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
448
-  0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
449
-  0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
450
-  0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
451
-  0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
452
-  0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
453
-  0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
454
-  0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
455
-  0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
456
-  0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
457
-  0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
458
-  0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
459
-  0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
460
-  0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
461
-  0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
462
-  0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
463
-  0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
464
-  0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
465
-  0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
466
-  0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
467
-  0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
468
-  0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
469
-  0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
470
-  0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
471
-  0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
472
-};
473
-static uint16_t CRC_CCITT(const uint8_t* data, size_t n) {
474
-  uint16_t crc = 0;
475
-  for (size_t i = 0; i < n; i++) {
476
-    crc = pgm_read_word(&crctab[(crc >> 8 ^ data[i]) & 0xFF]) ^ (crc << 8);
477
-  }
478
-  return crc;
479
-}
480
-#endif
438
+  static const uint16_t crctab[] PROGMEM = {
439
+    0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
440
+    0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
441
+    0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
442
+    0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
443
+    0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
444
+    0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
445
+    0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
446
+    0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
447
+    0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
448
+    0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
449
+    0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
450
+    0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
451
+    0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
452
+    0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
453
+    0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
454
+    0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
455
+    0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
456
+    0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
457
+    0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
458
+    0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
459
+    0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
460
+    0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
461
+    0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
462
+    0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
463
+    0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
464
+    0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
465
+    0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
466
+    0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
467
+    0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
468
+    0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
469
+    0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
470
+    0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
471
+  };
472
+  static uint16_t CRC_CCITT(const uint8_t* data, size_t n) {
473
+    uint16_t crc = 0;
474
+    for (size_t i = 0; i < n; i++) {
475
+      crc = pgm_read_word(&crctab[(crc >> 8 ^ data[i]) & 0xFF]) ^ (crc << 8);
476
+    }
477
+    return crc;
478
+  }
479
+#endif // SD_CHECK_AND_RETRY
481 480
 
482
-//------------------------------------------------------------------------------
483 481
 bool Sd2Card::readData(uint8_t* dst, uint16_t count) {
484 482
   // wait for start block token
485 483
   uint16_t t0 = millis();
@@ -521,61 +519,55 @@ bool Sd2Card::readData(uint8_t* dst, uint16_t count) {
521 519
   spiSend(0XFF);
522 520
   return false;
523 521
 }
524
-//------------------------------------------------------------------------------
522
+
525 523
 /** read CID or CSR register */
526 524
 bool Sd2Card::readRegister(uint8_t cmd, void* buf) {
527 525
   uint8_t* dst = reinterpret_cast<uint8_t*>(buf);
528 526
   if (cardCommand(cmd, 0)) {
529 527
     error(SD_CARD_ERROR_READ_REG);
530
-    goto FAIL;
528
+    chipSelectHigh();
529
+    return false;
531 530
   }
532 531
   return readData(dst, 16);
533
-  FAIL:
534
-  chipSelectHigh();
535
-  return false;
536 532
 }
537
-//------------------------------------------------------------------------------
538
-/** Start a read multiple blocks sequence.
533
+
534
+/**
535
+ * Start a read multiple blocks sequence.
539 536
  *
540 537
  * \param[in] blockNumber Address of first block in sequence.
541 538
  *
542 539
  * \note This function is used with readData() and readStop() for optimized
543 540
  * multiple block reads.  SPI chipSelect must be low for the entire sequence.
544 541
  *
545
- * \return The value one, true, is returned for success and
546
- * the value zero, false, is returned for failure.
542
+ * \return true for success, false for failure.
547 543
  */
548 544
 bool Sd2Card::readStart(uint32_t blockNumber) {
549 545
   if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
550 546
   if (cardCommand(CMD18, blockNumber)) {
551 547
     error(SD_CARD_ERROR_CMD18);
552
-    goto FAIL;
548
+    chipSelectHigh();
549
+    return false;
553 550
   }
554 551
   chipSelectHigh();
555 552
   return true;
556
-  FAIL:
557
-  chipSelectHigh();
558
-  return false;
559 553
 }
560
-//------------------------------------------------------------------------------
561
-/** End a read multiple blocks sequence.
554
+
555
+/**
556
+ * End a read multiple blocks sequence.
562 557
  *
563
-* \return The value one, true, is returned for success and
564
- * the value zero, false, is returned for failure.
558
+ * \return true for success, false for failure.
565 559
  */
566 560
 bool Sd2Card::readStop() {
567 561
   chipSelectLow();
568 562
   if (cardCommand(CMD12, 0)) {
569 563
     error(SD_CARD_ERROR_CMD12);
570
-    goto FAIL;
564
+    chipSelectHigh();
565
+    return false;
571 566
   }
572 567
   chipSelectHigh();
573 568
   return true;
574
-  FAIL:
575
-  chipSelectHigh();
576
-  return false;
577 569
 }
578
-//------------------------------------------------------------------------------
570
+
579 571
 /**
580 572
  * Set the SPI clock rate.
581 573
  *
@@ -596,25 +588,22 @@ bool Sd2Card::setSckRate(uint8_t sckRateID) {
596 588
   spiRate_ = sckRateID;
597 589
   return true;
598 590
 }
599
-//------------------------------------------------------------------------------
591
+
600 592
 // wait for card to go not busy
601 593
 bool Sd2Card::waitNotBusy(uint16_t timeoutMillis) {
602 594
   uint16_t t0 = millis();
603
-  while (spiRec() != 0XFF) {
604
-    if (((uint16_t)millis() - t0) >= timeoutMillis) goto FAIL;
605
-  }
595
+  while (spiRec() != 0XFF)
596
+    if (((uint16_t)millis() - t0) >= timeoutMillis) return false;
597
+
606 598
   return true;
607
-  FAIL:
608
-  return false;
609 599
 }
610
-//------------------------------------------------------------------------------
600
+
611 601
 /**
612 602
  * Writes a 512 byte block to an SD card.
613 603
  *
614 604
  * \param[in] blockNumber Logical block to be written.
615 605
  * \param[in] src Pointer to the location of the data to be written.
616
- * \return The value one, true, is returned for success and
617
- * the value zero, false, is returned for failure.
606
+ * \return true for success, false for failure.
618 607
  */
619 608
 bool Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) {
620 609
   // use address if not SDHC card
@@ -641,25 +630,24 @@ bool Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) {
641 630
   chipSelectHigh();
642 631
   return false;
643 632
 }
644
-//------------------------------------------------------------------------------
645
-/** Write one data block in a multiple block write sequence
633
+
634
+/**
635
+ * Write one data block in a multiple block write sequence
646 636
  * \param[in] src Pointer to the location of the data to be written.
647
- * \return The value one, true, is returned for success and
648
- * the value zero, false, is returned for failure.
637
+ * \return true for success, false for failure.
649 638
  */
650 639
 bool Sd2Card::writeData(const uint8_t* src) {
651 640
   chipSelectLow();
652 641
   // wait for previous write to finish
653
-  if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto FAIL;
654
-  if (!writeData(WRITE_MULTIPLE_TOKEN, src)) goto FAIL;
642
+  if (!waitNotBusy(SD_WRITE_TIMEOUT) || !writeData(WRITE_MULTIPLE_TOKEN, src)) {
643
+    error(SD_CARD_ERROR_WRITE_MULTIPLE);
644
+    chipSelectHigh();
645
+    return false;
646
+  }
655 647
   chipSelectHigh();
656 648
   return true;
657
-  FAIL:
658
-  error(SD_CARD_ERROR_WRITE_MULTIPLE);
659
-  chipSelectHigh();
660
-  return false;
661 649
 }
662
-//------------------------------------------------------------------------------
650
+
663 651
 // send one block of data for write block or write multiple blocks
664 652
 bool Sd2Card::writeData(uint8_t token, const uint8_t* src) {
665 653
   spiSendBlock(token, src);
@@ -670,15 +658,14 @@ bool Sd2Card::writeData(uint8_t token, const uint8_t* src) {
670 658
   status_ = spiRec();
671 659
   if ((status_ & DATA_RES_MASK) != DATA_RES_ACCEPTED) {
672 660
     error(SD_CARD_ERROR_WRITE);
673
-    goto FAIL;
661
+    chipSelectHigh();
662
+    return false;
674 663
   }
675 664
   return true;
676
-  FAIL:
677
-  chipSelectHigh();
678
-  return false;
679 665
 }
680
-//------------------------------------------------------------------------------
681
-/** Start a write multiple blocks sequence.
666
+
667
+/**
668
+ * Start a write multiple blocks sequence.
682 669
  *
683 670
  * \param[in] blockNumber Address of first block in sequence.
684 671
  * \param[in] eraseCount The number of blocks to be pre-erased.
@@ -686,8 +673,7 @@ bool Sd2Card::writeData(uint8_t token, const uint8_t* src) {
686 673
  * \note This function is used with writeData() and writeStop()
687 674
  * for optimized multiple block writes.
688 675
  *
689
- * \return The value one, true, is returned for success and
690
- * the value zero, false, is returned for failure.
676
+ * \return true for success, false for failure.
691 677
  */
692 678
 bool Sd2Card::writeStart(uint32_t blockNumber, uint32_t eraseCount) {
693 679
   // send pre-erase count
@@ -707,11 +693,11 @@ bool Sd2Card::writeStart(uint32_t blockNumber, uint32_t eraseCount) {
707 693
   chipSelectHigh();
708 694
   return false;
709 695
 }
710
-//------------------------------------------------------------------------------
711
-/** End a write multiple blocks sequence.
696
+
697
+/**
698
+ * End a write multiple blocks sequence.
712 699
  *
713
-* \return The value one, true, is returned for success and
714
- * the value zero, false, is returned for failure.
700
+ * \return true for success, false for failure.
715 701
  */
716 702
 bool Sd2Card::writeStop() {
717 703
   chipSelectLow();
@@ -726,4 +712,4 @@ bool Sd2Card::writeStop() {
726 712
   return false;
727 713
 }
728 714
 
729
-#endif
715
+#endif // SDSUPPORT

+ 85
- 132
Marlin/Sd2Card.h View File

@@ -21,164 +21,118 @@
21 21
  */
22 22
 
23 23
 /**
24
+ * \file
25
+ * \brief Sd2Card class for V2 SD/SDHC cards
26
+ */
27
+
28
+/**
24 29
  * Arduino Sd2Card Library
25 30
  * Copyright (C) 2009 by William Greiman
26 31
  *
27 32
  * This file is part of the Arduino Sd2Card Library
28 33
  */
34
+#ifndef _SD2CARD_H_
35
+#define _SD2CARD_H_
29 36
 
30
-#include "Marlin.h"
31
-#if ENABLED(SDSUPPORT)
32
-
33
-#ifndef Sd2Card_h
34
-#define Sd2Card_h
35
-/**
36
- * \file
37
- * \brief Sd2Card class for V2 SD/SDHC cards
38
- */
39 37
 #include "SdFatConfig.h"
40 38
 #include "SdInfo.h"
41
-//------------------------------------------------------------------------------
39
+
42 40
 // SPI speed is F_CPU/2^(1 + index), 0 <= index <= 6
43
-/** Set SCK to max rate of F_CPU/2. See Sd2Card::setSckRate(). */
44
-uint8_t const SPI_FULL_SPEED = 0;
45
-/** Set SCK rate to F_CPU/4. See Sd2Card::setSckRate(). */
46
-uint8_t const SPI_HALF_SPEED = 1;
47
-/** Set SCK rate to F_CPU/8. See Sd2Card::setSckRate(). */
48
-uint8_t const SPI_QUARTER_SPEED = 2;
49
-/** Set SCK rate to F_CPU/16. See Sd2Card::setSckRate(). */
50
-uint8_t const SPI_EIGHTH_SPEED = 3;
51
-/** Set SCK rate to F_CPU/32. See Sd2Card::setSckRate(). */
52
-uint8_t const SPI_SIXTEENTH_SPEED = 4;
53
-//------------------------------------------------------------------------------
54
-/** init timeout ms */
55
-uint16_t const SD_INIT_TIMEOUT = 2000;
56
-/** erase timeout ms */
57
-uint16_t const SD_ERASE_TIMEOUT = 10000;
58
-/** read timeout ms */
59
-uint16_t const SD_READ_TIMEOUT = 300;
60
-/** write time out ms */
61
-uint16_t const SD_WRITE_TIMEOUT = 600;
62
-//------------------------------------------------------------------------------
41
+uint8_t const SPI_FULL_SPEED = 0,         // Set SCK to max rate of F_CPU/2. See Sd2Card::setSckRate().
42
+              SPI_HALF_SPEED = 1,         // Set SCK rate to F_CPU/4. See Sd2Card::setSckRate().
43
+              SPI_QUARTER_SPEED = 2,      // Set SCK rate to F_CPU/8. See Sd2Card::setSckRate().
44
+              SPI_EIGHTH_SPEED = 3,       // Set SCK rate to F_CPU/16. See Sd2Card::setSckRate().
45
+              SPI_SIXTEENTH_SPEED = 4;    // Set SCK rate to F_CPU/32. See Sd2Card::setSckRate().
46
+
47
+uint16_t const SD_INIT_TIMEOUT = 2000,    // init timeout ms
48
+               SD_ERASE_TIMEOUT = 10000,  // erase timeout ms
49
+               SD_READ_TIMEOUT = 300,     // read timeout ms
50
+               SD_WRITE_TIMEOUT = 600;    // write time out ms
51
+
63 52
 // SD card errors
64
-/** timeout error for command CMD0 (initialize card in SPI mode) */
65
-uint8_t const SD_CARD_ERROR_CMD0 = 0X1;
66
-/** CMD8 was not accepted - not a valid SD card*/
67
-uint8_t const SD_CARD_ERROR_CMD8 = 0X2;
68
-/** card returned an error response for CMD12 (write stop) */
69
-uint8_t const SD_CARD_ERROR_CMD12 = 0X3;
70
-/** card returned an error response for CMD17 (read block) */
71
-uint8_t const SD_CARD_ERROR_CMD17 = 0X4;
72
-/** card returned an error response for CMD18 (read multiple block) */
73
-uint8_t const SD_CARD_ERROR_CMD18 = 0X5;
74
-/** card returned an error response for CMD24 (write block) */
75
-uint8_t const SD_CARD_ERROR_CMD24 = 0X6;
76
-/**  WRITE_MULTIPLE_BLOCKS command failed */
77
-uint8_t const SD_CARD_ERROR_CMD25 = 0X7;
78
-/** card returned an error response for CMD58 (read OCR) */
79
-uint8_t const SD_CARD_ERROR_CMD58 = 0X8;
80
-/** SET_WR_BLK_ERASE_COUNT failed */
81
-uint8_t const SD_CARD_ERROR_ACMD23 = 0X9;
82
-/** ACMD41 initialization process timeout */
83
-uint8_t const SD_CARD_ERROR_ACMD41 = 0XA;
84
-/** card returned a bad CSR version field */
85
-uint8_t const SD_CARD_ERROR_BAD_CSD = 0XB;
86
-/** erase block group command failed */
87
-uint8_t const SD_CARD_ERROR_ERASE = 0XC;
88
-/** card not capable of single block erase */
89
-uint8_t const SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0XD;
90
-/** Erase sequence timed out */
91
-uint8_t const SD_CARD_ERROR_ERASE_TIMEOUT = 0XE;
92
-/** card returned an error token instead of read data */
93
-uint8_t const SD_CARD_ERROR_READ = 0XF;
94
-/** read CID or CSD failed */
95
-uint8_t const SD_CARD_ERROR_READ_REG = 0x10;
96
-/** timeout while waiting for start of read data */
97
-uint8_t const SD_CARD_ERROR_READ_TIMEOUT = 0x11;
98
-/** card did not accept STOP_TRAN_TOKEN */
99
-uint8_t const SD_CARD_ERROR_STOP_TRAN = 0x12;
100
-/** card returned an error token as a response to a write operation */
101
-uint8_t const SD_CARD_ERROR_WRITE = 0x13;
102
-/** attempt to write protected block zero */
103
-uint8_t const SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0x14;  // REMOVE - not used
104
-/** card did not go ready for a multiple block write */
105
-uint8_t const SD_CARD_ERROR_WRITE_MULTIPLE = 0x15;
106
-/** card returned an error to a CMD13 status check after a write */
107
-uint8_t const SD_CARD_ERROR_WRITE_PROGRAMMING = 0x16;
108
-/** timeout occurred during write programming */
109
-uint8_t const SD_CARD_ERROR_WRITE_TIMEOUT = 0x17;
110
-/** incorrect rate selected */
111
-uint8_t const SD_CARD_ERROR_SCK_RATE = 0x18;
112
-/** init() not called */
113
-uint8_t const SD_CARD_ERROR_INIT_NOT_CALLED = 0x19;
114
-/** crc check error */
115
-uint8_t const SD_CARD_ERROR_CRC = 0x20;
116
-//------------------------------------------------------------------------------
53
+uint8_t const SD_CARD_ERROR_CMD0 = 0X1,                 // timeout error for command CMD0 (initialize card in SPI mode)
54
+              SD_CARD_ERROR_CMD8 = 0X2,                 // CMD8 was not accepted - not a valid SD card
55
+              SD_CARD_ERROR_CMD12 = 0X3,                // card returned an error response for CMD12 (write stop)
56
+              SD_CARD_ERROR_CMD17 = 0X4,                // card returned an error response for CMD17 (read block)
57
+              SD_CARD_ERROR_CMD18 = 0X5,                // card returned an error response for CMD18 (read multiple block)
58
+              SD_CARD_ERROR_CMD24 = 0X6,                // card returned an error response for CMD24 (write block)
59
+              SD_CARD_ERROR_CMD25 = 0X7,                // WRITE_MULTIPLE_BLOCKS command failed
60
+              SD_CARD_ERROR_CMD58 = 0X8,                // card returned an error response for CMD58 (read OCR)
61
+              SD_CARD_ERROR_ACMD23 = 0X9,               // SET_WR_BLK_ERASE_COUNT failed
62
+              SD_CARD_ERROR_ACMD41 = 0XA,               // ACMD41 initialization process timeout
63
+              SD_CARD_ERROR_BAD_CSD = 0XB,              // card returned a bad CSR version field
64
+              SD_CARD_ERROR_ERASE = 0XC,                // erase block group command failed
65
+              SD_CARD_ERROR_ERASE_SINGLE_BLOCK = 0XD,   // card not capable of single block erase
66
+              SD_CARD_ERROR_ERASE_TIMEOUT = 0XE,        // Erase sequence timed out
67
+              SD_CARD_ERROR_READ = 0XF,                 // card returned an error token instead of read data
68
+              SD_CARD_ERROR_READ_REG = 0x10,            // read CID or CSD failed
69
+              SD_CARD_ERROR_READ_TIMEOUT = 0x11,        // timeout while waiting for start of read data
70
+              SD_CARD_ERROR_STOP_TRAN = 0x12,           // card did not accept STOP_TRAN_TOKEN
71
+              SD_CARD_ERROR_WRITE = 0x13,               // card returned an error token as a response to a write operation
72
+              SD_CARD_ERROR_WRITE_BLOCK_ZERO = 0x14,    // REMOVE - not used ... attempt to write protected block zero
73
+              SD_CARD_ERROR_WRITE_MULTIPLE = 0x15,      // card did not go ready for a multiple block write
74
+              SD_CARD_ERROR_WRITE_PROGRAMMING = 0x16,   // card returned an error to a CMD13 status check after a write
75
+              SD_CARD_ERROR_WRITE_TIMEOUT = 0x17,       // timeout occurred during write programming
76
+              SD_CARD_ERROR_SCK_RATE = 0x18,            // incorrect rate selected
77
+              SD_CARD_ERROR_INIT_NOT_CALLED = 0x19,     // init() not called
78
+              SD_CARD_ERROR_CRC = 0x20;                 // crc check error
79
+
117 80
 // card types
118
-/** Standard capacity V1 SD card */
119
-uint8_t const SD_CARD_TYPE_SD1  = 1;
120
-/** Standard capacity V2 SD card */
121
-uint8_t const SD_CARD_TYPE_SD2  = 2;
122
-/** High Capacity SD card */
123
-uint8_t const SD_CARD_TYPE_SDHC = 3;
81
+uint8_t const SD_CARD_TYPE_SD1  = 1,                    // Standard capacity V1 SD card
82
+              SD_CARD_TYPE_SD2  = 2,                    // Standard capacity V2 SD card
83
+              SD_CARD_TYPE_SDHC = 3;                    // High Capacity SD card
84
+
124 85
 /**
125 86
  * define SOFTWARE_SPI to use bit-bang SPI
126 87
  */
127
-//------------------------------------------------------------------------------
128 88
 #if MEGA_SOFT_SPI
129 89
   #define SOFTWARE_SPI
130 90
 #elif USE_SOFTWARE_SPI
131 91
   #define SOFTWARE_SPI
132
-#endif  // MEGA_SOFT_SPI
133
-//------------------------------------------------------------------------------
92
+#endif
93
+
134 94
 // SPI pin definitions - do not edit here - change in SdFatConfig.h
135
-//
136 95
 #if DISABLED(SOFTWARE_SPI)
137 96
   // hardware pin defs
138
-  /** The default chip select pin for the SD card is SS. */
139
-  #define SD_CHIP_SELECT_PIN SS_PIN
97
+  #define SD_CHIP_SELECT_PIN SS_PIN   // The default chip select pin for the SD card is SS.
140 98
   // The following three pins must not be redefined for hardware SPI.
141
-  /** SPI Master Out Slave In pin */
142
-  #define SPI_MOSI_PIN MOSI_PIN
143
-  /** SPI Master In Slave Out pin */
144
-  #define SPI_MISO_PIN MISO_PIN
145
-  /** SPI Clock pin */
146
-  #define SPI_SCK_PIN SCK_PIN
147
-
99
+  #define SPI_MOSI_PIN MOSI_PIN       // SPI Master Out Slave In pin
100
+  #define SPI_MISO_PIN MISO_PIN       // SPI Master In Slave Out pin
101
+  #define SPI_SCK_PIN SCK_PIN         // SPI Clock pin
148 102
 #else  // SOFTWARE_SPI
149
-
150
-  /** SPI chip select pin */
151
-  #define SD_CHIP_SELECT_PIN SOFT_SPI_CS_PIN
152
-  /** SPI Master Out Slave In pin */
153
-  #define SPI_MOSI_PIN SOFT_SPI_MOSI_PIN
154
-  /** SPI Master In Slave Out pin */
155
-  #define SPI_MISO_PIN SOFT_SPI_MISO_PIN
156
-  /** SPI Clock pin */
157
-  #define SPI_SCK_PIN SOFT_SPI_SCK_PIN
103
+  #define SD_CHIP_SELECT_PIN SOFT_SPI_CS_PIN  // SPI chip select pin
104
+  #define SPI_MOSI_PIN SOFT_SPI_MOSI_PIN      // SPI Master Out Slave In pin
105
+  #define SPI_MISO_PIN SOFT_SPI_MISO_PIN      // SPI Master In Slave Out pin
106
+  #define SPI_SCK_PIN SOFT_SPI_SCK_PIN        // SPI Clock pin
158 107
 #endif  // SOFTWARE_SPI
159
-//------------------------------------------------------------------------------
108
+
160 109
 /**
161 110
  * \class Sd2Card
162 111
  * \brief Raw access to SD and SDHC flash memory cards.
163 112
  */
164 113
 class Sd2Card {
165
- public:
166
-  /** Construct an instance of Sd2Card. */
114
+  public:
115
+
167 116
   Sd2Card() : errorCode_(SD_CARD_ERROR_INIT_NOT_CALLED), type_(0) {}
117
+
168 118
   uint32_t cardSize();
169 119
   bool erase(uint32_t firstBlock, uint32_t lastBlock);
170 120
   bool eraseSingleBlockEnable();
121
+
171 122
   /**
172 123
    *  Set SD error code.
173 124
    *  \param[in] code value for error code.
174 125
    */
175 126
   void error(uint8_t code) {errorCode_ = code;}
127
+
176 128
   /**
177 129
    * \return error code for last error. See Sd2Card.h for a list of error codes.
178 130
    */
179 131
   int errorCode() const {return errorCode_;}
132
+
180 133
   /** \return error data for last error. */
181 134
   int errorData() const {return status_;}
135
+
182 136
   /**
183 137
    * Initialize an SD flash memory card with default clock rate and chip
184 138
    * select pin.  See sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin).
@@ -188,6 +142,7 @@ class Sd2Card {
188 142
   bool init(uint8_t sckRateID = SPI_FULL_SPEED,
189 143
             uint8_t chipSelectPin = SD_CHIP_SELECT_PIN);
190 144
   bool readBlock(uint32_t block, uint8_t* dst);
145
+
191 146
   /**
192 147
    * Read a card's CID register. The CID contains card identification
193 148
    * information such as Manufacturer ID, Product name, Product serial
@@ -197,9 +152,8 @@ class Sd2Card {
197 152
    *
198 153
    * \return true for success or false for failure.
199 154
    */
200
-  bool readCID(cid_t* cid) {
201
-    return readRegister(CMD10, cid);
202
-  }
155
+  bool readCID(cid_t* cid) { return readRegister(CMD10, cid); }
156
+
203 157
   /**
204 158
    * Read a card's CSD register. The CSD contains Card-Specific Data that
205 159
    * provides information regarding access to the card's contents.
@@ -208,14 +162,14 @@ class Sd2Card {
208 162
    *
209 163
    * \return true for success or false for failure.
210 164
    */
211
-  bool readCSD(csd_t* csd) {
212
-    return readRegister(CMD9, csd);
213
-  }
165
+  bool readCSD(csd_t* csd) { return readRegister(CMD9, csd); }
166
+
214 167
   bool readData(uint8_t* dst);
215 168
   bool readStart(uint32_t blockNumber);
216 169
   bool readStop();
217 170
   bool setSckRate(uint8_t sckRateID);
218
-  /** Return the card type: SD V1, SD V2 or SDHC
171
+  /**
172
+   * Return the card type: SD V1, SD V2 or SDHC
219 173
    * \return 0 - SD V1, 1 - SD V2, or 3 - SDHC.
220 174
    */
221 175
   int type() const {return type_;}
@@ -223,13 +177,14 @@ class Sd2Card {
223 177
   bool writeData(const uint8_t* src);
224 178
   bool writeStart(uint32_t blockNumber, uint32_t eraseCount);
225 179
   bool writeStop();
226
- private:
227
-  //----------------------------------------------------------------------------
228
-  uint8_t chipSelectPin_;
229
-  uint8_t errorCode_;
230
-  uint8_t spiRate_;
231
-  uint8_t status_;
232
-  uint8_t type_;
180
+
181
+  private:
182
+  uint8_t chipSelectPin_,
183
+          errorCode_,
184
+          spiRate_,
185
+          status_,
186
+          type_;
187
+
233 188
   // private functions
234 189
   uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
235 190
     cardCommand(CMD55, 0);
@@ -241,11 +196,9 @@ class Sd2Card {
241 196
   bool readRegister(uint8_t cmd, void* buf);
242 197
   void chipSelectHigh();
243 198
   void chipSelectLow();
244
-  void type(uint8_t value) {type_ = value;}
199
+  void type(uint8_t value) { type_ = value; }
245 200
   bool waitNotBusy(uint16_t timeoutMillis);
246 201
   bool writeData(uint8_t token, const uint8_t* src);
247 202
 };
248
-#endif  // Sd2Card_h
249 203
 
250
-
251
-#endif
204
+#endif  // _SD2CARD_H_

+ 305
- 405
Marlin/SdBaseFile.cpp
File diff suppressed because it is too large
View File


+ 229
- 220
Marlin/SdBaseFile.h View File

@@ -21,207 +21,195 @@
21 21
  */
22 22
 
23 23
 /**
24
+ * \file
25
+ * \brief SdBaseFile class
26
+ */
27
+
28
+/**
24 29
  * Arduino SdFat Library
25 30
  * Copyright (C) 2009 by William Greiman
26 31
  *
27 32
  * This file is part of the Arduino Sd2Card Library
28 33
  */
29
-#include "Marlin.h"
30
-#if ENABLED(SDSUPPORT)
34
+#ifndef _SDBASEFILE_H_
35
+#define _SDBASEFILE_H_
31 36
 
32
-#ifndef SdBaseFile_h
33
-#define SdBaseFile_h
34
-/**
35
- * \file
36
- * \brief SdBaseFile class
37
- */
38
-#include "Marlin.h"
39 37
 #include "SdFatConfig.h"
40 38
 #include "SdVolume.h"
41
-//------------------------------------------------------------------------------
39
+
42 40
 /**
43 41
  * \struct filepos_t
44 42
  * \brief internal type for istream
45 43
  * do not use in user apps
46 44
  */
47 45
 struct filepos_t {
48
-  /** stream position */
49
-  uint32_t position;
50
-  /** cluster for position */
51
-  uint32_t cluster;
46
+  uint32_t position;  // stream byte position
47
+  uint32_t cluster;   // cluster of position
52 48
   filepos_t() : position(0), cluster(0) {}
53 49
 };
54 50
 
55 51
 // use the gnu style oflag in open()
56
-/** open() oflag for reading */
57
-uint8_t const O_READ = 0x01;
58
-/** open() oflag - same as O_IN */
59
-uint8_t const O_RDONLY = O_READ;
60
-/** open() oflag for write */
61
-uint8_t const O_WRITE = 0x02;
62
-/** open() oflag - same as O_WRITE */
63
-uint8_t const O_WRONLY = O_WRITE;
64
-/** open() oflag for reading and writing */
65
-uint8_t const O_RDWR = (O_READ | O_WRITE);
66
-/** open() oflag mask for access modes */
67
-uint8_t const O_ACCMODE = (O_READ | O_WRITE);
68
-/** The file offset shall be set to the end of the file prior to each write. */
69
-uint8_t const O_APPEND = 0x04;
70
-/** synchronous writes - call sync() after each write */
71
-uint8_t const O_SYNC = 0x08;
72
-/** truncate the file to zero length */
73
-uint8_t const O_TRUNC = 0x10;
74
-/** set the initial position at the end of the file */
75
-uint8_t const O_AT_END = 0x20;
76
-/** create the file if nonexistent */
77
-uint8_t const O_CREAT = 0x40;
78
-/** If O_CREAT and O_EXCL are set, open() shall fail if the file exists */
79
-uint8_t const O_EXCL = 0x80;
52
+uint8_t const O_READ = 0x01,                    // open() oflag for reading
53
+              O_RDONLY = O_READ,                // open() oflag - same as O_IN
54
+              O_WRITE = 0x02,                   // open() oflag for write
55
+              O_WRONLY = O_WRITE,               // open() oflag - same as O_WRITE
56
+              O_RDWR = (O_READ | O_WRITE),      // open() oflag for reading and writing
57
+              O_ACCMODE = (O_READ | O_WRITE),   // open() oflag mask for access modes
58
+              O_APPEND = 0x04,                  // The file offset shall be set to the end of the file prior to each write.
59
+              O_SYNC = 0x08,                    // Synchronous writes - call sync() after each write
60
+              O_TRUNC = 0x10,                   // Truncate the file to zero length
61
+              O_AT_END = 0x20,                  // Set the initial position at the end of the file
62
+              O_CREAT = 0x40,                   // Create the file if nonexistent
63
+              O_EXCL = 0x80;                    // If O_CREAT and O_EXCL are set, open() shall fail if the file exists
80 64
 
81 65
 // SdBaseFile class static and const definitions
66
+
82 67
 // flags for ls()
83
-/** ls() flag to print modify date */
84
-uint8_t const LS_DATE = 1;
85
-/** ls() flag to print file size */
86
-uint8_t const LS_SIZE = 2;
87
-/** ls() flag for recursive list of subdirectories */
88
-uint8_t const LS_R = 4;
68
+uint8_t const LS_DATE = 1,    // ls() flag to print modify date
69
+              LS_SIZE = 2,    // ls() flag to print file size
70
+              LS_R = 4;       // ls() flag for recursive list of subdirectories
89 71
 
90 72
 
91 73
 // flags for timestamp
92
-/** set the file's last access date */
93
-uint8_t const T_ACCESS = 1;
94
-/** set the file's creation date and time */
95
-uint8_t const T_CREATE = 2;
96
-/** Set the file's write date and time */
97
-uint8_t const T_WRITE = 4;
74
+uint8_t const T_ACCESS = 1,   // Set the file's last access date
75
+              T_CREATE = 2,   // Set the file's creation date and time
76
+              T_WRITE = 4;    // Set the file's write date and time
77
+
98 78
 // values for type_
99
-/** This file has not been opened. */
100
-uint8_t const FAT_FILE_TYPE_CLOSED = 0;
101
-/** A normal file */
102
-uint8_t const FAT_FILE_TYPE_NORMAL = 1;
103
-/** A FAT12 or FAT16 root directory */
104
-uint8_t const FAT_FILE_TYPE_ROOT_FIXED = 2;
105
-/** A FAT32 root directory */
106
-uint8_t const FAT_FILE_TYPE_ROOT32 = 3;
107
-/** A subdirectory file*/
108
-uint8_t const FAT_FILE_TYPE_SUBDIR = 4;
109
-/** Test value for directory type */
110
-uint8_t const FAT_FILE_TYPE_MIN_DIR = FAT_FILE_TYPE_ROOT_FIXED;
111
-
112
-/** date field for FAT directory entry
79
+uint8_t const FAT_FILE_TYPE_CLOSED = 0,                           // This file has not been opened.
80
+              FAT_FILE_TYPE_NORMAL = 1,                           // A normal file
81
+              FAT_FILE_TYPE_ROOT_FIXED = 2,                       // A FAT12 or FAT16 root directory
82
+              FAT_FILE_TYPE_ROOT32 = 3,                           // A FAT32 root directory
83
+              FAT_FILE_TYPE_SUBDIR = 4,                           // A subdirectory file
84
+              FAT_FILE_TYPE_MIN_DIR = FAT_FILE_TYPE_ROOT_FIXED;   // Test value for directory type
85
+
86
+/**
87
+ * date field for FAT directory entry
113 88
  * \param[in] year [1980,2107]
114 89
  * \param[in] month [1,12]
115 90
  * \param[in] day [1,31]
116 91
  *
117 92
  * \return Packed date for dir_t entry.
118 93
  */
119
-static inline uint16_t FAT_DATE(uint16_t year, uint8_t month, uint8_t day) {
120
-  return (year - 1980) << 9 | month << 5 | day;
121
-}
122
-/** year part of FAT directory date field
94
+static inline uint16_t FAT_DATE(uint16_t year, uint8_t month, uint8_t day) { return (year - 1980) << 9 | month << 5 | day; }
95
+
96
+/**
97
+ * year part of FAT directory date field
123 98
  * \param[in] fatDate Date in packed dir format.
124 99
  *
125 100
  * \return Extracted year [1980,2107]
126 101
  */
127
-static inline uint16_t FAT_YEAR(uint16_t fatDate) {
128
-  return 1980 + (fatDate >> 9);
129
-}
130
-/** month part of FAT directory date field
102
+static inline uint16_t FAT_YEAR(uint16_t fatDate) { return 1980 + (fatDate >> 9); }
103
+
104
+/**
105
+ * month part of FAT directory date field
131 106
  * \param[in] fatDate Date in packed dir format.
132 107
  *
133 108
  * \return Extracted month [1,12]
134 109
  */
135
-static inline uint8_t FAT_MONTH(uint16_t fatDate) {
136
-  return (fatDate >> 5) & 0XF;
137
-}
138
-/** day part of FAT directory date field
110
+static inline uint8_t FAT_MONTH(uint16_t fatDate) { return (fatDate >> 5) & 0XF; }
111
+
112
+/**
113
+ * day part of FAT directory date field
139 114
  * \param[in] fatDate Date in packed dir format.
140 115
  *
141 116
  * \return Extracted day [1,31]
142 117
  */
143
-static inline uint8_t FAT_DAY(uint16_t fatDate) {
144
-  return fatDate & 0x1F;
145
-}
146
-/** time field for FAT directory entry
118
+static inline uint8_t FAT_DAY(uint16_t fatDate) { return fatDate & 0x1F; }
119
+
120
+/**
121
+ * time field for FAT directory entry
147 122
  * \param[in] hour [0,23]
148 123
  * \param[in] minute [0,59]
149 124
  * \param[in] second [0,59]
150 125
  *
151 126
  * \return Packed time for dir_t entry.
152 127
  */
153
-static inline uint16_t FAT_TIME(uint8_t hour, uint8_t minute, uint8_t second) {
154
-  return hour << 11 | minute << 5 | second >> 1;
155
-}
156
-/** hour part of FAT directory time field
128
+static inline uint16_t FAT_TIME(uint8_t hour, uint8_t minute, uint8_t second) { return hour << 11 | minute << 5 | second >> 1; }
129
+
130
+/**
131
+ * hour part of FAT directory time field
157 132
  * \param[in] fatTime Time in packed dir format.
158 133
  *
159 134
  * \return Extracted hour [0,23]
160 135
  */
161
-static inline uint8_t FAT_HOUR(uint16_t fatTime) {
162
-  return fatTime >> 11;
163
-}
164
-/** minute part of FAT directory time field
136
+static inline uint8_t FAT_HOUR(uint16_t fatTime) { return fatTime >> 11; }
137
+
138
+/**
139
+ * minute part of FAT directory time field
165 140
  * \param[in] fatTime Time in packed dir format.
166 141
  *
167 142
  * \return Extracted minute [0,59]
168 143
  */
169
-static inline uint8_t FAT_MINUTE(uint16_t fatTime) {
170
-  return (fatTime >> 5) & 0x3F;
171
-}
172
-/** second part of FAT directory time field
144
+static inline uint8_t FAT_MINUTE(uint16_t fatTime) { return (fatTime >> 5) & 0x3F; }
145
+
146
+/**
147
+ * second part of FAT directory time field
173 148
  * Note second/2 is stored in packed time.
174 149
  *
175 150
  * \param[in] fatTime Time in packed dir format.
176 151
  *
177 152
  * \return Extracted second [0,58]
178 153
  */
179
-static inline uint8_t FAT_SECOND(uint16_t fatTime) {
180
-  return 2 * (fatTime & 0x1F);
181
-}
182
-/** Default date for file timestamps is 1 Jan 2000 */
154
+static inline uint8_t FAT_SECOND(uint16_t fatTime) { return 2 * (fatTime & 0x1F); }
155
+
156
+// Default date for file timestamps is 1 Jan 2000
183 157
 uint16_t const FAT_DEFAULT_DATE = ((2000 - 1980) << 9) | (1 << 5) | 1;
184
-/** Default time for file timestamp is 1 am */
158
+// Default time for file timestamp is 1 am
185 159
 uint16_t const FAT_DEFAULT_TIME = (1 << 11);
186
-//------------------------------------------------------------------------------
160
+
187 161
 /**
188 162
  * \class SdBaseFile
189 163
  * \brief Base class for SdFile with Print and C++ streams.
190 164
  */
191 165
 class SdBaseFile {
192 166
  public:
193
-  /** Create an instance. */
194 167
   SdBaseFile() : writeError(false), type_(FAT_FILE_TYPE_CLOSED) {}
195 168
   SdBaseFile(const char* path, uint8_t oflag);
196
-  ~SdBaseFile() {if (isOpen()) close();}
169
+  ~SdBaseFile() { if (isOpen()) close(); }
170
+
197 171
   /**
198 172
    * writeError is set to true if an error occurs during a write().
199 173
    * Set writeError to false before calling print() and/or write() and check
200 174
    * for true after calls to print() and/or write().
201 175
    */
202 176
   bool writeError;
203
-  //----------------------------------------------------------------------------
177
+
204 178
   // helpers for stream classes
205
-  /** get position for streams
179
+
180
+  /**
181
+   * get position for streams
206 182
    * \param[out] pos struct to receive position
207 183
    */
208 184
   void getpos(filepos_t* pos);
209
-  /** set position for streams
185
+
186
+  /**
187
+   * set position for streams
210 188
    * \param[out] pos struct with value for new position
211 189
    */
212 190
   void setpos(filepos_t* pos);
213
-  //----------------------------------------------------------------------------
191
+
214 192
   bool close();
215 193
   bool contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
216 194
   bool createContiguous(SdBaseFile* dirFile,
217 195
                         const char* path, uint32_t size);
218
-  /** \return The current cluster number for a file or directory. */
219
-  uint32_t curCluster() const {return curCluster_;}
220
-  /** \return The current position for a file or directory. */
221
-  uint32_t curPosition() const {return curPosition_;}
222
-  /** \return Current working directory */
223
-  static SdBaseFile* cwd() {return cwd_;}
224
-  /** Set the date/time callback function
196
+  /**
197
+   * \return The current cluster number for a file or directory.
198
+   */
199
+  uint32_t curCluster() const { return curCluster_; }
200
+
201
+  /**
202
+   * \return The current position for a file or directory.
203
+   */
204
+  uint32_t curPosition() const { return curPosition_; }
205
+
206
+  /**
207
+   * \return Current working directory
208
+   */
209
+  static SdBaseFile* cwd() { return cwd_; }
210
+
211
+  /**
212
+   * Set the date/time callback function
225 213
    *
226 214
    * \param[in] dateTime The user's call back function.  The callback
227 215
    * function is of the form:
@@ -252,35 +240,55 @@ class SdBaseFile {
252 240
     void (*dateTime)(uint16_t* date, uint16_t* time)) {
253 241
     dateTime_ = dateTime;
254 242
   }
255
-  /**  Cancel the date/time callback function. */
256
-  static void dateTimeCallbackCancel() {dateTime_ = 0;}
243
+
244
+  /**
245
+   * Cancel the date/time callback function.
246
+   */
247
+  static void dateTimeCallbackCancel() { dateTime_ = 0; }
257 248
   bool dirEntry(dir_t* dir);
258 249
   static void dirName(const dir_t& dir, char* name);
259 250
   bool exists(const char* name);
260 251
   int16_t fgets(char* str, int16_t num, char* delim = 0);
261
-  /** \return The total number of bytes in a file or directory. */
262
-  uint32_t fileSize() const {return fileSize_;}
263
-  /** \return The first cluster number for a file or directory. */
264
-  uint32_t firstCluster() const {return firstCluster_;}
265
-  bool getFilename(char* name);
266
-  /** \return True if this is a directory else false. */
267
-  bool isDir() const {return type_ >= FAT_FILE_TYPE_MIN_DIR;}
268
-  /** \return True if this is a normal file else false. */
269
-  bool isFile() const {return type_ == FAT_FILE_TYPE_NORMAL;}
270
-  /** \return True if this is an open file/directory else false. */
271
-  bool isOpen() const {return type_ != FAT_FILE_TYPE_CLOSED;}
272
-  /** \return True if this is a subdirectory else false. */
273
-  bool isSubDir() const {return type_ == FAT_FILE_TYPE_SUBDIR;}
274
-  /** \return True if this is the root directory. */
275
-  bool isRoot() const {
276
-    return type_ == FAT_FILE_TYPE_ROOT_FIXED || type_ == FAT_FILE_TYPE_ROOT32;
277
-  }
252
+
253
+  /**
254
+   * \return The total number of bytes in a file or directory.
255
+   */
256
+  uint32_t fileSize() const { return fileSize_; }
257
+
258
+  /**
259
+   * \return The first cluster number for a file or directory.
260
+   */
261
+  uint32_t firstCluster() const { return firstCluster_; }
262
+
263
+  /**
264
+   * \return True if this is a directory else false.
265
+   */
266
+  bool isDir() const { return type_ >= FAT_FILE_TYPE_MIN_DIR; }
267
+
268
+  /**
269
+   * \return True if this is a normal file else false.
270
+   */
271
+  bool isFile() const { return type_ == FAT_FILE_TYPE_NORMAL; }
272
+
273
+  /**
274
+   * \return True if this is an open file/directory else false.
275
+   */
276
+  bool isOpen() const { return type_ != FAT_FILE_TYPE_CLOSED; }
277
+
278
+  /**
279
+   * \return True if this is a subdirectory else false.
280
+   */
281
+  bool isSubDir() const { return type_ == FAT_FILE_TYPE_SUBDIR; }
282
+
283
+  /**
284
+   * \return True if this is the root directory.
285
+   */
286
+  bool isRoot() const { return type_ == FAT_FILE_TYPE_ROOT_FIXED || type_ == FAT_FILE_TYPE_ROOT32; }
287
+
288
+  bool getFilename(char * const name);
278 289
   void ls(uint8_t flags = 0, uint8_t indent = 0);
290
+
279 291
   bool mkdir(SdBaseFile* dir, const char* path, bool pFlag = true);
280
-  // alias for backward compactability
281
-  bool makeDir(SdBaseFile* dir, const char* path) {
282
-    return mkdir(dir, path, false);
283
-  }
284 292
   bool open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag);
285 293
   bool open(SdBaseFile* dirFile, const char* path, uint8_t oflag);
286 294
   bool open(const char* path, uint8_t oflag = O_READ);
@@ -295,53 +303,58 @@ class SdBaseFile {
295 303
   int8_t readDir(dir_t* dir, char* longFilename);
296 304
   static bool remove(SdBaseFile* dirFile, const char* path);
297 305
   bool remove();
298
-  /** Set the file's current position to zero. */
299
-  void rewind() {seekSet(0);}
306
+
307
+  /**
308
+   * Set the file's current position to zero.
309
+   */
310
+  void rewind() { seekSet(0); }
300 311
   bool rename(SdBaseFile* dirFile, const char* newPath);
301 312
   bool rmdir();
302
-  // for backward compatibility
303
-  bool rmDir() {return rmdir();}
304 313
   bool rmRfStar();
305
-  /** Set the files position to current position + \a pos. See seekSet().
314
+
315
+  /**
316
+   * Set the files position to current position + \a pos. See seekSet().
306 317
    * \param[in] offset The new position in bytes from the current position.
307 318
    * \return true for success or false for failure.
308 319
    */
309
-  bool seekCur(int32_t offset) {
310
-    return seekSet(curPosition_ + offset);
311
-  }
312
-  /** Set the files position to end-of-file + \a offset. See seekSet().
320
+  bool seekCur(const int32_t offset) { return seekSet(curPosition_ + offset); }
321
+
322
+  /**
323
+   * Set the files position to end-of-file + \a offset. See seekSet().
313 324
    * \param[in] offset The new position in bytes from end-of-file.
314 325
    * \return true for success or false for failure.
315 326
    */
316
-  bool seekEnd(int32_t offset = 0) {return seekSet(fileSize_ + offset);}
317
-  bool seekSet(uint32_t pos);
327
+  bool seekEnd(const int32_t offset = 0) { return seekSet(fileSize_ + offset); }
328
+  bool seekSet(const uint32_t pos);
318 329
   bool sync();
319 330
   bool timestamp(SdBaseFile* file);
320 331
   bool timestamp(uint8_t flag, uint16_t year, uint8_t month, uint8_t day,
321 332
                  uint8_t hour, uint8_t minute, uint8_t second);
322
-  /** Type of file.  You should use isFile() or isDir() instead of type()
323
-   * if possible.
333
+
334
+  /**
335
+   * Type of file. Use isFile() or isDir() instead of type() if possible.
324 336
    *
325 337
    * \return The file or directory type.
326 338
    */
327
-  uint8_t type() const {return type_;}
339
+  uint8_t type() const { return type_; }
328 340
   bool truncate(uint32_t size);
329
-  /** \return SdVolume that contains this file. */
330
-  SdVolume* volume() const {return vol_;}
341
+
342
+  /**
343
+   * \return SdVolume that contains this file.
344
+   */
345
+  SdVolume* volume() const { return vol_; }
331 346
   int16_t write(const void* buf, uint16_t nbyte);
332
-  //------------------------------------------------------------------------------
347
+
333 348
  private:
334
-  // allow SdFat to set cwd_
335
-  friend class SdFat;
336
-  // global pointer to cwd dir
337
-  static SdBaseFile* cwd_;
349
+  friend class SdFat;           // allow SdFat to set cwd_
350
+  static SdBaseFile* cwd_;      // global pointer to cwd dir
351
+
338 352
   // data time callback function
339 353
   static void (*dateTime_)(uint16_t* date, uint16_t* time);
354
+
340 355
   // bits defined in flags_
341
-  // should be 0x0F
342
-  static uint8_t const F_OFLAG = (O_ACCMODE | O_APPEND | O_SYNC);
343
-  // sync of directory entry required
344
-  static uint8_t const F_FILE_DIR_DIRTY = 0x80;
356
+  static uint8_t const F_OFLAG = (O_ACCMODE | O_APPEND | O_SYNC),   // should be 0x0F
357
+                       F_FILE_DIR_DIRTY = 0x80;                     // sync of directory entry required
345 358
 
346 359
   // private data
347 360
   uint8_t   flags_;         // See above for definition of flags_ bits
@@ -355,8 +368,11 @@ class SdBaseFile {
355 368
   uint32_t  firstCluster_;  // first cluster of file
356 369
   SdVolume* vol_;           // volume where file is located
357 370
 
358
-  /** experimental don't use */
359
-  bool openParent(SdBaseFile* dir);
371
+  /**
372
+   * EXPERIMENTAL - Don't use!
373
+   */
374
+  //bool openParent(SdBaseFile* dir);
375
+
360 376
   // private functions
361 377
   bool addCluster();
362 378
   bool addDirCluster();
@@ -367,61 +383,48 @@ class SdBaseFile {
367 383
   bool open(SdBaseFile* dirFile, const uint8_t dname[11], uint8_t oflag);
368 384
   bool openCachedEntry(uint8_t cacheIndex, uint8_t oflags);
369 385
   dir_t* readDirCache();
370
-  //------------------------------------------------------------------------------
371
-  // to be deleted
372
-  static void printDirName(const dir_t& dir,
373
-                           uint8_t width, bool printSlash);
374
-  //------------------------------------------------------------------------------
375
-  // Deprecated functions  - suppress cpplint warnings with NOLINT comment
376
-#if ALLOW_DEPRECATED_FUNCTIONS && !defined(DOXYGEN)
386
+
387
+// Deprecated functions
388
+#if ALLOW_DEPRECATED_FUNCTIONS
377 389
  public:
378
-  /** \deprecated Use:
390
+
391
+  /**
392
+   * \deprecated Use:
379 393
    * bool contiguousRange(uint32_t* bgnBlock, uint32_t* endBlock);
380 394
    * \param[out] bgnBlock the first block address for the file.
381 395
    * \param[out] endBlock the last  block address for the file.
382 396
    * \return true for success or false for failure.
383 397
    */
384
-  bool contiguousRange(uint32_t& bgnBlock, uint32_t& endBlock) {  // NOLINT
398
+  bool contiguousRange(uint32_t& bgnBlock, uint32_t& endBlock) {
385 399
     return contiguousRange(&bgnBlock, &endBlock);
386 400
   }
387
-  /** \deprecated Use:
388
-    * bool createContiguous(SdBaseFile* dirFile,
389
-    *   const char* path, uint32_t size)
390
-    * \param[in] dirFile The directory where the file will be created.
391
-    * \param[in] path A path with a valid DOS 8.3 file name.
392
-    * \param[in] size The desired file size.
393
-    * \return true for success or false for failure.
394
-    */
395
-  bool createContiguous(SdBaseFile& dirFile,  // NOLINT
396
-                        const char* path, uint32_t size) {
401
+
402
+  /**
403
+   * \deprecated Use:
404
+   * bool createContiguous(SdBaseFile* dirFile, const char* path, uint32_t size)
405
+   * \param[in] dirFile The directory where the file will be created.
406
+   * \param[in] path A path with a valid DOS 8.3 file name.
407
+   * \param[in] size The desired file size.
408
+   * \return true for success or false for failure.
409
+   */
410
+  bool createContiguous(SdBaseFile& dirFile, const char* path, uint32_t size) {
397 411
     return createContiguous(&dirFile, path, size);
398 412
   }
399
-  /** \deprecated Use:
413
+
414
+  /**
415
+   * \deprecated Use:
400 416
    * static void dateTimeCallback(
401 417
    *   void (*dateTime)(uint16_t* date, uint16_t* time));
402 418
    * \param[in] dateTime The user's call back function.
403 419
    */
404 420
   static void dateTimeCallback(
405
-    void (*dateTime)(uint16_t &date, uint16_t &time)) {  // NOLINT
421
+    void (*dateTime)(uint16_t &date, uint16_t &time)) {
406 422
     oldDateTime_ = dateTime;
407 423
     dateTime_ = dateTime ? oldToNew : 0;
408 424
   }
409
-  /** \deprecated Use: bool dirEntry(dir_t* dir);
410
-   * \param[out] dir Location for return of the file's directory entry.
411
-   * \return true for success or false for failure.
412
-   */
413
-  bool dirEntry(dir_t& dir) {return dirEntry(&dir);}  // NOLINT
414
-  /** \deprecated Use:
415
-   * bool mkdir(SdBaseFile* dir, const char* path);
416
-   * \param[in] dir An open SdFat instance for the directory that will contain
417
-   * the new directory.
418
-   * \param[in] path A path with a valid 8.3 DOS name for the new directory.
419
-   * \return true for success or false for failure.
420
-   */
421
-  bool mkdir(SdBaseFile& dir, const char* path) {  // NOLINT
422
-    return mkdir(&dir, path);
423
-  }
424
-  /** \deprecated Use:
425
+
426
+  /**
427
+   * \deprecated Use:
425 428
    * bool open(SdBaseFile* dirFile, const char* path, uint8_t oflag);
426 429
    * \param[in] dirFile An open SdFat instance for the directory containing the
427 430
    * file to be opened.
@@ -430,20 +433,23 @@ class SdBaseFile {
430 433
    * OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC.
431 434
    * \return true for success or false for failure.
432 435
    */
433
-  bool open(SdBaseFile& dirFile, // NOLINT
434
-            const char* path, uint8_t oflag) {
436
+  bool open(SdBaseFile& dirFile, const char* path, uint8_t oflag) {
435 437
     return open(&dirFile, path, oflag);
436 438
   }
437
-  /** \deprecated  Do not use in new apps
439
+
440
+  /**
441
+   * \deprecated  Do not use in new apps
438 442
    * \param[in] dirFile An open SdFat instance for the directory containing the
439 443
    * file to be opened.
440 444
    * \param[in] path A path with a valid 8.3 DOS name for a file to be opened.
441 445
    * \return true for success or false for failure.
442 446
    */
443
-  bool open(SdBaseFile& dirFile, const char* path) {  // NOLINT
447
+  bool open(SdBaseFile& dirFile, const char* path) {
444 448
     return open(dirFile, path, O_RDWR);
445 449
   }
446
-  /** \deprecated Use:
450
+
451
+  /**
452
+   * \deprecated Use:
447 453
    * bool open(SdBaseFile* dirFile, uint16_t index, uint8_t oflag);
448 454
    * \param[in] dirFile An open SdFat instance for the directory.
449 455
    * \param[in] index The \a index of the directory entry for the file to be
@@ -452,35 +458,39 @@ class SdBaseFile {
452 458
    * OR of flags O_READ, O_WRITE, O_TRUNC, and O_SYNC.
453 459
    * \return true for success or false for failure.
454 460
    */
455
-  bool open(SdBaseFile& dirFile, uint16_t index, uint8_t oflag) {  // NOLINT
461
+  bool open(SdBaseFile& dirFile, uint16_t index, uint8_t oflag) {
456 462
     return open(&dirFile, index, oflag);
457 463
   }
458
-  /** \deprecated Use: bool openRoot(SdVolume* vol);
464
+
465
+  /**
466
+   * \deprecated Use: bool openRoot(SdVolume* vol);
459 467
    * \param[in] vol The FAT volume containing the root directory to be opened.
460 468
    * \return true for success or false for failure.
461 469
    */
462
-  bool openRoot(SdVolume& vol) {return openRoot(&vol);}  // NOLINT
463
-  /** \deprecated Use: int8_t readDir(dir_t* dir);
470
+  bool openRoot(SdVolume& vol) { return openRoot(&vol); }
471
+
472
+  /**
473
+   * \deprecated Use: int8_t readDir(dir_t* dir);
464 474
    * \param[out] dir The dir_t struct that will receive the data.
465 475
    * \return bytes read for success zero for eof or -1 for failure.
466 476
    */
467
-  int8_t readDir(dir_t& dir, char* longFilename) {return readDir(&dir, longFilename);}  // NOLINT
468
-  /** \deprecated Use:
477
+  int8_t readDir(dir_t& dir, char* longFilename) {
478
+    return readDir(&dir, longFilename);
479
+  }
480
+
481
+  /**
482
+   * \deprecated Use:
469 483
    * static uint8_t remove(SdBaseFile* dirFile, const char* path);
470 484
    * \param[in] dirFile The directory that contains the file.
471 485
    * \param[in] path The name of the file to be removed.
472 486
    * \return true for success or false for failure.
473 487
    */
474
-  static bool remove(SdBaseFile& dirFile, const char* path) {  // NOLINT
475
-    return remove(&dirFile, path);
476
-  }
477
-  //------------------------------------------------------------------------------
478
-  // rest are private
488
+  static bool remove(SdBaseFile& dirFile, const char* path) { return remove(&dirFile, path); }
489
+
479 490
  private:
480
-  static void (*oldDateTime_)(uint16_t &date, uint16_t &time);  // NOLINT
481
-  static void oldToNew(uint16_t* date, uint16_t* time) {
482
-    uint16_t d;
483
-    uint16_t t;
491
+  static void (*oldDateTime_)(uint16_t &date, uint16_t &time);
492
+  static void oldToNew(uint16_t * const date, uint16_t * const time) {
493
+    uint16_t d, t;
484 494
     oldDateTime_(d, t);
485 495
     *date = d;
486 496
     *time = t;
@@ -488,5 +498,4 @@ class SdBaseFile {
488 498
 #endif  // ALLOW_DEPRECATED_FUNCTIONS
489 499
 };
490 500
 
491
-#endif  // SdBaseFile_h
492
-#endif
501
+#endif // _SDBASEFILE_H_

+ 0
- 18
Marlin/SdFatConfig.h View File

@@ -33,8 +33,6 @@
33 33
 
34 34
 #include "MarlinConfig.h"
35 35
 
36
-//------------------------------------------------------------------------------
37
-
38 36
 /**
39 37
  * To use multiple SD cards set USE_MULTIPLE_CARDS nonzero.
40 38
  *
@@ -44,8 +42,6 @@
44 42
  */
45 43
 #define USE_MULTIPLE_CARDS 0
46 44
 
47
-//------------------------------------------------------------------------------
48
-
49 45
 /**
50 46
  * Call flush for endl if ENDL_CALLS_FLUSH is nonzero
51 47
  *
@@ -65,39 +61,29 @@
65 61
  */
66 62
 #define ENDL_CALLS_FLUSH 0
67 63
 
68
-//------------------------------------------------------------------------------
69
-
70 64
 /**
71 65
  * Allow use of deprecated functions if ALLOW_DEPRECATED_FUNCTIONS is nonzero
72 66
  */
73 67
 #define ALLOW_DEPRECATED_FUNCTIONS 1
74 68
 
75
-//------------------------------------------------------------------------------
76
-
77 69
 /**
78 70
  * Allow FAT12 volumes if FAT12_SUPPORT is nonzero.
79 71
  * FAT12 has not been well tested.
80 72
  */
81 73
 #define FAT12_SUPPORT 0
82 74
 
83
-//------------------------------------------------------------------------------
84
-
85 75
 /**
86 76
  * SPI init rate for SD initialization commands. Must be 5 (F_CPU/64)
87 77
  * or 6 (F_CPU/128).
88 78
  */
89 79
 #define SPI_SD_INIT_RATE 5
90 80
 
91
-//------------------------------------------------------------------------------
92
-
93 81
 /**
94 82
  * Set the SS pin high for hardware SPI.  If SS is chip select for another SPI
95 83
  * device this will disable that device during the SD init phase.
96 84
  */
97 85
 #define SET_SPI_SS_HIGH 1
98 86
 
99
-//------------------------------------------------------------------------------
100
-
101 87
 /**
102 88
  * Define MEGA_SOFT_SPI nonzero to use software SPI on Mega Arduinos.
103 89
  * Pins used are SS 10, MOSI 11, MISO 12, and SCK 13.
@@ -108,8 +94,6 @@
108 94
  */
109 95
 #define MEGA_SOFT_SPI 0
110 96
 
111
-//------------------------------------------------------------------------------
112
-
113 97
 // Set USE_SOFTWARE_SPI nonzero to ALWAYS use Software SPI.
114 98
 #define USE_SOFTWARE_SPI 0
115 99
 
@@ -119,8 +103,6 @@
119 103
 #define SOFT_SPI_MISO_PIN 12 // Software SPI Master In Slave Out pin
120 104
 #define SOFT_SPI_SCK_PIN  13 // Software SPI Clock pin
121 105
 
122
-//------------------------------------------------------------------------------
123
-
124 106
 /**
125 107
  * The __cxa_pure_virtual function is an error handler that is invoked when
126 108
  * a pure virtual function is called.

+ 384
- 424
Marlin/SdFatStructs.h View File

@@ -21,34 +21,30 @@
21 21
  */
22 22
 
23 23
 /**
24
+ * \file
25
+ * \brief FAT file structures
26
+ */
27
+
28
+/**
24 29
  * Arduino SdFat Library
25 30
  * Copyright (C) 2009 by William Greiman
26 31
  *
27 32
  * This file is part of the Arduino Sd2Card Library
28 33
  */
29
-#include "Marlin.h"
30
-#if ENABLED(SDSUPPORT)
31
-
32
-#ifndef SdFatStructs_h
33
-#define SdFatStructs_h
34
+#ifndef SDFATSTRUCTS_H
35
+#define SDFATSTRUCTS_H
34 36
 
35 37
 #define PACKED __attribute__((__packed__))
36
-/**
37
- * \file
38
- * \brief FAT file structures
39
- */
38
+
40 39
 /**
41 40
  * mostly from Microsoft document fatgen103.doc
42 41
  * http://www.microsoft.com/whdc/system/platform/firmware/fatgen.mspx
43 42
  */
44
-//------------------------------------------------------------------------------
45
-/** Value for byte 510 of boot block or MBR */
46
-uint8_t const BOOTSIG0 = 0x55;
47
-/** Value for byte 511 of boot block or MBR */
48
-uint8_t const BOOTSIG1 = 0xAA;
49
-/** Value for bootSignature field int FAT/FAT32 boot sector */
50
-uint8_t const EXTENDED_BOOT_SIG = 0x29;
51
-//------------------------------------------------------------------------------
43
+
44
+uint8_t const BOOTSIG0 = 0x55,          // Value for byte 510 of boot block or MBR
45
+              BOOTSIG1 = 0xAA,          // Value for byte 511 of boot block or MBR
46
+              EXTENDED_BOOT_SIG = 0x29; // Value for bootSignature field int FAT/FAT32 boot sector
47
+
52 48
 /**
53 49
  * \struct partitionTable
54 50
  * \brief MBR partition table entry
@@ -57,59 +53,58 @@ uint8_t const EXTENDED_BOOT_SIG = 0x29;
57 53
  * The MBR partition table has four entries.
58 54
  */
59 55
 struct partitionTable {
60
-          /**
61
-           * Boot Indicator . Indicates whether the volume is the active
62
-           * partition.  Legal values include: 0x00. Do not use for booting.
63
-           * 0x80 Active partition.
64
-           */
56
+  /**
57
+   * Boot Indicator . Indicates whether the volume is the active
58
+   * partition.  Legal values include: 0x00. Do not use for booting.
59
+   * 0x80 Active partition.
60
+   */
65 61
   uint8_t  boot;
66
-          /**
67
-           * Head part of Cylinder-head-sector address of the first block in
68
-           * the partition. Legal values are 0-255. Only used in old PC BIOS.
69
-           */
62
+  /**
63
+   * Head part of Cylinder-head-sector address of the first block in
64
+   * the partition. Legal values are 0-255. Only used in old PC BIOS.
65
+   */
70 66
   uint8_t  beginHead;
71
-          /**
72
-           * Sector part of Cylinder-head-sector address of the first block in
73
-           * the partition. Legal values are 1-63. Only used in old PC BIOS.
74
-           */
67
+  /**
68
+   * Sector part of Cylinder-head-sector address of the first block in
69
+   * the partition. Legal values are 1-63. Only used in old PC BIOS.
70
+   */
75 71
   unsigned beginSector : 6;
76
-           /** High bits cylinder for first block in partition. */
72
+  /** High bits cylinder for first block in partition. */
77 73
   unsigned beginCylinderHigh : 2;
78
-          /**
79
-           * Combine beginCylinderLow with beginCylinderHigh. Legal values
80
-           * are 0-1023.  Only used in old PC BIOS.
81
-           */
74
+  /**
75
+   * Combine beginCylinderLow with beginCylinderHigh. Legal values
76
+   * are 0-1023.  Only used in old PC BIOS.
77
+   */
82 78
   uint8_t  beginCylinderLow;
83
-          /**
84
-           * Partition type. See defines that begin with PART_TYPE_ for
85
-           * some Microsoft partition types.
86
-           */
79
+  /**
80
+   * Partition type. See defines that begin with PART_TYPE_ for
81
+   * some Microsoft partition types.
82
+   */
87 83
   uint8_t  type;
88
-          /**
89
-           * head part of cylinder-head-sector address of the last sector in the
90
-           * partition.  Legal values are 0-255. Only used in old PC BIOS.
91
-           */
84
+  /**
85
+   * head part of cylinder-head-sector address of the last sector in the
86
+   * partition.  Legal values are 0-255. Only used in old PC BIOS.
87
+   */
92 88
   uint8_t  endHead;
93
-          /**
94
-           * Sector part of cylinder-head-sector address of the last sector in
95
-           * the partition.  Legal values are 1-63. Only used in old PC BIOS.
96
-           */
89
+  /**
90
+   * Sector part of cylinder-head-sector address of the last sector in
91
+   * the partition.  Legal values are 1-63. Only used in old PC BIOS.
92
+   */
97 93
   unsigned endSector : 6;
98
-           /** High bits of end cylinder */
94
+  /** High bits of end cylinder */
99 95
   unsigned endCylinderHigh : 2;
100
-          /**
101
-           * Combine endCylinderLow with endCylinderHigh. Legal values
102
-           * are 0-1023.  Only used in old PC BIOS.
103
-           */
96
+  /**
97
+   * Combine endCylinderLow with endCylinderHigh. Legal values
98
+   * are 0-1023.  Only used in old PC BIOS.
99
+   */
104 100
   uint8_t  endCylinderLow;
105
-           /** Logical block address of the first block in the partition. */
106
-  uint32_t firstSector;
107
-           /** Length of the partition, in blocks. */
108
-  uint32_t totalSectors;
101
+
102
+  uint32_t firstSector;   // Logical block address of the first block in the partition.
103
+  uint32_t totalSectors;  // Length of the partition, in blocks.
109 104
 } PACKED;
110
-/** Type name for partitionTable */
111
-typedef struct partitionTable part_t;
112
-//------------------------------------------------------------------------------
105
+
106
+typedef struct partitionTable part_t; // Type name for partitionTable
107
+
113 108
 /**
114 109
  * \struct masterBootRecord
115 110
  *
@@ -118,22 +113,16 @@ typedef struct partitionTable part_t;
118 113
  * The first block of a storage device that is formatted with a MBR.
119 114
  */
120 115
 struct masterBootRecord {
121
-           /** Code Area for master boot program. */
122
-  uint8_t  codeArea[440];
123
-           /** Optional Windows NT disk signature. May contain boot code. */
124
-  uint32_t diskSignature;
125
-           /** Usually zero but may be more boot code. */
126
-  uint16_t usuallyZero;
127
-           /** Partition tables. */
128
-  part_t   part[4];
129
-           /** First MBR signature byte. Must be 0x55 */
130
-  uint8_t  mbrSig0;
131
-           /** Second MBR signature byte. Must be 0xAA */
132
-  uint8_t  mbrSig1;
116
+  uint8_t  codeArea[440]; // Code Area for master boot program.
117
+  uint32_t diskSignature; // Optional Windows NT disk signature. May contain boot code.
118
+  uint16_t usuallyZero;   // Usually zero but may be more boot code.
119
+  part_t   part[4];       // Partition tables.
120
+  uint8_t  mbrSig0;       // First MBR signature byte. Must be 0x55
121
+  uint8_t  mbrSig1;       // Second MBR signature byte. Must be 0xAA
133 122
 } PACKED;
134 123
 /** Type name for masterBootRecord */
135 124
 typedef struct masterBootRecord mbr_t;
136
-//------------------------------------------------------------------------------
125
+
137 126
 /**
138 127
  * \struct fat_boot
139 128
  *
@@ -141,285 +130,280 @@ typedef struct masterBootRecord mbr_t;
141 130
  *
142 131
  */
143 132
 struct fat_boot {
144
-         /**
145
-          * The first three bytes of the boot sector must be valid,
146
-          * executable x 86-based CPU instructions. This includes a
147
-          * jump instruction that skips the next nonexecutable bytes.
148
-          */
133
+  /**
134
+   * The first three bytes of the boot sector must be valid,
135
+   * executable x 86-based CPU instructions. This includes a
136
+   * jump instruction that skips the next nonexecutable bytes.
137
+   */
149 138
   uint8_t jump[3];
150
-         /**
151
-          * This is typically a string of characters that identifies
152
-          * the operating system that formatted the volume.
153
-          */
139
+  /**
140
+   * This is typically a string of characters that identifies
141
+   * the operating system that formatted the volume.
142
+   */
154 143
   char    oemId[8];
155
-          /**
156
-           * The size of a hardware sector. Valid decimal values for this
157
-           * field are 512, 1024, 2048, and 4096. For most disks used in
158
-           * the United States, the value of this field is 512.
159
-           */
144
+  /**
145
+   * The size of a hardware sector. Valid decimal values for this
146
+   * field are 512, 1024, 2048, and 4096. For most disks used in
147
+   * the United States, the value of this field is 512.
148
+   */
160 149
   uint16_t bytesPerSector;
161
-          /**
162
-           * Number of sectors per allocation unit. This value must be a
163
-           * power of 2 that is greater than 0. The legal values are
164
-           * 1, 2, 4, 8, 16, 32, 64, and 128.  128 should be avoided.
165
-           */
150
+  /**
151
+   * Number of sectors per allocation unit. This value must be a
152
+   * power of 2 that is greater than 0. The legal values are
153
+   * 1, 2, 4, 8, 16, 32, 64, and 128.  128 should be avoided.
154
+   */
166 155
   uint8_t  sectorsPerCluster;
167
-          /**
168
-           * The number of sectors preceding the start of the first FAT,
169
-           * including the boot sector. The value of this field is always 1.
170
-           */
156
+  /**
157
+   * The number of sectors preceding the start of the first FAT,
158
+   * including the boot sector. The value of this field is always 1.
159
+   */
171 160
   uint16_t reservedSectorCount;
172
-          /**
173
-           * The number of copies of the FAT on the volume.
174
-           * The value of this field is always 2.
175
-           */
161
+  /**
162
+   * The number of copies of the FAT on the volume.
163
+   * The value of this field is always 2.
164
+   */
176 165
   uint8_t  fatCount;
177
-          /**
178
-           * For FAT12 and FAT16 volumes, this field contains the count of
179
-           * 32-byte directory entries in the root directory. For FAT32 volumes,
180
-           * this field must be set to 0. For FAT12 and FAT16 volumes, this
181
-           * value should always specify a count that when multiplied by 32
182
-           * results in a multiple of bytesPerSector.  FAT16 volumes should
183
-           * use the value 512.
184
-           */
166
+  /**
167
+   * For FAT12 and FAT16 volumes, this field contains the count of
168
+   * 32-byte directory entries in the root directory. For FAT32 volumes,
169
+   * this field must be set to 0. For FAT12 and FAT16 volumes, this
170
+   * value should always specify a count that when multiplied by 32
171
+   * results in a multiple of bytesPerSector.  FAT16 volumes should
172
+   * use the value 512.
173
+   */
185 174
   uint16_t rootDirEntryCount;
186
-          /**
187
-           * This field is the old 16-bit total count of sectors on the volume.
188
-           * This count includes the count of all sectors in all four regions
189
-           * of the volume. This field can be 0; if it is 0, then totalSectors32
190
-           * must be nonzero.  For FAT32 volumes, this field must be 0. For
191
-           * FAT12 and FAT16 volumes, this field contains the sector count, and
192
-           * totalSectors32 is 0 if the total sector count fits
193
-           * (is less than 0x10000).
194
-           */
175
+  /**
176
+   * This field is the old 16-bit total count of sectors on the volume.
177
+   * This count includes the count of all sectors in all four regions
178
+   * of the volume. This field can be 0; if it is 0, then totalSectors32
179
+   * must be nonzero.  For FAT32 volumes, this field must be 0. For
180
+   * FAT12 and FAT16 volumes, this field contains the sector count, and
181
+   * totalSectors32 is 0 if the total sector count fits
182
+   * (is less than 0x10000).
183
+   */
195 184
   uint16_t totalSectors16;
196
-          /**
197
-           * This dates back to the old MS-DOS 1.x media determination and is
198
-           * no longer usually used for anything.  0xF8 is the standard value
199
-           * for fixed (nonremovable) media. For removable media, 0xF0 is
200
-           * frequently used. Legal values are 0xF0 or 0xF8-0xFF.
201
-           */
185
+  /**
186
+   * This dates back to the old MS-DOS 1.x media determination and is
187
+   * no longer usually used for anything.  0xF8 is the standard value
188
+   * for fixed (nonremovable) media. For removable media, 0xF0 is
189
+   * frequently used. Legal values are 0xF0 or 0xF8-0xFF.
190
+   */
202 191
   uint8_t  mediaType;
203
-          /**
204
-           * Count of sectors occupied by one FAT on FAT12/FAT16 volumes.
205
-           * On FAT32 volumes this field must be 0, and sectorsPerFat32
206
-           * contains the FAT size count.
207
-           */
192
+  /**
193
+   * Count of sectors occupied by one FAT on FAT12/FAT16 volumes.
194
+   * On FAT32 volumes this field must be 0, and sectorsPerFat32
195
+   * contains the FAT size count.
196
+   */
208 197
   uint16_t sectorsPerFat16;
209
-           /** Sectors per track for interrupt 0x13. Not used otherwise. */
210
-  uint16_t sectorsPerTrack;
211
-           /** Number of heads for interrupt 0x13.  Not used otherwise. */
212
-  uint16_t headCount;
213
-          /**
214
-           * Count of hidden sectors preceding the partition that contains this
215
-           * FAT volume. This field is generally only relevant for media
216
-           * visible on interrupt 0x13.
217
-           */
198
+
199
+  uint16_t sectorsPerTrack; // Sectors per track for interrupt 0x13. Not used otherwise.
200
+  uint16_t headCount;       // Number of heads for interrupt 0x13. Not used otherwise.
201
+
202
+  /**
203
+   * Count of hidden sectors preceding the partition that contains this
204
+   * FAT volume. This field is generally only relevant for media
205
+   * visible on interrupt 0x13.
206
+   */
218 207
   uint32_t hidddenSectors;
219
-          /**
220
-           * This field is the new 32-bit total count of sectors on the volume.
221
-           * This count includes the count of all sectors in all four regions
222
-           * of the volume.  This field can be 0; if it is 0, then
223
-           * totalSectors16 must be nonzero.
224
-           */
208
+  /**
209
+   * This field is the new 32-bit total count of sectors on the volume.
210
+   * This count includes the count of all sectors in all four regions
211
+   * of the volume.  This field can be 0; if it is 0, then
212
+   * totalSectors16 must be nonzero.
213
+   */
225 214
   uint32_t totalSectors32;
226
-           /**
227
-            * Related to the BIOS physical drive number. Floppy drives are
228
-            * identified as 0x00 and physical hard disks are identified as
229
-            * 0x80, regardless of the number of physical disk drives.
230
-            * Typically, this value is set prior to issuing an INT 13h BIOS
231
-            * call to specify the device to access. The value is only
232
-            * relevant if the device is a boot device.
233
-            */
215
+  /**
216
+   * Related to the BIOS physical drive number. Floppy drives are
217
+   * identified as 0x00 and physical hard disks are identified as
218
+   * 0x80, regardless of the number of physical disk drives.
219
+   * Typically, this value is set prior to issuing an INT 13h BIOS
220
+   * call to specify the device to access. The value is only
221
+   * relevant if the device is a boot device.
222
+   */
234 223
   uint8_t  driveNumber;
235
-           /** used by Windows NT - should be zero for FAT */
236
-  uint8_t  reserved1;
237
-           /** 0x29 if next three fields are valid */
238
-  uint8_t  bootSignature;
239
-           /**
240
-            * A random serial number created when formatting a disk,
241
-            * which helps to distinguish between disks.
242
-            * Usually generated by combining date and time.
243
-            */
224
+
225
+  uint8_t  reserved1;       // used by Windows NT - should be zero for FAT
226
+  uint8_t  bootSignature;   // 0x29 if next three fields are valid
227
+
228
+  /**
229
+   * A random serial number created when formatting a disk,
230
+   * which helps to distinguish between disks.
231
+   * Usually generated by combining date and time.
232
+   */
244 233
   uint32_t volumeSerialNumber;
245
-           /**
246
-            * A field once used to store the volume label. The volume label
247
-            * is now stored as a special file in the root directory.
248
-            */
234
+  /**
235
+   * A field once used to store the volume label. The volume label
236
+   * is now stored as a special file in the root directory.
237
+   */
249 238
   char     volumeLabel[11];
250
-           /**
251
-            * A field with a value of either FAT, FAT12 or FAT16,
252
-            * depending on the disk format.
253
-            */
239
+  /**
240
+   * A field with a value of either FAT, FAT12 or FAT16,
241
+   * depending on the disk format.
242
+   */
254 243
   char     fileSystemType[8];
255
-           /** X86 boot code */
256
-  uint8_t  bootCode[448];
257
-           /** must be 0x55 */
258
-  uint8_t  bootSectorSig0;
259
-           /** must be 0xAA */
260
-  uint8_t  bootSectorSig1;
244
+
245
+  uint8_t  bootCode[448];   // X86 boot code
246
+  uint8_t  bootSectorSig0;  // must be 0x55
247
+  uint8_t  bootSectorSig1;  // must be 0xAA
261 248
 } PACKED;
262
-/** Type name for FAT Boot Sector */
263
-typedef struct fat_boot fat_boot_t;
264
-//------------------------------------------------------------------------------
249
+
250
+typedef struct fat_boot fat_boot_t;   // Type name for FAT Boot Sector
251
+
265 252
 /**
266 253
  * \struct fat32_boot
267 254
  *
268 255
  * \brief Boot sector for a FAT32 volume.
269
- *
270 256
  */
271 257
 struct fat32_boot {
272
-         /**
273
-          * The first three bytes of the boot sector must be valid,
274
-          * executable x 86-based CPU instructions. This includes a
275
-          * jump instruction that skips the next nonexecutable bytes.
276
-          */
258
+  /**
259
+   * The first three bytes of the boot sector must be valid,
260
+   * executable x 86-based CPU instructions. This includes a
261
+   * jump instruction that skips the next nonexecutable bytes.
262
+   */
277 263
   uint8_t jump[3];
278
-         /**
279
-          * This is typically a string of characters that identifies
280
-          * the operating system that formatted the volume.
281
-          */
264
+  /**
265
+   * This is typically a string of characters that identifies
266
+   * the operating system that formatted the volume.
267
+   */
282 268
   char    oemId[8];
283
-          /**
284
-           * The size of a hardware sector. Valid decimal values for this
285
-           * field are 512, 1024, 2048, and 4096. For most disks used in
286
-           * the United States, the value of this field is 512.
287
-           */
269
+  /**
270
+   * The size of a hardware sector. Valid decimal values for this
271
+   * field are 512, 1024, 2048, and 4096. For most disks used in
272
+   * the United States, the value of this field is 512.
273
+   */
288 274
   uint16_t bytesPerSector;
289
-          /**
290
-           * Number of sectors per allocation unit. This value must be a
291
-           * power of 2 that is greater than 0. The legal values are
292
-           * 1, 2, 4, 8, 16, 32, 64, and 128.  128 should be avoided.
293
-           */
275
+  /**
276
+   * Number of sectors per allocation unit. This value must be a
277
+   * power of 2 that is greater than 0. The legal values are
278
+   * 1, 2, 4, 8, 16, 32, 64, and 128.  128 should be avoided.
279
+   */
294 280
   uint8_t  sectorsPerCluster;
295
-          /**
296
-           * The number of sectors preceding the start of the first FAT,
297
-           * including the boot sector. Must not be zero
298
-           */
281
+  /**
282
+   * The number of sectors preceding the start of the first FAT,
283
+   * including the boot sector. Must not be zero
284
+   */
299 285
   uint16_t reservedSectorCount;
300
-          /**
301
-           * The number of copies of the FAT on the volume.
302
-           * The value of this field is always 2.
303
-           */
286
+  /**
287
+   * The number of copies of the FAT on the volume.
288
+   * The value of this field is always 2.
289
+   */
304 290
   uint8_t  fatCount;
305
-          /**
306
-           * FAT12/FAT16 only. For FAT32 volumes, this field must be set to 0.
307
-           */
291
+  /**
292
+   * FAT12/FAT16 only. For FAT32 volumes, this field must be set to 0.
293
+   */
308 294
   uint16_t rootDirEntryCount;
309
-          /**
310
-           * For FAT32 volumes, this field must be 0.
311
-           */
295
+  /**
296
+   * For FAT32 volumes, this field must be 0.
297
+   */
312 298
   uint16_t totalSectors16;
313
-          /**
314
-           * This dates back to the old MS-DOS 1.x media determination and is
315
-           * no longer usually used for anything.  0xF8 is the standard value
316
-           * for fixed (nonremovable) media. For removable media, 0xF0 is
317
-           * frequently used. Legal values are 0xF0 or 0xF8-0xFF.
318
-           */
299
+  /**
300
+   * This dates back to the old MS-DOS 1.x media determination and is
301
+   * no longer usually used for anything.  0xF8 is the standard value
302
+   * for fixed (nonremovable) media. For removable media, 0xF0 is
303
+   * frequently used. Legal values are 0xF0 or 0xF8-0xFF.
304
+   */
319 305
   uint8_t  mediaType;
320
-          /**
321
-           * On FAT32 volumes this field must be 0, and sectorsPerFat32
322
-           * contains the FAT size count.
323
-           */
306
+  /**
307
+   * On FAT32 volumes this field must be 0, and sectorsPerFat32
308
+   * contains the FAT size count.
309
+   */
324 310
   uint16_t sectorsPerFat16;
325
-           /** Sectors per track for interrupt 0x13. Not used otherwise. */
326
-  uint16_t sectorsPerTrack;
327
-           /** Number of heads for interrupt 0x13.  Not used otherwise. */
328
-  uint16_t headCount;
329
-          /**
330
-           * Count of hidden sectors preceding the partition that contains this
331
-           * FAT volume. This field is generally only relevant for media
332
-           * visible on interrupt 0x13.
333
-           */
311
+
312
+  uint16_t sectorsPerTrack; // Sectors per track for interrupt 0x13. Not used otherwise.
313
+  uint16_t headCount;       // Number of heads for interrupt 0x13. Not used otherwise.
314
+
315
+  /**
316
+   * Count of hidden sectors preceding the partition that contains this
317
+   * FAT volume. This field is generally only relevant for media
318
+   * visible on interrupt 0x13.
319
+   */
334 320
   uint32_t hidddenSectors;
335
-          /**
336
-           * Contains the total number of sectors in the FAT32 volume.
337
-           */
321
+  /**
322
+   * Contains the total number of sectors in the FAT32 volume.
323
+   */
338 324
   uint32_t totalSectors32;
339
-         /**
340
-           * Count of sectors occupied by one FAT on FAT32 volumes.
341
-           */
325
+  /**
326
+   * Count of sectors occupied by one FAT on FAT32 volumes.
327
+   */
342 328
   uint32_t sectorsPerFat32;
343
-          /**
344
-           * This field is only defined for FAT32 media and does not exist on
345
-           * FAT12 and FAT16 media.
346
-           * Bits 0-3 -- Zero-based number of active FAT.
347
-           *             Only valid if mirroring is disabled.
348
-           * Bits 4-6 -- Reserved.
349
-           * Bit 7  -- 0 means the FAT is mirrored at runtime into all FATs.
350
-           *        -- 1 means only one FAT is active; it is the one referenced
351
-           *             in bits 0-3.
352
-           * Bits 8-15  -- Reserved.
353
-           */
329
+  /**
330
+   * This field is only defined for FAT32 media and does not exist on
331
+   * FAT12 and FAT16 media.
332
+   * Bits 0-3 -- Zero-based number of active FAT.
333
+   *             Only valid if mirroring is disabled.
334
+   * Bits 4-6 -- Reserved.
335
+   * Bit 7  -- 0 means the FAT is mirrored at runtime into all FATs.
336
+   *        -- 1 means only one FAT is active; it is the one referenced
337
+   *             in bits 0-3.
338
+   * Bits 8-15  -- Reserved.
339
+   */
354 340
   uint16_t fat32Flags;
355
-          /**
356
-           * FAT32 version. High byte is major revision number.
357
-           * Low byte is minor revision number. Only 0.0 define.
358
-           */
341
+  /**
342
+   * FAT32 version. High byte is major revision number.
343
+   * Low byte is minor revision number. Only 0.0 define.
344
+   */
359 345
   uint16_t fat32Version;
360
-          /**
361
-           * Cluster number of the first cluster of the root directory for FAT32.
362
-           * This usually 2 but not required to be 2.
363
-           */
346
+  /**
347
+   * Cluster number of the first cluster of the root directory for FAT32.
348
+   * This usually 2 but not required to be 2.
349
+   */
364 350
   uint32_t fat32RootCluster;
365
-          /**
366
-           * Sector number of FSINFO structure in the reserved area of the
367
-           * FAT32 volume. Usually 1.
368
-           */
351
+  /**
352
+   * Sector number of FSINFO structure in the reserved area of the
353
+   * FAT32 volume. Usually 1.
354
+   */
369 355
   uint16_t fat32FSInfo;
370
-          /**
371
-           * If nonzero, indicates the sector number in the reserved area
372
-           * of the volume of a copy of the boot record. Usually 6.
373
-           * No value other than 6 is recommended.
374
-           */
356
+  /**
357
+   * If nonzero, indicates the sector number in the reserved area
358
+   * of the volume of a copy of the boot record. Usually 6.
359
+   * No value other than 6 is recommended.
360
+   */
375 361
   uint16_t fat32BackBootBlock;
376
-          /**
377
-           * Reserved for future expansion. Code that formats FAT32 volumes
378
-           * should always set all of the bytes of this field to 0.
379
-           */
362
+  /**
363
+   * Reserved for future expansion. Code that formats FAT32 volumes
364
+   * should always set all of the bytes of this field to 0.
365
+   */
380 366
   uint8_t  fat32Reserved[12];
381
-           /**
382
-            * Related to the BIOS physical drive number. Floppy drives are
383
-            * identified as 0x00 and physical hard disks are identified as
384
-            * 0x80, regardless of the number of physical disk drives.
385
-            * Typically, this value is set prior to issuing an INT 13h BIOS
386
-            * call to specify the device to access. The value is only
387
-            * relevant if the device is a boot device.
388
-            */
367
+  /**
368
+   * Related to the BIOS physical drive number. Floppy drives are
369
+   * identified as 0x00 and physical hard disks are identified as
370
+   * 0x80, regardless of the number of physical disk drives.
371
+   * Typically, this value is set prior to issuing an INT 13h BIOS
372
+   * call to specify the device to access. The value is only
373
+   * relevant if the device is a boot device.
374
+   */
389 375
   uint8_t  driveNumber;
390
-           /** used by Windows NT - should be zero for FAT */
391
-  uint8_t  reserved1;
392
-           /** 0x29 if next three fields are valid */
393
-  uint8_t  bootSignature;
394
-           /**
395
-            * A random serial number created when formatting a disk,
396
-            * which helps to distinguish between disks.
397
-            * Usually generated by combining date and time.
398
-            */
376
+
377
+  uint8_t  reserved1;       // Used by Windows NT - should be zero for FAT
378
+  uint8_t  bootSignature;   // 0x29 if next three fields are valid
379
+
380
+  /**
381
+   * A random serial number created when formatting a disk,
382
+   * which helps to distinguish between disks.
383
+   * Usually generated by combining date and time.
384
+   */
399 385
   uint32_t volumeSerialNumber;
400
-           /**
401
-            * A field once used to store the volume label. The volume label
402
-            * is now stored as a special file in the root directory.
403
-            */
386
+  /**
387
+   * A field once used to store the volume label. The volume label
388
+   * is now stored as a special file in the root directory.
389
+   */
404 390
   char     volumeLabel[11];
405
-           /**
406
-            * A text field with a value of FAT32.
407
-            */
391
+  /**
392
+   * A text field with a value of FAT32.
393
+   */
408 394
   char     fileSystemType[8];
409
-           /** X86 boot code */
410
-  uint8_t  bootCode[420];
411
-           /** must be 0x55 */
412
-  uint8_t  bootSectorSig0;
413
-           /** must be 0xAA */
414
-  uint8_t  bootSectorSig1;
395
+
396
+  uint8_t  bootCode[420];   // X86 boot code
397
+  uint8_t  bootSectorSig0;  // must be 0x55
398
+  uint8_t  bootSectorSig1;  // must be 0xAA
399
+
415 400
 } PACKED;
416
-/** Type name for FAT32 Boot Sector */
417
-typedef struct fat32_boot fat32_boot_t;
418
-//------------------------------------------------------------------------------
419
-/** Lead signature for a FSINFO sector */
420
-uint32_t const FSINFO_LEAD_SIG = 0x41615252;
421
-/** Struct signature for a FSINFO sector */
422
-uint32_t const FSINFO_STRUCT_SIG = 0x61417272;
401
+
402
+typedef struct fat32_boot fat32_boot_t; // Type name for FAT32 Boot Sector
403
+
404
+uint32_t const FSINFO_LEAD_SIG   = 0x41615252,  // 'AaRR' Lead signature for a FSINFO sector
405
+               FSINFO_STRUCT_SIG = 0x61417272;  // 'aArr' Struct signature for a FSINFO sector
406
+
423 407
 /**
424 408
  * \struct fat32_fsinfo
425 409
  *
@@ -427,12 +411,9 @@ uint32_t const FSINFO_STRUCT_SIG = 0x61417272;
427 411
  *
428 412
  */
429 413
 struct fat32_fsinfo {
430
-           /** must be 0x52, 0x52, 0x61, 0x41 */
431
-  uint32_t  leadSignature;
432
-           /** must be zero */
433
-  uint8_t  reserved1[480];
434
-           /** must be 0x72, 0x72, 0x41, 0x61 */
435
-  uint32_t  structSignature;
414
+  uint32_t  leadSignature;    // must be 0x52, 0x52, 0x61, 0x41 'RRaA'
415
+  uint8_t  reserved1[480];    // must be zero
416
+  uint32_t  structSignature;  // must be 0x72, 0x72, 0x41, 0x61 'rrAa'
436 417
           /**
437 418
            * Contains the last known free cluster count on the volume.
438 419
            * If the value is 0xFFFFFFFF, then the free count is unknown
@@ -448,30 +429,22 @@ struct fat32_fsinfo {
448 429
            * should start looking at cluster 2.
449 430
            */
450 431
   uint32_t nextFree;
451
-           /** must be zero */
452
-  uint8_t  reserved2[12];
453
-           /** must be 0x00, 0x00, 0x55, 0xAA */
454
-  uint8_t  tailSignature[4];
432
+
433
+  uint8_t  reserved2[12];     // must be zero
434
+  uint8_t  tailSignature[4];  // must be 0x00, 0x00, 0x55, 0xAA
455 435
 } PACKED;
456
-/** Type name for FAT32 FSINFO Sector */
457
-typedef struct fat32_fsinfo fat32_fsinfo_t;
458
-//------------------------------------------------------------------------------
436
+
437
+typedef struct fat32_fsinfo fat32_fsinfo_t; // Type name for FAT32 FSINFO Sector
438
+
459 439
 // End Of Chain values for FAT entries
460
-/** FAT12 end of chain value used by Microsoft. */
461
-uint16_t const FAT12EOC = 0xFFF;
462
-/** Minimum value for FAT12 EOC.  Use to test for EOC. */
463
-uint16_t const FAT12EOC_MIN = 0xFF8;
464
-/** FAT16 end of chain value used by Microsoft. */
465
-uint16_t const FAT16EOC = 0xFFFF;
466
-/** Minimum value for FAT16 EOC.  Use to test for EOC. */
467
-uint16_t const FAT16EOC_MIN = 0xFFF8;
468
-/** FAT32 end of chain value used by Microsoft. */
469
-uint32_t const FAT32EOC = 0x0FFFFFFF;
470
-/** Minimum value for FAT32 EOC.  Use to test for EOC. */
471
-uint32_t const FAT32EOC_MIN = 0x0FFFFFF8;
472
-/** Mask a for FAT32 entry. Entries are 28 bits. */
473
-uint32_t const FAT32MASK = 0x0FFFFFFF;
474
-//------------------------------------------------------------------------------
440
+uint16_t const FAT12EOC = 0xFFF,          // FAT12 end of chain value used by Microsoft.
441
+               FAT12EOC_MIN = 0xFF8,      // Minimum value for FAT12 EOC.  Use to test for EOC.
442
+               FAT16EOC = 0xFFFF,         // FAT16 end of chain value used by Microsoft.
443
+               FAT16EOC_MIN = 0xFFF8;     // Minimum value for FAT16 EOC.  Use to test for EOC.
444
+uint32_t const FAT32EOC = 0x0FFFFFFF,     // FAT32 end of chain value used by Microsoft.
445
+               FAT32EOC_MIN = 0x0FFFFFF8, // Minimum value for FAT32 EOC.  Use to test for EOC.
446
+               FAT32MASK = 0x0FFFFFFF;    // Mask a for FAT32 entry. Entries are 28 bits.
447
+
475 448
 /**
476 449
  * \struct directoryEntry
477 450
  * \brief FAT short directory entry
@@ -503,54 +476,54 @@ uint32_t const FAT32MASK = 0x0FFFFFFF;
503 476
  * The valid time range is from Midnight 00:00:00 to 23:59:58.
504 477
  */
505 478
 struct directoryEntry {
506
-           /** Short 8.3 name.
507
-            *
508
-            * The first eight bytes contain the file name with blank fill.
509
-            * The last three bytes contain the file extension with blank fill.
510
-            */
479
+  /**
480
+   * Short 8.3 name.
481
+   *
482
+   * The first eight bytes contain the file name with blank fill.
483
+   * The last three bytes contain the file extension with blank fill.
484
+   */
511 485
   uint8_t  name[11];
512
-          /** Entry attributes.
513
-           *
514
-           * The upper two bits of the attribute byte are reserved and should
515
-           * always be set to 0 when a file is created and never modified or
516
-           * looked at after that.  See defines that begin with DIR_ATT_.
517
-           */
486
+  /**
487
+   * Entry attributes.
488
+   *
489
+   * The upper two bits of the attribute byte are reserved and should
490
+   * always be set to 0 when a file is created and never modified or
491
+   * looked at after that.  See defines that begin with DIR_ATT_.
492
+   */
518 493
   uint8_t  attributes;
519
-          /**
520
-           * Reserved for use by Windows NT. Set value to 0 when a file is
521
-           * created and never modify or look at it after that.
522
-           */
494
+  /**
495
+   * Reserved for use by Windows NT. Set value to 0 when a file is
496
+   * created and never modify or look at it after that.
497
+   */
523 498
   uint8_t  reservedNT;
524
-          /**
525
-           * The granularity of the seconds part of creationTime is 2 seconds
526
-           * so this field is a count of tenths of a second and it's valid
527
-           * value range is 0-199 inclusive. (WHG note - seems to be hundredths)
528
-           */
499
+  /**
500
+   * The granularity of the seconds part of creationTime is 2 seconds
501
+   * so this field is a count of tenths of a second and it's valid
502
+   * value range is 0-199 inclusive. (WHG note - seems to be hundredths)
503
+   */
529 504
   uint8_t  creationTimeTenths;
530
-           /** Time file was created. */
531
-  uint16_t creationTime;
532
-           /** Date file was created. */
533
-  uint16_t creationDate;
534
-          /**
535
-           * Last access date. Note that there is no last access time, only
536
-           * a date.  This is the date of last read or write. In the case of
537
-           * a write, this should be set to the same date as lastWriteDate.
538
-           */
505
+
506
+  uint16_t creationTime;    // Time file was created.
507
+  uint16_t creationDate;    // Date file was created.
508
+
509
+  /**
510
+   * Last access date. Note that there is no last access time, only
511
+   * a date.  This is the date of last read or write. In the case of
512
+   * a write, this should be set to the same date as lastWriteDate.
513
+   */
539 514
   uint16_t lastAccessDate;
540
-          /**
541
-           * High word of this entry's first cluster number (always 0 for a
542
-           * FAT12 or FAT16 volume).
543
-           */
515
+  /**
516
+   * High word of this entry's first cluster number (always 0 for a
517
+   * FAT12 or FAT16 volume).
518
+   */
544 519
   uint16_t firstClusterHigh;
545
-           /** Time of last write. File creation is considered a write. */
546
-  uint16_t lastWriteTime;
547
-           /** Date of last write. File creation is considered a write. */
548
-  uint16_t lastWriteDate;
549
-           /** Low word of this entry's first cluster number. */
550
-  uint16_t firstClusterLow;
551
-           /** 32-bit unsigned holding this file's size in bytes. */
552
-  uint32_t fileSize;
520
+
521
+  uint16_t lastWriteTime;   // Time of last write. File creation is considered a write.
522
+  uint16_t lastWriteDate;   // Date of last write. File creation is considered a write.
523
+  uint16_t firstClusterLow; // Low word of this entry's first cluster number.
524
+  uint32_t fileSize;        // 32-bit unsigned holding this file's size in bytes.
553 525
 } PACKED;
526
+
554 527
 /**
555 528
  * \struct directoryVFATEntry
556 529
  * \brief VFAT long filename directory entry
@@ -568,54 +541,36 @@ struct directoryVFATEntry {
568 541
    *  bit 0-4: the position of this long filename block (first block is 1)
569 542
    */
570 543
   uint8_t  sequenceNumber;
571
-  /** First set of UTF-16 characters */
572
-  uint16_t name1[5];//UTF-16
573
-  /** attributes (at the same location as in directoryEntry), always 0x0F */
574
-  uint8_t  attributes;
575
-  /** Reserved for use by Windows NT. Always 0. */
576
-  uint8_t  reservedNT;
577
-  /** Checksum of the short 8.3 filename, can be used to checked if the file system as modified by a not-long-filename aware implementation. */
578
-  uint8_t  checksum;
579
-  /** Second set of UTF-16 characters */
580
-  uint16_t name2[6];//UTF-16
581
-  /** firstClusterLow is always zero for longFilenames */
582
-  uint16_t firstClusterLow;
583
-  /** Third set of UTF-16 characters */
584
-  uint16_t name3[2];//UTF-16
544
+
545
+  uint16_t name1[5];        // First set of UTF-16 characters
546
+  uint8_t  attributes;      // attributes (at the same location as in directoryEntry), always 0x0F
547
+  uint8_t  reservedNT;      // Reserved for use by Windows NT. Always 0.
548
+  uint8_t  checksum;        // Checksum of the short 8.3 filename, can be used to checked if the file system as modified by a not-long-filename aware implementation.
549
+  uint16_t name2[6];        // Second set of UTF-16 characters
550
+  uint16_t firstClusterLow; // firstClusterLow is always zero for longFilenames
551
+  uint16_t name3[2];        // Third set of UTF-16 characters
585 552
 } PACKED;
586
-//------------------------------------------------------------------------------
553
+
587 554
 // Definitions for directory entries
588 555
 //
589
-/** Type name for directoryEntry */
590
-typedef struct directoryEntry dir_t;
591
-/** Type name for directoryVFATEntry */
592
-typedef struct directoryVFATEntry vfat_t;
593
-/** escape for name[0] = 0xE5 */
594
-uint8_t const DIR_NAME_0xE5 = 0x05;
595
-/** name[0] value for entry that is free after being "deleted" */
596
-uint8_t const DIR_NAME_DELETED = 0xE5;
597
-/** name[0] value for entry that is free and no allocated entries follow */
598
-uint8_t const DIR_NAME_FREE = 0x00;
599
-/** file is read-only */
600
-uint8_t const DIR_ATT_READ_ONLY = 0x01;
601
-/** File should hidden in directory listings */
602
-uint8_t const DIR_ATT_HIDDEN = 0x02;
603
-/** Entry is for a system file */
604
-uint8_t const DIR_ATT_SYSTEM = 0x04;
605
-/** Directory entry contains the volume label */
606
-uint8_t const DIR_ATT_VOLUME_ID = 0x08;
607
-/** Entry is for a directory */
608
-uint8_t const DIR_ATT_DIRECTORY = 0x10;
609
-/** Old DOS archive bit for backup support */
610
-uint8_t const DIR_ATT_ARCHIVE = 0x20;
611
-/** Test value for long name entry.  Test is
612
-  (d->attributes & DIR_ATT_LONG_NAME_MASK) == DIR_ATT_LONG_NAME. */
613
-uint8_t const DIR_ATT_LONG_NAME = 0x0F;
614
-/** Test mask for long name entry */
615
-uint8_t const DIR_ATT_LONG_NAME_MASK = 0x3F;
616
-/** defined attribute bits */
617
-uint8_t const DIR_ATT_DEFINED_BITS = 0x3F;
618
-/** Directory entry is part of a long name
556
+typedef struct directoryEntry dir_t;          // Type name for directoryEntry
557
+typedef struct directoryVFATEntry vfat_t;     // Type name for directoryVFATEntry
558
+
559
+uint8_t const DIR_NAME_0xE5     = 0x05,       // escape for name[0] = 0xE5
560
+              DIR_NAME_DELETED  = 0xE5,       // name[0] value for entry that is free after being "deleted"
561
+              DIR_NAME_FREE     = 0x00,       // name[0] value for entry that is free and no allocated entries follow
562
+              DIR_ATT_READ_ONLY = 0x01,       // file is read-only
563
+              DIR_ATT_HIDDEN    = 0x02,       // File should hidden in directory listings
564
+              DIR_ATT_SYSTEM    = 0x04,       // Entry is for a system file
565
+              DIR_ATT_VOLUME_ID = 0x08,       // Directory entry contains the volume label
566
+              DIR_ATT_DIRECTORY = 0x10,       // Entry is for a directory
567
+              DIR_ATT_ARCHIVE   = 0x20,       // Old DOS archive bit for backup support
568
+              DIR_ATT_LONG_NAME = 0x0F,       // Test value for long name entry.  Test is (d->attributes & DIR_ATT_LONG_NAME_MASK) == DIR_ATT_LONG_NAME.
569
+              DIR_ATT_LONG_NAME_MASK = 0x3F,  // Test mask for long name entry
570
+              DIR_ATT_DEFINED_BITS = 0x3F;    // defined attribute bits
571
+
572
+/**
573
+ * Directory entry is part of a long name
619 574
  * \param[in] dir Pointer to a directory entry.
620 575
  *
621 576
  * \return true if the entry is for part of a long name else false.
@@ -623,9 +578,12 @@ uint8_t const DIR_ATT_DEFINED_BITS = 0x3F;
623 578
 static inline uint8_t DIR_IS_LONG_NAME(const dir_t* dir) {
624 579
   return (dir->attributes & DIR_ATT_LONG_NAME_MASK) == DIR_ATT_LONG_NAME;
625 580
 }
581
+
626 582
 /** Mask for file/subdirectory tests */
627 583
 uint8_t const DIR_ATT_FILE_TYPE_MASK = (DIR_ATT_VOLUME_ID | DIR_ATT_DIRECTORY);
628
-/** Directory entry is for a file
584
+
585
+/**
586
+ * Directory entry is for a file
629 587
  * \param[in] dir Pointer to a directory entry.
630 588
  *
631 589
  * \return true if the entry is for a normal file else false.
@@ -633,7 +591,9 @@ uint8_t const DIR_ATT_FILE_TYPE_MASK = (DIR_ATT_VOLUME_ID | DIR_ATT_DIRECTORY);
633 591
 static inline uint8_t DIR_IS_FILE(const dir_t* dir) {
634 592
   return (dir->attributes & DIR_ATT_FILE_TYPE_MASK) == 0;
635 593
 }
636
-/** Directory entry is for a subdirectory
594
+
595
+/**
596
+ * Directory entry is for a subdirectory
637 597
  * \param[in] dir Pointer to a directory entry.
638 598
  *
639 599
  * \return true if the entry is for a subdirectory else false.
@@ -641,7 +601,9 @@ static inline uint8_t DIR_IS_FILE(const dir_t* dir) {
641 601
 static inline uint8_t DIR_IS_SUBDIR(const dir_t* dir) {
642 602
   return (dir->attributes & DIR_ATT_FILE_TYPE_MASK) == DIR_ATT_DIRECTORY;
643 603
 }
644
-/** Directory entry is for a file or subdirectory
604
+
605
+/**
606
+ * Directory entry is for a file or subdirectory
645 607
  * \param[in] dir Pointer to a directory entry.
646 608
  *
647 609
  * \return true if the entry is for a normal file or subdirectory else false.
@@ -649,7 +611,5 @@ static inline uint8_t DIR_IS_SUBDIR(const dir_t* dir) {
649 611
 static inline uint8_t DIR_IS_FILE_OR_SUBDIR(const dir_t* dir) {
650 612
   return (dir->attributes & DIR_ATT_VOLUME_ID) == 0;
651 613
 }
652
-#endif  // SdFatStructs_h
653
-
654 614
 
655
-#endif
615
+#endif // SDFATSTRUCTS_H

+ 23
- 23
Marlin/SdFatUtil.cpp View File

@@ -26,13 +26,15 @@
26 26
  *
27 27
  * This file is part of the Arduino Sd2Card Library
28 28
  */
29
-#include "Marlin.h"
29
+#include "MarlinConfig.h"
30 30
 
31 31
 #if ENABLED(SDSUPPORT)
32
+
32 33
 #include "SdFatUtil.h"
34
+#include "serial.h"
33 35
 
34
-//------------------------------------------------------------------------------
35
-/** Amount of free RAM
36
+/**
37
+ * Amount of free RAM
36 38
  * \return The number of free bytes.
37 39
  */
38 40
 #ifdef __arm__
@@ -44,7 +46,8 @@ int SdFatUtil::FreeRam() {
44 46
 #else  // __arm__
45 47
 extern char* __brkval;
46 48
 extern char __bss_end;
47
-/** Amount of free RAM
49
+/**
50
+ * Amount of free RAM
48 51
  * \return The number of free bytes.
49 52
  */
50 53
 int SdFatUtil::FreeRam() {
@@ -53,8 +56,8 @@ int SdFatUtil::FreeRam() {
53 56
 }
54 57
 #endif  // __arm
55 58
 
56
-//------------------------------------------------------------------------------
57
-/** %Print a string in flash memory.
59
+/**
60
+ * %Print a string in flash memory.
58 61
  *
59 62
  * \param[in] pr Print object for output.
60 63
  * \param[in] str Pointer to string stored in flash memory.
@@ -62,30 +65,27 @@ int SdFatUtil::FreeRam() {
62 65
 void SdFatUtil::print_P(PGM_P str) {
63 66
   for (uint8_t c; (c = pgm_read_byte(str)); str++) MYSERIAL.write(c);
64 67
 }
65
-//------------------------------------------------------------------------------
66
-/** %Print a string in flash memory followed by a CR/LF.
68
+
69
+/**
70
+ * %Print a string in flash memory followed by a CR/LF.
67 71
  *
68 72
  * \param[in] pr Print object for output.
69 73
  * \param[in] str Pointer to string stored in flash memory.
70 74
  */
71
-void SdFatUtil::println_P(PGM_P str) {
72
-  print_P(str);
73
-  MYSERIAL.println();
74
-}
75
-//------------------------------------------------------------------------------
76
-/** %Print a string in flash memory to Serial.
75
+void SdFatUtil::println_P(PGM_P str) { print_P(str); MYSERIAL.println(); }
76
+
77
+/**
78
+ * %Print a string in flash memory to Serial.
77 79
  *
78 80
  * \param[in] str Pointer to string stored in flash memory.
79 81
  */
80
-void SdFatUtil::SerialPrint_P(PGM_P str) {
81
-  print_P(str);
82
-}
83
-//------------------------------------------------------------------------------
84
-/** %Print a string in flash memory to Serial followed by a CR/LF.
82
+void SdFatUtil::SerialPrint_P(PGM_P str) { print_P(str); }
83
+
84
+/**
85
+ * %Print a string in flash memory to Serial followed by a CR/LF.
85 86
  *
86 87
  * \param[in] str Pointer to string stored in flash memory.
87 88
  */
88
-void SdFatUtil::SerialPrintln_P(PGM_P str) {
89
-  println_P(str);
90
-}
91
-#endif
89
+void SdFatUtil::SerialPrintln_P(PGM_P str) { println_P(str); }
90
+
91
+#endif // SDSUPPORT

+ 3
- 8
Marlin/SdFatUtil.h View File

@@ -26,11 +26,8 @@
26 26
  *
27 27
  * This file is part of the Arduino Sd2Card Library
28 28
  */
29
-#ifndef SdFatUtil_h
30
-#define SdFatUtil_h
31
-
32
-#include "Marlin.h"
33
-#if ENABLED(SDSUPPORT)
29
+#ifndef _SDFATUTIL_H_
30
+#define _SDFATUTIL_H_
34 31
 
35 32
 /**
36 33
  * \file
@@ -51,6 +48,4 @@ namespace SdFatUtil {
51 48
 
52 49
 using namespace SdFatUtil;  // NOLINT
53 50
 
54
-#endif // SDSUPPORT
55
-
56
-#endif // SdFatUtil_h
51
+#endif // _SDFATUTIL_H_

+ 26
- 28
Marlin/SdFile.cpp View File

@@ -26,21 +26,24 @@
26 26
  *
27 27
  * This file is part of the Arduino Sd2Card Library
28 28
  */
29
-#include "Marlin.h"
29
+#include "MarlinConfig.h"
30 30
 
31 31
 #if ENABLED(SDSUPPORT)
32
+
32 33
 #include "SdFile.h"
33
-/**  Create a file object and open it in the current working directory.
34
+
35
+/**
36
+ *  Create a file object and open it in the current working directory.
34 37
  *
35 38
  * \param[in] path A path with a valid 8.3 DOS name for a file to be opened.
36 39
  *
37 40
  * \param[in] oflag Values for \a oflag are constructed by a bitwise-inclusive
38 41
  * OR of open flags. see SdBaseFile::open(SdBaseFile*, const char*, uint8_t).
39 42
  */
40
-SdFile::SdFile(const char* path, uint8_t oflag) : SdBaseFile(path, oflag) {
41
-}
42
-//------------------------------------------------------------------------------
43
-/** Write data to an open file.
43
+SdFile::SdFile(const char* path, uint8_t oflag) : SdBaseFile(path, oflag) { }
44
+
45
+/**
46
+ * Write data to an open file.
44 47
  *
45 48
  * \note Data is moved to the cache but may not be written to the
46 49
  * storage device until sync() is called.
@@ -55,41 +58,37 @@ SdFile::SdFile(const char* path, uint8_t oflag) : SdBaseFile(path, oflag) {
55 58
  * for a read-only file, device is full, a corrupt file system or an I/O error.
56 59
  *
57 60
  */
58
-int16_t SdFile::write(const void* buf, uint16_t nbyte) {
59
-  return SdBaseFile::write(buf, nbyte);
60
-}
61
-//------------------------------------------------------------------------------
62
-/** Write a byte to a file. Required by the Arduino Print class.
61
+int16_t SdFile::write(const void* buf, uint16_t nbyte) { return SdBaseFile::write(buf, nbyte); }
62
+
63
+/**
64
+ * Write a byte to a file. Required by the Arduino Print class.
63 65
  * \param[in] b the byte to be written.
64 66
  * Use writeError to check for errors.
65 67
  */
66 68
 #if ARDUINO >= 100
67
-  size_t SdFile::write(uint8_t b) {
68
-    return SdBaseFile::write(&b, 1);
69
-  }
69
+  size_t SdFile::write(uint8_t b) { return SdBaseFile::write(&b, 1); }
70 70
 #else
71
-  void SdFile::write(uint8_t b) {
72
-    SdBaseFile::write(&b, 1);
73
-  }
71
+  void SdFile::write(uint8_t b) { SdBaseFile::write(&b, 1); }
74 72
 #endif
75
-//------------------------------------------------------------------------------
76
-/** Write a string to a file. Used by the Arduino Print class.
73
+
74
+/**
75
+ * Write a string to a file. Used by the Arduino Print class.
77 76
  * \param[in] str Pointer to the string.
78 77
  * Use writeError to check for errors.
79 78
  */
80
-void SdFile::write(const char* str) {
81
-  SdBaseFile::write(str, strlen(str));
82
-}
83
-//------------------------------------------------------------------------------
84
-/** Write a PROGMEM string to a file.
79
+void SdFile::write(const char* str) { SdBaseFile::write(str, strlen(str)); }
80
+
81
+/**
82
+ * Write a PROGMEM string to a file.
85 83
  * \param[in] str Pointer to the PROGMEM string.
86 84
  * Use writeError to check for errors.
87 85
  */
88 86
 void SdFile::write_P(PGM_P str) {
89 87
   for (uint8_t c; (c = pgm_read_byte(str)); str++) write(c);
90 88
 }
91
-//------------------------------------------------------------------------------
92
-/** Write a PROGMEM string followed by CR/LF to a file.
89
+
90
+/**
91
+ * Write a PROGMEM string followed by CR/LF to a file.
93 92
  * \param[in] str Pointer to the PROGMEM string.
94 93
  * Use writeError to check for errors.
95 94
  */
@@ -98,5 +97,4 @@ void SdFile::writeln_P(PGM_P str) {
98 97
   write_P(PSTR("\r\n"));
99 98
 }
100 99
 
101
-
102
-#endif
100
+#endif // SDSUPPORT

+ 9
- 12
Marlin/SdFile.h View File

@@ -21,23 +21,22 @@
21 21
  */
22 22
 
23 23
 /**
24
+ * \file
25
+ * \brief SdFile class
26
+ */
27
+
28
+/**
24 29
  * Arduino SdFat Library
25 30
  * Copyright (C) 2009 by William Greiman
26 31
  *
27 32
  * This file is part of the Arduino Sd2Card Library
28 33
  */
29
-/**
30
- * \file
31
- * \brief SdFile class
32
- */
33
-#include "Marlin.h"
34
+#ifndef _SDFILE_H_
35
+#define _SDFILE_H_
34 36
 
35
-#if ENABLED(SDSUPPORT)
36 37
 #include "SdBaseFile.h"
37 38
 #include <Print.h>
38
-#ifndef SdFile_h
39
-#define SdFile_h
40
-//------------------------------------------------------------------------------
39
+
41 40
 /**
42 41
  * \class SdFile
43 42
  * \brief SdBaseFile with Print.
@@ -57,7 +56,5 @@ class SdFile : public SdBaseFile, public Print {
57 56
   void write_P(PGM_P str);
58 57
   void writeln_P(PGM_P str);
59 58
 };
60
-#endif  // SdFile_h
61
-
62 59
 
63
-#endif
60
+#endif // _SDFILE_H_

+ 27
- 49
Marlin/SdInfo.h View File

@@ -26,12 +26,11 @@
26 26
  *
27 27
  * This file is part of the Arduino Sd2Card Library
28 28
  */
29
-#include "Marlin.h"
30
-#if ENABLED(SDSUPPORT)
29
+#ifndef _SDINFO_H_
30
+#define _SDINFO_H_
31 31
 
32
-#ifndef SdInfo_h
33
-#define SdInfo_h
34 32
 #include <stdint.h>
33
+
35 34
 // Based on the document:
36 35
 //
37 36
 // SD Specifications
@@ -42,46 +41,26 @@
42 41
 // May 18, 2010
43 42
 //
44 43
 // http://www.sdcard.org/developers/tech/sdcard/pls/simplified_specs
45
-//------------------------------------------------------------------------------
44
+
46 45
 // SD card commands
47
-/** GO_IDLE_STATE - init card in spi mode if CS low */
48
-uint8_t const CMD0 = 0x00;
49
-/** SEND_IF_COND - verify SD Memory Card interface operating condition.*/
50
-uint8_t const CMD8 = 0x08;
51
-/** SEND_CSD - read the Card Specific Data (CSD register) */
52
-uint8_t const CMD9 = 0x09;
53
-/** SEND_CID - read the card identification information (CID register) */
54
-uint8_t const CMD10 = 0x0A;
55
-/** STOP_TRANSMISSION - end multiple block read sequence */
56
-uint8_t const CMD12 = 0x0C;
57
-/** SEND_STATUS - read the card status register */
58
-uint8_t const CMD13 = 0x0D;
59
-/** READ_SINGLE_BLOCK - read a single data block from the card */
60
-uint8_t const CMD17 = 0x11;
61
-/** READ_MULTIPLE_BLOCK - read a multiple data blocks from the card */
62
-uint8_t const CMD18 = 0x12;
63
-/** WRITE_BLOCK - write a single data block to the card */
64
-uint8_t const CMD24 = 0x18;
65
-/** WRITE_MULTIPLE_BLOCK - write blocks of data until a STOP_TRANSMISSION */
66
-uint8_t const CMD25 = 0x19;
67
-/** ERASE_WR_BLK_START - sets the address of the first block to be erased */
68
-uint8_t const CMD32 = 0x20;
69
-/** ERASE_WR_BLK_END - sets the address of the last block of the continuous
70
-    range to be erased*/
71
-uint8_t const CMD33 = 0x21;
72
-/** ERASE - erase all previously selected blocks */
73
-uint8_t const CMD38 = 0x26;
74
-/** APP_CMD - escape for application specific command */
75
-uint8_t const CMD55 = 0x37;
76
-/** READ_OCR - read the OCR register of a card */
77
-uint8_t const CMD58 = 0x3A;
78
-/** SET_WR_BLK_ERASE_COUNT - Set the number of write blocks to be
79
-     pre-erased before writing */
80
-uint8_t const ACMD23 = 0x17;
81
-/** SD_SEND_OP_COMD - Sends host capacity support information and
82
-    activates the card's initialization process */
83
-uint8_t const ACMD41 = 0x29;
84
-//------------------------------------------------------------------------------
46
+uint8_t const CMD0 = 0x00,    // GO_IDLE_STATE - init card in spi mode if CS low
47
+              CMD8 = 0x08,    // SEND_IF_COND - verify SD Memory Card interface operating condition
48
+              CMD9 = 0x09,    // SEND_CSD - read the Card Specific Data (CSD register)
49
+              CMD10 = 0x0A,   // SEND_CID - read the card identification information (CID register)
50
+              CMD12 = 0x0C,   // STOP_TRANSMISSION - end multiple block read sequence
51
+              CMD13 = 0x0D,   // SEND_STATUS - read the card status register
52
+              CMD17 = 0x11,   // READ_SINGLE_BLOCK - read a single data block from the card
53
+              CMD18 = 0x12,   // READ_MULTIPLE_BLOCK - read a multiple data blocks from the card
54
+              CMD24 = 0x18,   // WRITE_BLOCK - write a single data block to the card
55
+              CMD25 = 0x19,   // WRITE_MULTIPLE_BLOCK - write blocks of data until a STOP_TRANSMISSION
56
+              CMD32 = 0x20,   // ERASE_WR_BLK_START - sets the address of the first block to be erased
57
+              CMD33 = 0x21,   // ERASE_WR_BLK_END - sets the address of the last block of the continuous range to be erased*/
58
+              CMD38 = 0x26,   // ERASE - erase all previously selected blocks */
59
+              CMD55 = 0x37,   // APP_CMD - escape for application specific command */
60
+              CMD58 = 0x3A,   // READ_OCR - read the OCR register of a card */
61
+              ACMD23 = 0x17,  // SET_WR_BLK_ERASE_COUNT - Set the number of write blocks to be pre-erased before writing */
62
+              ACMD41 = 0x29;  // SD_SEND_OP_COMD - Sends host capacity support information and activates the card's initialization process */
63
+
85 64
 /** status for card in the ready state */
86 65
 uint8_t const R1_READY_STATE = 0x00;
87 66
 /** status for card in the idle state */
@@ -98,7 +77,7 @@ uint8_t const WRITE_MULTIPLE_TOKEN = 0xFC;
98 77
 uint8_t const DATA_RES_MASK = 0x1F;
99 78
 /** write data accepted token */
100 79
 uint8_t const DATA_RES_ACCEPTED = 0x05;
101
-//------------------------------------------------------------------------------
80
+
102 81
 /** Card IDentification (CID) register */
103 82
 typedef struct CID {
104 83
   // byte 0
@@ -134,7 +113,7 @@ typedef struct CID {
134 113
   /** CRC7 checksum */
135 114
   unsigned char crc : 7;
136 115
 } cid_t;
137
-//------------------------------------------------------------------------------
116
+
138 117
 /** CSD for version 1.00 cards */
139 118
 typedef struct CSDV1 {
140 119
   // byte 0
@@ -196,7 +175,7 @@ typedef struct CSDV1 {
196 175
   unsigned char always1 : 1;
197 176
   unsigned char crc : 7;
198 177
 } csd1_t;
199
-//------------------------------------------------------------------------------
178
+
200 179
 /** CSD for version 2.00 cards */
201 180
 typedef struct CSDV2 {
202 181
   // byte 0
@@ -278,12 +257,11 @@ typedef struct CSDV2 {
278 257
   /** checksum */
279 258
   unsigned char crc : 7;
280 259
 } csd2_t;
281
-//------------------------------------------------------------------------------
260
+
282 261
 /** union of old and new style CSD register */
283 262
 union csd_t {
284 263
   csd1_t v1;
285 264
   csd2_t v2;
286 265
 };
287
-#endif  // SdInfo_h
288 266
 
289
-#endif
267
+#endif // _SDINFO_H_

+ 75
- 111
Marlin/SdVolume.cpp View File

@@ -26,11 +26,12 @@
26 26
  *
27 27
  * This file is part of the Arduino Sd2Card Library
28 28
  */
29
-#include "Marlin.h"
29
+#include "MarlinConfig.h"
30
+
30 31
 #if ENABLED(SDSUPPORT)
31 32
 
32 33
 #include "SdVolume.h"
33
-//------------------------------------------------------------------------------
34
+
34 35
 #if !USE_MULTIPLE_CARDS
35 36
   // raw block cache
36 37
   uint32_t SdVolume::cacheBlockNumber_;  // current block number
@@ -39,7 +40,7 @@
39 40
   bool     SdVolume::cacheDirty_;        // cacheFlush() will write block if true
40 41
   uint32_t SdVolume::cacheMirrorBlock_;  // mirror  block for second FAT
41 42
 #endif  // USE_MULTIPLE_CARDS
42
-//------------------------------------------------------------------------------
43
+
43 44
 // find a contiguous group of clusters
44 45
 bool SdVolume::allocContiguous(uint32_t count, uint32_t* curCluster) {
45 46
   // start of group
@@ -73,14 +74,14 @@ bool SdVolume::allocContiguous(uint32_t count, uint32_t* curCluster) {
73 74
   // search the FAT for free clusters
74 75
   for (uint32_t n = 0;; n++, endCluster++) {
75 76
     // can't find space checked all clusters
76
-    if (n >= clusterCount_) goto FAIL;
77
+    if (n >= clusterCount_) return false;
77 78
 
78 79
     // past end - start from beginning of FAT
79 80
     if (endCluster > fatEnd) {
80 81
       bgnCluster = endCluster = 2;
81 82
     }
82 83
     uint32_t f;
83
-    if (!fatGet(endCluster, &f)) goto FAIL;
84
+    if (!fatGet(endCluster, &f)) return false;
84 85
 
85 86
     if (f != 0) {
86 87
       // cluster in use try next cluster as bgnCluster
@@ -92,16 +93,16 @@ bool SdVolume::allocContiguous(uint32_t count, uint32_t* curCluster) {
92 93
     }
93 94
   }
94 95
   // mark end of chain
95
-  if (!fatPutEOC(endCluster)) goto FAIL;
96
+  if (!fatPutEOC(endCluster)) return false;
96 97
 
97 98
   // link clusters
98 99
   while (endCluster > bgnCluster) {
99
-    if (!fatPut(endCluster - 1, endCluster)) goto FAIL;
100
+    if (!fatPut(endCluster - 1, endCluster)) return false;
100 101
     endCluster--;
101 102
   }
102 103
   if (*curCluster != 0) {
103 104
     // connect chains
104
-    if (!fatPut(*curCluster, bgnCluster)) goto FAIL;
105
+    if (!fatPut(*curCluster, bgnCluster)) return false;
105 106
   }
106 107
   // return first cluster number to caller
107 108
   *curCluster = bgnCluster;
@@ -110,111 +111,94 @@ bool SdVolume::allocContiguous(uint32_t count, uint32_t* curCluster) {
110 111
   if (setStart) allocSearchStart_ = bgnCluster + 1;
111 112
 
112 113
   return true;
113
-  FAIL:
114
-  return false;
115 114
 }
116
-//------------------------------------------------------------------------------
115
+
117 116
 bool SdVolume::cacheFlush() {
118 117
   if (cacheDirty_) {
119
-    if (!sdCard_->writeBlock(cacheBlockNumber_, cacheBuffer_.data)) {
120
-      goto FAIL;
121
-    }
118
+    if (!sdCard_->writeBlock(cacheBlockNumber_, cacheBuffer_.data))
119
+      return false;
120
+
122 121
     // mirror FAT tables
123 122
     if (cacheMirrorBlock_) {
124
-      if (!sdCard_->writeBlock(cacheMirrorBlock_, cacheBuffer_.data)) {
125
-        goto FAIL;
126
-      }
123
+      if (!sdCard_->writeBlock(cacheMirrorBlock_, cacheBuffer_.data))
124
+        return false;
127 125
       cacheMirrorBlock_ = 0;
128 126
     }
129 127
     cacheDirty_ = 0;
130 128
   }
131 129
   return true;
132
-  FAIL:
133
-  return false;
134 130
 }
135
-//------------------------------------------------------------------------------
131
+
136 132
 bool SdVolume::cacheRawBlock(uint32_t blockNumber, bool dirty) {
137 133
   if (cacheBlockNumber_ != blockNumber) {
138
-    if (!cacheFlush()) goto FAIL;
139
-    if (!sdCard_->readBlock(blockNumber, cacheBuffer_.data)) goto FAIL;
134
+    if (!cacheFlush()) return false;
135
+    if (!sdCard_->readBlock(blockNumber, cacheBuffer_.data)) return false;
140 136
     cacheBlockNumber_ = blockNumber;
141 137
   }
142 138
   if (dirty) cacheDirty_ = true;
143 139
   return true;
144
-  FAIL:
145
-  return false;
146 140
 }
147
-//------------------------------------------------------------------------------
141
+
148 142
 // return the size in bytes of a cluster chain
149 143
 bool SdVolume::chainSize(uint32_t cluster, uint32_t* size) {
150 144
   uint32_t s = 0;
151 145
   do {
152
-    if (!fatGet(cluster, &cluster)) goto FAIL;
146
+    if (!fatGet(cluster, &cluster)) return false;
153 147
     s += 512UL << clusterSizeShift_;
154 148
   } while (!isEOC(cluster));
155 149
   *size = s;
156 150
   return true;
157
-  FAIL:
158
-  return false;
159 151
 }
160
-//------------------------------------------------------------------------------
152
+
161 153
 // Fetch a FAT entry
162 154
 bool SdVolume::fatGet(uint32_t cluster, uint32_t* value) {
163 155
   uint32_t lba;
164
-  if (cluster > (clusterCount_ + 1)) goto FAIL;
156
+  if (cluster > (clusterCount_ + 1)) return false;
165 157
   if (FAT12_SUPPORT && fatType_ == 12) {
166 158
     uint16_t index = cluster;
167 159
     index += index >> 1;
168 160
     lba = fatStartBlock_ + (index >> 9);
169
-    if (!cacheRawBlock(lba, CACHE_FOR_READ)) goto FAIL;
161
+    if (!cacheRawBlock(lba, CACHE_FOR_READ)) return false;
170 162
     index &= 0x1FF;
171 163
     uint16_t tmp = cacheBuffer_.data[index];
172 164
     index++;
173 165
     if (index == 512) {
174
-      if (!cacheRawBlock(lba + 1, CACHE_FOR_READ)) goto FAIL;
166
+      if (!cacheRawBlock(lba + 1, CACHE_FOR_READ)) return false;
175 167
       index = 0;
176 168
     }
177 169
     tmp |= cacheBuffer_.data[index] << 8;
178 170
     *value = cluster & 1 ? tmp >> 4 : tmp & 0xFFF;
179 171
     return true;
180 172
   }
181
-  if (fatType_ == 16) {
173
+
174
+  if (fatType_ == 16)
182 175
     lba = fatStartBlock_ + (cluster >> 8);
183
-  }
184
-  else if (fatType_ == 32) {
176
+  else if (fatType_ == 32)
185 177
     lba = fatStartBlock_ + (cluster >> 7);
186
-  }
187
-  else {
188
-    goto FAIL;
189
-  }
190
-  if (lba != cacheBlockNumber_) {
191
-    if (!cacheRawBlock(lba, CACHE_FOR_READ)) goto FAIL;
192
-  }
193
-  if (fatType_ == 16) {
194
-    *value = cacheBuffer_.fat16[cluster & 0xFF];
195
-  }
196
-  else {
197
-    *value = cacheBuffer_.fat32[cluster & 0x7F] & FAT32MASK;
198
-  }
178
+  else
179
+    return false;
180
+
181
+  if (lba != cacheBlockNumber_ && !cacheRawBlock(lba, CACHE_FOR_READ))
182
+    return false;
183
+
184
+  *value = (fatType_ == 16) ? cacheBuffer_.fat16[cluster & 0xFF] : (cacheBuffer_.fat32[cluster & 0x7F] & FAT32MASK);
199 185
   return true;
200
-  FAIL:
201
-  return false;
202 186
 }
203
-//------------------------------------------------------------------------------
187
+
204 188
 // Store a FAT entry
205 189
 bool SdVolume::fatPut(uint32_t cluster, uint32_t value) {
206 190
   uint32_t lba;
207 191
   // error if reserved cluster
208
-  if (cluster < 2) goto FAIL;
192
+  if (cluster < 2) return false;
209 193
 
210 194
   // error if not in FAT
211
-  if (cluster > (clusterCount_ + 1)) goto FAIL;
195
+  if (cluster > (clusterCount_ + 1)) return false;
212 196
 
213 197
   if (FAT12_SUPPORT && fatType_ == 12) {
214 198
     uint16_t index = cluster;
215 199
     index += index >> 1;
216 200
     lba = fatStartBlock_ + (index >> 9);
217
-    if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) goto FAIL;
201
+    if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) return false;
218 202
     // mirror second FAT
219 203
     if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
220 204
     index &= 0x1FF;
@@ -227,7 +211,7 @@ bool SdVolume::fatPut(uint32_t cluster, uint32_t value) {
227 211
     if (index == 512) {
228 212
       lba++;
229 213
       index = 0;
230
-      if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) goto FAIL;
214
+      if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) return false;
231 215
       // mirror second FAT
232 216
       if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
233 217
     }
@@ -238,51 +222,45 @@ bool SdVolume::fatPut(uint32_t cluster, uint32_t value) {
238 222
     cacheBuffer_.data[index] = tmp;
239 223
     return true;
240 224
   }
241
-  if (fatType_ == 16) {
225
+
226
+  if (fatType_ == 16)
242 227
     lba = fatStartBlock_ + (cluster >> 8);
243
-  }
244
-  else if (fatType_ == 32) {
228
+  else if (fatType_ == 32)
245 229
     lba = fatStartBlock_ + (cluster >> 7);
246
-  }
247
-  else {
248
-    goto FAIL;
249
-  }
250
-  if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) goto FAIL;
230
+  else
231
+    return false;
232
+
233
+  if (!cacheRawBlock(lba, CACHE_FOR_WRITE)) return false;
234
+
251 235
   // store entry
252
-  if (fatType_ == 16) {
236
+  if (fatType_ == 16)
253 237
     cacheBuffer_.fat16[cluster & 0xFF] = value;
254
-  }
255
-  else {
238
+  else
256 239
     cacheBuffer_.fat32[cluster & 0x7F] = value;
257
-  }
240
+
258 241
   // mirror second FAT
259 242
   if (fatCount_ > 1) cacheMirrorBlock_ = lba + blocksPerFat_;
260 243
   return true;
261
-  FAIL:
262
-  return false;
263 244
 }
264
-//------------------------------------------------------------------------------
245
+
265 246
 // free a cluster chain
266 247
 bool SdVolume::freeChain(uint32_t cluster) {
267
-  uint32_t next;
268
-
269 248
   // clear free cluster location
270 249
   allocSearchStart_ = 2;
271 250
 
272 251
   do {
273
-    if (!fatGet(cluster, &next)) goto FAIL;
252
+    uint32_t next;
253
+    if (!fatGet(cluster, &next)) return false;
274 254
 
275 255
     // free cluster
276
-    if (!fatPut(cluster, 0)) goto FAIL;
256
+    if (!fatPut(cluster, 0)) return false;
277 257
 
278 258
     cluster = next;
279 259
   } while (!isEOC(cluster));
280 260
 
281 261
   return true;
282
-  FAIL:
283
-  return false;
284 262
 }
285
-//------------------------------------------------------------------------------
263
+
286 264
 /** Volume free space in clusters.
287 265
  *
288 266
  * \return Count of free clusters for success or -1 if an error occurs.
@@ -292,34 +270,28 @@ int32_t SdVolume::freeClusterCount() {
292 270
   uint16_t n;
293 271
   uint32_t todo = clusterCount_ + 2;
294 272
 
295
-  if (fatType_ == 16) {
273
+  if (fatType_ == 16)
296 274
     n = 256;
297
-  }
298
-  else if (fatType_ == 32) {
275
+  else if (fatType_ == 32)
299 276
     n = 128;
300
-  }
301
-  else {
302
-    // put FAT12 here
277
+  else // put FAT12 here
303 278
     return -1;
304
-  }
305 279
 
306 280
   for (uint32_t lba = fatStartBlock_; todo; todo -= n, lba++) {
307 281
     if (!cacheRawBlock(lba, CACHE_FOR_READ)) return -1;
308 282
     NOMORE(n, todo);
309 283
     if (fatType_ == 16) {
310
-      for (uint16_t i = 0; i < n; i++) {
284
+      for (uint16_t i = 0; i < n; i++)
311 285
         if (cacheBuffer_.fat16[i] == 0) free++;
312
-      }
313 286
     }
314 287
     else {
315
-      for (uint16_t i = 0; i < n; i++) {
288
+      for (uint16_t i = 0; i < n; i++)
316 289
         if (cacheBuffer_.fat32[i] == 0) free++;
317
-      }
318 290
     }
319 291
   }
320 292
   return free;
321 293
 }
322
-//------------------------------------------------------------------------------
294
+
323 295
 /** Initialize a FAT volume.
324 296
  *
325 297
  * \param[in] dev The SD card where the volume is located.
@@ -329,14 +301,12 @@ int32_t SdVolume::freeClusterCount() {
329 301
  * a MBR, Master Boot Record, or zero if the device is formatted as
330 302
  * a super floppy with the FAT boot sector in block zero.
331 303
  *
332
- * \return The value one, true, is returned for success and
333
- * the value zero, false, is returned for failure.  Reasons for
334
- * failure include not finding a valid partition, not finding a valid
304
+ * \return true for success, false for failure.
305
+ * Reasons for failure include not finding a valid partition, not finding a valid
335 306
  * FAT file system in the specified partition or an I/O error.
336 307
  */
337 308
 bool SdVolume::init(Sd2Card* dev, uint8_t part) {
338
-  uint32_t totalBlocks;
339
-  uint32_t volumeStartBlock = 0;
309
+  uint32_t totalBlocks, volumeStartBlock = 0;
340 310
   fat32_boot_t* fbs;
341 311
 
342 312
   sdCard_ = dev;
@@ -349,25 +319,21 @@ bool SdVolume::init(Sd2Card* dev, uint8_t part) {
349 319
   // if part == 0 assume super floppy with FAT boot sector in block zero
350 320
   // if part > 0 assume mbr volume with partition table
351 321
   if (part) {
352
-    if (part > 4)goto FAIL;
353
-    if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) goto FAIL;
322
+    if (part > 4) return false;
323
+    if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) return false;
354 324
     part_t* p = &cacheBuffer_.mbr.part[part - 1];
355
-    if ((p->boot & 0x7F) != 0  ||
356
-        p->totalSectors < 100 ||
357
-        p->firstSector == 0) {
358
-      // not a valid partition
359
-      goto FAIL;
360
-    }
325
+    if ((p->boot & 0x7F) != 0  || p->totalSectors < 100 || p->firstSector == 0)
326
+      return false; // not a valid partition
361 327
     volumeStartBlock = p->firstSector;
362 328
   }
363
-  if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) goto FAIL;
329
+  if (!cacheRawBlock(volumeStartBlock, CACHE_FOR_READ)) return false;
364 330
   fbs = &cacheBuffer_.fbs32;
365 331
   if (fbs->bytesPerSector != 512 ||
366 332
       fbs->fatCount == 0 ||
367 333
       fbs->reservedSectorCount == 0 ||
368 334
       fbs->sectorsPerCluster == 0) {
369 335
     // not valid FAT volume
370
-    goto FAIL;
336
+    return false;
371 337
   }
372 338
   fatCount_ = fbs->fatCount;
373 339
   blocksPerCluster_ = fbs->sectorsPerCluster;
@@ -375,7 +341,7 @@ bool SdVolume::init(Sd2Card* dev, uint8_t part) {
375 341
   clusterSizeShift_ = 0;
376 342
   while (blocksPerCluster_ != _BV(clusterSizeShift_)) {
377 343
     // error if not power of 2
378
-    if (clusterSizeShift_++ > 7) goto FAIL;
344
+    if (clusterSizeShift_++ > 7) return false;
379 345
   }
380 346
   blocksPerFat_ = fbs->sectorsPerFat16 ?
381 347
                   fbs->sectorsPerFat16 : fbs->sectorsPerFat32;
@@ -404,17 +370,15 @@ bool SdVolume::init(Sd2Card* dev, uint8_t part) {
404 370
   // FAT type is determined by cluster count
405 371
   if (clusterCount_ < 4085) {
406 372
     fatType_ = 12;
407
-    if (!FAT12_SUPPORT) goto FAIL;
373
+    if (!FAT12_SUPPORT) return false;
408 374
   }
409
-  else if (clusterCount_ < 65525) {
375
+  else if (clusterCount_ < 65525)
410 376
     fatType_ = 16;
411
-  }
412 377
   else {
413 378
     rootDirStart_ = fbs->fat32RootCluster;
414 379
     fatType_ = 32;
415 380
   }
416 381
   return true;
417
-  FAIL:
418
-  return false;
419 382
 }
420
-#endif
383
+
384
+#endif // SDSUPPORT

+ 105
- 121
Marlin/SdVolume.h View File

@@ -21,19 +21,19 @@
21 21
  */
22 22
 
23 23
 /**
24
+ * \file
25
+ * \brief SdVolume class
26
+ */
27
+
28
+/**
24 29
  * Arduino SdFat Library
25 30
  * Copyright (C) 2009 by William Greiman
26 31
  *
27 32
  * This file is part of the Arduino Sd2Card Library
28 33
  */
29
-#include "Marlin.h"
30
-#if ENABLED(SDSUPPORT)
31
-#ifndef SdVolume_h
32
-#define SdVolume_h
33
-/**
34
- * \file
35
- * \brief SdVolume class
36
- */
34
+#ifndef _SDVOLUME_H_
35
+#define _SDVOLUME_H_
36
+
37 37
 #include "SdFatConfig.h"
38 38
 #include "Sd2Card.h"
39 39
 #include "SdFatStructs.h"
@@ -44,33 +44,26 @@
44 44
  * \brief Cache for an SD data block
45 45
  */
46 46
 union cache_t {
47
-           /** Used to access cached file data blocks. */
48
-  uint8_t  data[512];
49
-           /** Used to access cached FAT16 entries. */
50
-  uint16_t fat16[256];
51
-           /** Used to access cached FAT32 entries. */
52
-  uint32_t fat32[128];
53
-           /** Used to access cached directory entries. */
54
-  dir_t    dir[16];
55
-           /** Used to access a cached Master Boot Record. */
56
-  mbr_t    mbr;
57
-           /** Used to access to a cached FAT boot sector. */
58
-  fat_boot_t fbs;
59
-           /** Used to access to a cached FAT32 boot sector. */
60
-  fat32_boot_t fbs32;
61
-           /** Used to access to a cached FAT32 FSINFO sector. */
62
-  fat32_fsinfo_t fsinfo;
47
+  uint8_t         data[512];  // Used to access cached file data blocks.
48
+  uint16_t        fat16[256]; // Used to access cached FAT16 entries.
49
+  uint32_t        fat32[128]; // Used to access cached FAT32 entries.
50
+  dir_t           dir[16];    // Used to access cached directory entries.
51
+  mbr_t           mbr;        // Used to access a cached Master Boot Record.
52
+  fat_boot_t      fbs;        // Used to access to a cached FAT boot sector.
53
+  fat32_boot_t    fbs32;      // Used to access to a cached FAT32 boot sector.
54
+  fat32_fsinfo_t  fsinfo;     // Used to access to a cached FAT32 FSINFO sector.
63 55
 };
64
-//------------------------------------------------------------------------------
56
+
65 57
 /**
66 58
  * \class SdVolume
67 59
  * \brief Access FAT16 and FAT32 volumes on SD and SDHC cards.
68 60
  */
69 61
 class SdVolume {
70 62
  public:
71
-  /** Create an instance of SdVolume */
63
+  // Create an instance of SdVolume
72 64
   SdVolume() : fatType_(0) {}
73
-  /** Clear the cache and returns a pointer to the cache.  Used by the WaveRP
65
+  /**
66
+   * Clear the cache and returns a pointer to the cache.  Used by the WaveRP
74 67
    * recorder to do raw write to the SD card.  Not for normal apps.
75 68
    * \return A pointer to the cache buffer or zero if an error occurs.
76 69
    */
@@ -79,54 +72,53 @@ class SdVolume {
79 72
     cacheBlockNumber_ = 0xFFFFFFFF;
80 73
     return &cacheBuffer_;
81 74
   }
82
-  /** Initialize a FAT volume.  Try partition one first then try super
75
+
76
+  /**
77
+   * Initialize a FAT volume.  Try partition one first then try super
83 78
    * floppy format.
84 79
    *
85 80
    * \param[in] dev The Sd2Card where the volume is located.
86 81
    *
87
-   * \return The value one, true, is returned for success and
88
-   * the value zero, false, is returned for failure.  Reasons for
89
-   * failure include not finding a valid partition, not finding a valid
90
-   * FAT file system or an I/O error.
82
+   * \return true for success, false for failure.
83
+   * Reasons for failure include not finding a valid partition, not finding
84
+   * a valid FAT file system or an I/O error.
91 85
    */
92
-  bool init(Sd2Card* dev) { return init(dev, 1) ? true : init(dev, 0);}
86
+  bool init(Sd2Card* dev) { return init(dev, 1) ? true : init(dev, 0); }
93 87
   bool init(Sd2Card* dev, uint8_t part);
94 88
 
95 89
   // inline functions that return volume info
96
-  /** \return The volume's cluster size in blocks. */
97
-  uint8_t blocksPerCluster() const {return blocksPerCluster_;}
98
-  /** \return The number of blocks in one FAT. */
99
-  uint32_t blocksPerFat()  const {return blocksPerFat_;}
100
-  /** \return The total number of clusters in the volume. */
101
-  uint32_t clusterCount() const {return clusterCount_;}
102
-  /** \return The shift count required to multiply by blocksPerCluster. */
103
-  uint8_t clusterSizeShift() const {return clusterSizeShift_;}
104
-  /** \return The logical block number for the start of file data. */
105
-  uint32_t dataStartBlock() const {return dataStartBlock_;}
106
-  /** \return The number of FAT structures on the volume. */
107
-  uint8_t fatCount() const {return fatCount_;}
108
-  /** \return The logical block number for the start of the first FAT. */
109
-  uint32_t fatStartBlock() const {return fatStartBlock_;}
110
-  /** \return The FAT type of the volume. Values are 12, 16 or 32. */
111
-  uint8_t fatType() const {return fatType_;}
90
+  uint8_t blocksPerCluster() const { return blocksPerCluster_; } //> \return The volume's cluster size in blocks.
91
+  uint32_t blocksPerFat() const { return blocksPerFat_; }        //> \return The number of blocks in one FAT.
92
+  uint32_t clusterCount() const { return clusterCount_; }        //> \return The total number of clusters in the volume.
93
+  uint8_t clusterSizeShift() const { return clusterSizeShift_; } //> \return The shift count required to multiply by blocksPerCluster.
94
+  uint32_t dataStartBlock() const { return dataStartBlock_; }    //> \return The logical block number for the start of file data.
95
+  uint8_t fatCount() const { return fatCount_; }                 //> \return The number of FAT structures on the volume.
96
+  uint32_t fatStartBlock() const { return fatStartBlock_; }      //> \return The logical block number for the start of the first FAT.
97
+  uint8_t fatType() const { return fatType_; }                   //> \return The FAT type of the volume. Values are 12, 16 or 32.
112 98
   int32_t freeClusterCount();
113
-  /** \return The number of entries in the root directory for FAT16 volumes. */
114
-  uint32_t rootDirEntryCount() const {return rootDirEntryCount_;}
115
-  /** \return The logical block number for the start of the root directory
116
-       on FAT16 volumes or the first cluster number on FAT32 volumes. */
117
-  uint32_t rootDirStart() const {return rootDirStart_;}
118
-  /** Sd2Card object for this volume
99
+  uint32_t rootDirEntryCount() const { return rootDirEntryCount_; } /** \return The number of entries in the root directory for FAT16 volumes. */
100
+
101
+  /**
102
+   * \return The logical block number for the start of the root directory
103
+   *   on FAT16 volumes or the first cluster number on FAT32 volumes.
104
+   */
105
+  uint32_t rootDirStart() const { return rootDirStart_; }
106
+
107
+  /**
108
+   * Sd2Card object for this volume
119 109
    * \return pointer to Sd2Card object.
120 110
    */
121
-  Sd2Card* sdCard() {return sdCard_;}
122
-  /** Debug access to FAT table
111
+  Sd2Card* sdCard() { return sdCard_; }
112
+
113
+  /**
114
+   * Debug access to FAT table
123 115
    *
124 116
    * \param[in] n cluster number.
125 117
    * \param[out] v value of entry
126 118
    * \return true for success or false for failure
127 119
    */
128
-  bool dbgFat(uint32_t n, uint32_t* v) {return fatGet(n, v);}
129
-  //------------------------------------------------------------------------------
120
+  bool dbgFat(uint32_t n, uint32_t* v) { return fatGet(n, v); }
121
+
130 122
  private:
131 123
   // Allow SdBaseFile access to SdVolume private data.
132 124
   friend class SdBaseFile;
@@ -136,19 +128,20 @@ class SdVolume {
136 128
   // value for dirty argument in cacheRawBlock to indicate write to cache
137 129
   static bool const CACHE_FOR_WRITE = true;
138 130
 
139
-#if USE_MULTIPLE_CARDS
140
-  cache_t cacheBuffer_;        // 512 byte cache for device blocks
141
-  uint32_t cacheBlockNumber_;  // Logical number of block in the cache
142
-  Sd2Card* sdCard_;            // Sd2Card object for cache
143
-  bool cacheDirty_;            // cacheFlush() will write block if true
144
-  uint32_t cacheMirrorBlock_;  // block number for mirror FAT
145
-#else  // USE_MULTIPLE_CARDS
146
-  static cache_t cacheBuffer_;        // 512 byte cache for device blocks
147
-  static uint32_t cacheBlockNumber_;  // Logical number of block in the cache
148
-  static Sd2Card* sdCard_;            // Sd2Card object for cache
149
-  static bool cacheDirty_;            // cacheFlush() will write block if true
150
-  static uint32_t cacheMirrorBlock_;  // block number for mirror FAT
151
-#endif  // USE_MULTIPLE_CARDS
131
+  #if USE_MULTIPLE_CARDS
132
+    cache_t cacheBuffer_;        // 512 byte cache for device blocks
133
+    uint32_t cacheBlockNumber_;  // Logical number of block in the cache
134
+    Sd2Card* sdCard_;            // Sd2Card object for cache
135
+    bool cacheDirty_;            // cacheFlush() will write block if true
136
+    uint32_t cacheMirrorBlock_;  // block number for mirror FAT
137
+  #else
138
+    static cache_t cacheBuffer_;        // 512 byte cache for device blocks
139
+    static uint32_t cacheBlockNumber_;  // Logical number of block in the cache
140
+    static Sd2Card* sdCard_;            // Sd2Card object for cache
141
+    static bool cacheDirty_;            // cacheFlush() will write block if true
142
+    static uint32_t cacheMirrorBlock_;  // block number for mirror FAT
143
+  #endif
144
+
152 145
   uint32_t allocSearchStart_;   // start cluster for alloc search
153 146
   uint8_t blocksPerCluster_;    // cluster size in blocks
154 147
   uint32_t blocksPerFat_;       // FAT size in blocks
@@ -160,68 +153,59 @@ class SdVolume {
160 153
   uint8_t fatType_;             // volume type (12, 16, OR 32)
161 154
   uint16_t rootDirEntryCount_;  // number of entries in FAT16 root dir
162 155
   uint32_t rootDirStart_;       // root start block for FAT16, cluster for FAT32
163
-  //----------------------------------------------------------------------------
156
+
164 157
   bool allocContiguous(uint32_t count, uint32_t* curCluster);
165
-  uint8_t blockOfCluster(uint32_t position) const {
166
-    return (position >> 9) & (blocksPerCluster_ - 1);
167
-  }
168
-  uint32_t clusterStartBlock(uint32_t cluster) const {
169
-    return dataStartBlock_ + ((cluster - 2) << clusterSizeShift_);
170
-  }
171
-  uint32_t blockNumber(uint32_t cluster, uint32_t position) const {
172
-    return clusterStartBlock(cluster) + blockOfCluster(position);
173
-  }
174
-  cache_t* cache() {return &cacheBuffer_;}
175
-  uint32_t cacheBlockNumber() {return cacheBlockNumber_;}
176
-#if USE_MULTIPLE_CARDS
177
-  bool cacheFlush();
178
-  bool cacheRawBlock(uint32_t blockNumber, bool dirty);
179
-#else  // USE_MULTIPLE_CARDS
180
-  static bool cacheFlush();
181
-  static bool cacheRawBlock(uint32_t blockNumber, bool dirty);
182
-#endif  // USE_MULTIPLE_CARDS
158
+  uint8_t blockOfCluster(uint32_t position) const { return (position >> 9) & (blocksPerCluster_ - 1); }
159
+  uint32_t clusterStartBlock(uint32_t cluster) const { return dataStartBlock_ + ((cluster - 2) << clusterSizeShift_); }
160
+  uint32_t blockNumber(uint32_t cluster, uint32_t position) const { return clusterStartBlock(cluster) + blockOfCluster(position); }
161
+
162
+  cache_t* cache() { return &cacheBuffer_; }
163
+  uint32_t cacheBlockNumber() const { return cacheBlockNumber_; }
164
+
165
+  #if USE_MULTIPLE_CARDS
166
+    bool cacheFlush();
167
+    bool cacheRawBlock(uint32_t blockNumber, bool dirty);
168
+  #else
169
+    static bool cacheFlush();
170
+    static bool cacheRawBlock(uint32_t blockNumber, bool dirty);
171
+  #endif
172
+
183 173
   // used by SdBaseFile write to assign cache to SD location
184 174
   void cacheSetBlockNumber(uint32_t blockNumber, bool dirty) {
185 175
     cacheDirty_ = dirty;
186 176
     cacheBlockNumber_  = blockNumber;
187 177
   }
188
-  void cacheSetDirty() {cacheDirty_ |= CACHE_FOR_WRITE;}
178
+  void cacheSetDirty() { cacheDirty_ |= CACHE_FOR_WRITE; }
189 179
   bool chainSize(uint32_t beginCluster, uint32_t* size);
190 180
   bool fatGet(uint32_t cluster, uint32_t* value);
191 181
   bool fatPut(uint32_t cluster, uint32_t value);
192
-  bool fatPutEOC(uint32_t cluster) {
193
-    return fatPut(cluster, 0x0FFFFFFF);
194
-  }
182
+  bool fatPutEOC(uint32_t cluster) { return fatPut(cluster, 0x0FFFFFFF); }
195 183
   bool freeChain(uint32_t cluster);
196 184
   bool isEOC(uint32_t cluster) const {
197 185
     if (FAT12_SUPPORT && fatType_ == 12) return  cluster >= FAT12EOC_MIN;
198 186
     if (fatType_ == 16) return cluster >= FAT16EOC_MIN;
199 187
     return  cluster >= FAT32EOC_MIN;
200 188
   }
201
-  bool readBlock(uint32_t block, uint8_t* dst) {
202
-    return sdCard_->readBlock(block, dst);
203
-  }
204
-  bool writeBlock(uint32_t block, const uint8_t* dst) {
205
-    return sdCard_->writeBlock(block, dst);
206
-  }
207
-  //------------------------------------------------------------------------------
208
-  // Deprecated functions  - suppress cpplint warnings with NOLINT comment
209
-#if ALLOW_DEPRECATED_FUNCTIONS && !defined(DOXYGEN)
210
- public:
211
-  /** \deprecated Use: bool SdVolume::init(Sd2Card* dev);
212
-   * \param[in] dev The SD card where the volume is located.
213
-   * \return true for success or false for failure.
214
-   */
215
-  bool init(Sd2Card& dev) {return init(&dev);}  // NOLINT
216
-  /** \deprecated Use: bool SdVolume::init(Sd2Card* dev, uint8_t vol);
217
-   * \param[in] dev The SD card where the volume is located.
218
-   * \param[in] part The partition to be used.
219
-   * \return true for success or false for failure.
220
-   */
221
-  bool init(Sd2Card& dev, uint8_t part) {  // NOLINT
222
-    return init(&dev, part);
223
-  }
224
-#endif  // ALLOW_DEPRECATED_FUNCTIONS
189
+  bool readBlock(uint32_t block, uint8_t* dst) { return sdCard_->readBlock(block, dst); }
190
+  bool writeBlock(uint32_t block, const uint8_t* dst) { return sdCard_->writeBlock(block, dst); }
191
+
192
+  // Deprecated functions
193
+  #if ALLOW_DEPRECATED_FUNCTIONS
194
+    public:
195
+      /**
196
+       * \deprecated Use: bool SdVolume::init(Sd2Card* dev);
197
+       * \param[in] dev The SD card where the volume is located.
198
+       * \return true for success or false for failure.
199
+       */
200
+      bool init(Sd2Card& dev) { return init(&dev); }
201
+      /**
202
+       * \deprecated Use: bool SdVolume::init(Sd2Card* dev, uint8_t vol);
203
+       * \param[in] dev The SD card where the volume is located.
204
+       * \param[in] part The partition to be used.
205
+       * \return true for success or false for failure.
206
+       */
207
+      bool init(Sd2Card& dev, uint8_t part) { return init(&dev, part); }
208
+  #endif  // ALLOW_DEPRECATED_FUNCTIONS
225 209
 };
226
-#endif  // SdVolume
227
-#endif
210
+
211
+#endif // _SDVOLUME_H_

+ 23
- 42
Marlin/cardreader.cpp View File

@@ -20,16 +20,16 @@
20 20
  *
21 21
  */
22 22
 
23
+#include "MarlinConfig.h"
24
+
25
+#if ENABLED(SDSUPPORT)
26
+
23 27
 #include "cardreader.h"
24 28
 
25 29
 #include "ultralcd.h"
26 30
 #include "stepper.h"
27 31
 #include "language.h"
28 32
 
29
-#include "Marlin.h"
30
-
31
-#if ENABLED(SDSUPPORT)
32
-
33 33
 #define LONGEST_FILENAME (longFilename[0] ? longFilename : filename)
34 34
 
35 35
 CardReader::CardReader() {
@@ -44,8 +44,9 @@ CardReader::CardReader() {
44 44
   sdprinting = cardOK = saving = logging = false;
45 45
   filesize = 0;
46 46
   sdpos = 0;
47
-  workDirDepth = 0;
48 47
   file_subcall_ctr = 0;
48
+
49
+  workDirDepth = 0;
49 50
   ZERO(workDirParents);
50 51
 
51 52
   autostart_stilltocheck = true; //the SD start is delayed, because otherwise the serial cannot answer fast enough to make contact with the host software.
@@ -255,16 +256,7 @@ void CardReader::initsd() {
255 256
     SERIAL_ECHO_START();
256 257
     SERIAL_ECHOLNPGM(MSG_SD_CARD_OK);
257 258
   }
258
-  workDir = root;
259
-  curDir = &root;
260
-  #if ENABLED(SDCARD_SORT_ALPHA)
261
-    presort();
262
-  #endif
263
-  /**
264
-  if (!workDir.openRoot(&volume)) {
265
-    SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
266
-  }
267
-  */
259
+  setroot();
268 260
 }
269 261
 
270 262
 void CardReader::setroot() {
@@ -328,8 +320,8 @@ void CardReader::openFile(char* name, bool read, bool push_current/*=false*/) {
328 320
   if (!cardOK) return;
329 321
 
330 322
   uint8_t doing = 0;
331
-  if (isFileOpen()) { //replacing current file by new file, or subfile call
332 323
     if (push_current) {
324
+  if (isFileOpen()) {                     // Replacing current file or doing a subroutine
333 325
       if (file_subcall_ctr > SD_PROCEDURE_DEPTH - 1) {
334 326
         SERIAL_ERROR_START();
335 327
         SERIAL_ERRORPGM("trying to call sub-gcode files with too many levels. MAX level is:");
@@ -338,19 +330,18 @@ void CardReader::openFile(char* name, bool read, bool push_current/*=false*/) {
338 330
         return;
339 331
       }
340 332
 
341
-      // Store current filename and position
333
+      // Store current filename (based on workDirParents) and position
342 334
       getAbsFilename(proc_filenames[file_subcall_ctr]);
335
+      filespos[file_subcall_ctr] = sdpos;
343 336
 
344 337
       SERIAL_ECHO_START();
345 338
       SERIAL_ECHOPAIR("SUBROUTINE CALL target:\"", name);
346 339
       SERIAL_ECHOPAIR("\" parent:\"", proc_filenames[file_subcall_ctr]);
347 340
       SERIAL_ECHOLNPAIR("\" pos", sdpos);
348
-      filespos[file_subcall_ctr] = sdpos;
349 341
       file_subcall_ctr++;
350 342
     }
351
-    else {
343
+    else
352 344
       doing = 1;
353
-    }
354 345
   }
355 346
   else { // Opening fresh file
356 347
     doing = 2;
@@ -360,7 +351,7 @@ void CardReader::openFile(char* name, bool read, bool push_current/*=false*/) {
360 351
   if (doing) {
361 352
     SERIAL_ECHO_START();
362 353
     SERIAL_ECHOPGM("Now ");
363
-    SERIAL_ECHO(doing == 1 ? "doing" : "fresh");
354
+    serialprintPGM(doing == 1 ? PSTR("doing") : PSTR("fresh"));
364 355
     SERIAL_ECHOLNPAIR(" file: ", name);
365 356
   }
366 357
 
@@ -380,8 +371,7 @@ void CardReader::openFile(char* name, bool read, bool push_current/*=false*/) {
380 371
       if (dirname_end != NULL && dirname_end > dirname_start) {
381 372
         char subdirname[FILENAME_LENGTH];
382 373
         strncpy(subdirname, dirname_start, dirname_end - dirname_start);
383
-        subdirname[dirname_end - dirname_start] = 0;
384
-        SERIAL_ECHOLN(subdirname);
374
+        subdirname[dirname_end - dirname_start] = '\0';
385 375
         if (!myDir.open(curDir, subdirname, O_READ)) {
386 376
           SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
387 377
           SERIAL_PROTOCOL(subdirname);
@@ -403,17 +393,15 @@ void CardReader::openFile(char* name, bool read, bool push_current/*=false*/) {
403 393
       }
404 394
     }
405 395
   }
406
-  else { //relative path
407
-    curDir = &workDir;
408
-  }
396
+  else
397
+    curDir = &workDir; // Relative paths start in current directory
409 398
 
410 399
   if (read) {
411 400
     if (file.open(curDir, fname, O_READ)) {
412 401
       filesize = file.fileSize();
402
+      sdpos = 0;
413 403
       SERIAL_PROTOCOLPAIR(MSG_SD_FILE_OPENED, fname);
414 404
       SERIAL_PROTOCOLLNPAIR(MSG_SD_SIZE, filesize);
415
-      sdpos = 0;
416
-
417 405
       SERIAL_PROTOCOLLNPGM(MSG_SD_FILE_SELECTED);
418 406
       getfilename(0, fname);
419 407
       lcd_setstatus(longFilename[0] ? longFilename : fname);
@@ -438,14 +426,14 @@ void CardReader::openFile(char* name, bool read, bool push_current/*=false*/) {
438 426
   }
439 427
 }
440 428
 
441
-void CardReader::removeFile(char* name) {
429
+void CardReader::removeFile(const char * const name) {
442 430
   if (!cardOK) return;
443 431
 
444 432
   stopSDPrint();
445 433
 
446 434
   SdFile myDir;
447 435
   curDir = &root;
448
-  char *fname = name;
436
+  const char *fname = name;
449 437
 
450 438
   char *dirname_start, *dirname_end;
451 439
   if (name[0] == '/') {
@@ -460,29 +448,23 @@ void CardReader::removeFile(char* name) {
460 448
         subdirname[dirname_end - dirname_start] = 0;
461 449
         SERIAL_ECHOLN(subdirname);
462 450
         if (!myDir.open(curDir, subdirname, O_READ)) {
463
-          SERIAL_PROTOCOLPAIR("open failed, File: ", subdirname);
451
+          SERIAL_PROTOCOLPAIR(MSG_SD_OPEN_FILE_FAIL, subdirname);
464 452
           SERIAL_PROTOCOLCHAR('.');
465 453
           SERIAL_EOL();
466 454
           return;
467 455
         }
468
-        else {
469
-          //SERIAL_ECHOLNPGM("dive ok");
470
-        }
471 456
 
472 457
         curDir = &myDir;
473 458
         dirname_start = dirname_end + 1;
474 459
       }
475
-      else { // the remainder after all /fsa/fdsa/ is the filename
460
+      else {
476 461
         fname = dirname_start;
477
-        //SERIAL_ECHOLNPGM("remainder");
478
-        //SERIAL_ECHOLN(fname);
479 462
         break;
480 463
       }
481 464
     }
482 465
   }
483
-  else { // relative path
466
+  else // Relative paths are rooted in the current directory
484 467
     curDir = &workDir;
485
-  }
486 468
 
487 469
   if (file.remove(curDir, fname)) {
488 470
     SERIAL_PROTOCOLPGM("File deleted:");
@@ -506,14 +488,13 @@ void CardReader::getStatus() {
506 488
     SERIAL_PROTOCOLCHAR('/');
507 489
     SERIAL_PROTOCOLLN(filesize);
508 490
   }
509
-  else {
491
+  else
510 492
     SERIAL_PROTOCOLLNPGM(MSG_SD_NOT_PRINTING);
511
-  }
512 493
 }
513 494
 
514 495
 void CardReader::write_command(char *buf) {
515 496
   char* begin = buf;
516
-  char* npos = 0;
497
+  char* npos = NULL;
517 498
   char* end = buf + strlen(buf) - 1;
518 499
 
519 500
   file.writeError = false;

+ 21
- 21
Marlin/cardreader.h View File

@@ -20,8 +20,8 @@
20 20
  *
21 21
  */
22 22
 
23
-#ifndef CARDREADER_H
24
-#define CARDREADER_H
23
+#ifndef _CARDREADER_H_
24
+#define _CARDREADER_H_
25 25
 
26 26
 #include "MarlinConfig.h"
27 27
 
@@ -30,7 +30,6 @@
30 30
 #define MAX_DIR_DEPTH 10          // Maximum folder depth
31 31
 
32 32
 #include "SdFile.h"
33
-
34 33
 #include "types.h"
35 34
 #include "enum.h"
36 35
 
@@ -40,13 +39,15 @@ public:
40 39
 
41 40
   void initsd();
42 41
   void write_command(char *buf);
43
-  //files auto[0-9].g on the sd card are performed in a row
44
-  //this is to delay autostart and hence the initialisaiton of the sd card to some seconds after the normal init, so the device is available quick after a reset
42
+  // Files auto[0-9].g on the sd card are performed in sequence.
43
+  // This is to delay autostart and hence the initialisation of
44
+  // the sd card to some seconds after the normal init, so the
45
+  // device is available soon after a reset.
45 46
 
46 47
   void checkautostart(bool x);
47 48
   void openFile(char* name, bool read, bool push_current=false);
48 49
   void openLogFile(char* name);
49
-  void removeFile(char* name);
50
+  void removeFile(const char * const name);
50 51
   void closefile(bool store_location=false);
51 52
   void release();
52 53
   void openAndPrintFile(const char *name);
@@ -148,8 +149,7 @@ private:
148 149
   uint8_t file_subcall_ctr;
149 150
   uint32_t filespos[SD_PROCEDURE_DEPTH];
150 151
   char proc_filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH];
151
-  uint32_t filesize;
152
-  uint32_t sdpos;
152
+  uint32_t filesize, sdpos;
153 153
 
154 154
   millis_t next_autostart_ms;
155 155
   bool autostart_stilltocheck; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
@@ -164,27 +164,27 @@ private:
164 164
   #endif
165 165
 };
166 166
 
167
-extern CardReader card;
168
-
169
-#define IS_SD_PRINTING (card.sdprinting)
170
-#define IS_SD_FILE_OPEN (card.isFileOpen())
171
-
172 167
 #if PIN_EXISTS(SD_DETECT)
173 168
   #if ENABLED(SD_DETECT_INVERTED)
174
-    #define IS_SD_INSERTED (READ(SD_DETECT_PIN) != 0)
169
+    #define IS_SD_INSERTED (READ(SD_DETECT_PIN) == HIGH)
175 170
   #else
176
-    #define IS_SD_INSERTED (READ(SD_DETECT_PIN) == 0)
171
+    #define IS_SD_INSERTED (READ(SD_DETECT_PIN) == LOW)
177 172
   #endif
178 173
 #else
179
-  //No card detect line? Assume the card is inserted.
174
+  // No card detect line? Assume the card is inserted.
180 175
   #define IS_SD_INSERTED true
181 176
 #endif
182 177
 
183
-#else
184
-
185
-#define IS_SD_PRINTING (false)
186
-#define IS_SD_FILE_OPEN (false)
178
+extern CardReader card;
187 179
 
188 180
 #endif // SDSUPPORT
189 181
 
190
-#endif // __CARDREADER_H
182
+#if ENABLED(SDSUPPORT)
183
+  #define IS_SD_PRINTING (card.sdprinting)
184
+  #define IS_SD_FILE_OPEN (card.isFileOpen())
185
+#else
186
+  #define IS_SD_PRINTING (false)
187
+  #define IS_SD_FILE_OPEN (false)
188
+#endif
189
+
190
+#endif // _CARDREADER_H_

Loading…
Cancel
Save