|
@@ -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
|