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.
This commit is contained in:
Marcio Teixeira 2019-01-01 14:17:48 -07:00 committed by Scott Lahteine
parent 4f2473053c
commit 60cb36bef3
12 changed files with 188 additions and 209 deletions

View file

@ -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) { 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; rs_last_state = rs;
spiSend(rs ? 0x0FA : 0x0F8); // Send data or command
if ( rs == 0 ) DELAY_US(40); // Give the controller some time: 20 is bad, 30 is OK, 40 is safe
/* 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(val & 0x0F0); spiSend(val & 0xF0);
spiSend(val << 4); spiSend(val << 4);
} }
@ -105,7 +98,7 @@ uint8_t u8g_com_HAL_LPC1768_ST7920_hw_spi_fn(u8g_t *u8g, uint8_t msg, uint8_t ar
u8g_Delay(5); u8g_Delay(5);
spiBegin(); spiBegin();
spiInit(SPI_EIGHTH_SPEED); // ST7920 max speed is about 1.1 MHz 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 */ u8g->pin_list[U8G_PI_A0_STATE] = 0; // initial RS state: command mode
break; break;
case U8G_COM_MSG_STOP: 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); u8g_SetPILevel(u8g, U8G_PI_RESET, arg_val);
break; 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; u8g->pin_list[U8G_PI_A0_STATE] = arg_val;
break; break;
case U8G_COM_MSG_CHIP_SELECT: 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; break;
case U8G_COM_MSG_WRITE_BYTE: case U8G_COM_MSG_WRITE_BYTE:

View file

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

View file

