1
0
mirror of https://github.com/MarlinFirmware/Marlin.git synced 2024-11-27 22:08:02 +00:00
MarlinFirmware/Marlin/src/Marlin.cpp

982 lines
24 KiB
C++
Raw Normal View History

/**
2016-03-24 18:01:20 +00:00
* Marlin 3D Printer Firmware
2017-03-18 15:15:54 +00:00
* Copyright (C) 2016, 2017 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2016-03-24 18:01:20 +00:00
*
*/
/**
* About Marlin
*
* This firmware is a mashup between Sprinter and grbl.
* - https://github.com/kliment/Sprinter
2018-02-06 08:28:18 +00:00
* - https://github.com/grbl/grbl
2015-04-14 00:17:36 +00:00
*/
2016-11-19 03:54:38 +00:00
#include "Marlin.h"
2017-09-06 11:28:33 +00:00
#include "lcd/ultralcd.h"
#include "module/motion.h"
2017-09-06 11:28:33 +00:00
#include "module/planner.h"
#include "module/stepper.h"
#include "module/endstops.h"
2017-09-08 20:35:25 +00:00
#include "module/probe.h"
2017-09-06 11:28:33 +00:00
#include "module/temperature.h"
#include "sd/cardreader.h"
#include "module/configuration_store.h"
2017-09-09 05:18:43 +00:00
#include "module/printcounter.h" // PrintCounter or Stopwatch
#include "feature/closedloop.h"
#include "HAL/shared/Delay.h"
#ifdef ARDUINO
2017-09-06 11:28:33 +00:00
#include <pins_arduino.h>
#endif
2017-09-06 11:28:33 +00:00
#include <math.h>
#include "libs/nozzle.h"
#include "gcode/gcode.h"
2017-09-06 11:28:33 +00:00
#include "gcode/parser.h"
#include "gcode/queue.h"
2016-11-19 03:54:38 +00:00
2017-09-14 20:33:07 +00:00
#if HAS_BUZZER && DISABLED(LCD_USE_I2C_BUZZER)
#include "libs/buzzer.h"
#endif
2017-09-17 23:45:21 +00:00
#if ENABLED(DIGIPOT_I2C)
#include "feature/digipot/digipot.h"
#endif
2017-09-18 05:06:16 +00:00
#if ENABLED(MIXING_EXTRUDER)
#include "feature/mixing.h"
#endif
2016-11-19 03:54:38 +00:00
#if ENABLED(BEZIER_CURVE_SUPPORT)
2017-09-06 11:28:33 +00:00
#include "module/planner_bezier.h"
2016-11-19 03:54:38 +00:00
#endif
#if ENABLED(MAX7219_DEBUG)
2017-09-08 19:47:47 +00:00
#include "feature/Max7219_Debug_LEDs.h"
#endif
2017-09-08 19:47:47 +00:00
#if HAS_COLOR_LEDS
#include "feature/leds/leds.h"
#endif
2016-11-19 03:54:38 +00:00
#if HAS_SERVOS
2017-11-19 19:39:46 +00:00
#include "module/servo.h"
2016-11-19 03:54:38 +00:00
#endif
#if HAS_DIGIPOTSS
#include <SPI.h>
#endif
#if ENABLED(DAC_STEPPER_CURRENT)
2017-09-06 11:28:33 +00:00
#include "feature/dac/stepper_dac.h"
2016-11-19 03:54:38 +00:00
#endif
#if ENABLED(EXPERIMENTAL_I2CBUS)
2017-09-06 11:28:33 +00:00
#include "feature/twibus.h"
2017-09-17 07:49:13 +00:00
TWIBus i2c;
2016-11-19 03:54:38 +00:00
#endif
#if ENABLED(I2C_POSITION_ENCODERS)
2017-09-06 11:28:33 +00:00
#include "feature/I2CPositionEncoder.h"
#endif
2017-12-15 21:03:14 +00:00
#if HAS_TRINAMIC
#include "feature/tmc_util.h"
2017-09-17 23:34:04 +00:00
#endif
#if ENABLED(SDSUPPORT)
CardReader card;
#endif
2016-09-29 20:06:01 +00:00
#if ENABLED(G38_PROBE_TARGET)
2016-09-26 06:30:34 +00:00
bool G38_move = false,
G38_endstop_hit = false;
#endif
2017-09-08 20:35:25 +00:00
#if ENABLED(DELTA)
#include "module/delta.h"
#elif IS_SCARA
#include "module/scara.h"
#endif
#if HAS_LEVELING
#include "feature/bedlevel/bedlevel.h"
2017-03-18 15:15:54 +00:00
#endif
#if ENABLED(ADVANCED_PAUSE_FEATURE) && ENABLED(PAUSE_PARK_NO_STEPPER_TIMEOUT)
#include "feature/pause.h"
#endif
2017-09-09 05:18:43 +00:00
2018-04-22 00:41:26 +00:00
#if ENABLED(POWER_LOSS_RECOVERY)
#include "feature/power_loss_recovery.h"
#endif
#if ENABLED(FILAMENT_RUNOUT_SENSOR)
#include "feature/runout.h"
#endif
2017-09-16 10:09:34 +00:00
#if ENABLED(TEMP_STAT_LEDS)
#include "feature/leds/tempstat.h"
#endif
2017-09-17 23:56:29 +00:00
#if HAS_CASE_LIGHT
#include "feature/caselight.h"
#endif
2017-11-05 02:01:41 +00:00
#if HAS_FANMUX
#include "feature/fanmux.h"
#endif
#if DO_SWITCH_EXTRUDER || ENABLED(SWITCHING_NOZZLE) || ENABLED(PARKING_EXTRUDER)
2017-09-18 06:05:44 +00:00
#include "module/tool_change.h"
#endif
2017-09-18 06:44:59 +00:00
#if ENABLED(USE_CONTROLLER_FAN)
#include "feature/controllerfan.h"
#endif
2018-10-08 20:44:05 +00:00
#if ENABLED(EXTENSIBLE_UI)
#include "lcd/extensible_ui/ui_api.h"
#endif
2015-04-08 07:56:19 +00:00
bool Running = true;
#if ENABLED(TEMPERATURE_UNITS_SUPPORT)
TempUnit input_temp_units = TEMPUNIT_C;
#endif
2016-10-07 03:06:33 +00:00
// For M109 and M190, this flag may be cleared (by M108) to exit the wait loop
2016-07-07 02:59:19 +00:00
volatile bool wait_for_heatup = true;
2015-04-08 07:56:19 +00:00
2016-10-07 03:06:33 +00:00
// For M0/M1, this flag may be cleared (by M108) to exit the wait-for-user loop
#if HAS_RESUME_CONTINUE
2018-03-21 10:45:23 +00:00
volatile bool wait_for_user; // = false;
2016-09-12 01:51:53 +00:00
#endif
#if HAS_AUTO_REPORTING || ENABLED(HOST_KEEPALIVE_FEATURE)
bool suspend_auto_report; // = false
#endif
2015-04-03 22:45:41 +00:00
// Inactivity shutdown
2018-03-21 10:45:23 +00:00
millis_t max_inactive_time, // = 0
2017-09-17 04:57:09 +00:00
stepper_inactive_time = (DEFAULT_STEPPER_DEACTIVE_TIME) * 1000UL;
2016-04-18 06:13:07 +00:00
2018-11-05 07:08:40 +00:00
#if PIN_EXISTS(CHDK)
extern bool chdk_active;
extern millis_t chdk_timeout;
2017-09-08 20:35:25 +00:00
#endif
2016-10-02 06:48:17 +00:00
2017-09-08 20:35:25 +00:00
#if ENABLED(I2C_POSITION_ENCODERS)
I2CPositionEncodersMgr I2CPEM;
#endif
2017-04-15 03:44:08 +00:00
2016-09-15 18:22:23 +00:00
/**
2017-09-08 20:35:25 +00:00
* ***************************************************************************
* ******************************** FUNCTIONS ********************************
* ***************************************************************************
2016-09-15 18:22:23 +00:00
*/
2017-09-08 20:35:25 +00:00
void setup_killpin() {
#if HAS_KILL
SET_INPUT_PULLUP(KILL_PIN);
2016-09-15 20:43:06 +00:00
#endif
2017-09-08 20:35:25 +00:00
}
2017-09-08 20:35:25 +00:00
void setup_powerhold() {
#if HAS_SUICIDE
OUT_WRITE(SUICIDE_PIN, HIGH);
#endif
2017-09-08 20:35:25 +00:00
#if HAS_POWER_SWITCH
#if ENABLED(PS_DEFAULT_OFF)
PSU_OFF();
2017-09-08 20:35:25 +00:00
#else
PSU_ON();
2017-04-15 03:44:08 +00:00
#endif
#endif
2017-09-08 20:35:25 +00:00
}
2017-04-15 03:44:08 +00:00
2017-09-08 20:35:25 +00:00
/**
* Stepper Reset (RigidBoard, et.al.)
*/
#if HAS_STEPPER_RESET
void disableStepperDrivers() {
OUT_WRITE(STEPPER_RESET_PIN, LOW); // drive it down to hold in reset motor driver chips
}
void enableStepperDrivers() { SET_INPUT(STEPPER_RESET_PIN); } // set to input, which allows it to be pulled high by pullups
#endif
2016-08-28 00:53:02 +00:00
2017-09-08 20:35:25 +00:00
#if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
2015-04-29 02:10:07 +00:00
2017-09-08 20:35:25 +00:00
void i2c_on_receive(int bytes) { // just echo all bytes received to serial
i2c.receive(bytes);
}
2017-04-15 03:44:08 +00:00
2017-09-08 20:35:25 +00:00
void i2c_on_request() { // just send dummy data for now
i2c.reply("Hello World!\n");
}
2015-07-09 23:57:44 +00:00
2017-09-08 20:35:25 +00:00
#endif
2017-09-06 11:28:33 +00:00
/**
* Sensitive pin test for M42, M226
*/
2018-10-11 05:58:41 +00:00
#include "pins/sensitive_pins.h"
bool pin_is_protected(const pin_t pin) {
static const pin_t sensitive_pins[] PROGMEM = SENSITIVE_PINS;
for (uint8_t i = 0; i < COUNT(sensitive_pins); i++) {
pin_t sensitive_pin;
memcpy_P(&sensitive_pin, &sensitive_pins[i], sizeof(pin_t));
if (pin == sensitive_pin) return true;
}
2017-09-06 11:28:33 +00:00
return false;
}
2018-06-10 22:45:39 +00:00
void protected_pin_err() {
SERIAL_ERROR_MSG(MSG_ERR_PROTECTED_PIN);
2018-06-10 22:45:39 +00:00
}
2017-09-06 11:28:33 +00:00
void quickstop_stepper() {
planner.quick_stop();
planner.synchronize();
2017-09-06 11:28:33 +00:00
set_current_from_steppers_for_axis(ALL_AXES);
sync_plan_position();
2017-09-06 11:28:33 +00:00
}
void enable_all_steppers() {
#if ENABLED(AUTO_POWER_CONTROL)
powerManager.power_on();
#endif
enable_X();
enable_Y();
enable_Z();
enable_E0();
enable_E1();
enable_E2();
enable_E3();
enable_E4();
enable_E5();
}
2017-03-18 02:12:19 +00:00
void disable_e_steppers() {
disable_E0();
disable_E1();
disable_E2();
disable_E3();
disable_E4();
disable_E5();
2015-04-03 22:31:35 +00:00
}
void disable_e_stepper(const uint8_t e) {
switch (e) {
case 0: disable_E0(); break;
case 1: disable_E1(); break;
case 2: disable_E2(); break;
case 3: disable_E3(); break;
case 4: disable_E4(); break;
case 5: disable_E5(); break;
}
}
2017-03-18 02:12:19 +00:00
void disable_all_steppers() {
disable_X();
disable_Y();
disable_Z();
2017-03-18 02:12:19 +00:00
disable_e_steppers();
}
2015-04-03 22:31:35 +00:00
/**
* Manage several activities:
* - Check for Filament Runout
* - Keep the command buffer full
* - Check for maximum inactive time between commands
* - Check for maximum inactive time between stepper commands
2018-11-05 07:08:40 +00:00
* - Check if CHDK_PIN needs to go LOW
* - Check for KILL button held down
* - Check for HOME button held down
* - Check if cooling fan needs to be switched on
* - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
2015-04-03 22:31:35 +00:00
*/
2018-04-08 09:00:26 +00:00
void manage_inactivity(const bool ignore_stepper_queue/*=false*/) {
2015-08-05 11:40:36 +00:00
#if ENABLED(FILAMENT_RUNOUT_SENSOR)
runout.run();
2015-04-03 22:31:35 +00:00
#endif
if (commands_in_queue < BUFSIZE) get_available_commands();
2015-04-03 22:31:35 +00:00
2017-03-29 00:45:54 +00:00
const millis_t ms = millis();
2015-04-03 22:31:35 +00:00
if (max_inactive_time && ELAPSED(ms, gcode.previous_move_ms + max_inactive_time)) {
2017-06-09 15:51:23 +00:00
SERIAL_ERROR_START();
2017-05-20 08:03:08 +00:00
SERIAL_ECHOLNPAIR(MSG_KILL_INACTIVE_TIME, parser.command_ptr);
kill();
}
2017-02-10 06:13:58 +00:00
// Prevent steppers timing-out in the middle of M600
#if ENABLED(ADVANCED_PAUSE_FEATURE) && ENABLED(PAUSE_PARK_NO_STEPPER_TIMEOUT)
#define MOVE_AWAY_TEST !did_pause_print
2017-02-10 06:13:58 +00:00
#else
#define MOVE_AWAY_TEST true
#endif
if (stepper_inactive_time) {
if (planner.has_blocks_queued())
gcode.previous_move_ms = ms; // reset_stepper_timeout to keep steppers powered
else if (MOVE_AWAY_TEST && !ignore_stepper_queue && ELAPSED(ms, gcode.previous_move_ms + stepper_inactive_time)) {
#if ENABLED(DISABLE_INACTIVE_X)
disable_X();
#endif
#if ENABLED(DISABLE_INACTIVE_Y)
disable_Y();
#endif
#if ENABLED(DISABLE_INACTIVE_Z)
disable_Z();
#endif
#if ENABLED(DISABLE_INACTIVE_E)
disable_e_steppers();
#endif
2018-10-30 21:34:45 +00:00
#if HAS_LCD_MENU && ENABLED(AUTO_BED_LEVELING_UBL)
if (ubl.lcd_map_control) {
ubl.lcd_map_control = false;
ui.defer_status_screen(false);
}
#endif
}
}
2018-11-05 07:08:40 +00:00
#if PIN_EXISTS(CHDK) // Check if pin should be set to LOW (after M240 set it HIGH)
if (chdk_active && ELAPSED(ms, chdk_timeout)) {
chdk_active = false;
WRITE(CHDK_PIN, LOW);
}
#endif
2015-04-03 22:31:35 +00:00
#if HAS_KILL
2015-08-05 11:40:36 +00:00
// Check if the kill button was pressed and wait just in case it was an accidental
// key kill key press
// -------------------------------------------------------------------------------
2015-04-03 22:45:41 +00:00
static int killCount = 0; // make the inactivity button a bit less responsive
const int KILL_DELAY = 750;
2015-04-03 22:31:35 +00:00
if (!READ(KILL_PIN))
killCount++;
else if (killCount > 0)
killCount--;
2015-04-03 22:31:35 +00:00
// Exceeded threshold and we can confirm that it was not accidental
// KILL the machine
// ----------------------------------------------------------------
if (killCount >= KILL_DELAY) {
SERIAL_ERROR_MSG(MSG_KILL_BUTTON);
kill();
}
#endif
2015-04-03 22:31:35 +00:00
#if HAS_HOME
// Check to see if we have to home, use poor man's debouncer
// ---------------------------------------------------------
2015-04-03 22:45:41 +00:00
static int homeDebounceCount = 0; // poor man's debouncing count
const int HOME_DEBOUNCE_DELAY = 2500;
2018-10-19 18:52:44 +00:00
if (!IS_SD_PRINTING() && !READ(HOME_PIN)) {
2015-04-03 22:31:35 +00:00
if (!homeDebounceCount) {
2016-02-21 01:35:35 +00:00
enqueue_and_echo_commands_P(PSTR("G28"));
LCD_MESSAGEPGM(MSG_AUTO_HOME);
2015-04-03 22:31:35 +00:00
}
if (homeDebounceCount < HOME_DEBOUNCE_DELAY)
homeDebounceCount++;
else
homeDebounceCount = 0;
}
2015-04-03 22:31:35 +00:00
#endif
2015-08-05 11:40:36 +00:00
2017-05-04 21:38:29 +00:00
#if ENABLED(USE_CONTROLLER_FAN)
2017-09-18 06:44:59 +00:00
controllerfan_update(); // Check if fan should be turned on to cool stepper drivers down
#endif
2015-04-03 22:31:35 +00:00
#if ENABLED(AUTO_POWER_CONTROL)
powerManager.check();
#endif
#if ENABLED(EXTRUDER_RUNOUT_PREVENT)
if (thermalManager.degHotend(active_extruder) > EXTRUDER_RUNOUT_MINTEMP
&& ELAPSED(ms, gcode.previous_move_ms + (EXTRUDER_RUNOUT_SECONDS) * 1000UL)
&& !planner.has_blocks_queued()
) {
#if ENABLED(SWITCHING_EXTRUDER)
2018-05-27 22:56:21 +00:00
bool oldstatus;
switch (active_extruder) {
default: oldstatus = E0_ENABLE_READ; enable_E0(); break;
#if E_STEPPERS > 1
case 2: case 3: oldstatus = E1_ENABLE_READ; enable_E1(); break;
#if E_STEPPERS > 2
case 4: case 5: oldstatus = E2_ENABLE_READ; enable_E2(); break;
2018-05-27 22:56:21 +00:00
#endif // E_STEPPERS > 2
#endif // E_STEPPERS > 1
}
#else // !SWITCHING_EXTRUDER
bool oldstatus;
switch (active_extruder) {
default: oldstatus = E0_ENABLE_READ; enable_E0(); break;
#if E_STEPPERS > 1
case 1: oldstatus = E1_ENABLE_READ; enable_E1(); break;
#if E_STEPPERS > 2
case 2: oldstatus = E2_ENABLE_READ; enable_E2(); break;
#if E_STEPPERS > 3
case 3: oldstatus = E3_ENABLE_READ; enable_E3(); break;
#if E_STEPPERS > 4
case 4: oldstatus = E4_ENABLE_READ; enable_E4(); break;
#if E_STEPPERS > 5
case 5: oldstatus = E5_ENABLE_READ; enable_E5(); break;
#endif // E_STEPPERS > 5
#endif // E_STEPPERS > 4
#endif // E_STEPPERS > 3
#endif // E_STEPPERS > 2
#endif // E_STEPPERS > 1
}
#endif // !SWITCHING_EXTRUDER
const float olde = current_position[E_AXIS];
current_position[E_AXIS] += EXTRUDER_RUNOUT_EXTRUDE;
planner.buffer_line(current_position, MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED), active_extruder);
current_position[E_AXIS] = olde;
planner.set_e_position_mm(olde);
planner.synchronize();
2018-05-27 22:56:21 +00:00
#if ENABLED(SWITCHING_EXTRUDER)
2018-05-27 22:56:21 +00:00
switch (active_extruder) {
default: oldstatus = E0_ENABLE_WRITE(oldstatus); break;
#if E_STEPPERS > 1
case 2: case 3: oldstatus = E1_ENABLE_WRITE(oldstatus); break;
#if E_STEPPERS > 2
case 4: case 5: oldstatus = E2_ENABLE_WRITE(oldstatus); break;
2018-05-27 22:56:21 +00:00
#endif // E_STEPPERS > 2
#endif // E_STEPPERS > 1
}
#else // !SWITCHING_EXTRUDER
switch (active_extruder) {
case 0: E0_ENABLE_WRITE(oldstatus); break;
#if E_STEPPERS > 1
case 1: E1_ENABLE_WRITE(oldstatus); break;
#if E_STEPPERS > 2
case 2: E2_ENABLE_WRITE(oldstatus); break;
#if E_STEPPERS > 3
case 3: E3_ENABLE_WRITE(oldstatus); break;
#if E_STEPPERS > 4
case 4: E4_ENABLE_WRITE(oldstatus); break;
#if E_STEPPERS > 5
case 5: E5_ENABLE_WRITE(oldstatus); break;
#endif // E_STEPPERS > 5
#endif // E_STEPPERS > 4
#endif // E_STEPPERS > 3
#endif // E_STEPPERS > 2
#endif // E_STEPPERS > 1
}
#endif // !SWITCHING_EXTRUDER
gcode.previous_move_ms = ms; // reset_stepper_timeout to keep steppers powered
}
#endif // EXTRUDER_RUNOUT_PREVENT
2015-04-03 22:31:35 +00:00
#if ENABLED(DUAL_X_CARRIAGE)
// handle delayed move timeout
2016-04-10 22:55:12 +00:00
if (delayed_move_time && ELAPSED(ms, delayed_move_time + 1000UL) && IsRunning()) {
// travel moves have been received so enact them
delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
set_destination_from_current();
prepare_move_to_destination();
}
#endif
2015-04-03 22:31:35 +00:00
#if ENABLED(TEMP_STAT_LEDS)
2015-04-03 22:31:35 +00:00
handle_status_leds();
#endif
2015-04-03 22:31:35 +00:00
2017-12-15 21:03:14 +00:00
#if ENABLED(MONITOR_DRIVER_STATUS)
monitor_tmc_driver();
#endif
// Limit check_axes_activity frequency to 10Hz
static millis_t next_check_axes_ms = 0;
if (ELAPSED(ms, next_check_axes_ms)) {
planner.check_axes_activity();
next_check_axes_ms = ms + 100UL;
}
}
/**
* Standard idle routine keeps the machine alive
*/
void idle(
#if ENABLED(ADVANCED_PAUSE_FEATURE)
bool no_stepper_sleep/*=false*/
#endif
) {
#if ENABLED(MAX7219_DEBUG)
max7219.idle_tasks();
#endif
ui.update();
2017-09-09 04:49:49 +00:00
#if ENABLED(HOST_KEEPALIVE_FEATURE)
gcode.host_keepalive();
#endif
manage_inactivity(
#if ENABLED(ADVANCED_PAUSE_FEATURE)
no_stepper_sleep
#endif
);
thermalManager.manage_heater();
#if ENABLED(PRINTCOUNTER)
print_job_timer.tick();
#endif
2016-10-05 02:31:50 +00:00
#if HAS_BUZZER && DISABLED(LCD_USE_I2C_BUZZER)
buzzer.tick();
#endif
#if ENABLED(I2C_POSITION_ENCODERS)
static millis_t i2cpem_next_update_ms;
if (planner.has_blocks_queued() && ELAPSED(millis(), i2cpem_next_update_ms)) {
I2CPEM.update();
i2cpem_next_update_ms = millis() + I2CPE_MIN_UPD_TIME_MS;
}
#endif
#ifdef HAL_IDLETASK
HAL_idletask();
#endif
#if HAS_AUTO_REPORTING
if (!suspend_auto_report) {
#if ENABLED(AUTO_REPORT_TEMPERATURES)
thermalManager.auto_report_temperatures();
#endif
#if ENABLED(AUTO_REPORT_SD_STATUS)
card.auto_report_sd_status();
#endif
}
#endif
2018-07-02 15:06:17 +00:00
#if ENABLED(USB_FLASH_DRIVE_SUPPORT)
Sd2Card::idle();
#endif
}
/**
* Kill all activity and lock the machine.
* After this the machine will need to be reset.
*/
void kill(PGM_P const lcd_msg/*=NULL*/) {
thermalManager.disable_all_heaters();
2017-03-29 00:45:54 +00:00
SERIAL_ERROR_MSG(MSG_ERR_KILLED);
#if HAS_SPI_LCD || ENABLED(EXTENSIBLE_UI)
ui.kill_screen(lcd_msg ? lcd_msg : PSTR(MSG_KILLED));
2015-10-04 15:33:55 +00:00
#else
UNUSED(lcd_msg);
#endif
2017-07-28 04:42:01 +00:00
#ifdef ACTION_ON_KILL
SERIAL_ECHOLNPGM("//action:" ACTION_ON_KILL);
#endif
2017-07-07 02:24:30 +00:00
minkill();
}
void minkill() {
// Wait a short time (allows messages to get out before shutting down.
2018-10-21 18:43:21 +00:00
for (int i = 1000; i--;) DELAY_US(600);
cli(); // Stop interrupts
// Wait to ensure all interrupts stopped
2018-10-21 18:43:21 +00:00
for (int i = 1000; i--;) DELAY_US(250);
thermalManager.disable_all_heaters(); // turn off heaters again
2015-03-30 23:50:05 +00:00
#if HAS_POWER_SWITCH
PSU_OFF();
2015-03-30 23:50:05 +00:00
#endif
#if HAS_SUICIDE
suicide();
#endif
while (1) {
#if ENABLED(USE_WATCHDOG)
watchdog_reset();
#endif
} // Wait for reset
}
/**
* Turn off heaters and stop the print in progress
* After a stop the machine may be resumed with M999
*/
2016-04-18 07:05:22 +00:00
void stop() {
thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
print_job_timer.stop();
#if ENABLED(PROBING_FANS_OFF)
if (thermalManager.fans_paused) thermalManager.set_fans_paused(false); // put things back the way they were
#endif
if (IsRunning()) {
Stopped_gcode_LastN = gcode_LastN; // Save last g_code for restart
SERIAL_ERROR_MSG(MSG_ERR_STOPPED);
LCD_MESSAGEPGM(MSG_STOPPED);
safe_delay(350); // allow enough time for messages to get out before stopping
Running = false;
}
}
2016-09-12 01:21:54 +00:00
/**
* Marlin entry-point: Set up before the program loop
* - Set up the kill pin, filament runout, power hold
* - Start the serial port
* - Print startup messages and diagnostics
* - Get EEPROM or default settings
* - Initialize managers for:
* temperature
* planner
* watchdog
* stepper
* photo pin
* servos
* LCD controller
* Digipot I2C
* Z probe sled
* status LEDs
*/
void setup() {
#ifdef HAL_INIT
HAL_init();
#endif
#if ENABLED(MAX7219_DEBUG)
max7219.init();
#endif
#if ENABLED(DISABLE_DEBUG)
// Disable any hardware debug to free up pins for IO
#ifdef JTAGSWD_DISABLE
JTAGSWD_DISABLE();
#elif defined(JTAG_DISABLE)
JTAG_DISABLE();
#else
#error "DISABLE_DEBUG is not supported for the selected MCU/Board"
#endif
#elif ENABLED(DISABLE_JTAG)
// Disable JTAG to free up pins for IO
#ifdef JTAG_DISABLE
JTAG_DISABLE();
#else
#error "DISABLE_JTAG is not supported for the selected MCU/Board"
#endif
2016-09-12 01:21:54 +00:00
#endif
#if ENABLED(FILAMENT_RUNOUT_SENSOR)
runout.setup();
2016-09-12 01:21:54 +00:00
#endif
setup_killpin();
setup_powerhold();
#if HAS_STEPPER_RESET
disableStepperDrivers();
#endif
2017-11-05 14:49:38 +00:00
#if NUM_SERIAL > 0
MYSERIAL0.begin(BAUDRATE);
#if NUM_SERIAL > 1
MYSERIAL1.begin(BAUDRATE);
#endif
#endif
#if NUM_SERIAL > 0
uint32_t serial_connect_timeout = millis() + 1000UL;
2017-10-08 16:38:10 +00:00
while (!MYSERIAL0 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
2017-11-05 14:49:38 +00:00
#if NUM_SERIAL > 1
serial_connect_timeout = millis() + 1000UL;
2017-10-08 16:38:10 +00:00
while (!MYSERIAL1 && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
2017-11-05 14:49:38 +00:00
#endif
#endif
SERIAL_ECHOLNPGM("start");
2017-06-09 15:51:23 +00:00
SERIAL_ECHO_START();
2016-09-12 01:21:54 +00:00
2018-10-06 22:18:10 +00:00
#if TMC_HAS_SPI
2018-10-03 07:48:49 +00:00
#if DISABLED(TMC_USE_SW_SPI)
SPI.begin();
#endif
tmc_init_cs_pins();
#endif
#if HAS_DRIVER(TMC2208)
tmc2208_serial_begin();
#endif
#ifdef BOARD_INIT
BOARD_INIT();
#endif
2016-09-12 01:21:54 +00:00
// Check startup - does nothing if bootloader sets MCUSR to 0
byte mcu = HAL_get_reset_source();
2017-06-16 19:20:58 +00:00
if (mcu & 1) SERIAL_ECHOLNPGM(MSG_POWERUP);
if (mcu & 2) SERIAL_ECHOLNPGM(MSG_EXTERNAL_RESET);
if (mcu & 4) SERIAL_ECHOLNPGM(MSG_BROWNOUT_RESET);
if (mcu & 8) SERIAL_ECHOLNPGM(MSG_WATCHDOG_RESET);
2016-09-12 01:21:54 +00:00
if (mcu & 32) SERIAL_ECHOLNPGM(MSG_SOFTWARE_RESET);
HAL_clear_reset_source();
2016-09-12 01:21:54 +00:00
SERIAL_ECHOPGM(MSG_MARLIN);
2016-09-12 22:49:35 +00:00
SERIAL_CHAR(' ');
SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
2017-06-09 15:51:23 +00:00
SERIAL_EOL();
2016-09-12 01:21:54 +00:00
2016-09-12 22:49:35 +00:00
#if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
2017-06-09 15:51:23 +00:00
SERIAL_ECHO_START();
2016-09-12 22:49:35 +00:00
SERIAL_ECHOPGM(MSG_CONFIGURATION_VER);
SERIAL_ECHOPGM(STRING_DISTRIBUTION_DATE);
SERIAL_ECHOLNPGM(MSG_AUTHOR STRING_CONFIG_H_AUTHOR);
SERIAL_ECHO_MSG("Compiled: " __DATE__);
2016-09-12 22:49:35 +00:00
#endif
2016-09-12 01:21:54 +00:00
2017-06-09 15:51:23 +00:00
SERIAL_ECHO_START();
2016-09-12 22:49:35 +00:00
SERIAL_ECHOPAIR(MSG_FREE_MEMORY, freeMemory());
SERIAL_ECHOLNPAIR(MSG_PLANNER_BUFFER_BYTES, (int)sizeof(block_t)*BLOCK_BUFFER_SIZE);
2016-09-12 01:21:54 +00:00
queue_setup();
2016-09-12 01:21:54 +00:00
// Load data from EEPROM if available (or use defaults)
// This also updates variables in the planner, elsewhere
2017-04-10 02:47:49 +00:00
(void)settings.load();
#if HAS_M206_COMMAND
2017-03-05 00:01:33 +00:00
// Initialize current position based on home_offset
2017-03-05 01:19:06 +00:00
COPY(current_position, home_offset);
2017-03-05 00:01:33 +00:00
#else
ZERO(current_position);
#endif
2016-09-12 01:21:54 +00:00
// Vital to init stepper/planner equivalent for current_position
sync_plan_position();
2016-09-12 01:21:54 +00:00
thermalManager.init(); // Initialize temperature loop
print_job_timer.init(); // Initial setup of print job timer
endstops.init(); // Init endstops and pullups
stepper.init(); // Init stepper. This enables interrupts!
2017-09-08 20:35:25 +00:00
#if HAS_SERVOS
servo_init();
#endif
2016-09-12 01:21:54 +00:00
2018-04-02 04:54:12 +00:00
#if HAS_Z_SERVO_PROBE
2017-11-19 19:39:46 +00:00
servo_probe_init();
#endif
2016-11-01 11:24:21 +00:00
#if HAS_PHOTOGRAPH
OUT_WRITE(PHOTOGRAPH_PIN, LOW);
#endif
2017-04-07 18:52:45 +00:00
#if ENABLED(SPINDLE_LASER_ENABLE)
OUT_WRITE(SPINDLE_LASER_ENABLE_PIN, !SPINDLE_LASER_ENABLE_INVERT); // init spindle to off
#if SPINDLE_DIR_CHANGE
OUT_WRITE(SPINDLE_DIR_PIN, SPINDLE_INVERT_DIR ? 255 : 0); // init rotation to clockwise (M3)
#endif
2017-09-06 11:28:33 +00:00
#if ENABLED(SPINDLE_LASER_PWM) && defined(SPINDLE_LASER_PWM_PIN) && SPINDLE_LASER_PWM_PIN >= 0
2017-04-07 18:52:45 +00:00
SET_OUTPUT(SPINDLE_LASER_PWM_PIN);
analogWrite(SPINDLE_LASER_PWM_PIN, SPINDLE_LASER_PWM_INVERT ? 255 : 0); // set to lowest speed
#endif
#endif
2016-09-12 01:21:54 +00:00
#if HAS_BED_PROBE
endstops.enable_z_probe(false);
#endif
2017-05-04 21:38:29 +00:00
#if ENABLED(USE_CONTROLLER_FAN)
2018-10-01 23:33:52 +00:00
SET_OUTPUT(CONTROLLER_FAN_PIN);
2016-09-12 01:21:54 +00:00
#endif
#if HAS_STEPPER_RESET
enableStepperDrivers();
#endif
#if ENABLED(DIGIPOT_I2C)
digipot_i2c_init();
#endif
#if ENABLED(DAC_STEPPER_CURRENT)
dac_init();
#endif
2017-04-14 21:36:02 +00:00
#if (ENABLED(Z_PROBE_SLED) || ENABLED(SOLENOID_PROBE)) && HAS_SOLENOID_1
2018-10-01 23:33:52 +00:00
OUT_WRITE(SOL1_PIN, LOW); // OFF
2017-04-14 21:36:02 +00:00
#endif
2016-09-12 01:21:54 +00:00
2017-07-02 07:26:49 +00:00
#if HAS_HOME
SET_INPUT_PULLUP(HOME_PIN);
#endif
2016-09-12 01:21:54 +00:00
2016-09-24 20:33:29 +00:00
#if PIN_EXISTS(STAT_LED_RED)
2018-10-01 23:33:52 +00:00
OUT_WRITE(STAT_LED_RED_PIN, LOW); // OFF
2016-09-12 01:21:54 +00:00
#endif
2016-09-24 20:33:29 +00:00
#if PIN_EXISTS(STAT_LED_BLUE)
2018-10-01 23:33:52 +00:00
OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // OFF
2016-09-12 01:21:54 +00:00
#endif
2017-11-28 07:07:10 +00:00
#if HAS_COLOR_LEDS
leds.setup();
#endif
2017-04-14 21:54:53 +00:00
#if ENABLED(RGB_LED) || ENABLED(RGBW_LED)
2017-03-18 15:17:00 +00:00
SET_OUTPUT(RGB_LED_R_PIN);
SET_OUTPUT(RGB_LED_G_PIN);
SET_OUTPUT(RGB_LED_B_PIN);
2017-04-14 21:54:53 +00:00
#if ENABLED(RGBW_LED)
SET_OUTPUT(RGB_LED_W_PIN);
#endif
2016-11-30 01:51:13 +00:00
#endif
2019-01-16 00:50:56 +00:00
#if HAS_CASE_LIGHT
update_case_light();
#endif
#if ENABLED(MK2_MULTIPLEXER)
SET_OUTPUT(E_MUX0_PIN);
SET_OUTPUT(E_MUX1_PIN);
SET_OUTPUT(E_MUX2_PIN);
#endif
#if HAS_FANMUX
fanmux_init();
#endif
ui.init();
ui.reset_status();
2017-07-02 05:35:41 +00:00
2016-09-12 01:21:54 +00:00
#if ENABLED(SHOW_BOOTSCREEN)
ui.show_bootscreen();
2016-09-12 01:21:54 +00:00
#endif
2018-10-16 08:38:57 +00:00
#if ENABLED(MIXING_EXTRUDER)
mixer.init();
2016-09-12 01:21:54 +00:00
#endif
#if ENABLED(BLTOUCH)
2017-09-08 20:35:25 +00:00
bltouch_init();
#endif
#if ENABLED(I2C_POSITION_ENCODERS)
I2CPEM.init();
#endif
2016-09-12 01:21:54 +00:00
#if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
i2c.onReceive(i2c_on_receive);
i2c.onRequest(i2c_on_request);
#endif
#if DO_SWITCH_EXTRUDER
2018-11-17 02:47:07 +00:00
move_extruder_servo(0); // Initialize extruder servo
#endif
#if ENABLED(SWITCHING_NOZZLE)
2018-11-17 02:47:07 +00:00
move_nozzle_servo(0); // Initialize nozzle servo
#endif
#if ENABLED(PARKING_EXTRUDER)
2017-09-18 06:05:44 +00:00
pe_magnet_init();
#endif
2018-03-08 23:34:50 +00:00
2018-04-22 00:41:26 +00:00
#if ENABLED(POWER_LOSS_RECOVERY)
2018-11-17 02:47:07 +00:00
recovery.check();
2018-04-22 00:41:26 +00:00
#endif
2018-11-17 02:47:07 +00:00
#if ENABLED(USE_WATCHDOG)
watchdog_init(); // Reinit watchdog after HAL_get_reset_source call
2018-03-08 23:34:50 +00:00
#endif
#if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
init_closedloop();
#endif
2018-09-12 08:49:46 +00:00
#if ENABLED(SDSUPPORT) && DISABLED(ULTRA_LCD)
card.beginautostart();
#endif
#if HAS_TRINAMIC && DISABLED(PS_DEFAULT_OFF)
test_tmc_connection(true, true, true, true);
#endif
2016-09-12 01:21:54 +00:00
}
/**
* The main Marlin program loop
*
* - Save or log commands to SD
* - Process available commands (if not saving)
* - Call endstop manager
2018-05-01 06:01:02 +00:00
* - Call inactivity manager
2016-09-12 01:21:54 +00:00
*/
void loop() {
for (;;) {
2018-05-01 06:01:02 +00:00
#if ENABLED(SDSUPPORT)
card.checkautostart();
2018-10-19 20:27:07 +00:00
if (card.flag.abort_sd_printing) {
card.stopSDPrint(
#if SD_RESORT
true
#endif
);
clear_command_queue();
quickstop_stepper();
print_job_timer.stop();
thermalManager.disable_all_heaters();
thermalManager.zero_fan_speeds();
wait_for_heatup = false;
#if ENABLED(POWER_LOSS_RECOVERY)
card.removeJobRecoveryFile();
#endif
}
2018-10-19 19:25:07 +00:00
#endif // SDSUPPORT
if (commands_in_queue < BUFSIZE) get_available_commands();
advance_command_queue();
endstops.event_handler();
idle();
}
2016-09-12 01:21:54 +00:00
}