From 60cb36bef3644640f2eb1c9d2b30189e41e81ef2 Mon Sep 17 00:00:00 2001 From: Marcio Teixeira Date: Tue, 1 Jan 2019 14:17:48 -0700 Subject: [PATCH] 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. --- .../u8g/u8g_com_HAL_LPC1768_st7920_hw_spi.cpp | 23 +- Marlin/src/gcode/temperature/M104_M109.cpp | 2 +- Marlin/src/lcd/extensible_ui/ui_api.cpp | 10 +- Marlin/src/lcd/menu/menu_temperature.cpp | 2 +- Marlin/src/module/motion.cpp | 4 +- Marlin/src/module/temperature.cpp | 2 +- Marlin/src/module/tool_change.cpp | 2 +- Marlin/src/sd/Sd2Card.cpp | 265 ++++++++---------- Marlin/src/sd/Sd2Card.h | 33 +-- Marlin/src/sd/cardreader.cpp | 2 +- .../sd/usb_flashdrive/Sd2Card_FlashDrive.cpp | 26 +- .../sd/usb_flashdrive/Sd2Card_FlashDrive.h | 26 +- 12 files changed, 188 insertions(+), 209 deletions(-) diff --git a/Marlin/src/HAL/HAL_LPC1768/u8g/u8g_com_HAL_LPC1768_st7920_hw_spi.cpp b/Marlin/src/HAL/HAL_LPC1768/u8g/u8g_com_HAL_LPC1768_st7920_hw_spi.cpp index 9139f1b8d8..c4b7966b68 100644 --- a/Marlin/src/HAL/HAL_LPC1768/u8g/u8g_com_HAL_LPC1768_st7920_hw_spi.cpp +++ b/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; static void u8g_com_LPC1768_st7920_write_byte_hw_spi(uint8_t rs, uint8_t val) { - if ( rs != rs_last_state) { // time to send a command/data byte + if (rs != rs_last_state) { // Time to send a command/data byte rs_last_state = rs; - - if ( rs == 0 ) - /* command */ - spiSend(0x0F8); - else - /* data */ - spiSend(0x0FA); - - DELAY_US(40); // give the controller some time to process the data: 20 is bad, 30 is OK, 40 is safe + spiSend(rs ? 0x0FA : 0x0F8); // Send data or command + DELAY_US(40); // Give the controller some time: 20 is bad, 30 is OK, 40 is safe } - spiSend(val & 0x0F0); + spiSend(val & 0xF0); spiSend(val << 4); } @@ -104,8 +97,8 @@ uint8_t u8g_com_HAL_LPC1768_ST7920_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t ar u8g_SetPIOutput(u8g, U8G_PI_CS); u8g_Delay(5); spiBegin(); - spiInit(SPI_EIGHTH_SPEED); // ST7920 max speed is about 1.1 MHz - u8g->pin_list[U8G_PI_A0_STATE] = 0; /* inital RS state: command mode */ + spiInit(SPI_EIGHTH_SPEED); // ST7920 max speed is about 1.1 MHz + u8g->pin_list[U8G_PI_A0_STATE] = 0; // initial RS state: command mode break; 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 u8g_SetPILevel(u8g, U8G_PI_RESET, arg_val); break; - case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */ + case U8G_COM_MSG_ADDRESS: // Define cmd (arg_val = 0) or data mode (arg_val = 1) u8g->pin_list[U8G_PI_A0_STATE] = arg_val; break; case U8G_COM_MSG_CHIP_SELECT: - u8g_SetPILevel(u8g, U8G_PI_CS, arg_val); //note: the st7920 has an active high chip select + u8g_SetPILevel(u8g, U8G_PI_CS, arg_val); // Note: the ST7920 has an active high chip-select break; case U8G_COM_MSG_WRITE_BYTE: diff --git a/Marlin/src/gcode/temperature/M104_M109.cpp b/Marlin/src/gcode/temperature/M104_M109.cpp index 39d65dc1a6..fede0fa8ff 100644 --- a/Marlin/src/gcode/temperature/M104_M109.cpp +++ b/Marlin/src/gcode/temperature/M104_M109.cpp @@ -125,7 +125,7 @@ void GcodeSuite::M109() { print_job_timer.start(); #endif - #if ENABLED(ULTRA_LCD) + #if ENABLED(ULTRA_LCD) || ENABLED(EXTENSIBLE_UI) if (thermalManager.isHeatingHotend(target_extruder) || !no_wait_for_cooling) thermalManager.set_heating_message(target_extruder); #endif diff --git a/Marlin/src/lcd/extensible_ui/ui_api.cpp b/Marlin/src/lcd/extensible_ui/ui_api.cpp index a9dabaa0ac..2c756cdb39 100644 --- a/Marlin/src/lcd/extensible_ui/ui_api.cpp +++ b/Marlin/src/lcd/extensible_ui/ui_api.cpp @@ -544,16 +544,20 @@ namespace ExtUI { } void setTargetTemp_celsius(float value, const heater_t heater) { + constexpr int16_t heater_maxtemp[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP, HEATER_2_MAXTEMP, HEATER_3_MAXTEMP, HEATER_4_MAXTEMP); + const int16_t e = heater - H0; #if HAS_HEATED_BED if (heater == BED) - thermalManager.setTargetBed(clamp(value,0,200)); + thermalManager.setTargetBed(clamp(value, 0, BED_MAXTEMP - 15)); else #endif - thermalManager.setTargetHotend(clamp(value,0,500), heater - H0); + thermalManager.setTargetHotend(clamp(value, 0, heater_maxtemp[e] - 15), e); } void setTargetTemp_celsius(float value, const extruder_t extruder) { - thermalManager.setTargetHotend(clamp(value,0,500), extruder - E0); + constexpr int16_t heater_maxtemp[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_MAXTEMP, HEATER_1_MAXTEMP, HEATER_2_MAXTEMP, HEATER_3_MAXTEMP, HEATER_4_MAXTEMP); + const int16_t e = extruder - E0; + thermalManager.setTargetHotend(clamp(value, 0, heater_maxtemp[e] - 15), e); } void setFan_percent(float value, const fan_t fan) { diff --git a/Marlin/src/lcd/menu/menu_temperature.cpp b/Marlin/src/lcd/menu/menu_temperature.cpp index 2883fe752e..e0d559025e 100644 --- a/Marlin/src/lcd/menu/menu_temperature.cpp +++ b/Marlin/src/lcd/menu/menu_temperature.cpp @@ -44,7 +44,7 @@ uint8_t MarlinUI::preheat_fan_speed[2]; // void _lcd_preheat(const int16_t endnum, const int16_t temph, const int16_t tempb, const uint8_t fan) { - if (temph > 0) thermalManager.setTargetHotend(MIN(heater_maxtemp[endnum], temph), endnum); + if (temph > 0) thermalManager.setTargetHotend(MIN(heater_maxtemp[endnum] - 15, temph), endnum); #if HAS_HEATED_BED if (tempb >= 0) thermalManager.setTargetBed(tempb); #else diff --git a/Marlin/src/module/motion.cpp b/Marlin/src/module/motion.cpp index be0a6cda80..803ddbc8c0 100644 --- a/Marlin/src/module/motion.cpp +++ b/Marlin/src/module/motion.cpp @@ -47,7 +47,7 @@ #include "../feature/bedlevel/bedlevel.h" #endif -#if HAS_AXIS_UNHOMED_ERR && ENABLED(ULTRA_LCD) +#if HAS_AXIS_UNHOMED_ERR && (ENABLED(ULTRA_LCD) || ENABLED(EXTENSIBLE_UI)) #include "../lcd/ultralcd.h" #endif @@ -1019,7 +1019,7 @@ void prepare_move_to_destination() { if (zz) SERIAL_ECHOPGM(MSG_Z); SERIAL_ECHOLNPGM(" " MSG_FIRST); - #if ENABLED(ULTRA_LCD) + #if ENABLED(ULTRA_LCD) || ENABLED(EXTENSIBLE_UI) ui.status_printf_P(0, PSTR(MSG_HOME " %s%s%s " MSG_FIRST), xx ? MSG_X : "", yy ? MSG_Y : "", zz ? MSG_Z : ""); #endif return true; diff --git a/Marlin/src/module/temperature.cpp b/Marlin/src/module/temperature.cpp index cd0a66d6f4..8adcd8c67e 100644 --- a/Marlin/src/module/temperature.cpp +++ b/Marlin/src/module/temperature.cpp @@ -2470,7 +2470,7 @@ void Temperature::isr() { #endif // AUTO_REPORT_TEMPERATURES - #if ENABLED(ULTRA_LCD) + #if ENABLED(ULTRA_LCD) || ENABLED(EXTENSIBLE_UI) void Temperature::set_heating_message(const uint8_t e) { const bool heating = isHeatingHotend(e); #if HOTENDS > 1 diff --git a/Marlin/src/module/tool_change.cpp b/Marlin/src/module/tool_change.cpp index 81dbe9043f..2125736cf5 100644 --- a/Marlin/src/module/tool_change.cpp +++ b/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 singlenozzle_temp[active_extruder] = thermalManager.target_temperature[0]; if (singlenozzle_temp[tmp_extruder] && singlenozzle_temp[tmp_extruder] != singlenozzle_temp[active_extruder]) { thermalManager.setTargetHotend(singlenozzle_temp[tmp_extruder], 0); - #if ENABLED(ULTRA_LCD) + #if ENABLED(ULTRA_LCD) || ENABLED(EXTENSIBLE_UI) thermalManager.set_heating_message(0); #endif (void)thermalManager.wait_for_hotend(0, false); // Wait for heating or cooling diff --git a/Marlin/src/sd/Sd2Card.cpp b/Marlin/src/sd/Sd2Card.cpp index d1116cdb35..0f959a3ccb 100644 --- a/Marlin/src/sd/Sd2Card.cpp +++ b/Marlin/src/sd/Sd2Card.cpp @@ -65,7 +65,7 @@ static uint8_t CRC7(const uint8_t* data, uint8_t n) { uint8_t crc = 0; - while ( n > 0 ) { + while (n > 0) { crc = pgm_read_byte(&crctab7[ (crc << 1) ^ *data++ ]); n--; } @@ -87,44 +87,42 @@ #endif #endif -// send command and return error code. Return zero for OK -uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) { - // select card +// Send command and return error code. Return zero for OK +uint8_t Sd2Card::cardCommand(const uint8_t cmd, const uint32_t arg) { + // Select card chipSelect(); - // wait up to 300 ms if busy - waitNotBusy( SD_WRITE_TIMEOUT ); + // Wait up to 300 ms if busy + waitNotBusy(SD_WRITE_TIMEOUT); uint8_t *pa = (uint8_t *)(&arg); -#if ENABLED(SD_CHECK_AND_RETRY) + #if ENABLED(SD_CHECK_AND_RETRY) - // form message - uint8_t d[6] = {(uint8_t) (cmd | 0x40), pa[3], pa[2], pa[1], pa[0] }; + // Form message + uint8_t d[6] = {(uint8_t) (cmd | 0x40), pa[3], pa[2], pa[1], pa[0] }; - // add crc - d[5] = CRC7(d, 5); + // Add crc + d[5] = CRC7(d, 5); - // send message - for (uint8_t k = 0; k < 6; k++ ) - spiSend( d[k] ); + // Send message + for (uint8_t k = 0; k < 6; k++) spiSend(d[k]); -#else - // send command - spiSend(cmd | 0x40); + #else + // Send command + spiSend(cmd | 0x40); - // send argument - for( int8_t i = 3; i >= 0; i-- ) - spiSend( pa[i] ); + // Send argument + for (int8_t i = 3; i >= 0; i--) spiSend(pa[i]); - // send CRC - correct for CMD0 with arg zero or CMD8 with arg 0X1AA - spiSend( cmd == CMD0 ? 0X95 : 0X87 ); -#endif + // Send CRC - correct for CMD0 with arg zero or CMD8 with arg 0X1AA + spiSend(cmd == CMD0 ? 0X95 : 0X87); + #endif - // skip stuff byte for stop read + // Skip stuff byte for stop read if (cmd == CMD12) spiRec(); - // wait for response + // Wait for response for (uint8_t i = 0; ((status_ = spiRec()) & 0x80) && i != 0xFF; i++) { /* Intentionally left empty */ } return status_; } @@ -159,9 +157,7 @@ uint32_t Sd2Card::cardSize() { void Sd2Card::chipDeselect() { digitalWrite(chipSelectPin_, HIGH); - - // insure MISO goes high impedance - spiSend( 0xFF ); + spiSend(0xFF); // Ensure MISO goes high impedance } void Sd2Card::chipSelect() { @@ -195,13 +191,8 @@ bool Sd2Card::erase(uint32_t firstBlock, uint32_t lastBlock) { goto FAIL; } } - if (type_ != SD_CARD_TYPE_SDHC) { - firstBlock <<= 9; - lastBlock <<= 9; - } - if (cardCommand(CMD32, firstBlock) - || cardCommand(CMD33, lastBlock) - || cardCommand(CMD38, 0)) { + if (type_ != SD_CARD_TYPE_SDHC) { firstBlock <<= 9; lastBlock <<= 9; } + if (cardCommand(CMD32, firstBlock) || cardCommand(CMD33, lastBlock) || cardCommand(CMD38, 0)) { error(SD_CARD_ERROR_ERASE); goto FAIL; } @@ -236,7 +227,7 @@ bool Sd2Card::eraseSingleBlockEnable() { * \return true for success, false for failure. * The reason for failure can be determined by calling errorCode() and errorData(). */ -bool Sd2Card::init(uint8_t sckRateID, pin_t chipSelectPin) { +bool Sd2Card::init(const uint8_t sckRateID/*=0*/, const pin_t chipSelectPin/*=SD_CHIP_SELECT_PIN*/) { errorCode_ = type_ = 0; chipSelectPin_ = chipSelectPin; // 16-bit init start time allows over a minute @@ -308,23 +299,23 @@ bool Sd2Card::init(uint8_t sckRateID, pin_t chipSelectPin) { watchdog_reset(); #endif - // initialize card and send host supports SDHC if SD2 + // Initialize card and send host supports SDHC if SD2 arg = type() == SD_CARD_TYPE_SD2 ? 0x40000000 : 0; while ((status_ = cardAcmd(ACMD41, arg)) != R1_READY_STATE) { - // check for timeout + // Check for timeout if (ELAPSED(millis(), init_timeout)) { error(SD_CARD_ERROR_ACMD41); goto FAIL; } } - // if SD2 read OCR register to check for SDHC card + // If SD2 read OCR register to check for SDHC card if (type() == SD_CARD_TYPE_SD2) { if (cardCommand(CMD58, 0)) { error(SD_CARD_ERROR_CMD58); goto FAIL; } if ((spiRec() & 0xC0) == 0xC0) type(SD_CARD_TYPE_SDHC); - // discard rest of ocr - contains allowed voltage range + // Discard rest of ocr - contains allowed voltage range for (uint8_t i = 0; i < 3; i++) spiRec(); } chipDeselect(); @@ -344,8 +335,7 @@ bool Sd2Card::init(uint8_t sckRateID, pin_t chipSelectPin) { * \return true for success, false for failure. */ bool Sd2Card::readBlock(uint32_t blockNumber, uint8_t* dst) { - // use address if not SDHC card - if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9; + if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9; // Use address if not SDHC card #if ENABLED(SD_CHECK_AND_RETRY) uint8_t retryCnt = 3; @@ -447,44 +437,39 @@ bool Sd2Card::readData(uint8_t* dst) { #endif #endif // SD_CHECK_AND_RETRY -bool Sd2Card::readData(uint8_t* dst, uint16_t count) { - // wait for start block token +bool Sd2Card::readData(uint8_t* dst, const uint16_t count) { + bool success = false; + const millis_t read_timeout = millis() + SD_READ_TIMEOUT; - while ((status_ = spiRec()) == 0xFF) { + while ((status_ = spiRec()) == 0xFF) { // Wait for start block token if (ELAPSED(millis(), read_timeout)) { error(SD_CARD_ERROR_READ_TIMEOUT); goto FAIL; } } - if (status_ != DATA_START_BLOCK) { - error(SD_CARD_ERROR_READ); - goto FAIL; - } - // transfer data - spiRead(dst, count); -#if ENABLED(SD_CHECK_AND_RETRY) - { - uint16_t recvCrc = (spiRec() << 8) | spiRec(); - if (crcSupported && recvCrc != CRC_CCITT(dst, count)) { - error(SD_CARD_ERROR_READ_CRC); - goto FAIL; - } + if (status_ == DATA_START_BLOCK) { + spiRead(dst, count); // Transfer data + + const uint16_t recvCrc = (spiRec() << 8) | spiRec(); + #if ENABLED(SD_CHECK_AND_RETRY) + success = !crcSupported || recvCrc == CRC_CCITT(dst, count); + if (!success) error(SD_CARD_ERROR_READ_CRC); + #else + success = true; + UNUSED(recvCrc); + #endif } -#else - // discard CRC - spiRec(); - spiRec(); -#endif - chipDeselect(); - return true; + else + error(SD_CARD_ERROR_READ); + FAIL: chipDeselect(); - return false; + return success; } /** read CID or CSR register */ -bool Sd2Card::readRegister(uint8_t cmd, void* buf) { +bool Sd2Card::readRegister(const uint8_t cmd, void* buf) { uint8_t* dst = reinterpret_cast(buf); if (cardCommand(cmd, 0)) { error(SD_CARD_ERROR_READ_REG); @@ -506,13 +491,11 @@ bool Sd2Card::readRegister(uint8_t cmd, void* buf) { */ bool Sd2Card::readStart(uint32_t blockNumber) { if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9; - if (cardCommand(CMD18, blockNumber)) { - error(SD_CARD_ERROR_CMD18); - chipDeselect(); - return false; - } + + const bool success = !cardCommand(CMD18, blockNumber); + if (!success) error(SD_CARD_ERROR_CMD18); chipDeselect(); - return true; + return success; } /** @@ -522,13 +505,10 @@ bool Sd2Card::readStart(uint32_t blockNumber) { */ bool Sd2Card::readStop() { chipSelect(); - if (cardCommand(CMD12, 0)) { - error(SD_CARD_ERROR_CMD12); - chipDeselect(); - return false; - } + const bool success = !cardCommand(CMD12, 0); + if (!success) error(SD_CARD_ERROR_CMD12); chipDeselect(); - return true; + return success; } /** @@ -543,16 +523,20 @@ bool Sd2Card::readStop() { * \return The value one, true, is returned for success and the value zero, * false, is returned for an invalid value of \a sckRateID. */ -bool Sd2Card::setSckRate(uint8_t sckRateID) { - if (sckRateID > 6) { +bool Sd2Card::setSckRate(const uint8_t sckRateID) { + const bool success = (sckRateID <= 6); + if (success) + spiRate_ = sckRateID; + else error(SD_CARD_ERROR_SCK_RATE); - return false; - } - spiRate_ = sckRateID; - return true; + return success; } -// wait for card to go not busy +/** + * Wait for card to become not-busy + * \param[in] timeout_ms Timeout to abort. + * \return true for success, false for timeout. + */ bool Sd2Card::waitNotBusy(const millis_t timeout_ms) { const millis_t wait_timeout = millis() + timeout_ms; while (spiRec() != 0xFF) @@ -562,36 +546,31 @@ bool Sd2Card::waitNotBusy(const millis_t timeout_ms) { } /** - * Writes a 512 byte block to an SD card. + * Write a 512 byte block to an SD card. * * \param[in] blockNumber Logical block to be written. * \param[in] src Pointer to the location of the data to be written. * \return true for success, false for failure. */ bool Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) { - // use address if not SDHC card - if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9; - if (cardCommand(CMD24, blockNumber)) { - error(SD_CARD_ERROR_CMD24); - goto FAIL; - } - if (!writeData(DATA_START_BLOCK, src)) goto FAIL; + if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9; // Use address if not SDHC card - // wait for flash programming to complete - if (!waitNotBusy(SD_WRITE_TIMEOUT)) { - error(SD_CARD_ERROR_WRITE_TIMEOUT); - goto FAIL; - } - // response is r2 so get and check two bytes for nonzero - if (cardCommand(CMD13, 0) || spiRec()) { - error(SD_CARD_ERROR_WRITE_PROGRAMMING); - goto FAIL; + bool success = false; + if (!cardCommand(CMD24, blockNumber)) { + if (writeData(DATA_START_BLOCK, src)) { + if (waitNotBusy(SD_WRITE_TIMEOUT)) { // Wait for flashing to complete + success = !(cardCommand(CMD13, 0) || spiRec()); // Response is r2 so get and check two bytes for nonzero + if (!success) error(SD_CARD_ERROR_WRITE_PROGRAMMING); + } + else + error(SD_CARD_ERROR_WRITE_TIMEOUT); + } } + else + error(SD_CARD_ERROR_CMD24); + chipDeselect(); - return true; - FAIL: - chipDeselect(); - return false; + return success; } /** @@ -600,28 +579,30 @@ bool Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) { * \return true for success, false for failure. */ bool Sd2Card::writeData(const uint8_t* src) { + bool success = true; chipSelect(); - // wait for previous write to finish + // Wait for previous write to finish if (!waitNotBusy(SD_WRITE_TIMEOUT) || !writeData(WRITE_MULTIPLE_TOKEN, src)) { error(SD_CARD_ERROR_WRITE_MULTIPLE); - chipDeselect(); - return false; + success = false; } chipDeselect(); - return true; + return success; } -// send one block of data for write block or write multiple blocks -bool Sd2Card::writeData(uint8_t token, const uint8_t* src) { +// Send one block of data for write block or write multiple blocks +bool Sd2Card::writeData(const uint8_t token, const uint8_t* src) { -#if ENABLED(SD_CHECK_AND_RETRY) - uint16_t crc = CRC_CCITT( src, 512 ); -#else // ENABLED(SD_CHECK_AND_RETRY) - uint16_t crc = 0xFFFF; -#endif // ENABLED(SD_CHECK_AND_RETRY) - spiSendBlock( token, src ); - spiSend( crc >> 8 ); - spiSend( crc & 0XFF ); + uint16_t crc = + #if ENABLED(SD_CHECK_AND_RETRY) + CRC_CCITT(src, 512) + #else + 0xFFFF + #endif + ; + spiSendBlock(token, src); + spiSend(crc >> 8); + spiSend(crc & 0xFF); status_ = spiRec(); if ((status_ & DATA_RES_MASK) != DATA_RES_ACCEPTED) { @@ -643,23 +624,18 @@ bool Sd2Card::writeData(uint8_t token, const uint8_t* src) { * * \return true for success, false for failure. */ -bool Sd2Card::writeStart(uint32_t blockNumber, uint32_t eraseCount) { - // send pre-erase count - if (cardAcmd(ACMD23, eraseCount)) { +bool Sd2Card::writeStart(uint32_t blockNumber, const uint32_t eraseCount) { + bool success = false; + if (!cardAcmd(ACMD23, eraseCount)) { // Send pre-erase count + if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9; // Use address if not SDHC card + success = !cardCommand(CMD25, blockNumber); + if (!success) error(SD_CARD_ERROR_CMD25); + } + else error(SD_CARD_ERROR_ACMD23); - goto FAIL; - } - // use address if not SDHC card - if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9; - if (cardCommand(CMD25, blockNumber)) { - error(SD_CARD_ERROR_CMD25); - goto FAIL; - } + chipDeselect(); - return true; - FAIL: - chipDeselect(); - return false; + return success; } /** @@ -668,16 +644,17 @@ bool Sd2Card::writeStart(uint32_t blockNumber, uint32_t eraseCount) { * \return true for success, false for failure. */ bool Sd2Card::writeStop() { + bool success = false; chipSelect(); - if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto FAIL; - spiSend(STOP_TRAN_TOKEN); - if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto FAIL; + if (waitNotBusy(SD_WRITE_TIMEOUT)) { + spiSend(STOP_TRAN_TOKEN); + success = waitNotBusy(SD_WRITE_TIMEOUT); + } + else + error(SD_CARD_ERROR_STOP_TRAN); + chipDeselect(); - return true; - FAIL: - error(SD_CARD_ERROR_STOP_TRAN); - chipDeselect(); - return false; + return success; } #endif // SDSUPPORT diff --git a/Marlin/src/sd/Sd2Card.h b/Marlin/src/sd/Sd2Card.h index 8f14073df8..2701f65a47 100644 --- a/Marlin/src/sd/Sd2Card.h +++ b/Marlin/src/sd/Sd2Card.h @@ -112,7 +112,7 @@ uint8_t const SD_CARD_TYPE_SD1 = 1, // Standard capacity V1 * \brief Raw access to SD and SDHC flash memory cards. */ class Sd2Card { - public: +public: Sd2Card() : errorCode_(SD_CARD_ERROR_INIT_NOT_CALLED), type_(0) {} @@ -124,15 +124,15 @@ class Sd2Card { * Set SD error code. * \param[in] code value for error code. */ - void error(uint8_t code) {errorCode_ = code;} + inline void error(const uint8_t code) { errorCode_ = code; } /** * \return error code for last error. See Sd2Card.h for a list of error codes. */ - int errorCode() const {return errorCode_;} + inline int errorCode() const { return errorCode_; } /** \return error data for last error. */ - int errorData() const {return status_;} + inline int errorData() const { return status_; } /** * Initialize an SD flash memory card with default clock rate and chip @@ -140,8 +140,8 @@ class Sd2Card { * * \return true for success or false for failure. */ - bool init(uint8_t sckRateID = SPI_FULL_SPEED, - pin_t chipSelectPin = SD_CHIP_SELECT_PIN); + bool init(const uint8_t sckRateID=SPI_FULL_SPEED, const pin_t chipSelectPin=SD_CHIP_SELECT_PIN); + bool readBlock(uint32_t block, uint8_t* dst); /** @@ -163,12 +163,13 @@ class Sd2Card { * * \return true for success or false for failure. */ - bool readCSD(csd_t* csd) { return readRegister(CMD9, csd); } + inline bool readCSD(csd_t* csd) { return readRegister(CMD9, csd); } bool readData(uint8_t* dst); bool readStart(uint32_t blockNumber); bool readStop(); - bool setSckRate(uint8_t sckRateID); + bool setSckRate(const uint8_t sckRateID); + /** * Return the card type: SD V1, SD V2 or SDHC * \return 0 - SD V1, 1 - SD V2, or 3 - SDHC. @@ -176,10 +177,10 @@ class Sd2Card { int type() const {return type_;} bool writeBlock(uint32_t blockNumber, const uint8_t* src); bool writeData(const uint8_t* src); - bool writeStart(uint32_t blockNumber, uint32_t eraseCount); + bool writeStart(uint32_t blockNumber, const uint32_t eraseCount); bool writeStop(); - private: +private: uint8_t chipSelectPin_, errorCode_, spiRate_, @@ -187,17 +188,17 @@ class Sd2Card { type_; // private functions - uint8_t cardAcmd(uint8_t cmd, uint32_t arg) { + inline uint8_t cardAcmd(const uint8_t cmd, const uint32_t arg) { cardCommand(CMD55, 0); return cardCommand(cmd, arg); } - uint8_t cardCommand(uint8_t cmd, uint32_t arg); + uint8_t cardCommand(const uint8_t cmd, const uint32_t arg); - bool readData(uint8_t* dst, uint16_t count); - bool readRegister(uint8_t cmd, void* buf); + bool readData(uint8_t* dst, const uint16_t count); + bool readRegister(const uint8_t cmd, void* buf); void chipDeselect(); void chipSelect(); - void type(uint8_t value) { type_ = value; } + inline void type(const uint8_t value) { type_ = value; } bool waitNotBusy(const millis_t timeout_ms); - bool writeData(uint8_t token, const uint8_t* src); + bool writeData(const uint8_t token, const uint8_t* src); }; diff --git a/Marlin/src/sd/cardreader.cpp b/Marlin/src/sd/cardreader.cpp index fb21aab814..c47bc03dbb 100644 --- a/Marlin/src/sd/cardreader.cpp +++ b/Marlin/src/sd/cardreader.cpp @@ -1022,7 +1022,7 @@ void CardReader::printingHasFinished() { presort(); #endif - #if ENABLED(ULTRA_LCD) && ENABLED(LCD_SET_PROGRESS_MANUALLY) + #if (ENABLED(ULTRA_LCD) || ENABLED(EXTENSIBLE_UI)) && ENABLED(LCD_SET_PROGRESS_MANUALLY) ui.progress_bar_percent = 0; #endif diff --git a/Marlin/src/sd/usb_flashdrive/Sd2Card_FlashDrive.cpp b/Marlin/src/sd/usb_flashdrive/Sd2Card_FlashDrive.cpp index f4fda4b3c9..6cf6dd8c3b 100644 --- a/Marlin/src/sd/usb_flashdrive/Sd2Card_FlashDrive.cpp +++ b/Marlin/src/sd/usb_flashdrive/Sd2Card_FlashDrive.cpp @@ -31,6 +31,10 @@ #include "Sd2Card_FlashDrive.h" +#if ENABLED(ULTRA_LCD) || ENABLED(EXTENSIBLE_UI) + #include "../../lcd/ultralcd.h" +#endif + USB usb; BulkOnly bulk(&usb); @@ -46,25 +50,27 @@ void Sd2Card::idle() { switch (state) { case USB_HOST_DELAY_INIT: - next_retry = millis() + 10000; + next_retry = millis() + 2000; state = USB_HOST_WAITING; break; case USB_HOST_WAITING: if (ELAPSED(millis(), next_retry)) { - next_retry = millis() + 10000; + next_retry = millis() + 2000; state = USB_HOST_UNINITIALIZED; } break; case USB_HOST_UNINITIALIZED: - SERIAL_ECHOLNPGM("Starting USB host"); + SERIAL_ECHOPGM("Starting USB host..."); if (!usb.start()) { - SERIAL_ECHOLNPGM("USB host failed to start. Will retry in 10 seconds."); + SERIAL_ECHOPGM(" Failed. Retrying in 2s."); + #if ENABLED(ULTRA_LCD) || ENABLED(EXTENSIBLE_UI) + LCD_MESSAGEPGM("USB start failed"); + #endif state = USB_HOST_DELAY_INIT; } - else { - SERIAL_ECHOLNPGM("USB host initialized"); + else state = USB_HOST_INITIALIZED; - } + SERIAL_EOL(); break; case USB_HOST_INITIALIZED: const uint8_t lastUsbTaskState = usb.getUsbTaskState(); @@ -91,10 +97,10 @@ void Sd2Card::idle() { // This is equivalent to polling the SD_DETECT when using SD cards. bool Sd2Card::isInserted() { return usb.getUsbTaskState() == USB_STATE_RUNNING; -}; +} // Marlin calls this to initialize an SD card once it is inserted. -bool Sd2Card::init(uint8_t sckRateID, uint8_t chipSelectPin) { +bool Sd2Card::init(const uint8_t sckRateID/*=0*/, const pin_t chipSelectPin/*=SD_CHIP_SELECT_PIN*/) { if (!ready()) return false; if (!bulk.LUNIsGood(0)) { @@ -121,7 +127,7 @@ uint32_t Sd2Card::cardSize() { #ifndef USB_DEBUG const uint32_t #endif - lun0_capacity = bulk.GetCapacity(0); + lun0_capacity = bulk.GetCapacity(0); return lun0_capacity; } diff --git a/Marlin/src/sd/usb_flashdrive/Sd2Card_FlashDrive.h b/Marlin/src/sd/usb_flashdrive/Sd2Card_FlashDrive.h index ee044a19b6..b27715ee03 100644 --- a/Marlin/src/sd/usb_flashdrive/Sd2Card_FlashDrive.h +++ b/Marlin/src/sd/usb_flashdrive/Sd2Card_FlashDrive.h @@ -32,7 +32,6 @@ */ //#define USB_DEBUG 1 - #include "../SdFatConfig.h" #include "../SdInfo.h" @@ -61,11 +60,11 @@ class Sd2Card { private: - typedef enum { - USB_HOST_DELAY_INIT, - USB_HOST_WAITING, + typedef enum : uint8_t { USB_HOST_UNINITIALIZED, - USB_HOST_INITIALIZED + USB_HOST_INITIALIZED, + USB_HOST_DELAY_INIT, + USB_HOST_WAITING } state_t; static state_t state; @@ -75,21 +74,20 @@ class Sd2Card { uint32_t lun0_capacity; #endif - static inline bool ready() {return state == USB_HOST_INITIALIZED;} + static inline bool ready() { return state == USB_HOST_INITIALIZED; } public: - bool init(uint8_t sckRateID = 0, uint8_t chipSelectPin = SD_CHIP_SELECT_PIN); + bool init(const uint8_t sckRateID=0, const pin_t chipSelectPin=SD_CHIP_SELECT_PIN); static void idle(); - bool readStart(uint32_t block) { pos = block; return ready(); } - bool readData(uint8_t* dst) { return readBlock(pos++, dst); } - bool readStop() { return true; } - - bool writeStart(uint32_t block, uint32_t eraseCount) { pos = block; return ready(); } - bool writeData(uint8_t* src) { return writeBlock(pos++, src); } - bool writeStop() { return true; } + inline bool readStart(const uint32_t block) { pos = block; return ready(); } + inline bool readData(uint8_t* dst) { return readBlock(pos++, dst); } + inline bool readStop() const { return true; } + inline bool writeStart(const uint32_t block, const uint32_t eraseCount) { UNUSED(eraseCount); pos = block; return ready(); } + inline bool writeData(uint8_t* src) { return writeBlock(pos++, src); } + inline bool writeStop() const { return true; } bool readBlock(uint32_t block, uint8_t* dst); bool writeBlock(uint32_t blockNumber, const uint8_t* src);