Browse Source

Save lots of PROGMEM, ~20b SRAM with DIGIPOT_I2C

Scott Lahteine 7 years ago
parent
commit
c9e3caf928
4 changed files with 53 additions and 28 deletions
  1. 1
    1
      Marlin/Marlin_main.cpp
  2. 10
    0
      Marlin/SanityCheck.h
  3. 36
    21
      Marlin/digipot_mcp4018.cpp
  4. 6
    6
      Marlin/digipot_mcp4451.cpp

+ 1
- 1
Marlin/Marlin_main.cpp View File

782
 #endif // !SDSUPPORT
782
 #endif // !SDSUPPORT
783
 
783
 
784
 #if ENABLED(DIGIPOT_I2C)
784
 #if ENABLED(DIGIPOT_I2C)
785
-  extern void digipot_i2c_set_current(int channel, float current);
785
+  extern void digipot_i2c_set_current(uint8_t channel, float current);
786
   extern void digipot_i2c_init();
786
   extern void digipot_i2c_init();
787
 #endif
787
 #endif
788
 
788
 

+ 10
- 0
Marlin/SanityCheck.h View File

1166
 #endif
1166
 #endif
1167
 
1167
 
1168
 /**
1168
 /**
1169
+ * Digipot requirement
1170
+ */
1171
+#if ENABLED(DIGIPOT_MCP4018)
1172
+  #if !defined(DIGIPOTS_I2C_SDA_X) || !defined(DIGIPOTS_I2C_SDA_Y) || !defined(DIGIPOTS_I2C_SDA_Z) \
1173
+    || !defined(DIGIPOTS_I2C_SDA_E0) || !defined(DIGIPOTS_I2C_SDA_E1)
1174
+      #error "DIGIPOT_MCP4018 requires DIGIPOTS_I2C_SDA_* pins to be defined."
1175
+  #endif
1176
+#endif
1177
+
1178
+/**
1169
  * Require 4 or more elements in per-axis initializers
1179
  * Require 4 or more elements in per-axis initializers
1170
  */
1180
  */
1171
 constexpr float sanity_arr_1[] = DEFAULT_AXIS_STEPS_PER_UNIT,
1181
 constexpr float sanity_arr_1[] = DEFAULT_AXIS_STEPS_PER_UNIT,

+ 36
- 21
Marlin/digipot_mcp4018.cpp View File

24
 
24
 
25
 #if ENABLED(DIGIPOT_I2C) && ENABLED(DIGIPOT_MCP4018)
25
 #if ENABLED(DIGIPOT_I2C) && ENABLED(DIGIPOT_MCP4018)
26
 
26
 
27
+#include "enum.h"
27
 #include "Stream.h"
28
 #include "Stream.h"
28
 #include "utility/twi.h"
29
 #include "utility/twi.h"
29
 #include <SlowSoftI2CMaster.h>  //https://github.com/stawel/SlowSoftI2CMaster
30
 #include <SlowSoftI2CMaster.h>  //https://github.com/stawel/SlowSoftI2CMaster
38
 
39
 
39
 #define DIGIPOT_A4988_Itripmax(Vref)    ((Vref)/(8.0*DIGIPOT_A4988_Rsx))
40
 #define DIGIPOT_A4988_Itripmax(Vref)    ((Vref)/(8.0*DIGIPOT_A4988_Rsx))
40
 
41
 
41
-#define DIGIPOT_A4988_FACTOR            (DIGIPOT_A4988_MAX_VALUE/DIGIPOT_A4988_Itripmax(DIGIPOT_A4988_Vrefmax))
42
+#define DIGIPOT_A4988_FACTOR            ((DIGIPOT_A4988_MAX_VALUE)/DIGIPOT_A4988_Itripmax(DIGIPOT_A4988_Vrefmax))
42
 //TODO: MAX_CURRENT -0.5A ?? (currently set to 2A, max possible current 2.5A)
43
 //TODO: MAX_CURRENT -0.5A ?? (currently set to 2A, max possible current 2.5A)
43
 #define DIGIPOT_A4988_MAX_CURRENT       (DIGIPOT_A4988_Itripmax(DIGIPOT_A4988_Vrefmax) - 0.5)
44
 #define DIGIPOT_A4988_MAX_CURRENT       (DIGIPOT_A4988_Itripmax(DIGIPOT_A4988_Vrefmax) - 0.5)
44
 
45
 
