Skip to content

Commit f6ecdae

Browse files
ellenspthinkyhead
andauthored
🔧 Base NUM_SERVO_PLUGS on SERVO PINS (MarlinFirmware#26640)
Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
1 parent 477b70e commit f6ecdae

File tree

10 files changed

+70
-33
lines changed

10 files changed

+70
-33
lines changed

Marlin/src/HAL/AVR/HAL.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ void MarlinHAL::init() {
8181
#if HAS_SERVO_3
8282
OUT_WRITE(SERVO3_PIN, LOW);
8383
#endif
84+
#if HAS_SERVO_4
85+
OUT_WRITE(SERVO4_PIN, LOW);
86+
#endif
87+
#if HAS_SERVO_5
88+
OUT_WRITE(SERVO5_PIN, LOW);
89+
#endif
8490

8591
init_pwm_timers(); // Init user timers to default frequency - 1000HZ
8692

Marlin/src/HAL/LPC1768/HAL.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ void MarlinHAL::init() {
111111
#if HAS_SERVO_3
112112
INIT_SERVO(3);
113113
#endif
114+
#if HAS_SERVO_4
115+
INIT_SERVO(4);
116+
#endif
117+
#if HAS_SERVO_5
118+
INIT_SERVO(5);
119+
#endif
114120

115121
//debug_frmwrk_init();
116122
//_DBG("\n\nDebug running\n");

Marlin/src/gcode/config/M43.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ inline void toggle_pins() {
118118

119119
inline void servo_probe_test() {
120120

121-
#if !(NUM_SERVOS > 0 && HAS_SERVO_0)
121+
#if !HAS_SERVO_0
122122

123123
SERIAL_ERROR_MSG("SERVO not set up.");
124124

Marlin/src/inc/Conditionals_post.h

+18-12
Original file line numberDiff line numberDiff line change
@@ -2762,20 +2762,26 @@
27622762
#endif
27632763

27642764
// Servos
2765-
#if PIN_EXISTS(SERVO0) && NUM_SERVOS > 0
2766-
#define HAS_SERVO_0 1
2767-
#endif
2768-
#if PIN_EXISTS(SERVO1) && NUM_SERVOS > 1
2769-
#define HAS_SERVO_1 1
2770-
#endif
2771-
#if PIN_EXISTS(SERVO2) && NUM_SERVOS > 2
2772-
#define HAS_SERVO_2 1
2773-
#endif
2774-
#if PIN_EXISTS(SERVO3) && NUM_SERVOS > 3
2775-
#define HAS_SERVO_3 1
2776-
#endif
27772765
#if NUM_SERVOS > 0
27782766
#define HAS_SERVOS 1
2767+
#if PIN_EXISTS(SERVO0)
2768+
#define HAS_SERVO_0 1
2769+
#endif
2770+
#if PIN_EXISTS(SERVO1) && NUM_SERVOS > 1
2771+
#define HAS_SERVO_1 1
2772+
#endif
2773+
#if PIN_EXISTS(SERVO2) && NUM_SERVOS > 2
2774+
#define HAS_SERVO_2 1
2775+
#endif
2776+
#if PIN_EXISTS(SERVO3) && NUM_SERVOS > 3
2777+
#define HAS_SERVO_3 1
2778+
#endif
2779+
#if PIN_EXISTS(SERVO4) && NUM_SERVOS > 4
2780+
#define HAS_SERVO_4 1
2781+
#endif
2782+
#if PIN_EXISTS(SERVO5) && NUM_SERVOS > 5
2783+
#define HAS_SERVO_5 1
2784+
#endif
27792785
#if defined(PAUSE_SERVO_OUTPUT) && defined(RESUME_SERVO_OUTPUT)
27802786
#define HAS_PAUSE_SERVO_OUTPUT 1
27812787
#endif

Marlin/src/inc/SanityCheck.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -961,9 +961,7 @@ static_assert(COUNT(arm) == LOGICAL_AXES, "AXIS_RELATIVE_MODES must contain " _L
961961
/**
962962
* Limited number of servos
963963
*/
964-
#if NUM_SERVOS > NUM_SERVO_PLUGS
965-
#error "The selected board doesn't support enough servos for your configuration. Reduce NUM_SERVOS."
966-
#endif
964+
static_assert(NUM_SERVOS <= NUM_SERVO_PLUGS, "NUM_SERVOS (or some servo index) is too large. The selected board only has " STRINGIFY(NUM_SERVO_PLUGS) " servos.");
967965

968966
/**
969967
* Servo deactivation depends on servo endstops, switching nozzle, or switching extruder

Marlin/src/module/servo.cpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,30 @@ hal_servo_t servo[NUM_SERVOS];
3737
#endif
3838

3939
void servo_init() {
40-
#if NUM_SERVOS >= 1 && HAS_SERVO_0
40+
#if HAS_SERVO_0
4141
servo[0].attach(SERVO0_PIN);
4242
servo[0].detach(); // Just set up the pin. We don't have a position yet. Don't move to a random position.
4343
#endif
44-
#if NUM_SERVOS >= 2 && HAS_SERVO_1
44+
#if HAS_SERVO_1
4545
servo[1].attach(SERVO1_PIN);
4646
servo[1].detach();
4747
#endif
48-
#if NUM_SERVOS >= 3 && HAS_SERVO_2
48+
#if HAS_SERVO_2
4949
servo[2].attach(SERVO2_PIN);
5050
servo[2].detach();
5151
#endif
52-
#if NUM_SERVOS >= 4 && HAS_SERVO_3
52+
#if HAS_SERVO_3
5353
servo[3].attach(SERVO3_PIN);
5454
servo[3].detach();
5555
#endif
56+
#if HAS_SERVO_4
57+
servo[4].attach(SERVO4_PIN);
58+
servo[4].detach();
59+
#endif
60+
#if HAS_SERVO_5
61+
servo[5].attach(SERVO5_PIN);
62+
servo[5].detach();
63+
#endif
5664
}
5765

5866
#endif // HAS_SERVOS

Marlin/src/module/servo.h

+3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@
107107
, { ASRC(3,0), ASRC(3,1) }
108108
#if NUM_SERVOS > 4
109109
, { ASRC(4,0), ASRC(4,1) }
110+
#if NUM_SERVOS > 5
111+
, { ASRC(5,0), ASRC(5,1) }
112+
#endif
110113
#endif
111114
#endif
112115
#endif

Marlin/src/module/settings.cpp

+9-12
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,6 @@
9292
#include "servo.h"
9393
#endif
9494

95-
#if HAS_SERVOS && HAS_SERVO_ANGLES
96-
#define EEPROM_NUM_SERVOS NUM_SERVOS
97-
#else
98-
#define EEPROM_NUM_SERVOS NUM_SERVO_PLUGS
99-
#endif
100-
10195
#include "../feature/fwretract.h"
10296

10397
#if ENABLED(POWER_LOSS_RECOVERY)
@@ -318,7 +312,9 @@ typedef struct SettingsDataStruct {
318312
//
319313
// SERVO_ANGLES
320314
//
321-
uint16_t servo_angles[EEPROM_NUM_SERVOS][2]; // M281 P L U
315+
#if HAS_SERVO_ANGLES
316+
uint16_t servo_angles[NUM_SERVOS][2]; // M281 P L U
317+
#endif
322318

323319
//
324320
// Temperature first layer compensation values
@@ -1051,13 +1047,12 @@ void MarlinSettings::postprocess() {
10511047
//
10521048
// Servo Angles
10531049
//
1050+
#if HAS_SERVO_ANGLES
10541051
{
10551052
_FIELD_TEST(servo_angles);
1056-
#if !HAS_SERVO_ANGLES
1057-
uint16_t servo_angles[EEPROM_NUM_SERVOS][2] = { { 0, 0 } };
1058-
#endif
10591053
EEPROM_WRITE(servo_angles);
10601054
}
1055+
#endif
10611056

10621057
//
10631058
// Thermal first layer compensation values
@@ -2082,15 +2077,17 @@ void MarlinSettings::postprocess() {
20822077
//
20832078
// SERVO_ANGLES
20842079
//
2080+
#if HAS_SERVO_ANGLES
20852081
{
20862082
_FIELD_TEST(servo_angles);
20872083
#if ENABLED(EDITABLE_SERVO_ANGLES)
2088-
uint16_t (&servo_angles_arr)[EEPROM_NUM_SERVOS][2] = servo_angles;
2084+
uint16_t (&servo_angles_arr)[NUM_SERVOS][2] = servo_angles;
20892085
#else
2090-
uint16_t servo_angles_arr[EEPROM_NUM_SERVOS][2];
2086+
uint16_t servo_angles_arr[NUM_SERVOS][2];
20912087
#endif
20922088
EEPROM_READ(servo_angles_arr);
20932089
}
2090+
#endif
20942091

20952092
//
20962093
// Thermal first layer compensation values

Marlin/src/pins/pins_postprocess.h

+13-1
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,20 @@
487487
#define SUICIDE_PIN_STATE LOW
488488
#endif
489489

490-
#ifndef NUM_SERVO_PLUGS
490+
#if PIN_EXISTS(SERVO5)
491+
#define NUM_SERVO_PLUGS 6
492+
#elif PIN_EXISTS(SERVO4)
493+
#define NUM_SERVO_PLUGS 5
494+
#elif PIN_EXISTS(SERVO3)
491495
#define NUM_SERVO_PLUGS 4
496+
#elif PIN_EXISTS(SERVO2)
497+
#define NUM_SERVO_PLUGS 3
498+
#elif PIN_EXISTS(SERVO1)
499+
#define NUM_SERVO_PLUGS 2
500+
#elif PIN_EXISTS(SERVO0)
501+
#define NUM_SERVO_PLUGS 1
502+
#else
503+
#define NUM_SERVO_PLUGS 0
492504
#endif
493505

494506
// Only used within pins files

buildroot/tests/BIGTREE_GTR_V1_0

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ exec_test $1 $2 "BigTreeTech GTR | 6 Extruders | Quad Z + Endstops" "$3"
3333
restore_configs
3434
opt_set MOTHERBOARD BOARD_BTT_GTR_V1_0 SERIAL_PORT -1 \
3535
EXTRUDERS 3 TEMP_SENSOR_1 1 TEMP_SENSOR_2 1 \
36+
SERVO1_PIN PE9 SERVO2_PIN PE11 \
3637
SERVO_DELAY '{ 300, 300, 300 }' \
3738
SWITCHING_TOOLHEAD_X_POS '{ 215, 0 ,0 }' \
3839
MPC_HEATER_POWER '{ 40.0f, 40.0f, 40.0f }' \

0 commit comments

Comments
 (0)