Skip to content

Commit

Permalink
Fix compilation with SPINDLE_FEATURE
Browse files Browse the repository at this point in the history
  • Loading branch information
DerAndere1 committed Oct 12, 2024
1 parent 1716258 commit 4928e7f
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 30 deletions.
2 changes: 2 additions & 0 deletions Marlin/src/HAL/AVR/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
#include "../ServoTimers.h" // Needed to check timer availability (_useTimer3)
#if SPINDLE_LASER_PWM_PIN == 4 || WITHIN(SPINDLE_LASER_PWM_PIN, 11, 13)
#error "Counter/Timer for SPINDLE_LASER_PWM_PIN is used by a system interrupt."
#elif LASER_PWM_PIN == 4 || WITHIN(LASER_PWM_PIN, 11, 13)
#error "Counter/Timer for LASER_PWM_PIN is used by a system interrupt."
#elif NUM_SERVOS > 0 && defined(_useTimer3) && (WITHIN(SPINDLE_LASER_PWM_PIN, 2, 3) || SPINDLE_LASER_PWM_PIN == 5)
#error "Counter/Timer for SPINDLE_LASER_PWM_PIN is used by the servo system."
#endif
Expand Down
4 changes: 4 additions & 0 deletions Marlin/src/HAL/LINUX/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
#error "SPINDLE_LASER_PWM_PIN must use SERVO0, SERVO1 or SERVO3 connector"
#endif

#if ENABLED(LASER_USE_PWM) && !(LASER_PWM_PIN == 4 || LASER_PWM_PIN == 6 || LASER_PWM_PIN == 11)
#error "LASER_PWM_PIN must use SERVO0, SERVO1 or SERVO3 connector"
#endif

#if ENABLED(FAST_PWM_FAN) || SPINDLE_LASER_FREQUENCY
#error "Features requiring Hardware PWM (FAST_PWM_FAN, SPINDLE_LASER_FREQUENCY) are not yet supported for HAL/LINUX."
#endif
Expand Down
4 changes: 4 additions & 0 deletions Marlin/src/HAL/NATIVE_SIM/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
#error "SPINDLE_LASER_PWM_PIN must use SERVO0, SERVO1 or SERVO3 connector"
#endif

#if ENABLED(LASER_USE_PWM) && !(LASER_PWM_PIN == 4 || LASER_PWM_PIN == 6 || LASER_PWM_PIN == 11)
#error "LASER_PWM_PIN must use SERVO0, SERVO1 or SERVO3 connector"
#endif

