Skip to content

Commit

Permalink
Migrate code out of MarlinCore
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Jan 21, 2021
1 parent 00bc094 commit 3d4afb2
Show file tree
Hide file tree
Showing 31 changed files with 135 additions and 137 deletions.
118 changes: 23 additions & 95 deletions Marlin/src/MarlinCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <math.h>

#include "core/utility.h"

#include "module/motion.h"
#include "module/planner.h"
#include "module/endstops.h"
Expand All @@ -57,6 +58,7 @@
#include "gcode/parser.h"
#include "gcode/queue.h"

#include "feature/pause.h"
#include "sd/cardreader.h"

#include "lcd/marlinui.h"
Expand Down Expand Up @@ -139,7 +141,6 @@

#if ENABLED(EXPERIMENTAL_I2CBUS)
#include "feature/twibus.h"
TWIBus i2c;
#endif

#if ENABLED(I2C_POSITION_ENCODERS)
Expand Down Expand Up @@ -173,10 +174,6 @@
#include "feature/bedlevel/bedlevel.h"
#endif

#if BOTH(ADVANCED_PAUSE_FEATURE, PAUSE_PARK_NO_STEPPER_TIMEOUT)
#include "feature/pause.h"
#endif

#if ENABLED(GCODE_REPEAT_MARKERS)
#include "feature/repeat.h"
#endif
Expand Down Expand Up @@ -267,40 +264,12 @@ bool wait_for_heatup = true;

#endif

#if PIN_EXISTS(CHDK)
extern millis_t chdk_timeout;
#endif

#if ENABLED(I2C_POSITION_ENCODERS)
I2CPositionEncodersMgr I2CPEM;
#endif

/**
* ***************************************************************************
* ******************************** FUNCTIONS ********************************
* ***************************************************************************
*/

void setup_killpin() {
#if HAS_KILL
#if KILL_PIN_STATE
SET_INPUT_PULLDOWN(KILL_PIN);
#else
SET_INPUT_PULLUP(KILL_PIN);
#endif
#endif
}

void setup_powerhold() {
#if HAS_SUICIDE
OUT_WRITE(SUICIDE_PIN, !SUICIDE_PIN_INVERTING);
#endif
#if ENABLED(PSU_CONTROL)
powersupply_on = ENABLED(PSU_DEFAULT_OFF);
if (ENABLED(PSU_DEFAULT_OFF)) PSU_OFF(); else PSU_ON();
#endif
}

