Browse Source

Digipots refactor / cleanup (#19690)

Scott Lahteine 4 years ago
parent
commit
beb17d8855

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

154
   NVIC_SetPriority(PWM_IRQn, NVIC_EncodePriority(0, 10, 0));  // normal priority for PWM module (can stand some jitter on the Vref signals)
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
   if (!(PWM->PWM_CH_NUM[0].PWM_CPRD == PWM_PERIOD_US)) digipot_init();  // Init PWM system if needed
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
 //
191
 //
192
 // Flag any i2c pin conflicts
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
   #define USEDI2CDEV_M 1  // <Arduino>/Wire.cpp
195
   #define USEDI2CDEV_M 1  // <Arduino>/Wire.cpp
196
 
196
 
197
   #if USEDI2CDEV_M == 0         // P0_27 [D57] (AUX-1) .......... P0_28 [D58] (AUX-1)
197
   #if USEDI2CDEV_M == 0         // P0_27 [D57] (AUX-1) .......... P0_28 [D58] (AUX-1)

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

97
   #include "feature/closedloop.h"
97
   #include "feature/closedloop.h"
98
 #endif
98
 #endif
99
 
99
 
100
-#if HAS_I2C_DIGIPOT
100
+#if HAS_MOTOR_CURRENT_I2C
101
   #include "feature/digipot/digipot.h"
101
   #include "feature/digipot/digipot.h"
102
 #endif
102
 #endif
103
 
103
 
125
   #include "module/servo.h"
125
   #include "module/servo.h"
126
 #endif
126
 #endif
127
 
127
 
128
-#if ENABLED(DAC_STEPPER_CURRENT)
128
+#if ENABLED(HAS_MOTOR_CURRENT_DAC)
129
   #include "feature/dac/stepper_dac.h"
129
   #include "feature/dac/stepper_dac.h"
130
 #endif
130
 #endif
131
 
131
 
1137
     SETUP_RUN(enableStepperDrivers());
1137
     SETUP_RUN(enableStepperDrivers());
1138
   #endif
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
   #endif
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
   #endif
1146
   #endif
1147
 
1147
 
1148
   #if EITHER(Z_PROBE_SLED, SOLENOID_PROBE) && HAS_SOLENOID_1
1148
   #if EITHER(Z_PROBE_SLED, SOLENOID_PROBE) && HAS_SOLENOID_1

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

32
 
32
 
33
 #include "../../inc/MarlinConfig.h"
33
 #include "../../inc/MarlinConfig.h"
34
 
34
 
35
-#if ENABLED(DAC_STEPPER_CURRENT)
35
+#if ENABLED(HAS_MOTOR_CURRENT_DAC)
36
 
36
 
37
 #include "dac_mcp4728.h"
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
  * Begin I2C, get current values (input register and eeprom) of mcp4728
44
  * Begin I2C, get current values (input register and eeprom) of mcp4728
43
  */
45
  */
44
-void mcp4728_init() {
46
+void MCP4728::init() {
45
   Wire.begin();
47
   Wire.begin();
46
   Wire.requestFrom(I2C_ADDRESS(DAC_DEV_ADDRESS), uint8_t(24));
48
   Wire.requestFrom(I2C_ADDRESS(DAC_DEV_ADDRESS), uint8_t(24));
47
   while (Wire.available()) {
49
   while (Wire.available()) {
50
          loByte = Wire.read();
52
          loByte = Wire.read();
51
 
53
 
52
     if (!(deviceID & 0x08))
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
  * Write input resister value to specified channel using fastwrite method.
60
  * Write input resister value to specified channel using fastwrite method.
59
  * Channel : 0-3, Values : 0-4095
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
  * This will update both input register and EEPROM value
70
  * This will update both input register and EEPROM value
69
  * This will also write current Vref, PowerDown, Gain settings to EEPROM
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
   Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
74
   Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
73
   Wire.write(SEQWRITE);
75
   Wire.write(SEQWRITE);
74
   LOOP_XYZE(i) {
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
   return Wire.endTransmission();
80
   return Wire.endTransmission();
79
 }
81
 }
81
 /**
83
 /**
82
  * Write Voltage reference setting to all input regiters
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
   Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
87
   Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
86
   Wire.write(VREFWRITE | (value ? 0x0F : 0x00));
88
   Wire.write(VREFWRITE | (value ? 0x0F : 0x00));
87
   return Wire.endTransmission();
89
   return Wire.endTransmission();
89
 /**
91
 /**
90
  * Write Gain setting to all input regiters
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
   Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
95
   Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
94
   Wire.write(GAINWRITE | (value ? 0x0F : 0x00));
96
   Wire.write(GAINWRITE | (value ? 0x0F : 0x00));
95
   return Wire.endTransmission();
97
   return Wire.endTransmission();
98
 /**
100
 /**
99
  * Return Input Register value
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
 #if 0
105
 #if 0
104
 /**
106
 /**
105
  * Steph: Might be useful in the future
107
  * Steph: Might be useful in the future
106
  * Return Vout
108
  * Return Vout
107
  */
109
  */
108
-uint16_t mcp4728_getVout(const uint8_t channel) {
110
+uint16_t MCP4728::getVout(const uint8_t channel) {
109
   const uint32_t vref = 2048,
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
   return _MIN(vOut, defaultVDD);
113
   return _MIN(vOut, defaultVDD);
112
 }
114
 }
113
 #endif
115
 #endif
115
 /**
117
 /**
116
  * Returns DAC values as a 0-100 percentage of drive strength
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
  * Receives all Drive strengths as 0-100 percent values, updates
123
  * Receives all Drive strengths as 0-100 percent values, updates
122
  * DAC Values array and calls fastwrite to update the DAC.
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
  * DAC Input and PowerDown bits update.
133
  * DAC Input and PowerDown bits update.
132
  * No EEPROM update
134
  * No EEPROM update
133
  */
135
  */
134
-uint8_t mcp4728_fastWrite() {
136
+uint8_t MCP4728::fastWrite() {
135
   Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
137
   Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS));
136
   LOOP_XYZE(i) {
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
   return Wire.endTransmission();
142
   return Wire.endTransmission();
141
 }
143
 }
143
 /**
145
 /**
144
  * Common function for simple general commands
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
   Wire.beginTransmission(I2C_ADDRESS(GENERALCALL));
149
   Wire.beginTransmission(I2C_ADDRESS(GENERALCALL));
148
   Wire.write(simpleCommand);
150
   Wire.write(simpleCommand);
149
   return Wire.endTransmission();
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
 // DAC_OR_ADDRESS defined in pins_BOARD.h  file
65
 // DAC_OR_ADDRESS defined in pins_BOARD.h  file
66
 #define DAC_DEV_ADDRESS (BASE_ADDR | DAC_OR_ADDRESS)
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
 
26
 
27
 #include "../../inc/MarlinConfig.h"
27
 #include "../../inc/MarlinConfig.h"
28
 
28
 
29
-#if ENABLED(DAC_STEPPER_CURRENT)
29
+#if ENABLED(HAS_MOTOR_CURRENT_DAC)
30
 
30
 
31
 #include "stepper_dac.h"
31
 #include "stepper_dac.h"
32
 #include "../../MarlinCore.h" // for SP_X_LBL...
32
 #include "../../MarlinCore.h" // for SP_X_LBL...
35
 constexpr xyze_uint8_t dac_order = DAC_STEPPER_ORDER;
35
 constexpr xyze_uint8_t dac_order = DAC_STEPPER_ORDER;
36
 xyze_uint8_t dac_channel_pct = DAC_MOTOR_CURRENT_DEFAULT;
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
   #if PIN_EXISTS(DAC_DISABLE)
41
   #if PIN_EXISTS(DAC_DISABLE)
40
     OUT_WRITE(DAC_DISABLE_PIN, LOW);  // set pin low to enable DAC
42
     OUT_WRITE(DAC_DISABLE_PIN, LOW);  // set pin low to enable DAC
41
   #endif
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
   dac_present = true;
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
   return 0;
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
   if (!dac_present) return;
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
   LOOP_XYZE(i) dac_channel_pct[i] = pct[dac_order[i]];
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
   if (!dac_present) return;
85
   if (!dac_present) return;
89
   SERIAL_ECHO_MSG("Stepper current values in % (Amps):");
86
   SERIAL_ECHO_MSG("Stepper current values in % (Amps):");
90
   SERIAL_ECHO_START();
87
   SERIAL_ECHO_START();
94
   SERIAL_ECHOLNPAIR_P(SP_E_LBL, dac_perc(E_AXIS), PSTR(" ("), dac_amps(E_AXIS), PSTR(")"));
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
   if (!dac_present) return;
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
 
27
 
28
 #include "dac_mcp4728.h"
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
  */
21
  */
22
 #pragma once
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
 
24
 
25
 #if ENABLED(DIGIPOT_MCP4018)
25
 #if ENABLED(DIGIPOT_MCP4018)
26
 
26
 
27
+#include "digipot.h"
28
+
27
 #include <Stream.h>
29
 #include <Stream.h>
28
 #include <SlowSoftI2CMaster.h>  // https://github.com/stawel/SlowSoftI2CMaster
30
 #include <SlowSoftI2CMaster.h>  // https://github.com/stawel/SlowSoftI2CMaster
29
 
31
 
68
   #endif
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
   if (WITHIN(channel, 0, DIGIPOT_I2C_NUM_CHANNELS - 1)) {
74
   if (WITHIN(channel, 0, DIGIPOT_I2C_NUM_CHANNELS - 1)) {
73
     pots[channel].i2c_start(((DIGIPOT_I2C_ADDRESS_A) << 1) | I2C_WRITE);
75
     pots[channel].i2c_start(((DIGIPOT_I2C_ADDRESS_A) << 1) | I2C_WRITE);
74
     pots[channel].i2c_write(v);
76
     pots[channel].i2c_write(v);
77
 }
79
 }
78
 
80
 
79
 // This is for the MCP4018 I2C based digipot
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
   const float ival = _MIN(_MAX(current, 0), float(DIGIPOT_MCP4018_MAX_VALUE));
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
   LOOP_L_N(i, DIGIPOT_I2C_NUM_CHANNELS) pots[i].i2c_init();
88
   LOOP_L_N(i, DIGIPOT_I2C_NUM_CHANNELS) pots[i].i2c_init();
87
 
89
 
88
   // Init currents according to Configuration_adv.h
90
   // Init currents according to Configuration_adv.h
94
     #endif
96
     #endif
95
   ;
97
   ;
96
   LOOP_L_N(i, COUNT(digipot_motor_current))
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
 #endif // DIGIPOT_MCP4018
102
 #endif // DIGIPOT_MCP4018

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

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

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

22
 
22
 
23
 #include "../../../inc/MarlinConfig.h"
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
 #include "../../gcode.h"
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
   #include "../../../module/stepper.h"
30
   #include "../../../module/stepper.h"
31
 #endif
31
 #endif
32
 
32
 
33
-#if HAS_I2C_DIGIPOT
33
+#if HAS_MOTOR_CURRENT_I2C
34
   #include "../../../feature/digipot/digipot.h"
34
   #include "../../../feature/digipot/digipot.h"
35
 #endif
35
 #endif
36
 
36
 
37
-#if ENABLED(DAC_STEPPER_CURRENT)
37
+#if ENABLED(HAS_MOTOR_CURRENT_DAC)
38
   #include "../../../feature/dac/stepper_dac.h"
38
   #include "../../../feature/dac/stepper_dac.h"
39
 #endif
39
 #endif
40
 
40
 
42
  * M907: Set digital trimpot motor current using axis codes X, Y, Z, E, B, S
42
  * M907: Set digital trimpot motor current using axis codes X, Y, Z, E, B, S
43
  */
43
  */
44
 void GcodeSuite::M907() {
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
   #elif HAS_MOTOR_CURRENT_PWM
51
   #elif HAS_MOTOR_CURRENT_PWM
52
 
52
 
53
     #if ANY_PIN(MOTOR_CURRENT_PWM_X, MOTOR_CURRENT_PWM_Y, MOTOR_CURRENT_PWM_XY)
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
     #endif
55
     #endif
56
     #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
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
     #endif
58
     #endif
59
     #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
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
     #endif
61
     #endif
62
 
62
 
63
   #endif
63
   #endif
64
 
64
 
65
-  #if HAS_I2C_DIGIPOT
65
+  #if HAS_MOTOR_CURRENT_I2C
66
     // this one uses actual amps in floating point
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
     // Additional extruders use B,C,D for channels 4,5,6.
68
     // Additional extruders use B,C,D for channels 4,5,6.
69
     // TODO: Change these parameters because 'E' is used. B<index>?
69
     // TODO: Change these parameters because 'E' is used. B<index>?
70
     for (uint8_t i = E_AXIS + 1; i < DIGIPOT_I2C_NUM_CHANNELS; i++)
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
   #endif
72
   #endif
73
 
73
 
74
-  #if ENABLED(DAC_STEPPER_CURRENT)
74
+  #if ENABLED(HAS_MOTOR_CURRENT_DAC)
75
     if (parser.seenval('S')) {
75
     if (parser.seenval('S')) {
76
       const float dac_percent = parser.value_float();
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
   #endif
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
    * M908: Control digital trimpot directly (M908 P<pin> S<current>)
86
    * M908: Control digital trimpot directly (M908 P<pin> S<current>)
87
    */
87
    */
88
   void GcodeSuite::M908() {
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
         case 900: M900(); break;                                  // M900: Set advance K factor.
823
         case 900: M900(); break;                                  // M900: Set advance K factor.
824
       #endif
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
         case 907: M907(); break;                                  // M907: Set digital trimpot motor current using axis codes.
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
           case 908: M908(); break;                                // M908: Control digital trimpot directly.
829
           case 908: M908(); break;                                // M908: Control digital trimpot directly.
830
-          #if ENABLED(DAC_STEPPER_CURRENT)
830
+          #if ENABLED(HAS_MOTOR_CURRENT_DAC)
831
             case 909: M909(); break;                              // M909: Print digipot/DAC current value
831
             case 909: M909(); break;                              // M909: Print digipot/DAC current value
832
             case 910: M910(); break;                              // M910: Commit digipot/DAC value to external EEPROM
832
             case 910: M910(); break;                              // M910: Commit digipot/DAC value to external EEPROM
833
           #endif
833
           #endif

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

258
  * M900 - Get or Set Linear Advance K-factor. (Requires LIN_ADVANCE)
258
  * M900 - Get or Set Linear Advance K-factor. (Requires LIN_ADVANCE)
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)
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
  * M907 - Set digital trimpot motor current using axis codes. (Requires a board with digital trimpots)
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
  * M911 - Report stepper driver overtemperature pre-warn condition. (Requires at least one _DRIVER_TYPE defined as TMC2130/2160/5130/5160/2208/2209/2660)
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
  * M912 - Clear stepper driver overtemperature pre-warn condition flag. (Requires at least one _DRIVER_TYPE defined as TMC2130/2160/5130/5160/2208/2209/2660)
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
  * M913 - Set HYBRID_THRESHOLD speed. (Requires HYBRID_THRESHOLD)
266
  * M913 - Set HYBRID_THRESHOLD speed. (Requires HYBRID_THRESHOLD)
848
     static void M918();
848
     static void M918();
849
   #endif
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
     static void M907();
852
     static void M907();
853
-    #if EITHER(HAS_DIGIPOTSS, DAC_STEPPER_CURRENT)
853
+    #if EITHER(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_DAC)
854
       static void M908();
854
       static void M908();
855
-      #if ENABLED(DAC_STEPPER_CURRENT)
855
+      #if ENABLED(HAS_MOTOR_CURRENT_DAC)
856
         static void M909();
856
         static void M909();
857
         static void M910();
857
         static void M910();
858
       #endif
858
       #endif

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

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

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

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

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

2667
 /**
2667
 /**
2668
  * Digipot requirement
2668
  * Digipot requirement
2669
  */
2669
  */
2670
-#if HAS_I2C_DIGIPOT
2670
+#if HAS_MOTOR_CURRENT_I2C
2671
   #if BOTH(DIGIPOT_MCP4018, DIGIPOT_MCP4451)
2671
   #if BOTH(DIGIPOT_MCP4018, DIGIPOT_MCP4451)
2672
     #error "Enable only one of DIGIPOT_MCP4018 or DIGIPOT_MCP4451."
2672
     #error "Enable only one of DIGIPOT_MCP4018 or DIGIPOT_MCP4451."
2673
   #elif !MB(MKS_SBASE) \
2673
   #elif !MB(MKS_SBASE) \

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

58
 void menu_tmc();
58
 void menu_tmc();
59
 void menu_backlash();
59
 void menu_backlash();
60
 
60
 
61
-#if ENABLED(DAC_STEPPER_CURRENT)
61
+#if ENABLED(HAS_MOTOR_CURRENT_DAC)
62
 
62
 
63
   #include "../../feature/dac/stepper_dac.h"
63
   #include "../../feature/dac/stepper_dac.h"
64
 
64
 
65
   void menu_dac() {
65
   void menu_dac() {
66
     static xyze_uint8_t driverPercent;
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
     START_MENU();
68
     START_MENU();
69
     BACK_ITEM(MSG_ADVANCED_SETTINGS);
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
     EDIT_DAC_PERCENT(X);
71
     EDIT_DAC_PERCENT(X);
72
     EDIT_DAC_PERCENT(Y);
72
     EDIT_DAC_PERCENT(Y);
73
     EDIT_DAC_PERCENT(Z);
73
     EDIT_DAC_PERCENT(Z);
568
     SUBMENU(MSG_BACKLASH, menu_backlash);
568
     SUBMENU(MSG_BACKLASH, menu_backlash);
569
   #endif
569
   #endif
570
 
570
 
571
-  #if ENABLED(DAC_STEPPER_CURRENT)
571
+  #if ENABLED(HAS_MOTOR_CURRENT_DAC)
572
     SUBMENU(MSG_DRIVE_STRENGTH, menu_dac);
572
     SUBMENU(MSG_DRIVE_STRENGTH, menu_dac);
573
   #endif
573
   #endif
574
   #if HAS_MOTOR_CURRENT_PWM
574
   #if HAS_MOTOR_CURRENT_PWM

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

36
  */
36
  */
37
 
37
 
38
 // Change EEPROM version if the structure changes
38
 // Change EEPROM version if the structure changes
39
-#define EEPROM_VERSION "V81"
39
+#define EEPROM_VERSION "V82"
40
 #define EEPROM_OFFSET 100
40
 #define EEPROM_OFFSET 100
41
 
41
 
42
 // Check the integrity of data offsets.
42
 // Check the integrity of data offsets.
365
   //
365
   //
366
   // HAS_MOTOR_CURRENT_PWM
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
   // CNC_COORDINATE_SYSTEMS
374
   // CNC_COORDINATE_SYSTEMS
1277
     {
1280
     {
1278
       _FIELD_TEST(motor_current_setting);
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
         EEPROM_WRITE(stepper.motor_current_setting);
1284
         EEPROM_WRITE(stepper.motor_current_setting);
1282
       #else
1285
       #else
1283
-        const uint32_t no_current[3] = { 0 };
1286
+        const uint32_t no_current[MOTOR_CURRENT_COUNT] = { 0 };
1284
         EEPROM_WRITE(no_current);
1287
         EEPROM_WRITE(no_current);
1285
       #endif
1288
       #endif
1286
     }
1289
     }
2110
       // Motor Current PWM
2113
       // Motor Current PWM
2111
       //
2114
       //
2112
       {
2115
       {
2113
-        uint32_t motor_current_setting[3];
2114
         _FIELD_TEST(motor_current_setting);
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
         EEPROM_READ(motor_current_setting);
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
           if (!validating)
2126
           if (!validating)
2118
             COPY(stepper.motor_current_setting, motor_current_setting);
2127
             COPY(stepper.motor_current_setting, motor_current_setting);
2119
         #endif
2128
         #endif
2791
   //
2800
   //
2792
 
2801
 
2793
   #if HAS_MOTOR_CURRENT_PWM
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
   #endif
2817
   #endif
2798
 
2818
 
2799
   //
2819
   //
3695
       #endif
3715
       #endif
3696
     #endif
3716
     #endif
3697
 
3717
 
3698
-    #if HAS_MOTOR_CURRENT_PWM
3718
+    #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
3699
       CONFIG_ECHO_HEADING("Stepper motor currents:");
3719
       CONFIG_ECHO_HEADING("Stepper motor currents:");
3700
       CONFIG_ECHO_START();
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
     #endif
3735
     #endif
3707
 
3736
 
3708
     /**
3737
     /**

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

83
 
83
 
84
 #define BABYSTEPPING_EXTRA_DIR_WAIT
84
 #define BABYSTEPPING_EXTRA_DIR_WAIT
85
 
85
 
86
-#if HAS_MOTOR_CURRENT_PWM
87
-  bool Stepper::initialized; // = false
88
-#endif
89
-
90
 #ifdef __AVR__
86
 #ifdef __AVR__
91
   #include "speed_lookuptable.h"
87
   #include "speed_lookuptable.h"
92
 #endif
88
 #endif
110
   #include "../feature/dac/dac_dac084s085.h"
106
   #include "../feature/dac/dac_dac084s085.h"
111
 #endif
107
 #endif
112
 
108
 
113
-#if HAS_DIGIPOTSS
109
+#if HAS_MOTOR_CURRENT_SPI
114
   #include <SPI.h>
110
   #include <SPI.h>
115
 #endif
111
 #endif
116
 
112
 
142
   bool Stepper::separate_multi_axis = false;
138
   bool Stepper::separate_multi_axis = false;
143
 #endif
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
 #endif
147
 #endif
148
 
148
 
149
 // private:
149
 // private:
2590
 
2590
 
2591
   set_directions();
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
     digipot_init();
2595
     digipot_init();
2596
   #endif
2596
   #endif
2597
 }
2597
 }
2930
  * Software-controlled Stepper Motor Current
2930
  * Software-controlled Stepper Motor Current
2931
  */
2931
  */
2932
 
2932
 
2933
-#if HAS_DIGIPOTSS
2933
+#if HAS_MOTOR_CURRENT_SPI
2934
 
2934
 
2935
   // From Arduino DigitalPotControl example
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
     WRITE(DIGIPOTSS_PIN, LOW);  // Take the SS pin low to select the chip
2937
     WRITE(DIGIPOTSS_PIN, LOW);  // Take the SS pin low to select the chip
2938
     SPI.transfer(address);      // Send the address and value via SPI
2938
     SPI.transfer(address);      // Send the address and value via SPI
2939
     SPI.transfer(value);
2939
     SPI.transfer(value);
2941
     //delay(10);
2941
     //delay(10);
2942
   }
2942
   }
2943
 
2943
 
2944
-#endif // HAS_DIGIPOTSS
2944
+#endif // HAS_MOTOR_CURRENT_SPI
2945
 
2945
 
2946
 #if HAS_MOTOR_CURRENT_PWM
2946
 #if HAS_MOTOR_CURRENT_PWM
2947
 
2947
 
2958
         #if ANY_PIN(MOTOR_CURRENT_PWM_E, MOTOR_CURRENT_PWM_E0, MOTOR_CURRENT_PWM_E1)
2958
         #if ANY_PIN(MOTOR_CURRENT_PWM_E, MOTOR_CURRENT_PWM_E0, MOTOR_CURRENT_PWM_E1)
2959
           case 2:
2959
           case 2:
2960
         #endif
2960
         #endif
2961
-            digipot_current(i, motor_current_setting[i]);
2961
+            set_digipot_current(i, motor_current_setting[i]);
2962
         default: break;
2962
         default: break;
2963
       }
2963
       }
2964
     }
2964
     }
2968
 
2968
 
2969
 #if !MB(PRINTRBOARD_G2)
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
         #define _WRITE_CURRENT_PWM(P) analogWrite(pin_t(MOTOR_CURRENT_PWM_## P ##_PIN), 255L * current / (MOTOR_CURRENT_PWM_RANGE))
2988
         #define _WRITE_CURRENT_PWM(P) analogWrite(pin_t(MOTOR_CURRENT_PWM_## P ##_PIN), 255L * current / (MOTOR_CURRENT_PWM_RANGE))
2988
         switch (driver) {
2989
         switch (driver) {
3019
 
3020
 
3020
     void Stepper::digipot_init() {
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
         SPI.begin();
3025
         SPI.begin();
3027
         SET_OUTPUT(DIGIPOTSS_PIN);
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
       #elif HAS_MOTOR_CURRENT_PWM
3031
       #elif HAS_MOTOR_CURRENT_PWM
3035
 
3032
 

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

245
       static bool separate_multi_axis;
245
       static bool separate_multi_axis;
246
     #endif
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
       #endif
257
       #endif
252
-      static uint32_t motor_current_setting[3];
253
       static bool initialized;
258
       static bool initialized;
259
+      static uint32_t motor_current_setting[MOTOR_CURRENT_COUNT]; // Initialized by settings.load()
254
     #endif
260
     #endif
255
 
261
 
256
     // Last-moved extruder, as set when the last movement was fetched from planner
262
     // Last-moved extruder, as set when the last movement was fetched from planner
457
     // Triggered position of an axis in steps
463
     // Triggered position of an axis in steps
458
     static int32_t triggered_position(const AxisEnum axis);
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
     #endif
469
     #endif
464
 
470
 
465
     #if HAS_MICROSTEPS
471
     #if HAS_MICROSTEPS
582
       static int32_t _eval_bezier_curve(const uint32_t curr_step);
588
       static int32_t _eval_bezier_curve(const uint32_t curr_step);
583
     #endif
589
     #endif
584
 
590
 
585
-    #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM
591
+    #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM
586
       static void digipot_init();
592
       static void digipot_init();
587
     #endif
593
     #endif
588
 
594
 

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

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

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

143
 #endif // NO_EXTRUDRBOARD
143
 #endif // NO_EXTRUDRBOARD
144
 
144
 
145
 // Enable control of stepper motor currents with the I2C based MCP4728 DAC used on Printrboard REVF
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
 // Set default drive strength percents if not already defined - X, Y, Z, E axis
148
 // Set default drive strength percents if not already defined - X, Y, Z, E axis
149
 #ifndef DAC_MOTOR_CURRENT_DEFAULT
149
 #ifndef DAC_MOTOR_CURRENT_DEFAULT

+ 2
- 2
platformio.ini View File

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>
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
 HAS_STEALTHCHOP         = src_filter=+<src/gcode/feature/trinamic/M569.cpp>
213
 HAS_STEALTHCHOP         = src_filter=+<src/gcode/feature/trinamic/M569.cpp>
214
 SR_LCD_3W_NL            = SailfishLCD=https://github.com/mikeshub/SailfishLCD/archive/master.zip
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
                           src_filter=+<src/feature/digipot>
216
                           src_filter=+<src/feature/digipot>
217
 HAS_TMC26X              = TMC26XStepper=https://github.com/trinamic/TMC26XStepper/archive/master.zip
217
 HAS_TMC26X              = TMC26XStepper=https://github.com/trinamic/TMC26XStepper/archive/master.zip
218
                           src_filter=+<src/module/TMC26X.cpp>
218
                           src_filter=+<src/module/TMC26X.cpp>
278
 CASE_LIGHT_ENABLE       = src_filter=+<src/feature/caselight.cpp> +<src/gcode/feature/caselight>
278
 CASE_LIGHT_ENABLE       = src_filter=+<src/feature/caselight.cpp> +<src/gcode/feature/caselight>
279
 EXTERNAL_CLOSED_LOOP_CONTROLLER = src_filter=+<src/feature/closedloop.cpp> +<src/gcode/calibrate/M12.cpp>
279
 EXTERNAL_CLOSED_LOOP_CONTROLLER = src_filter=+<src/feature/closedloop.cpp> +<src/gcode/calibrate/M12.cpp>
280
 USE_CONTROLLER_FAN      = src_filter=+<src/feature/controllerfan.cpp>
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
 DIRECT_STEPPING         = src_filter=+<src/feature/direct_stepping.cpp> +<src/gcode/motion/G6.cpp>
282
 DIRECT_STEPPING         = src_filter=+<src/feature/direct_stepping.cpp> +<src/gcode/motion/G6.cpp>
283
 EMERGENCY_PARSER        = src_filter=+<src/feature/e_parser.cpp> -<src/gcode/control/M108_*.cpp>
283
 EMERGENCY_PARSER        = src_filter=+<src/feature/e_parser.cpp> -<src/gcode/control/M108_*.cpp>
284
 I2C_POSITION_ENCODERS   = src_filter=+<src/feature/encoder_i2c.cpp>
284
 I2C_POSITION_ENCODERS   = src_filter=+<src/feature/encoder_i2c.cpp>

Loading…
Cancel
Save