My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MarlinCore.cpp 40KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * About Marlin
  24. *
  25. * This firmware is a mashup between Sprinter and grbl.
  26. * - https://github.com/kliment/Sprinter
  27. * - https://github.com/grbl/grbl
  28. */
  29. #include "MarlinCore.h"
  30. #if ENABLED(MARLIN_DEV_MODE)
  31. #warning "WARNING! Disable MARLIN_DEV_MODE for the final build!"
  32. #endif
  33. #include "HAL/shared/Delay.h"
  34. #include "HAL/shared/esp_wifi.h"
  35. #include "HAL/shared/cpu_exception/exception_hook.h"
  36. #ifdef ARDUINO
  37. #include <pins_arduino.h>
  38. #endif
  39. #include <math.h>
  40. #include "core/utility.h"
  41. #include "module/motion.h"
  42. #include "module/planner.h"
  43. #include "module/endstops.h"
  44. #include "module/temperature.h"
  45. #include "module/settings.h"
  46. #include "module/printcounter.h" // PrintCounter or Stopwatch
  47. #include "module/stepper.h"
  48. #include "module/stepper/indirection.h"
  49. #include "gcode/gcode.h"
  50. #include "gcode/parser.h"
  51. #include "gcode/queue.h"
  52. #include "feature/pause.h"
  53. #include "sd/cardreader.h"
  54. #include "lcd/marlinui.h"
  55. #if HAS_TOUCH_BUTTONS
  56. #include "lcd/touch/touch_buttons.h"
  57. #endif
  58. #if HAS_TFT_LVGL_UI
  59. #include "lcd/extui/lib/mks_ui/tft_lvgl_configuration.h"
  60. #include "lcd/extui/lib/mks_ui/draw_ui.h"
  61. #include "lcd/extui/lib/mks_ui/mks_hardware_test.h"
  62. #include <lvgl.h>
  63. #endif
  64. #if ENABLED(DWIN_CREALITY_LCD)
  65. #include "lcd/dwin/e3v2/dwin.h"
  66. #include "lcd/dwin/dwin_lcd.h"
  67. #include "lcd/dwin/e3v2/rotary_encoder.h"
  68. #endif
  69. #if ENABLED(EXTENSIBLE_UI)
  70. #include "lcd/extui/ui_api.h"
  71. #endif
  72. #if HAS_ETHERNET
  73. #include "feature/ethernet.h"
  74. #endif
  75. #if ENABLED(IIC_BL24CXX_EEPROM)
  76. #include "libs/BL24CXX.h"
  77. #endif
  78. #if ENABLED(DIRECT_STEPPING)
  79. #include "feature/direct_stepping.h"
  80. #endif
  81. #if ENABLED(HOST_ACTION_COMMANDS)
  82. #include "feature/host_actions.h"
  83. #endif
  84. #if USE_BEEPER
  85. #include "libs/buzzer.h"
  86. #endif
  87. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  88. #include "feature/closedloop.h"
  89. #endif
  90. #if HAS_MOTOR_CURRENT_I2C
  91. #include "feature/digipot/digipot.h"
  92. #endif
  93. #if ENABLED(MIXING_EXTRUDER)
  94. #include "feature/mixing.h"
  95. #endif
  96. #if ENABLED(MAX7219_DEBUG)
  97. #include "feature/max7219.h"
  98. #endif
  99. #if HAS_COLOR_LEDS
  100. #include "feature/leds/leds.h"
  101. #endif
  102. #if ENABLED(BLTOUCH)
  103. #include "feature/bltouch.h"
  104. #endif
  105. #if ENABLED(POLL_JOG)
  106. #include "feature/joystick.h"
  107. #endif
  108. #if HAS_SERVOS
  109. #include "module/servo.h"
  110. #endif
  111. #if ENABLED(HAS_MOTOR_CURRENT_DAC)
  112. #include "feature/dac/stepper_dac.h"
  113. #endif
  114. #if ENABLED(EXPERIMENTAL_I2CBUS)
  115. #include "feature/twibus.h"
  116. #endif
  117. #if ENABLED(I2C_POSITION_ENCODERS)
  118. #include "feature/encoder_i2c.h"
  119. #endif
  120. #if HAS_TRINAMIC_CONFIG && DISABLED(PSU_DEFAULT_OFF)
  121. #include "feature/tmc_util.h"
  122. #endif
  123. #if HAS_CUTTER
  124. #include "feature/spindle_laser.h"
  125. #endif
  126. #if ENABLED(SDSUPPORT)
  127. CardReader card;
  128. #endif
  129. #if ENABLED(G38_PROBE_TARGET)
  130. uint8_t G38_move; // = 0
  131. bool G38_did_trigger; // = false
  132. #endif
  133. #if ENABLED(DELTA)
  134. #include "module/delta.h"
  135. #elif IS_SCARA
  136. #include "module/scara.h"
  137. #endif
  138. #if HAS_LEVELING
  139. #include "feature/bedlevel/bedlevel.h"
  140. #endif
  141. #if ENABLED(GCODE_REPEAT_MARKERS)
  142. #include "feature/repeat.h"
  143. #endif
  144. #if ENABLED(POWER_LOSS_RECOVERY)
  145. #include "feature/powerloss.h"
  146. #endif
  147. #if ENABLED(CANCEL_OBJECTS)
  148. #include "feature/cancel_object.h"
  149. #endif
  150. #if HAS_FILAMENT_SENSOR
  151. #include "feature/runout.h"
  152. #endif
  153. #if EITHER(PROBE_TARE, HAS_Z_SERVO_PROBE)
  154. #include "module/probe.h"
  155. #endif
  156. #if ENABLED(HOTEND_IDLE_TIMEOUT)
  157. #include "feature/hotend_idle.h"
  158. #endif
  159. #if ENABLED(TEMP_STAT_LEDS)
  160. #include "feature/leds/tempstat.h"
  161. #endif
  162. #if ENABLED(CASE_LIGHT_ENABLE)
  163. #include "feature/caselight.h"
  164. #endif
  165. #if HAS_FANMUX
  166. #include "feature/fanmux.h"
  167. #endif
  168. #if DO_SWITCH_EXTRUDER || ANY(SWITCHING_NOZZLE, PARKING_EXTRUDER, MAGNETIC_PARKING_EXTRUDER, ELECTROMAGNETIC_SWITCHING_TOOLHEAD, SWITCHING_TOOLHEAD)
  169. #include "module/tool_change.h"
  170. #endif
  171. #if ENABLED(USE_CONTROLLER_FAN)
  172. #include "feature/controllerfan.h"
  173. #endif
  174. #if HAS_PRUSA_MMU2
  175. #include "feature/mmu/mmu2.h"
  176. #endif
  177. #if HAS_L64XX
  178. #include "libs/L64XX/L64XX_Marlin.h"
  179. #endif
  180. #if ENABLED(PASSWORD_FEATURE)
  181. #include "feature/password/password.h"
  182. #endif
  183. #if ENABLED(DGUS_LCD_UI_MKS)
  184. #include "lcd/extui/lib/dgus/DGUSScreenHandler.h"
  185. #endif
  186. PGMSTR(M112_KILL_STR, "M112 Shutdown");
  187. MarlinState marlin_state = MF_INITIALIZING;
  188. // For M109 and M190, this flag may be cleared (by M108) to exit the wait loop
  189. bool wait_for_heatup = true;
  190. // For M0/M1, this flag may be cleared (by M108) to exit the wait-for-user loop
  191. #if HAS_RESUME_CONTINUE
  192. bool wait_for_user; // = false;
  193. void wait_for_user_response(millis_t ms/*=0*/, const bool no_sleep/*=false*/) {
  194. UNUSED(no_sleep);
  195. KEEPALIVE_STATE(PAUSED_FOR_USER);
  196. wait_for_user = true;
  197. if (ms) ms += millis(); // expire time
  198. while (wait_for_user && !(ms && ELAPSED(millis(), ms)))
  199. idle(TERN_(ADVANCED_PAUSE_FEATURE, no_sleep));
  200. wait_for_user = false;
  201. }
  202. #endif
  203. /**
  204. * ***************************************************************************
  205. * ******************************** FUNCTIONS ********************************
  206. * ***************************************************************************
  207. */
  208. /**
  209. * Stepper Reset (RigidBoard, et.al.)
  210. */
  211. #if HAS_STEPPER_RESET
  212. void disableStepperDrivers() { OUT_WRITE(STEPPER_RESET_PIN, LOW); } // Drive down to keep motor driver chips in reset
  213. void enableStepperDrivers() { SET_INPUT(STEPPER_RESET_PIN); } // Set to input, allowing pullups to pull the pin high
  214. #endif
  215. /**
  216. * Sensitive pin test for M42, M226
  217. */
  218. #include "pins/sensitive_pins.h"
  219. #pragma GCC diagnostic push
  220. #pragma GCC diagnostic ignored "-Wnarrowing"
  221. bool pin_is_protected(const pin_t pin) {
  222. static const pin_t sensitive_pins[] PROGMEM = SENSITIVE_PINS;
  223. LOOP_L_N(i, COUNT(sensitive_pins)) {
  224. pin_t sensitive_pin;
  225. memcpy_P(&sensitive_pin, &sensitive_pins[i], sizeof(pin_t));
  226. if (pin == sensitive_pin) return true;
  227. }
  228. return false;
  229. }
  230. #pragma GCC diagnostic pop
  231. void enable_e_steppers() {
  232. #define _ENA_E(N) ENABLE_AXIS_E##N();
  233. REPEAT(E_STEPPERS, _ENA_E)
  234. }
  235. void enable_all_steppers() {
  236. TERN_(AUTO_POWER_CONTROL, powerManager.power_on());
  237. ENABLE_AXIS_X();
  238. ENABLE_AXIS_Y();
  239. ENABLE_AXIS_Z();
  240. enable_e_steppers();
  241. TERN_(EXTENSIBLE_UI, ExtUI::onSteppersEnabled());
  242. }
  243. void disable_e_steppers() {
  244. #define _DIS_E(N) DISABLE_AXIS_E##N();
  245. REPEAT(E_STEPPERS, _DIS_E)
  246. }
  247. void disable_e_stepper(const uint8_t e) {
  248. #define _CASE_DIS_E(N) case N: DISABLE_AXIS_E##N(); break;
  249. switch (e) {
  250. REPEAT(EXTRUDERS, _CASE_DIS_E)
  251. }
  252. }
  253. void disable_all_steppers() {
  254. DISABLE_AXIS_X();
  255. DISABLE_AXIS_Y();
  256. DISABLE_AXIS_Z();
  257. disable_e_steppers();
  258. TERN_(EXTENSIBLE_UI, ExtUI::onSteppersDisabled());
  259. }
  260. /**
  261. * A Print Job exists when the timer is running or SD printing
  262. */
  263. bool printJobOngoing() {
  264. return print_job_timer.isRunning() || IS_SD_PRINTING();
  265. }
  266. /**
  267. * Printing is active when the print job timer is running
  268. */
  269. bool printingIsActive() {
  270. return !did_pause_print && (print_job_timer.isRunning() || IS_SD_PRINTING());
  271. }
  272. /**
  273. * Printing is paused according to SD or host indicators
  274. */
  275. bool printingIsPaused() {
  276. return did_pause_print || print_job_timer.isPaused() || IS_SD_PAUSED();
  277. }
  278. void startOrResumeJob() {
  279. if (!printingIsPaused()) {
  280. TERN_(GCODE_REPEAT_MARKERS, repeat.reset());
  281. TERN_(CANCEL_OBJECTS, cancelable.reset());
  282. TERN_(LCD_SHOW_E_TOTAL, e_move_accumulator = 0);
  283. #if BOTH(LCD_SET_PROGRESS_MANUALLY, USE_M73_REMAINING_TIME)
  284. ui.reset_remaining_time();
  285. #endif
  286. }
  287. print_job_timer.start();
  288. }
  289. #if ENABLED(SDSUPPORT)
  290. inline void abortSDPrinting() {
  291. IF_DISABLED(NO_SD_AUTOSTART, card.autofile_cancel());
  292. card.endFilePrint(TERN_(SD_RESORT, true));
  293. queue.clear();
  294. quickstop_stepper();
  295. print_job_timer.abort();
  296. IF_DISABLED(SD_ABORT_NO_COOLDOWN, thermalManager.disable_all_heaters());
  297. TERN(HAS_CUTTER, cutter.kill(), thermalManager.zero_fan_speeds()); // Full cutter shutdown including ISR control
  298. wait_for_heatup = false;
  299. TERN_(POWER_LOSS_RECOVERY, recovery.purge());
  300. #ifdef EVENT_GCODE_SD_ABORT
  301. queue.inject_P(PSTR(EVENT_GCODE_SD_ABORT));
  302. #endif
  303. TERN_(PASSWORD_AFTER_SD_PRINT_ABORT, password.lock_machine());
  304. }
  305. inline void finishSDPrinting() {
  306. if (queue.enqueue_one_P(PSTR("M1001"))) {
  307. marlin_state = MF_RUNNING;
  308. TERN_(PASSWORD_AFTER_SD_PRINT_END, password.lock_machine());
  309. TERN_(DGUS_LCD_UI_MKS, ScreenHandler.SDPrintingFinished());
  310. }
  311. }
  312. #endif // SDSUPPORT
  313. /**
  314. * Minimal management of Marlin's core activities:
  315. * - Keep the command buffer full
  316. * - Check for maximum inactive time between commands
  317. * - Check for maximum inactive time between stepper commands
  318. * - Check if CHDK_PIN needs to go LOW
  319. * - Check for KILL button held down
  320. * - Check for HOME button held down
  321. * - Check for CUSTOM USER button held down
  322. * - Check if cooling fan needs to be switched on
  323. * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
  324. * - Pulse FET_SAFETY_PIN if it exists
  325. */
  326. inline void manage_inactivity(const bool ignore_stepper_queue=false) {
  327. queue.get_available_commands();
  328. const millis_t ms = millis();
  329. // Prevent steppers timing-out in the middle of M600
  330. // unless PAUSE_PARK_NO_STEPPER_TIMEOUT is disabled
  331. const bool parked_or_ignoring = ignore_stepper_queue
  332. || TERN0(PAUSE_PARK_NO_STEPPER_TIMEOUT, did_pause_print);
  333. // Reset both the M18/M84 activity timeout and the M85 max 'kill' timeout
  334. if (parked_or_ignoring) gcode.reset_stepper_timeout(ms);
  335. if (gcode.stepper_max_timed_out(ms)) {
  336. SERIAL_ERROR_MSG(STR_KILL_INACTIVE_TIME, parser.command_ptr);
  337. kill();
  338. }
  339. // M18 / M84 : Handle steppers inactive time timeout
  340. if (gcode.stepper_inactive_time) {
  341. static bool already_shutdown_steppers; // = false
  342. // Any moves in the planner? Resets both the M18/M84
  343. // activity timeout and the M85 max 'kill' timeout
  344. if (planner.has_blocks_queued())
  345. gcode.reset_stepper_timeout(ms);
  346. else if (!parked_or_ignoring && gcode.stepper_inactive_timeout()) {
  347. if (!already_shutdown_steppers) {
  348. already_shutdown_steppers = true; // L6470 SPI will consume 99% of free time without this
  349. // Individual axes will be disabled if configured
  350. if (ENABLED(DISABLE_INACTIVE_X)) DISABLE_AXIS_X();
  351. if (ENABLED(DISABLE_INACTIVE_Y)) DISABLE_AXIS_Y();
  352. if (ENABLED(DISABLE_INACTIVE_Z)) DISABLE_AXIS_Z();
  353. if (ENABLED(DISABLE_INACTIVE_E)) disable_e_steppers();
  354. TERN_(AUTO_BED_LEVELING_UBL, ubl.steppers_were_disabled());
  355. }
  356. }
  357. else
  358. already_shutdown_steppers = false;
  359. }
  360. #if PIN_EXISTS(CHDK) // Check if pin should be set to LOW (after M240 set it HIGH)
  361. extern millis_t chdk_timeout;
  362. if (chdk_timeout && ELAPSED(ms, chdk_timeout)) {
  363. chdk_timeout = 0;
  364. WRITE(CHDK_PIN, LOW);
  365. }
  366. #endif
  367. #if HAS_KILL
  368. // Check if the kill button was pressed and wait just in case it was an accidental
  369. // key kill key press
  370. // -------------------------------------------------------------------------------
  371. static int killCount = 0; // make the inactivity button a bit less responsive
  372. const int KILL_DELAY = 750;
  373. if (kill_state())
  374. killCount++;
  375. else if (killCount > 0)
  376. killCount--;
  377. // Exceeded threshold and we can confirm that it was not accidental
  378. // KILL the machine
  379. // ----------------------------------------------------------------
  380. if (killCount >= KILL_DELAY) {
  381. SERIAL_ERROR_MSG(STR_KILL_BUTTON);
  382. kill();
  383. }
  384. #endif
  385. #if HAS_HOME
  386. // Handle a standalone HOME button
  387. constexpr millis_t HOME_DEBOUNCE_DELAY = 1000UL;
  388. static millis_t next_home_key_ms; // = 0
  389. if (!IS_SD_PRINTING() && !READ(HOME_PIN)) { // HOME_PIN goes LOW when pressed
  390. const millis_t ms = millis();
  391. if (ELAPSED(ms, next_home_key_ms)) {
  392. next_home_key_ms = ms + HOME_DEBOUNCE_DELAY;
  393. LCD_MESSAGEPGM(MSG_AUTO_HOME);
  394. queue.inject_P(G28_STR);
  395. }
  396. }
  397. #endif
  398. #if ENABLED(CUSTOM_USER_BUTTONS)
  399. // Handle a custom user button if defined
  400. const bool printer_not_busy = !printingIsActive();
  401. #define HAS_CUSTOM_USER_BUTTON(N) (PIN_EXISTS(BUTTON##N) && defined(BUTTON##N##_HIT_STATE) && defined(BUTTON##N##_GCODE) && defined(BUTTON##N##_DESC))
  402. #define CHECK_CUSTOM_USER_BUTTON(N) do{ \
  403. constexpr millis_t CUB_DEBOUNCE_DELAY_##N = 250UL; \
  404. static millis_t next_cub_ms_##N; \
  405. if (BUTTON##N##_HIT_STATE == READ(BUTTON##N##_PIN) \
  406. && (ENABLED(BUTTON##N##_WHEN_PRINTING) || printer_not_busy)) { \
  407. const millis_t ms = millis(); \
  408. if (ELAPSED(ms, next_cub_ms_##N)) { \
  409. next_cub_ms_##N = ms + CUB_DEBOUNCE_DELAY_##N; \
  410. if (strlen(BUTTON##N##_DESC)) \
  411. LCD_MESSAGEPGM_P(PSTR(BUTTON##N##_DESC)); \
  412. queue.inject_P(PSTR(BUTTON##N##_GCODE)); \
  413. } \
  414. } \
  415. }while(0)
  416. #if HAS_CUSTOM_USER_BUTTON(1)
  417. CHECK_CUSTOM_USER_BUTTON(1);
  418. #endif
  419. #if HAS_CUSTOM_USER_BUTTON(2)
  420. CHECK_CUSTOM_USER_BUTTON(2);
  421. #endif
  422. #if HAS_CUSTOM_USER_BUTTON(3)
  423. CHECK_CUSTOM_USER_BUTTON(3);
  424. #endif
  425. #if HAS_CUSTOM_USER_BUTTON(4)
  426. CHECK_CUSTOM_USER_BUTTON(4);
  427. #endif
  428. #if HAS_CUSTOM_USER_BUTTON(5)
  429. CHECK_CUSTOM_USER_BUTTON(5);
  430. #endif
  431. #if HAS_CUSTOM_USER_BUTTON(6)
  432. CHECK_CUSTOM_USER_BUTTON(6);
  433. #endif
  434. #if HAS_CUSTOM_USER_BUTTON(7)
  435. CHECK_CUSTOM_USER_BUTTON(7);
  436. #endif
  437. #if HAS_CUSTOM_USER_BUTTON(8)
  438. CHECK_CUSTOM_USER_BUTTON(8);
  439. #endif
  440. #if HAS_CUSTOM_USER_BUTTON(9)
  441. CHECK_CUSTOM_USER_BUTTON(9);
  442. #endif
  443. #if HAS_CUSTOM_USER_BUTTON(10)
  444. CHECK_CUSTOM_USER_BUTTON(10);
  445. #endif
  446. #if HAS_CUSTOM_USER_BUTTON(11)
  447. CHECK_CUSTOM_USER_BUTTON(11);
  448. #endif
  449. #if HAS_CUSTOM_USER_BUTTON(12)
  450. CHECK_CUSTOM_USER_BUTTON(12);
  451. #endif
  452. #if HAS_CUSTOM_USER_BUTTON(13)
  453. CHECK_CUSTOM_USER_BUTTON(13);
  454. #endif
  455. #if HAS_CUSTOM_USER_BUTTON(14)
  456. CHECK_CUSTOM_USER_BUTTON(14);
  457. #endif
  458. #if HAS_CUSTOM_USER_BUTTON(15)
  459. CHECK_CUSTOM_USER_BUTTON(15);
  460. #endif
  461. #if HAS_CUSTOM_USER_BUTTON(16)
  462. CHECK_CUSTOM_USER_BUTTON(16);
  463. #endif
  464. #if HAS_CUSTOM_USER_BUTTON(17)
  465. CHECK_CUSTOM_USER_BUTTON(17);
  466. #endif
  467. #if HAS_CUSTOM_USER_BUTTON(18)
  468. CHECK_CUSTOM_USER_BUTTON(18);
  469. #endif
  470. #if HAS_CUSTOM_USER_BUTTON(19)
  471. CHECK_CUSTOM_USER_BUTTON(19);
  472. #endif
  473. #if HAS_CUSTOM_USER_BUTTON(20)
  474. CHECK_CUSTOM_USER_BUTTON(20);
  475. #endif
  476. #if HAS_CUSTOM_USER_BUTTON(21)
  477. CHECK_CUSTOM_USER_BUTTON(21);
  478. #endif
  479. #if HAS_CUSTOM_USER_BUTTON(22)
  480. CHECK_CUSTOM_USER_BUTTON(22);
  481. #endif
  482. #if HAS_CUSTOM_USER_BUTTON(23)
  483. CHECK_CUSTOM_USER_BUTTON(23);
  484. #endif
  485. #if HAS_CUSTOM_USER_BUTTON(24)
  486. CHECK_CUSTOM_USER_BUTTON(24);
  487. #endif
  488. #if HAS_CUSTOM_USER_BUTTON(25)
  489. CHECK_CUSTOM_USER_BUTTON(25);
  490. #endif
  491. #endif
  492. TERN_(USE_CONTROLLER_FAN, controllerFan.update()); // Check if fan should be turned on to cool stepper drivers down
  493. TERN_(AUTO_POWER_CONTROL, powerManager.check());
  494. TERN_(HOTEND_IDLE_TIMEOUT, hotend_idle.check());
  495. #if ENABLED(EXTRUDER_RUNOUT_PREVENT)
  496. if (thermalManager.degHotend(active_extruder) > EXTRUDER_RUNOUT_MINTEMP
  497. && ELAPSED(ms, gcode.previous_move_ms + SEC_TO_MS(EXTRUDER_RUNOUT_SECONDS))
  498. && !planner.has_blocks_queued()
  499. ) {
  500. #if ENABLED(SWITCHING_EXTRUDER)
  501. bool oldstatus;
  502. switch (active_extruder) {
  503. default: oldstatus = E0_ENABLE_READ(); ENABLE_AXIS_E0(); break;
  504. #if E_STEPPERS > 1
  505. case 2: case 3: oldstatus = E1_ENABLE_READ(); ENABLE_AXIS_E1(); break;
  506. #if E_STEPPERS > 2
  507. case 4: case 5: oldstatus = E2_ENABLE_READ(); ENABLE_AXIS_E2(); break;
  508. #if E_STEPPERS > 3
  509. case 6: case 7: oldstatus = E3_ENABLE_READ(); ENABLE_AXIS_E3(); break;
  510. #endif // E_STEPPERS > 3
  511. #endif // E_STEPPERS > 2
  512. #endif // E_STEPPERS > 1
  513. }
  514. #else // !SWITCHING_EXTRUDER
  515. bool oldstatus;
  516. switch (active_extruder) {
  517. default:
  518. #define _CASE_EN(N) case N: oldstatus = E##N##_ENABLE_READ(); ENABLE_AXIS_E##N(); break;
  519. REPEAT(E_STEPPERS, _CASE_EN);
  520. }
  521. #endif
  522. const float olde = current_position.e;
  523. current_position.e += EXTRUDER_RUNOUT_EXTRUDE;
  524. line_to_current_position(MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED));
  525. current_position.e = olde;
  526. planner.set_e_position_mm(olde);
  527. planner.synchronize();
  528. #if ENABLED(SWITCHING_EXTRUDER)
  529. switch (active_extruder) {
  530. default: oldstatus = E0_ENABLE_WRITE(oldstatus); break;
  531. #if E_STEPPERS > 1
  532. case 2: case 3: oldstatus = E1_ENABLE_WRITE(oldstatus); break;
  533. #if E_STEPPERS > 2
  534. case 4: case 5: oldstatus = E2_ENABLE_WRITE(oldstatus); break;
  535. #endif // E_STEPPERS > 2
  536. #endif // E_STEPPERS > 1
  537. }
  538. #else // !SWITCHING_EXTRUDER
  539. switch (active_extruder) {
  540. #define _CASE_RESTORE(N) case N: E##N##_ENABLE_WRITE(oldstatus); break;
  541. REPEAT(E_STEPPERS, _CASE_RESTORE);
  542. }
  543. #endif // !SWITCHING_EXTRUDER
  544. gcode.reset_stepper_timeout(ms);
  545. }
  546. #endif // EXTRUDER_RUNOUT_PREVENT
  547. #if ENABLED(DUAL_X_CARRIAGE)
  548. // handle delayed move timeout
  549. if (delayed_move_time && ELAPSED(ms, delayed_move_time) && IsRunning()) {
  550. // travel moves have been received so enact them
  551. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  552. destination = current_position;
  553. prepare_line_to_destination();
  554. planner.synchronize();
  555. }
  556. #endif
  557. TERN_(TEMP_STAT_LEDS, handle_status_leds());
  558. TERN_(MONITOR_DRIVER_STATUS, monitor_tmc_drivers());
  559. TERN_(MONITOR_L6470_DRIVER_STATUS, L64xxManager.monitor_driver());
  560. // Limit check_axes_activity frequency to 10Hz
  561. static millis_t next_check_axes_ms = 0;
  562. if (ELAPSED(ms, next_check_axes_ms)) {
  563. planner.check_axes_activity();
  564. next_check_axes_ms = ms + 100UL;
  565. }
  566. #if PIN_EXISTS(FET_SAFETY)
  567. static millis_t FET_next;
  568. if (ELAPSED(ms, FET_next)) {
  569. FET_next = ms + FET_SAFETY_DELAY; // 2µs pulse every FET_SAFETY_DELAY mS
  570. OUT_WRITE(FET_SAFETY_PIN, !FET_SAFETY_INVERTED);
  571. DELAY_US(2);
  572. WRITE(FET_SAFETY_PIN, FET_SAFETY_INVERTED);
  573. }
  574. #endif
  575. }
  576. /**
  577. * Standard idle routine keeps the machine alive:
  578. * - Core Marlin activities
  579. * - Manage heaters (and Watchdog)
  580. * - Max7219 heartbeat, animation, etc.
  581. *
  582. * Only after setup() is complete:
  583. * - Handle filament runout sensors
  584. * - Run HAL idle tasks
  585. * - Handle Power-Loss Recovery
  586. * - Run StallGuard endstop checks
  587. * - Handle SD Card insert / remove
  588. * - Handle USB Flash Drive insert / remove
  589. * - Announce Host Keepalive state (if any)
  590. * - Update the Print Job Timer state
  591. * - Update the Beeper queue
  592. * - Read Buttons and Update the LCD
  593. * - Run i2c Position Encoders
  594. * - Auto-report Temperatures / SD Status
  595. * - Update the Průša MMU2
  596. * - Handle Joystick jogging
  597. */
  598. void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) {
  599. #if ENABLED(MARLIN_DEV_MODE)
  600. static uint16_t idle_depth = 0;
  601. if (++idle_depth > 5) SERIAL_ECHOLNPAIR("idle() call depth: ", idle_depth);
  602. #endif
  603. // Core Marlin activities
  604. manage_inactivity(TERN_(ADVANCED_PAUSE_FEATURE, no_stepper_sleep));
  605. // Manage Heaters (and Watchdog)
  606. thermalManager.manage_heater();
  607. // Max7219 heartbeat, animation, etc
  608. TERN_(MAX7219_DEBUG, max7219.idle_tasks());
  609. // Return if setup() isn't completed
  610. if (marlin_state == MF_INITIALIZING) goto IDLE_DONE;
  611. // Handle filament runout sensors
  612. TERN_(HAS_FILAMENT_SENSOR, runout.run());
  613. // Run HAL idle tasks
  614. TERN_(HAL_IDLETASK, HAL_idletask());
  615. // Check network connection
  616. TERN_(HAS_ETHERNET, ethernet.check());
  617. // Handle Power-Loss Recovery
  618. #if ENABLED(POWER_LOSS_RECOVERY) && PIN_EXISTS(POWER_LOSS)
  619. if (printJobOngoing()) recovery.outage();
  620. #endif
  621. // Run StallGuard endstop checks
  622. #if ENABLED(SPI_ENDSTOPS)
  623. if (endstops.tmc_spi_homing.any
  624. && TERN1(IMPROVE_HOMING_RELIABILITY, ELAPSED(millis(), sg_guard_period))
  625. ) LOOP_L_N(i, 4) // Read SGT 4 times per idle loop
  626. if (endstops.tmc_spi_homing_check()) break;
  627. #endif
  628. // Handle SD Card insert / remove
  629. TERN_(SDSUPPORT, card.manage_media());
  630. // Handle USB Flash Drive insert / remove
  631. TERN_(USB_FLASH_DRIVE_SUPPORT, Sd2Card::idle());
  632. // Announce Host Keepalive state (if any)
  633. TERN_(HOST_KEEPALIVE_FEATURE, gcode.host_keepalive());
  634. // Update the Print Job Timer state
  635. TERN_(PRINTCOUNTER, print_job_timer.tick());
  636. // Update the Beeper queue
  637. TERN_(USE_BEEPER, buzzer.tick());
  638. // Handle UI input / draw events
  639. TERN(DWIN_CREALITY_LCD, DWIN_Update(), ui.update());
  640. // Run i2c Position Encoders
  641. #if ENABLED(I2C_POSITION_ENCODERS)
  642. {
  643. static millis_t i2cpem_next_update_ms;
  644. if (planner.has_blocks_queued()) {
  645. const millis_t ms = millis();
  646. if (ELAPSED(ms, i2cpem_next_update_ms)) {
  647. I2CPEM.update();
  648. i2cpem_next_update_ms = ms + I2CPE_MIN_UPD_TIME_MS;
  649. }
  650. }
  651. }
  652. #endif
  653. // Auto-report Temperatures / SD Status
  654. #if HAS_AUTO_REPORTING
  655. if (!gcode.autoreport_paused) {
  656. TERN_(AUTO_REPORT_TEMPERATURES, thermalManager.auto_reporter.tick());
  657. TERN_(AUTO_REPORT_SD_STATUS, card.auto_reporter.tick());
  658. }
  659. #endif
  660. // Update the Průša MMU2
  661. TERN_(HAS_PRUSA_MMU2, mmu2.mmu_loop());
  662. // Handle Joystick jogging
  663. TERN_(POLL_JOG, joystick.inject_jog_moves());
  664. // Direct Stepping
  665. TERN_(DIRECT_STEPPING, page_manager.write_responses());
  666. // Update the LVGL interface
  667. TERN_(HAS_TFT_LVGL_UI, LV_TASK_HANDLER());
  668. IDLE_DONE:
  669. TERN_(MARLIN_DEV_MODE, idle_depth--);
  670. return;
  671. }
  672. /**
  673. * Kill all activity and lock the machine.
  674. * After this the machine will need to be reset.
  675. */
  676. void kill(PGM_P const lcd_error/*=nullptr*/, PGM_P const lcd_component/*=nullptr*/, const bool steppers_off/*=false*/) {
  677. thermalManager.disable_all_heaters();
  678. TERN_(HAS_CUTTER, cutter.kill()); // Full cutter shutdown including ISR control
  679. SERIAL_ERROR_MSG(STR_ERR_KILLED);
  680. #if HAS_DISPLAY
  681. ui.kill_screen(lcd_error ?: GET_TEXT(MSG_KILLED), lcd_component ?: NUL_STR);
  682. #else
  683. UNUSED(lcd_error);
  684. UNUSED(lcd_component);
  685. #endif
  686. #if HAS_TFT_LVGL_UI
  687. lv_draw_error_message(lcd_error);
  688. #endif
  689. #ifdef ACTION_ON_KILL
  690. host_action_kill();
  691. #endif
  692. minkill(steppers_off);
  693. }
  694. void minkill(const bool steppers_off/*=false*/) {
  695. // Wait a short time (allows messages to get out before shutting down.
  696. for (int i = 1000; i--;) DELAY_US(600);
  697. cli(); // Stop interrupts
  698. // Wait to ensure all interrupts stopped
  699. for (int i = 1000; i--;) DELAY_US(250);
  700. // Reiterate heaters off
  701. thermalManager.disable_all_heaters();
  702. TERN_(HAS_CUTTER, cutter.kill()); // Reiterate cutter shutdown
  703. // Power off all steppers (for M112) or just the E steppers
  704. steppers_off ? disable_all_steppers() : disable_e_steppers();
  705. TERN_(PSU_CONTROL, PSU_OFF());
  706. TERN_(HAS_SUICIDE, suicide());
  707. #if HAS_KILL
  708. // Wait for kill to be released
  709. while (kill_state()) watchdog_refresh();
  710. // Wait for kill to be pressed
  711. while (!kill_state()) watchdog_refresh();
  712. void (*resetFunc)() = 0; // Declare resetFunc() at address 0
  713. resetFunc(); // Jump to address 0
  714. #else
  715. for (;;) watchdog_refresh(); // Wait for reset
  716. #endif
  717. }
  718. /**
  719. * Turn off heaters and stop the print in progress
  720. * After a stop the machine may be resumed with M999
  721. */
  722. void stop() {
  723. thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
  724. print_job_timer.stop();
  725. #if ENABLED(PROBING_FANS_OFF)
  726. if (thermalManager.fans_paused) thermalManager.set_fans_paused(false); // put things back the way they were
  727. #endif
  728. if (IsRunning()) {
  729. SERIAL_ERROR_MSG(STR_ERR_STOPPED);
  730. LCD_MESSAGEPGM(MSG_STOPPED);
  731. safe_delay(350); // allow enough time for messages to get out before stopping
  732. marlin_state = MF_STOPPED;
  733. }
  734. }
  735. inline void tmc_standby_setup() {
  736. #if PIN_EXISTS(X_STDBY)
  737. SET_INPUT_PULLDOWN(X_STDBY_PIN);
  738. #endif
  739. #if PIN_EXISTS(X2_STDBY)
  740. SET_INPUT_PULLDOWN(X2_STDBY_PIN);
  741. #endif
  742. #if PIN_EXISTS(Y_STDBY)
  743. SET_INPUT_PULLDOWN(Y_STDBY_PIN);
  744. #endif
  745. #if PIN_EXISTS(Y2_STDBY)
  746. SET_INPUT_PULLDOWN(Y2_STDBY_PIN);
  747. #endif
  748. #if PIN_EXISTS(Z_STDBY)
  749. SET_INPUT_PULLDOWN(Z_STDBY_PIN);
  750. #endif
  751. #if PIN_EXISTS(Z2_STDBY)
  752. SET_INPUT_PULLDOWN(Z2_STDBY_PIN);
  753. #endif
  754. #if PIN_EXISTS(Z3_STDBY)
  755. SET_INPUT_PULLDOWN(Z3_STDBY_PIN);
  756. #endif
  757. #if PIN_EXISTS(Z4_STDBY)
  758. SET_INPUT_PULLDOWN(Z4_STDBY_PIN);
  759. #endif
  760. #if PIN_EXISTS(E0_STDBY)
  761. SET_INPUT_PULLDOWN(E0_STDBY_PIN);
  762. #endif
  763. #if PIN_EXISTS(E1_STDBY)
  764. SET_INPUT_PULLDOWN(E1_STDBY_PIN);
  765. #endif
  766. #if PIN_EXISTS(E2_STDBY)
  767. SET_INPUT_PULLDOWN(E2_STDBY_PIN);
  768. #endif
  769. #if PIN_EXISTS(E3_STDBY)
  770. SET_INPUT_PULLDOWN(E3_STDBY_PIN);
  771. #endif
  772. #if PIN_EXISTS(E4_STDBY)
  773. SET_INPUT_PULLDOWN(E4_STDBY_PIN);
  774. #endif
  775. #if PIN_EXISTS(E5_STDBY)
  776. SET_INPUT_PULLDOWN(E5_STDBY_PIN);
  777. #endif
  778. #if PIN_EXISTS(E6_STDBY)
  779. SET_INPUT_PULLDOWN(E6_STDBY_PIN);
  780. #endif
  781. #if PIN_EXISTS(E7_STDBY)
  782. SET_INPUT_PULLDOWN(E7_STDBY_PIN);
  783. #endif
  784. }
  785. /**
  786. * Marlin entry-point: Set up before the program loop
  787. * - Set up the kill pin, filament runout, power hold, custom user buttons
  788. * - Start the serial port
  789. * - Print startup messages and diagnostics
  790. * - Get EEPROM or default settings
  791. * - Initialize managers for:
  792. * • temperature
  793. * • planner
  794. * • watchdog
  795. * • stepper
  796. * • photo pin
  797. * • servos
  798. * • LCD controller
  799. * • Digipot I2C
  800. * • Z probe sled
  801. * • status LEDs
  802. * • Max7219
  803. */
  804. void setup() {
  805. #ifdef BOARD_PREINIT
  806. BOARD_PREINIT(); // Low-level init (before serial init)
  807. #endif
  808. tmc_standby_setup(); // TMC Low Power Standby pins must be set early or they're not usable
  809. #if ENABLED(MARLIN_DEV_MODE)
  810. auto log_current_ms = [&](PGM_P const msg) {
  811. SERIAL_ECHO_START();
  812. SERIAL_CHAR('['); SERIAL_ECHO(millis()); SERIAL_ECHOPGM("] ");
  813. SERIAL_ECHOLNPGM_P(msg);
  814. };
  815. #define SETUP_LOG(M) log_current_ms(PSTR(M))
  816. #else
  817. #define SETUP_LOG(...) NOOP
  818. #endif
  819. #define SETUP_RUN(C) do{ SETUP_LOG(STRINGIFY(C)); C; }while(0)
  820. MYSERIAL1.begin(BAUDRATE);
  821. millis_t serial_connect_timeout = millis() + 1000UL;
  822. while (!MYSERIAL1.connected() && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  823. #if HAS_MULTI_SERIAL && !HAS_ETHERNET
  824. MYSERIAL2.begin(BAUDRATE);
  825. serial_connect_timeout = millis() + 1000UL;
  826. while (!MYSERIAL2.connected() && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  827. #endif
  828. SERIAL_ECHOLNPGM("start");
  829. // Set up these pins early to prevent suicide
  830. #if HAS_KILL
  831. SETUP_LOG("KILL_PIN");
  832. #if KILL_PIN_STATE
  833. SET_INPUT_PULLDOWN(KILL_PIN);
  834. #else
  835. SET_INPUT_PULLUP(KILL_PIN);
  836. #endif
  837. #endif
  838. #if HAS_SUICIDE
  839. SETUP_LOG("SUICIDE_PIN");
  840. OUT_WRITE(SUICIDE_PIN, !SUICIDE_PIN_INVERTING);
  841. #endif
  842. #if EITHER(DISABLE_DEBUG, DISABLE_JTAG)
  843. // Disable any hardware debug to free up pins for IO
  844. #if ENABLED(DISABLE_DEBUG) && defined(JTAGSWD_DISABLE)
  845. JTAGSWD_DISABLE();
  846. #elif defined(JTAG_DISABLE)
  847. JTAG_DISABLE();
  848. #else
  849. #error "DISABLE_(DEBUG|JTAG) is not supported for the selected MCU/Board."
  850. #endif
  851. #endif
  852. #if BOTH(HAS_TFT_LVGL_UI, MKS_WIFI_MODULE)
  853. mks_esp_wifi_init();
  854. WIFISERIAL.begin(WIFI_BAUDRATE);
  855. serial_connect_timeout = millis() + 1000UL;
  856. while (/*!WIFISERIAL && */PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  857. #endif
  858. TERN_(DYNAMIC_VECTORTABLE, hook_cpu_exceptions()); // If supported, install Marlin exception handlers at runtime
  859. SETUP_RUN(HAL_init());
  860. // Init and disable SPI thermocouples; this is still needed
  861. #if TEMP_SENSOR_0_IS_MAX_TC
  862. OUT_WRITE(MAX6675_SS_PIN, HIGH); // Disable
  863. #endif
  864. #if TEMP_SENSOR_1_IS_MAX_TC
  865. OUT_WRITE(MAX6675_SS2_PIN, HIGH); // Disable
  866. #endif
  867. #if ENABLED(DUET_SMART_EFFECTOR) && PIN_EXISTS(SMART_EFFECTOR_MOD)
  868. OUT_WRITE(SMART_EFFECTOR_MOD_PIN, LOW); // Put Smart Effector into NORMAL mode
  869. #endif
  870. #if HAS_FILAMENT_SENSOR
  871. SETUP_RUN(runout.setup());
  872. #endif
  873. #if HAS_TMC220x
  874. SETUP_RUN(tmc_serial_begin());
  875. #endif
  876. #if ENABLED(PSU_CONTROL)
  877. SETUP_LOG("PSU_CONTROL");
  878. powersupply_on = ENABLED(PSU_DEFAULT_OFF);
  879. if (ENABLED(PSU_DEFAULT_OFF)) PSU_OFF(); else PSU_ON();
  880. #endif
  881. #if ENABLED(POWER_LOSS_RECOVERY)
  882. SETUP_RUN(recovery.setup());
  883. #endif
  884. #if HAS_L64XX
  885. SETUP_RUN(L64xxManager.init()); // Set up SPI, init drivers
  886. #endif
  887. #if HAS_STEPPER_RESET
  888. SETUP_RUN(disableStepperDrivers());
  889. #endif
  890. #if HAS_TMC_SPI
  891. #if DISABLED(TMC_USE_SW_SPI)
  892. SETUP_RUN(SPI.begin());
  893. #endif
  894. SETUP_RUN(tmc_init_cs_pins());
  895. #endif
  896. #ifdef BOARD_INIT
  897. SETUP_LOG("BOARD_INIT");
  898. BOARD_INIT();
  899. #endif
  900. SETUP_RUN(esp_wifi_init());
  901. // Check startup - does nothing if bootloader sets MCUSR to 0
  902. const byte mcu = HAL_get_reset_source();
  903. if (mcu & RST_POWER_ON) SERIAL_ECHOLNPGM(STR_POWERUP);
  904. if (mcu & RST_EXTERNAL) SERIAL_ECHOLNPGM(STR_EXTERNAL_RESET);
  905. if (mcu & RST_BROWN_OUT) SERIAL_ECHOLNPGM(STR_BROWNOUT_RESET);
  906. if (mcu & RST_WATCHDOG) SERIAL_ECHOLNPGM(STR_WATCHDOG_RESET);
  907. if (mcu & RST_SOFTWARE) SERIAL_ECHOLNPGM(STR_SOFTWARE_RESET);
  908. HAL_clear_reset_source();
  909. SERIAL_ECHOPGM_P(GET_TEXT(MSG_MARLIN));
  910. SERIAL_CHAR(' ');
  911. SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
  912. SERIAL_EOL();
  913. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  914. SERIAL_ECHO_MSG(
  915. " Last Updated: " STRING_DISTRIBUTION_DATE
  916. " | Author: " STRING_CONFIG_H_AUTHOR
  917. );
  918. #endif
  919. SERIAL_ECHO_MSG("Compiled: " __DATE__);
  920. SERIAL_ECHO_MSG(STR_FREE_MEMORY, freeMemory(), STR_PLANNER_BUFFER_BYTES, sizeof(block_t) * (BLOCK_BUFFER_SIZE));
  921. // Some HAL need precise delay adjustment
  922. calibrate_delay_loop();
  923. // Init buzzer pin(s)
  924. #if USE_BEEPER
  925. SETUP_RUN(buzzer.init());
  926. #endif
  927. // Set up LEDs early
  928. #if HAS_COLOR_LEDS
  929. SETUP_RUN(leds.setup());
  930. #endif
  931. #if ENABLED(NEOPIXEL2_SEPARATE)
  932. SETUP_RUN(leds2.setup());
  933. #endif
  934. #if ENABLED(USE_CONTROLLER_FAN) // Set up fan controller to initialize also the default configurations.
  935. SETUP_RUN(controllerFan.setup());
  936. #endif
  937. // UI must be initialized before EEPROM
  938. // (because EEPROM code calls the UI).
  939. #if ENABLED(DWIN_CREALITY_LCD)
  940. delay(800); // Required delay (since boot?)
  941. SERIAL_ECHOPGM("\nDWIN handshake ");
  942. if (DWIN_Handshake()) SERIAL_ECHOLNPGM("ok."); else SERIAL_ECHOLNPGM("error.");
  943. DWIN_Frame_SetDir(1); // Orientation 90°
  944. DWIN_UpdateLCD(); // Show bootscreen (first image)
  945. #else
  946. SETUP_RUN(ui.init());
  947. #if HAS_WIRED_LCD && ENABLED(SHOW_BOOTSCREEN)
  948. SETUP_RUN(ui.show_bootscreen());
  949. #endif
  950. SETUP_RUN(ui.reset_status()); // Load welcome message early. (Retained if no errors exist.)
  951. #endif
  952. #if ENABLED(PROBE_TARE)
  953. SETUP_RUN(probe.tare_init());
  954. #endif
  955. #if BOTH(SDSUPPORT, SDCARD_EEPROM_EMULATION)
  956. SETUP_RUN(card.mount()); // Mount media with settings before first_load
  957. #endif
  958. SETUP_RUN(settings.first_load()); // Load data from EEPROM if available (or use defaults)
  959. // This also updates variables in the planner, elsewhere
  960. #if HAS_ETHERNET
  961. SETUP_RUN(ethernet.init());
  962. #endif
  963. #if HAS_TOUCH_BUTTONS
  964. SETUP_RUN(touch.init());
  965. #endif
  966. TERN_(HAS_M206_COMMAND, current_position += home_offset); // Init current position based on home_offset
  967. sync_plan_position(); // Vital to init stepper/planner equivalent for current_position
  968. SETUP_RUN(thermalManager.init()); // Initialize temperature loop
  969. SETUP_RUN(print_job_timer.init()); // Initial setup of print job timer
  970. SETUP_RUN(endstops.init()); // Init endstops and pullups
  971. SETUP_RUN(stepper.init()); // Init stepper. This enables interrupts!
  972. #if HAS_SERVOS
  973. SETUP_RUN(servo_init());
  974. #endif
  975. #if HAS_Z_SERVO_PROBE
  976. SETUP_RUN(probe.servo_probe_init());
  977. #endif
  978. #if HAS_PHOTOGRAPH
  979. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  980. #endif
  981. #if HAS_CUTTER
  982. SETUP_RUN(cutter.init());
  983. #endif
  984. #if ENABLED(COOLANT_MIST)
  985. OUT_WRITE(COOLANT_MIST_PIN, COOLANT_MIST_INVERT); // Init Mist Coolant OFF
  986. #endif
  987. #if ENABLED(COOLANT_FLOOD)
  988. OUT_WRITE(COOLANT_FLOOD_PIN, COOLANT_FLOOD_INVERT); // Init Flood Coolant OFF
  989. #endif
  990. #if HAS_BED_PROBE
  991. SETUP_RUN(endstops.enable_z_probe(false));
  992. #endif
  993. #if HAS_STEPPER_RESET
  994. SETUP_RUN(enableStepperDrivers());
  995. #endif
  996. #if HAS_MOTOR_CURRENT_I2C
  997. SETUP_RUN(digipot_i2c.init());
  998. #endif
  999. #if ENABLED(HAS_MOTOR_CURRENT_DAC)
  1000. SETUP_RUN(stepper_dac.init());
  1001. #endif
  1002. #if EITHER(Z_PROBE_SLED, SOLENOID_PROBE) && HAS_SOLENOID_1
  1003. OUT_WRITE(SOL1_PIN, LOW); // OFF
  1004. #endif
  1005. #if HAS_HOME
  1006. SET_INPUT_PULLUP(HOME_PIN);
  1007. #endif
  1008. #if ENABLED(CUSTOM_USER_BUTTONS)
  1009. #define INIT_CUSTOM_USER_BUTTON_PIN(N) do{ SET_INPUT(BUTTON##N##_PIN); WRITE(BUTTON##N##_PIN, !BUTTON##N##_HIT_STATE); }while(0)
  1010. #if HAS_CUSTOM_USER_BUTTON(1)
  1011. INIT_CUSTOM_USER_BUTTON_PIN(1);
  1012. #endif
  1013. #if HAS_CUSTOM_USER_BUTTON(2)
  1014. INIT_CUSTOM_USER_BUTTON_PIN(2);
  1015. #endif
  1016. #if HAS_CUSTOM_USER_BUTTON(3)
  1017. INIT_CUSTOM_USER_BUTTON_PIN(3);
  1018. #endif
  1019. #if HAS_CUSTOM_USER_BUTTON(4)
  1020. INIT_CUSTOM_USER_BUTTON_PIN(4);
  1021. #endif
  1022. #if HAS_CUSTOM_USER_BUTTON(5)
  1023. INIT_CUSTOM_USER_BUTTON_PIN(5);
  1024. #endif
  1025. #if HAS_CUSTOM_USER_BUTTON(6)
  1026. INIT_CUSTOM_USER_BUTTON_PIN(6);
  1027. #endif
  1028. #if HAS_CUSTOM_USER_BUTTON(7)
  1029. INIT_CUSTOM_USER_BUTTON_PIN(7);
  1030. #endif
  1031. #if HAS_CUSTOM_USER_BUTTON(8)
  1032. INIT_CUSTOM_USER_BUTTON_PIN(8);
  1033. #endif
  1034. #if HAS_CUSTOM_USER_BUTTON(9)
  1035. INIT_CUSTOM_USER_BUTTON_PIN(9);
  1036. #endif
  1037. #if HAS_CUSTOM_USER_BUTTON(10)
  1038. INIT_CUSTOM_USER_BUTTON_PIN(10);
  1039. #endif
  1040. #if HAS_CUSTOM_USER_BUTTON(11)
  1041. INIT_CUSTOM_USER_BUTTON_PIN(11);
  1042. #endif
  1043. #if HAS_CUSTOM_USER_BUTTON(12)
  1044. INIT_CUSTOM_USER_BUTTON_PIN(12);
  1045. #endif
  1046. #if HAS_CUSTOM_USER_BUTTON(13)
  1047. INIT_CUSTOM_USER_BUTTON_PIN(13);
  1048. #endif
  1049. #if HAS_CUSTOM_USER_BUTTON(14)
  1050. INIT_CUSTOM_USER_BUTTON_PIN(14);
  1051. #endif
  1052. #if HAS_CUSTOM_USER_BUTTON(15)
  1053. INIT_CUSTOM_USER_BUTTON_PIN(15);
  1054. #endif
  1055. #if HAS_CUSTOM_USER_BUTTON(16)
  1056. INIT_CUSTOM_USER_BUTTON_PIN(16);
  1057. #endif
  1058. #if HAS_CUSTOM_USER_BUTTON(17)
  1059. INIT_CUSTOM_USER_BUTTON_PIN(17);
  1060. #endif
  1061. #if HAS_CUSTOM_USER_BUTTON(18)
  1062. INIT_CUSTOM_USER_BUTTON_PIN(18);
  1063. #endif
  1064. #if HAS_CUSTOM_USER_BUTTON(19)
  1065. INIT_CUSTOM_USER_BUTTON_PIN(19);
  1066. #endif
  1067. #if HAS_CUSTOM_USER_BUTTON(20)
  1068. INIT_CUSTOM_USER_BUTTON_PIN(20);
  1069. #endif
  1070. #if HAS_CUSTOM_USER_BUTTON(21)
  1071. INIT_CUSTOM_USER_BUTTON_PIN(21);
  1072. #endif
  1073. #if HAS_CUSTOM_USER_BUTTON(22)
  1074. INIT_CUSTOM_USER_BUTTON_PIN(22);
  1075. #endif
  1076. #if HAS_CUSTOM_USER_BUTTON(23)
  1077. INIT_CUSTOM_USER_BUTTON_PIN(23);
  1078. #endif
  1079. #if HAS_CUSTOM_USER_BUTTON(24)
  1080. INIT_CUSTOM_USER_BUTTON_PIN(24);
  1081. #endif
  1082. #if HAS_CUSTOM_USER_BUTTON(25)
  1083. INIT_CUSTOM_USER_BUTTON_PIN(25);
  1084. #endif
  1085. #endif
  1086. #if PIN_EXISTS(STAT_LED_RED)
  1087. OUT_WRITE(STAT_LED_RED_PIN, LOW); // OFF
  1088. #endif
  1089. #if PIN_EXISTS(STAT_LED_BLUE)
  1090. OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // OFF
  1091. #endif
  1092. #if ENABLED(CASE_LIGHT_ENABLE)
  1093. SETUP_RUN(caselight.init());
  1094. #endif
  1095. #if HAS_PRUSA_MMU1
  1096. SETUP_LOG("Prusa MMU1");
  1097. SET_OUTPUT(E_MUX0_PIN);
  1098. SET_OUTPUT(E_MUX1_PIN);
  1099. SET_OUTPUT(E_MUX2_PIN);
  1100. #endif
  1101. #if HAS_FANMUX
  1102. SETUP_RUN(fanmux_init());
  1103. #endif
  1104. #if ENABLED(MIXING_EXTRUDER)
  1105. SETUP_RUN(mixer.init());
  1106. #endif
  1107. #if ENABLED(BLTOUCH)
  1108. SETUP_RUN(bltouch.init(/*set_voltage=*/true));
  1109. #endif
  1110. #if ENABLED(I2C_POSITION_ENCODERS)
  1111. SETUP_RUN(I2CPEM.init());
  1112. #endif
  1113. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  1114. SETUP_LOG("i2c...");
  1115. i2c.onReceive(i2c_on_receive);
  1116. i2c.onRequest(i2c_on_request);
  1117. #endif
  1118. #if DO_SWITCH_EXTRUDER
  1119. SETUP_RUN(move_extruder_servo(0)); // Initialize extruder servo
  1120. #endif
  1121. #if ENABLED(SWITCHING_NOZZLE)
  1122. SETUP_LOG("SWITCHING_NOZZLE");
  1123. // Initialize nozzle servo(s)
  1124. #if SWITCHING_NOZZLE_TWO_SERVOS
  1125. lower_nozzle(0);
  1126. raise_nozzle(1);
  1127. #else
  1128. move_nozzle_servo(0);
  1129. #endif
  1130. #endif
  1131. #if ENABLED(MAGNETIC_PARKING_EXTRUDER)
  1132. SETUP_RUN(mpe_settings_init());
  1133. #endif
  1134. #if ENABLED(PARKING_EXTRUDER)
  1135. SETUP_RUN(pe_solenoid_init());
  1136. #endif
  1137. #if ENABLED(SWITCHING_TOOLHEAD)
  1138. SETUP_RUN(swt_init());
  1139. #endif
  1140. #if ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  1141. SETUP_RUN(est_init());
  1142. #endif
  1143. #if ENABLED(USE_WATCHDOG)
  1144. SETUP_RUN(watchdog_init()); // Reinit watchdog after HAL_get_reset_source call
  1145. #endif
  1146. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  1147. SETUP_RUN(closedloop.init());
  1148. #endif
  1149. #ifdef STARTUP_COMMANDS
  1150. SETUP_LOG("STARTUP_COMMANDS");
  1151. queue.inject_P(PSTR(STARTUP_COMMANDS));
  1152. #endif
  1153. #if ENABLED(HOST_PROMPT_SUPPORT)
  1154. SETUP_RUN(host_action_prompt_end());
  1155. #endif
  1156. #if HAS_TRINAMIC_CONFIG && DISABLED(PSU_DEFAULT_OFF)
  1157. SETUP_RUN(test_tmc_connection(true, true, true, true));
  1158. #endif
  1159. #if HAS_PRUSA_MMU2
  1160. SETUP_RUN(mmu2.init());
  1161. #endif
  1162. #if ENABLED(IIC_BL24CXX_EEPROM)
  1163. BL24CXX::init();
  1164. const uint8_t err = BL24CXX::check();
  1165. SERIAL_ECHO_TERNARY(err, "BL24CXX Check ", "failed", "succeeded", "!\n");
  1166. #endif
  1167. #if ENABLED(DWIN_CREALITY_LCD)
  1168. Encoder_Configuration();
  1169. HMI_Init();
  1170. HMI_StartFrame(true);
  1171. #endif
  1172. #if HAS_SERVICE_INTERVALS && DISABLED(DWIN_CREALITY_LCD)
  1173. ui.reset_status(true); // Show service messages or keep current status
  1174. #endif
  1175. #if ENABLED(MAX7219_DEBUG)
  1176. SETUP_RUN(max7219.init());
  1177. #endif
  1178. #if ENABLED(DIRECT_STEPPING)
  1179. SETUP_RUN(page_manager.init());
  1180. #endif
  1181. #if HAS_TFT_LVGL_UI
  1182. #if ENABLED(SDSUPPORT)
  1183. if (!card.isMounted()) SETUP_RUN(card.mount()); // Mount SD to load graphics and fonts
  1184. #endif
  1185. SETUP_RUN(tft_lvgl_init());
  1186. #endif
  1187. #if ENABLED(PASSWORD_ON_STARTUP)
  1188. SETUP_RUN(password.lock_machine()); // Will not proceed until correct password provided
  1189. #endif
  1190. #if BOTH(HAS_LCD_MENU, TOUCH_SCREEN_CALIBRATION) && EITHER(TFT_CLASSIC_UI, TFT_COLOR_UI)
  1191. ui.check_touch_calibration();
  1192. #endif
  1193. marlin_state = MF_RUNNING;
  1194. SETUP_LOG("setup() completed.");
  1195. }
  1196. /**
  1197. * The main Marlin program loop
  1198. *
  1199. * - Call idle() to handle all tasks between G-code commands
  1200. * Note that no G-codes from the queue can be executed during idle()
  1201. * but many G-codes can be called directly anytime like macros.
  1202. * - Check whether SD card auto-start is needed now.
  1203. * - Check whether SD print finishing is needed now.
  1204. * - Run one G-code command from the immediate or main command queue
  1205. * and open up one space. Commands in the main queue may come from sd
  1206. * card, host, or by direct injection. The queue will continue to fill
  1207. * as long as idle() or manage_inactivity() are being called.
  1208. */
  1209. void loop() {
  1210. do {
  1211. idle();
  1212. #if ENABLED(SDSUPPORT)
  1213. if (card.flag.abort_sd_printing) abortSDPrinting();
  1214. if (marlin_state == MF_SD_COMPLETE) finishSDPrinting();
  1215. #endif
  1216. queue.advance();
  1217. endstops.event_handler();
  1218. TERN_(HAS_TFT_LVGL_UI, printer_state_polling());
  1219. } while (ENABLED(__AVR__)); // Loop forever on slower (AVR) boards
  1220. }