|
@ -22,7 +22,7 @@ |
|
|
|
|
|
|
|
|
#include "../../inc/MarlinConfig.h" |
|
|
#include "../../inc/MarlinConfig.h" |
|
|
|
|
|
|
|
|
#if BOTH(DIGIPOT_I2C, DIGIPOT_MCP4018) |
|
|
#if ENABLED(DIGIPOT_MCP4018) |
|
|
|
|
|
|
|
|
#include <Stream.h> |
|
|
#include <Stream.h> |
|
|
#include <SlowSoftI2CMaster.h> // https://github.com/stawel/SlowSoftI2CMaster |
|
|
#include <SlowSoftI2CMaster.h> // https://github.com/stawel/SlowSoftI2CMaster |
|
@ -31,44 +31,37 @@ |
|
|
|
|
|
|
|
|
#define DIGIPOT_A4988_Rsx 0.250 |
|
|
#define DIGIPOT_A4988_Rsx 0.250 |
|
|
#define DIGIPOT_A4988_Vrefmax 1.666 |
|
|
#define DIGIPOT_A4988_Vrefmax 1.666 |
|
|
#define DIGIPOT_A4988_MAX_VALUE 127 |
|
|
#define DIGIPOT_MCP4018_MAX_VALUE 127 |
|
|
|
|
|
|
|
|
#define DIGIPOT_A4988_Itripmax(Vref) ((Vref) / (8.0 * DIGIPOT_A4988_Rsx)) |
|
|
#define DIGIPOT_A4988_Itripmax(Vref) ((Vref) / (8.0 * DIGIPOT_A4988_Rsx)) |
|
|
|
|
|
|
|
|
#define DIGIPOT_A4988_FACTOR ((DIGIPOT_A4988_MAX_VALUE)/DIGIPOT_A4988_Itripmax(DIGIPOT_A4988_Vrefmax)) |
|
|
#define DIGIPOT_A4988_FACTOR ((DIGIPOT_MCP4018_MAX_VALUE) / DIGIPOT_A4988_Itripmax(DIGIPOT_A4988_Vrefmax)) |
|
|
#define DIGIPOT_A4988_MAX_CURRENT 2.0 |
|
|
#define DIGIPOT_A4988_MAX_CURRENT 2.0 |
|
|
|
|
|
|
|
|
static byte current_to_wiper(const float current) { |
|
|
static byte current_to_wiper(const float current) { |
|
|
const int16_t value = ceil(float(DIGIPOT_A4988_FACTOR) * current); |
|
|
const int16_t value = TERN(DIGIPOT_USE_RAW_VALUES, current, CEIL(current * DIGIPOT_A4988_FACTOR)); |
|
|
return byte(constrain(value, 0, DIGIPOT_A4988_MAX_VALUE)); |
|
|
return byte(constrain(value, 0, DIGIPOT_MCP4018_MAX_VALUE)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const uint8_t sda_pins[DIGIPOT_I2C_NUM_CHANNELS] = { |
|
|
static SlowSoftI2CMaster pots[DIGIPOT_I2C_NUM_CHANNELS] = { |
|
|
DIGIPOTS_I2C_SDA_X |
|
|
SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_X, DIGIPOTS_I2C_SCL) |
|
|
#if DIGIPOT_I2C_NUM_CHANNELS > 1 |
|
|
#if DIGIPOT_I2C_NUM_CHANNELS > 1 |
|
|
, DIGIPOTS_I2C_SDA_Y |
|
|
, SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_Y, DIGIPOTS_I2C_SCL) |
|
|
#if DIGIPOT_I2C_NUM_CHANNELS > 2 |
|
|
#if DIGIPOT_I2C_NUM_CHANNELS > 2 |
|
|
, DIGIPOTS_I2C_SDA_Z |
|
|
, SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_Z, DIGIPOTS_I2C_SCL) |
|
|
#if DIGIPOT_I2C_NUM_CHANNELS > 3 |
|
|
#if DIGIPOT_I2C_NUM_CHANNELS > 3 |
|
|
, DIGIPOTS_I2C_SDA_E0 |
|
|
, SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_E0, DIGIPOTS_I2C_SCL) |
|
|
#if DIGIPOT_I2C_NUM_CHANNELS > 4 |
|
|
#if DIGIPOT_I2C_NUM_CHANNELS > 4 |
|
|
, DIGIPOTS_I2C_SDA_E1 |
|
|
, SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_E1, DIGIPOTS_I2C_SCL) |
|
|
#endif |
|
|
#if DIGIPOT_I2C_NUM_CHANNELS > 5 |
|
|
|
|
|
, SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_E2, DIGIPOTS_I2C_SCL) |
|
|
|
|
|
#if DIGIPOT_I2C_NUM_CHANNELS > 6 |
|
|
|
|
|
, SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_E3, DIGIPOTS_I2C_SCL) |
|
|
|
|
|
#if DIGIPOT_I2C_NUM_CHANNELS > 7 |
|
|
|
|
|
, SlowSoftI2CMaster(DIGIPOTS_I2C_SDA_E4, DIGIPOTS_I2C_SCL) |
|
|
#endif |
|
|
#endif |
|
|
#endif |
|
|
#endif |
|
|
#endif |
|
|
#endif |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
static SlowSoftI2CMaster pots[DIGIPOT_I2C_NUM_CHANNELS] = { |
|
|
|
|
|
SlowSoftI2CMaster { sda_pins[X_AXIS], DIGIPOTS_I2C_SCL } |
|
|
|
|
|
#if DIGIPOT_I2C_NUM_CHANNELS > 1 |
|
|
|
|
|
, SlowSoftI2CMaster { sda_pins[Y_AXIS], DIGIPOTS_I2C_SCL } |
|
|
|
|
|
#if DIGIPOT_I2C_NUM_CHANNELS > 2 |
|
|
|
|
|
, SlowSoftI2CMaster { sda_pins[Z_AXIS], DIGIPOTS_I2C_SCL } |
|
|
|
|
|
#if DIGIPOT_I2C_NUM_CHANNELS > 3 |
|
|
|
|
|
, SlowSoftI2CMaster { sda_pins[E_AXIS], DIGIPOTS_I2C_SCL } |
|
|
|
|
|
#if DIGIPOT_I2C_NUM_CHANNELS > 4 |
|
|
|
|
|
, SlowSoftI2CMaster { sda_pins[E_AXIS + 1], DIGIPOTS_I2C_SCL } |
|
|
|
|
|
#endif |
|
|
#endif |
|
|
#endif |
|
|
#endif |
|
|
#endif |
|
|
#endif |
|
@ -85,18 +78,23 @@ static void i2c_send(const uint8_t channel, const byte v) { |
|
|
|
|
|
|
|
|
// This is for the MCP4018 I2C based digipot
|
|
|
// This is for the MCP4018 I2C based digipot
|
|
|
void digipot_i2c_set_current(const uint8_t channel, const float current) { |
|
|
void digipot_i2c_set_current(const uint8_t channel, const float current) { |
|
|
i2c_send(channel, current_to_wiper(_MIN(_MAX(current, 0), float(DIGIPOT_A4988_MAX_CURRENT)))); |
|
|
const float ival = _MIN(_MAX(current, 0), float(DIGIPOT_MCP4018_MAX_VALUE)); |
|
|
|
|
|
i2c_send(channel, current_to_wiper(ival)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void digipot_i2c_init() { |
|
|
void digipot_i2c_init() { |
|
|
static const float digipot_motor_current[] PROGMEM = DIGIPOT_I2C_MOTOR_CURRENTS; |
|
|
LOOP_L_N(i, DIGIPOT_I2C_NUM_CHANNELS) pots[i].i2c_init(); |
|
|
|
|
|
|
|
|
LOOP_L_N(i, DIGIPOT_I2C_NUM_CHANNELS) |
|
|
// Init currents according to Configuration_adv.h
|
|
|
pots[i].i2c_init(); |
|
|
static const float digipot_motor_current[] PROGMEM = |
|
|
|
|
|
#if ENABLED(DIGIPOT_USE_RAW_VALUES) |
|
|
// setup initial currents as defined in Configuration_adv.h
|
|
|
DIGIPOT_MOTOR_CURRENT |
|
|
|
|
|
#else |
|
|
|
|
|
DIGIPOT_I2C_MOTOR_CURRENTS |
|
|
|
|
|
#endif |
|
|
|
|
|
; |
|
|
LOOP_L_N(i, COUNT(digipot_motor_current)) |
|
|
LOOP_L_N(i, COUNT(digipot_motor_current)) |
|
|
digipot_i2c_set_current(i, pgm_read_float(&digipot_motor_current[i])); |
|
|
digipot_i2c_set_current(i, pgm_read_float(&digipot_motor_current[i])); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#endif // DIGIPOT_I2C && DIGIPOT_MCP4018
|
|
|
#endif // DIGIPOT_MCP4018
|
|
|