45
-static byte current_to_wiper(float current) {
46
-  return byte(ceil(float((DIGIPOT_A4988_FACTOR * current))));
46
+static byte current_to_wiper(const float current) {
47
+  return byte(ceil(float(DIGIPOT_A4988_FACTOR) * current));
47
 }
48
 }
48
 
49
 
49
-static uint8_t sda_pins[DIGIPOT_I2C_NUM_CHANNELS] = {
50
-  DIGIPOTS_I2C_SDA_X,
51
-  DIGIPOTS_I2C_SDA_Y,
52
-  DIGIPOTS_I2C_SDA_Z,
53
-  DIGIPOTS_I2C_SDA_E0,
54
-  DIGIPOTS_I2C_SDA_E1,
50
+const uint8_t sda_pins[DIGIPOT_I2C_NUM_CHANNELS] = {
51
+  DIGIPOTS_I2C_SDA_X
52
+  #if DIGIPOT_I2C_NUM_CHANNELS > 1
53
+    , DIGIPOTS_I2C_SDA_Y
54
+    #if DIGIPOT_I2C_NUM_CHANNELS > 2
55
+      , DIGIPOTS_I2C_SDA_Z
56
+      #if DIGIPOT_I2C_NUM_CHANNELS > 3
57
+        , DIGIPOTS_I2C_SDA_E0
58
+        #if DIGIPOT_I2C_NUM_CHANNELS > 4
59
+          , DIGIPOTS_I2C_SDA_E1
60
+        #endif
61
+      #endif
62
+    #endif
63
+  #endif
55
 };
64
 };
56
 
65
 
57
 static SlowSoftI2CMaster pots[DIGIPOT_I2C_NUM_CHANNELS] = {
66
 static SlowSoftI2CMaster pots[DIGIPOT_I2C_NUM_CHANNELS] = {
58
-  SlowSoftI2CMaster { sda_pins[0], DIGIPOTS_I2C_SCL },
59
-  SlowSoftI2CMaster { sda_pins[1], DIGIPOTS_I2C_SCL },
60
-  SlowSoftI2CMaster { sda_pins[2], DIGIPOTS_I2C_SCL },
61
-  SlowSoftI2CMaster { sda_pins[3], DIGIPOTS_I2C_SCL },
62
-  SlowSoftI2CMaster { sda_pins[4], DIGIPOTS_I2C_SCL }
67
+  SlowSoftI2CMaster { sda_pins[X_AXIS], DIGIPOTS_I2C_SCL }
68
+  #if DIGIPOT_I2C_NUM_CHANNELS > 1
69
+    , SlowSoftI2CMaster { sda_pins[Y_AXIS], DIGIPOTS_I2C_SCL }
70
+    #if DIGIPOT_I2C_NUM_CHANNELS > 2
71
+      , SlowSoftI2CMaster { sda_pins[Z_AXIS], DIGIPOTS_I2C_SCL }
72
+      #if DIGIPOT_I2C_NUM_CHANNELS > 3
73
+        , SlowSoftI2CMaster { sda_pins[E_AXIS], DIGIPOTS_I2C_SCL }
74
+        #if DIGIPOT_I2C_NUM_CHANNELS > 4
75
+          , SlowSoftI2CMaster { sda_pins[E_AXIS + 1], DIGIPOTS_I2C_SCL }
76
+        #endif
77
+      #endif
78
+    #endif
79
+  #endif
63
 };
80
 };
64
 
81
 