@ -544,16 +544,20 @@ namespace ExtUI {
} }
void setTargetTemp_celsius(float value, const heater_t heater) { 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 HAS_HEATED_BED
if (heater == BED) if (heater == BED)
thermalManager.setTargetBed(clamp(value,0,200)); thermalManager.setTargetBed(clamp(value, 0, BED_MAXTEMP - 15));
else else
#endif #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) { 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) { void setFan_percent(float value, const fan_t fan) {

View file

@ -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) { 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 HAS_HEATED_BED
if (tempb >= 0) thermalManager.setTargetBed(tempb); if (tempb >= 0) thermalManager.setTargetBed(tempb);
#else #else

View file

@ -47,7 +47,7 @@
#include "../feature/bedlevel/bedlevel.h" #include "../feature/bedlevel/bedlevel.h"
#endif #endif
#if HAS_AXIS_UNHOMED_ERR && ENABLED(ULTRA_LCD) #if HAS_AXIS_UNHOMED_ERR && (ENABLED(ULTRA_LCD) || ENABLED(EXTENSIBLE_UI))
#include "../lcd/ultralcd.h" #include "../lcd/ultralcd.h"
#endif #endif
@ -1019,7 +1019,7 @@ void prepare_move_to_destination() {
if (zz) SERIAL_ECHOPGM(MSG_Z); if (zz) SERIAL_ECHOPGM(MSG_Z);
SERIAL_ECHOLNPGM(" " MSG_FIRST); 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 : ""); ui.status_printf_P(0, PSTR(MSG_HOME " %s%s%s " MSG_FIRST), xx ? MSG_X : "", yy ? MSG_Y : "", zz ? MSG_Z : "");
#endif #endif
return true; return true;

View file

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

View file

@ -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]; singlenozzle_temp[active_extruder] = thermalManager.target_temperature[0];
if (singlenozzle_temp[tmp_extruder] && singlenozzle_temp[tmp_extruder] != singlenozzle_temp[active_extruder]) { if (singlenozzle_temp[tmp_extruder] && singlenozzle_temp[tmp_extruder] != singlenozzle_temp[active_extruder]) {
thermalManager.setTargetHotend(singlenozzle_temp[tmp_extruder], 0); thermalManager.setTargetHotend(singlenozzle_temp[tmp_extruder], 0);
#if ENABLED(ULTRA_LCD) #if ENABLED(ULTRA_LCD) || ENABLED(EXTENSIBLE_UI)
thermalManager.set_heating_message(0); thermalManager.set_heating_message(0);
#endif #endif
(void)thermalManager.wait_for_hotend(0, false); // Wait for heating or cooling (void)thermalManager.wait_for_hotend(0, false); // Wait for heating or cooling

View file

@ -65,7 +65,7 @@
static uint8_t CRC7(const uint8_t* data, uint8_t n) { static uint8_t CRC7(const uint8_t* data, uint8_t n) {
uint8_t crc = 0; uint8_t crc = 0;
while ( n > 0 ) { while (n > 0) {
crc = pgm_read_byte(&crctab7[ (crc << 1) ^ *data++ ]); crc = pgm_read_byte(&crctab7[ (crc << 1) ^ *data++ ]);
n--; n--;
} }
@ -87,44 +87,42 @@
#endif #endif
#endif #endif
// send command and return error code. Return zero for OK // Send command and return error code. Return zero for OK
uint8_t Sd2Card::cardCommand(uint8_t cmd, uint32_t arg) { uint8_t Sd2Card::cardCommand(const uint8_t cmd, const uint32_t arg) {
// select card // Select card
chipSelect(); chipSelect();
// wait up to 300 ms if busy // Wait up to 300 ms if busy
waitNotBusy( SD_WRITE_TIMEOUT ); waitNotBusy(SD_WRITE_TIMEOUT);
uint8_t *pa = (uint8_t *)(&arg); uint8_t *pa = (uint8_t *)(&arg);
#if ENABLED(SD_CHECK_AND_RETRY) #if ENABLED(SD_CHECK_AND_RETRY)
// form message // Form message
uint8_t d[6] = {(uint8_t) (cmd | 0x40), pa[3], pa[2], pa[1], pa[0] }; uint8_t d[6] = {(uint8_t) (cmd | 0x40), pa[3], pa[2], pa[1], pa[0] };
// add crc // Add crc
d[5] = CRC7(d, 5); d[5] = CRC7(d, 5);
// send message // Send message
for (uint8_t k = 0; k < 6; k++ ) for (uint8_t k = 0; k < 6; k++) spiSend(d[k]);
spiSend( d[k] );
#else #else
// send command // Send command
spiSend(cmd | 0x40); spiSend(cmd | 0x40);
// send argument // Send argument
for( int8_t i = 3; i >= 0; i-- ) for (int8_t i = 3; i >= 0; i--) spiSend(pa[i]);
spiSend( pa[i] );
// send CRC - correct for CMD0 with arg zero or CMD8 with arg 0X1AA // Send CRC - correct for CMD0 with arg zero or CMD8 with arg 0X1AA
spiSend( cmd == CMD0 ? 0X95 : 0X87 ); spiSend(cmd == CMD0 ? 0X95 : 0X87);
#endif #endif
// skip stuff byte for stop read // Skip stuff byte for stop read
if (cmd == CMD12) spiRec(); if (cmd == CMD12) spiRec();
// wait for response // Wait for response
for (uint8_t i = 0; ((status_ = spiRec()) & 0x80) && i != 0xFF; i++) { /* Intentionally left empty */ } for (uint8_t i = 0; ((status_ = spiRec()) & 0x80) && i != 0xFF; i++) { /* Intentionally left empty */ }
return status_; return status_;
} }
@ -159,9 +157,7 @@ uint32_t Sd2Card::cardSize() {
void Sd2Card::chipDeselect() { void Sd2Card::chipDeselect() {
digitalWrite(chipSelectPin_, HIGH); digitalWrite(chipSelectPin_, HIGH);
spiSend(0xFF); // Ensure MISO goes high impedance
// insure MISO goes high impedance
spiSend( 0xFF );
} }
void Sd2Card::chipSelect() { void Sd2Card::chipSelect() {
@ -195,13 +191,8 @@ bool Sd2Card::erase(uint32_t firstBlock, uint32_t lastBlock) {
goto FAIL; goto FAIL;
} }
} }
if (type_ != SD_CARD_TYPE_SDHC) { if (type_ != SD_CARD_TYPE_SDHC) { firstBlock <<= 9; lastBlock <<= 9; }
firstBlock <<= 9; if (cardCommand(CMD32, firstBlock) || cardCommand(CMD33, lastBlock) || cardCommand(CMD38, 0)) {
lastBlock <<= 9;
}
if (cardCommand(CMD32, firstBlock)
|| cardCommand(CMD33, lastBlock)
|| cardCommand(CMD38, 0)) {
error(SD_CARD_ERROR_ERASE); error(SD_CARD_ERROR_ERASE);
goto FAIL; goto FAIL;
} }
@ -236,7 +227,7 @@ bool Sd2Card::eraseSingleBlockEnable() {
* \return true for success, false for failure. * \return true for success, false for failure.
* The reason for failure can be determined by calling errorCode() and errorData(). * 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; errorCode_ = type_ = 0;
chipSelectPin_ = chipSelectPin; chipSelectPin_ = chipSelectPin;
// 16-bit init start time allows over a minute // 16-bit init start time allows over a minute
@ -308,23 +299,23 @@ bool Sd2Card::init(uint8_t sckRateID, pin_t chipSelectPin) {
watchdog_reset(); watchdog_reset();
#endif #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; arg = type() == SD_CARD_TYPE_SD2 ? 0x40000000 : 0;
while ((status_ = cardAcmd(ACMD41, arg)) != R1_READY_STATE) { while ((status_ = cardAcmd(ACMD41, arg)) != R1_READY_STATE) {
// check for timeout // Check for timeout
if (ELAPSED(millis(), init_timeout)) { if (ELAPSED(millis(), init_timeout)) {
error(SD_CARD_ERROR_ACMD41); error(SD_CARD_ERROR_ACMD41);
goto FAIL; 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 (type() == SD_CARD_TYPE_SD2) {
if (cardCommand(CMD58, 0)) { if (cardCommand(CMD58, 0)) {
error(SD_CARD_ERROR_CMD58); error(SD_CARD_ERROR_CMD58);
goto FAIL; goto FAIL;
} }
if ((spiRec() & 0xC0) == 0xC0) type(SD_CARD_TYPE_SDHC); 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(); for (uint8_t i = 0; i < 3; i++) spiRec();
} }
chipDeselect(); chipDeselect();
@ -344,8 +335,7 @@ bool Sd2Card::init(uint8_t sckRateID, pin_t chipSelectPin) {
* \return true for success, false for failure. * \return true for success, false for failure.
*/ */
bool Sd2Card::readBlock(uint32_t blockNumber, uint8_t* dst) { bool Sd2Card::readBlock(uint32_t blockNumber, uint8_t* dst) {
// use address if not SDHC card if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9; // Use address if not SDHC card
if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
#if ENABLED(SD_CHECK_AND_RETRY) #if ENABLED(SD_CHECK_AND_RETRY)
uint8_t retryCnt = 3; uint8_t retryCnt = 3;
@ -447,44 +437,39 @@ bool Sd2Card::readData(uint8_t* dst) {
#endif #endif
#endif // SD_CHECK_AND_RETRY #endif // SD_CHECK_AND_RETRY
bool Sd2Card::readData(uint8_t* dst, uint16_t count) { bool Sd2Card::readData(uint8_t* dst, const uint16_t count) {
// wait for start block token bool success = false;
const millis_t read_timeout = millis() + SD_READ_TIMEOUT; 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)) { if (ELAPSED(millis(), read_timeout)) {
error(SD_CARD_ERROR_READ_TIMEOUT); error(SD_CARD_ERROR_READ_TIMEOUT);
goto FAIL; 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) if (status_ == DATA_START_BLOCK) {
{ spiRead(dst, count); // Transfer data
uint16_t recvCrc = (spiRec() << 8) | spiRec();
if (crcSupported && recvCrc != CRC_CCITT(dst, count)) { const uint16_t recvCrc = (spiRec() << 8) | spiRec();
error(SD_CARD_ERROR_READ_CRC); #if ENABLED(SD_CHECK_AND_RETRY)
goto FAIL; success = !crcSupported || recvCrc == CRC_CCITT(dst, count);
if (!success) error(SD_CARD_ERROR_READ_CRC);
#else
success = true;
UNUSED(recvCrc);
#endif
} }
} else
#else error(SD_CARD_ERROR_READ);
// discard CRC
spiRec();
spiRec();
#endif
chipDeselect();
return true;
FAIL: FAIL:
chipDeselect(); chipDeselect();
return false; return success;
} }
/** read CID or CSR register */ /** 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<uint8_t*>(buf); uint8_t* dst = reinterpret_cast<uint8_t*>(buf);
if (cardCommand(cmd, 0)) { if (cardCommand(cmd, 0)) {
error(SD_CARD_ERROR_READ_REG); error(SD_CARD_ERROR_READ_REG);
@ -506,13 +491,11 @@ bool Sd2Card::readRegister(uint8_t cmd, void* buf) {
*/ */
bool Sd2Card::readStart(uint32_t blockNumber) { bool Sd2Card::readStart(uint32_t blockNumber) {
if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9; if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9;
if (cardCommand(CMD18, blockNumber)) {
error(SD_CARD_ERROR_CMD18); const bool success = !cardCommand(CMD18, blockNumber);
if (!success) error(SD_CARD_ERROR_CMD18);
chipDeselect(); chipDeselect();
return false; return success;
}
chipDeselect();
return true;
} }
/** /**
@ -522,13 +505,10 @@ bool Sd2Card::readStart(uint32_t blockNumber) {
*/ */
bool Sd2Card::readStop() { bool Sd2Card::readStop() {
chipSelect(); chipSelect();
if (cardCommand(CMD12, 0)) { const bool success = !cardCommand(CMD12, 0);
error(SD_CARD_ERROR_CMD12); if (!success) error(SD_CARD_ERROR_CMD12);
chipDeselect(); chipDeselect();
return false; return success;
}
chipDeselect();
return true;
} }
/** /**
@ -543,16 +523,20 @@ bool Sd2Card::readStop() {
* \return The value one, true, is returned for success and the value zero, * \return The value one, true, is returned for success and the value zero,
* false, is returned for an invalid value of \a sckRateID. * false, is returned for an invalid value of \a sckRateID.
*/ */
bool Sd2Card::setSckRate(uint8_t sckRateID) { bool Sd2Card::setSckRate(const uint8_t sckRateID) {
if (sckRateID > 6) { const bool success = (sckRateID <= 6);
error(SD_CARD_ERROR_SCK_RATE); if (success)
return false;
}
spiRate_ = sckRateID; spiRate_ = sckRateID;
return true; else
error(SD_CARD_ERROR_SCK_RATE);
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) { bool Sd2Card::waitNotBusy(const millis_t timeout_ms) {
const millis_t wait_timeout = millis() + timeout_ms; const millis_t wait_timeout = millis() + timeout_ms;
while (spiRec() != 0xFF) 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] blockNumber Logical block to be written.
* \param[in] src Pointer to the location of the data to be written. * \param[in] src Pointer to the location of the data to be written.
* \return true for success, false for failure. * \return true for success, false for failure.
*/ */
bool Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) { bool Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) {
// use address if not SDHC card if (type() != SD_CARD_TYPE_SDHC) blockNumber <<= 9; // 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;
// wait for flash programming to complete bool success = false;
if (!waitNotBusy(SD_WRITE_TIMEOUT)) { 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); 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;
} }
else
error(SD_CARD_ERROR_CMD24);
chipDeselect(); chipDeselect();
return true; return success;
FAIL:
chipDeselect();
return false;
} }
/** /**
@ -600,28 +579,30 @@ bool Sd2Card::writeBlock(uint32_t blockNumber, const uint8_t* src) {
* \return true for success, false for failure. * \return true for success, false for failure.
*/ */
bool Sd2Card::writeData(const uint8_t* src) { bool Sd2Card::writeData(const uint8_t* src) {
bool success = true;
chipSelect(); chipSelect();
// wait for previous write to finish // Wait for previous write to finish
if (!waitNotBusy(SD_WRITE_TIMEOUT) || !writeData(WRITE_MULTIPLE_TOKEN, src)) { if (!waitNotBusy(SD_WRITE_TIMEOUT) || !writeData(WRITE_MULTIPLE_TOKEN, src)) {
error(SD_CARD_ERROR_WRITE_MULTIPLE); error(SD_CARD_ERROR_WRITE_MULTIPLE);
chipDeselect(); success = false;
return false;
} }
chipDeselect(); chipDeselect();
return true; return success;
} }
// send one block of data for write block or write multiple blocks // Send one block of data for write block or write multiple blocks
bool Sd2Card::writeData(uint8_t token, const uint8_t* src) { bool Sd2Card::writeData(const uint8_t token, const uint8_t* src) {
#if ENABLED(SD_CHECK_AND_RETRY) uint16_t crc =
uint16_t crc = CRC_CCITT( src, 512 ); #if ENABLED(SD_CHECK_AND_RETRY)
#else // ENABLED(SD_CHECK_AND_RETRY) CRC_CCITT(src, 512)
uint16_t crc = 0xFFFF; #else
#endif // ENABLED(SD_CHECK_AND_RETRY) 0xFFFF
spiSendBlock( token, src ); #endif
spiSend( crc >> 8 ); ;
spiSend( crc & 0XFF ); spiSendBlock(token, src);
spiSend(crc >> 8);
spiSend(crc & 0xFF);
status_ = spiRec(); status_ = spiRec();
if ((status_ & DATA_RES_MASK) != DATA_RES_ACCEPTED) { 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. * \return true for success, false for failure.
*/ */
bool Sd2Card::writeStart(uint32_t blockNumber, uint32_t eraseCount) { bool Sd2Card::writeStart(uint32_t blockNumber, const uint32_t eraseCount) {
// send pre-erase count bool success = false;
if (cardAcmd(ACMD23, eraseCount)) { 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); 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(); chipDeselect();
return true; return success;
FAIL:
chipDeselect();
return false;
} }
/** /**
@ -668,16 +644,17 @@ bool Sd2Card::writeStart(uint32_t blockNumber, uint32_t eraseCount) {
* \return true for success, false for failure. * \return true for success, false for failure.
*/ */
bool Sd2Card::writeStop() { bool Sd2Card::writeStop() {
bool success = false;
chipSelect(); chipSelect();
if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto FAIL; if (waitNotBusy(SD_WRITE_TIMEOUT)) {
spiSend(STOP_TRAN_TOKEN); spiSend(STOP_TRAN_TOKEN);
if (!waitNotBusy(SD_WRITE_TIMEOUT)) goto FAIL; success = waitNotBusy(SD_WRITE_TIMEOUT);
chipDeselect(); }
return true; else
FAIL:
error(SD_CARD_ERROR_STOP_TRAN); error(SD_CARD_ERROR_STOP_TRAN);
chipDeselect(); chipDeselect();
return false; return success;
} }
#endif // SDSUPPORT #endif // SDSUPPORT

View file

@ -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. * \brief Raw access to SD and SDHC flash memory cards.
*/ */
class Sd2Card { class Sd2Card {
public: public:
Sd2Card() : errorCode_(SD_CARD_ERROR_INIT_NOT_CALLED), type_(0) {} Sd2Card() : errorCode_(SD_CARD_ERROR_INIT_NOT_CALLED), type_(0) {}
@ -124,15 +124,15 @@ class Sd2Card {
* Set SD error code. * Set SD error code.
* \param[in] code value for 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. * \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. */ /** \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 * 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. * \return true for success or false for failure.
*/ */
bool init(uint8_t sckRateID = SPI_FULL_SPEED, bool init(const uint8_t sckRateID=SPI_FULL_SPEED, const pin_t chipSelectPin=SD_CHIP_SELECT_PIN);
pin_t chipSelectPin = SD_CHIP_SELECT_PIN);
bool readBlock(uint32_t block, uint8_t* dst); bool readBlock(uint32_t block, uint8_t* dst);
/** /**
@ -163,12 +163,13 @@ class Sd2Card {
* *
* \return true for success or false for failure. * \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 readData(uint8_t* dst);
bool readStart(uint32_t blockNumber); bool readStart(uint32_t blockNumber);
bool readStop(); bool readStop();
bool setSckRate(uint8_t sckRateID); bool setSckRate(const uint8_t sckRateID);
/** /**
* Return the card type: SD V1, SD V2 or SDHC * Return the card type: SD V1, SD V2 or SDHC
* \return 0 - SD V1, 1 - SD V2, or 3 - SDHC. * \return 0 - SD V1, 1 - SD V2, or 3 - SDHC.
@ -176,10 +177,10 @@ class Sd2Card {
int type() const {return type_;} int type() const {return type_;}
bool writeBlock(uint32_t blockNumber, const uint8_t* src); bool writeBlock(uint32_t blockNumber, const uint8_t* src);
bool writeData(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(); bool writeStop();
private: private:
uint8_t chipSelectPin_, uint8_t chipSelectPin_,
errorCode_, errorCode_,
spiRate_, spiRate_,
@ -187,17 +188,17 @@ class Sd2Card {
type_; type_;
// private functions // 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); cardCommand(CMD55, 0);
return cardCommand(cmd, arg); 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 readData(uint8_t* dst, const uint16_t count);
bool readRegister(uint8_t cmd, void* buf); bool readRegister(const uint8_t cmd, void* buf);
void chipDeselect(); void chipDeselect();
void chipSelect(); 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 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);
}; };

View file

@ -1022,7 +1022,7 @@ void CardReader::printingHasFinished() {
presort(); presort();
#endif #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; ui.progress_bar_percent = 0;
#endif #endif

View file

@ -31,6 +31,10 @@
#include "Sd2Card_FlashDrive.h" #include "Sd2Card_FlashDrive.h"
#if ENABLED(ULTRA_LCD) || ENABLED(EXTENSIBLE_UI)
#include "../../lcd/ultralcd.h"
#endif
USB usb; USB usb;
BulkOnly bulk(&usb); BulkOnly bulk(&usb);
@ -46,25 +50,27 @@ void Sd2Card::idle() {
switch (state) { switch (state) {
case USB_HOST_DELAY_INIT: case USB_HOST_DELAY_INIT:
next_retry = millis() + 10000; next_retry = millis() + 2000;
state = USB_HOST_WAITING; state = USB_HOST_WAITING;
break; break;
case USB_HOST_WAITING: case USB_HOST_WAITING:
if (ELAPSED(millis(), next_retry)) { if (ELAPSED(millis(), next_retry)) {
next_retry = millis() + 10000; next_retry = millis() + 2000;
state = USB_HOST_UNINITIALIZED; state = USB_HOST_UNINITIALIZED;
} }
break; break;
case USB_HOST_UNINITIALIZED: case USB_HOST_UNINITIALIZED:
SERIAL_ECHOLNPGM("Starting USB host"); SERIAL_ECHOPGM("Starting USB host...");
if (!usb.start()) { 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; state = USB_HOST_DELAY_INIT;
} }
else { else
SERIAL_ECHOLNPGM("USB host initialized");
state = USB_HOST_INITIALIZED; state = USB_HOST_INITIALIZED;
} SERIAL_EOL();
break; break;
case USB_HOST_INITIALIZED: case USB_HOST_INITIALIZED:
const uint8_t lastUsbTaskState = usb.getUsbTaskState(); 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. // This is equivalent to polling the SD_DETECT when using SD cards.
bool Sd2Card::isInserted() { bool Sd2Card::isInserted() {
return usb.getUsbTaskState() == USB_STATE_RUNNING; return usb.getUsbTaskState() == USB_STATE_RUNNING;
}; }
// Marlin calls this to initialize an SD card once it is inserted. // 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 (!ready()) return false;
if (!bulk.LUNIsGood(0)) { if (!bulk.LUNIsGood(0)) {

View file

@ -32,7 +32,6 @@
*/ */
//#define USB_DEBUG 1 //#define USB_DEBUG 1
#include "../SdFatConfig.h" #include "../SdFatConfig.h"
#include "../SdInfo.h" #include "../SdInfo.h"
@ -61,11 +60,11 @@
class Sd2Card { class Sd2Card {
private: private:
typedef enum { typedef enum : uint8_t {
USB_HOST_DELAY_INIT,
USB_HOST_WAITING,
USB_HOST_UNINITIALIZED, USB_HOST_UNINITIALIZED,
USB_HOST_INITIALIZED USB_HOST_INITIALIZED,
USB_HOST_DELAY_INIT,
USB_HOST_WAITING
} state_t; } state_t;
static state_t state; static state_t state;
@ -75,21 +74,20 @@ class Sd2Card {
uint32_t lun0_capacity; uint32_t lun0_capacity;
#endif #endif
static inline bool ready() {return state == USB_HOST_INITIALIZED;} static inline bool ready() { return state == USB_HOST_INITIALIZED; }
public: 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(); static void idle();
bool readStart(uint32_t block) { pos = block; return ready(); } inline bool readStart(const uint32_t block) { pos = block; return ready(); }
bool readData(uint8_t* dst) { return readBlock(pos++, dst); } inline bool readData(uint8_t* dst) { return readBlock(pos++, dst); }
bool readStop() { return true; } inline bool readStop() const { 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 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 readBlock(uint32_t block, uint8_t* dst);
bool writeBlock(uint32_t blockNumber, const uint8_t* src); bool writeBlock(uint32_t blockNumber, const uint8_t* src);