Skip to content

Commit d3687d9

Browse files
committed
πŸ§‘β€πŸ’» Add stepper/control.cpp (2)
1 parent 4b32be9 commit d3687d9

File tree

4 files changed

+51
-52
lines changed

4 files changed

+51
-52
lines changed

β€ŽMarlin/src/inc/Conditionals-5-post.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3012,7 +3012,7 @@
30123012
#undef MICROSTEP_MODES
30133013
#endif
30143014

3015-
#if MB(PRINTRBOARD_G2) || ANY(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_PWM, HAS_MICROSTEPS)
3015+
#if ANY(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_PWM, HAS_MICROSTEPS)
30163016
#define HAS_STEPPER_CONTROL 1
30173017
#endif
30183018

β€ŽMarlin/src/module/settings.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ typedef struct SettingsDataStruct {
508508
#endif
509509

510510
//
511-
// HAS_MOTOR_CURRENT_PWM
511+
// Stepper Motors Current
512512
//
513513
#ifndef MOTOR_CURRENT_COUNT
514514
#if HAS_MOTOR_CURRENT_PWM

β€ŽMarlin/src/module/stepper.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,6 @@ Stepper stepper; // Singleton
120120
#include "../feature/dac/dac_dac084s085.h"
121121
#endif
122122

123-
#if HAS_MOTOR_CURRENT_SPI
124-
#include <SPI.h>
125-
#endif
126-
127123
#if ENABLED(MIXING_EXTRUDER)
128124
#include "../feature/mixing.h"
129125
#endif
@@ -158,14 +154,6 @@ Stepper stepper; // Singleton
158154
bool Stepper::separate_multi_axis = false;
159155
#endif
160156

161-
#if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
162-
bool Stepper::initialized; // = false
163-
uint32_t Stepper::motor_current_setting[MOTOR_CURRENT_COUNT]; // Initialized by settings.load
164-
#if HAS_MOTOR_CURRENT_SPI
165-
constexpr uint32_t Stepper::digipot_count[];
166-
#endif
167-
#endif
168-
169157
stepper_flags_t Stepper::axis_enabled; // {0}
170158

171159
// private:

β€ŽMarlin/src/module/stepper/control.cpp

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,24 @@
3030

3131
#if MB(PRINTRBOARD_G2)
3232
#include HAL_PATH(../.., fastio/G2_PWM.h)
33+
#elif HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
34+
#define HAS_NON_G2_MOTOR_CURRENT 1
35+
#endif
36+
37+
#if HAS_MOTOR_CURRENT_PWM || HAS_NON_G2_MOTOR_CURRENT
38+
bool Stepper::initialized; // = false
39+
uint32_t Stepper::motor_current_setting[MOTOR_CURRENT_COUNT]; // Initialized by settings.load
3340
#endif
3441

3542
/**
36-
* Software-controlled Stepper Motor Current
43+
* SPI-controlled Stepper Motor Current
3744
*/
3845

3946
#if HAS_MOTOR_CURRENT_SPI
4047

48+
#include <SPI.h>
49+
constexpr uint32_t Stepper::digipot_count[];
50+
4151
// From Arduino DigitalPotControl example
4252
void Stepper::set_digipot_value_spi(const int16_t address, const int16_t value) {
4353
WRITE(DIGIPOTSS_PIN, LOW); // Take the SS pin low to select the chip
@@ -47,8 +57,36 @@
4757
//delay(10);
4858
}
4959

60+
#if HAS_NON_G2_MOTOR_CURRENT
61+
62+
void Stepper::set_digipot_current(const uint8_t driver, const int16_t current) {
63+
if (WITHIN(driver, 0, COUNT(motor_current_setting) - 1))
64+
motor_current_setting[driver] = current; // update motor_current_setting
65+
66+
if (!initialized) return;
67+
68+
//SERIAL_ECHOLNPGM("Digipotss current ", current);
69+
70+
const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
71+
set_digipot_value_spi(digipot_ch[driver], current);
72+
}
73+
74+
void Stepper::digipot_init() {
75+
SPI.begin();
76+
SET_OUTPUT(DIGIPOTSS_PIN);
77+
78+
for (uint8_t i = 0; i < COUNT(motor_current_setting); ++i)
79+
set_digipot_current(i, motor_current_setting[i]);
80+
}
81+
82+
#endif // HAS_NON_G2_MOTOR_CURRENT
83+
5084
#endif // HAS_MOTOR_CURRENT_SPI
5185

86+
/**
87+
* PWM-controlled Stepper Motor Current
88+
*/
89+
5290
#if HAS_MOTOR_CURRENT_PWM
5391

5492
void Stepper::refresh_motor_power() {
@@ -70,28 +108,13 @@
70108
}
71109
}
72110

73-
#endif // HAS_MOTOR_CURRENT_PWM
111+
#if HAS_NON_G2_MOTOR_CURRENT
74112

75-
/**
76-
* PWM-controlled Stepper Motor Current
77-
*/
113+
void Stepper::set_digipot_current(const uint8_t driver, const int16_t current) {
114+
if (WITHIN(driver, 0, COUNT(motor_current_setting) - 1))
115+
motor_current_setting[driver] = current; // update motor_current_setting
78116

79-
#if !MB(PRINTRBOARD_G2) && (HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM)
80-
81-
void Stepper::set_digipot_current(const uint8_t driver, const int16_t current) {
82-
if (WITHIN(driver, 0, COUNT(motor_current_setting) - 1))
83-
motor_current_setting[driver] = current; // update motor_current_setting
84-
85-
if (!initialized) return;
86-
87-
#if HAS_MOTOR_CURRENT_SPI
88-
89-
//SERIAL_ECHOLNPGM("Digipotss current ", current);
90-
91-
const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
92-
set_digipot_value_spi(digipot_ch[driver], current);
93-
94-
#elif HAS_MOTOR_CURRENT_PWM
117+
if (!initialized) return;
95118

96119
#define _WRITE_CURRENT_PWM(P) hal.set_pwm_duty(pin_t(MOTOR_CURRENT_PWM_## P ##_PIN), 255L * current / (MOTOR_CURRENT_PWM_RANGE))
97120
switch (driver) {
@@ -141,21 +164,9 @@
141164
#endif
142165
break;
143166
}
144-
#endif
145-
}
146-
147-
void Stepper::digipot_init() {
148-
149-
#if HAS_MOTOR_CURRENT_SPI
150-
151-
SPI.begin();
152-
SET_OUTPUT(DIGIPOTSS_PIN);
153-
154-
for (uint8_t i = 0; i < COUNT(motor_current_setting); ++i)
155-
set_digipot_current(i, motor_current_setting[i]);
156-
157-
#elif HAS_MOTOR_CURRENT_PWM
167+
}
158168

169+
void Stepper::digipot_init() {
159170
#ifdef __SAM3X8E__
160171
#define _RESET_CURRENT_PWM_FREQ(P) NOOP
161172
#else
@@ -204,11 +215,11 @@
204215
#endif
205216

206217
refresh_motor_power();
218+
}
207219

208-
#endif
209-
}
220+
#endif // HAS_NON_G2_MOTOR_CURRENT
210221

211-
#endif
222+
#endif // HAS_MOTOR_CURRENT_PWM
212223

213224
/**
214225
* Software-controlled Microstepping with digital pins

0 commit comments

Comments
Β (0)