65
-static void i2c_send(int channel, byte v) {
82
+static void i2c_send(const uint8_t channel, const byte v) {
66
   if (WITHIN(channel, 0, DIGIPOT_I2C_NUM_CHANNELS - 1)) {
83
   if (WITHIN(channel, 0, DIGIPOT_I2C_NUM_CHANNELS - 1)) {
67
     pots[channel].i2c_start(((DIGIPOT_I2C_ADDRESS) << 1) | I2C_WRITE);
84
     pots[channel].i2c_start(((DIGIPOT_I2C_ADDRESS) << 1) | I2C_WRITE);
68
     pots[channel].i2c_write(v);
85
     pots[channel].i2c_write(v);
71
 }
88
 }
72
 
89
 
73
 // This is for the MCP4018 I2C based digipot
90
 // This is for the MCP4018 I2C based digipot
74
-void digipot_i2c_set_current(int channel, float current) {
75
-  current = min(max(current, 0.0f), float(DIGIPOT_A4988_MAX_CURRENT));
76
-
77
-  i2c_send(channel, current_to_wiper(current));
91
+void digipot_i2c_set_current(uint8_t channel, float current) {
92
+  i2c_send(channel, current_to_wiper(min(max(current, 0.0f), float(DIGIPOT_A4988_MAX_CURRENT))));
78
 }
93
 }
79
 
94
 
80
 void digipot_i2c_init() {
95
 void digipot_i2c_init() {
81
-  const float digipot_motor_current[] = DIGIPOT_I2C_MOTOR_CURRENTS;
96
+  static const float digipot_motor_current[] PROGMEM = DIGIPOT_I2C_MOTOR_CURRENTS;
82
 
97
 
83
   for (uint8_t i = 0; i < DIGIPOT_I2C_NUM_CHANNELS; i++)
98
   for (uint8_t i = 0; i < DIGIPOT_I2C_NUM_CHANNELS; i++)
84
     pots[i].i2c_init();
99
     pots[i].i2c_init();
85
 
100
 
86
   // setup initial currents as defined in Configuration_adv.h
101
   // setup initial currents as defined in Configuration_adv.h
87
   for (uint8_t i = 0; i < COUNT(digipot_motor_current); i++)
102
   for (uint8_t i = 0; i < COUNT(digipot_motor_current); i++)
88
-    digipot_i2c_set_current(i, digipot_motor_current[i]);
103
+    digipot_i2c_set_current(i, pgm_read_float(&digipot_motor_current[i]));
89
 }
104
 }
90
 
105
 
91
 #endif // DIGIPOT_I2C && DIGIPOT_MCP4018
106
 #endif // DIGIPOT_I2C && DIGIPOT_MCP4018

+ 6
- 6
Marlin/digipot_mcp4451.cpp View File

37
   #define DIGIPOT_I2C_MAX_CURRENT 2.5
37
   #define DIGIPOT_I2C_MAX_CURRENT 2.5
38
 #endif
38
 #endif
39
 
39
 
40
-static byte current_to_wiper(float current) {
40
+static byte current_to_wiper(const float current) {
41
   return byte(ceil(float((DIGIPOT_I2C_FACTOR * current))));
41
   return byte(ceil(float((DIGIPOT_I2C_FACTOR * current))));
42
 }
42
 }
43
 
43
 
44
-static void i2c_send(byte addr, byte a, byte b) {
44
+static void i2c_send(const byte addr, const byte a, const byte b) {
45
   Wire.beginTransmission(addr);
45
   Wire.beginTransmission(addr);
46
   Wire.write(a);
46
   Wire.write(a);
47
   Wire.write(b);
47
   Wire.write(b);
49
 }
49
 }
50
 
50
 
51
 // This is for the MCP4451 I2C based digipot
51
 // This is for the MCP4451 I2C based digipot
52
-void digipot_i2c_set_current(int channel, float current) {
52
+void digipot_i2c_set_current(uint8_t channel, float current) {
53
   current = min((float) max(current, 0.0f), DIGIPOT_I2C_MAX_CURRENT);
53
   current = min((float) max(current, 0.0f), DIGIPOT_I2C_MAX_CURRENT);
54
   // these addresses are specific to Azteeg X3 Pro, can be set to others,
54
   // these addresses are specific to Azteeg X3 Pro, can be set to others,
55
   // In this case first digipot is at address A0=0, A1= 0, second one is at A0=0, A1= 1
55
   // In this case first digipot is at address A0=0, A1= 0, second one is at A0=0, A1= 1
69
 }
69
 }
70
 
70
 
71
 void digipot_i2c_init() {
71
 void digipot_i2c_init() {
72
-  const float digipot_motor_current[] = DIGIPOT_I2C_MOTOR_CURRENTS;
72
+  static const float digipot_motor_current[] PROGMEM = DIGIPOT_I2C_MOTOR_CURRENTS;
73
   Wire.begin();
73
   Wire.begin();
74
   // setup initial currents as defined in Configuration_adv.h
74
   // setup initial currents as defined in Configuration_adv.h
75
-  for (int i = 0; i < COUNT(digipot_motor_current); i++)
76
-    digipot_i2c_set_current(i, digipot_motor_current[i]);
75
+  for (uint8_t i = 0; i < COUNT(digipot_motor_current); i++)
76
+    digipot_i2c_set_current(i, pgm_read_float(&digipot_motor_current[i]));
77
 }
77
 }
78
 
78
 
79
 #endif // DIGIPOT_I2C
79
 #endif // DIGIPOT_I2C

Loading…
Cancel
Save