/**
* Stepper Reset (RigidBoard, et.al.)
*/
Expand All @@ -309,18 +278,6 @@ void setup_powerhold() {
void enableStepperDrivers() { SET_INPUT(STEPPER_RESET_PIN); } // Set to input, allowing pullups to pull the pin high
#endif

#if ENABLED(EXPERIMENTAL_I2CBUS) && I2C_SLAVE_ADDRESS > 0

void i2c_on_receive(int bytes) { // just echo all bytes received to serial
i2c.receive(bytes);
}

void i2c_on_request() { // just send dummy data for now
i2c.reply("Hello World!\n");
}

#endif

/**
* Sensitive pin test for M42, M226
*/
Expand All @@ -342,17 +299,6 @@ bool pin_is_protected(const pin_t pin) {

#pragma GCC diagnostic pop

void protected_pin_err() {
SERIAL_ERROR_MSG(STR_ERR_PROTECTED_PIN);
}

void quickstop_stepper() {
planner.quick_stop();
planner.synchronize();
set_current_from_steppers_for_axis(ALL_AXES);
sync_plan_position();
}

void enable_e_steppers() {
#define _ENA_E(N) ENABLE_AXIS_E##N();
REPEAT(E_STEPPERS, _ENA_E)
Expand Down Expand Up @@ -389,41 +335,6 @@ void disable_all_steppers() {
TERN_(EXTENSIBLE_UI, ExtUI::onSteppersDisabled());
}

#if ENABLED(G29_RETRY_AND_RECOVER)

void event_probe_failure() {
#ifdef ACTION_ON_G29_FAILURE
host_action(PSTR(ACTION_ON_G29_FAILURE));
#endif
#ifdef G29_FAILURE_COMMANDS
gcode.process_subcommands_now_P(PSTR(G29_FAILURE_COMMANDS));
#endif
#if ENABLED(G29_HALT_ON_FAILURE)
#ifdef ACTION_ON_CANCEL
host_action_cancel();
#endif
kill(GET_TEXT(MSG_LCD_PROBING_FAILED));
#endif
}

void event_probe_recover() {
TERN_(HOST_PROMPT_SUPPORT, host_prompt_do(PROMPT_INFO, PSTR("G29 Retrying"), DISMISS_STR));
#ifdef ACTION_ON_G29_RECOVER
host_action(PSTR(ACTION_ON_G29_RECOVER));
#endif
#ifdef G29_RECOVER_COMMANDS
gcode.process_subcommands_now_P(PSTR(G29_RECOVER_COMMANDS));
#endif
}

#endif

#if ENABLED(ADVANCED_PAUSE_FEATURE)
#include "feature/pause.h"
#else
constexpr bool did_pause_print = false;
#endif

/**
* A Print Job exists when the timer is running or SD printing
*/
Expand Down Expand Up @@ -511,8 +422,8 @@ inline void manage_inactivity(const bool ignore_stepper_queue=false) {

// Prevent steppers timing-out in the middle of M600
// unless PAUSE_PARK_NO_STEPPER_TIMEOUT is disabled
const bool parked_or_ignoring = ignore_stepper_queue ||
(BOTH(ADVANCED_PAUSE_FEATURE, PAUSE_PARK_NO_STEPPER_TIMEOUT) && did_pause_print);
const bool parked_or_ignoring = ignore_stepper_queue
|| TERN0(PAUSE_PARK_NO_STEPPER_TIMEOUT, did_pause_print);

// Reset both the M18/M84 activity timeout and the M85 max 'kill' timeout
if (parked_or_ignoring) gcode.reset_stepper_timeout(ms);
Expand Down Expand Up @@ -550,6 +461,7 @@ inline void manage_inactivity(const bool ignore_stepper_queue=false) {
}

#if PIN_EXISTS(CHDK) // Check if pin should be set to LOW (after M240 set it HIGH)
extern millis_t chdk_timeout;
if (chdk_timeout && ELAPSED(ms, chdk_timeout)) {
chdk_timeout = 0;
WRITE(CHDK_PIN, LOW);
Expand Down Expand Up @@ -1038,13 +950,29 @@ void setup() {
SETUP_RUN(recovery.setup());
#endif

SETUP_RUN(setup_killpin());
#if HAS_KILL
SETUP_LOG("KILL_PIN");
#if KILL_PIN_STATE
SET_INPUT_PULLDOWN(KILL_PIN);
#else
SET_INPUT_PULLUP(KILL_PIN);
#endif
#endif

#if HAS_TMC220x
SETUP_RUN(tmc_serial_begin());
#endif

SETUP_RUN(setup_powerhold());
#if HAS_SUICIDE
SETUP_LOG("SUICIDE_PIN")
OUT_WRITE(SUICIDE_PIN, !SUICIDE_PIN_INVERTING);
#endif

#if ENABLED(PSU_CONTROL)
SETUP_LOG("PSU_CONTROL");
powersupply_on = ENABLED(PSU_DEFAULT_OFF);
if (ENABLED(PSU_DEFAULT_OFF)) PSU_OFF(); else PSU_ON();
#endif

#if HAS_STEPPER_RESET
SETUP_RUN(disableStepperDrivers());
Expand Down
13 changes: 0 additions & 13 deletions Marlin/src/MarlinCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ void stop();
void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep=false));
inline void idle_no_sleep() { idle(TERN_(ADVANCED_PAUSE_FEATURE, true)); }

#if ENABLED(EXPERIMENTAL_I2CBUS)
#include "feature/twibus.h"
extern TWIBus i2c;
#endif

#if ENABLED(G38_PROBE_TARGET)
extern uint8_t G38_move; // Flag to tell the ISR that G38 is in progress, and the type
extern bool G38_did_trigger; // Flag from the ISR to indicate the endstop changed
Expand All @@ -59,8 +54,6 @@ void disable_all_steppers();
void kill(PGM_P const lcd_error=nullptr, PGM_P const lcd_component=nullptr, const bool steppers_off=false);
void minkill(const bool steppers_off=false);

void quickstop_stepper();

// Global State of the firmware
enum MarlinState : uint8_t {
MF_INITIALIZING = 0,
Expand Down Expand Up @@ -103,7 +96,6 @@ extern bool wait_for_heatup;
#endif

bool pin_is_protected(const pin_t pin);
void protected_pin_err();

#if HAS_SUICIDE
inline void suicide() { OUT_WRITE(SUICIDE_PIN, SUICIDE_PIN_INVERTING); }
Expand All @@ -116,11 +108,6 @@ void protected_pin_err();
inline bool kill_state() { return READ(KILL_PIN) == KILL_PIN_STATE; }
#endif

#if ENABLED(G29_RETRY_AND_RECOVER)
void event_probe_recover();
void event_probe_failure();
#endif

extern const char NUL_STR[], M112_KILL_STR[], G28_STR[], M21_STR[], M23_STR[], M24_STR[],
SP_A_STR[], SP_B_STR[], SP_C_STR[],
SP_P_STR[], SP_T_STR[], SP_X_STR[], SP_Y_STR[], SP_Z_STR[], SP_E_STR[],
Expand Down
1 change: 0 additions & 1 deletion Marlin/src/feature/e_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

// External references
extern bool wait_for_user, wait_for_heatup;
void quickstop_stepper();

class EmergencyParser {

Expand Down
2 changes: 2 additions & 0 deletions Marlin/src/feature/encoder_i2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

#include <Wire.h>

I2CPositionEncodersMgr I2CPEM;

void I2CPositionEncoder::init(const uint8_t address, const AxisEnum axis) {
encoderAxis = axis;
i2cAddress = address;
Expand Down
6 changes: 5 additions & 1 deletion Marlin/src/feature/pause.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,8 @@ bool unload_filament(const float &unload_length, const bool show_lcd=false, cons
#endif
);

#endif // ADVANCED_PAUSE_FEATURE
#else // !ADVANCED_PAUSE_FEATURE

constexpr uint8_t did_pause_print = 0;

#endif // !ADVANCED_PAUSE_FEATURE
3 changes: 2 additions & 1 deletion Marlin/src/feature/runout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ bool FilamentMonitorBase::enabled = true,
// Filament Runout event handler
//
#include "../MarlinCore.h"
#include "../feature/pause.h"
#include "../gcode/queue.h"

#if ENABLED(HOST_ACTION_COMMANDS)
Expand All @@ -71,7 +72,7 @@ bool FilamentMonitorBase::enabled = true,

void event_filament_runout() {

if (TERN0(ADVANCED_PAUSE_FEATURE, did_pause_print)) return; // Action already in progress. Purge triggered repeated runout.
if (did_pause_print) return; // Action already in progress. Purge triggered repeated runout.

#if ENABLED(TOOLCHANGE_MIGRATION_FEATURE)
if (migration.in_progress) {
Expand Down
8 changes: 2 additions & 6 deletions Marlin/src/feature/runout.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ class TFilamentMonitor : public FilamentMonitorBase {

// Give the response a chance to update its counter.
static inline void run() {
if ( enabled && !filament_ran_out
&& (printingIsActive() || TERN0(ADVANCED_PAUSE_FEATURE, did_pause_print))
) {
if (enabled && !filament_ran_out && (printingIsActive() || did_pause_print)) {
TERN_(HAS_FILAMENT_RUNOUT_DISTANCE, cli()); // Prevent RunoutResponseDelayed::block_completed from accumulating here
response.run();
sensor.run();
Expand Down Expand Up @@ -343,9 +341,7 @@ class FilamentSensorBase {
}

static inline void block_completed(const block_t* const b) {
if (b->steps.x || b->steps.y || b->steps.z
|| TERN0(ADVANCED_PAUSE_FEATURE, did_pause_print) // Allow pause purge move to re-trigger runout state
) {
if (b->steps.x || b->steps.y || b->steps.z || did_pause_print) { // Allow pause purge move to re-trigger runout state
// Only trigger on extrusion with XYZ movement to allow filament change and retract/recover.
const uint8_t e = b->extruder;
const int32_t steps = b->steps.e;
Expand Down
10 changes: 10 additions & 0 deletions Marlin/src/feature/twibus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

#include <Wire.h>

TWIBus i2c;

TWIBus::TWIBus() {
#if I2C_SLAVE_ADDRESS == 0
Wire.begin(); // No address joins the BUS as the master
Expand Down Expand Up @@ -155,6 +157,14 @@ void TWIBus::flush() {
reset();
}

void i2c_on_receive(int bytes) { // just echo all bytes received to serial
i2c.receive(bytes);
}

void i2c_on_request() { // just send dummy data for now
i2c.reply("Hello World!\n");
}

#endif

#if ENABLED(DEBUG_TWIBUS)
Expand Down
13 changes: 13 additions & 0 deletions Marlin/src/feature/twibus.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@
typedef void (*twiReceiveFunc_t)(int bytes);
typedef void (*twiRequestFunc_t)();

/**
* For a light i2c protocol that runs on two boards running Marlin see:
* See https://github.com/MarlinFirmware/Marlin/issues/4776#issuecomment-246262879
*/
#if I2C_SLAVE_ADDRESS > 0

void i2c_on_receive(int bytes); // Demo i2c onReceive handler
void i2c_on_request(); // Demo i2c onRequest handler

#endif

#define TWIBUS_BUFFER_SIZE 32

/**
Expand Down Expand Up @@ -238,3 +249,5 @@ class TWIBus {
static inline void debug(const char[], uint8_t) {}
#endif
};

extern TWIBus i2c;
3 changes: 2 additions & 1 deletion Marlin/src/gcode/control/M108_M112_M410.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
#if DISABLED(EMERGENCY_PARSER)

#include "../gcode.h"
#include "../../MarlinCore.h" // for wait_for_heatup, kill, quickstop_stepper
#include "../../MarlinCore.h" // for wait_for_heatup, kill
#include "../../module/motion.h" // for quickstop_stepper

/**
* M108: Stop the waiting for heaters in M109, M190, M303. Does not affect the target temperature.
Expand Down
2 changes: 2 additions & 0 deletions Marlin/src/gcode/control/M226.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "../../MarlinCore.h" // for pin_is_protected and idle()
#include "../../module/stepper.h"

void protected_pin_err();

/**
* M226: Wait until the specified pin reaches the state required (M226 P<pin> S<state>)
*/
Expand Down
4 changes: 4 additions & 0 deletions Marlin/src/gcode/control/M42.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
#include "../../module/temperature.h"
#endif

void protected_pin_err() {
SERIAL_ERROR_MSG(STR_ERR_PROTECTED_PIN);
}

/**
* M42: Change pin status via GCode
*
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/gcode/feature/i2c/M260_M261.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include "../../gcode.h"

#include "../../../MarlinCore.h" // for i2c
#include "../../../feature/twibus.h"

/**
* M260: Send data to a I2C slave device
Expand Down
Loading

0 comments on commit 3d4afb2

Please sign in to comment.