Browse Source

Merge pull request #8434 from thinkyhead/bf2_fix_M32_subroutines

[2.0] Fix 'M32 P' subroutines
Scott Lahteine 6 years ago
parent
commit
9c3761047e
No account linked to committer's email address

+ 17
- 11
Marlin/src/gcode/queue.cpp View File

@@ -361,19 +361,25 @@ inline void get_serial_commands() {
361 361
           || ((sd_char == '#' || sd_char == ':') && !sd_comment_mode)
362 362
       ) {
363 363
         if (card_eof) {
364
-          SERIAL_PROTOCOLLNPGM(MSG_FILE_PRINTED);
364
+
365 365
           card.printingHasFinished();
366
-          #if ENABLED(PRINTER_EVENT_LEDS)
367
-            LCD_MESSAGEPGM(MSG_INFO_COMPLETED_PRINTS);
368
-            set_led_color(0, 255, 0); // Green
369
-            #if HAS_RESUME_CONTINUE
370
-              enqueue_and_echo_commands_P(PSTR("M0")); // end of the queue!
371
-            #else
372
-              safe_delay(1000);
366
+
367
+          if (card.sdprinting)
368
+            sd_count = 0; // If a sub-file was printing, continue from call point
369
+          else {
370
+            SERIAL_PROTOCOLLNPGM(MSG_FILE_PRINTED);
371
+            #if ENABLED(PRINTER_EVENT_LEDS)
372
+              LCD_MESSAGEPGM(MSG_INFO_COMPLETED_PRINTS);
373
+              set_led_color(0, 255, 0); // Green
374
+              #if HAS_RESUME_CONTINUE
375
+                enqueue_and_echo_commands_P(PSTR("M0")); // end of the queue!
376
+              #else
377
+                safe_delay(1000);
378
+              #endif
379
+              set_led_color(0, 0, 0);   // OFF
373 380
             #endif
374
-            set_led_color(0, 0, 0);   // OFF
375
-          #endif
376
-          card.checkautostart(true);
381
+            card.checkautostart(true);
382
+          }
377 383
         }
378 384
         else if (n == -1) {
379 385
           SERIAL_ERROR_START();

+ 12
- 8
Marlin/src/gcode/sdcard/M20-M30_M32-M34_M928.cpp View File

@@ -124,19 +124,23 @@ void GcodeSuite::M30() {
124 124
 
125 125
 /**
126 126
  * M32: Select file and start SD Print
127
+ *
128
+ * Examples:
129
+ *
130
+ *    M32 !PATH/TO/FILE.GCO#      ; Start FILE.GCO
131
+ *    M32 P !PATH/TO/FILE.GCO#    ; Start FILE.GCO as a procedure
132
+ *    M32 S60 !PATH/TO/FILE.GCO#  ; Start FILE.GCO at byte 60
133
+ *
127 134
  */
128 135
 void GcodeSuite::M32() {
129
-  if (IS_SD_PRINTING)
130
-    stepper.synchronize();
131
-
132
-  char* namestartpos = parser.string_arg;
133
-  const bool call_procedure = parser.boolval('P');
136
+  if (card.sdprinting) stepper.synchronize();
134 137
 
135 138
   if (card.cardOK) {
136
-    card.openFile(namestartpos, true, call_procedure);
139
+    const bool call_procedure = parser.boolval('P');
140
+
141
+    card.openFile(parser.string_arg, true, call_procedure);
137 142
 
138
-    if (parser.seenval('S'))
139
-      card.setIndex(parser.value_long());
143
+    if (parser.seenval('S')) card.setIndex(parser.value_long());
140 144
 
141 145
     card.startFileprint();
142 146
 

+ 110
- 126
Marlin/src/sd/Sd2Card.cpp View File

@@ -35,7 +35,6 @@
35 35
 
36 36
 #include "../Marlin.h"
37 37
 
38
-//------------------------------------------------------------------------------
39 38
 // send command and return error code.  Return zero for OK
40 39
 uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) {
41 40
   // select card
@@ -63,7 +62,7 @@ uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) {
63 62
   for (uint8_t i = 0; ((status_ = spiRec()) & 0x80) && i != 0xFF; i++) { /* Intentionally left empty */ }
64 63
   return status_;
65 64
 }
66
-//------------------------------------------------------------------------------
65
+
67 66
 /**
68 67
  * Determine the size of an SD flash memory card.
69 68
  *
@@ -91,19 +90,20 @@ uint32_t Sd2Card::cardSize() {
91 90
     return 0;
92 91
   }
93 92
 }
94
-//------------------------------------------------------------------------------
93
+
95 94
 void Sd2Card::chipSelectHigh() {
96 95
   digitalWrite(chipSelectPin_, HIGH);
97 96
 }
98
-//------------------------------------------------------------------------------
97
+
99 98
 void Sd2Card::chipSelectLow() {
100 99
   #if DISABLED(SOFTWARE_SPI)
101 100
     spiInit(spiRate_);
102 101
   #endif  // SOFTWARE_SPI
103 102
   digitalWrite(chipSelectPin_, LOW);
104 103
 }
105
-//------------------------------------------------------------------------------
106
-/** Erase a range of blocks.
104
+
105
+/**
106
+ * Erase a range of blocks.
107 107
  *
108 108
  * \param[in] firstBlock The address of the first block in the range.
109 109
  * \param[in] lastBlock The address of the last block in the range.
@@ -113,8 +113,7 @@ void Sd2Card::chipSelectLow() {
113 113
  * either 0 or 1, depends on the card vendor.  The card must support
114 114
  * single block erase.
115 115
  *
116
- * \return The value one, true, is returned for success and
117
- * the value zero, false, is returned for failure.
116
+ * \return true for success, false for failure.
118 117
  */
119 118
 bool Sd2Card::erase(uint32_t firstBlock, uint32_t lastBlock) {
120 119
   csd_t csd;
@@ -149,26 +148,26 @@ bool Sd2Card::erase(uint32_t firstBlock, uint32_t lastBlock) {
149 148
   chipSelectHigh();
150 149
   return false;
151 150
 }
152
-//------------------------------------------------------------------------------
153
-/** Determine if card supports single block erase.
151
+
152
+/**
153
+ * Determine if card supports single block erase.
154 154
  *
155
- * \return The value one, true, is returned if single block erase is supported.
156
- * The value zero, false, is returned if single block erase is not supported.
155
+ * \return true if single block erase is supported.
156
+ *         false if single block erase is not supported.
157 157
  */
158 158
 bool Sd2Card::eraseSingleBlockEnable() {
159 159
   csd_t csd;
160 160
   return readCSD(&csd) ? csd.v1.erase_blk_en : false;
161 161
 }
162
-//------------------------------------------------------------------------------
162
+
163 163
 /**
164 164
  * Initialize an SD flash memory card.
165 165
  *
166 166
  * \param[in] sckRateID SPI clock rate selector. See setSckRate().
167 167
  * \param[in] chipSelectPin SD chip select pin number.
168 168
  *
169
- * \return The value one, true, is returned for success and
170
- * the value zero, false, is returned for failure.  The reason for failure
171
- * can be determined by calling errorCode() and errorData().
169
+ * \return true for success, false for failure.
170
+ * The reason for failure can be determined by calling errorCode() and errorData().
172 171
  */
173 172
 bool Sd2Card::init(uint8_t sckRateID, pin_t chipSelectPin) {
174 173
   errorCode_ = type_ = 0;
@@ -247,14 +246,13 @@ bool Sd2Card::init(uint8_t sckRateID, pin_t chipSelectPin) {
247 246
   chipSelectHigh();
248 247
   return false;
249 248
 }
250
-//------------------------------------------------------------------------------
249
+
251 250
 /**
252 251
  * Read a 512 byte block from an SD card.
253 252
  *
254 253
  * \param[in] blockNumber Logical block to be read.
255 254
  * \param[out] dst Pointer to the location that will receive the data.
256
- * \return The value one, true, is returned for success and
257
- * the value zero, false, is returned for failure.
255
+ * \return true for success, false for failure.
258 256
  */
259 257
 bool Sd2Card::readBlock(uint32_t blockNumber, uint8_t* dst) {
260 258
   // use address if not SDHC card
@@ -262,19 +260,18 @@ bool Sd2Card::readBlock(uint32_t blockNumber, uint8_t* dst) {
262 260
 
263 261
   #if ENABLED(SD_CHECK_AND_RETRY)
264 262
     uint8_t retryCnt = 3;
265
-    do {
266
-      if (!cardCommand(CMD17, blockNumber)) {
267
-        if (readData(dst, 512)) return true;
268
-      }
269
-      else
263
+    for(;;) {
264
+      if (cardCommand(CMD17, blockNumber))
270 265
         error(SD_CARD_ERROR_CMD17);
266
+      else if (readData(dst, 512))
267
+        return true;
271 268
 
272 269
       if (!--retryCnt) break;
273 270
 
274 271
       chipSelectHigh();
275 272
       cardCommand(CMD12, 0); // Try sending a stop command, ignore the result.
276 273
       errorCode_ = 0;
277
-    } while (true);
274
+    }
278 275
   #else
279 276
     if (cardCommand(CMD17, blockNumber))
280 277
       error(SD_CARD_ERROR_CMD17);
@@ -285,13 +282,13 @@ bool Sd2Card::readBlock(uint32_t blockNumber, uint8_t* dst) {
285 282
   chipSelectHigh();
286 283
   return false;
287 284
 }
288
-//------------------------------------------------------------------------------
289
-/** Read one data block in a multiple block read sequence
285
+
286
+/**
287
+ * Read one data block in a multiple block read sequence
290 288
  *
291 289
  * \param[in] dst Pointer to the location for the data to be read.
292 290
  *
293
- * \return The value one, true, is returned for success and
294
- * the value zero, false, is returned for failure.
291
+ * \return true for success, false for failure.
295 292
  */
296 293
 bool Sd2Card::readData(uint8_t* dst) {
297 294
   chipSelectLow();
@@ -299,50 +296,49 @@ bool Sd2Card::readData(uint8_t* dst) {
299 296
 }
300 297
 
301 298
 #if ENABLED(SD_CHECK_AND_RETRY)
302
-static const uint16_t crctab[] PROGMEM = {
303
-  0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
304
-  0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
305
-  0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
306
-  0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
307
-  0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
308
-  0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
309
-  0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
310
-  0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
311
-  0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
312
-  0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
313
-  0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
314
-  0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
315
-  0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
316
-  0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
317
-  0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
318
-  0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
319
-  0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
320
-  0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
321
-  0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
322
-  0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
323
-  0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
324
-  0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
325
-  0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
326
-  0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
327
-  0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
328
-  0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
329
-  0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
330
-  0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
331
-  0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
332
-  0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
333
-  0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
334
-  0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
335
-};
336
-static uint16_t CRC_CCITT(const uint8_t* data, size_t n) {
337
-  uint16_t crc = 0;
338
-  for (size_t i = 0; i < n; i++) {
339
-    crc = pgm_read_word(&crctab[(crc >> 8 ^ data[i]) & 0xFF]) ^ (crc << 8);
299
+  static const uint16_t crctab[] PROGMEM = {
300
+    0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
301
+    0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
302
+    0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
303
+    0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
304
+    0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
305
+    0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
306
+    0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
307
+    0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
308
+    0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
309
+    0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
310
+    0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
311
+    0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
312
+    0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
313
+    0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
314
+    0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
315
+    0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
316
+    0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
317
+    0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
318
+    0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
319
+    0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
320
+    0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
321
+    0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
322
+    0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
323
+    0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
324
+    0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
325
+    0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
326
+    0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
327
+    0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
328
+    0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
329
+    0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
330
+    0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
331
+    0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
332
+  };
333
+  static uint16_t CRC_CCITT(const uint8_t* data, size_t n) {
334
+    uint16_t crc = 0;
335
+    for (size_t i = 0; i < n; i++) {
336
+      crc = pgm_read_word(&crctab[(crc >> 8 ^ data[i]) & 0xFF]) ^ (crc << 8);
337
+    }
338
+    return crc;
340 339
   }
341
-  return crc;
342
-}
343
-#endif
340
+#endif // SD_CHECK_AND_RETRY
344 341
 
345
-//------------------------------------------------------------------------------
346 342
 bool Sd2Card::readData(uint8_t* dst, uint16_t count) {
347 343
   // wait for start block token
348 344
   uint16_t t0 = millis();
@@ -384,61 +380,55 @@ bool Sd2Card::readData(uint8_t* dst, uint16_t count) {
384 380
   spiSend(0XFF);
385 381
   return false;
386 382
 }
387
-//------------------------------------------------------------------------------
383
+
388 384
 /** read CID or CSR register */
389 385
 bool Sd2Card::readRegister(uint8_t cmd, void* buf) {
390 386
   uint8_t* dst = reinterpret_cast<uint8_t*>(buf);
391 387
   if (cardCommand(cmd, 0)) {
392 388
     error(SD_CARD_ERROR_READ_REG);
393
-    goto FAIL;
389
+    chipSelectHigh();
390
+    return false;
394 391
   }
395 392
   return readData(dst, 16);
396
-  FAIL:
397
-  chipSelectHigh();
398
-  return false;
399 393
 }
400
-//------------------------------------------------------------------------------
401
-/** Start a read multiple blocks sequence.
394
+
395
+/**
396
+ * Start a read multiple blocks sequence.
402 397
  *
403 398
  * \param[in] blockNumber Address of first block in sequence.
404 399
  *
405 400
  * \note This function is used with readData() and readStop() for optimized
406 401
  * multiple block reads.  SPI chipSelect must be low for the entire sequence.
407 402
  *
408
- * \return The value one, true, is returned for success and
409
- * the value zero, false, is returned for failure.
403
+ * \return true for success, false for failure.
410 404
  */
411 405
 bool Sd2Card::readStart(uint32_t blockNumber) {
412 406
   if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
413 407
   if (cardCommand(CMD18, blockNumber)) {
414 408
     error(SD_CARD_ERROR_CMD18);
415
-    goto FAIL;
409
+    chipSelectHigh();
410
+    return false;
416 411
   }
417 412
   chipSelectHigh();
418 413
   return true;
419
-  FAIL:
420
-  chipSelectHigh();
421
-  return false;
422 414
 }
423
-//------------------------------------------------------------------------------
424
-/** End a read multiple blocks sequence.
415
+
416
+/**
417
+ * End a read multiple blocks sequence.
425 418
  *
426
-* \return The value one, true, is returned for success and
427
- * the value zero, false, is returned for failure.
419
+ * \return true for success, false for failure.
428 420
  */
429 421
 bool Sd2Card::readStop() {
430 422
   chipSelectLow();
431 423
   if (cardCommand(CMD12, 0)) {
432 424
     error(SD_CARD_ERROR_CMD12);
433
-    goto FAIL;
425
+    chipSelectHigh();
426
+    return false;
434 427
   }
435 428
   chipSelectHigh();
436 429
   return true;
437
-  FAIL:
438
-  chipSelectHigh();
439
-  return false;
440 430
 }
441
-//------------------------------------------------------------------------------
431
+
442 432
 /**
443 433
  * Set the SPI clock rate.
444 434
  *
@@ -459,25 +449,22 @@ bool Sd2Card::setSckRate(uint8_t sckRateID) {
459 449
   spiRate_ = sckRateID;
460 450
   return true;
461 451
 }
462
-//------------------------------------------------------------------------------
452
+
463 453
 // wait for card to go not busy
464 454
 bool Sd2Card::waitNotBusy(uint16_t timeoutMillis) {
465 455
   uint16_t t0 = millis();
466
-  while (spiRec() != 0XFF) {
467
-    if (((uint16_t)millis() - t0) >= timeoutMillis) goto FAIL;
468
-  }
456
+  while (spiRec() != 0XFF)
457
+    if (((uint16_t)millis() - t0) >= timeoutMillis) return false;
458
+
469 459
   return true;
470
-  FAIL:
471
-  return false;
472 460
 }
473
-//------------------------------------------------------------------------------
461
+
474 462
 /**
475 463
  * Writes a 512 byte block to an SD card.
476 464
  *
477 465
  * \param[in] blockNumber Logical block to be written.
478 466
  * \param[in] src Pointer to the location of the data to be written.
479
- * \return The value one, true, is returned for success and
480
- * the value zero, false, is returned for failure.
467
+ * \return true for success, false for failure.
481 468
  */
482 469
 bool Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) {
483 470
   // use address if not SDHC card
@@ -504,25 +491,24 @@ bool Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) {
504 491
   chipSelectHigh();
505 492
   return false;
506 493
 }
507
-//------------------------------------------------------------------------------
508
-/** Write one data block in a multiple block write sequence
494
+
495
+/**
496
+ * Write one data block in a multiple block write sequence
509 497
  * \param[in] src Pointer to the location of the data to be written.
510
- * \return The value one, true, is returned for success and
511
- * the value zero, false, is returned for failure.
498
+ * \return true for success, false for failure.
512 499
  */
513 500
 bool Sd2Card::writeData(const uint8_t* src) {
514 501
   chipSelectLow();
515 502
   // wait for previous write to finish
516
-  if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto FAIL;
517
-  if (!writeData(WRITE_MULTIPLE_TOKEN, src)) goto FAIL;
503
+  if (!waitNotBusy(SD_WRITE_TIMEOUT) || !writeData(WRITE_MULTIPLE_TOKEN, src)) {
504
+    error(SD_CARD_ERROR_WRITE_MULTIPLE);
505
+    chipSelectHigh();
506
+    return false;
507
+  }
518 508
   chipSelectHigh();
519 509
   return true;
520
-  FAIL:
521
-  error(SD_CARD_ERROR_WRITE_MULTIPLE);
522
-  chipSelectHigh();
523
-  return false;
524 510
 }
525
-//------------------------------------------------------------------------------
511
+
526 512
 // send one block of data for write block or write multiple blocks
527 513
 bool Sd2Card::writeData(uint8_t token, const uint8_t* src) {
528 514
   spiSendBlock(token, src);
@@ -533,15 +519,14 @@ bool Sd2Card::writeData(uint8_t token, const uint8_t* src) {
533 519
   status_ = spiRec();
534 520
   if ((status_ & DATA_RES_MASK) != DATA_RES_ACCEPTED) {
535 521
     error(SD_CARD_ERROR_WRITE);
536
-    goto FAIL;
522
+    chipSelectHigh();
523
+    return false;
537 524
   }
538 525
   return true;
539
-  FAIL:
540
-  chipSelectHigh();
541
-  return false;
542 526
 }
543
-//------------------------------------------------------------------------------
544
-/** Start a write multiple blocks sequence.
527
+
528
+/**
529
+ * Start a write multiple blocks sequence.
545 530
  *
546 531
  * \param[in] blockNumber Address of first block in sequence.
547 532
  * \param[in] eraseCount The number of blocks to be pre-erased.
@@ -549,8 +534,7 @@ bool Sd2Card::writeData(uint8_t token, const uint8_t* src) {
549 534
  * \note This function is used with writeData() and writeStop()
550 535
  * for optimized multiple block writes.
551 536
  *
552
- * \return The value one, true, is returned for success and
553
- * the value zero, false, is returned for failure.
537
+ * \return true for success, false for failure.
554 538
  */
555 539
 bool Sd2Card::writeStart(uint32_t blockNumber, uint32_t eraseCount) {
556 540
   // send pre-erase count
@@ -570,11 +554,11 @@ bool Sd2Card::writeStart(uint32_t blockNumber, uint32_t eraseCount) {
570 554
   chipSelectHigh();
571 555
   return false;
572 556
 }
573
-//------------------------------------------------------------------------------
574
-/** End a write multiple blocks sequence.
557
+
558
+/**
559
+ * End a write multiple blocks sequence.
575 560
  *
576
-* \return The value one, true, is returned for success and
577
- * the value zero, false, is returned for failure.
561
+ * \return true for success, false for failure.
578 562
  */
579 563
 bool Sd2Card::writeStop() {
580 564
   chipSelectLow();
@@ -589,4 +573,4 @@ bool Sd2Card::writeStop() {
589 573
   return false;
590 574
 }
591 575
 
592
-#endif
576
+#endif // SDSUPPORT

+ 75
- 113
Marlin/src/sd/Sd2Card.h View File

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

+ 300
- 403
Marlin/src/sd/SdBaseFile.cpp
File diff suppressed because it is too large
View File


+ 228
- 216
Marlin/src/sd/SdBaseFile.h View File

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

+ 0
- 17
Marlin/src/sd/SdFatConfig.h View File

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

+ 381
- 417
Marlin/src/sd/SdFatStructs.h View File

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

+ 18
- 21
Marlin/src/sd/SdFatUtil.cpp View File

@@ -33,8 +33,8 @@
33 33
 
34 34
 #include "SdFatUtil.h"
35 35
 
36
-//------------------------------------------------------------------------------
37
-/** Amount of free RAM
36
+/**
37
+ * Amount of free RAM
38 38
  * \return The number of free bytes.
39 39
  */
40 40
 #ifdef __arm__
@@ -46,7 +46,8 @@ int SdFatUtil::FreeRam() {
46 46
 #else  // __arm__
47 47
 extern char* __brkval;
48 48
 extern char __bss_end;
49
-/** Amount of free RAM
49
+/**
50
+ * Amount of free RAM
50 51
  * \return The number of free bytes.
51 52
  */
52 53
 int SdFatUtil::FreeRam() {
@@ -55,8 +56,8 @@ int SdFatUtil::FreeRam() {
55 56
 }
56 57
 #endif  // __arm
57 58
 
58
-//------------------------------------------------------------------------------
59
-/** %Print a string in flash memory.
59
+/**
60
+ * %Print a string in flash memory.
60 61
  *
61 62
  * \param[in] pr Print object for output.
62 63
  * \param[in] str Pointer to string stored in flash memory.
@@ -64,31 +65,27 @@ int SdFatUtil::FreeRam() {
64 65
 void SdFatUtil::print_P(PGM_P str) {
65 66
   for (uint8_t c; (c = pgm_read_byte(str)); str++) MYSERIAL.write(c);
66 67
 }
67
-//------------------------------------------------------------------------------
68
-/** %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.
69 71
  *
70 72
  * \param[in] pr Print object for output.
71 73
  * \param[in] str Pointer to string stored in flash memory.
72 74
  */
73
-void SdFatUtil::println_P(PGM_P str) {
74
-  print_P(str);
75
-  MYSERIAL.println();
76
-}
77
-//------------------------------------------------------------------------------
78
-/** %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.
79 79
  *
80 80
  * \param[in] str Pointer to string stored in flash memory.
81 81
  */
82
-void SdFatUtil::SerialPrint_P(PGM_P str) {
83
-  print_P(str);
84
-}
85
-//------------------------------------------------------------------------------
86
-/** %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.
87 86
  *
88 87
  * \param[in] str Pointer to string stored in flash memory.
89 88
  */
90
-void SdFatUtil::SerialPrintln_P(PGM_P str) {
91
-  println_P(str);
92
-}
89
+void SdFatUtil::SerialPrintln_P(PGM_P str) { println_P(str); }
93 90
 
94 91
 #endif // SDSUPPORT

+ 3
- 3
Marlin/src/sd/SdFatUtil.h View File

@@ -26,8 +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
29
+#ifndef _SDFATUTIL_H_
30
+#define _SDFATUTIL_H_
31 31
 
32 32
 #include <string.h>
33 33
 
@@ -50,4 +50,4 @@ namespace SdFatUtil {
50 50
 
51 51
 using namespace SdFatUtil;  // NOLINT
52 52
 
53
-#endif // SDFATUTIL_H
53
+#endif // _SDFATUTIL_H_

+ 23
- 27
Marlin/src/sd/SdFile.cpp View File

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

+ 8
- 10
Marlin/src/sd/SdFile.h View File

@@ -21,26 +21,24 @@
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
-/**
31
- * \file
32
- * \brief SdFile class
33
- */
34
-
35
-#ifndef SDFILE_H
36
-#define SDFILE_H
34
+#ifndef _SDFILE_H_
35
+#define _SDFILE_H_
37 36
 
38 37
 #include "SdBaseFile.h"
39 38
 
40 39
 #include <stdint.h>
41 40
 #include <string.h>
42 41
 
43
-//------------------------------------------------------------------------------
44 42
 /**
45 43
  * \class SdFile
46 44
  * \brief SdBaseFile with Print.
@@ -61,4 +59,4 @@ class SdFile : public SdBaseFile/*, public Print*/ {
61 59
   void writeln_P(PGM_P str);
62 60
 };
63 61
 
64
-#endif // SDFILE_H
62
+#endif // _SDFILE_H_

+ 26
- 46
Marlin/src/sd/SdInfo.h View File

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

+ 70
- 109
Marlin/src/sd/SdVolume.cpp View File

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

+ 78
- 93
Marlin/src/sd/SdVolume.h View File

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

+ 48
- 56
Marlin/src/sd/cardreader.cpp View File

@@ -49,8 +49,9 @@ CardReader::CardReader() {
49 49
   sdprinting = cardOK = saving = logging = false;
50 50
   filesize = 0;
51 51
   sdpos = 0;
52
-  workDirDepth = 0;
53 52
   file_subcall_ctr = 0;
53
+
54
+  workDirDepth = 0;
54 55
   ZERO(workDirParents);
55 56
 
56 57
   autostart_stilltocheck = true; //the SD start is delayed, because otherwise the serial cannot answer fast enough to make contact with the host software.
@@ -263,16 +264,7 @@ void CardReader::initsd() {
263 264
     SERIAL_ECHO_START();
264 265
     SERIAL_ECHOLNPGM(MSG_SD_CARD_OK);
265 266
   }
266
-  workDir = root;
267
-  curDir = &root;
268
-  #if ENABLED(SDCARD_SORT_ALPHA)
269
-    presort();
270
-  #endif
271
-  /**
272
-  if (!workDir.openRoot(&volume)) {
273
-    SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
274
-  }
275
-  */
267
+  setroot();
276 268
 }
277 269
 
278 270
 void CardReader::setroot() {
@@ -318,26 +310,33 @@ void CardReader::openLogFile(char* name) {
318 310
   openFile(name, false);
319 311
 }
320 312
 
313
+void appendAtom(SdFile &file, char *& dst, uint8_t &cnt) {
314
+  file.getFilename(dst);
315
+  while (*dst && cnt < MAXPATHNAMELENGTH) { dst++; cnt++; }
316
+  if (cnt < MAXPATHNAMELENGTH) { *dst = '/'; dst++; cnt++; }
317
+}
318
+
321 319
 void CardReader::getAbsFilename(char *t) {
322
-  uint8_t cnt = 0;
323
-  *t = '/'; t++; cnt++;
324
-  for (uint8_t i = 0; i < workDirDepth; i++) {
325
-    workDirParents[i].getFilename(t); //SDBaseFile.getfilename!
326
-    while (*t && cnt < MAXPATHNAMELENGTH) { t++; cnt++; } //crawl counter forward.
320
+  *t++ = '/';                                               // Root folder
321
+  uint8_t cnt = 1;
322
+
323
+  for (uint8_t i = 0; i < workDirDepth; i++)                // Loop to current work dir
324
+    appendAtom(workDirParents[i], t, cnt);
325
+
326
+  if (cnt < MAXPATHNAMELENGTH - (FILENAME_LENGTH)) {
327
+    appendAtom(file, t, cnt);
328
+    --t;
327 329
   }
328
-  if (cnt < MAXPATHNAMELENGTH - (FILENAME_LENGTH))
329
-    file.getFilename(t);
330
-  else
331
-    t[0] = 0;
330
+  *t = '\0';
332 331
 }
333 332
 
334
-void CardReader::openFile(char* name, bool read, bool push_current/*=false*/) {
333
+void CardReader::openFile(char* name, const bool read, const bool subcall/*=false*/) {
335 334
 
336 335
   if (!cardOK) return;
337 336
 
338 337
   uint8_t doing = 0;
339
-  if (isFileOpen()) { //replacing current file by new file, or subfile call
340
-    if (push_current) {
338
+  if (isFileOpen()) {                     // Replacing current file or doing a subroutine
339
+    if (subcall) {
341 340
       if (file_subcall_ctr > SD_PROCEDURE_DEPTH - 1) {
342 341
         SERIAL_ERROR_START();
343 342
         SERIAL_ERRORPGM("trying to call sub-gcode files with too many levels. MAX level is:");
@@ -346,21 +345,24 @@ void CardReader::openFile(char* name, bool read, bool push_current/*=false*/) {
346 345
         return;
347 346
       }
348 347
 
349
-      // Store current filename and position
348
+      // Store current filename (based on workDirParents) and position
350 349
       getAbsFilename(proc_filenames[file_subcall_ctr]);
350
+      filespos[file_subcall_ctr] = sdpos;
351 351
 
352 352
       SERIAL_ECHO_START();
353 353
       SERIAL_ECHOPAIR("SUBROUTINE CALL target:\"", name);
354 354
       SERIAL_ECHOPAIR("\" parent:\"", proc_filenames[file_subcall_ctr]);
355 355
       SERIAL_ECHOLNPAIR("\" pos", sdpos);
356
-      filespos[file_subcall_ctr] = sdpos;
357 356
       file_subcall_ctr++;
358 357
     }
359
-    else {
358
+    else
360 359
       doing = 1;
361
-    }
362 360
   }
363
-  else { // Opening fresh file
361
+  else if (subcall) {     // Returning from a subcall?
362
+    SERIAL_ECHO_START();
363
+    SERIAL_ECHOLNPGM("END SUBROUTINE");
364
+  }
365
+  else {                  // Opening fresh file
364 366
     doing = 2;
365 367
     file_subcall_ctr = 0; // Reset procedure depth in case user cancels print while in procedure
366 368
   }
@@ -368,7 +370,7 @@ void CardReader::openFile(char* name, bool read, bool push_current/*=false*/) {
368 370
   if (doing) {
369 371
     SERIAL_ECHO_START();
370 372
     SERIAL_ECHOPGM("Now ");
371
-    SERIAL_ECHO(doing == 1 ? "doing" : "fresh");
373
+    serialprintPGM(doing == 1 ? PSTR("doing") : PSTR("fresh"));
372 374
     SERIAL_ECHOLNPAIR(" file: ", name);
373 375
   }
374 376
 
@@ -388,8 +390,7 @@ void CardReader::openFile(char* name, bool read, bool push_current/*=false*/) {
388 390
       if (dirname_end != NULL && dirname_end > dirname_start) {
389 391
         char subdirname[FILENAME_LENGTH];
390 392
         strncpy(subdirname, dirname_start, dirname_end - dirname_start);
391
-        subdirname[dirname_end - dirname_start] = 0;
392
-        SERIAL_ECHOLN(subdirname);
393
+        subdirname[dirname_end - dirname_start] = '\0';
393 394
         if (!myDir.open(curDir, subdirname, O_READ)) {
394 395
           SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
395 396
           SERIAL_PROTOCOL(subdirname);
@@ -411,17 +412,15 @@ void CardReader::openFile(char* name, bool read, bool push_current/*=false*/) {
411 412
       }
412 413
     }
413 414
   }
414
-  else { //relative path
415
-    curDir = &workDir;
416
-  }
415
+  else
416
+    curDir = &workDir; // Relative paths start in current directory
417 417
 
418 418
   if (read) {
419 419
     if (file.open(curDir, fname, O_READ)) {
420 420
       filesize = file.fileSize();
421
+      sdpos = 0;
421 422
       SERIAL_PROTOCOLPAIR(MSG_SD_FILE_OPENED, fname);
422 423
       SERIAL_PROTOCOLLNPAIR(MSG_SD_SIZE, filesize);
423
-      sdpos = 0;
424
-
425 424
       SERIAL_PROTOCOLLNPGM(MSG_SD_FILE_SELECTED);
426 425
       getfilename(0, fname);
427 426
       lcd_setstatus(longFilename[0] ? longFilename : fname);
@@ -446,14 +445,14 @@ void CardReader::openFile(char* name, bool read, bool push_current/*=false*/) {
446 445
   }
447 446
 }
448 447
 
449
-void CardReader::removeFile(char* name) {
448
+void CardReader::removeFile(const char * const name) {
450 449
   if (!cardOK) return;
451 450
 
452 451
   stopSDPrint();
453 452
 
454 453
   SdFile myDir;
455 454
   curDir = &root;
456
-  char *fname = name;
455
+  const char *fname = name;
457 456
 
458 457
   char *dirname_start, *dirname_end;
459 458
   if (name[0] == '/') {
@@ -468,29 +467,23 @@ void CardReader::removeFile(char* name) {
468 467
         subdirname[dirname_end - dirname_start] = 0;
469 468
         SERIAL_ECHOLN(subdirname);
470 469
         if (!myDir.open(curDir, subdirname, O_READ)) {
471
-          SERIAL_PROTOCOLPAIR("open failed, File: ", subdirname);
470
+          SERIAL_PROTOCOLPAIR(MSG_SD_OPEN_FILE_FAIL, subdirname);
472 471
           SERIAL_PROTOCOLCHAR('.');
473 472
           SERIAL_EOL();
474 473
           return;
475 474
         }
476
-        else {
477
-          //SERIAL_ECHOLNPGM("dive ok");
478
-        }
479 475
 
480 476
         curDir = &myDir;
481 477
         dirname_start = dirname_end + 1;
482 478
       }
483
-      else { // the remainder after all /fsa/fdsa/ is the filename
479
+      else {
484 480
         fname = dirname_start;
485
-        //SERIAL_ECHOLNPGM("remainder");
486
-        //SERIAL_ECHOLN(fname);
487 481
         break;
488 482
       }
489 483
     }
490 484
   }
491
-  else { // relative path
485
+  else // Relative paths are rooted in the current directory
492 486
     curDir = &workDir;
493
-  }
494 487
 
495 488
   if (file.remove(curDir, fname)) {
496 489
     SERIAL_PROTOCOLPGM("File deleted:");
@@ -514,14 +507,13 @@ void CardReader::getStatus() {
514 507
     SERIAL_PROTOCOLCHAR('/');
515 508
     SERIAL_PROTOCOLLN(filesize);
516 509
   }
517
-  else {
510
+  else
518 511
     SERIAL_PROTOCOLLNPGM(MSG_SD_NOT_PRINTING);
519
-  }
520 512
 }
521 513
 
522 514
 void CardReader::write_command(char *buf) {
523 515
   char* begin = buf;
524
-  char* npos = 0;
516
+  char* npos = NULL;
525 517
   char* end = buf + strlen(buf) - 1;
526 518
 
527 519
   file.writeError = false;
@@ -619,20 +611,20 @@ uint16_t CardReader::getnrfilenames() {
619 611
 }
620 612
 
621 613
 void CardReader::chdir(const char * relpath) {
622
-  SdFile newfile;
614
+  SdFile newDir;
623 615
   SdFile *parent = &root;
624 616
 
625 617
   if (workDir.isOpen()) parent = &workDir;
626 618
 
627
-  if (!newfile.open(*parent, relpath, O_READ)) {
619
+  if (!newDir.open(*parent, relpath, O_READ)) {
628 620
     SERIAL_ECHO_START();
629 621
     SERIAL_ECHOPGM(MSG_SD_CANT_ENTER_SUBDIR);
630 622
     SERIAL_ECHOLN(relpath);
631 623
   }
632 624
   else {
625
+    workDir = newDir;
633 626
     if (workDirDepth < MAX_DIR_DEPTH)
634
-      workDirParents[workDirDepth++] = *parent;
635
-    workDir = newfile;
627
+      workDirParents[workDirDepth++] = workDir;
636 628
     #if ENABLED(SDCARD_SORT_ALPHA)
637 629
       presort();
638 630
     #endif
@@ -640,8 +632,8 @@ void CardReader::chdir(const char * relpath) {
640 632
 }
641 633
 
642 634
 void CardReader::updir() {
643
-  if (workDirDepth > 0) {
644
-    workDir = workDirParents[--workDirDepth];
635
+  if (workDirDepth > 0) {                                           // At least 1 dir has been saved
636
+    workDir = --workDirDepth ? workDirParents[workDirDepth] : root; // Use parent, or root if none
645 637
     #if ENABLED(SDCARD_SORT_ALPHA)
646 638
       presort();
647 639
     #endif

+ 10
- 9
Marlin/src/sd/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
 #define MAX_DIR_DEPTH 10          // Maximum folder depth
27 27
 
@@ -35,13 +35,15 @@ public:
35 35
 
36 36
   void initsd();
37 37
   void write_command(char *buf);
38
-  //files auto[0-9].g on the sd card are performed in a row
39
-  //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
38
+  // Files auto[0-9].g on the sd card are performed in sequence.
39
+  // This is to delay autostart and hence the initialisation of
40
+  // the sd card to some seconds after the normal init, so the
41
+  // device is available soon after a reset.
40 42
 
41 43
   void checkautostart(bool x);
42
-  void openFile(char* name, bool read, bool push_current=false);
44
+  void openFile(char* name, const bool read, const bool subcall=false);
43 45
   void openLogFile(char* name);
44
-  void removeFile(char* name);
46
+  void removeFile(const char * const name);
45 47
   void closefile(bool store_location=false);
46 48
   void release();
47 49
   void openAndPrintFile(const char *name);
@@ -151,8 +153,7 @@ private:
151 153
   uint8_t file_subcall_ctr;
152 154
   uint32_t filespos[SD_PROCEDURE_DEPTH];
153 155
   char proc_filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH];
154
-  uint32_t filesize;
155
-  uint32_t sdpos;
156
+  uint32_t filesize, sdpos;
156 157
 
157 158
   millis_t next_autostart_ms;
158 159
   bool autostart_stilltocheck; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
@@ -188,4 +189,4 @@ private:
188 189
 
189 190
 extern CardReader card;
190 191
 
191
-#endif // CARDREADER_H
192
+#endif // _CARDREADER_H_

Loading…
Cancel
Save