|
30 | 30 |
|
31 | 31 | #if MB(PRINTRBOARD_G2)
|
32 | 32 | #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 |
33 | 40 | #endif
|
34 | 41 |
|
35 | 42 | /**
|
36 |
| - * Software-controlled Stepper Motor Current |
| 43 | + * SPI-controlled Stepper Motor Current |
37 | 44 | */
|
38 | 45 |
|
39 | 46 | #if HAS_MOTOR_CURRENT_SPI
|
40 | 47 |
|
| 48 | + #include <SPI.h> |
| 49 | + constexpr uint32_t Stepper::digipot_count[]; |
| 50 | + |
41 | 51 | // From Arduino DigitalPotControl example
|
42 | 52 | void Stepper::set_digipot_value_spi(const int16_t address, const int16_t value) {
|
43 | 53 | WRITE(DIGIPOTSS_PIN, LOW); // Take the SS pin low to select the chip
|
|
47 | 57 | //delay(10);
|
48 | 58 | }
|
49 | 59 |
|
| 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 | + |
50 | 84 | #endif // HAS_MOTOR_CURRENT_SPI
|
51 | 85 |
|
| 86 | +/** |
| 87 | + * PWM-controlled Stepper Motor Current |
| 88 | + */ |
| 89 | + |
52 | 90 | #if HAS_MOTOR_CURRENT_PWM
|
53 | 91 |
|
54 | 92 | void Stepper::refresh_motor_power() {
|
|
70 | 108 | }
|
71 | 109 | }
|
72 | 110 |
|
73 |
| -#endif // HAS_MOTOR_CURRENT_PWM |
| 111 | + #if HAS_NON_G2_MOTOR_CURRENT |
74 | 112 |
|
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 |
78 | 116 |
|
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; |
95 | 118 |
|
96 | 119 | #define _WRITE_CURRENT_PWM(P) hal.set_pwm_duty(pin_t(MOTOR_CURRENT_PWM_## P ##_PIN), 255L * current / (MOTOR_CURRENT_PWM_RANGE))
|
97 | 120 | switch (driver) {
|
|
141 | 164 | #endif
|
142 | 165 | break;
|
143 | 166 | }
|
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 | + } |
158 | 168 |
|
| 169 | + void Stepper::digipot_init() { |
159 | 170 | #ifdef __SAM3X8E__
|
160 | 171 | #define _RESET_CURRENT_PWM_FREQ(P) NOOP
|
161 | 172 | #else
|
|
204 | 215 | #endif
|
205 | 216 |
|
206 | 217 | refresh_motor_power();
|
| 218 | + } |
207 | 219 |
|
208 |
| - #endif |
209 |
| - } |
| 220 | + #endif // HAS_NON_G2_MOTOR_CURRENT |
210 | 221 |
|
211 |
| -#endif |
| 222 | +#endif // HAS_MOTOR_CURRENT_PWM |
212 | 223 |
|
213 | 224 | /**
|
214 | 225 | * Software-controlled Microstepping with digital pins
|
|
0 commit comments