Browse Source

Digipots refactor / cleanup (#19690)

Scott Lahteine 3 years ago
parent
commit
beb17d8855

+ 1
- 1
Marlin/src/HAL/DUE/fastio/G2_PWM.cpp View File

@@ -154,7 +154,7 @@ void Stepper::digipot_init() {
154 154
   NVIC_SetPriority(PWM_IRQn, NVIC_EncodePriority(0, 10, 0));  // normal priority for PWM module (can stand some jitter on the Vref signals)
155 155
 }
156 156
 
157
-void Stepper::digipot_current(const uint8_t driver, const int16_t current) {
157
+void Stepper::set_digipot_current(const uint8_t driver, const int16_t current) {
158 158
 
159 159
   if (!(PWM->PWM_CH_NUM[0].PWM_CPRD == PWM_PERIOD_US)) digipot_init();  // Init PWM system if needed
160 160
 

+ 1
- 1
Marlin/src/HAL/LPC1768/inc/SanityCheck.h View File

@@ -191,7 +191,7 @@ static_assert(DISABLED(BAUD_RATE_GCODE), "BAUD_RATE_GCODE is not yet supported o
191 191
 //
192 192
 // Flag any i2c pin conflicts
193 193
 //
194
-#if ANY(HAS_I2C_DIGIPOT, DAC_STEPPER_CURRENT, EXPERIMENTAL_I2CBUS, I2C_POSITION_ENCODERS, PCA9632, I2C_EEPROM)
194
+#if ANY(HAS_MOTOR_CURRENT_I2C, HAS_MOTOR_CURRENT_DAC, EXPERIMENTAL_I2CBUS, I2C_POSITION_ENCODERS, PCA9632, I2C_EEPROM)
195 195
   #define USEDI2CDEV_M 1  // <Arduino>/Wire.cpp
196 196
 
197 197
   #if USEDI2CDEV_M == 0         // P0_27 [D57] (AUX-1) .......... P0_28 [D58] (AUX-1)

+ 6
- 6
Marlin/src/MarlinCore.cpp View File

@@ -97,7 +97,7 @@
97 97
   #include "feature/closedloop.h"
98 98
 #endif
99 99
 
100
-#if HAS_I2C_DIGIPOT
100
+#if HAS_MOTOR_CURRENT_I2C
101 101
   #include "feature/digipot/digipot.h"
102 102
 #endif
103 103
 
@@ -125,7 +125,7 @@
125 125
   #include "module/servo.h"
126 126
 #endif
127 127
 
128
-#if ENABLED(DAC_STEPPER_CURRENT)
128
+#if ENABLED(HAS_MOTOR_CURRENT_DAC)
129 129
   #include "feature/dac/stepper_dac.h"
130 130
 #endif
131 131
 
@@ -1137,12 +1137,12 @@ void setup() {
1137 1137
     SETUP_RUN(enableStepperDrivers());
1138 1138
   #endif
1139 1139
 
1140
-  #if HAS_I2C_DIGIPOT
1141
-    SETUP_RUN(digipot_i2c_init());
1140
+  #if HAS_MOTOR_CURRENT_I2C
1141
+    SETUP_RUN(digipot_i2c.init());
1142 1142
   #endif
1143 1143
 
1144
-  #if ENABLED(DAC_STEPPER_CURRENT)
1145
-    SETUP_RUN(dac_init());
1144
+  #if ENABLED(HAS_MOTOR_CURRENT_DAC)
1145
+    SETUP_RUN(stepper_dac.init());
1146 1146
   #endif
1147 1147
 
1148 1148
   #if EITHER(Z_PROBE_SLED, SOLENOID_PROBE) && HAS_SOLENOID_1

+ 26
- 24
Marlin/src/feature/dac/dac_mcp4728.cpp View File

@@ -32,16 +32,18 @@
32 32
 
33 33
 #include "../../inc/MarlinConfig.h"
34 34
 
35
-#if ENABLED(DAC_STEPPER_CURRENT)
35
+#if ENABLED(HAS_MOTOR_CURRENT_DAC)
36 36
 
37 37
 #include "dac_mcp4728.h"
38 38
 
39
-xyze_uint_t mcp4728_values;
39
+MCP4728 mcp4728;
40
+
41
+xyze_uint_t dac_values;
40 42
 
41 43
 /**
42 44
  * Begin I2C, get current values (input register and eeprom) of mcp4728
43 45
  */
44
-void mcp4728_init() {
46
+void MCP4728::init() {
45 47
   Wire.begin();
46 48
   Wire.requestFrom(I2C_ADDRESS(DAC_DEV_ADDRESS), uint8_t(24));
47 49
   while (Wire.available()) {
@@ -50,7 +52,7 @@ void mcp4728_init() {
50 52
          loByte = Wire.read();
51 53
 
52 54
     if (!(deviceID & 0x08))
53
-      mcp4728_values[(deviceID & 0x30) >> 4] = word((hiByte & 0x0F), loByte);
55
+      dac_values[(deviceID & 0x30) >> 4] = word((hiByte & 0x0F), loByte);
54 56
   }
55 57
 }
56 58
 
@@ -58,9 +60,9 @@ void mcp4728_init() {
58 60
  * Write input resister value to specified channel using fastwrite method.
59 61
  * Channel : 0-3, Values : 0-4095
60 62
  */
61
-uint8_t mcp4728_analogWrite(const uint8_t channel, const uint16_t value) {
62
-  mcp4728_values[channel] = value;
63
-  return mcp4728_fastWrite();
63
+uint8_t MCP4728::analogWrite(const uint8_t channel, const uint16_t value) {
64
+  dac_values[channel] = value;
65
+  return fastWrite();
64 66
 }
65 67
 
66 68
 /**
@@ -68,12 +70,12 @@ uint8_t mcp4728_analogWrite(const uint8_t channel, const uint16_t value) {
68 70
  * This will update both input register and EEPROM value
69 71
  * This will also write current Vref, PowerDown, Gain settings to EEPROM
70 72
  */
71
-uint8_t mcp4728_eepromWrite() {
73
+uint8_t MCP4728::eepromWrite() {
72 74
   Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
73 75
   Wire.write(SEQWRITE);
74 76
   LOOP_XYZE(i) {
75
-    Wire.write(DAC_STEPPER_VREF << 7 | DAC_STEPPER_GAIN << 4 | highByte(mcp4728_values[i]));
76
-    Wire.write(lowByte(mcp4728_values[i]));
77
+    Wire.write(DAC_STEPPER_VREF << 7 | DAC_STEPPER_GAIN << 4 | highByte(dac_values[i]));
78
+    Wire.write(lowByte(dac_values[i]));
77 79
   }
78 80
   return Wire.endTransmission();
79 81
 }
@@ -81,7 +83,7 @@ uint8_t mcp4728_eepromWrite() {
81 83
 /**
82 84
  * Write Voltage reference setting to all input regiters
83 85
  */
84
-uint8_t mcp4728_setVref_all(const uint8_t value) {
86
+uint8_t MCP4728::setVref_all(const uint8_t value) {
85 87
   Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
86 88
   Wire.write(VREFWRITE | (value ? 0x0F : 0x00));
87 89
   return Wire.endTransmission();
@@ -89,7 +91,7 @@ uint8_t mcp4728_setVref_all(const uint8_t value) {
89 91
 /**
90 92
  * Write Gain setting to all input regiters
91 93
  */
92
-uint8_t mcp4728_setGain_all(const uint8_t value) {
94
+uint8_t MCP4728::setGain_all(const uint8_t value) {
93 95
   Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
94 96
   Wire.write(GAINWRITE | (value ? 0x0F : 0x00));
95 97
   return Wire.endTransmission();
@@ -98,16 +100,16 @@ uint8_t mcp4728_setGain_all(const uint8_t value) {
98 100
 /**
99 101
  * Return Input Register value
100 102
  */
101
-uint16_t mcp4728_getValue(const uint8_t channel) { return mcp4728_values[channel]; }
103
+uint16_t MCP4728::getValue(const uint8_t channel) { return dac_values[channel]; }
102 104
 
103 105
 #if 0
104 106
 /**
105 107
  * Steph: Might be useful in the future
106 108
  * Return Vout
107 109
  */
108
-uint16_t mcp4728_getVout(const uint8_t channel) {
110
+uint16_t MCP4728::getVout(const uint8_t channel) {
109 111
   const uint32_t vref = 2048,
110
-                 vOut = (vref * mcp4728_values[channel] * (_DAC_STEPPER_GAIN + 1)) / 4096;
112
+                 vOut = (vref * dac_values[channel] * (_DAC_STEPPER_GAIN + 1)) / 4096;
111 113
   return _MIN(vOut, defaultVDD);
112 114
 }
113 115
 #endif
@@ -115,15 +117,15 @@ uint16_t mcp4728_getVout(const uint8_t channel) {
115 117
 /**
116 118
  * Returns DAC values as a 0-100 percentage of drive strength
117 119
  */
118
-uint8_t mcp4728_getDrvPct(const uint8_t channel) { return uint8_t(100.0 * mcp4728_values[channel] / (DAC_STEPPER_MAX) + 0.5); }
120
+uint8_t MCP4728::getDrvPct(const uint8_t channel) { return uint8_t(100.0 * dac_values[channel] / (DAC_STEPPER_MAX) + 0.5); }
119 121
 
120 122
 /**
121 123
  * Receives all Drive strengths as 0-100 percent values, updates
122 124
  * DAC Values array and calls fastwrite to update the DAC.
123 125
  */
124
-void mcp4728_setDrvPct(xyze_uint8_t &pct) {
125
-  mcp4728_values *= 0.01 * pct * (DAC_STEPPER_MAX);
126
-  mcp4728_fastWrite();
126
+void MCP4728::setDrvPct(xyze_uint8_t &pct) {
127
+  dac_values *= 0.01 * pct * (DAC_STEPPER_MAX);
128
+  fastWrite();
127 129
 }
128 130
 
129 131
 /**
@@ -131,11 +133,11 @@ void mcp4728_setDrvPct(xyze_uint8_t &pct) {
131 133
  * DAC Input and PowerDown bits update.
132 134
  * No EEPROM update
133 135
  */
134
-uint8_t mcp4728_fastWrite() {
136
+uint8_t MCP4728::fastWrite() {
135 137
   Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
136 138
   LOOP_XYZE(i) {
137
-    Wire.write(highByte(mcp4728_values[i]));
138
-    Wire.write(lowByte(mcp4728_values[i]));
139
+    Wire.write(highByte(dac_values[i]));
140
+    Wire.write(lowByte(dac_values[i]));
139 141
   }
140 142
   return Wire.endTransmission();
141 143
 }
@@ -143,10 +145,10 @@ uint8_t mcp4728_fastWrite() {
143 145
 /**
144 146
  * Common function for simple general commands
145 147
  */
146
-uint8_t mcp4728_simpleCommand(const byte simpleCommand) {
148
+uint8_t MCP4728::simpleCommand(const byte simpleCommand) {
147 149
   Wire.beginTransmission(I2C_ADDRESS(GENERALCALL));
148 150
   Wire.write(simpleCommand);
149 151
   return Wire.endTransmission();
150 152
 }
151 153
 
152
-#endif // DAC_STEPPER_CURRENT
154
+#endif // HAS_MOTOR_CURRENT_DAC

+ 15
- 10
Marlin/src/feature/dac/dac_mcp4728.h View File

@@ -65,13 +65,18 @@
65 65
 // DAC_OR_ADDRESS defined in pins_BOARD.h  file
66 66
 #define DAC_DEV_ADDRESS (BASE_ADDR | DAC_OR_ADDRESS)
67 67
 
68
-void mcp4728_init();
69
-uint8_t mcp4728_analogWrite(const uint8_t channel, const uint16_t value);
70
-uint8_t mcp4728_eepromWrite();
71
-uint8_t mcp4728_setVref_all(const uint8_t value);
72
-uint8_t mcp4728_setGain_all(const uint8_t value);
73
-uint16_t mcp4728_getValue(const uint8_t channel);
74
-uint8_t mcp4728_fastWrite();
75
-uint8_t mcp4728_simpleCommand(const byte simpleCommand);
76
-uint8_t mcp4728_getDrvPct(const uint8_t channel);
77
-void mcp4728_setDrvPct(xyze_uint8_t &pct);
68
+class MCP4728 {
69
+public:
70
+  static void     init();
71
+  static uint8_t  analogWrite(const uint8_t channel, const uint16_t value);
72
+  static uint8_t  eepromWrite();
73
+  static uint8_t  setVref_all(const uint8_t value);
74
+  static uint8_t  setGain_all(const uint8_t value);
75
+  static uint16_t getValue(const uint8_t channel);
76
+  static uint8_t  fastWrite();
77
+  static uint8_t  simpleCommand(const byte simpleCommand);
78
+  static uint8_t  getDrvPct(const uint8_t channel);
79
+  static void     setDrvPct(xyze_uint8_t &pct);
80
+};
81
+
82
+extern MCP4728 mcp4728;

+ 26
- 29
Marlin/src/feature/dac/stepper_dac.cpp View File

@@ -26,7 +26,7 @@
26 26
 
27 27
 #include "../../inc/MarlinConfig.h"
28 28
 
29
-#if ENABLED(DAC_STEPPER_CURRENT)
29
+#if ENABLED(HAS_MOTOR_CURRENT_DAC)
30 30
 
31 31
 #include "stepper_dac.h"
32 32
 #include "../../MarlinCore.h" // for SP_X_LBL...
@@ -35,56 +35,53 @@ bool dac_present = false;
35 35
 constexpr xyze_uint8_t dac_order = DAC_STEPPER_ORDER;
36 36
 xyze_uint8_t dac_channel_pct = DAC_MOTOR_CURRENT_DEFAULT;
37 37
 
38
-int dac_init() {
38
+StepperDAC stepper_dac;
39
+
40
+int StepperDAC::init() {
39 41
   #if PIN_EXISTS(DAC_DISABLE)
40 42
     OUT_WRITE(DAC_DISABLE_PIN, LOW);  // set pin low to enable DAC
41 43
   #endif
42 44
 
43
-  mcp4728_init();
45
+  mcp4728.init();
44 46
 
45
-  if (mcp4728_simpleCommand(RESET)) return -1;
47
+  if (mcp4728.simpleCommand(RESET)) return -1;
46 48
 
47 49
   dac_present = true;
48 50
 
49
-  mcp4728_setVref_all(DAC_STEPPER_VREF);
50
-  mcp4728_setGain_all(DAC_STEPPER_GAIN);
51
+  mcp4728.setVref_all(DAC_STEPPER_VREF);
52
+  mcp4728.setGain_all(DAC_STEPPER_GAIN);
51 53
 
52
-  if (mcp4728_getDrvPct(0) < 1 || mcp4728_getDrvPct(1) < 1 || mcp4728_getDrvPct(2) < 1 || mcp4728_getDrvPct(3) < 1 ) {
53
-    mcp4728_setDrvPct(dac_channel_pct);
54
-    mcp4728_eepromWrite();
54
+  if (mcp4728.getDrvPct(0) < 1 || mcp4728.getDrvPct(1) < 1 || mcp4728.getDrvPct(2) < 1 || mcp4728.getDrvPct(3) < 1 ) {
55
+    mcp4728.setDrvPct(dac_channel_pct);
56
+    mcp4728.eepromWrite();
55 57
   }
56 58
 
57 59
   return 0;
58 60
 }
59 61
 
60
-void dac_current_percent(uint8_t channel, float val) {
62
+void StepperDAC::set_current_value(const uint8_t channel, uint16_t val) {
61 63
   if (!dac_present) return;
62 64
 
63
-  NOMORE(val, 100);
65
+  NOMORE(val, uint16_t(DAC_STEPPER_MAX));
64 66
 
65
-  mcp4728_analogWrite(dac_order[channel], val * 0.01 * (DAC_STEPPER_MAX));
66
-  mcp4728_simpleCommand(UPDATE);
67
+  mcp4728.analogWrite(dac_order[channel], val);
68
+  mcp4728.simpleCommand(UPDATE);
67 69
 }
68 70
 
69
-void dac_current_raw(uint8_t channel, uint16_t val) {
70
-  if (!dac_present) return;
71
-
72
-  NOMORE(val, uint16_t(DAC_STEPPER_MAX));
73
-
74
-  mcp4728_analogWrite(dac_order[channel], val);
75
-  mcp4728_simpleCommand(UPDATE);
71
+void StepperDAC::set_current_percent(const uint8_t channel, float val) {
72
+  set_current_value(channel, _MIN(val, 100.0f) * (DAC_STEPPER_MAX) / 100.0f);
76 73
 }
77 74
 
78
-static float dac_perc(int8_t n) { return 100.0 * mcp4728_getValue(dac_order[n]) * RECIPROCAL(DAC_STEPPER_MAX); }
79
-static float dac_amps(int8_t n) { return mcp4728_getDrvPct(dac_order[n]) * (DAC_STEPPER_MAX) * 0.125 * RECIPROCAL(DAC_STEPPER_SENSE); }
75
+static float dac_perc(int8_t n) { return 100.0 * mcp4728.getValue(dac_order[n]) * RECIPROCAL(DAC_STEPPER_MAX); }
76
+static float dac_amps(int8_t n) { return mcp4728.getDrvPct(dac_order[n]) * (DAC_STEPPER_MAX) * 0.125 * RECIPROCAL(DAC_STEPPER_SENSE); }
80 77
 
81
-uint8_t dac_current_get_percent(const AxisEnum axis) { return mcp4728_getDrvPct(dac_order[axis]); }
82
-void dac_current_set_percents(xyze_uint8_t &pct) {
78
+uint8_t StepperDAC::get_current_percent(const AxisEnum axis) { return mcp4728.getDrvPct(dac_order[axis]); }
79
+void StepperDAC::set_current_percents(xyze_uint8_t &pct) {
83 80
   LOOP_XYZE(i) dac_channel_pct[i] = pct[dac_order[i]];
84
-  mcp4728_setDrvPct(dac_channel_pct);
81
+  mcp4728.setDrvPct(dac_channel_pct);
85 82
 }
86 83
 
87
-void dac_print_values() {
84
+void StepperDAC::print_values() {
88 85
   if (!dac_present) return;
89 86
   SERIAL_ECHO_MSG("Stepper current values in % (Amps):");
90 87
   SERIAL_ECHO_START();
@@ -94,9 +91,9 @@ void dac_print_values() {
94 91
   SERIAL_ECHOLNPAIR_P(SP_E_LBL, dac_perc(E_AXIS), PSTR(" ("), dac_amps(E_AXIS), PSTR(")"));
95 92
 }
96 93
 
97
-void dac_commit_eeprom() {
94
+void StepperDAC::commit_eeprom() {
98 95
   if (!dac_present) return;
99
-  mcp4728_eepromWrite();
96
+  mcp4728.eepromWrite();
100 97
 }
101 98
 
102
-#endif // DAC_STEPPER_CURRENT
99
+#endif // HAS_MOTOR_CURRENT_DAC

+ 12
- 7
Marlin/src/feature/dac/stepper_dac.h View File

@@ -27,10 +27,15 @@
27 27
 
28 28
 #include "dac_mcp4728.h"
29 29
 
30
-int dac_init();
31
-void dac_current_percent(uint8_t channel, float val);
32
-void dac_current_raw(uint8_t channel, uint16_t val);
33
-void dac_print_values();
34
-void dac_commit_eeprom();
35
-uint8_t dac_current_get_percent(AxisEnum axis);
36
-void dac_current_set_percents(xyze_uint8_t &pct);
30
+class StepperDAC {
31
+public:
32
+  static int init();
33
+  static void set_current_percent(const uint8_t channel, float val);
34
+  static void set_current_value(const uint8_t channel, uint16_t val);
35
+  static void print_values();
36
+  static void commit_eeprom();
37
+  static uint8_t get_current_percent(AxisEnum axis);
38
+  static void set_current_percents(xyze_uint8_t &pct);
39
+};
40
+
41
+extern StepperDAC stepper_dac;

+ 10
- 2
Marlin/src/feature/digipot/digipot.h View File

@@ -21,5 +21,13 @@
21 21
  */
22 22
 #pragma once
23 23
 
24
-void digipot_i2c_set_current(const uint8_t channel, const float current);
25
-void digipot_i2c_init();
24
+//
25
+// Header for MCP4018 and MCP4451 current control i2c devices
26
+//
27
+class DigipotI2C {
28
+public:
29
+  static void init();
30
+  static void set_current(const uint8_t channel, const float current);
31
+};
32
+
33
+DigipotI2C digipot_i2c;

+ 7
- 5
Marlin/src/feature/digipot/digipot_mcp4018.cpp View File

@@ -24,6 +24,8 @@
24 24
 
25 25
 #if ENABLED(DIGIPOT_MCP4018)
26 26
 
27
+#include "digipot.h"
28
+
27 29
 #include <Stream.h>
28 30
 #include <SlowSoftI2CMaster.h>  // https://github.com/stawel/SlowSoftI2CMaster
29 31
 
@@ -68,7 +70,7 @@ static SlowSoftI2CMaster pots[DIGIPOT_I2C_NUM_CHANNELS] = {
68 70
   #endif
69 71
 };
70 72
 
71
-static void i2c_send(const uint8_t channel, const byte v) {
73
+static void digipot_i2c_send(const uint8_t channel, const byte v) {
72 74
   if (WITHIN(channel, 0, DIGIPOT_I2C_NUM_CHANNELS - 1)) {
73 75
     pots[channel].i2c_start(((DIGIPOT_I2C_ADDRESS_A) << 1) | I2C_WRITE);
74 76
     pots[channel].i2c_write(v);
@@ -77,12 +79,12 @@ static void i2c_send(const uint8_t channel, const byte v) {
77 79
 }
78 80
 
79 81
 // This is for the MCP4018 I2C based digipot
80
-void digipot_i2c_set_current(const uint8_t channel, const float current) {
82
+void DigipotI2C::set_current(const uint8_t channel, const float current) {
81 83
   const float ival = _MIN(_MAX(current, 0), float(DIGIPOT_MCP4018_MAX_VALUE));
82
-  i2c_send(channel, current_to_wiper(ival));
84
+  digipot_i2c_send(channel, current_to_wiper(ival));
83 85
 }
84 86
 
85
-void digipot_i2c_init() {
87
+void DigipotI2C::init() {
86 88
   LOOP_L_N(i, DIGIPOT_I2C_NUM_CHANNELS) pots[i].i2c_init();
87 89
 
88 90
   // Init currents according to Configuration_adv.h
@@ -94,7 +96,7 @@ void digipot_i2c_init() {
94 96
     #endif
95 97
   ;
96 98
   LOOP_L_N(i, COUNT(digipot_motor_current))
97
-    digipot_i2c_set_current(i, pgm_read_float(&digipot_motor_current[i]));
99
+    set_current(i, pgm_read_float(&digipot_motor_current[i]));
98 100
 }
99 101
 
100 102
 #endif // DIGIPOT_MCP4018

+ 5
- 3
Marlin/src/feature/digipot/digipot_mcp4451.cpp View File

@@ -24,6 +24,8 @@
24 24
 
25 25
 #if ENABLED(DIGIPOT_MCP4451)
26 26
 
27
+#include "digipot.h"
28
+
27 29
 #include <Stream.h>
28 30
 #include <Wire.h>
29 31
 
@@ -61,7 +63,7 @@ static void digipot_i2c_send(const byte addr, const byte a, const byte b) {
61 63
 }
62 64
 
63 65
 // This is for the MCP4451 I2C based digipot
64
-void digipot_i2c_set_current(const uint8_t channel, const float current) {
66
+void DigipotI2C::set_current(const uint8_t channel, const float current) {
65 67
   // These addresses are specific to Azteeg X3 Pro, can be set to others.
66 68
   // In this case first digipot is at address A0=0, A1=0, second one is at A0=0, A1=1
67 69
   const byte addr = channel < 4 ? DIGIPOT_I2C_ADDRESS_A : DIGIPOT_I2C_ADDRESS_B; // channel 0-3 vs 4-7
@@ -75,7 +77,7 @@ void digipot_i2c_set_current(const uint8_t channel, const float current) {
75 77
   digipot_i2c_send(addr, addresses[channel & 0x3], current_to_wiper(_MIN(float(_MAX(current, 0)), DIGIPOT_I2C_MAX_CURRENT)));
76 78
 }
77 79
 
78
-void digipot_i2c_init() {
80
+void DigipotI2C::init() {
79 81
   #if MB(MKS_SBASE)
80 82
     configure_i2c(16); // Set clock_option to 16 ensure I2C is initialized at 400kHz
81 83
   #else
@@ -90,7 +92,7 @@ void digipot_i2c_init() {
90 92
     #endif
91 93
   ;
92 94
   LOOP_L_N(i, COUNT(digipot_motor_current))
93
-    digipot_i2c_set_current(i, pgm_read_float(&digipot_motor_current[i]));
95
+    set_current(i, pgm_read_float(&digipot_motor_current[i]));
94 96
 }
95 97
 
96 98
 #endif // DIGIPOT_MCP4451

+ 26
- 26
Marlin/src/gcode/feature/digipot/M907-M910.cpp View File

@@ -22,19 +22,19 @@
22 22
 
23 23
 #include "../../../inc/MarlinConfig.h"
24 24
 
25
-#if ANY(HAS_DIGIPOTSS, HAS_MOTOR_CURRENT_PWM, HAS_I2C_DIGIPOT, DAC_STEPPER_CURRENT)
25
+#if ANY(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_PWM, HAS_MOTOR_CURRENT_I2C, HAS_MOTOR_CURRENT_DAC)
26 26
 
27 27
 #include "../../gcode.h"
28 28
 
29
-#if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
29
+#if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
30 30
   #include "../../../module/stepper.h"
31 31
 #endif
32 32
 
33
-#if HAS_I2C_DIGIPOT
33
+#if HAS_MOTOR_CURRENT_I2C
34 34
   #include "../../../feature/digipot/digipot.h"
35 35
 #endif
36 36
 
37
-#if ENABLED(DAC_STEPPER_CURRENT)
37
+#if ENABLED(HAS_MOTOR_CURRENT_DAC)
38 38
   #include "../../../feature/dac/stepper_dac.h"
39 39
 #endif
40 40
 
@@ -42,61 +42,61 @@
42 42
  * M907: Set digital trimpot motor current using axis codes X, Y, Z, E, B, S
43 43
  */
44 44
 void GcodeSuite::M907() {
45
-  #if HAS_DIGIPOTSS
45
+  #if HAS_MOTOR_CURRENT_SPI
46 46
 
47
-    LOOP_XYZE(i) if (parser.seenval(axis_codes[i])) stepper.digipot_current(i, parser.value_int());
48
-    if (parser.seenval('B')) stepper.digipot_current(4, parser.value_int());
49
-    if (parser.seenval('S')) LOOP_LE_N(i, 4) stepper.digipot_current(i, parser.value_int());
47
+    LOOP_XYZE(i) if (parser.seenval(axis_codes[i])) stepper.set_digipot_current(i, parser.value_int());
48
+    if (parser.seenval('B')) stepper.set_digipot_current(4, parser.value_int());
49
+    if (parser.seenval('S')) LOOP_LE_N(i, 4) stepper.set_digipot_current(i, parser.value_int());
50 50
 
51 51
   #elif HAS_MOTOR_CURRENT_PWM
52 52
 
53 53
     #if ANY_PIN(MOTOR_CURRENT_PWM_X, MOTOR_CURRENT_PWM_Y, MOTOR_CURRENT_PWM_XY)
54
-      if (parser.seenval('X') || parser.seenval('Y')) stepper.digipot_current(0, parser.value_int());
54
+      if (parser.seenval('X') || parser.seenval('Y')) stepper.set_digipot_current(0, parser.value_int());
55 55
     #endif
56 56
     #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
57
-      if (parser.seenval('Z')) stepper.digipot_current(1, parser.value_int());
57
+      if (parser.seenval('Z')) stepper.set_digipot_current(1, parser.value_int());
58 58
     #endif
59 59
     #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
60
-      if (parser.seenval('E')) stepper.digipot_current(2, parser.value_int());
60
+      if (parser.seenval('E')) stepper.set_digipot_current(2, parser.value_int());
61 61
     #endif
62 62
 
63 63
   #endif
64 64
 
65
-  #if HAS_I2C_DIGIPOT
65
+  #if HAS_MOTOR_CURRENT_I2C
66 66
     // this one uses actual amps in floating point
67
-    LOOP_XYZE(i) if (parser.seenval(axis_codes[i])) digipot_i2c_set_current(i, parser.value_float());
67
+    LOOP_XYZE(i) if (parser.seenval(axis_codes[i])) digipot_i2c.set_current(i, parser.value_float());
68 68
     // Additional extruders use B,C,D for channels 4,5,6.
69 69
     // TODO: Change these parameters because 'E' is used. B<index>?
70 70
     for (uint8_t i = E_AXIS + 1; i < DIGIPOT_I2C_NUM_CHANNELS; i++)
71
-      if (parser.seenval('B' + i - (E_AXIS + 1))) digipot_i2c_set_current(i, parser.value_float());
71
+      if (parser.seenval('B' + i - (E_AXIS + 1))) digipot_i2c.set_current(i, parser.value_float());
72 72
   #endif
73 73
 
74
-  #if ENABLED(DAC_STEPPER_CURRENT)
74
+  #if ENABLED(HAS_MOTOR_CURRENT_DAC)
75 75
     if (parser.seenval('S')) {
76 76
       const float dac_percent = parser.value_float();
77
-      LOOP_LE_N(i, 4) dac_current_percent(i, dac_percent);
77
+      LOOP_LE_N(i, 4) stepper_dac.set_current_percent(i, dac_percent);
78 78
     }
79
-    LOOP_XYZE(i) if (parser.seenval(axis_codes[i])) dac_current_percent(i, parser.value_float());
79
+    LOOP_XYZE(i) if (parser.seenval(axis_codes[i])) stepper_dac.set_current_percent(i, parser.value_float());
80 80
   #endif
81 81
 }
82 82
 
83
-#if EITHER(HAS_DIGIPOTSS, DAC_STEPPER_CURRENT)
83
+#if EITHER(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_DAC)
84 84
 
85 85
   /**
86 86
    * M908: Control digital trimpot directly (M908 P<pin> S<current>)
87 87
    */
88 88
   void GcodeSuite::M908() {
89
-    TERN_(HAS_DIGIPOTSS, stepper.digitalPotWrite(parser.intval('P'), parser.intval('S')));
90
-    TERN_(DAC_STEPPER_CURRENT, dac_current_raw(parser.byteval('P', -1), parser.ushortval('S', 0)));
89
+    TERN_(HAS_MOTOR_CURRENT_SPI, stepper.set_digipot_value_spi(parser.intval('P'), parser.intval('S')));
90
+    TERN_(HAS_MOTOR_CURRENT_DAC, stepper_dac.set_current_value(parser.byteval('P', -1), parser.ushortval('S', 0)));
91 91
   }
92 92
 
93
-#endif // HAS_DIGIPOTSS || DAC_STEPPER_CURRENT
93
+  #if ENABLED(HAS_MOTOR_CURRENT_DAC)
94 94
 
95
-#if ENABLED(DAC_STEPPER_CURRENT)
95
+    void GcodeSuite::M909() { stepper_dac.print_values(); }
96
+    void GcodeSuite::M910() { stepper_dac.commit_eeprom(); }
96 97
 
97
-  void GcodeSuite::M909() { dac_print_values(); }
98
-  void GcodeSuite::M910() { dac_commit_eeprom(); }
98
+  #endif // HAS_MOTOR_CURRENT_DAC
99 99
 
100
-#endif // DAC_STEPPER_CURRENT
100
+#endif // HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_DAC
101 101
 
102
-#endif // HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM || HAS_I2C_DIGIPOT || DAC_STEPPER_CURRENT
102
+#endif // HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_I2C || HAS_MOTOR_CURRENT_DAC

+ 3
- 3
Marlin/src/gcode/gcode.cpp View File

@@ -823,11 +823,11 @@ void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/) {
823 823
         case 900: M900(); break;                                  // M900: Set advance K factor.
824 824
       #endif
825 825
 
826
-      #if ANY(HAS_DIGIPOTSS, HAS_MOTOR_CURRENT_PWM, HAS_I2C_DIGIPOT, DAC_STEPPER_CURRENT)
826
+      #if ANY(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_PWM, HAS_MOTOR_CURRENT_I2C, HAS_MOTOR_CURRENT_DAC)
827 827
         case 907: M907(); break;                                  // M907: Set digital trimpot motor current using axis codes.
828
-        #if EITHER(HAS_DIGIPOTSS, DAC_STEPPER_CURRENT)
828
+        #if EITHER(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_DAC)
829 829
           case 908: M908(); break;                                // M908: Control digital trimpot directly.
830
-          #if ENABLED(DAC_STEPPER_CURRENT)
830
+          #if ENABLED(HAS_MOTOR_CURRENT_DAC)
831 831
             case 909: M909(); break;                              // M909: Print digipot/DAC current value
832 832
             case 910: M910(); break;                              // M910: Commit digipot/DAC value to external EEPROM
833 833
           #endif

+ 6
- 6
Marlin/src/gcode/gcode.h View File

@@ -258,9 +258,9 @@
258 258
  * M900 - Get or Set Linear Advance K-factor. (Requires LIN_ADVANCE)
259 259
  * M906 - Set or get motor current in milliamps using axis codes X, Y, Z, E. Report values if no axis codes given. (Requires at least one _DRIVER_TYPE defined as TMC2130/2160/5130/5160/2208/2209/2660 or L6470)
260 260
  * M907 - Set digital trimpot motor current using axis codes. (Requires a board with digital trimpots)
261
- * M908 - Control digital trimpot directly. (Requires DAC_STEPPER_CURRENT or DIGIPOTSS_PIN)
262
- * M909 - Print digipot/DAC current value. (Requires DAC_STEPPER_CURRENT)
263
- * M910 - Commit digipot/DAC value to external EEPROM via I2C. (Requires DAC_STEPPER_CURRENT)
261
+ * M908 - Control digital trimpot directly. (Requires HAS_MOTOR_CURRENT_DAC or DIGIPOTSS_PIN)
262
+ * M909 - Print digipot/DAC current value. (Requires HAS_MOTOR_CURRENT_DAC)
263
+ * M910 - Commit digipot/DAC value to external EEPROM via I2C. (Requires HAS_MOTOR_CURRENT_DAC)
264 264
  * M911 - Report stepper driver overtemperature pre-warn condition. (Requires at least one _DRIVER_TYPE defined as TMC2130/2160/5130/5160/2208/2209/2660)
265 265
  * M912 - Clear stepper driver overtemperature pre-warn condition flag. (Requires at least one _DRIVER_TYPE defined as TMC2130/2160/5130/5160/2208/2209/2660)
266 266
  * M913 - Set HYBRID_THRESHOLD speed. (Requires HYBRID_THRESHOLD)
@@ -848,11 +848,11 @@ private:
848 848
     static void M918();
849 849
   #endif
850 850
 
851
-  #if ANY(HAS_DIGIPOTSS, HAS_MOTOR_CURRENT_PWM, HAS_I2C_DIGIPOT, DAC_STEPPER_CURRENT)
851
+  #if ANY(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_PWM, HAS_MOTOR_CURRENT_I2C, HAS_MOTOR_CURRENT_DAC)
852 852
     static void M907();
853
-    #if EITHER(HAS_DIGIPOTSS, DAC_STEPPER_CURRENT)
853
+    #if EITHER(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_DAC)
854 854
       static void M908();
855
-      #if ENABLED(DAC_STEPPER_CURRENT)
855
+      #if ENABLED(HAS_MOTOR_CURRENT_DAC)
856 856
         static void M909();
857 857
         static void M910();
858 858
       #endif

+ 1
- 1
Marlin/src/inc/Conditionals_adv.h View File

@@ -175,7 +175,7 @@
175 175
 #endif
176 176
 
177 177
 #if EITHER(DIGIPOT_MCP4018, DIGIPOT_MCP4451)
178
-  #define HAS_I2C_DIGIPOT 1
178
+  #define HAS_MOTOR_CURRENT_I2C 1
179 179
 #endif
180 180
 
181 181
 // Multiple Z steppers

+ 1
- 1
Marlin/src/inc/Conditionals_post.h View File

@@ -1976,7 +1976,7 @@
1976 1976
   #define HAS_STEPPER_RESET 1
1977 1977
 #endif
1978 1978
 #if PIN_EXISTS(DIGIPOTSS)
1979
-  #define HAS_DIGIPOTSS 1
1979
+  #define HAS_MOTOR_CURRENT_SPI 1
1980 1980
 #endif
1981 1981
 #if ANY_PIN(MOTOR_CURRENT_PWM_X, MOTOR_CURRENT_PWM_Y, MOTOR_CURRENT_PWM_XY, MOTOR_CURRENT_PWM_Z, MOTOR_CURRENT_PWM_E)
1982 1982
   #define HAS_MOTOR_CURRENT_PWM 1

+ 1
- 1
Marlin/src/inc/SanityCheck.h View File

@@ -2667,7 +2667,7 @@ static_assert(hbm[Z_AXIS] >= 0, "HOMING_BUMP_MM.Z must be greater than or equal
2667 2667
 /**
2668 2668
  * Digipot requirement
2669 2669
  */
2670
-#if HAS_I2C_DIGIPOT
2670
+#if HAS_MOTOR_CURRENT_I2C
2671 2671
   #if BOTH(DIGIPOT_MCP4018, DIGIPOT_MCP4451)
2672 2672
     #error "Enable only one of DIGIPOT_MCP4018 or DIGIPOT_MCP4451."
2673 2673
   #elif !MB(MKS_SBASE) \

+ 4
- 4
Marlin/src/lcd/menu/menu_advanced.cpp View File

@@ -58,16 +58,16 @@
58 58
 void menu_tmc();
59 59
 void menu_backlash();
60 60
 
61
-#if ENABLED(DAC_STEPPER_CURRENT)
61
+#if ENABLED(HAS_MOTOR_CURRENT_DAC)
62 62
 
63 63
   #include "../../feature/dac/stepper_dac.h"
64 64
 
65 65
   void menu_dac() {
66 66
     static xyze_uint8_t driverPercent;
67
-    LOOP_XYZE(i) driverPercent[i] = dac_current_get_percent((AxisEnum)i);
67
+    LOOP_XYZE(i) driverPercent[i] = stepper_dac.get_current_percent((AxisEnum)i);
68 68
     START_MENU();
69 69
     BACK_ITEM(MSG_ADVANCED_SETTINGS);
70
-    #define EDIT_DAC_PERCENT(A) EDIT_ITEM(uint8, MSG_DAC_PERCENT_##A, &driverPercent[_AXIS(A)], 0, 100, []{ dac_current_set_percents(driverPercent); })
70
+    #define EDIT_DAC_PERCENT(A) EDIT_ITEM(uint8, MSG_DAC_PERCENT_##A, &driverPercent[_AXIS(A)], 0, 100, []{ stepper_dac.set_current_percents(driverPercent); })
71 71
     EDIT_DAC_PERCENT(X);
72 72
     EDIT_DAC_PERCENT(Y);
73 73
     EDIT_DAC_PERCENT(Z);
@@ -568,7 +568,7 @@ void menu_advanced_settings() {
568 568
     SUBMENU(MSG_BACKLASH, menu_backlash);
569 569
   #endif
570 570
 
571
-  #if ENABLED(DAC_STEPPER_CURRENT)
571
+  #if ENABLED(HAS_MOTOR_CURRENT_DAC)
572 572
     SUBMENU(MSG_DRIVE_STRENGTH, menu_dac);
573 573
   #endif
574 574
   #if HAS_MOTOR_CURRENT_PWM

+ 44
- 15
Marlin/src/module/settings.cpp View File

@@ -36,7 +36,7 @@
36 36
  */
37 37
 
38 38
 // Change EEPROM version if the structure changes
39
-#define EEPROM_VERSION "V81"
39
+#define EEPROM_VERSION "V82"
40 40
 #define EEPROM_OFFSET 100
41 41
 
42 42
 // Check the integrity of data offsets.
@@ -365,7 +365,10 @@ typedef struct SettingsDataStruct {
365 365
   //
366 366
   // HAS_MOTOR_CURRENT_PWM
367 367
   //
368
-  uint32_t motor_current_setting[3];                    // M907 X Z E
368
+  #ifndef MOTOR_CURRENT_COUNT
369
+    #define MOTOR_CURRENT_COUNT 3
370
+  #endif
371
+  uint32_t motor_current_setting[MOTOR_CURRENT_COUNT];  // M907 X Z E
369 372
 
370 373
   //
371 374
   // CNC_COORDINATE_SYSTEMS
@@ -1277,10 +1280,10 @@ void MarlinSettings::postprocess() {
1277 1280
     {
1278 1281
       _FIELD_TEST(motor_current_setting);
1279 1282
 
1280
-      #if HAS_MOTOR_CURRENT_PWM
1283
+      #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
1281 1284
         EEPROM_WRITE(stepper.motor_current_setting);
1282 1285
       #else
1283
-        const uint32_t no_current[3] = { 0 };
1286
+        const uint32_t no_current[MOTOR_CURRENT_COUNT] = { 0 };
1284 1287
         EEPROM_WRITE(no_current);
1285 1288
       #endif
1286 1289
     }
@@ -2110,10 +2113,16 @@ void MarlinSettings::postprocess() {
2110 2113
       // Motor Current PWM
2111 2114
       //
2112 2115
       {
2113
-        uint32_t motor_current_setting[3];
2114 2116
         _FIELD_TEST(motor_current_setting);
2117
+        uint32_t motor_current_setting[MOTOR_CURRENT_COUNT]
2118
+          #if HAS_MOTOR_CURRENT_SPI
2119
+             = DIGIPOT_MOTOR_CURRENT
2120
+          #endif
2121
+        ;
2122
+        DEBUG_ECHOLNPGM("DIGIPOTS Loading");
2115 2123
         EEPROM_READ(motor_current_setting);
2116
-        #if HAS_MOTOR_CURRENT_PWM
2124
+        DEBUG_ECHOLNPGM("DIGIPOTS Loaded");
2125
+        #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
2117 2126
           if (!validating)
2118 2127
             COPY(stepper.motor_current_setting, motor_current_setting);
2119 2128
         #endif
@@ -2791,9 +2800,20 @@ void MarlinSettings::reset() {
2791 2800
   //
2792 2801
 
2793 2802
   #if HAS_MOTOR_CURRENT_PWM
2794
-    constexpr uint32_t tmp_motor_current_setting[3] = PWM_MOTOR_CURRENT;
2795
-    LOOP_L_N(q, 3)
2796
-      stepper.digipot_current(q, (stepper.motor_current_setting[q] = tmp_motor_current_setting[q]));
2803
+    constexpr uint32_t tmp_motor_current_setting[MOTOR_CURRENT_COUNT] = PWM_MOTOR_CURRENT;
2804
+    LOOP_L_N(q, MOTOR_CURRENT_COUNT)
2805
+      stepper.set_digipot_current(q, (stepper.motor_current_setting[q] = tmp_motor_current_setting[q]));
2806
+  #endif
2807
+
2808
+  //
2809
+  // DIGIPOTS
2810
+  //
2811
+  #if HAS_MOTOR_CURRENT_SPI
2812
+    static constexpr uint32_t tmp_motor_current_setting[] = DIGIPOT_MOTOR_CURRENT;
2813
+    DEBUG_ECHOLNPGM("Writing Digipot");
2814
+    LOOP_L_N(q, COUNT(tmp_motor_current_setting))
2815
+      stepper.set_digipot_current(q, tmp_motor_current_setting[q]);
2816
+    DEBUG_ECHOLNPGM("Digipot Written");
2797 2817
   #endif
2798 2818
 
2799 2819
   //
@@ -3695,14 +3715,23 @@ void MarlinSettings::reset() {
3695 3715
       #endif
3696 3716
     #endif
3697 3717
 
3698
-    #if HAS_MOTOR_CURRENT_PWM
3718
+    #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
3699 3719
       CONFIG_ECHO_HEADING("Stepper motor currents:");
3700 3720
       CONFIG_ECHO_START();
3701
-      SERIAL_ECHOLNPAIR_P(
3702
-          PSTR("  M907 X"), stepper.motor_current_setting[0]
3703
-        , SP_Z_STR, stepper.motor_current_setting[1]
3704
-        , SP_E_STR, stepper.motor_current_setting[2]
3705
-      );
3721
+      #if HAS_MOTOR_CURRENT_PWM
3722
+        SERIAL_ECHOLNPAIR_P(
3723
+            PSTR("  M907 X"), stepper.motor_current_setting[0]
3724
+          , SP_Z_STR, stepper.motor_current_setting[1]
3725
+          , SP_E_STR, stepper.motor_current_setting[2]
3726
+        );
3727
+      #elif HAS_MOTOR_CURRENT_SPI
3728
+        SERIAL_ECHOPGM("  M907");
3729
+        LOOP_L_N(q, MOTOR_CURRENT_COUNT) {
3730
+          SERIAL_CHAR(' ');
3731
+          SERIAL_CHAR(axis_codes[q]);
3732
+          SERIAL_ECHO(stepper.motor_current_setting[q]);
3733
+        }
3734
+      #endif
3706 3735
     #endif
3707 3736
 
3708 3737
     /**

+ 26
- 29
Marlin/src/module/stepper.cpp View File

@@ -83,10 +83,6 @@ Stepper stepper; // Singleton
83 83
 
84 84
 #define BABYSTEPPING_EXTRA_DIR_WAIT
85 85
 
86
-#if HAS_MOTOR_CURRENT_PWM
87
-  bool Stepper::initialized; // = false
88
-#endif
89
-
90 86
 #ifdef __AVR__
91 87
   #include "speed_lookuptable.h"
92 88
 #endif
@@ -110,7 +106,7 @@ Stepper stepper; // Singleton
110 106
   #include "../feature/dac/dac_dac084s085.h"
111 107
 #endif
112 108
 
113
-#if HAS_DIGIPOTSS
109
+#if HAS_MOTOR_CURRENT_SPI
114 110
   #include <SPI.h>
115 111
 #endif
116 112
 
@@ -142,8 +138,12 @@ Stepper stepper; // Singleton
142 138
   bool Stepper::separate_multi_axis = false;
143 139
 #endif
144 140
 
145
-#if HAS_MOTOR_CURRENT_PWM
146
-  uint32_t Stepper::motor_current_setting[3]; // Initialized by settings.load()
141
+#if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
142
+  bool Stepper::initialized; // = false
143
+  uint32_t Stepper::motor_current_setting[MOTOR_CURRENT_COUNT]; // Initialized by settings.load()
144
+  #if HAS_MOTOR_CURRENT_SPI
145
+    constexpr uint32_t Stepper::digipot_count[];
146
+  #endif
147 147
 #endif
148 148
 
149 149
 // private:
@@ -2590,8 +2590,8 @@ void Stepper::init() {
2590 2590
 
2591 2591
   set_directions();
2592 2592
 
2593
-  #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
2594
-    TERN_(HAS_MOTOR_CURRENT_PWM, initialized = true);
2593
+  #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
2594
+    initialized = true;
2595 2595
     digipot_init();
2596 2596
   #endif
2597 2597
 }
@@ -2930,10 +2930,10 @@ void Stepper::report_positions() {
2930 2930
  * Software-controlled Stepper Motor Current
2931 2931
  */
2932 2932
 
2933
-#if HAS_DIGIPOTSS
2933
+#if HAS_MOTOR_CURRENT_SPI
2934 2934
 
2935 2935
   // From Arduino DigitalPotControl example
2936
-  void Stepper::digitalPotWrite(const int16_t address, const int16_t value) {
2936
+  void Stepper::set_digipot_value_spi(const int16_t address, const int16_t value) {
2937 2937
     WRITE(DIGIPOTSS_PIN, LOW);  // Take the SS pin low to select the chip
2938 2938
     SPI.transfer(address);      // Send the address and value via SPI
2939 2939
     SPI.transfer(value);
@@ -2941,7 +2941,7 @@ void Stepper::report_positions() {
2941 2941
     //delay(10);
2942 2942
   }
2943 2943
 
2944
-#endif // HAS_DIGIPOTSS
2944
+#endif // HAS_MOTOR_CURRENT_SPI
2945 2945
 
2946 2946
 #if HAS_MOTOR_CURRENT_PWM
2947 2947
 
@@ -2958,7 +2958,7 @@ void Stepper::report_positions() {
2958 2958
         #if ANY_PIN(MOTOR_CURRENT_PWM_E, MOTOR_CURRENT_PWM_E0, MOTOR_CURRENT_PWM_E1)
2959 2959
           case 2:
2960 2960
         #endif
2961
-            digipot_current(i, motor_current_setting[i]);
2961
+            set_digipot_current(i, motor_current_setting[i]);
2962 2962
         default: break;
2963 2963
       }
2964 2964
     }
@@ -2968,21 +2968,22 @@ void Stepper::report_positions() {
2968 2968
 
2969 2969
 #if !MB(PRINTRBOARD_G2)
2970 2970
 
2971
-  #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
2971
+  #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
2972 2972
 
2973
-    void Stepper::digipot_current(const uint8_t driver, const int16_t current) {
2973
+    void Stepper::set_digipot_current(const uint8_t driver, const int16_t current) {
2974
+      if (WITHIN(driver, 0, COUNT(motor_current_setting) - 1))
2975
+        motor_current_setting[driver] = current; // update motor_current_setting
2974 2976
 
2975
-      #if HAS_DIGIPOTSS
2977
+      if (!initialized) return;
2976 2978
 
2977
-        const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
2978
-        digitalPotWrite(digipot_ch[driver], current);
2979
+      #if HAS_MOTOR_CURRENT_SPI
2979 2980
 
2980
-      #elif HAS_MOTOR_CURRENT_PWM
2981
+        //SERIAL_ECHOLNPAIR("Digipotss current ", current);
2981 2982
 
2982
-        if (!initialized) return;
2983
+        const uint8_t digipot_ch[] = DIGIPOT_CHANNELS;
2984
+        set_digipot_value_spi(digipot_ch[driver], current);
2983 2985
 
2984
-        if (WITHIN(driver, 0, COUNT(motor_current_setting) - 1))
2985
-          motor_current_setting[driver] = current; // update motor_current_setting
2986
+      #elif HAS_MOTOR_CURRENT_PWM
2986 2987
 
2987 2988
         #define _WRITE_CURRENT_PWM(P) analogWrite(pin_t(MOTOR_CURRENT_PWM_## P ##_PIN), 255L * current / (MOTOR_CURRENT_PWM_RANGE))
2988 2989
         switch (driver) {
@@ -3019,17 +3020,13 @@ void Stepper::report_positions() {
3019 3020
 
3020 3021
     void Stepper::digipot_init() {
3021 3022
 
3022
-      #if HAS_DIGIPOTSS
3023
-
3024
-        static const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT;
3023
+      #if HAS_MOTOR_CURRENT_SPI
3025 3024
 
3026 3025
         SPI.begin();
3027 3026
         SET_OUTPUT(DIGIPOTSS_PIN);
3028 3027
 
3029
-        LOOP_L_N(i, COUNT(digipot_motor_current)) {
3030
-          //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]);
3031
-          digipot_current(i, digipot_motor_current[i]);
3032
-        }
3028
+        LOOP_L_N(i, COUNT(motor_current_setting))
3029
+          set_digipot_current(i, motor_current_setting[i]);
3033 3030
 
3034 3031
       #elif HAS_MOTOR_CURRENT_PWM
3035 3032
 

+ 14
- 8
Marlin/src/module/stepper.h View File

@@ -245,12 +245,18 @@ class Stepper {
245 245
       static bool separate_multi_axis;
246 246
     #endif
247 247
 
248
-    #if HAS_MOTOR_CURRENT_PWM
249
-      #ifndef PWM_MOTOR_CURRENT
250
-        #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT
248
+    #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
249
+      #if HAS_MOTOR_CURRENT_PWM
250
+        #ifndef PWM_MOTOR_CURRENT
251
+          #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT
252
+        #endif
253
+        #define MOTOR_CURRENT_COUNT 3
254
+      #elif HAS_MOTOR_CURRENT_SPI
255
+        static constexpr uint32_t digipot_count[] = DIGIPOT_MOTOR_CURRENT;
256
+        #define MOTOR_CURRENT_COUNT COUNT(Stepper::digipot_count)
251 257
       #endif
252
-      static uint32_t motor_current_setting[3];
253 258
       static bool initialized;
259
+      static uint32_t motor_current_setting[MOTOR_CURRENT_COUNT]; // Initialized by settings.load()
254 260
     #endif
255 261
 
256 262
     // Last-moved extruder, as set when the last movement was fetched from planner
@@ -457,9 +463,9 @@ class Stepper {
457 463
     // Triggered position of an axis in steps
458 464
     static int32_t triggered_position(const AxisEnum axis);
459 465
 
460
-    #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
461
-      static void digitalPotWrite(const int16_t address, const int16_t value);
462
-      static void digipot_current(const uint8_t driver, const int16_t current);
466
+    #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
467
+      static void set_digipot_value_spi(const int16_t address, const int16_t value);
468
+      static void set_digipot_current(const uint8_t driver, const int16_t current);
463 469
     #endif
464 470
 
465 471
     #if HAS_MICROSTEPS
@@ -582,7 +588,7 @@ class Stepper {
582 588
       static int32_t _eval_bezier_curve(const uint32_t curr_step);
583 589
     #endif
584 590
 
585
-    #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
591
+    #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
586 592
       static void digipot_init();
587 593
     #endif
588 594
 

+ 1
- 1
Marlin/src/pins/ramps/pins_RIGIDBOARD_V2.h View File

@@ -34,7 +34,7 @@
34 34
 //
35 35
 
36 36
 // I2C based DAC like on the Printrboard REVF
37
-#define DAC_STEPPER_CURRENT
37
+#define HAS_MOTOR_CURRENT_DAC
38 38
 
39 39
 // Channels available for DAC, For Rigidboard there are 4
40 40
 #define DAC_STEPPER_ORDER { 0, 1, 2, 3 }

+ 1
- 1
Marlin/src/pins/teensy2/pins_PRINTRBOARD_REVF.h View File

@@ -143,7 +143,7 @@
143 143
 #endif // NO_EXTRUDRBOARD
144 144
 
145 145
 // Enable control of stepper motor currents with the I2C based MCP4728 DAC used on Printrboard REVF
146
-#define DAC_STEPPER_CURRENT
146
+#define HAS_MOTOR_CURRENT_DAC
147 147
 
148 148
 // Set default drive strength percents if not already defined - X, Y, Z, E axis
149 149
 #ifndef DAC_MOTOR_CURRENT_DEFAULT

+ 2
- 2
platformio.ini View File

@@ -212,7 +212,7 @@ HAS_TRINAMIC_CONFIG     = TMCStepper@~0.7.1
212 212
                           src_filter=+<src/feature/tmc_util.cpp> +<src/module/stepper/trinamic.cpp> +<src/gcode/feature/trinamic/M122.cpp> +<src/gcode/feature/trinamic/M906.cpp> +<src/gcode/feature/trinamic/M911-M914.cpp>
213 213
 HAS_STEALTHCHOP         = src_filter=+<src/gcode/feature/trinamic/M569.cpp>
214 214
 SR_LCD_3W_NL            = SailfishLCD=https://github.com/mikeshub/SailfishLCD/archive/master.zip
215
-HAS_I2C_DIGIPOT         = SlowSoftI2CMaster
215
+HAS_MOTOR_CURRENT_I2C   = SlowSoftI2CMaster
216 216
                           src_filter=+<src/feature/digipot>
217 217
 HAS_TMC26X              = TMC26XStepper=https://github.com/trinamic/TMC26XStepper/archive/master.zip
218 218
                           src_filter=+<src/module/TMC26X.cpp>
@@ -278,7 +278,7 @@ CANCEL_OBJECTS          = src_filter=+<src/feature/cancel_object.cpp> +<src/gcod
278 278
 CASE_LIGHT_ENABLE       = src_filter=+<src/feature/caselight.cpp> +<src/gcode/feature/caselight>
279 279
 EXTERNAL_CLOSED_LOOP_CONTROLLER = src_filter=+<src/feature/closedloop.cpp> +<src/gcode/calibrate/M12.cpp>
280 280
 USE_CONTROLLER_FAN      = src_filter=+<src/feature/controllerfan.cpp>
281
-DAC_STEPPER_CURRENT     = src_filter=+<src/feature/dac>
281
+HAS_MOTOR_CURRENT_DAC   = src_filter=+<src/feature/dac>
282 282
 DIRECT_STEPPING         = src_filter=+<src/feature/direct_stepping.cpp> +<src/gcode/motion/G6.cpp>
283 283
 EMERGENCY_PARSER        = src_filter=+<src/feature/e_parser.cpp> -<src/gcode/control/M108_*.cpp>
284 284
 I2C_POSITION_ENCODERS   = src_filter=+<src/feature/encoder_i2c.cpp>

Loading…
Cancel
Save