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 45KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  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/mks_ui/tft_lvgl_configuration.h"
  60. #include "lcd/extui/mks_ui/draw_ui.h"
  61. #include "lcd/extui/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/e3v2/rotary_encoder.h"
  67. #endif
  68. #if ENABLED(EXTENSIBLE_UI)
  69. #include "lcd/extui/ui_api.h"
  70. #endif
  71. #if HAS_ETHERNET
  72. #include "feature/ethernet.h"
  73. #endif
  74. #if ENABLED(IIC_BL24CXX_EEPROM)
  75. #include "libs/BL24CXX.h"
  76. #endif
  77. #if ENABLED(DIRECT_STEPPING)
  78. #include "feature/direct_stepping.h"
  79. #endif
  80. #if ENABLED(HOST_ACTION_COMMANDS)
  81. #include "feature/host_actions.h"
  82. #endif
  83. #if USE_BEEPER
  84. #include "libs/buzzer.h"
  85. #endif
  86. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  87. #include "feature/closedloop.h"
  88. #endif
  89. #if HAS_MOTOR_CURRENT_I2C
  90. #include "feature/digipot/digipot.h"
  91. #endif
  92. #if ENABLED(MIXING_EXTRUDER)
  93. #include "feature/mixing.h"
  94. #endif
  95. #if ENABLED(MAX7219_DEBUG)
  96. #include "feature/max7219.h"
  97. #endif
  98. #if HAS_COLOR_LEDS
  99. #include "feature/leds/leds.h"
  100. #endif
  101. #if ENABLED(BLTOUCH)
  102. #include "feature/bltouch.h"
  103. #endif
  104. #if ENABLED(POLL_JOG)
  105. #include "feature/joystick.h"
  106. #endif
  107. #if HAS_SERVOS
  108. #include "module/servo.h"
  109. #endif
  110. #if ENABLED(HAS_MOTOR_CURRENT_DAC)
  111. #include "feature/dac/stepper_dac.h"
  112. #endif
  113. #if ENABLED(EXPERIMENTAL_I2CBUS)
  114. #include "feature/twibus.h"
  115. #endif
  116. #if ENABLED(I2C_POSITION_ENCODERS)
  117. #include "feature/encoder_i2c.h"
  118. #endif
  119. #if HAS_TRINAMIC_CONFIG && DISABLED(PSU_DEFAULT_OFF)
  120. #include "feature/tmc_util.h"
  121. #endif
  122. #if HAS_CUTTER
  123. #include "feature/spindle_laser.h"
  124. #endif
  125. #if ENABLED(SDSUPPORT)
  126. CardReader card;
  127. #endif
  128. #if ENABLED(G38_PROBE_TARGET)
  129. uint8_t G38_move; // = 0
  130. bool G38_did_trigger; // = false
  131. #endif
  132. #if ENABLED(DELTA)
  133. #include "module/delta.h"
  134. #elif IS_SCARA
  135. #include "module/scara.h"
  136. #endif
  137. #if HAS_LEVELING
  138. #include "feature/bedlevel/bedlevel.h"
  139. #endif
  140. #if ENABLED(GCODE_REPEAT_MARKERS)
  141. #include "feature/repeat.h"
  142. #endif
  143. #if ENABLED(POWER_LOSS_RECOVERY)
  144. #include "feature/powerloss.h"
  145. #endif
  146. #if ENABLED(CANCEL_OBJECTS)
  147. #include "feature/cancel_object.h"
  148. #endif
  149. #if HAS_FILAMENT_SENSOR
  150. #include "feature/runout.h"
  151. #endif
  152. #if EITHER(PROBE_TARE, HAS_Z_SERVO_PROBE)
  153. #include "module/probe.h"
  154. #endif
  155. #if ENABLED(HOTEND_IDLE_TIMEOUT)
  156. #include "feature/hotend_idle.h"
  157. #endif
  158. #if ENABLED(TEMP_STAT_LEDS)
  159. #include "feature/leds/tempstat.h"
  160. #endif
  161. #if ENABLED(CASE_LIGHT_ENABLE)
  162. #include "feature/caselight.h"
  163. #endif
  164. #if HAS_FANMUX
  165. #include "feature/fanmux.h"
  166. #endif
  167. #include "module/tool_change.h"
  168. #if ENABLED(USE_CONTROLLER_FAN)
  169. #include "feature/controllerfan.h"
  170. #endif
  171. #if HAS_PRUSA_MMU2
  172. #include "feature/mmu/mmu2.h"
  173. #endif
  174. #if HAS_L64XX
  175. #include "libs/L64XX/L64XX_Marlin.h"
  176. #endif
  177. #if ENABLED(PASSWORD_FEATURE)
  178. #include "feature/password/password.h"
  179. #endif
  180. #if ENABLED(DGUS_LCD_UI_MKS)
  181. #include "lcd/extui/dgus/DGUSScreenHandler.h"
  182. #endif
  183. #if HAS_DRIVER_SAFE_POWER_PROTECT
  184. #include "feature/stepper_driver_safety.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. #ifdef RUNTIME_ONLY_ANALOG_TO_DIGITAL
  222. static const pin_t sensitive_pins[] PROGMEM = { SENSITIVE_PINS };
  223. #else
  224. template <pin_t ...D>
  225. constexpr pin_t OnlyPins<-2, D...>::table[sizeof...(D)];
  226. #define sensitive_pins OnlyPins<SENSITIVE_PINS>::table
  227. #endif
  228. bool pin_is_protected(const pin_t pin) {
  229. LOOP_L_N(i, COUNT(sensitive_pins)) {
  230. pin_t sensitive_pin;
  231. memcpy_P(&sensitive_pin, &sensitive_pins[i], sizeof(pin_t));
  232. if (pin == sensitive_pin) return true;
  233. }
  234. return false;
  235. }
  236. #pragma GCC diagnostic pop
  237. void enable_e_steppers() {
  238. #define _ENA_E(N) ENABLE_AXIS_E##N();
  239. REPEAT(E_STEPPERS, _ENA_E)
  240. }
  241. void enable_all_steppers() {
  242. TERN_(AUTO_POWER_CONTROL, powerManager.power_on());
  243. ENABLE_AXIS_X();
  244. ENABLE_AXIS_Y();
  245. ENABLE_AXIS_Z();
  246. ENABLE_AXIS_I(); // Marlin 6-axis support by DerAndere (https://github.com/DerAndere1/Marlin/wiki)
  247. ENABLE_AXIS_J();
  248. ENABLE_AXIS_K();
  249. enable_e_steppers();
  250. TERN_(EXTENSIBLE_UI, ExtUI::onSteppersEnabled());
  251. }
  252. void disable_e_steppers() {
  253. #define _DIS_E(N) DISABLE_AXIS_E##N();
  254. REPEAT(E_STEPPERS, _DIS_E)
  255. }
  256. void disable_e_stepper(const uint8_t e) {
  257. #define _CASE_DIS_E(N) case N: DISABLE_AXIS_E##N(); break;
  258. switch (e) {
  259. REPEAT(E_STEPPERS, _CASE_DIS_E)
  260. }
  261. }
  262. void disable_all_steppers() {
  263. DISABLE_AXIS_X();
  264. DISABLE_AXIS_Y();
  265. DISABLE_AXIS_Z();
  266. DISABLE_AXIS_I();
  267. DISABLE_AXIS_J();
  268. DISABLE_AXIS_K();
  269. disable_e_steppers();
  270. TERN_(EXTENSIBLE_UI, ExtUI::onSteppersDisabled());
  271. }
  272. /**
  273. * A Print Job exists when the timer is running or SD is printing
  274. */
  275. bool printJobOngoing() { return print_job_timer.isRunning() || IS_SD_PRINTING(); }
  276. /**
  277. * Printing is active when a job is underway but not paused
  278. */
  279. bool printingIsActive() { return !did_pause_print && printJobOngoing(); }
  280. /**
  281. * Printing is paused according to SD or host indicators
  282. */
  283. bool printingIsPaused() {
  284. return did_pause_print || print_job_timer.isPaused() || IS_SD_PAUSED();
  285. }
  286. void startOrResumeJob() {
  287. if (!printingIsPaused()) {
  288. TERN_(GCODE_REPEAT_MARKERS, repeat.reset());
  289. TERN_(CANCEL_OBJECTS, cancelable.reset());
  290. TERN_(LCD_SHOW_E_TOTAL, e_move_accumulator = 0);
  291. #if BOTH(LCD_SET_PROGRESS_MANUALLY, USE_M73_REMAINING_TIME)
  292. ui.reset_remaining_time();
  293. #endif
  294. }
  295. print_job_timer.start();
  296. }
  297. #if ENABLED(SDSUPPORT)
  298. inline void abortSDPrinting() {
  299. IF_DISABLED(NO_SD_AUTOSTART, card.autofile_cancel());
  300. card.abortFilePrintNow(TERN_(SD_RESORT, true));
  301. queue.clear();
  302. quickstop_stepper();
  303. print_job_timer.abort();
  304. IF_DISABLED(SD_ABORT_NO_COOLDOWN, thermalManager.disable_all_heaters());
  305. TERN(HAS_CUTTER, cutter.kill(), thermalManager.zero_fan_speeds()); // Full cutter shutdown including ISR control
  306. wait_for_heatup = false;
  307. TERN_(POWER_LOSS_RECOVERY, recovery.purge());
  308. #ifdef EVENT_GCODE_SD_ABORT
  309. queue.inject_P(PSTR(EVENT_GCODE_SD_ABORT));
  310. #endif
  311. TERN_(PASSWORD_AFTER_SD_PRINT_ABORT, password.lock_machine());
  312. }
  313. inline void finishSDPrinting() {
  314. if (queue.enqueue_one_P(PSTR("M1001"))) { // Keep trying until it gets queued
  315. marlin_state = MF_RUNNING; // Signal to stop trying
  316. TERN_(PASSWORD_AFTER_SD_PRINT_END, password.lock_machine());
  317. TERN_(DGUS_LCD_UI_MKS, ScreenHandler.SDPrintingFinished());
  318. }
  319. }
  320. #endif // SDSUPPORT
  321. /**
  322. * Minimal management of Marlin's core activities:
  323. * - Keep the command buffer full
  324. * - Check for maximum inactive time between commands
  325. * - Check for maximum inactive time between stepper commands
  326. * - Check if CHDK_PIN needs to go LOW
  327. * - Check for KILL button held down
  328. * - Check for HOME button held down
  329. * - Check for CUSTOM USER button held down
  330. * - Check if cooling fan needs to be switched on
  331. * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
  332. * - Pulse FET_SAFETY_PIN if it exists
  333. */
  334. inline void manage_inactivity(const bool ignore_stepper_queue=false) {
  335. queue.get_available_commands();
  336. const millis_t ms = millis();
  337. // Prevent steppers timing-out in the middle of M600
  338. // unless PAUSE_PARK_NO_STEPPER_TIMEOUT is disabled
  339. const bool parked_or_ignoring = ignore_stepper_queue
  340. || TERN0(PAUSE_PARK_NO_STEPPER_TIMEOUT, did_pause_print);
  341. // Reset both the M18/M84 activity timeout and the M85 max 'kill' timeout
  342. if (parked_or_ignoring) gcode.reset_stepper_timeout(ms);
  343. if (gcode.stepper_max_timed_out(ms)) {
  344. SERIAL_ERROR_MSG(STR_KILL_INACTIVE_TIME, parser.command_ptr);
  345. kill();
  346. }
  347. // M18 / M84 : Handle steppers inactive time timeout
  348. if (gcode.stepper_inactive_time) {
  349. static bool already_shutdown_steppers; // = false
  350. // Any moves in the planner? Resets both the M18/M84
  351. // activity timeout and the M85 max 'kill' timeout
  352. if (planner.has_blocks_queued())
  353. gcode.reset_stepper_timeout(ms);
  354. else if (!parked_or_ignoring && gcode.stepper_inactive_timeout()) {
  355. if (!already_shutdown_steppers) {
  356. already_shutdown_steppers = true; // L6470 SPI will consume 99% of free time without this
  357. // Individual axes will be disabled if configured
  358. if (ENABLED(DISABLE_INACTIVE_X)) DISABLE_AXIS_X();
  359. if (ENABLED(DISABLE_INACTIVE_Y)) DISABLE_AXIS_Y();
  360. if (ENABLED(DISABLE_INACTIVE_Z)) DISABLE_AXIS_Z();
  361. if (ENABLED(DISABLE_INACTIVE_I)) DISABLE_AXIS_I();
  362. if (ENABLED(DISABLE_INACTIVE_J)) DISABLE_AXIS_J();
  363. if (ENABLED(DISABLE_INACTIVE_K)) DISABLE_AXIS_K();
  364. if (ENABLED(DISABLE_INACTIVE_E)) disable_e_steppers();
  365. TERN_(AUTO_BED_LEVELING_UBL, ubl.steppers_were_disabled());
  366. }
  367. }
  368. else
  369. already_shutdown_steppers = false;
  370. }
  371. #if ENABLED(PHOTO_GCODE) && PIN_EXISTS(CHDK)
  372. // Check if CHDK should be set to LOW (after M240 set it HIGH)
  373. extern millis_t chdk_timeout;
  374. if (chdk_timeout && ELAPSED(ms, chdk_timeout)) {
  375. chdk_timeout = 0;
  376. WRITE(CHDK_PIN, LOW);
  377. }
  378. #endif
  379. #if HAS_KILL
  380. // Check if the kill button was pressed and wait just in case it was an accidental
  381. // key kill key press
  382. // -------------------------------------------------------------------------------
  383. static int killCount = 0; // make the inactivity button a bit less responsive
  384. const int KILL_DELAY = 750;
  385. if (kill_state())
  386. killCount++;
  387. else if (killCount > 0)
  388. killCount--;
  389. // Exceeded threshold and we can confirm that it was not accidental
  390. // KILL the machine
  391. // ----------------------------------------------------------------
  392. if (killCount >= KILL_DELAY) {
  393. SERIAL_ERROR_MSG(STR_KILL_BUTTON);
  394. kill();
  395. }
  396. #endif
  397. #if HAS_FREEZE_PIN
  398. Stepper::frozen = !READ(FREEZE_PIN);
  399. #endif
  400. #if HAS_HOME
  401. // Handle a standalone HOME button
  402. constexpr millis_t HOME_DEBOUNCE_DELAY = 1000UL;
  403. static millis_t next_home_key_ms; // = 0
  404. if (!IS_SD_PRINTING() && !READ(HOME_PIN)) { // HOME_PIN goes LOW when pressed
  405. const millis_t ms = millis();
  406. if (ELAPSED(ms, next_home_key_ms)) {
  407. next_home_key_ms = ms + HOME_DEBOUNCE_DELAY;
  408. LCD_MESSAGEPGM(MSG_AUTO_HOME);
  409. queue.inject_P(G28_STR);
  410. }
  411. }
  412. #endif
  413. #if ENABLED(CUSTOM_USER_BUTTONS)
  414. // Handle a custom user button if defined
  415. const bool printer_not_busy = !printingIsActive();
  416. #define HAS_CUSTOM_USER_BUTTON(N) (PIN_EXISTS(BUTTON##N) && defined(BUTTON##N##_HIT_STATE) && defined(BUTTON##N##_GCODE) && defined(BUTTON##N##_DESC))
  417. #define CHECK_CUSTOM_USER_BUTTON(N) do{ \
  418. constexpr millis_t CUB_DEBOUNCE_DELAY_##N = 250UL; \
  419. static millis_t next_cub_ms_##N; \
  420. if (BUTTON##N##_HIT_STATE == READ(BUTTON##N##_PIN) \
  421. && (ENABLED(BUTTON##N##_WHEN_PRINTING) || printer_not_busy)) { \
  422. const millis_t ms = millis(); \
  423. if (ELAPSED(ms, next_cub_ms_##N)) { \
  424. next_cub_ms_##N = ms + CUB_DEBOUNCE_DELAY_##N; \
  425. if (strlen(BUTTON##N##_DESC)) \
  426. LCD_MESSAGEPGM_P(PSTR(BUTTON##N##_DESC)); \
  427. queue.inject_P(PSTR(BUTTON##N##_GCODE)); \
  428. } \
  429. } \
  430. }while(0)
  431. #if HAS_CUSTOM_USER_BUTTON(1)
  432. CHECK_CUSTOM_USER_BUTTON(1);
  433. #endif
  434. #if HAS_CUSTOM_USER_BUTTON(2)
  435. CHECK_CUSTOM_USER_BUTTON(2);
  436. #endif
  437. #if HAS_CUSTOM_USER_BUTTON(3)
  438. CHECK_CUSTOM_USER_BUTTON(3);
  439. #endif
  440. #if HAS_CUSTOM_USER_BUTTON(4)
  441. CHECK_CUSTOM_USER_BUTTON(4);
  442. #endif
  443. #if HAS_CUSTOM_USER_BUTTON(5)
  444. CHECK_CUSTOM_USER_BUTTON(5);
  445. #endif
  446. #if HAS_CUSTOM_USER_BUTTON(6)
  447. CHECK_CUSTOM_USER_BUTTON(6);
  448. #endif
  449. #if HAS_CUSTOM_USER_BUTTON(7)
  450. CHECK_CUSTOM_USER_BUTTON(7);
  451. #endif
  452. #if HAS_CUSTOM_USER_BUTTON(8)
  453. CHECK_CUSTOM_USER_BUTTON(8);
  454. #endif
  455. #if HAS_CUSTOM_USER_BUTTON(9)
  456. CHECK_CUSTOM_USER_BUTTON(9);
  457. #endif
  458. #if HAS_CUSTOM_USER_BUTTON(10)
  459. CHECK_CUSTOM_USER_BUTTON(10);
  460. #endif
  461. #if HAS_CUSTOM_USER_BUTTON(11)
  462. CHECK_CUSTOM_USER_BUTTON(11);
  463. #endif
  464. #if HAS_CUSTOM_USER_BUTTON(12)
  465. CHECK_CUSTOM_USER_BUTTON(12);
  466. #endif
  467. #if HAS_CUSTOM_USER_BUTTON(13)
  468. CHECK_CUSTOM_USER_BUTTON(13);
  469. #endif
  470. #if HAS_CUSTOM_USER_BUTTON(14)
  471. CHECK_CUSTOM_USER_BUTTON(14);
  472. #endif
  473. #if HAS_CUSTOM_USER_BUTTON(15)
  474. CHECK_CUSTOM_USER_BUTTON(15);
  475. #endif
  476. #if HAS_CUSTOM_USER_BUTTON(16)
  477. CHECK_CUSTOM_USER_BUTTON(16);
  478. #endif
  479. #if HAS_CUSTOM_USER_BUTTON(17)
  480. CHECK_CUSTOM_USER_BUTTON(17);
  481. #endif
  482. #if HAS_CUSTOM_USER_BUTTON(18)
  483. CHECK_CUSTOM_USER_BUTTON(18);
  484. #endif
  485. #if HAS_CUSTOM_USER_BUTTON(19)
  486. CHECK_CUSTOM_USER_BUTTON(19);
  487. #endif
  488. #if HAS_CUSTOM_USER_BUTTON(20)
  489. CHECK_CUSTOM_USER_BUTTON(20);
  490. #endif
  491. #if HAS_CUSTOM_USER_BUTTON(21)
  492. CHECK_CUSTOM_USER_BUTTON(21);
  493. #endif
  494. #if HAS_CUSTOM_USER_BUTTON(22)
  495. CHECK_CUSTOM_USER_BUTTON(22);
  496. #endif
  497. #if HAS_CUSTOM_USER_BUTTON(23)
  498. CHECK_CUSTOM_USER_BUTTON(23);
  499. #endif
  500. #if HAS_CUSTOM_USER_BUTTON(24)
  501. CHECK_CUSTOM_USER_BUTTON(24);
  502. #endif
  503. #if HAS_CUSTOM_USER_BUTTON(25)
  504. CHECK_CUSTOM_USER_BUTTON(25);
  505. #endif
  506. #endif
  507. TERN_(USE_CONTROLLER_FAN, controllerFan.update()); // Check if fan should be turned on to cool stepper drivers down
  508. TERN_(AUTO_POWER_CONTROL, powerManager.check());
  509. TERN_(HOTEND_IDLE_TIMEOUT, hotend_idle.check());
  510. #if ENABLED(EXTRUDER_RUNOUT_PREVENT)
  511. if (thermalManager.degHotend(active_extruder) > (EXTRUDER_RUNOUT_MINTEMP)
  512. && ELAPSED(ms, gcode.previous_move_ms + SEC_TO_MS(EXTRUDER_RUNOUT_SECONDS))
  513. && !planner.has_blocks_queued()
  514. ) {
  515. #if ENABLED(SWITCHING_EXTRUDER)
  516. bool oldstatus;
  517. switch (active_extruder) {
  518. default: oldstatus = E0_ENABLE_READ(); ENABLE_AXIS_E0(); break;
  519. #if E_STEPPERS > 1
  520. case 2: case 3: oldstatus = E1_ENABLE_READ(); ENABLE_AXIS_E1(); break;
  521. #if E_STEPPERS > 2
  522. case 4: case 5: oldstatus = E2_ENABLE_READ(); ENABLE_AXIS_E2(); break;
  523. #if E_STEPPERS > 3
  524. case 6: case 7: oldstatus = E3_ENABLE_READ(); ENABLE_AXIS_E3(); break;
  525. #endif // E_STEPPERS > 3
  526. #endif // E_STEPPERS > 2
  527. #endif // E_STEPPERS > 1
  528. }
  529. #else // !SWITCHING_EXTRUDER
  530. bool oldstatus;
  531. switch (active_extruder) {
  532. default:
  533. #define _CASE_EN(N) case N: oldstatus = E##N##_ENABLE_READ(); ENABLE_AXIS_E##N(); break;
  534. REPEAT(E_STEPPERS, _CASE_EN);
  535. }
  536. #endif
  537. const float olde = current_position.e;
  538. current_position.e += EXTRUDER_RUNOUT_EXTRUDE;
  539. line_to_current_position(MMM_TO_MMS(EXTRUDER_RUNOUT_SPEED));
  540. current_position.e = olde;
  541. planner.set_e_position_mm(olde);
  542. planner.synchronize();
  543. #if ENABLED(SWITCHING_EXTRUDER)
  544. switch (active_extruder) {
  545. default: oldstatus = E0_ENABLE_WRITE(oldstatus); break;
  546. #if E_STEPPERS > 1
  547. case 2: case 3: oldstatus = E1_ENABLE_WRITE(oldstatus); break;
  548. #if E_STEPPERS > 2
  549. case 4: case 5: oldstatus = E2_ENABLE_WRITE(oldstatus); break;
  550. #endif // E_STEPPERS > 2
  551. #endif // E_STEPPERS > 1
  552. }
  553. #else // !SWITCHING_EXTRUDER
  554. switch (active_extruder) {
  555. #define _CASE_RESTORE(N) case N: E##N##_ENABLE_WRITE(oldstatus); break;
  556. REPEAT(E_STEPPERS, _CASE_RESTORE);
  557. }
  558. #endif // !SWITCHING_EXTRUDER
  559. gcode.reset_stepper_timeout(ms);
  560. }
  561. #endif // EXTRUDER_RUNOUT_PREVENT
  562. #if ENABLED(DUAL_X_CARRIAGE)
  563. // handle delayed move timeout
  564. if (delayed_move_time && ELAPSED(ms, delayed_move_time) && IsRunning()) {
  565. // travel moves have been received so enact them
  566. delayed_move_time = 0xFFFFFFFFUL; // force moves to be done
  567. destination = current_position;
  568. prepare_line_to_destination();
  569. planner.synchronize();
  570. }
  571. #endif
  572. TERN_(TEMP_STAT_LEDS, handle_status_leds());
  573. TERN_(MONITOR_DRIVER_STATUS, monitor_tmc_drivers());
  574. TERN_(MONITOR_L6470_DRIVER_STATUS, L64xxManager.monitor_driver());
  575. // Limit check_axes_activity frequency to 10Hz
  576. static millis_t next_check_axes_ms = 0;
  577. if (ELAPSED(ms, next_check_axes_ms)) {
  578. planner.check_axes_activity();
  579. next_check_axes_ms = ms + 100UL;
  580. }
  581. #if PIN_EXISTS(FET_SAFETY)
  582. static millis_t FET_next;
  583. if (ELAPSED(ms, FET_next)) {
  584. FET_next = ms + FET_SAFETY_DELAY; // 2µs pulse every FET_SAFETY_DELAY mS
  585. OUT_WRITE(FET_SAFETY_PIN, !FET_SAFETY_INVERTED);
  586. DELAY_US(2);
  587. WRITE(FET_SAFETY_PIN, FET_SAFETY_INVERTED);
  588. }
  589. #endif
  590. }
  591. /**
  592. * Standard idle routine keeps the machine alive:
  593. * - Core Marlin activities
  594. * - Manage heaters (and Watchdog)
  595. * - Max7219 heartbeat, animation, etc.
  596. *
  597. * Only after setup() is complete:
  598. * - Handle filament runout sensors
  599. * - Run HAL idle tasks
  600. * - Handle Power-Loss Recovery
  601. * - Run StallGuard endstop checks
  602. * - Handle SD Card insert / remove
  603. * - Handle USB Flash Drive insert / remove
  604. * - Announce Host Keepalive state (if any)
  605. * - Update the Print Job Timer state
  606. * - Update the Beeper queue
  607. * - Read Buttons and Update the LCD
  608. * - Run i2c Position Encoders
  609. * - Auto-report Temperatures / SD Status
  610. * - Update the Průša MMU2
  611. * - Handle Joystick jogging
  612. */
  613. void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) {
  614. #if ENABLED(MARLIN_DEV_MODE)
  615. static uint16_t idle_depth = 0;
  616. if (++idle_depth > 5) SERIAL_ECHOLNPAIR("idle() call depth: ", idle_depth);
  617. #endif
  618. // Core Marlin activities
  619. manage_inactivity(TERN_(ADVANCED_PAUSE_FEATURE, no_stepper_sleep));
  620. // Manage Heaters (and Watchdog)
  621. thermalManager.manage_heater();
  622. // Max7219 heartbeat, animation, etc
  623. TERN_(MAX7219_DEBUG, max7219.idle_tasks());
  624. // Return if setup() isn't completed
  625. if (marlin_state == MF_INITIALIZING) goto IDLE_DONE;
  626. // TODO: Still causing errors
  627. (void)check_tool_sensor_stats(active_extruder, true);
  628. // Handle filament runout sensors
  629. TERN_(HAS_FILAMENT_SENSOR, runout.run());
  630. // Run HAL idle tasks
  631. TERN_(HAL_IDLETASK, HAL_idletask());
  632. // Check network connection
  633. TERN_(HAS_ETHERNET, ethernet.check());
  634. // Handle Power-Loss Recovery
  635. #if ENABLED(POWER_LOSS_RECOVERY) && PIN_EXISTS(POWER_LOSS)
  636. if (IS_SD_PRINTING()) recovery.outage();
  637. #endif
  638. // Run StallGuard endstop checks
  639. #if ENABLED(SPI_ENDSTOPS)
  640. if (endstops.tmc_spi_homing.any
  641. && TERN1(IMPROVE_HOMING_RELIABILITY, ELAPSED(millis(), sg_guard_period))
  642. ) LOOP_L_N(i, 4) // Read SGT 4 times per idle loop
  643. if (endstops.tmc_spi_homing_check()) break;
  644. #endif
  645. // Handle SD Card insert / remove
  646. TERN_(SDSUPPORT, card.manage_media());
  647. // Handle USB Flash Drive insert / remove
  648. TERN_(USB_FLASH_DRIVE_SUPPORT, card.diskIODriver()->idle());
  649. // Announce Host Keepalive state (if any)
  650. TERN_(HOST_KEEPALIVE_FEATURE, gcode.host_keepalive());
  651. // Update the Print Job Timer state
  652. TERN_(PRINTCOUNTER, print_job_timer.tick());
  653. // Update the Beeper queue
  654. TERN_(USE_BEEPER, buzzer.tick());
  655. // Handle UI input / draw events
  656. TERN(DWIN_CREALITY_LCD, DWIN_Update(), ui.update());
  657. // Run i2c Position Encoders
  658. #if ENABLED(I2C_POSITION_ENCODERS)
  659. {
  660. static millis_t i2cpem_next_update_ms;
  661. if (planner.has_blocks_queued()) {
  662. const millis_t ms = millis();
  663. if (ELAPSED(ms, i2cpem_next_update_ms)) {
  664. I2CPEM.update();
  665. i2cpem_next_update_ms = ms + I2CPE_MIN_UPD_TIME_MS;
  666. }
  667. }
  668. }
  669. #endif
  670. // Auto-report Temperatures / SD Status
  671. #if HAS_AUTO_REPORTING
  672. if (!gcode.autoreport_paused) {
  673. TERN_(AUTO_REPORT_TEMPERATURES, thermalManager.auto_reporter.tick());
  674. TERN_(AUTO_REPORT_SD_STATUS, card.auto_reporter.tick());
  675. TERN_(AUTO_REPORT_POSITION, position_auto_reporter.tick());
  676. }
  677. #endif
  678. // Update the Průša MMU2
  679. TERN_(HAS_PRUSA_MMU2, mmu2.mmu_loop());
  680. // Handle Joystick jogging
  681. TERN_(POLL_JOG, joystick.inject_jog_moves());
  682. // Direct Stepping
  683. TERN_(DIRECT_STEPPING, page_manager.write_responses());
  684. // Update the LVGL interface
  685. TERN_(HAS_TFT_LVGL_UI, LV_TASK_HANDLER());
  686. IDLE_DONE:
  687. TERN_(MARLIN_DEV_MODE, idle_depth--);
  688. return;
  689. }
  690. /**
  691. * Kill all activity and lock the machine.
  692. * After this the machine will need to be reset.
  693. */
  694. void kill(PGM_P const lcd_error/*=nullptr*/, PGM_P const lcd_component/*=nullptr*/, const bool steppers_off/*=false*/) {
  695. thermalManager.disable_all_heaters();
  696. TERN_(HAS_CUTTER, cutter.kill()); // Full cutter shutdown including ISR control
  697. // Echo the LCD message to serial for extra context
  698. if (lcd_error) { SERIAL_ECHO_START(); SERIAL_ECHOLNPGM_P(lcd_error); }
  699. #if HAS_DISPLAY
  700. ui.kill_screen(lcd_error ?: GET_TEXT(MSG_KILLED), lcd_component ?: NUL_STR);
  701. #else
  702. UNUSED(lcd_error); UNUSED(lcd_component);
  703. #endif
  704. TERN_(HAS_TFT_LVGL_UI, lv_draw_error_message(lcd_error));
  705. // "Error:Printer halted. kill() called!"
  706. SERIAL_ERROR_MSG(STR_ERR_KILLED);
  707. #ifdef ACTION_ON_KILL
  708. host_action_kill();
  709. #endif
  710. minkill(steppers_off);
  711. }
  712. void minkill(const bool steppers_off/*=false*/) {
  713. // Wait a short time (allows messages to get out before shutting down.
  714. for (int i = 1000; i--;) DELAY_US(600);
  715. cli(); // Stop interrupts
  716. // Wait to ensure all interrupts stopped
  717. for (int i = 1000; i--;) DELAY_US(250);
  718. // Reiterate heaters off
  719. thermalManager.disable_all_heaters();
  720. TERN_(HAS_CUTTER, cutter.kill()); // Reiterate cutter shutdown
  721. // Power off all steppers (for M112) or just the E steppers
  722. steppers_off ? disable_all_steppers() : disable_e_steppers();
  723. TERN_(PSU_CONTROL, PSU_OFF());
  724. TERN_(HAS_SUICIDE, suicide());
  725. #if EITHER(HAS_KILL, SOFT_RESET_ON_KILL)
  726. // Wait for both KILL and ENC to be released
  727. while (TERN0(HAS_KILL, !kill_state()) || TERN0(SOFT_RESET_ON_KILL, !ui.button_pressed()))
  728. watchdog_refresh();
  729. // Wait for either KILL or ENC press
  730. while (TERN1(HAS_KILL, kill_state()) && TERN1(SOFT_RESET_ON_KILL, ui.button_pressed()))
  731. watchdog_refresh();
  732. // Reboot the board
  733. HAL_reboot();
  734. #else
  735. for (;;) watchdog_refresh(); // Wait for RESET button or power-cycle
  736. #endif
  737. }
  738. /**
  739. * Turn off heaters and stop the print in progress
  740. * After a stop the machine may be resumed with M999
  741. */
  742. void stop() {
  743. thermalManager.disable_all_heaters(); // 'unpause' taken care of in here
  744. print_job_timer.stop();
  745. #if EITHER(PROBING_FANS_OFF, ADVANCED_PAUSE_FANS_PAUSE)
  746. thermalManager.set_fans_paused(false); // Un-pause fans for safety
  747. #endif
  748. if (!IsStopped()) {
  749. SERIAL_ERROR_MSG(STR_ERR_STOPPED);
  750. LCD_MESSAGEPGM(MSG_STOPPED);
  751. safe_delay(350); // allow enough time for messages to get out before stopping
  752. marlin_state = MF_STOPPED;
  753. }
  754. }
  755. inline void tmc_standby_setup() {
  756. #if PIN_EXISTS(X_STDBY)
  757. SET_INPUT_PULLDOWN(X_STDBY_PIN);
  758. #endif
  759. #if PIN_EXISTS(X2_STDBY)
  760. SET_INPUT_PULLDOWN(X2_STDBY_PIN);
  761. #endif
  762. #if PIN_EXISTS(Y_STDBY)
  763. SET_INPUT_PULLDOWN(Y_STDBY_PIN);
  764. #endif
  765. #if PIN_EXISTS(Y2_STDBY)
  766. SET_INPUT_PULLDOWN(Y2_STDBY_PIN);
  767. #endif
  768. #if PIN_EXISTS(Z_STDBY)
  769. SET_INPUT_PULLDOWN(Z_STDBY_PIN);
  770. #endif
  771. #if PIN_EXISTS(Z2_STDBY)
  772. SET_INPUT_PULLDOWN(Z2_STDBY_PIN);
  773. #endif
  774. #if PIN_EXISTS(Z3_STDBY)
  775. SET_INPUT_PULLDOWN(Z3_STDBY_PIN);
  776. #endif
  777. #if PIN_EXISTS(Z4_STDBY)
  778. SET_INPUT_PULLDOWN(Z4_STDBY_PIN);
  779. #endif
  780. #if PIN_EXISTS(I_STDBY)
  781. SET_INPUT_PULLDOWN(I_STDBY_PIN);
  782. #endif
  783. #if PIN_EXISTS(J_STDBY)
  784. SET_INPUT_PULLDOWN(J_STDBY_PIN);
  785. #endif
  786. #if PIN_EXISTS(K_STDBY)
  787. SET_INPUT_PULLDOWN(K_STDBY_PIN);
  788. #endif
  789. #if PIN_EXISTS(E0_STDBY)
  790. SET_INPUT_PULLDOWN(E0_STDBY_PIN);
  791. #endif
  792. #if PIN_EXISTS(E1_STDBY)
  793. SET_INPUT_PULLDOWN(E1_STDBY_PIN);
  794. #endif
  795. #if PIN_EXISTS(E2_STDBY)
  796. SET_INPUT_PULLDOWN(E2_STDBY_PIN);
  797. #endif
  798. #if PIN_EXISTS(E3_STDBY)
  799. SET_INPUT_PULLDOWN(E3_STDBY_PIN);
  800. #endif
  801. #if PIN_EXISTS(E4_STDBY)
  802. SET_INPUT_PULLDOWN(E4_STDBY_PIN);
  803. #endif
  804. #if PIN_EXISTS(E5_STDBY)
  805. SET_INPUT_PULLDOWN(E5_STDBY_PIN);
  806. #endif
  807. #if PIN_EXISTS(E6_STDBY)
  808. SET_INPUT_PULLDOWN(E6_STDBY_PIN);
  809. #endif
  810. #if PIN_EXISTS(E7_STDBY)
  811. SET_INPUT_PULLDOWN(E7_STDBY_PIN);
  812. #endif
  813. }
  814. /**
  815. * Marlin Firmware entry-point. Abandon Hope All Ye Who Enter Here.
  816. * Setup before the program loop:
  817. *
  818. * - Call any special pre-init set for the board
  819. * - Put TMC drivers into Low Power Standby mode
  820. * - Init the serial ports (so setup can be debugged)
  821. * - Set up the kill and suicide pins
  822. * - Prepare (disable) board JTAG and Debug ports
  823. * - Init serial for a connected MKS TFT with WiFi
  824. * - Install Marlin custom Exception Handlers, if set.
  825. * - Init Marlin's HAL interfaces (for SPI, i2c, etc.)
  826. * - Init some optional hardware and features:
  827. * • MAX Thermocouple pins
  828. * • Duet Smart Effector
  829. * • Filament Runout Sensor
  830. * • TMC220x Stepper Drivers (Serial)
  831. * • PSU control
  832. * • Power-loss Recovery
  833. * • L64XX Stepper Drivers (SPI)
  834. * • Stepper Driver Reset: DISABLE
  835. * • TMC Stepper Drivers (SPI)
  836. * • Run BOARD_INIT if defined
  837. * • ESP WiFi
  838. * - Get the Reset Reason and report it
  839. * - Print startup messages and diagnostics
  840. * - Calibrate the HAL DELAY for precise timing
  841. * - Init the buzzer, possibly a custom timer
  842. * - Init more optional hardware:
  843. * • Color LED illumination
  844. * • Neopixel illumination
  845. * • Controller Fan
  846. * • Creality DWIN LCD (show boot image)
  847. * • Tare the Probe if possible
  848. * - Mount the (most likely external) SD Card
  849. * - Load settings from EEPROM (or use defaults)
  850. * - Init the Ethernet Port
  851. * - Init Touch Buttons (for emulated DOGLCD)
  852. * - Adjust the (certainly wrong) current position by the home offset
  853. * - Init the Planner::position (steps) based on current (native) position
  854. * - Initialize more managers and peripherals:
  855. * • Temperatures
  856. * • Print Job Timer
  857. * • Endstops and Endstop Interrupts
  858. * • Stepper ISR - Kind of Important!
  859. * • Servos
  860. * • Servo-based Probe
  861. * • Photograph Pin
  862. * • Laser/Spindle tool Power / PWM
  863. * • Coolant Control
  864. * • Bed Probe
  865. * • Stepper Driver Reset: ENABLE
  866. * • Digipot I2C - Stepper driver current control
  867. * • Stepper DAC - Stepper driver current control
  868. * • Solenoid (probe, or for other use)
  869. * • Home Pin
  870. * • Custom User Buttons
  871. * • Red/Blue Status LEDs
  872. * • Case Light
  873. * • Prusa MMU filament changer
  874. * • Fan Multiplexer
  875. * • Mixing Extruder
  876. * • BLTouch Probe
  877. * • I2C Position Encoders
  878. * • Custom I2C Bus handlers
  879. * • Enhanced tools or extruders:
  880. * • Switching Extruder
  881. * • Switching Nozzle
  882. * • Parking Extruder
  883. * • Magnetic Parking Extruder
  884. * • Switching Toolhead
  885. * • Electromagnetic Switching Toolhead
  886. * • Watchdog Timer - Also Kind of Important!
  887. * • Closed Loop Controller
  888. * - Run Startup Commands, if defined
  889. * - Tell host to close Host Prompts
  890. * - Test Trinamic driver connections
  891. * - Init Prusa MMU2 filament changer
  892. * - Init and test BL24Cxx EEPROM
  893. * - Init Creality DWIN encoder, show faux progress bar
  894. * - Reset Status Message / Show Service Messages
  895. * - Init MAX7219 LED Matrix
  896. * - Init Direct Stepping (Klipper-style motion control)
  897. * - Init TFT LVGL UI (with 3D Graphics)
  898. * - Apply Password Lock - Hold for Authentication
  899. * - Open Touch Screen Calibration screen, if not calibrated
  900. * - Set Marlin to RUNNING State
  901. */
  902. void setup() {
  903. #ifdef BOARD_PREINIT
  904. BOARD_PREINIT(); // Low-level init (before serial init)
  905. #endif
  906. tmc_standby_setup(); // TMC Low Power Standby pins must be set early or they're not usable
  907. #if ENABLED(MARLIN_DEV_MODE)
  908. auto log_current_ms = [&](PGM_P const msg) {
  909. SERIAL_ECHO_START();
  910. SERIAL_CHAR('['); SERIAL_ECHO(millis()); SERIAL_ECHOPGM("] ");
  911. SERIAL_ECHOLNPGM_P(msg);
  912. };
  913. #define SETUP_LOG(M) log_current_ms(PSTR(M))
  914. #else
  915. #define SETUP_LOG(...) NOOP
  916. #endif
  917. #define SETUP_RUN(C) do{ SETUP_LOG(STRINGIFY(C)); C; }while(0)
  918. MYSERIAL1.begin(BAUDRATE);
  919. millis_t serial_connect_timeout = millis() + 1000UL;
  920. while (!MYSERIAL1.connected() && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  921. #if HAS_MULTI_SERIAL && !HAS_ETHERNET
  922. #ifndef BAUDRATE_2
  923. #define BAUDRATE_2 BAUDRATE
  924. #endif
  925. MYSERIAL2.begin(BAUDRATE_2);
  926. serial_connect_timeout = millis() + 1000UL;
  927. while (!MYSERIAL2.connected() && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  928. #ifdef SERIAL_PORT_3
  929. #ifndef BAUDRATE_3
  930. #define BAUDRATE_3 BAUDRATE
  931. #endif
  932. MYSERIAL3.begin(BAUDRATE_3);
  933. serial_connect_timeout = millis() + 1000UL;
  934. while (!MYSERIAL3.connected() && PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  935. #endif
  936. #endif
  937. SERIAL_ECHOLNPGM("start");
  938. // Set up these pins early to prevent suicide
  939. #if HAS_KILL
  940. SETUP_LOG("KILL_PIN");
  941. #if KILL_PIN_STATE
  942. SET_INPUT_PULLDOWN(KILL_PIN);
  943. #else
  944. SET_INPUT_PULLUP(KILL_PIN);
  945. #endif
  946. #endif
  947. #if HAS_FREEZE_PIN
  948. SET_INPUT_PULLUP(FREEZE_PIN);
  949. #endif
  950. #if HAS_SUICIDE
  951. SETUP_LOG("SUICIDE_PIN");
  952. OUT_WRITE(SUICIDE_PIN, !SUICIDE_PIN_INVERTING);
  953. #endif
  954. #if EITHER(DISABLE_DEBUG, DISABLE_JTAG)
  955. // Disable any hardware debug to free up pins for IO
  956. #if ENABLED(DISABLE_DEBUG) && defined(JTAGSWD_DISABLE)
  957. JTAGSWD_DISABLE();
  958. #elif defined(JTAG_DISABLE)
  959. JTAG_DISABLE();
  960. #else
  961. #error "DISABLE_(DEBUG|JTAG) is not supported for the selected MCU/Board."
  962. #endif
  963. #endif
  964. #if BOTH(HAS_TFT_LVGL_UI, MKS_WIFI_MODULE)
  965. mks_esp_wifi_init();
  966. WIFISERIAL.begin(WIFI_BAUDRATE);
  967. serial_connect_timeout = millis() + 1000UL;
  968. while (/*!WIFISERIAL && */PENDING(millis(), serial_connect_timeout)) { /*nada*/ }
  969. #endif
  970. TERN_(DYNAMIC_VECTORTABLE, hook_cpu_exceptions()); // If supported, install Marlin exception handlers at runtime
  971. SETUP_RUN(HAL_init());
  972. // Init and disable SPI thermocouples; this is still needed
  973. #if TEMP_SENSOR_0_IS_MAX_TC || (TEMP_SENSOR_REDUNDANT_IS_MAX_TC && TEMP_SENSOR_REDUNDANT_SOURCE == 0)
  974. OUT_WRITE(MAX6675_SS_PIN, HIGH); // Disable
  975. #endif
  976. #if TEMP_SENSOR_1_IS_MAX_TC || (TEMP_SENSOR_REDUNDANT_IS_MAX_TC && TEMP_SENSOR_REDUNDANT_SOURCE == 1)
  977. OUT_WRITE(MAX6675_SS2_PIN, HIGH); // Disable
  978. #endif
  979. #if ENABLED(DUET_SMART_EFFECTOR) && PIN_EXISTS(SMART_EFFECTOR_MOD)
  980. OUT_WRITE(SMART_EFFECTOR_MOD_PIN, LOW); // Put Smart Effector into NORMAL mode
  981. #endif
  982. #if HAS_FILAMENT_SENSOR
  983. SETUP_RUN(runout.setup());
  984. #endif
  985. #if HAS_TMC220x
  986. SETUP_RUN(tmc_serial_begin());
  987. #endif
  988. #if ENABLED(PSU_CONTROL)
  989. SETUP_LOG("PSU_CONTROL");
  990. powersupply_on = ENABLED(PSU_DEFAULT_OFF);
  991. if (ENABLED(PSU_DEFAULT_OFF)) PSU_OFF(); else PSU_ON();
  992. #endif
  993. #if ENABLED(POWER_LOSS_RECOVERY)
  994. SETUP_RUN(recovery.setup());
  995. #endif
  996. #if HAS_L64XX
  997. SETUP_RUN(L64xxManager.init()); // Set up SPI, init drivers
  998. #endif
  999. #if HAS_STEPPER_RESET
  1000. SETUP_RUN(disableStepperDrivers());
  1001. #endif
  1002. #if HAS_TMC_SPI
  1003. #if DISABLED(TMC_USE_SW_SPI)
  1004. SETUP_RUN(SPI.begin());
  1005. #endif
  1006. SETUP_RUN(tmc_init_cs_pins());
  1007. #endif
  1008. #ifdef BOARD_INIT
  1009. SETUP_LOG("BOARD_INIT");
  1010. BOARD_INIT();
  1011. #endif
  1012. SETUP_RUN(esp_wifi_init());
  1013. // Check startup - does nothing if bootloader sets MCUSR to 0
  1014. const byte mcu = HAL_get_reset_source();
  1015. if (mcu & RST_POWER_ON) SERIAL_ECHOLNPGM(STR_POWERUP);
  1016. if (mcu & RST_EXTERNAL) SERIAL_ECHOLNPGM(STR_EXTERNAL_RESET);
  1017. if (mcu & RST_BROWN_OUT) SERIAL_ECHOLNPGM(STR_BROWNOUT_RESET);
  1018. if (mcu & RST_WATCHDOG) SERIAL_ECHOLNPGM(STR_WATCHDOG_RESET);
  1019. if (mcu & RST_SOFTWARE) SERIAL_ECHOLNPGM(STR_SOFTWARE_RESET);
  1020. HAL_clear_reset_source();
  1021. SERIAL_ECHOPGM_P(GET_TEXT(MSG_MARLIN));
  1022. SERIAL_CHAR(' ');
  1023. SERIAL_ECHOLNPGM(SHORT_BUILD_VERSION);
  1024. SERIAL_EOL();
  1025. #if defined(STRING_DISTRIBUTION_DATE) && defined(STRING_CONFIG_H_AUTHOR)
  1026. SERIAL_ECHO_MSG(
  1027. " Last Updated: " STRING_DISTRIBUTION_DATE
  1028. " | Author: " STRING_CONFIG_H_AUTHOR
  1029. );
  1030. #endif
  1031. SERIAL_ECHO_MSG("Compiled: " __DATE__);
  1032. SERIAL_ECHO_MSG(STR_FREE_MEMORY, freeMemory(), STR_PLANNER_BUFFER_BYTES, sizeof(block_t) * (BLOCK_BUFFER_SIZE));
  1033. // Some HAL need precise delay adjustment
  1034. calibrate_delay_loop();
  1035. // Init buzzer pin(s)
  1036. #if USE_BEEPER
  1037. SETUP_RUN(buzzer.init());
  1038. #endif
  1039. // Set up LEDs early
  1040. #if HAS_COLOR_LEDS
  1041. SETUP_RUN(leds.setup());
  1042. #endif
  1043. #if ENABLED(NEOPIXEL2_SEPARATE)
  1044. SETUP_RUN(leds2.setup());
  1045. #endif
  1046. #if ENABLED(USE_CONTROLLER_FAN) // Set up fan controller to initialize also the default configurations.
  1047. SETUP_RUN(controllerFan.setup());
  1048. #endif
  1049. // UI must be initialized before EEPROM
  1050. // (because EEPROM code calls the UI).
  1051. #if ENABLED(DWIN_CREALITY_LCD)
  1052. delay(800); // Required delay (since boot?)
  1053. SERIAL_ECHOPGM("\nDWIN handshake ");
  1054. if (DWIN_Handshake()) SERIAL_ECHOLNPGM("ok."); else SERIAL_ECHOLNPGM("error.");
  1055. DWIN_Frame_SetDir(1); // Orientation 90°
  1056. DWIN_UpdateLCD(); // Show bootscreen (first image)
  1057. #else
  1058. SETUP_RUN(ui.init());
  1059. #if BOTH(HAS_WIRED_LCD, SHOW_BOOTSCREEN)
  1060. SETUP_RUN(ui.show_bootscreen());
  1061. const millis_t bootscreen_ms = millis();
  1062. #endif
  1063. SETUP_RUN(ui.reset_status()); // Load welcome message early. (Retained if no errors exist.)
  1064. #endif
  1065. #if PIN_EXISTS(SAFE_POWER)
  1066. #if HAS_DRIVER_SAFE_POWER_PROTECT
  1067. SETUP_RUN(stepper_driver_backward_check());
  1068. #else
  1069. SETUP_LOG("SAFE_POWER");
  1070. OUT_WRITE(SAFE_POWER_PIN, HIGH);
  1071. #endif
  1072. #endif
  1073. #if ENABLED(PROBE_TARE)
  1074. SETUP_RUN(probe.tare_init());
  1075. #endif
  1076. #if BOTH(SDSUPPORT, SDCARD_EEPROM_EMULATION)
  1077. SETUP_RUN(card.mount()); // Mount media with settings before first_load
  1078. #endif
  1079. SETUP_RUN(settings.first_load()); // Load data from EEPROM if available (or use defaults)
  1080. // This also updates variables in the planner, elsewhere
  1081. #if HAS_ETHERNET
  1082. SETUP_RUN(ethernet.init());
  1083. #endif
  1084. #if HAS_TOUCH_BUTTONS
  1085. SETUP_RUN(touch.init());
  1086. #endif
  1087. TERN_(HAS_M206_COMMAND, current_position += home_offset); // Init current position based on home_offset
  1088. sync_plan_position(); // Vital to init stepper/planner equivalent for current_position
  1089. SETUP_RUN(thermalManager.init()); // Initialize temperature loop
  1090. SETUP_RUN(print_job_timer.init()); // Initial setup of print job timer
  1091. SETUP_RUN(endstops.init()); // Init endstops and pullups
  1092. SETUP_RUN(stepper.init()); // Init stepper. This enables interrupts!
  1093. #if HAS_SERVOS
  1094. SETUP_RUN(servo_init());
  1095. #endif
  1096. #if HAS_Z_SERVO_PROBE
  1097. SETUP_RUN(probe.servo_probe_init());
  1098. #endif
  1099. #if HAS_PHOTOGRAPH
  1100. OUT_WRITE(PHOTOGRAPH_PIN, LOW);
  1101. #endif
  1102. #if HAS_CUTTER
  1103. SETUP_RUN(cutter.init());
  1104. #endif
  1105. #if ENABLED(COOLANT_MIST)
  1106. OUT_WRITE(COOLANT_MIST_PIN, COOLANT_MIST_INVERT); // Init Mist Coolant OFF
  1107. #endif
  1108. #if ENABLED(COOLANT_FLOOD)
  1109. OUT_WRITE(COOLANT_FLOOD_PIN, COOLANT_FLOOD_INVERT); // Init Flood Coolant OFF
  1110. #endif
  1111. #if HAS_BED_PROBE
  1112. SETUP_RUN(endstops.enable_z_probe(false));
  1113. #endif
  1114. #if HAS_STEPPER_RESET
  1115. SETUP_RUN(enableStepperDrivers());
  1116. #endif
  1117. #if HAS_MOTOR_CURRENT_I2C
  1118. SETUP_RUN(digipot_i2c.init());
  1119. #endif
  1120. #if ENABLED(HAS_MOTOR_CURRENT_DAC)
  1121. SETUP_RUN(stepper_dac.init());
  1122. #endif
  1123. #if EITHER(Z_PROBE_SLED, SOLENOID_PROBE) && HAS_SOLENOID_1
  1124. OUT_WRITE(SOL1_PIN, LOW); // OFF
  1125. #endif
  1126. #if HAS_HOME
  1127. SET_INPUT_PULLUP(HOME_PIN);
  1128. #endif
  1129. #if ENABLED(CUSTOM_USER_BUTTONS)
  1130. #define INIT_CUSTOM_USER_BUTTON_PIN(N) do{ SET_INPUT(BUTTON##N##_PIN); WRITE(BUTTON##N##_PIN, !BUTTON##N##_HIT_STATE); }while(0)
  1131. #if HAS_CUSTOM_USER_BUTTON(1)
  1132. INIT_CUSTOM_USER_BUTTON_PIN(1);
  1133. #endif
  1134. #if HAS_CUSTOM_USER_BUTTON(2)
  1135. INIT_CUSTOM_USER_BUTTON_PIN(2);
  1136. #endif
  1137. #if HAS_CUSTOM_USER_BUTTON(3)
  1138. INIT_CUSTOM_USER_BUTTON_PIN(3);
  1139. #endif
  1140. #if HAS_CUSTOM_USER_BUTTON(4)
  1141. INIT_CUSTOM_USER_BUTTON_PIN(4);
  1142. #endif
  1143. #if HAS_CUSTOM_USER_BUTTON(5)
  1144. INIT_CUSTOM_USER_BUTTON_PIN(5);
  1145. #endif
  1146. #if HAS_CUSTOM_USER_BUTTON(6)
  1147. INIT_CUSTOM_USER_BUTTON_PIN(6);
  1148. #endif
  1149. #if HAS_CUSTOM_USER_BUTTON(7)
  1150. INIT_CUSTOM_USER_BUTTON_PIN(7);
  1151. #endif
  1152. #if HAS_CUSTOM_USER_BUTTON(8)
  1153. INIT_CUSTOM_USER_BUTTON_PIN(8);
  1154. #endif
  1155. #if HAS_CUSTOM_USER_BUTTON(9)
  1156. INIT_CUSTOM_USER_BUTTON_PIN(9);
  1157. #endif
  1158. #if HAS_CUSTOM_USER_BUTTON(10)
  1159. INIT_CUSTOM_USER_BUTTON_PIN(10);
  1160. #endif
  1161. #if HAS_CUSTOM_USER_BUTTON(11)
  1162. INIT_CUSTOM_USER_BUTTON_PIN(11);
  1163. #endif
  1164. #if HAS_CUSTOM_USER_BUTTON(12)
  1165. INIT_CUSTOM_USER_BUTTON_PIN(12);
  1166. #endif
  1167. #if HAS_CUSTOM_USER_BUTTON(13)
  1168. INIT_CUSTOM_USER_BUTTON_PIN(13);
  1169. #endif
  1170. #if HAS_CUSTOM_USER_BUTTON(14)
  1171. INIT_CUSTOM_USER_BUTTON_PIN(14);
  1172. #endif
  1173. #if HAS_CUSTOM_USER_BUTTON(15)
  1174. INIT_CUSTOM_USER_BUTTON_PIN(15);
  1175. #endif
  1176. #if HAS_CUSTOM_USER_BUTTON(16)
  1177. INIT_CUSTOM_USER_BUTTON_PIN(16);
  1178. #endif
  1179. #if HAS_CUSTOM_USER_BUTTON(17)
  1180. INIT_CUSTOM_USER_BUTTON_PIN(17);
  1181. #endif
  1182. #if HAS_CUSTOM_USER_BUTTON(18)
  1183. INIT_CUSTOM_USER_BUTTON_PIN(18);
  1184. #endif
  1185. #if HAS_CUSTOM_USER_BUTTON(19)
  1186. INIT_CUSTOM_USER_BUTTON_PIN(19);
  1187. #endif
  1188. #if HAS_CUSTOM_USER_BUTTON(20)
  1189. INIT_CUSTOM_USER_BUTTON_PIN(20);
  1190. #endif
  1191. #if HAS_CUSTOM_USER_BUTTON(21)
  1192. INIT_CUSTOM_USER_BUTTON_PIN(21);
  1193. #endif
  1194. #if HAS_CUSTOM_USER_BUTTON(22)
  1195. INIT_CUSTOM_USER_BUTTON_PIN(22);
  1196. #endif
  1197. #if HAS_CUSTOM_USER_BUTTON(23)
  1198. INIT_CUSTOM_USER_BUTTON_PIN(23);
  1199. #endif
  1200. #if HAS_CUSTOM_USER_BUTTON(24)
  1201. INIT_CUSTOM_USER_BUTTON_PIN(24);
  1202. #endif
  1203. #if HAS_CUSTOM_USER_BUTTON(25)
  1204. INIT_CUSTOM_USER_BUTTON_PIN(25);
  1205. #endif
  1206. #endif
  1207. #if PIN_EXISTS(STAT_LED_RED)
  1208. OUT_WRITE(STAT_LED_RED_PIN, LOW); // OFF
  1209. #endif
  1210. #if PIN_EXISTS(STAT_LED_BLUE)
  1211. OUT_WRITE(STAT_LED_BLUE_PIN, LOW); // OFF
  1212. #endif
  1213. #if ENABLED(CASE_LIGHT_ENABLE)
  1214. SETUP_RUN(caselight.init());
  1215. #endif
  1216. #if HAS_PRUSA_MMU1
  1217. SETUP_RUN(mmu_init());
  1218. #endif
  1219. #if HAS_FANMUX
  1220. SETUP_RUN(fanmux_init());
  1221. #endif
  1222. #if ENABLED(MIXING_EXTRUDER)
  1223. SETUP_RUN(mixer.init());
  1224. #endif
  1225. #if ENABLED(BLTOUCH)
  1226. SETUP_RUN(bltouch.init(/*set_voltage=*/true));
  1227. #endif
  1228. #if ENABLED(I2C_POSITION_ENCODERS)
  1229. SETUP_RUN(I2CPEM.init());
  1230. #endif
  1231. #if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0
  1232. SETUP_LOG("i2c...");
  1233. i2c.onReceive(i2c_on_receive);
  1234. i2c.onRequest(i2c_on_request);
  1235. #endif
  1236. #if DO_SWITCH_EXTRUDER
  1237. SETUP_RUN(move_extruder_servo(0)); // Initialize extruder servo
  1238. #endif
  1239. #if ENABLED(SWITCHING_NOZZLE)
  1240. SETUP_LOG("SWITCHING_NOZZLE");
  1241. // Initialize nozzle servo(s)
  1242. #if SWITCHING_NOZZLE_TWO_SERVOS
  1243. lower_nozzle(0);
  1244. raise_nozzle(1);
  1245. #else
  1246. move_nozzle_servo(0);
  1247. #endif
  1248. #endif
  1249. #if ENABLED(PARKING_EXTRUDER)
  1250. SETUP_RUN(pe_solenoid_init());
  1251. #elif ENABLED(MAGNETIC_PARKING_EXTRUDER)
  1252. SETUP_RUN(mpe_settings_init());
  1253. #elif ENABLED(SWITCHING_TOOLHEAD)
  1254. SETUP_RUN(swt_init());
  1255. #elif ENABLED(ELECTROMAGNETIC_SWITCHING_TOOLHEAD)
  1256. SETUP_RUN(est_init());
  1257. #endif
  1258. #if ENABLED(USE_WATCHDOG)
  1259. SETUP_RUN(watchdog_init()); // Reinit watchdog after HAL_get_reset_source call
  1260. #endif
  1261. #if ENABLED(EXTERNAL_CLOSED_LOOP_CONTROLLER)
  1262. SETUP_RUN(closedloop.init());
  1263. #endif
  1264. #ifdef STARTUP_COMMANDS
  1265. SETUP_LOG("STARTUP_COMMANDS");
  1266. queue.inject_P(PSTR(STARTUP_COMMANDS));
  1267. #endif
  1268. #if ENABLED(HOST_PROMPT_SUPPORT)
  1269. SETUP_RUN(host_action_prompt_end());
  1270. #endif
  1271. #if HAS_TRINAMIC_CONFIG && DISABLED(PSU_DEFAULT_OFF)
  1272. SETUP_RUN(test_tmc_connection());
  1273. #endif
  1274. #if HAS_DRIVER_SAFE_POWER_PROTECT
  1275. SETUP_RUN(stepper_driver_backward_report());
  1276. #endif
  1277. #if HAS_PRUSA_MMU2
  1278. SETUP_RUN(mmu2.init());
  1279. #endif
  1280. #if ENABLED(IIC_BL24CXX_EEPROM)
  1281. BL24CXX::init();
  1282. const uint8_t err = BL24CXX::check();
  1283. SERIAL_ECHO_TERNARY(err, "BL24CXX Check ", "failed", "succeeded", "!\n");
  1284. #endif
  1285. #if ENABLED(DWIN_CREALITY_LCD)
  1286. Encoder_Configuration();
  1287. HMI_Init();
  1288. DWIN_JPG_CacheTo1(Language_English);
  1289. HMI_StartFrame(true);
  1290. DWIN_StatusChanged(GET_TEXT(WELCOME_MSG));
  1291. #endif
  1292. #if HAS_SERVICE_INTERVALS && DISABLED(DWIN_CREALITY_LCD)
  1293. ui.reset_status(true); // Show service messages or keep current status
  1294. #endif
  1295. #if ENABLED(MAX7219_DEBUG)
  1296. SETUP_RUN(max7219.init());
  1297. #endif
  1298. #if ENABLED(DIRECT_STEPPING)
  1299. SETUP_RUN(page_manager.init());
  1300. #endif
  1301. #if HAS_TFT_LVGL_UI
  1302. #if ENABLED(SDSUPPORT)
  1303. if (!card.isMounted()) SETUP_RUN(card.mount()); // Mount SD to load graphics and fonts
  1304. #endif
  1305. SETUP_RUN(tft_lvgl_init());
  1306. #endif
  1307. #if BOTH(HAS_WIRED_LCD, SHOW_BOOTSCREEN)
  1308. const millis_t elapsed = millis() - bootscreen_ms;
  1309. #if ENABLED(MARLIN_DEV_MODE)
  1310. SERIAL_ECHOLNPAIR("elapsed=", elapsed);
  1311. #endif
  1312. SETUP_RUN(ui.bootscreen_completion(elapsed));
  1313. #endif
  1314. #if ENABLED(PASSWORD_ON_STARTUP)
  1315. SETUP_RUN(password.lock_machine()); // Will not proceed until correct password provided
  1316. #endif
  1317. #if BOTH(HAS_LCD_MENU, TOUCH_SCREEN_CALIBRATION) && EITHER(TFT_CLASSIC_UI, TFT_COLOR_UI)
  1318. ui.check_touch_calibration();
  1319. #endif
  1320. marlin_state = MF_RUNNING;
  1321. SETUP_LOG("setup() completed.");
  1322. }
  1323. /**
  1324. * The main Marlin program loop
  1325. *
  1326. * - Call idle() to handle all tasks between G-code commands
  1327. * Note that no G-codes from the queue can be executed during idle()
  1328. * but many G-codes can be called directly anytime like macros.
  1329. * - Check whether SD card auto-start is needed now.
  1330. * - Check whether SD print finishing is needed now.
  1331. * - Run one G-code command from the immediate or main command queue
  1332. * and open up one space. Commands in the main queue may come from sd
  1333. * card, host, or by direct injection. The queue will continue to fill
  1334. * as long as idle() or manage_inactivity() are being called.
  1335. */
  1336. void loop() {
  1337. do {
  1338. idle();
  1339. #if ENABLED(SDSUPPORT)
  1340. if (card.flag.abort_sd_printing) abortSDPrinting();
  1341. if (marlin_state == MF_SD_COMPLETE) finishSDPrinting();
  1342. #endif
  1343. queue.advance();
  1344. endstops.event_handler();
  1345. TERN_(HAS_TFT_LVGL_UI, printer_state_polling());
  1346. } while (ENABLED(__AVR__)); // Loop forever on slower (AVR) boards
  1347. }