Преглед изворни кода

Misc. improvements (#12747)

* Make ExtUI respect MAXTEMP limits
  - Temperatures are now clamped by MAXTEMP limits rather than arbitrary values.
* Speed up USB init, add status
  - Speed up USB initialization
  - Show status message if init failed
* Enable status messages for EXTENSIBLE_UI
* Adjust max limit to MAX_TEMP - 15
* Misc. tweaks to formatting, const, etc.
Marcio Teixeira пре 6 година
родитељ
комит
60cb36bef3

+ 8
- 15
Marlin/src/HAL/HAL_LPC1768/u8g/u8g_com_HAL_LPC1768_st7920_hw_spi.cpp Прегледај датотеку

@@ -80,20 +80,13 @@ static uint8_t rs_last_state = 255;
80 80
 
81 81
 static void u8g_com_LPC1768_st7920_write_byte_hw_spi(uint8_t rs, uint8_t val) {
82 82
 
83
-  if ( rs != rs_last_state) {  // time to send a command/data byte
83
+  if (rs != rs_last_state) {      // Time to send a command/data byte
84 84
     rs_last_state = rs;
85
-
86
-    if ( rs == 0 )
87
-      /* command */
88
-      spiSend(0x0F8);
89
-    else
90
-       /* data */
91
-      spiSend(0x0FA);
92
-
93
-    DELAY_US(40); // give the controller some time to process the data: 20 is bad, 30 is OK, 40 is safe
85
+    spiSend(rs ? 0x0FA : 0x0F8);  // Send data or command
86
+    DELAY_US(40);                 // Give the controller some time: 20 is bad, 30 is OK, 40 is safe
94 87
   }
95 88
 
96
-  spiSend(val & 0x0F0);
89
+  spiSend(val & 0xF0);
97 90
   spiSend(val << 4);
98 91
 }
99 92
 
@@ -104,8 +97,8 @@ uint8_t u8g_com_HAL_LPC1768_ST7920_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t ar
104 97
       u8g_SetPIOutput(u8g, U8G_PI_CS);
105 98
       u8g_Delay(5);
106 99
       spiBegin();
107
-      spiInit(SPI_EIGHTH_SPEED);  // ST7920 max speed is about 1.1 MHz
108
-      u8g->pin_list[U8G_PI_A0_STATE] = 0;       /* inital RS state: command mode */
100
+      spiInit(SPI_EIGHTH_SPEED);            // ST7920 max speed is about 1.1 MHz
101
+      u8g->pin_list[U8G_PI_A0_STATE] = 0;   // initial RS state: command mode
109 102
       break;
110 103
 
111 104
     case U8G_COM_MSG_STOP:
@@ -115,12 +108,12 @@ uint8_t u8g_com_HAL_LPC1768_ST7920_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t ar
115 108
       u8g_SetPILevel(u8g, U8G_PI_RESET, arg_val);
116 109
       break;
117 110
 
118
-    case U8G_COM_MSG_ADDRESS:                     /* define cmd (arg_val = 0) or data mode (arg_val = 1) */
111
+    case U8G_COM_MSG_ADDRESS:                   // Define cmd (arg_val = 0) or data mode (arg_val = 1)
119 112
       u8g->pin_list[U8G_PI_A0_STATE] = arg_val;
120 113
       break;
121 114
 
122 115
     case U8G_COM_MSG_CHIP_SELECT:
123
-      u8g_SetPILevel(u8g, U8G_PI_CS, arg_val);  //note: the st7920 has an active high chip select
116
+      u8g_SetPILevel(u8g, U8G_PI_CS, arg_val);  // Note: the ST7920 has an active high chip-select
124 117
       break;
125 118
 
126 119
     case U8G_COM_MSG_WRITE_BYTE:

+ 1
- 1
Marlin/src/gcode/temperature/M104_M109.cpp Прегледај датотеку

@@ -125,7 +125,7 @@ void GcodeSuite::M109() {
125 125
         print_job_timer.start();
126 126
     #endif
127 127
 
128
-    #if ENABLED(ULTRA_LCD)
128
+    #if ENABLED(ULTRA_LCD) || ENABLED(EXTENSIBLE_UI)
129 129
       if (thermalManager.isHeatingHotend(target_extruder) || !no_wait_for_cooling)
130 130
         thermalManager.set_heating_message(target_extruder);
131 131
     #endif

+ 7
- 3
Marlin/src/lcd/extensible_ui/ui_api.cpp Прегледај датотеку

@@ -544,16 +544,20 @@ namespace ExtUI {
544 544
   }
545 545
 
546 546
   void setTargetTemp_celsius(float value, const heater_t heater) {
547
+    constexpr int16_t heater_maxtemp[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP, HEATER_2_MAXTEMP, HEATER_3_MAXTEMP, HEATER_4_MAXTEMP);
548
+    const int16_t e = heater - H0;
547 549
     #if HAS_HEATED_BED
548 550
       if (heater == BED)
549
-        thermalManager.setTargetBed(clamp(value,0,200));
551
+        thermalManager.setTargetBed(clamp(value, 0, BED_MAXTEMP - 15));
550 552
       else
551 553
     #endif
552
-        thermalManager.setTargetHotend(clamp(value,0,500), heater - H0);
554
+        thermalManager.setTargetHotend(clamp(value, 0, heater_maxtemp[e] - 15), e);
553 555
   }
554 556
 
555 557
   void setTargetTemp_celsius(float value, const extruder_t extruder) {
556
-    thermalManager.setTargetHotend(clamp(value,0,500), extruder - E0);
558
+    constexpr int16_t heater_maxtemp[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP, HEATER_2_MAXTEMP, HEATER_3_MAXTEMP, HEATER_4_MAXTEMP);
559
+    const int16_t e = extruder - E0;
560
+    thermalManager.setTargetHotend(clamp(value, 0, heater_maxtemp[e] - 15), e);
557 561
   }
558 562
 
559 563
   void setFan_percent(float value, const fan_t fan) {

+ 1
- 1
Marlin/src/lcd/menu/menu_temperature.cpp Прегледај датотеку

@@ -44,7 +44,7 @@ uint8_t MarlinUI::preheat_fan_speed[2];
44 44
 //
45 45
 
46 46
 void _lcd_preheat(const int16_t endnum, const int16_t temph, const int16_t tempb, const uint8_t fan) {
47
-  if (temph > 0) thermalManager.setTargetHotend(MIN(heater_maxtemp[endnum], temph), endnum);
47
+  if (temph > 0) thermalManager.setTargetHotend(MIN(heater_maxtemp[endnum] - 15, temph), endnum);
48 48
   #if HAS_HEATED_BED
49 49
     if (tempb >= 0) thermalManager.setTargetBed(tempb);
50 50
   #else

+ 2
- 2
Marlin/src/module/motion.cpp Прегледај датотеку

@@ -47,7 +47,7 @@
47 47
   #include "../feature/bedlevel/bedlevel.h"
48 48
 #endif
49 49
 
50
-#if HAS_AXIS_UNHOMED_ERR && ENABLED(ULTRA_LCD)
50
+#if HAS_AXIS_UNHOMED_ERR && (ENABLED(ULTRA_LCD) || ENABLED(EXTENSIBLE_UI))
51 51
   #include "../lcd/ultralcd.h"
52 52
 #endif
53 53
 
@@ -1019,7 +1019,7 @@ void prepare_move_to_destination() {
1019 1019
       if (zz) SERIAL_ECHOPGM(MSG_Z);
1020 1020
       SERIAL_ECHOLNPGM(" " MSG_FIRST);
1021 1021
 
1022
-      #if ENABLED(ULTRA_LCD)
1022
+      #if ENABLED(ULTRA_LCD) || ENABLED(EXTENSIBLE_UI)
1023 1023
         ui.status_printf_P(0, PSTR(MSG_HOME " %s%s%s " MSG_FIRST), xx ? MSG_X : "", yy ? MSG_Y : "", zz ? MSG_Z : "");
1024 1024
       #endif
1025 1025
       return true;

+ 1
- 1
Marlin/src/module/temperature.cpp Прегледај датотеку

@@ -2470,7 +2470,7 @@ void Temperature::isr() {
2470 2470
 
2471 2471
   #endif // AUTO_REPORT_TEMPERATURES
2472 2472
 
2473
-  #if ENABLED(ULTRA_LCD)
2473
+  #if ENABLED(ULTRA_LCD) || ENABLED(EXTENSIBLE_UI)
2474 2474
     void Temperature::set_heating_message(const uint8_t e) {
2475 2475
       const bool heating = isHeatingHotend(e);
2476 2476
       #if HOTENDS > 1

+ 1
- 1
Marlin/src/module/tool_change.cpp Прегледај датотеку

@@ -681,7 +681,7 @@ void tool_change(const uint8_t tmp_extruder, const float fr_mm_s/*=0.0*/, bool n
681 681
           singlenozzle_temp[active_extruder] = thermalManager.target_temperature[0];
682 682
           if (singlenozzle_temp[tmp_extruder] && singlenozzle_temp[tmp_extruder] != singlenozzle_temp[active_extruder]) {
683 683
             thermalManager.setTargetHotend(singlenozzle_temp[tmp_extruder], 0);
684
-            #if ENABLED(ULTRA_LCD)
684
+            #if ENABLED(ULTRA_LCD) || ENABLED(EXTENSIBLE_UI)
685 685
               thermalManager.set_heating_message(0);
686 686
             #endif
687 687
             (void)thermalManager.wait_for_hotend(0, false);  // Wait for heating or cooling

+ 121
- 144
Marlin/src/sd/Sd2Card.cpp Прегледај датотеку

@@ -65,7 +65,7 @@
65 65
 
66 66
     static uint8_t CRC7(const uint8_t* data, uint8_t n) {
67 67
       uint8_t crc = 0;
68
-      while ( n > 0 ) {
68
+      while (n > 0) {
69 69
         crc = pgm_read_byte(&crctab7[ (crc << 1) ^ *data++ ]);
70 70
         n--;
71 71
       }
@@ -87,44 +87,42 @@
87 87
   #endif
88 88
 #endif
89 89
 
90
-// send command and return error code.  Return zero for OK
91
-uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) {
92
-  // select card
90
+// Send command and return error code. Return zero for OK
91
+uint8_t Sd2Card::cardCommand(const uint8_t cmd, const uint32_t arg) {
92
+  // Select card
93 93
   chipSelect();
94 94
 
95
-  // wait up to 300 ms if busy
96
-  waitNotBusy( SD_WRITE_TIMEOUT );
95
+  // Wait up to 300 ms if busy
96
+  waitNotBusy(SD_WRITE_TIMEOUT);
97 97
 
98 98
   uint8_t *pa = (uint8_t *)(&arg);
99 99
 
100
-#if ENABLED(SD_CHECK_AND_RETRY)
100
+  #if ENABLED(SD_CHECK_AND_RETRY)
101 101
 
102
-  // form message
103
-  uint8_t d[6] = {(uint8_t) (cmd | 0x40), pa[3], pa[2], pa[1], pa[0] };
102
+    // Form message
103
+    uint8_t d[6] = {(uint8_t) (cmd | 0x40), pa[3], pa[2], pa[1], pa[0] };
104 104
 
105
-  // add crc
106
-  d[5] = CRC7(d, 5);
105
+    // Add crc
106
+    d[5] = CRC7(d, 5);
107 107
 
108
-  // send message
109
-  for (uint8_t k = 0; k < 6; k++ )
110
-    spiSend( d[k] );
108
+    // Send message
109
+    for (uint8_t k = 0; k < 6; k++) spiSend(d[k]);
111 110
 
112
-#else
113
-  // send command
114
-  spiSend(cmd | 0x40);
111
+  #else
112
+    // Send command
113
+    spiSend(cmd | 0x40);
115 114
 
116
-  // send argument
117
-  for( int8_t i = 3; i >= 0; i-- )
118
-    spiSend( pa[i] );
115
+    // Send argument
116
+    for (int8_t i = 3; i >= 0; i--) spiSend(pa[i]);
119 117
 
120
-  // send CRC - correct for CMD0 with arg zero or CMD8 with arg 0X1AA
121
-  spiSend( cmd == CMD0 ? 0X95 : 0X87 );
122
-#endif
118
+    // Send CRC - correct for CMD0 with arg zero or CMD8 with arg 0X1AA
119
+    spiSend(cmd == CMD0 ? 0X95 : 0X87);
120
+  #endif
123 121
 
124
-  // skip stuff byte for stop read
122
+  // Skip stuff byte for stop read
125 123
   if (cmd == CMD12) spiRec();
126 124
 
127
-  // wait for response
125
+  // Wait for response
128 126
   for (uint8_t i = 0; ((status_ = spiRec()) & 0x80) && i != 0xFF; i++) { /* Intentionally left empty */ }
129 127
   return status_;
130 128
 }
@@ -159,9 +157,7 @@ uint32_t Sd2Card::cardSize() {
159 157
 
160 158
 void Sd2Card::chipDeselect() {
161 159
   digitalWrite(chipSelectPin_, HIGH);
162
-
163
-  // insure MISO goes high impedance
164
-  spiSend( 0xFF );
160
+  spiSend(0xFF); // Ensure MISO goes high impedance
165 161
 }
166 162
 
167 163
 void Sd2Card::chipSelect() {
@@ -195,13 +191,8 @@ bool Sd2Card::erase(uint32_t firstBlock, uint32_t lastBlock) {
195 191
       goto FAIL;
196 192
     }
197 193
   }
198
-  if (type_ != SD_CARD_TYPE_SDHC) {
199
-    firstBlock <<= 9;
200
-    lastBlock <<= 9;
201
-  }
202
-  if (cardCommand(CMD32, firstBlock)
203
-      || cardCommand(CMD33, lastBlock)
204
-      || cardCommand(CMD38, 0)) {
194
+  if (type_ != SD_CARD_TYPE_SDHC) { firstBlock <<= 9; lastBlock <<= 9; }
195
+  if (cardCommand(CMD32, firstBlock) || cardCommand(CMD33, lastBlock) || cardCommand(CMD38, 0)) {
205 196
     error(SD_CARD_ERROR_ERASE);
206 197
     goto FAIL;
207 198
   }
@@ -236,7 +227,7 @@ bool Sd2Card::eraseSingleBlockEnable() {
236 227
  * \return true for success, false for failure.
237 228
  * The reason for failure can be determined by calling errorCode() and errorData().
238 229
  */
239
-bool Sd2Card::init(uint8_t sckRateID, pin_t chipSelectPin) {
230
+bool Sd2Card::init(const uint8_t sckRateID/*=0*/, const pin_t chipSelectPin/*=SD_CHIP_SELECT_PIN*/) {
240 231
   errorCode_ = type_ = 0;
241 232
   chipSelectPin_ = chipSelectPin;
242 233
   // 16-bit init start time allows over a minute
@@ -308,23 +299,23 @@ bool Sd2Card::init(uint8_t sckRateID, pin_t chipSelectPin) {
308 299
     watchdog_reset();
309 300
   #endif
310 301
 
311
-  // initialize card and send host supports SDHC if SD2
302
+  // Initialize card and send host supports SDHC if SD2
312 303
   arg = type() == SD_CARD_TYPE_SD2 ? 0x40000000 : 0;
313 304
   while ((status_ = cardAcmd(ACMD41, arg)) != R1_READY_STATE) {
314
-    // check for timeout
305
+    // Check for timeout
315 306
     if (ELAPSED(millis(), init_timeout)) {
316 307
       error(SD_CARD_ERROR_ACMD41);
317 308
       goto FAIL;
318 309
     }
319 310
   }
320
-  // if SD2 read OCR register to check for SDHC card
311
+  // If SD2 read OCR register to check for SDHC card
321 312
   if (type() == SD_CARD_TYPE_SD2) {
322 313
     if (cardCommand(CMD58, 0)) {
323 314
       error(SD_CARD_ERROR_CMD58);
324 315
       goto FAIL;
325 316
     }
326 317
     if ((spiRec() & 0xC0) == 0xC0) type(SD_CARD_TYPE_SDHC);
327
-    // discard rest of ocr - contains allowed voltage range
318
+    // Discard rest of ocr - contains allowed voltage range
328 319
     for (uint8_t i = 0; i < 3; i++) spiRec();
329 320
   }
330 321
   chipDeselect();
@@ -344,8 +335,7 @@ bool Sd2Card::init(uint8_t sckRateID, pin_t chipSelectPin) {
344 335
  * \return true for success, false for failure.
345 336
  */
346 337
 bool Sd2Card::readBlock(uint32_t blockNumber, uint8_t* dst) {
347
-  // use address if not SDHC card
348
-  if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
338
+  if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;   // Use address if not SDHC card
349 339
 
350 340
   #if ENABLED(SD_CHECK_AND_RETRY)
351 341
     uint8_t retryCnt = 3;
@@ -447,44 +437,39 @@ bool Sd2Card::readData(uint8_t* dst) {
447 437
   #endif
448 438
 #endif // SD_CHECK_AND_RETRY
449 439
 
450
-bool Sd2Card::readData(uint8_t* dst, uint16_t count) {
451
-  // wait for start block token
440
+bool Sd2Card::readData(uint8_t* dst, const uint16_t count) {
441
+  bool success = false;
442
+
452 443
   const millis_t read_timeout = millis() + SD_READ_TIMEOUT;
453
-  while ((status_ = spiRec()) == 0xFF) {
444
+  while ((status_ = spiRec()) == 0xFF) {      // Wait for start block token
454 445
     if (ELAPSED(millis(), read_timeout)) {
455 446
       error(SD_CARD_ERROR_READ_TIMEOUT);
456 447
       goto FAIL;
457 448
     }
458 449
   }
459
-  if (status_ != DATA_START_BLOCK) {
460
-    error(SD_CARD_ERROR_READ);
461
-    goto FAIL;
462
-  }
463
-  // transfer data
464
-  spiRead(dst, count);
465 450
 
466
-#if ENABLED(SD_CHECK_AND_RETRY)
467
-  {
468
-    uint16_t recvCrc = (spiRec() << 8) | spiRec();
469
-    if (crcSupported && recvCrc != CRC_CCITT(dst, count)) {
470
-      error(SD_CARD_ERROR_READ_CRC);
471
-      goto FAIL;
472
-    }
451
+  if (status_ == DATA_START_BLOCK) {
452
+    spiRead(dst, count);                      // Transfer data
453
+
454
+    const uint16_t recvCrc = (spiRec() << 8) | spiRec();
455
+    #if ENABLED(SD_CHECK_AND_RETRY)
456
+      success = !crcSupported || recvCrc == CRC_CCITT(dst, count);
457
+      if (!success) error(SD_CARD_ERROR_READ_CRC);
458
+    #else
459
+      success = true;
460
+      UNUSED(recvCrc);
461
+    #endif
473 462
   }
474
-#else
475
-  // discard CRC
476
-  spiRec();
477
-  spiRec();
478
-#endif
479
-  chipDeselect();
480
-  return true;
463
+  else
464
+    error(SD_CARD_ERROR_READ);
465
+
481 466
   FAIL:
482 467
   chipDeselect();
483
-  return false;
468
+  return success;
484 469
 }
485 470
 
486 471
 /** read CID or CSR register */
487
-bool Sd2Card::readRegister(uint8_t cmd, void* buf) {
472
+bool Sd2Card::readRegister(const uint8_t cmd, void* buf) {
488 473
   uint8_t* dst = reinterpret_cast<uint8_t*>(buf);
489 474
   if (cardCommand(cmd, 0)) {
490 475
     error(SD_CARD_ERROR_READ_REG);
@@ -506,13 +491,11 @@ bool Sd2Card::readRegister(uint8_t cmd, void* buf) {
506 491
  */
507 492
 bool Sd2Card::readStart(uint32_t blockNumber) {
508 493
   if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
509
-  if (cardCommand(CMD18, blockNumber)) {
510
-    error(SD_CARD_ERROR_CMD18);
511
-    chipDeselect();
512
-    return false;
513
-  }
494
+
495
+  const bool success = !cardCommand(CMD18, blockNumber);
496
+  if (!success) error(SD_CARD_ERROR_CMD18);
514 497
   chipDeselect();
515
-  return true;
498
+  return success;
516 499
 }
517 500
 
518 501
 /**
@@ -522,13 +505,10 @@ bool Sd2Card::readStart(uint32_t blockNumber) {
522 505
  */
523 506
 bool Sd2Card::readStop() {
524 507
   chipSelect();
525
-  if (cardCommand(CMD12, 0)) {
526
-    error(SD_CARD_ERROR_CMD12);
527
-    chipDeselect();
528
-    return false;
529
-  }
508
+  const bool success = !cardCommand(CMD12, 0);
509
+  if (!success) error(SD_CARD_ERROR_CMD12);
530 510
   chipDeselect();
531
-  return true;
511
+  return success;
532 512
 }
533 513
 
534 514
 /**
@@ -543,16 +523,20 @@ bool Sd2Card::readStop() {
543 523
  * \return The value one, true, is returned for success and the value zero,
544 524
  * false, is returned for an invalid value of \a sckRateID.
545 525
  */
546
-bool Sd2Card::setSckRate(uint8_t sckRateID) {
547
-  if (sckRateID > 6) {
526
+bool Sd2Card::setSckRate(const uint8_t sckRateID) {
527
+  const bool success = (sckRateID <= 6);
528
+  if (success) 
529
+    spiRate_ = sckRateID;
530
+  else
548 531
     error(SD_CARD_ERROR_SCK_RATE);
549
-    return false;
550
-  }
551
-  spiRate_ = sckRateID;
552
-  return true;
532
+  return success;
553 533
 }
554 534
 
555
-// wait for card to go not busy
535
+/**
536
+ * Wait for card to become not-busy
537
+ * \param[in] timeout_ms Timeout to abort.
538
+ * \return true for success, false for timeout.
539
+ */
556 540
 bool Sd2Card::waitNotBusy(const millis_t timeout_ms) {
557 541
   const millis_t wait_timeout = millis() + timeout_ms;
558 542
   while (spiRec() != 0xFF)
@@ -562,36 +546,31 @@ bool Sd2Card::waitNotBusy(const millis_t timeout_ms) {
562 546
 }
563 547
 
564 548
 /**
565
- * Writes a 512 byte block to an SD card.
549
+ * Write a 512 byte block to an SD card.
566 550
  *
567 551
  * \param[in] blockNumber Logical block to be written.
568 552
  * \param[in] src Pointer to the location of the data to be written.
569 553
  * \return true for success, false for failure.
570 554
  */
571 555
 bool Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) {
572
-  // use address if not SDHC card
573
-  if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
574
-  if (cardCommand(CMD24, blockNumber)) {
575
-    error(SD_CARD_ERROR_CMD24);
576
-    goto FAIL;
556
+  if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;   // Use address if not SDHC card
557
+
558
+  bool success = false;
559
+  if (!cardCommand(CMD24, blockNumber)) {
560
+    if (writeData(DATA_START_BLOCK, src)) {
561
+      if (waitNotBusy(SD_WRITE_TIMEOUT)) {              // Wait for flashing to complete
562
+        success = !(cardCommand(CMD13, 0) || spiRec()); // Response is r2 so get and check two bytes for nonzero
563
+        if (!success) error(SD_CARD_ERROR_WRITE_PROGRAMMING);
564
+      }
565
+      else
566
+        error(SD_CARD_ERROR_WRITE_TIMEOUT);
567
+    }
577 568
   }
578
-  if (!writeData(DATA_START_BLOCK, src)) goto FAIL;
569
+  else
570
+    error(SD_CARD_ERROR_CMD24);
579 571
 
580
-  // wait for flash programming to complete
581
-  if (!waitNotBusy(SD_WRITE_TIMEOUT)) {
582
-    error(SD_CARD_ERROR_WRITE_TIMEOUT);
583
-    goto FAIL;
584
-  }
585
-  // response is r2 so get and check two bytes for nonzero
586
-  if (cardCommand(CMD13, 0) || spiRec()) {
587
-    error(SD_CARD_ERROR_WRITE_PROGRAMMING);
588
-    goto FAIL;
589
-  }
590 572
   chipDeselect();
591
-  return true;
592
-  FAIL:
593
-  chipDeselect();
594
-  return false;
573
+  return success;
595 574
 }
596 575
 
597 576
 /**
@@ -600,28 +579,30 @@ bool Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) {
600 579
  * \return true for success, false for failure.
601 580
  */
602 581
 bool Sd2Card::writeData(const uint8_t* src) {
582
+  bool success = true;
603 583
   chipSelect();
604
-  // wait for previous write to finish
584
+  // Wait for previous write to finish
605 585
   if (!waitNotBusy(SD_WRITE_TIMEOUT) || !writeData(WRITE_MULTIPLE_TOKEN, src)) {
606 586
     error(SD_CARD_ERROR_WRITE_MULTIPLE);
607
-    chipDeselect();
608
-    return false;
587
+    success = false;
609 588
   }
610 589
   chipDeselect();
611
-  return true;
590
+  return success;
612 591
 }
613 592
 
614
-// send one block of data for write block or write multiple blocks
615
-bool Sd2Card::writeData(uint8_t token, const uint8_t* src) {
593
+// Send one block of data for write block or write multiple blocks
594
+bool Sd2Card::writeData(const uint8_t token, const uint8_t* src) {
616 595
 
617
-#if ENABLED(SD_CHECK_AND_RETRY)
618
-  uint16_t crc = CRC_CCITT( src, 512 );
619
-#else  // ENABLED(SD_CHECK_AND_RETRY)
620
-  uint16_t crc = 0xFFFF;
621
-#endif  // ENABLED(SD_CHECK_AND_RETRY)
622
-  spiSendBlock( token, src );
623
-  spiSend( crc >> 8 );
624
-  spiSend( crc & 0XFF );
596
+  uint16_t crc =
597
+    #if ENABLED(SD_CHECK_AND_RETRY)
598
+      CRC_CCITT(src, 512)
599
+    #else
600
+      0xFFFF
601
+    #endif
602
+  ;
603
+  spiSendBlock(token, src);
604
+  spiSend(crc >> 8);
605
+  spiSend(crc & 0xFF);
625 606
 
626 607
   status_ = spiRec();
627 608
   if ((status_ & DATA_RES_MASK) != DATA_RES_ACCEPTED) {
@@ -643,23 +624,18 @@ bool Sd2Card::writeData(uint8_t token, const uint8_t* src) {
643 624
  *
644 625
  * \return true for success, false for failure.
645 626
  */
646
-bool Sd2Card::writeStart(uint32_t blockNumber, uint32_t eraseCount) {
647
-  // send pre-erase count
648
-  if (cardAcmd(ACMD23, eraseCount)) {
649
-    error(SD_CARD_ERROR_ACMD23);
650
-    goto FAIL;
651
-  }
652
-  // use address if not SDHC card
653
-  if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
654
-  if (cardCommand(CMD25, blockNumber)) {
655
-    error(SD_CARD_ERROR_CMD25);
656
-    goto FAIL;
627
+bool Sd2Card::writeStart(uint32_t blockNumber, const uint32_t eraseCount) {
628
+  bool success = false;
629
+  if (!cardAcmd(ACMD23, eraseCount)) {                    // Send pre-erase count
630
+    if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;   // Use address if not SDHC card
631
+    success = !cardCommand(CMD25, blockNumber);
632
+    if (!success) error(SD_CARD_ERROR_CMD25);
657 633
   }
634
+  else
635
+    error(SD_CARD_ERROR_ACMD23);
636
+
658 637
   chipDeselect();
659
-  return true;
660
-  FAIL:
661
-  chipDeselect();
662
-  return false;
638
+  return success;
663 639
 }
664 640
 
665 641
 /**
@@ -668,16 +644,17 @@ bool Sd2Card::writeStart(uint32_t blockNumber, uint32_t eraseCount) {
668 644
  * \return true for success, false for failure.
669 645
  */
670 646
 bool Sd2Card::writeStop() {
647
+  bool success = false;
671 648
   chipSelect();
672
-  if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto FAIL;
673
-  spiSend(STOP_TRAN_TOKEN);
674
-  if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto FAIL;
675
-  chipDeselect();
676
-  return true;
677
-  FAIL:
678
-  error(SD_CARD_ERROR_STOP_TRAN);
649
+  if (waitNotBusy(SD_WRITE_TIMEOUT)) {
650
+    spiSend(STOP_TRAN_TOKEN);
651
+    success = waitNotBusy(SD_WRITE_TIMEOUT);
652
+  }
653
+  else
654
+    error(SD_CARD_ERROR_STOP_TRAN);
655
+
679 656
   chipDeselect();
680
-  return false;
657
+  return success;
681 658
 }
682 659
 
683 660
 #endif // SDSUPPORT

+ 17
- 16
Marlin/src/sd/Sd2Card.h Прегледај датотеку

@@ -112,7 +112,7 @@ uint8_t const SD_CARD_TYPE_SD1  = 1,                    // Standard capacity V1
112 112
  * \brief Raw access to SD and SDHC flash memory cards.
113 113
  */
114 114
 class Sd2Card {
115
-  public:
115
+public:
116 116
 
117 117
   Sd2Card() : errorCode_(SD_CARD_ERROR_INIT_NOT_CALLED), type_(0) {}
118 118
 
@@ -124,15 +124,15 @@ class Sd2Card {
124 124
    *  Set SD error code.
125 125
    *  \param[in] code value for error code.
126 126
    */
127
-  void error(uint8_t code) {errorCode_ = code;}
127
+  inline void error(const uint8_t code) { errorCode_ = code; }
128 128
 
129 129
   /**
130 130
    * \return error code for last error. See Sd2Card.h for a list of error codes.
131 131
    */
132
-  int errorCode() const {return errorCode_;}
132
+  inline int errorCode() const { return errorCode_; }
133 133
 
134 134
   /** \return error data for last error. */
135
-  int errorData() const {return status_;}
135
+  inline int errorData() const { return status_; }
136 136
 
137 137
   /**
138 138
    * Initialize an SD flash memory card with default clock rate and chip
@@ -140,8 +140,8 @@ class Sd2Card {
140 140
    *
141 141
    * \return true for success or false for failure.
142 142
    */
143
-  bool init(uint8_t sckRateID = SPI_FULL_SPEED,
144
-            pin_t chipSelectPin = SD_CHIP_SELECT_PIN);
143
+  bool init(const uint8_t sckRateID=SPI_FULL_SPEED, const pin_t chipSelectPin=SD_CHIP_SELECT_PIN);
144
+
145 145
   bool readBlock(uint32_t block, uint8_t* dst);
146 146
 
147 147
   /**
@@ -163,12 +163,13 @@ class Sd2Card {
163 163
    *
164 164
    * \return true for success or false for failure.
165 165
    */
166
-  bool readCSD(csd_t* csd) { return readRegister(CMD9, csd); }
166
+  inline bool readCSD(csd_t* csd) { return readRegister(CMD9, csd); }
167 167
 
168 168
   bool readData(uint8_t* dst);
169 169
   bool readStart(uint32_t blockNumber);
170 170
   bool readStop();
171
-  bool setSckRate(uint8_t sckRateID);
171
+  bool setSckRate(const uint8_t sckRateID);
172
+
172 173
   /**
173 174
    * Return the card type: SD V1, SD V2 or SDHC
174 175
    * \return 0 - SD V1, 1 - SD V2, or 3 - SDHC.
@@ -176,10 +177,10 @@ class Sd2Card {
176 177
   int type() const {return type_;}
177 178
   bool writeBlock(uint32_t blockNumber, const uint8_t* src);
178 179
   bool writeData(const uint8_t* src);
179
-  bool writeStart(uint32_t blockNumber, uint32_t eraseCount);
180
+  bool writeStart(uint32_t blockNumber, const uint32_t eraseCount);
180 181
   bool writeStop();
181 182
 
182
-  private:
183
+private:
183 184
   uint8_t chipSelectPin_,
184 185
           errorCode_,
185 186
           spiRate_,
@@ -187,17 +188,17 @@ class Sd2Card {
187 188
           type_;
188 189
 
189 190
   // private functions
190
-  uint8_t cardAcmd(uint8_t cmd, uint32_t arg) {
191
+  inline uint8_t cardAcmd(const uint8_t cmd, const uint32_t arg) {
191 192
     cardCommand(CMD55, 0);
192 193
     return cardCommand(cmd, arg);
193 194
   }
194
-  uint8_t cardCommand(uint8_t cmd, uint32_t arg);
195
+  uint8_t cardCommand(const uint8_t cmd, const uint32_t arg);
195 196
 
196
-  bool readData(uint8_t* dst, uint16_t count);
197
-  bool readRegister(uint8_t cmd, void* buf);
197
+  bool readData(uint8_t* dst, const uint16_t count);
198
+  bool readRegister(const uint8_t cmd, void* buf);
198 199
   void chipDeselect();
199 200
   void chipSelect();
200
-  void type(uint8_t value) { type_ = value; }
201
+  inline void type(const uint8_t value) { type_ = value; }
201 202
   bool waitNotBusy(const millis_t timeout_ms);
202
-  bool writeData(uint8_t token, const uint8_t* src);
203
+  bool writeData(const uint8_t token, const uint8_t* src);
203 204
 };

+ 1
- 1
Marlin/src/sd/cardreader.cpp Прегледај датотеку

@@ -1022,7 +1022,7 @@ void CardReader::printingHasFinished() {
1022 1022
       presort();
1023 1023
     #endif
1024 1024
 
1025
-    #if ENABLED(ULTRA_LCD) && ENABLED(LCD_SET_PROGRESS_MANUALLY)
1025
+    #if (ENABLED(ULTRA_LCD) || ENABLED(EXTENSIBLE_UI)) && ENABLED(LCD_SET_PROGRESS_MANUALLY)
1026 1026
       ui.progress_bar_percent = 0;
1027 1027
     #endif
1028 1028
 

+ 16
- 10
Marlin/src/sd/usb_flashdrive/Sd2Card_FlashDrive.cpp Прегледај датотеку

@@ -31,6 +31,10 @@
31 31
 
32 32
 #include "Sd2Card_FlashDrive.h"
33 33
 
34
+#if ENABLED(ULTRA_LCD) || ENABLED(EXTENSIBLE_UI)
35
+  #include "../../lcd/ultralcd.h"
36
+#endif
37
+
34 38
 USB usb;
35 39
 BulkOnly bulk(&usb);
36 40
 
@@ -46,25 +50,27 @@ void Sd2Card::idle() {
46 50
 
47 51
   switch (state) {
48 52
     case USB_HOST_DELAY_INIT:
49
-      next_retry = millis() + 10000;
53
+      next_retry = millis() + 2000;
50 54
       state = USB_HOST_WAITING;
51 55
       break;
52 56
     case USB_HOST_WAITING:
53 57
       if (ELAPSED(millis(), next_retry)) {
54
-        next_retry = millis() + 10000;
58
+        next_retry = millis() + 2000;
55 59
         state = USB_HOST_UNINITIALIZED;
56 60
       }
57 61
       break;
58 62
     case USB_HOST_UNINITIALIZED:
59
-      SERIAL_ECHOLNPGM("Starting USB host");
63
+      SERIAL_ECHOPGM("Starting USB host...");
60 64
       if (!usb.start()) {
61
-        SERIAL_ECHOLNPGM("USB host failed to start. Will retry in 10 seconds.");
65
+        SERIAL_ECHOPGM(" Failed. Retrying in 2s.");
66
+        #if ENABLED(ULTRA_LCD) || ENABLED(EXTENSIBLE_UI)
67
+          LCD_MESSAGEPGM("USB start failed");
68
+        #endif
62 69
         state = USB_HOST_DELAY_INIT;
63 70
       }
64
-      else {
65
-        SERIAL_ECHOLNPGM("USB host initialized");
71
+      else
66 72
         state = USB_HOST_INITIALIZED;
67
-      }
73
+      SERIAL_EOL();
68 74
       break;
69 75
     case USB_HOST_INITIALIZED:
70 76
       const uint8_t lastUsbTaskState = usb.getUsbTaskState();
@@ -91,10 +97,10 @@ void Sd2Card::idle() {
91 97
 // This is equivalent to polling the SD_DETECT when using SD cards.
92 98
 bool Sd2Card::isInserted() {
93 99
   return usb.getUsbTaskState() == USB_STATE_RUNNING;
94
-};
100
+}
95 101
 
96 102
 // Marlin calls this to initialize an SD card once it is inserted.
97
-bool Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) {
103
+bool Sd2Card::init(const uint8_t sckRateID/*=0*/, const pin_t chipSelectPin/*=SD_CHIP_SELECT_PIN*/) {
98 104
   if (!ready()) return false;
99 105
 
100 106
   if (!bulk.LUNIsGood(0)) {
@@ -121,7 +127,7 @@ uint32_t Sd2Card::cardSize() {
121 127
   #ifndef USB_DEBUG
122 128
     const uint32_t
123 129
   #endif
124
-  lun0_capacity = bulk.GetCapacity(0);
130
+      lun0_capacity = bulk.GetCapacity(0);
125 131
   return lun0_capacity;
126 132
 }
127 133
 

+ 12
- 14
Marlin/src/sd/usb_flashdrive/Sd2Card_FlashDrive.h Прегледај датотеку

@@ -32,7 +32,6 @@
32 32
  */
33 33
 //#define USB_DEBUG 1
34 34
 
35
-
36 35
 #include "../SdFatConfig.h"
37 36
 #include "../SdInfo.h"
38 37
 
@@ -61,11 +60,11 @@
61 60
 class Sd2Card {
62 61
   private:
63 62
 
64
-    typedef enum {
65
-      USB_HOST_DELAY_INIT,
66
-      USB_HOST_WAITING,
63
+    typedef enum : uint8_t {
67 64
       USB_HOST_UNINITIALIZED,
68
-      USB_HOST_INITIALIZED
65
+      USB_HOST_INITIALIZED,
66
+      USB_HOST_DELAY_INIT,
67
+      USB_HOST_WAITING
69 68
     } state_t;
70 69
 
71 70
     static state_t state;
@@ -75,21 +74,20 @@ class Sd2Card {
75 74
       uint32_t lun0_capacity;
76 75
     #endif
77 76
 
78
-    static inline bool ready() {return state == USB_HOST_INITIALIZED;}
77
+    static inline bool ready() { return state == USB_HOST_INITIALIZED; }
79 78
 
80 79
   public:
81
-    bool init(uint8_t sckRateID = 0, uint8_t chipSelectPin = SD_CHIP_SELECT_PIN);
80
+    bool init(const uint8_t sckRateID=0, const pin_t chipSelectPin=SD_CHIP_SELECT_PIN);
82 81
 
83 82
     static void idle();
84 83
 
85
-    bool readStart(uint32_t block)                       { pos = block; return ready(); }
86
-    bool readData(uint8_t* dst)                          { return readBlock(pos++, dst); }
87
-    bool readStop()                                      { return true; }
88
-
89
-    bool writeStart(uint32_t block, uint32_t eraseCount) { pos = block; return ready(); }
90
-    bool writeData(uint8_t* src)                         { return writeBlock(pos++, src); }
91
-    bool writeStop()                                     { return true; }
84
+    inline bool readStart(const uint32_t block)                             { pos = block; return ready(); }
85
+    inline bool readData(uint8_t* dst)                                      { return readBlock(pos++, dst); }
86
+    inline bool readStop() const                                            { return true; }
92 87
 
88
+    inline bool writeStart(const uint32_t block, const uint32_t eraseCount) { UNUSED(eraseCount); pos = block; return ready(); }
89
+    inline bool writeData(uint8_t* src)                                     { return writeBlock(pos++, src); }
90
+    inline bool writeStop() const                                           { return true; }
93 91
 
94 92
     bool readBlock(uint32_t block, uint8_t* dst);
95 93
     bool writeBlock(uint32_t blockNumber, const uint8_t* src);

Loading…
Откажи
Сачувај