#if ENABLED(FAST_PWM_FAN) || SPINDLE_LASER_FREQUENCY
#error "Features requiring Hardware PWM (FAST_PWM_FAN, SPINDLE_LASER_FREQUENCY) are not yet supported for HAL/LINUX."
#endif
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/core/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ enum AxisEnum : uint8_t {
enum ToolTypeEnum : uint8_t {
TYPE_EXTRUDER,
TYPE_LASER,
TYPE_SPINLDE
TYPE_SPINDLE
};

//
Expand Down
59 changes: 38 additions & 21 deletions Marlin/src/feature/spindle_laser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#endif

SpindleLaser cutter;
uint8_t SpindleLaser::active_tool_type; // Tool type: 0 for extruder, 1 for spindle, 2 for laser
ToolTypeEnum SpindleLaser::active_tool_type; // Tool type: 0 for extruder, 1 for spindle, 2 for laser
bool SpindleLaser::enable_state; // Virtual enable state, controls enable pin if present and or apply power if > 0
uint8_t SpindleLaser::power, // Actual power output 0-255 ocr or "0 = off" > 0 = "on"
SpindleLaser::last_power_applied; // = 0 // Basic power state tracking
Expand Down Expand Up @@ -68,8 +68,8 @@ cutter_frequency_t SpindleLaser::frequency; // PWM fre
void SpindleLaser::init() {
#if ENABLED(LASER_FEATURE) && LASER_TOOL == 0
active_tool_type = TYPE_LASER;
#elif ENABLED(SPINDLE_FEATURE) && LASER_TOOL == 0
active_tool_type = TYPE_LASER;
#elif ENABLED(SPINDLE_FEATURE) && DISABLED(LASER_FEATURE) && !EXTRUDERS
active_tool_type = TYPE_SPINDLE;
#else
active_tool_type = TYPE_EXTRUDER;
#endif
Expand All @@ -86,13 +86,19 @@ void SpindleLaser::init() {
#endif
#if ENABLED(HAL_CAN_SET_PWM_FREQ) && SPINDLE_LASER_FREQUENCY
frequency = SPINDLE_LASER_FREQUENCY;
hal.set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_FREQUENCY);
hal.set_pwm_frequency(pin_t(LASER_PWM_PIN), SPINDLE_LASER_FREQUENCY);
#if ENABLED(SPINDLE_FEATURE)
hal.set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_FREQUENCY);
#endif
#if ENABLED(LASER_FEATURE)
hal.set_pwm_frequency(pin_t(LASER_PWM_PIN), SPINDLE_LASER_FREQUENCY);
#endif
#endif
#if ENABLED(SPINDLE_LASER_USE_PWM)
SET_PWM(SPINDLE_LASER_PWM_PIN);
hal.set_pwm_duty(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Set to lowest speed
#if PIN_EXISTS(LASER_PWM)
#if ENABLED(SPINDLE_FEATURE)
SET_PWM(SPINDLE_LASER_PWM_PIN);
hal.set_pwm_duty(pin_t(SPINDLE_LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Set to lowest speed
#endif
#if ENABLED(LASER_FEATURE)
SET_PWM(SPINDLE_LASER_PWM_PIN);
hal.set_pwm_duty(pin_t(LASER_PWM_PIN), SPINDLE_LASER_PWM_OFF); // Set to lowest speed
#endif
Expand All @@ -114,24 +120,33 @@ void SpindleLaser::init() {
*/
void SpindleLaser::_set_ocr(const uint8_t ocr) {
#if ENABLED(HAL_CAN_SET_PWM_FREQ) && SPINDLE_LASER_FREQUENCY
#if ENABLED(LASER_FEATURE)
if (active_tool_type == TYPE_LASER)
hal.set_pwm_frequency(pin_t(LASER_PWM_PIN), frequency);
#endif
#if ENABLED(SPINDLE_FEATURE)
if (active_tool_type != TYPE_LASER)
hal.set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), frequency);
#endif
#endif

#if ENABLED(LASER_FEATURE)
if (active_tool_type == TYPE_LASER)
hal.set_pwm_frequency(pin_t(LASER_PWM_PIN), frequency);
else
hal.set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), frequency);
hal.set_pwm_duty(pin_t(LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF);
#endif
#if ENABLED(SPINDLE_FEATURE)
if (active_tool_type != TYPE_LASER)
hal.set_pwm_duty(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF);
#endif
if (active_tool_type == TYPE_LASER)
hal.set_pwm_duty(pin_t(LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF);
elif (active_tool_type == TYPE_SPINDLE)
hal.set_pwm_duty(pin_t(SPINDLE_LASER_PWM_PIN), ocr ^ SPINDLE_LASER_PWM_OFF);
}

void SpindleLaser::set_ocr(const uint8_t ocr) {
#if PIN_EXISTS(LASER_ENA)
if (tool_type == TYPE_LASER)
if (active_tool_type == TYPE_LASER)
WRITE(LASER_ENA_PIN, SPINDLE_LASER_ACTIVE_STATE); // Cutter ON
#endif
#if PIN_EXISTS(SPINDLE_LASER_ENA)
if (tool_type == TYPE_SPINDLE)
if (active_tool_type != TYPE_LASER)
WRITE(SPINDLE_LASER_ENA_PIN, SPINDLE_LASER_ACTIVE_STATE); // Cutter ON
#endif
_set_ocr(ocr);
Expand All @@ -143,7 +158,7 @@ void SpindleLaser::init() {
WRITE(LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_STATE); // Cutter OFF
#endif
#if PIN_EXISTS(SPINDLE_LASER_ENA)
if (active_tool_type TYPE_SPINDLE)
if (active_tool_type == TYPE_SPINDLE)
WRITE(SPINDLE_LASER_ENA_PIN, !SPINDLE_LASER_ACTIVE_STATE); // Cutter OFF
#endif
_set_ocr(0);
Expand Down Expand Up @@ -177,7 +192,7 @@ void SpindleLaser::apply_power(const uint8_t opwr) {
#else
if (active_tool_type == TYPE_LASER)
WRITE(LASER_ENA_PIN, enabled() ? SPINDLE_LASER_ACTIVE_STATE : !SPINDLE_LASER_ACTIVE_STATE);
elif (active_tool_type == TYPE_SPINDLE)
else if (active_tool_type == TYPE_SPINDLE)
WRITE(SPINDLE_LASER_ENA_PIN, enabled() ? SPINDLE_LASER_ACTIVE_STATE : !SPINDLE_LASER_ACTIVE_STATE);
isReadyForUI = true;
#endif
Expand All @@ -203,8 +218,10 @@ void SpindleLaser::apply_power(const uint8_t opwr) {
*/
void SpindleLaser::set_reverse(const bool reverse) {
const bool dir_state = (reverse == SPINDLE_INVERT_DIR); // Forward (M3) HIGH when not inverted
if (TERN0(SPINDLE_STOP_ON_DIR_CHANGE, enabled()) && READ(SPINDLE_DIR_PIN) != dir_state) disable();
WRITE(SPINDLE_DIR_PIN, dir_state);
if (active_tool_type == TYPE_SPINDLE) {
if (TERN0(SPINDLE_STOP_ON_DIR_CHANGE, enabled()) && READ(SPINDLE_DIR_PIN) != dir_state) disable();
WRITE(SPINDLE_DIR_PIN, dir_state);
}
}
#endif

Expand Down
21 changes: 14 additions & 7 deletions Marlin/src/feature/spindle_laser.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

#include "../libs/buzzer.h"

#include "../core/types.h"

// Inline laser power
#include "../module/planner.h"

Expand Down Expand Up @@ -105,6 +107,7 @@ class SpindleLaser {
#endif

static bool isReadyForUI; // Ready to apply power setting from the UI to OCR
static ToolTypeEnum active_tool_type; // Tool type: 0 for extruder, 1 for spindle, 2 for laser
static bool enable_state;
static uint8_t power,
last_power_applied; // Basic power state tracking
Expand All @@ -117,13 +120,17 @@ class SpindleLaser {
static void init();

#if ENABLED(HAL_CAN_SET_PWM_FREQ) && SPINDLE_LASER_FREQUENCY
static void refresh_frequency() {
if (active_tool_type == TYPE_LASER) {
hal.set_pwm_frequency(pin_t(LASER_PWM_PIN), frequency);
}
elif (active_tool_type == TYPE_SPINDLE) {
hal.set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), frequency);
}
static void refresh_frequency() {
#if ENABLED(LASER_FEATURE)
if (active_tool_type == TYPE_LASER) {
hal.set_pwm_frequency(pin_t(LASER_PWM_PIN), frequency);
}
#endif
#if ENABLED(SPINDLE_FEATURE)
if (active_tool_type != TYPE_LASER) {
hal.set_pwm_frequency(pin_t(SPINDLE_LASER_PWM_PIN), frequency);
}
#endif
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/module/tool_change.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1544,7 +1544,7 @@ void tool_change(const uint8_t new_tool, bool no_move/*=false*/) {
if (new_tool == LASER_TOOL) {
cutter.active_tool_type = TYPE_LASER;
}
elif (new_tool < LASER_TOOL) {
else if (new_tool < LASER_TOOL) {
cutter.active_tool_type = TYPE_EXTRUDER;
}
else {
Expand Down
17 changes: 17 additions & 0 deletions Marlin/src/pins/rambo/pins_MINIRAMBO.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,29 @@
#ifndef SPINDLE_LASER_PWM_PIN
#define SPINDLE_LASER_PWM_PIN 9 // Hardware PWM
#endif
#if ENABLED(LASER_FEATURE)
#ifndef LASER_PWM_PIN
#define LASER_PWM_PIN 9 // Hardware PWM
#endif
#endif

#ifndef SPINDLE_LASER_ENA_PIN
#define SPINDLE_LASER_ENA_PIN 18 // Pullup!
#endif
#if ENABLED(LASER_FEATURE)
#ifndef LASER_ENA_PIN
#define LASER_ENA_PIN 18 // Pullup!
#endif
#endif
#ifndef SPINDLE_DIR_PIN
#define SPINDLE_DIR_PIN 19
#endif
#if ENABLED(LASER_FEATURE)
#ifndef LASER_DIR_PIN
#define LASER_DIR_PIN 19
#endif
#endif

#endif

//
Expand Down
15 changes: 15 additions & 0 deletions Marlin/src/pins/ramps/pins_RAMPS.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,26 @@
#ifndef SPINDLE_LASER_PWM_PIN
#define SPINDLE_LASER_PWM_PIN 6 // Hardware PWM
#endif
#if ENABLED(LASER_FEATURE)
#ifndef LASER_PWM_PIN
#define LASER_PWM_PIN 6 // Hardware PWM
#endif
#endif
#define SPINDLE_LASER_ENA_PIN 4 // Pullup or pulldown!
#define SPINDLE_DIR_PIN 5
#elif HAS_FREE_AUX2_PINS
#define SPINDLE_LASER_PWM_PIN AUX2_07 // Hardware PWM
#if ENABLED(LASER_FEATURE)
#ifndef LASER_PWM_PIN
#define LASER_PWM_PIN AUX2_07 // Hardware PWM
#endif
#endif
#define SPINDLE_LASER_ENA_PIN AUX2_06 // Pullup or pulldown!
#if ENABLED(LASER_FEATURE)
#ifndef LASER_ENA_PIN
#define LASER_ENA_PIN AUX2_06 // Pullup or pulldown!
#endif
#endif
#define SPINDLE_DIR_PIN AUX2_10
#else
#error "No auto-assignable Spindle/Laser pins available."
Expand Down
16 changes: 16 additions & 0 deletions Marlin/src/pins/ramps/pins_RUMBA.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,28 @@
#ifndef SPINDLE_LASER_PWM_PIN
#define SPINDLE_LASER_PWM_PIN 4 // Hardware PWM. Pin 4 interrupts OC0* and OC1* always in use?
#endif
#if ENABLED(LASER_FEATURE)
#ifndef LASER_PWM_PIN
#define LASER_PWM_PIN 4 // Hardware PWM. Pin 4 interrupts OC0* and OC1* always in use?
#endif
#endif

#ifndef SPINDLE_LASER_ENA_PIN
#define SPINDLE_LASER_ENA_PIN 14 // Pullup!
#endif
#if ENABLED(LASER_FEATURE)
#ifndef LASER_ENA_PIN
#define LASER_ENA_PIN 14 // Pullup!
#endif
#endif
#ifndef SPINDLE_DIR_PIN
#define SPINDLE_DIR_PIN 15
#endif
#if ENABLED(LASER_FEATURE)
#ifndef LASER_DIR_PIN
#define LASER_DIR_PIN 15
#endif
#endif

//
// LCD / Controller
Expand Down
12 changes: 12 additions & 0 deletions Marlin/src/pins/stm32f4/pins_BTT_SKR_V2_0_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,21 @@
#ifndef SPINDLE_LASER_PWM_PIN
#define SPINDLE_LASER_PWM_PIN PB5
#endif
#if ENABLED(LASER_FEATURE)
#ifndef LASER_PWM_PIN
#define LASER_PWM_PIN PB5
#endif
#endif

#ifndef SPINDLE_LASER_ENA_PIN
#define SPINDLE_LASER_ENA_PIN PB6
#endif
#if ENABLED(LASER_FEATURE)
#ifndef LASER_ENA_PIN
#define LASER_ENA_PIN PB6
#endif
#endif

#else
#ifndef FAN1_PIN
#define FAN1_PIN PB6 // Fan1
Expand Down
12 changes: 12 additions & 0 deletions Marlin/src/pins/stm32h7/pins_BTT_SKR_V3_0_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,21 @@
#ifndef SPINDLE_LASER_PWM_PIN
#define SPINDLE_LASER_PWM_PIN PB5
#endif
#if ENABLED(LASER_FEATURE)
#ifndef LASER_PWM_PIN
#define LASER_PWM_PIN PB5
#endif
#endif

#ifndef SPINDLE_LASER_ENA_PIN
#define SPINDLE_LASER_ENA_PIN PB6
#endif
#if ENABLED(LASER_FEATURE)
#ifndef LASER_ENA_PIN
#define LASER_ENA_PIN PB6
#endif
#endif

#else
#ifndef FAN1_PIN
#define FAN1_PIN PB6 // Fan1
Expand Down

0 comments on commit 4928e7f

Please sign in to comment.