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,7 +782,7 @@ extern "C" {
782 782
 #endif // !SDSUPPORT
783 783
 
784 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 786
   extern void digipot_i2c_init();
787 787
 #endif
788 788
 

+ 10
- 0
Marlin/SanityCheck.h View File

@@ -1166,6 +1166,16 @@ static_assert(1 >= 0
1166 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 1179
  * Require 4 or more elements in per-axis initializers
1170 1180
  */
1171 1181
 constexpr float sanity_arr_1[] = DEFAULT_AXIS_STEPS_PER_UNIT,

+ 36
- 21
Marlin/digipot_mcp4018.cpp View File

@@ -24,6 +24,7 @@
24 24
 
25 25
 #if ENABLED(DIGIPOT_I2C) && ENABLED(DIGIPOT_MCP4018)
26 26
 
27
+#include "enum.h"
27 28
 #include "Stream.h"
28 29
 #include "utility/twi.h"
29 30
 #include <SlowSoftI2CMaster.h>  //https://github.com/stawel/SlowSoftI2CMaster
@@ -38,31 +39,47 @@
38 39
 
39 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 43
 //TODO: MAX_CURRENT -0.5A ?? (currently set to 2A, max possible current 2.5A)
43 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 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 83
   if (WITHIN(channel, 0, DIGIPOT_I2C_NUM_CHANNELS - 1)) {
67 84
     pots[channel].i2c_start(((DIGIPOT_I2C_ADDRESS) << 1) | I2C_WRITE);
68 85
     pots[channel].i2c_write(v);
@@ -71,21 +88,19 @@ static void i2c_send(int channel, byte v) {
71 88
 }
72 89
 
73 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 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 98
   for (uint8_t i = 0; i < DIGIPOT_I2C_NUM_CHANNELS; i++)
84 99
     pots[i].i2c_init();
85 100
 
86 101
   // setup initial currents as defined in Configuration_adv.h
87 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 106
 #endif // DIGIPOT_I2C && DIGIPOT_MCP4018

+ 6
- 6
Marlin/digipot_mcp4451.cpp View File

@@ -37,11 +37,11 @@
37 37
   #define DIGIPOT_I2C_MAX_CURRENT 2.5
38 38
 #endif
39 39
 
40
-static byte current_to_wiper(float current) {
40
+static byte current_to_wiper(const float current) {
41 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 45
   Wire.beginTransmission(addr);
46 46
   Wire.write(a);
47 47
   Wire.write(b);
@@ -49,7 +49,7 @@ static void i2c_send(byte addr, byte a, byte b) {
49 49
 }
50 50
 
51 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 53
   current = min((float) max(current, 0.0f), DIGIPOT_I2C_MAX_CURRENT);
54 54
   // these addresses are specific to Azteeg X3 Pro, can be set to others,
55 55
   // In this case first digipot is at address A0=0, A1= 0, second one is at A0=0, A1= 1
@@ -69,11 +69,11 @@ void digipot_i2c_set_current(int channel, float current) {
69 69
 }
70 70
 
71 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 73
   Wire.begin();
74 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 79
 #endif // DIGIPOT_I2C

Loading…
Cancel
Save